|
| 1 | +//! UTF-8-safe text truncation helpers. |
| 2 | +//! |
| 3 | +//! Byte-index slicing (`&s[..n]`) panics when `n` falls inside a multi-byte |
| 4 | +//! character. These helpers clamp indices to char boundaries so display |
| 5 | +//! truncation never panics on user text (em dashes, accents, CJK, emoji). |
| 6 | +
|
| 7 | +/// Find the largest index at or below `index` that is a char boundary of `s`. |
| 8 | +/// |
| 9 | +/// Returns `s.len()` if `index` is past the end of the string. This mirrors |
| 10 | +/// the unstable `str::floor_char_boundary`; swap to the std method once it |
| 11 | +/// stabilizes. |
| 12 | +/// |
| 13 | +/// # Examples |
| 14 | +/// |
| 15 | +/// ``` |
| 16 | +/// use lash_types::text::floor_char_boundary; |
| 17 | +/// |
| 18 | +/// let s = "a—b"; // '—' occupies bytes 1..4 |
| 19 | +/// assert_eq!(floor_char_boundary(s, 2), 1); |
| 20 | +/// assert_eq!(floor_char_boundary(s, 4), 4); |
| 21 | +/// assert_eq!(floor_char_boundary(s, 99), s.len()); |
| 22 | +/// ``` |
| 23 | +#[must_use] |
| 24 | +pub fn floor_char_boundary(s: &str, index: usize) -> usize { |
| 25 | + if index >= s.len() { |
| 26 | + return s.len(); |
| 27 | + } |
| 28 | + let mut i = index; |
| 29 | + while !s.is_char_boundary(i) { |
| 30 | + i -= 1; |
| 31 | + } |
| 32 | + i |
| 33 | +} |
| 34 | + |
| 35 | +/// Truncate `s` to at most `max_len` bytes, appending `"..."` when truncated. |
| 36 | +/// |
| 37 | +/// The ellipsis counts toward `max_len`, so the result is never longer than |
| 38 | +/// `max_len` bytes (except when `max_len < 3`, where `"..."` is returned |
| 39 | +/// as-is). Truncation lands on a char boundary, so multi-byte characters at |
| 40 | +/// the cut point are dropped rather than split. |
| 41 | +/// |
| 42 | +/// # Examples |
| 43 | +/// |
| 44 | +/// ``` |
| 45 | +/// use lash_types::text::truncate_with_ellipsis; |
| 46 | +/// |
| 47 | +/// assert_eq!(truncate_with_ellipsis("short", 10), "short"); |
| 48 | +/// assert_eq!(truncate_with_ellipsis("Hello, World!", 10), "Hello, ..."); |
| 49 | +/// assert_eq!(truncate_with_ellipsis("Hello", 2), "..."); |
| 50 | +/// |
| 51 | +/// // A multi-byte char straddling the cut point is dropped, not split. |
| 52 | +/// let s = "aaaa—bbbb"; // '—' occupies bytes 4..7 |
| 53 | +/// assert_eq!(truncate_with_ellipsis(s, 8), "aaaa..."); |
| 54 | +/// ``` |
| 55 | +#[must_use] |
| 56 | +pub fn truncate_with_ellipsis(s: &str, max_len: usize) -> String { |
| 57 | + if s.len() <= max_len { |
| 58 | + s.to_string() |
| 59 | + } else if max_len <= 3 { |
| 60 | + "...".to_string() |
| 61 | + } else { |
| 62 | + format!("{}...", &s[..floor_char_boundary(s, max_len - 3)]) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(test)] |
| 67 | +mod tests { |
| 68 | + use super::*; |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn floor_boundary_on_ascii_is_identity() { |
| 72 | + let s = "abcdef"; |
| 73 | + for i in 0..=s.len() { |
| 74 | + assert_eq!(floor_char_boundary(s, i), i); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn floor_boundary_walks_back_inside_multibyte_char() { |
| 80 | + let s = "a—b"; // bytes: a=0, — =1..4, b=4 |
| 81 | + assert_eq!(floor_char_boundary(s, 1), 1); |
| 82 | + assert_eq!(floor_char_boundary(s, 2), 1); |
| 83 | + assert_eq!(floor_char_boundary(s, 3), 1); |
| 84 | + assert_eq!(floor_char_boundary(s, 4), 4); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn floor_boundary_clamps_past_end() { |
| 89 | + assert_eq!(floor_char_boundary("abc", 100), 3); |
| 90 | + assert_eq!(floor_char_boundary("", 5), 0); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn truncate_short_string_unchanged() { |
| 95 | + assert_eq!(truncate_with_ellipsis("hi", 10), "hi"); |
| 96 | + assert_eq!(truncate_with_ellipsis("exact", 5), "exact"); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn truncate_tiny_budget_returns_bare_ellipsis() { |
| 101 | + assert_eq!(truncate_with_ellipsis("hello", 3), "..."); |
| 102 | + assert_eq!(truncate_with_ellipsis("hello", 0), "..."); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn truncate_never_splits_multibyte_chars() { |
| 107 | + // '—' is 3 bytes; place it so every cut point from 4..=9 lands |
| 108 | + // somewhere interesting and none of them panic. |
| 109 | + let s = "aaaa—bbbb"; |
| 110 | + for max_len in 4..=s.len() + 3 { |
| 111 | + let out = truncate_with_ellipsis(s, max_len); |
| 112 | + assert!(out.len() <= max_len.max(3)); |
| 113 | + assert!(out.is_char_boundary(out.len())); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn truncate_handles_emoji_and_cjk() { |
| 119 | + let s = "日本語のテキストです、長いので切り詰められます"; |
| 120 | + for max_len in 0..=s.len() + 3 { |
| 121 | + let _ = truncate_with_ellipsis(s, max_len); // must not panic |
| 122 | + } |
| 123 | + let e = "🦀🦀🦀🦀🦀"; |
| 124 | + for max_len in 0..=e.len() + 3 { |
| 125 | + let _ = truncate_with_ellipsis(e, max_len); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments