Coverage Report

Created: 2025-10-28 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/unicode-normalization/src/quick_check.rs
Line
Count
Source
1
use crate::lookups::canonical_combining_class;
2
use crate::stream_safe;
3
use crate::tables;
4
use crate::UnicodeNormalization;
5
6
/// QuickCheck quickly determines if a string is normalized, it can return
7
/// `Maybe`
8
///
9
/// The QuickCheck algorithm can quickly determine if a text is or isn't
10
/// normalized without any allocations in many cases, but it has to be able to
11
/// return `Maybe` when a full decomposition and recomposition is necessary.
12
#[derive(Debug, Eq, PartialEq)]
13
pub enum IsNormalized {
14
    /// The text is definitely normalized.
15
    Yes,
16
    /// The text is definitely not normalized.
17
    No,
18
    /// The text may be normalized.
19
    Maybe,
20
}
21
22
// https://unicode.org/reports/tr15/#Detecting_Normalization_Forms
23
#[inline]
24
159k
fn quick_check<F, I>(s: I, is_allowed: F, stream_safe: bool) -> IsNormalized
25
159k
where
26
159k
    I: Iterator<Item = char>,
27
159k
    F: Fn(char) -> IsNormalized,
28
{
29
159k
    let mut last_cc = 0u8;
30
159k
    let mut nonstarter_count = 0;
31
159k
    let mut result = IsNormalized::Yes;
32
596M
    for ch in s {
33
        // For ASCII we know it's always allowed and a starter
34
596M
        if ch <= '\x7f' {
35
227M
            last_cc = 0;
36
227M
            nonstarter_count = 0;
37
227M
            continue;
38
368M
        }
39
40
        // Otherwise, lookup the combining class and QC property
41
368M
        let cc = canonical_combining_class(ch);
42
368M
        if last_cc > cc && cc != 0 {
43
1.41k
            return IsNormalized::No;
44
368M
        }
45
368M
        match is_allowed(ch) {
46
235M
            IsNormalized::Yes => (),
47
28.1k
            IsNormalized::No => return IsNormalized::No,
48
132M
            IsNormalized::Maybe => {
49
132M
                result = IsNormalized::Maybe;
50
132M
            }
51
        }
52
368M
        if stream_safe {
53
163M
            let decomp = stream_safe::classify_nonstarters(ch);
54
55
            // If we're above `MAX_NONSTARTERS`, we're definitely *not*
56
            // stream-safe normalized.
57
163M
            if nonstarter_count + decomp.leading_nonstarters > stream_safe::MAX_NONSTARTERS {
58
1.38k
                return IsNormalized::No;
59
163M
            }
60
163M
            if decomp.leading_nonstarters == decomp.decomposition_len {
61
141M
                nonstarter_count += decomp.decomposition_len;
62
141M
            } else {
63
21.8M
                nonstarter_count = decomp.trailing_nonstarters;
64
21.8M
            }
65
205M
        }
66
368M
        last_cc = cc;
67
    }
68
128k
    result
69
159k
}
unicode_normalization::quick_check::quick_check::<unicode_normalization::tables::qc_nfc, core::str::iter::Chars>
Line
Count
Source
24
56.8k
fn quick_check<F, I>(s: I, is_allowed: F, stream_safe: bool) -> IsNormalized
25
56.8k
where
26
56.8k
    I: Iterator<Item = char>,
27
56.8k
    F: Fn(char) -> IsNormalized,
