fix(guess_width): slice strings using byte- instead of char indices

This commit is contained in:
alex-tdrn 2024-06-29 21:18:16 +02:00
parent 21a6a392a3
commit 2765eec9d4
No known key found for this signature in database

View File

@ -175,34 +175,34 @@ fn separator_position(lr: &[char], p: usize, pos: &[usize], n: usize) -> usize {
fn split(line: &str, pos: &[usize], trim_space: bool) -> Vec<String> {
let mut n = 0;
let mut start = 0;
let mut start_char = 0;
let mut columns = Vec::with_capacity(pos.len() + 1);
let lr: Vec<char> = line.chars().collect();
let (line_char_boundaries, line_chars): (Vec<usize>, Vec<char>) = line.char_indices().unzip();
let mut w = 0;
for p in 0..lr.len() {
for p in 0..line_char_boundaries.len() {
if pos.is_empty() || n > pos.len() - 1 {
start = p;
start_char = p;
break;
}
if pos[n] <= w {
let end = separator_position(&lr, p, pos, n);
if start > end {
let end_char = separator_position(&line_chars, p, pos, n);
if start_char > end_char {
break;
}
let col = &line[start..end];
let col = &line[line_char_boundaries[start_char]..line_char_boundaries[end_char]];
let col = if trim_space { col.trim() } else { col };
columns.push(col.to_string());
n += 1;
start = end;
start_char = end_char;
}
w += UnicodeWidthStr::width(lr[p].to_string().as_str());
w += UnicodeWidthStr::width(line_chars[p].to_string().as_str());
}
// add last part.
let col = &line[start..];
let col = &line[line_char_boundaries[start_char]..];
let col = if trim_space { col.trim() } else { col };
columns.push(col.to_string());
columns