fix cutting commit msg in between utf8 clusters (#188)

This commit is contained in:
Stephan Dilly 2020-07-09 08:51:31 +02:00
parent 7995519383
commit cb3b968e3a

View File

@ -103,7 +103,11 @@ pub fn get_message(
fn limit_str(s: &str, limit: usize) -> &str {
if let Some(first) = s.lines().next() {
&first[0..limit.min(first.len())]
let mut limit = limit.min(first.len());
while !first.is_char_boundary(limit) {
limit += 1
}
&first[0..limit]
} else {
""
}
@ -112,7 +116,7 @@ fn limit_str(s: &str, limit: usize) -> &str {
#[cfg(test)]
mod tests {
use super::get_commits_info;
use super::{get_commits_info, limit_str};
use crate::error::Result;
use crate::sync::{
commit, stage_add_file, tests::repo_init_empty,
@ -171,4 +175,9 @@ mod tests {
Ok(())
}
#[test]
fn test_limit_string_utf8() {
assert_eq!(limit_str("里里", 1), "");
}
}