//! Full Unicode case folding (`CaseFolding.txt` statuses `C` + `F`, Unicode //! 17.0.0), equivalent to uucode's `case_folding_full` field. //! //! The table is generated by `build.rs` from `ucd/CaseFolding.txt`. include!(concat!(env!("OUT_DIR"), "/case_folding_full.rs")); /// The full case folding of `cp`, or `None` if it folds to itself. /// /// The result has 1 to 3 codepoints (unused trailing entries are `0`); use /// [`fold_full`] for a slice. #[must_use] pub fn lookup(cp: u32) -> Option<[u32; 3]> { CASE_FOLDING_FULL .binary_search_by_key(&cp, |e| e.0) .ok() .map(|i| CASE_FOLDING_FULL[i].1) } /// The full case folding of `cp` as a zero-padded 3-element array. Like /// uucode's `.with(&buffer, cp)`, a codepoint without a folding folds to /// itself. #[must_use] pub fn fold_full(cp: u32) -> [u32; 3] { lookup(cp).unwrap_or([cp, 0, 0]) } #[cfg(test)] mod tests { use super::*; #[test] fn simple_and_identity() { assert_eq!(fold_full('A' as u32), ['a' as u32, 0, 0]); assert_eq!(fold_full('a' as u32), ['a' as u32, 0, 0]); assert_eq!(fold_full('1' as u32), ['1' as u32, 0, 0]); assert_eq!(fold_full(0x00B5), [0x03BC, 0, 0]); assert_eq!(fold_full(0x017F), ['s' as u32, 0, 0]); assert_eq!(fold_full(0x03C2), [0x03C3, 0, 0]); assert_eq!(fold_full(0x1E9E), [0x73, 0x73, 0]); } #[test] fn multi_codepoint() { // U+00DF LATIN SMALL LETTER SHARP S -> "ss" assert_eq!(fold_full(0x00DF), [0x73, 0x73, 0]); // U+0390 -> 03B9 0308 0301 assert_eq!(fold_full(0x0390), [0x03B9, 0x0308, 0x0301]); // U+FB03 LATIN SMALL LIGATURE FFI -> "ffi" assert_eq!(fold_full(0xFB03), [0x66, 0x66, 0x69]); } #[test] fn table_sorted_and_nonempty() { assert!(CASE_FOLDING_FULL.len() > 1500); assert!(CASE_FOLDING_FULL.windows(2).all(|w| w[0].0 < w[1].0)); } #[test] fn dotted_capital_i_uses_full_folding() { assert_eq!(fold_full(0x0130), [0x69, 0x307, 0]); // F entry for dotted I } }