🐛 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:
Jeremiah Russell
2025-10-11 10:02:20 +01:00
committed by Jeremiah Russell
parent 4e4e11f20d
commit 558b2cfd7f

View File

@@ -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
}