unicode_normalization/
quick_check.rs
1use crate::lookups::canonical_combining_class;
2use crate::stream_safe;
3use crate::tables;
4use crate::UnicodeNormalization;
5
6#[derive(Debug, Eq, PartialEq)]
13pub enum IsNormalized {
14 Yes,
16 No,
18 Maybe,
20}
21
22#[inline]
24fn quick_check<F, I>(s: I, is_allowed: F, stream_safe: bool) -> IsNormalized
25where
26 I: Iterator<Item = char>,
27 F: Fn(char) -> IsNormalized,
28{
29 let mut last_cc = 0u8;
30 let mut nonstarter_count = 0;
31 let mut result = IsNormalized::Yes;
32 for ch in s {
33 if ch <= '\x7f' {
35 last_cc = 0;
36 nonstarter_count = 0;
37 continue;
38 }
39
40 let cc = canonical_combining_class(ch);
42 if last_cc > cc && cc != 0 {
43 return IsNormalized::No;
44 }
45 match is_allowed(ch) {
46 IsNormalized::Yes => (),
47 IsNormalized::No => return IsNormalized::No,
48 IsNormalized::Maybe => {
49 result = IsNormalized::Maybe;
50 }
51 }
52 if stream_safe {
53 let decomp = stream_safe::classify_nonstarters(ch);
54
55 if nonstarter_count + decomp.leading_nonstarters > stream_safe::MAX_NONSTARTERS {
58 return IsNormalized::No;
59 }
60 if decomp.leading_nonstarters == decomp.decomposition_len {
61 nonstarter_count += decomp.decomposition_len;
62 } else {
63 nonstarter_count = decomp.trailing_nonstarters;
64 }
65 }
66 last_cc = cc;
67 }
68 result
69}
70
71#[inline]
75pub fn is_nfc_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
76 quick_check(s, tables::qc_nfc, false)
77}
78
79#[inline]
81pub fn is_nfkc_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
82 quick_check(s, tables::qc_nfkc, false)
83}
84
85#[inline]
87pub fn is_nfd_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
88 quick_check(s, tables::qc_nfd, false)
89}
90
91#[inline]
93pub fn is_nfkd_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
94 quick_check(s, tables::qc_nfkd, false)
95}
96
97#[inline]
99pub fn is_nfc_stream_safe_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
100 quick_check(s, tables::qc_nfc, true)
101}
102
103#[inline]
105pub fn is_nfd_stream_safe_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
106 quick_check(s, tables::qc_nfd, true)
107}
108
109#[inline]
111pub fn is_nfc(s: &str) -> bool {
112 match is_nfc_quick(s.chars()) {
113 IsNormalized::Yes => true,
114 IsNormalized::No => false,
115 IsNormalized::Maybe => s.chars().eq(s.chars().nfc()),
116 }
117}
118
119#[inline]
121pub fn is_nfkc(s: &str) -> bool {
122 match is_nfkc_quick(s.chars()) {
123 IsNormalized::Yes => true,
124 IsNormalized::No => false,
125 IsNormalized::Maybe => s.chars().eq(s.chars().nfkc()),
126 }
127}
128
129#[inline]
131pub fn is_nfd(s: &str) -> bool {
132 match is_nfd_quick(s.chars()) {
133 IsNormalized::Yes => true,
134 IsNormalized::No => false,
135 IsNormalized::Maybe => s.chars().eq(s.chars().nfd()),
136 }
137}
138
139#[inline]
141pub fn is_nfkd(s: &str) -> bool {
142 match is_nfkd_quick(s.chars()) {
143 IsNormalized::Yes => true,
144 IsNormalized::No => false,
145 IsNormalized::Maybe => s.chars().eq(s.chars().nfkd()),
146 }
147}
148
149#[inline]
151pub fn is_nfc_stream_safe(s: &str) -> bool {
152 match is_nfc_stream_safe_quick(s.chars()) {
153 IsNormalized::Yes => true,
154 IsNormalized::No => false,
155 IsNormalized::Maybe => s.chars().eq(s.chars().stream_safe().nfc()),
156 }
157}
158
159#[inline]
161pub fn is_nfd_stream_safe(s: &str) -> bool {
162 match is_nfd_stream_safe_quick(s.chars()) {
163 IsNormalized::Yes => true,
164 IsNormalized::No => false,
165 IsNormalized::Maybe => s.chars().eq(s.chars().stream_safe().nfd()),
166 }
167}
168
169#[cfg(test)]
170mod tests {
171 use super::{is_nfc_stream_safe_quick, is_nfd_stream_safe_quick, IsNormalized};
172
173 #[test]
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]
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}