1 use UnicodeNormalization;
2 use lookups::canonical_combining_class;
3 use stream_safe;
4 use tables;
5 
6 /// The QuickCheck algorithm can quickly determine if a text is or isn't
7 /// normalized without any allocations in many cases, but it has to be able to
8 /// return `Maybe` when a full decomposition and recomposition is necessary.
9 #[derive(Debug, Eq, PartialEq)]
10 pub enum IsNormalized {
11     /// The text is definitely normalized.
12     Yes,
13     /// The text is definitely not normalized.
14     No,
15     /// The text may be normalized.
16     Maybe,
17 }
18 
19 // https://unicode.org/reports/tr15/#Detecting_Normalization_Forms
20 #[inline]
quick_check<F, I>(s: I, is_allowed: F, stream_safe: bool) -> IsNormalized where I: Iterator<Item=char>, F: Fn(char) -> IsNormalized21 fn quick_check<F, I>(s: I, is_allowed: F, stream_safe: bool) -> IsNormalized
22     where I: Iterator<Item=char>, F: Fn(char) -> IsNormalized
23 {
24     let mut last_cc = 0u8;
25     let mut nonstarter_count = 0;
26     let mut result = IsNormalized::Yes;
27     for ch in s {
28         // For ASCII we know it's always allowed and a starter
29         if ch <= '\x7f' {
30             last_cc = 0;
31             nonstarter_count = 0;
32             continue;
33         }
34 
35         // Otherwise, lookup the combining class and QC property
36         let cc = canonical_combining_class(ch);
37         if last_cc > cc && cc != 0 {
38             return IsNormalized::No;
39         }
40         match is_allowed(ch) {
41             IsNormalized::Yes => (),
42             IsNormalized::No => return IsNormalized::No,
43             IsNormalized::Maybe => {
44                 result = IsNormalized::Maybe;
45             },
46         }
47         if stream_safe {
48             let decomp = stream_safe::classify_nonstarters(ch);
49 
50             // If we're above `MAX_NONSTARTERS`, we're definitely *not*
51             // stream-safe normalized.
52             if nonstarter_count + decomp.leading_nonstarters > stream_safe::MAX_NONSTARTERS {
53                 return IsNormalized::No;
54             }
55             if decomp.leading_nonstarters == decomp.decomposition_len {
56                 nonstarter_count += decomp.decomposition_len;
57             } else {
58                 nonstarter_count = decomp.trailing_nonstarters;
59             }
60         }
61         last_cc = cc;
62     }
63     result
64 }
65 
66 /// Quickly check if a string is in NFC, potentially returning
67 /// `IsNormalized::Maybe` if further checks are necessary.  In this case a check
68 /// like `s.chars().nfc().eq(s.chars())` should suffice.
69 #[inline]
is_nfc_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized70 pub fn is_nfc_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
71     quick_check(s, tables::qc_nfc, false)
72 }
73 
74 
75 /// Quickly check if a string is in NFKC.
76 #[inline]
is_nfkc_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized77 pub fn is_nfkc_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
78     quick_check(s, tables::qc_nfkc, false)
79 }
80 
81 /// Quickly check if a string is in NFD.
82 #[inline]
is_nfd_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized83 pub fn is_nfd_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
84     quick_check(s, tables::qc_nfd, false)
85 }
86 
87 /// Quickly check if a string is in NFKD.
88 #[inline]
is_nfkd_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized89 pub fn is_nfkd_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
90     quick_check(s, tables::qc_nfkd, false)
91 }
92 
93 /// Quickly check if a string is Stream-Safe NFC.
94 #[inline]
is_nfc_stream_safe_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized95 pub fn is_nfc_stream_safe_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
96     quick_check(s, tables::qc_nfc, true)
97 }
98 
99 /// Quickly check if a string is Stream-Safe NFD.
100 #[inline]
is_nfd_stream_safe_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized101 pub fn is_nfd_stream_safe_quick<I: Iterator<Item=char>>(s: I) -> IsNormalized {
102     quick_check(s, tables::qc_nfd, true)
103 }
104 
105 /// Authoritatively check if a string is in NFC.
106 #[inline]
is_nfc(s: &str) -> bool107 pub fn is_nfc(s: &str) -> bool {
108     match is_nfc_quick(s.chars()) {
109         IsNormalized::Yes => true,
110         IsNormalized::No => false,
111         IsNormalized::Maybe => s.chars().eq(s.chars().nfc()),
112     }
113 }
114 
115 /// Authoritatively check if a string is in NFKC.
116 #[inline]
is_nfkc(s: &str) -> bool117 pub fn is_nfkc(s: &str) -> bool {
118     match is_nfkc_quick(s.chars()) {
119         IsNormalized::Yes => true,
120         IsNormalized::No => false,
121         IsNormalized::Maybe => s.chars().eq(s.chars().nfkc()),
122     }
123 }
124 
125 /// Authoritatively check if a string is in NFD.
126 #[inline]
is_nfd(s: &str) -> bool127 pub fn is_nfd(s: &str) -> bool {
128     match is_nfd_quick(s.chars()) {
129         IsNormalized::Yes => true,
130         IsNormalized::No => false,
131         IsNormalized::Maybe => s.chars().eq(s.chars().nfd()),
132     }
133 }
134 
135 /// Authoritatively check if a string is in NFKD.
136 #[inline]
is_nfkd(s: &str) -> bool137 pub fn is_nfkd(s: &str) -> bool {
138     match is_nfkd_quick(s.chars()) {
139         IsNormalized::Yes => true,
140         IsNormalized::No => false,
141         IsNormalized::Maybe => s.chars().eq(s.chars().nfkd()),
142     }
143 }
144 
145 /// Authoritatively check if a string is Stream-Safe NFC.
146 #[inline]
is_nfc_stream_safe(s: &str) -> bool147 pub fn is_nfc_stream_safe(s: &str) -> bool {
148     match is_nfc_stream_safe_quick(s.chars()) {
149         IsNormalized::Yes => true,
150         IsNormalized::No => false,
151         IsNormalized::Maybe => s.chars().eq(s.chars().stream_safe().nfc()),
152     }
153 }
154 
155 /// Authoritatively check if a string is Stream-Safe NFD.
156 #[inline]
is_nfd_stream_safe(s: &str) -> bool157 pub fn is_nfd_stream_safe(s: &str) -> bool {
158     match is_nfd_stream_safe_quick(s.chars()) {
159         IsNormalized::Yes => true,
160         IsNormalized::No => false,
161         IsNormalized::Maybe => s.chars().eq(s.chars().stream_safe().nfd()),
162     }
163 }
164 
165 #[cfg(test)]
166 mod tests {
167     use super::{
168         IsNormalized,
169         is_nfc_stream_safe_quick,
170         is_nfd_stream_safe_quick,
171     };
172 
173     #[test]
test_stream_safe_nfd()174     fn test_stream_safe_nfd() {
175         let okay = "Da\u{031b}\u{0316}\u{0317}\u{0318}\u{0319}\u{031c}\u{031d}\u{0300}\u{0301}\u{0302}\u{0303}\u{0304}\u{0305}\u{0306}\u{0307}\u{0308}\u{0309}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{0314}\u{0315}\u{031a}ngerzone";
176         assert_eq!(is_nfd_stream_safe_quick(okay.chars()), IsNormalized::Yes);
177 
178         let too_much = "Da\u{031b}\u{0316}\u{0317}\u{0318}\u{0319}\u{031c}\u{031d}\u{031e}\u{0300}\u{0301}\u{0302}\u{0303}\u{0304}\u{0305}\u{0306}\u{0307}\u{0308}\u{0309}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{0314}\u{0315}\u{031a}ngerzone";
179         assert_eq!(is_nfd_stream_safe_quick(too_much.chars()), IsNormalized::No);
180     }
181 
182     #[test]
test_stream_safe_nfc()183     fn test_stream_safe_nfc() {
184         let okay = "ok\u{e0}\u{031b}\u{0316}\u{0317}\u{0318}\u{0319}\u{031c}\u{031d}\u{0301}\u{0302}\u{0303}\u{0304}\u{0305}\u{0306}\u{0307}\u{0308}\u{0309}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{0314}\u{0315}\u{031a}y";
185         assert_eq!(is_nfc_stream_safe_quick(okay.chars()), IsNormalized::Maybe);
186 
187         let too_much = "not ok\u{e0}\u{031b}\u{0316}\u{0317}\u{0318}\u{0319}\u{031c}\u{031d}\u{031e}\u{0301}\u{0302}\u{0303}\u{0304}\u{0305}\u{0306}\u{0307}\u{0308}\u{0309}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{0314}\u{0315}\u{031a}y";
188         assert_eq!(is_nfc_stream_safe_quick(too_much.chars()), IsNormalized::No);
189     }
190 }
191