mirror of
https://git.meli.delivery/meli/meli
synced 2024-11-19 03:25:38 +00:00
text_processing: use grapheme length in Truncate
This commit is contained in:
parent
d9b568cfb4
commit
651fda1467
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user