28
{
29
56.8k
    let mut last_cc = 0u8;
30
56.8k
    let mut nonstarter_count = 0;
31
56.8k
    let mut result = IsNormalized::Yes;
32
199M
    for ch in s {
33
        // For ASCII we know it's always allowed and a starter
34
199M
        if ch <= '\x7f' {
35
81.7M
            last_cc = 0;
36
81.7M
            nonstarter_count = 0;
37
81.7M
            continue;
38
117M
        }
39
40
        // Otherwise, lookup the combining class and QC property
41
117M
        let cc = canonical_combining_class(ch);
42
117M
        if last_cc > cc && cc != 0 {
43
542
            return IsNormalized::No;
44
117M
        }
45
117M
        match is_allowed(ch) {
46
18.5M
            IsNormalized::Yes => (),
47
5.76k
            IsNormalized::No => return IsNormalized::No,
48
99.4M
            IsNormalized::Maybe => {
49
99.4M
                result = IsNormalized::Maybe;
50
99.4M
            }
51
        }
52
117M
        if stream_safe {
53
78.6M
            let decomp = stream_safe::classify_nonstarters(ch);
54
55
            // If we're above `MAX_NONSTARTERS`, we're definitely *not*
56
            // stream-safe normalized.
57
78.6M
            if nonstarter_count + decomp.leading_nonstarters > stream_safe::MAX_NONSTARTERS {
58
770
                return IsNormalized::No;
59
78.6M
            }
60
78.6M
            if decomp.leading_nonstarters == decomp.decomposition_len {
61
66.9M
                nonstarter_count += decomp.decomposition_len;
62
66.9M
            } else {
63
11.7M
                nonstarter_count = decomp.trailing_nonstarters;
64
11.7M
            }
65
39.3M
        }
66
117M
        last_cc = cc;
67
    }
68
49.7k
    result
69
56.8k
}
unicode_normalization::quick_check::quick_check::<unicode_normalization::tables::qc_nfd, core::str::iter::Chars>
Line
Count
Source
24
56.8k
fn quick_check<F, I>(s: I, is_allowed: F, stream_safe: bool) -> IsNormalized
25
56.8k
where
26
56.8k
    I: Iterator<Item = char>,
27
56.8k
    F: Fn(char) -> IsNormalized,
28
{
29
56.8k
    let mut last_cc = 0u8;
30
56.8k
    let mut nonstarter_count = 0;
31
56.8k
    let mut result = IsNormalized::Yes;
32
205M
    for ch in s {
33
        // For ASCII we know it's always allowed and a starter
34
205M
        if ch <= '\x7f' {
35
79.3M
            last_cc = 0;
36
79.3M
            nonstarter_count = 0;
37
79.3M
            continue;
38
125M
        }
39
40
        // Otherwise, lookup the combining class and QC property
41
125M
        let cc = canonical_combining_class(ch);
42
125M
        if last_cc > cc && cc != 0 {
43
452
            return IsNormalized::No;
44
125M
        }
45
125M
        match is_allowed(ch) {
46
125M
            IsNormalized::Yes => (),
47
10.3k
            IsNormalized::No => return IsNormalized::No,
48
0
            IsNormalized::Maybe => {
49
0
                result = IsNormalized::Maybe;
50
0
            }
51
        }
52
125M
        if stream_safe {
53
84.4M
            let decomp = stream_safe::classify_nonstarters(ch);
54
55
            // If we're above `MAX_NONSTARTERS`, we're definitely *not*
56
            // stream-safe normalized.
57
84.4M
            if nonstarter_count + decomp.leading_nonstarters > stream_safe::MAX_NONSTARTERS {
58
610
                return IsNormalized::No;
59
84.4M
            }
60
84.4M
            if decomp.leading_nonstarters == decomp.decomposition_len {
61
74.4M
                nonstarter_count += decomp.decomposition_len;
62
74.4M
            } else {
63
10.0M
                nonstarter_count = decomp.trailing_nonstarters;
64
10.0M
            }
65
41.2M
        }
66
125M
        last_cc = cc;
67
    }
68
45.4k
    result
69
56.8k
}
unicode_normalization::quick_check::quick_check::<unicode_normalization::tables::qc_nfkc, core::str::iter::Chars>
Line
Count
Source
24
22.7k
fn quick_check<F, I>(s: I, is_allowed: F, stream_safe: bool) -> IsNormalized
25
22.7k
where
26
22.7k
    I: Iterator<Item = char>,
27
22.7k
    F: Fn(char) -> IsNormalized,
