diff --git a/crates/lxmf-core/src/presentation.rs b/crates/lxmf-core/src/presentation.rs index 9ec9083..126e907 100644 --- a/crates/lxmf-core/src/presentation.rs +++ b/crates/lxmf-core/src/presentation.rs @@ -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) {