use std::fmt;
use icu_segmenter::GraphemeClusterSegmenter;
pub fn truncate_graphemes(s: &str, max_octets: usize) -> &str {
if s.len() <= max_octets {
return s;
}
let mut cut = 0;
for boundary in GraphemeClusterSegmenter::new().segment_str(s) {
if boundary > max_octets {
break;
}
cut = boundary;
}
&s[..cut]
}
pub fn truncate_graphemes_chars(s: &str, max_chars: usize) -> &str {
let limit = s.char_indices().nth(max_chars).map_or(s.len(), |(i, _)| i);
truncate_graphemes(s, limit)
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DisplayTextError {
pub index: usize,
pub ch: char,
}
impl fmt::Display for DisplayTextError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"display text has U+{:04X} at byte {}, which the display-text profile excludes",
self.ch as u32, self.index
)
}
}
impl std::error::Error for DisplayTextError {}
pub fn check_display_text(s: &str, multiline: bool) -> Result<(), DisplayTextError> {
for (index, ch) in s.char_indices() {
let u = ch as u32;
let bad = match u {
0x0A => !multiline,
0x00..=0x1F | 0x7F..=0x9F => true,
0x2028 | 0x2029 | 0xFFFE | 0xFFFF => true,
0xFEFF => index == 0,
_ => false,
};
if bad {
return Err(DisplayTextError { index, ch });
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn never_splits_clusters() {
let tech = "\u{1F469}\u{200D}\u{1F4BB}";
assert_eq!(truncate_graphemes(&format!("{tech}x"), 10), "");
assert_eq!(truncate_graphemes(&format!("{tech}x"), 11), tech);
let de = "\u{1F1E9}\u{1F1EA}";
assert_eq!(truncate_graphemes(&format!("a{de}"), 8), "a");
let gak = "\u{1100}\u{1161}\u{11A8}";
assert_eq!(truncate_graphemes(&format!("{gak}{gak}"), 12), gak);
}
#[test]
fn crlf_is_one_cluster() {
assert_eq!(truncate_graphemes("a\r\nb", 2), "a");
assert_eq!(truncate_graphemes("a\r\nb", 3), "a\r\n");
}
#[test]
fn zero_limit() {
assert_eq!(truncate_graphemes("abc", 0), "");
}
#[test]
fn display_text_profile() {
for (s, multi) in [
("tab\there", true),
("nul\0", true),
("del\u{7F}", true),
("c1\u{85}", true),
("ls\u{2028}", true),
("ps\u{2029}", true),
("nc\u{FFFE}", true),
("nc\u{FFFF}", true),
("\u{FEFF}bom", true),
("cr\r", true),
] {
assert!(check_display_text(s, multi).is_err(), "{s:?}");
}
assert!(check_display_text("a\u{FEFF}b", false).is_ok());
assert!(check_display_text("Équipe \u{200D} 東京", false).is_ok());
let err = check_display_text("ab\u{2028}", false).unwrap_err();
assert_eq!((err.index, err.ch), (2, '\u{2028}'));
}
}