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