Skip to main content

mos_fonts/
normalize.rs

1use std::borrow::Cow;
2
3use unicode_normalization::{UnicodeNormalization, is_nfc_quick};
4
5/// Return `text` in Unicode NFC form, borrowing when it is already normalized.
6///
7/// # Examples
8///
9/// ```
10/// use mos_fonts::nfc_text;
11///
12/// assert_eq!(nfc_text("S\u{0326}"), "\u{0218}");
13/// ```
14#[must_use]
15pub fn nfc_text(text: &str) -> Cow<'_, str> {
16    if is_nfc_quick(text.chars()) == unicode_normalization::IsNormalized::Yes {
17        Cow::Borrowed(text)
18    } else {
19        Cow::Owned(text.nfc().collect())
20    }
21}