28
{
29
22.7k
    let mut last_cc = 0u8;
30
22.7k
    let mut nonstarter_count = 0;
31
22.7k
    let mut result = IsNormalized::Yes;
32
94.2M
    for ch in s {
33
        // For ASCII we know it's always allowed and a starter
34
94.2M
        if ch <= '\x7f' {
35
33.8M
            last_cc = 0;
36
33.8M
            nonstarter_count = 0;
37
33.8M
            continue;
38
60.4M
        }
39
40
        // Otherwise, lookup the combining class and QC property
41
60.4M
        let cc = canonical_combining_class(ch);
42
60.4M
        if last_cc > cc && cc != 0 {
43
228
            return IsNormalized::No;
44
60.4M
        }
45
60.4M
        match is_allowed(ch) {
46
27.2M
            IsNormalized::Yes => (),
47
5.14k
            IsNormalized::No => return IsNormalized::No,
48
33.1M
            IsNormalized::Maybe => {
49
33.1M
                result = IsNormalized::Maybe;
50
33.1M
            }
51
        }
52
60.4M
        if stream_safe {
53
0
            let decomp = stream_safe::classify_nonstarters(ch);
54
55
            // If we're above `MAX_NONSTARTERS`, we're definitely *not*
56
            // stream-safe normalized.
57
0
            if nonstarter_count + decomp.leading_nonstarters > stream_safe::MAX_NONSTARTERS {
58
0
                return IsNormalized::No;
59
0
            }
60
0
            if decomp.leading_nonstarters == decomp.decomposition_len {
61
0
                nonstarter_count += decomp.decomposition_len;
62
0
            } else {
63
0
                nonstarter_count = decomp.trailing_nonstarters;
64
0
            }
65
60.4M
        }
66
60.4M
        last_cc = cc;
67
    }
68
17.3k
    result
69
22.7k
}
unicode_normalization::quick_check::quick_check::<unicode_normalization::tables::qc_nfkd, core::str::iter::Chars>
Line
Count
Source
24
22.7k
fn quick_check<F, I>(s: I, is_allowed: F, stream_safe: bool) -> IsNormalized
25
22.7k
where
26
22.7k
    I: Iterator<Item = char>,
27
22.7k
    F: Fn(char) -> IsNormalized,
28
{
29
22.7k
    let mut last_cc = 0u8;
30
22.7k
    let mut nonstarter_count = 0;
31
22.7k
    let mut result = IsNormalized::Yes;
32
97.3M
    for ch in s {
33
        // For ASCII we know it's always allowed and a starter
34
97.3M
        if ch <= '\x7f' {
35
32.8M
            last_cc = 0;
36
32.8M
            nonstarter_count = 0;
37
32.8M
            continue;
38
64.4M
        }
39
40
        // Otherwise, lookup the combining class and QC property
41
64.4M
        let cc = canonical_combining_class(ch);
42
64.4M
        if last_cc > cc && cc != 0 {
43
190
            return IsNormalized::No;
44
64.4M
        }
45
64.4M
        match is_allowed(ch) {
46
64.4M
            IsNormalized::Yes => (),
47
6.87k
            IsNormalized::No => return IsNormalized::No,
48
0
            IsNormalized::Maybe => {
49
0
                result = IsNormalized::Maybe;
50
0
            }
51
        }
52
64.4M
        if stream_safe {
53
0
            let decomp = stream_safe::classify_nonstarters(ch);
54
55
            // If we're above `MAX_NONSTARTERS`, we're definitely *not*
56
            // stream-safe normalized.
57
0
            if nonstarter_count + decomp.leading_nonstarters > stream_safe::MAX_NONSTARTERS {
58
0
                return IsNormalized::No;
59
0
            }
60
0
            if decomp.leading_nonstarters == decomp.decomposition_len {
61
0
                nonstarter_count += decomp.decomposition_len;
62
0
            } else {
63
0
                nonstarter_count = decomp.trailing_nonstarters;
64
0
            }
65
64.4M
        }
66
64.4M
        last_cc = cc;
67
    }
68
15.6k
    result
69
22.7k
}
Unexecuted instantiation: unicode_normalization::quick_check::quick_check::<_, _>
70
71
/// Quickly check if a string is in NFC, potentially returning
72
/// `IsNormalized::Maybe` if further checks are necessary.  In this case a check
73
/// like `s.chars().nfc().eq(s.chars())` should suffice.
74
#[inline]
75
22.7k
pub fn is_nfc_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
76
22.7k
    quick_check(s, tables::qc_nfc, false)
77
22.7k
}
unicode_normalization::quick_check::is_nfc_quick::<core::str::iter::Chars>
Line
Count
Source
75
22.7k
pub fn is_nfc_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
76
22.7k
    quick_check(s, tables::qc_nfc, false)
