text_processing: use grapheme length in Truncate

async
Manos Pitsidianakis 2019-12-12 11:01:13 +02:00
parent d9b568cfb4
commit 651fda1467
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 11 additions and 3 deletions

View File

@ -14,10 +14,18 @@ pub trait Truncate {
impl Truncate for &mut String {
fn truncate_at_boundary(self, mut new_len: usize) {
while new_len > 0 && !self.is_char_boundary(new_len) {
new_len -= 1;
if new_len >= self.len() {
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);
}
}