From cb3b968e3ada47c1c1c64d4d87ac343eee452dd8 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Thu, 9 Jul 2020 08:51:31 +0200 Subject: [PATCH] fix cutting commit msg in between utf8 clusters (#188) --- asyncgit/src/sync/commits_info.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/asyncgit/src/sync/commits_info.rs b/asyncgit/src/sync/commits_info.rs index 741ea30f..ae92808e 100644 --- a/asyncgit/src/sync/commits_info.rs +++ b/asyncgit/src/sync/commits_info.rs @@ -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), "里"); + } }