77
22.7k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfc_quick::<_>
78
79
/// Quickly check if a string is in NFKC.
80
#[inline]
81
22.7k
pub fn is_nfkc_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
82
22.7k
    quick_check(s, tables::qc_nfkc, false)
83
22.7k
}
unicode_normalization::quick_check::is_nfkc_quick::<core::str::iter::Chars>
Line
Count
Source
81
22.7k
pub fn is_nfkc_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
82
22.7k
    quick_check(s, tables::qc_nfkc, false)
83
22.7k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfkc_quick::<_>
84
85
/// Quickly check if a string is in NFD.
86
#[inline]
87
22.7k
pub fn is_nfd_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
88
22.7k
    quick_check(s, tables::qc_nfd, false)
89
22.7k
}
unicode_normalization::quick_check::is_nfd_quick::<core::str::iter::Chars>
Line
Count
Source
87
22.7k
pub fn is_nfd_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
88
22.7k
    quick_check(s, tables::qc_nfd, false)
89
22.7k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfd_quick::<_>
90
91
/// Quickly check if a string is in NFKD.
92
#[inline]
93
22.7k
pub fn is_nfkd_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
94
22.7k
    quick_check(s, tables::qc_nfkd, false)
95
22.7k
}
unicode_normalization::quick_check::is_nfkd_quick::<core::str::iter::Chars>
Line
Count
Source
93
22.7k
pub fn is_nfkd_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
94
22.7k
    quick_check(s, tables::qc_nfkd, false)
95
22.7k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfkd_quick::<_>
96
97
/// Quickly check if a string is Stream-Safe NFC.
98
#[inline]
99
34.0k
pub fn is_nfc_stream_safe_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
100
34.0k
    quick_check(s, tables::qc_nfc, true)
101
34.0k
}
unicode_normalization::quick_check::is_nfc_stream_safe_quick::<core::str::iter::Chars>
Line
Count
Source
99
34.0k
pub fn is_nfc_stream_safe_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
100
34.0k
    quick_check(s, tables::qc_nfc, true)
101
34.0k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfc_stream_safe_quick::<_>
102
103
/// Quickly check if a string is Stream-Safe NFD.
104
#[inline]
105
34.0k
pub fn is_nfd_stream_safe_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
106
34.0k
    quick_check(s, tables::qc_nfd, true)
107
34.0k
}
unicode_normalization::quick_check::is_nfd_stream_safe_quick::<core::str::iter::Chars>
Line
Count
Source
105
34.0k
pub fn is_nfd_stream_safe_quick<I: Iterator<Item = char>>(s: I) -> IsNormalized {
106
34.0k
    quick_check(s, tables::qc_nfd, true)
107
34.0k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfd_stream_safe_quick::<_>
108
109
/// Authoritatively check if a string is in NFC.
110
#[inline]
111
11.3k
pub fn is_nfc(s: &str) -> bool {
112
11.3k
    match is_nfc_quick(s.chars()) {
113
5.95k
        IsNormalized::Yes => true,
114
1.63k
        IsNormalized::No => false,
115
3.77k
        IsNormalized::Maybe => s.chars().eq(s.chars().nfc()),
116
    }
117
11.3k
}
unicode_normalization::quick_check::is_nfc
Line
Count
Source
111
11.3k
pub fn is_nfc(s: &str) -> bool {
112
11.3k
    match is_nfc_quick(s.chars()) {
113
5.95k
        IsNormalized::Yes => true,
114
1.63k
        IsNormalized::No => false,
115
3.77k
        IsNormalized::Maybe => s.chars().eq(s.chars().nfc()),
116
    }
117
11.3k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfc
118
119
/// Authoritatively check if a string is in NFKC.
120
#[inline]
121
11.3k
pub fn is_nfkc(s: &str) -> bool {
122
11.3k
    match is_nfkc_quick(s.chars()) {
123
5.10k
        IsNormalized::Yes => true,
124
2.68k
        IsNormalized::No => false,
125
3.57k
        IsNormalized::Maybe => s.chars().eq(s.chars().nfkc()),
126
    }
127
11.3k
}
unicode_normalization::quick_check::is_nfkc
Line
Count
Source
121
11.3k
pub fn is_nfkc(s: &str) -> bool {
122
11.3k
    match is_nfkc_quick(s.chars()) {
123
5.10k
        IsNormalized::Yes => true,
124
2.68k
        IsNormalized::No => false,
125
3.57k
        IsNormalized::Maybe => s.chars().eq(s.chars().nfkc()),
126
    }
127
11.3k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfkc
128
129
/// Authoritatively check if a string is in NFD.
130
#[inline]
131
11.3k
pub fn is_nfd(s: &str) -> bool {
132
11.3k
    match is_nfd_quick(s.chars()) {
133
8.53k
        IsNormalized::Yes => true,
134
2.83k
        IsNormalized::No => false,
135
0
        IsNormalized::Maybe => s.chars().eq(s.chars().nfd()),
136
    }
137
11.3k
}
unicode_normalization::quick_check::is_nfd
Line
Count
Source
131
11.3k
pub fn is_nfd(s: &str) -> bool {
132
11.3k
    match is_nfd_quick(s.chars()) {
133
8.53k
        IsNormalized::Yes => true,
134
2.83k
        IsNormalized::No => false,
135
0
        IsNormalized::Maybe => s.chars().eq(s.chars().nfd()),
136
    }
137
11.3k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfd
138
139
/// Authoritatively check if a string is in NFKD.
140
#[inline]
141
11.3k
pub fn is_nfkd(s: &str) -> bool {
142
11.3k
    match is_nfkd_quick(s.chars()) {
143
7.83k
        IsNormalized::Yes => true,
144
3.53k
        IsNormalized::No => false,
145
0
        IsNormalized::Maybe => s.chars().eq(s.chars().nfkd()),
146
    }
147
11.3k
}
unicode_normalization::quick_check::is_nfkd
Line
Count
Source
141
11.3k
pub fn is_nfkd(s: &str) -> bool {
142
11.3k
    match is_nfkd_quick(s.chars()) {
143
7.83k
        IsNormalized::Yes => true,
144
3.53k
        IsNormalized::No => false,
145
0
        IsNormalized::Maybe => s.chars().eq(s.chars().nfkd()),
146
    }
147
11.3k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfkd
148
149
/// Authoritatively check if a string is Stream-Safe NFC.
150
#[inline]
151
17.0k
pub fn is_nfc_stream_safe(s: &str) -> bool {
152
17.0k
    match is_nfc_stream_safe_quick(s.chars()) {
153
9.05k
        IsNormalized::Yes => true,
154
1.89k
        IsNormalized::No => false,
155
6.09k
        IsNormalized::Maybe => s.chars().eq(s.chars().stream_safe().nfc()),
156
    }
157
17.0k
}
unicode_normalization::quick_check::is_nfc_stream_safe
Line
Count
Source
151
17.0k
pub fn is_nfc_stream_safe(s: &str) -> bool {
152
17.0k
    match is_nfc_stream_safe_quick(s.chars()) {
153
9.05k
        IsNormalized::Yes => true,
154
1.89k
        IsNormalized::No => false,
155
6.09k
        IsNormalized::Maybe => s.chars().eq(s.chars().stream_safe().nfc()),
156
    }
157
17.0k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfc_stream_safe
158
159
/// Authoritatively check if a string is Stream-Safe NFD.
160
#[inline]
161
17.0k
pub fn is_nfd_stream_safe(s: &str) -> bool {
162
17.0k
    match is_nfd_stream_safe_quick(s.chars()) {
163
14.1k
        IsNormalized::Yes => true,
164
2.87k
        IsNormalized::No => false,
165
0
        IsNormalized::Maybe => s.chars().eq(s.chars().stream_safe().nfd()),
166
    }
167
17.0k
}
unicode_normalization::quick_check::is_nfd_stream_safe
Line
Count
Source
161
17.0k
pub fn is_nfd_stream_safe(s: &str) -> bool {
162
17.0k
    match is_nfd_stream_safe_quick(s.chars()) {
163
14.1k
        IsNormalized::Yes => true,
164
2.87k
        IsNormalized::No => false,
165
0
        IsNormalized::Maybe => s.chars().eq(s.chars().stream_safe().nfd()),
166
    }
167
17.0k
}
Unexecuted instantiation: unicode_normalization::quick_check::is_nfd_stream_safe
168
169
#[cfg(test)]
170
mod 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
}