From a87e7828ffc6e5c36f2a2003ad2fae8010ade7bd Mon Sep 17 00:00:00 2001 From: djairoh Date: Wed, 10 May 2023 14:32:06 +0200 Subject: [PATCH] fixed a bug with non-utf8 characters in output --- src/print_text.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/print_text.rs b/src/print_text.rs index f185915..4abfc87 100644 --- a/src/print_text.rs +++ b/src/print_text.rs @@ -5,17 +5,29 @@ use string_builder::Builder; use crate::structs::{config::{Field, Config}, data::Data}; -fn fuzzy_cutoff(str: &str) -> usize{ +fn fuzzy_cutoff(str: &str) -> usize { str.rfind(char::is_whitespace).unwrap_or(usize::MAX) } +fn get_char_boundary(str: &str, max_len: usize) -> usize { + match str.is_char_boundary(max_len) { + true => max_len, + false => { + let mut idx = max_len; + while !str.is_char_boundary(idx) { + idx -= 1; + } + idx + }, + } +} + fn cutoff(fields: &Vec, brk: Option, fuzzy: bool, strings: &mut HashMap) { for field in fields { if let Some(str) = strings.get_mut(&field.field) { if !field.field.eq("xesam:userRating") && str.len() >= field.num_chars as usize { - str.truncate(field.num_chars as usize); + str.truncate(get_char_boundary(str, field.num_chars as usize)); if fuzzy {str.truncate(fuzzy_cutoff(str))} - // The above crashes on non-utf8 characters. FIXME: do something about that if let Some(c) = brk { str.push(c); }