Skip to content

Commit

Permalink
don't panic when truncating non-utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyandrews committed Oct 30, 2023
1 parent e7aea3b commit 2c8618b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,15 @@ pub fn median(
/// // All characters are returned as the string is less than 15 characters long.
/// assert_eq!(util::truncate_string("shorter string", 15), "shorter string");
/// ```
pub fn truncate_string(str_to_truncate: &str, max_length: u64) -> String {
let mut string_to_truncate = str_to_truncate.to_string();
if string_to_truncate.len() as u64 > max_length {
let truncated_length = max_length - 2;
string_to_truncate.truncate(truncated_length as usize);
string_to_truncate += "..";
pub fn truncate_string(str_to_truncate: &str, max_length: usize) -> String {
if str_to_truncate.char_indices().count() > max_length {
match str_to_truncate.char_indices().nth(max_length - 2) {
None => str_to_truncate.to_string(),
Some((idx, _)) => format!("{}..", &str_to_truncate[..idx]),
}
} else {
str_to_truncate.to_string()
}
string_to_truncate
}

/// Determine if a timer expired, with second granularity.
Expand Down Expand Up @@ -561,6 +562,8 @@ mod tests {
assert_eq!(truncate_string("abcde", 4), "ab..");
assert_eq!(truncate_string("abcde", 3), "a..");
assert_eq!(truncate_string("abcde", 2), "..");
assert_eq!(truncate_string("これはテストだ", 10), "これはテストだ");
assert_eq!(truncate_string("これはテストだ", 3), "こ..");
}

#[tokio::test]
Expand Down

0 comments on commit 2c8618b

Please sign in to comment.