use std::borrow::Cow;
use unicode_normalization::{IsNormalized, UnicodeNormalization, is_nfc_quick};
pub fn nfc_identifier(name: &str) -> Cow<'_, str> {
if name.is_ascii() || is_nfc_quick(name.chars()) == IsNormalized::Yes {
Cow::Borrowed(name)
} else {
Cow::Owned(name.nfc().collect())
}
}
#[cfg(test)]
mod tests {
use super::nfc_identifier;
use std::borrow::Cow;
#[test]
fn ascii_borrows() {
assert!(matches!(nfc_identifier("dispatch_tool"), Cow::Borrowed(_)));
}
#[test]
fn nfc_hangul_borrows() {
assert!(matches!(nfc_identifier("후원금_정산"), Cow::Borrowed(_)));
}
#[test]
fn nfd_hangul_composes_to_nfc() {
let nfd = "\u{1112}\u{116e}\u{110b}\u{116f}\u{11ab}\u{110c}\u{1161}";
let out = nfc_identifier(nfd);
assert_eq!(out.as_ref(), "후원자");
assert_eq!(out.chars().count(), 3);
}
}