Skip to content

Commit bf6ee32

Browse files
committed
Improve string truncation to handle Unicode chars
Replaces byte-based string truncation with a new `truncate_chars` function that truncates by Unicode scalar values, ensuring proper handling of multi-byte characters and appending ellipsis when needed.
1 parent 0626de7 commit bf6ee32

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

langcodec-cli/src/view.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
use langcodec::Codec;
22

3+
/// Truncate a string by Unicode scalar values (chars),
4+
/// appending ellipsis if content exceeds `max_chars`.
5+
fn truncate_chars(s: &str, max_chars: usize) -> String {
6+
let mut iter = s.chars();
7+
let truncated: String = iter.by_ref().take(max_chars).collect();
8+
if iter.next().is_some() {
9+
format!("{}...", truncated)
10+
} else {
11+
truncated
12+
}
13+
}
14+
315
/// Print a view of the resources in a codec.
416
pub fn print_view(codec: &Codec, lang_filter: &Option<String>, full: bool) {
517
println!("Processing resources...");
@@ -60,11 +72,7 @@ pub fn print_view(codec: &Codec, lang_filter: &Option<String>, full: bool) {
6072
if full {
6173
println!(" Value: {}", value);
6274
} else {
63-
let truncated = if value.len() > 50 {
64-
format!("{}...", &value[..50])
65-
} else {
66-
value.clone()
67-
};
75+
let truncated = truncate_chars(value, 50);
6876
println!(" Value: {}", truncated);
6977
}
7078
}
@@ -75,11 +83,7 @@ pub fn print_view(codec: &Codec, lang_filter: &Option<String>, full: bool) {
7583
if full {
7684
println!(" {:?}: {}", category, value);
7785
} else {
78-
let truncated = if value.len() > 50 {
79-
format!("{}...", &value[..50])
80-
} else {
81-
value.clone()
82-
};
86+
let truncated = truncate_chars(value, 50);
8387
println!(" {:?}: {}", category, truncated);
8488
}
8589
}

0 commit comments

Comments
 (0)