Fix crash on unicode characters on line boundaries

Fixes #74
pull/77/head
Wilfred Hughes 2021-12-11 15:58:07 +07:00
parent 740a82e6fc
commit 447ed2c14d
2 changed files with 21 additions and 3 deletions

@ -7,6 +7,8 @@ variable DFT_WIDTH.
Fixed terminal width calculations on Windows. Fixed terminal width calculations on Windows.
Fixed crash on displaying unicode characters on line boundaries.
### Build ### Build
Fixed some build issues on Windows. Fixed some build issues on Windows.

@ -45,9 +45,9 @@ fn split_string(s: &str, max_len: usize) -> Vec<String> {
let mut res = vec![]; let mut res = vec![];
let mut s = s; let mut s = s;
while s.len() > max_len { while codepoint_len(s) > max_len {
res.push(s[..max_len].into()); res.push(substring_by_codepoint(s, 0, max_len).into());
s = &s[max_len..]; s = substring_by_codepoint(s, max_len, codepoint_len(s));
} }
if res.is_empty() || 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 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"]);
}
}