🐛 fix(utils): correct string elision boundary calculation
- ensure elision respects character boundaries for utf-8 strings - prevent panics when eliding near multi-byte characters
This commit is contained in:
committed by
Jeremiah Russell
parent
4e4e11f20d
commit
558b2cfd7f
10
src/utils.rs
10
src/utils.rs
@@ -46,9 +46,17 @@ impl Elide for String {
|
|||||||
if self.len() <= to {
|
if self.len() <= to {
|
||||||
self
|
self
|
||||||
} else {
|
} else {
|
||||||
let range = to - 4;
|
let range = get_start_boundary(self.clone(), to - 4);
|
||||||
self.replace_range(range.., " ...");
|
self.replace_range(range.., " ...");
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_start_boundary(string: String, mut start: usize) -> usize {
|
||||||
|
while !string.is_char_boundary(start) {
|
||||||
|
start -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
start
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user