fix(guess_width): slice strings using byte- instead of char indices
This commit is contained in:
parent
21a6a392a3
commit
2765eec9d4
|
@ -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> {
|
fn split(line: &str, pos: &[usize], trim_space: bool) -> Vec<String> {
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
let mut start = 0;
|
let mut start_char = 0;
|
||||||
let mut columns = Vec::with_capacity(pos.len() + 1);
|
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;
|
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 {
|
if pos.is_empty() || n > pos.len() - 1 {
|
||||||
start = p;
|
start_char = p;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if pos[n] <= w {
|
if pos[n] <= w {
|
||||||
let end = separator_position(&lr, p, pos, n);
|
let end_char = separator_position(&line_chars, p, pos, n);
|
||||||
if start > end {
|
if start_char > end_char {
|
||||||
break;
|
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 };
|
let col = if trim_space { col.trim() } else { col };
|
||||||
columns.push(col.to_string());
|
columns.push(col.to_string());
|
||||||
n += 1;
|
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.
|
// add last part.
|
||||||
let col = &line[start..];
|
let col = &line[line_char_boundaries[start_char]..];
|
||||||
let col = if trim_space { col.trim() } else { col };
|
let col = if trim_space { col.trim() } else { col };
|
||||||
columns.push(col.to_string());
|
columns.push(col.to_string());
|
||||||
columns
|
columns
|
||||||
|
|
Loading…
Reference in New Issue
Block a user