presentation: avoid trailing separator truncation

This commit is contained in:
DeFiDude committed 2026-07-26 13:38:34 -06:00
1 parent b4c2e09ca6
commit 830db46735
1 file changed
+10 -1
+10 -1
View File
@@ -28,7 +28,10 @@ pub fn sanitize_name(input: &str, max_chars: usize) -> String {
continue;
}
if pending_space {
if retained == max_chars {
// A collapsed separator is useful only when the current visible
// character also fits. Never consume the final slot with a
// trailing space that disappears on the next sanitization pass.
if max_chars.saturating_sub(retained) < 2 {
break;
}
output.push(' ');
@@ -137,6 +140,12 @@ mod tests {
assert_eq!(sanitize_name("anything", 0), "");
}
#[test]
fn truncation_never_retains_a_separator_without_following_text() {
assert_eq!(sanitize_name("¡ A", 2), "¡");
assert_eq!(sanitize_name("A B", 3), "A B");
}
proptest! {
#[test]
fn arbitrary_utf8_is_bounded_and_idempotent(input in ".*", limit in 0usize..256) {