indicium/simple/internal/
normalize.rs

1#[cfg(feature = "unicode-normalization")]
2use unicode_normalization::UnicodeNormalization;
3
4impl<K: Ord> crate::simple::SearchIndex<K> {
5    /// Returns a normalized string according to the search index's settings.
6    ///
7    /// Normalization ensures consistent matching by canonicalizing Unicode
8    /// representations and optionally folding case. This allows searches to
9    /// match equivalent characters (like "fi" and "fi") regardless of how
10    /// they were encoded.
11    ///
12    /// When the `unicode-normalization` feature is enabled, NFKC normalization
13    /// is applied to decompose compatibility characters into their canonical
14    /// forms. When case insensitivity is also enabled, the string is
15    /// additionally lowercased.
16    ///
17    /// * If the search index case been set to be case sensitive, the string
18    ///   will be returned as-is.
19    ///
20    /// * If the search index case been set to be case insensitive, the string
21    ///   will be returned in lower-case form.
22    #[inline]
23    pub(crate) fn normalize<'k>(
24        &self,
25        keyword: &'k str
26    ) -> beef::lean::Cow<'k, str> {
27        if self.case_sensitive {
28            #[cfg(feature = "unicode-normalization")]
29            let normalized = keyword.nfkc().collect::<String>().into();
30
31            #[cfg(not(feature = "unicode-normalization"))]
32            let normalized = keyword.into();
33
34            normalized
35        } else {
36            #[cfg(feature = "unicode-normalization")]
37            let normalized = keyword.nfkc().collect::<String>().to_lowercase().into();
38
39            #[cfg(not(feature = "unicode-normalization"))]
40            let normalized = keyword.to_lowercase().into();
41
42            normalized
43        } // if
44    } // fn
45} // impl