diff --git a/CHANGELOG.md b/CHANGELOG.md index 14e0c1d79..6776fda2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ variable DFT_WIDTH. Fixed terminal width calculations on Windows. +Fixed crash on displaying unicode characters on line boundaries. + ### Build Fixed some build issues on Windows. diff --git a/src/style.rs b/src/style.rs index d6e216827..79f185be5 100644 --- a/src/style.rs +++ b/src/style.rs @@ -45,9 +45,9 @@ fn split_string(s: &str, max_len: usize) -> Vec { let mut res = vec![]; let mut s = s; - while s.len() > max_len { - res.push(s[..max_len].into()); - s = &s[max_len..]; + while codepoint_len(s) > max_len { + res.push(substring_by_codepoint(s, 0, max_len).into()); + s = substring_by_codepoint(s, max_len, codepoint_len(s)); } if res.is_empty() || s != "" { @@ -283,3 +283,19 @@ pub fn header(file_name: &str, hunk_num: usize, hunk_total: usize, language_name language_name ) } + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + + #[test] + fn split_string_simple() { + assert_eq!(split_string("fooba", 3), vec!["foo", "ba "]); + } + + #[test] + fn split_string_unicode() { + assert_eq!(split_string("ab📦def", 3), vec!["ab📦", "def"]); + } +}