text_processing: use grapheme length in Truncate
parent
d9b568cfb4
commit
651fda1467
|
@ -14,10 +14,18 @@ pub trait Truncate {
|
||||||
|
|
||||||
impl Truncate for &mut String {
|
impl Truncate for &mut String {
|
||||||
fn truncate_at_boundary(self, mut new_len: usize) {
|
fn truncate_at_boundary(self, mut new_len: usize) {
|
||||||
while new_len > 0 && !self.is_char_boundary(new_len) {
|
if new_len >= self.len() {
|
||||||
new_len -= 1;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern crate unicode_segmentation;
|
||||||
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
if let Some((last, _)) = UnicodeSegmentation::grapheme_indices(self.as_str(), true)
|
||||||
|
.take(new_len)
|
||||||
|
.last()
|
||||||
|
{
|
||||||
|
String::truncate(self, last);
|
||||||
}
|
}
|
||||||
String::truncate(self, new_len);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue