simd-normalizer 0.1.1

SIMD-accelerated Unicode normalization (NFC, NFD, NFKC, NFKD)
Documentation
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// src/quick_check.rs

//! Quick-check for normalization forms (UAX#15 Section 9).
//!
//! Uses SIMD scanning to skip safe chunks in bulk for inputs >= 64 bytes.
//! Form-specific SIMD bounds and code-point range fast paths avoid trie
//! lookups for the vast majority of BMP characters.

use crate::simd;
use crate::tables;
use crate::utf8;

/// Result of a quick-check test.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IsNormalized {
    /// The string is definitely in the target normalization form.
    Yes,
    /// The string is definitely *not* in the target normalization form.
    No,
    /// The string *might* not be normalized; a full check is required.
    Maybe,
}

/// Convert a QC trie value (0=Y, 1=M, 2=N) to IsNormalized.
#[inline]
fn qc_value_to_result(v: u8) -> IsNormalized {
    match v {
        0 => IsNormalized::Yes,
        1 => IsNormalized::Maybe,
        _ => IsNormalized::No,
    }
}

/// Check if a code point is a CJK Unified Ideograph (CCC=0, QC=Yes for all forms).
#[inline(always)]
fn is_cjk_unified(cp: u32) -> bool {
    // BMP: CJK Unified Ideographs + Extension A (most common)
    (0x4E00..=0x9FFF).contains(&cp) || (0x3400..=0x4DBF).contains(&cp)
}

/// Check if a supplementary code point (cp >= 0x10000) is safe for all
/// normalization forms (CCC=0 and QC=Yes). Returns false only for narrow
/// exception ranges that may have decompositions or non-zero CCC.
#[inline(always)]
fn is_supp_safe(cp: u32) -> bool {
    if cp >= 0x20000 {
        // Plane 2+: safe except CJK Compatibility Ideographs Supplement
        return !(0x2F800..=0x2FA1F).contains(&cp);
    }
    // Plane 1: core emoji and symbols block (U+1F252-U+1FBEF) is safe.
    // Verified: no decompositions and CCC=0 for all normalization forms.
    (0x1F252..=0x1FBEF).contains(&cp)
}

/// Check if a code point is Hiragana or Katakana (CCC=0, QC=Yes for NFC/NFKC).
/// Excludes: combining marks U+3099-309A (CCC>0), NFKC-decomposing U+309B-309C,
/// U+309F (ゟ), U+30FF (ヿ).
#[inline(always)]
fn is_kana(cp: u32) -> bool {
    // Hiragana base (U+3041-3098)
    (0x3041..0x3099).contains(&cp)
        // Hiragana iteration marks (U+309D-309E)
        || cp == 0x309D
        || cp == 0x309E
        // Katakana (U+30A0-30FE)
        || (0x30A0..=0x30FE).contains(&cp)
}

/// Generic quick-check implementation.
///
/// For inputs >= 64 bytes, uses SIMD scanning to skip chunks in bulk.
/// For shorter inputs, falls back to a scalar character-by-character loop.
/// Returns as soon as a definitive No is found.
///
/// # Parameters
/// - `qc_shift`: bit shift to extract this form's 2-bit QC from the fused CCC+QC trie.
/// - `simd_bound`: SIMD scan threshold; bytes below this are skipped in bulk.
///   For NFC this is 0xCC (all chars below U+0300 are safe), for other forms 0xC0.
/// - `safe_below`: code point below which CCC=0 and QC=Yes is guaranteed.
/// - `hangul_safe`: whether Hangul Syllables (U+AC00..U+D7A3) are QC=Yes for this form.
/// - `kana_safe`: whether Hiragana/Katakana (U+3040..U+30FF) are QC=Yes for this form.
/// - `latin1_upper_safe`: whether U+00C0..U+00FF (precomposed Latin Supplement upper)
///   is uniformly CCC=0 and QC=Yes. True for NFC/NFKC (precomposed forms are kept
///   precomposed under both compose-normalizing forms). False for NFD/NFKD where
///   they all decompose. NFC already has `simd_bound=0xCC` which skips this range
///   in bulk; the flag is consulted on the bit-walk for forms whose `simd_bound`
///   does flag bytes in the U+00C0..U+00FF lead-byte range.
#[inline]
fn quick_check_impl(
    input: &str,
    qc_shift: u32,
    simd_bound: u8,
    safe_below: u32,
    hangul_safe: bool,
    kana_safe: bool,
    latin1_upper_safe: bool,
) -> IsNormalized {
    let bytes = input.as_bytes();
    let len = bytes.len();

    if len < 64 {
        return quick_check_scalar(
            input,
            qc_shift,
            safe_below,
            hangul_safe,
            kana_safe,
            latin1_upper_safe,
        );
    }

    let ptr = bytes.as_ptr();

    let mut last_ccc: u8 = 0;
    let mut result = IsNormalized::Yes;
    // Byte offset past the last character we've examined.
    let mut processed_up_to: usize = 0;
    let mut pos: usize = 0;

    // SIMD chunk loop: skip chunks where all bytes < simd_bound in bulk.
    while pos + 64 <= len {
        // SAFETY: pos + 64 <= len, so ptr.add(pos) is valid for 64 bytes.
        let mask = unsafe { simd::scan_chunk(ptr.add(pos), simd_bound) };
        let chunk_end = pos + 64;

        if mask == 0 {
            // All bytes < simd_bound — characters in this chunk are either ASCII
            // or known-safe non-ASCII (CCC=0, QC=Yes). CCC resets to 0.
            last_ccc = 0;
            processed_up_to = chunk_end;
            pos = chunk_end;
            continue;
        }

        // Walk set bits — each is a lead byte of a character that needs inspection.
        let chunk_start = pos;
        let mut chunk_mask = mask;
        while chunk_mask != 0 {
            let bit_pos = chunk_mask.trailing_zeros() as usize;
            chunk_mask &= chunk_mask.wrapping_sub(1); // clear lowest set bit

            let byte_pos = chunk_start + bit_pos;

            // Skip bytes already covered by a previous multi-byte decode.
            if byte_pos < processed_up_to {
                continue;
            }

            // Gap before this lead byte → safe characters → CCC resets to 0.
            if byte_pos > processed_up_to {
                last_ccc = 0;
            }

            // Decode the character at this position.
            let (ch, width) = utf8::decode_char_at(bytes, byte_pos);
            processed_up_to = byte_pos + width;

            // Fast path: known-safe code point ranges (CCC=0 and QC=Yes).
            let cp = ch as u32;
            if cp < safe_below
                || (latin1_upper_safe && (0x00C0..0x0100).contains(&cp))
                || is_cjk_unified(cp)
                || (hangul_safe && (0xAC00..=0xD7A3).contains(&cp))
                || (kana_safe && is_kana(cp))
                || (cp >= 0x10000 && is_supp_safe(cp))
            {
                last_ccc = 0;
                continue;
            }

            // Fused CCC + QC lookup (single trie access).
            let (ccc, qc) = tables::lookup_ccc_qc(ch, qc_shift);
            if ccc != 0 && last_ccc > ccc {
                return IsNormalized::No;
            }

            // Check QC property.
            match qc_value_to_result(qc) {
                IsNormalized::No => return IsNormalized::No,
                IsNormalized::Maybe => result = IsNormalized::Maybe,
                IsNormalized::Yes => {},
            }

            last_ccc = ccc;
        }

        // Trailing safe bytes in this chunk after the last flagged char.
        if processed_up_to < chunk_end {
            last_ccc = 0;
            processed_up_to = chunk_end;
        }

        pos = chunk_end;
    }

    // Scalar tail for remaining bytes after the last full 64-byte chunk.
    let tail_start = processed_up_to.max(pos);
    if tail_start > processed_up_to {
        // Gap of safe characters between last processed char and tail start.
        last_ccc = 0;
    }
    let mut tail_pos = tail_start;
    while tail_pos < len {
        let b = bytes[tail_pos];
        if b < 0x80 {
            // ASCII: CCC=0, QC=Yes for all forms.
            last_ccc = 0;
            tail_pos += 1;
            continue;
        }
        // Skip continuation bytes from a character that crossed the chunk/tail
        // boundary. Its lead byte was < simd_bound, so it is safe (CCC=0, QC=Yes).
        if utf8::is_continuation_byte(b) {
            tail_pos += 1;
            continue;
        }
        // Lead byte of a non-ASCII character.
        let (ch, width) = utf8::decode_char_at(bytes, tail_pos);

        // Fast path: known-safe code point ranges.
        let cp = ch as u32;
        if cp < safe_below
            || (latin1_upper_safe && (0x00C0..0x0100).contains(&cp))
            || is_cjk_unified(cp)
            || (hangul_safe && (0xAC00..=0xD7A3).contains(&cp))
            || (cp >= 0x10000 && is_supp_safe(cp))
        {
            last_ccc = 0;
            tail_pos += width;
            continue;
        }

        let (ccc, qc) = tables::lookup_ccc_qc(ch, qc_shift);
        if ccc != 0 && last_ccc > ccc {
            return IsNormalized::No;
        }
        match qc_value_to_result(qc) {
            IsNormalized::No => return IsNormalized::No,
            IsNormalized::Maybe => result = IsNormalized::Maybe,
            IsNormalized::Yes => {},
        }
        last_ccc = ccc;
        tail_pos += width;
    }

    result
}

/// Scalar quick-check for short inputs (< 64 bytes).
#[inline]
fn quick_check_scalar(
    input: &str,
    qc_shift: u32,
    safe_below: u32,
    hangul_safe: bool,
    kana_safe: bool,
    latin1_upper_safe: bool,
) -> IsNormalized {
    let mut last_ccc: u8 = 0;
    let mut result = IsNormalized::Yes;

    for ch in input.chars() {
        let cp = ch as u32;

        // ASCII fast path
        if cp <= 0x7F {
            last_ccc = 0;
            continue;
        }

        // Fast path: known-safe code point ranges (CCC=0 and QC=Yes).
        if cp < safe_below
            || (latin1_upper_safe && (0x00C0..0x0100).contains(&cp))
            || is_cjk_unified(cp)
            || (hangul_safe && (0xAC00..=0xD7A3).contains(&cp))
            || (kana_safe && is_kana(cp))
            || (cp >= 0x10000 && is_supp_safe(cp))
        {
            last_ccc = 0;
            continue;
        }

        let (ccc, qc) = tables::lookup_ccc_qc(ch, qc_shift);

        // CCC must be non-decreasing among non-zero values.
        if ccc != 0 && last_ccc > ccc {
            return IsNormalized::No;
        }

        match qc_value_to_result(qc) {
            IsNormalized::No => return IsNormalized::No,
            IsNormalized::Maybe => result = IsNormalized::Maybe,
            IsNormalized::Yes => {},
        }

        last_ccc = ccc;
    }

    result
}

// ---------------------------------------------------------------------------
// SIMD bound and safe-below thresholds by normalization form
// ---------------------------------------------------------------------------
//
// NFC:  simd_bound=0xCC, safe_below=0x0300, hangul_safe=true, kana_safe=true
//       All chars U+0000..U+02FF have CCC=0 and NFC_QC=Yes.
//       The first CCC != 0 is U+0300 (lead byte 0xCC).
//       CJK Unified, Hangul Syllables, and Hiragana/Katakana are NFC-safe.
//
// NFD:  simd_bound=0xC3, safe_below=0x00C0, hangul_safe=false, kana_safe=false
//       U+00C0 is first NFD_QC=No (lead byte 0xC3).
//       Hangul Syllables and some kana have NFD_QC=No (they decompose).
//
// NFKC: simd_bound=0xC0, safe_below=0x00A0, hangul_safe=true, kana_safe=true
//       U+00A0 is first NFKC_QC=No (NBSP → SPACE).
//       Kana are NFKC-safe (only halfwidth/enclosed forms decompose, in other blocks).
//
// NFKD: simd_bound=0xC0, safe_below=0x00A0, hangul_safe=false, kana_safe=false
//       Same as NFKC threshold, but Hangul and some kana decompose.

/// Quick-check whether `input` is in NFC.
#[cfg(not(feature = "quick_check_oracle"))]
pub(crate) fn quick_check_nfc(input: &str) -> IsNormalized {
    quick_check_impl(
        input,
        tables::CCC_QC_NFC_SHIFT,
        0xCC,
        0x0300,
        true,
        true,
        true,
    )
}

/// Quick-check whether `input` is in NFC.
#[cfg(feature = "quick_check_oracle")]
pub fn quick_check_nfc(input: &str) -> IsNormalized {
    quick_check_impl(
        input,
        tables::CCC_QC_NFC_SHIFT,
        0xCC,
        0x0300,
        true,
        true,
        true,
    )
}

/// Quick-check whether `input` is in NFD.
#[cfg(not(feature = "quick_check_oracle"))]
pub(crate) fn quick_check_nfd(input: &str) -> IsNormalized {
    quick_check_impl(
        input,
        tables::CCC_QC_NFD_SHIFT,
        0xC3,
        0x00C0,
        false,
        false,
        false,
    )
}

/// Quick-check whether `input` is in NFD.
#[cfg(feature = "quick_check_oracle")]
pub fn quick_check_nfd(input: &str) -> IsNormalized {
    quick_check_impl(
        input,
        tables::CCC_QC_NFD_SHIFT,
        0xC3,
        0x00C0,
        false,
        false,
        false,
    )
}

/// Quick-check whether `input` is in NFKC.
#[cfg(not(feature = "quick_check_oracle"))]
pub(crate) fn quick_check_nfkc(input: &str) -> IsNormalized {
    quick_check_impl(
        input,
        tables::CCC_QC_NFKC_SHIFT,
        0xC0,
        0x00A0,
        true,
        true,
        true,
    )
}

/// Quick-check whether `input` is in NFKC.
#[cfg(feature = "quick_check_oracle")]
pub fn quick_check_nfkc(input: &str) -> IsNormalized {
    quick_check_impl(
        input,
        tables::CCC_QC_NFKC_SHIFT,
        0xC0,
        0x00A0,
        true,
        true,
        true,
    )
}

/// Quick-check whether `input` is in NFKD.
#[cfg(not(feature = "quick_check_oracle"))]
pub(crate) fn quick_check_nfkd(input: &str) -> IsNormalized {
    quick_check_impl(
        input,
        tables::CCC_QC_NFKD_SHIFT,
        0xC0,
        0x00A0,
        false,
        false,
        false,
    )
}

/// Quick-check whether `input` is in NFKD.
#[cfg(feature = "quick_check_oracle")]
pub fn quick_check_nfkd(input: &str) -> IsNormalized {
    quick_check_impl(
        input,
        tables::CCC_QC_NFKD_SHIFT,
        0xC0,
        0x00A0,
        false,
        false,
        false,
    )
}

// ---------------------------------------------------------------------------
// Oracle (slow-path) implementation for differential testing.
// ---------------------------------------------------------------------------
//
// The oracle deliberately routes every flagged byte through the Layer-2
// decode + range + trie path, i.e. it calls `simd::scan_chunk` (no
// safe_lead_mask) and omits the short-circuit. It exists so that
// tests/quick_check_fastpath_equivalence.rs can assert
// `quick_check_X(s) == quick_check_X_oracle(s)` for every form, 8192
// cases per form, on arbitrary Unicode input.

#[cfg(feature = "quick_check_oracle")]
#[inline]
fn quick_check_impl_oracle(
    input: &str,
    qc_shift: u32,
    simd_bound: u8,
    safe_below: u32,
    hangul_safe: bool,
    kana_safe: bool,
) -> IsNormalized {
    let bytes = input.as_bytes();
    let len = bytes.len();

    if len < 64 {
        return quick_check_scalar(input, qc_shift, safe_below, hangul_safe, kana_safe, false);
    }

    let ptr = bytes.as_ptr();
    let mut last_ccc: u8 = 0;
    let mut result = IsNormalized::Yes;
    let mut processed_up_to: usize = 0;
    let mut pos: usize = 0;

    while pos + 64 <= len {
        // SAFETY: pos + 64 <= len, so ptr.add(pos) is valid for 64 bytes.
        let mask = unsafe { simd::scan_chunk(ptr.add(pos), simd_bound) };
        let chunk_end = pos + 64;

        if mask == 0 {
            last_ccc = 0;
            processed_up_to = chunk_end;
            pos = chunk_end;
            continue;
        }

        let chunk_start = pos;
        let mut chunk_mask = mask;
        while chunk_mask != 0 {
            let bit_pos = chunk_mask.trailing_zeros() as usize;
            chunk_mask &= chunk_mask.wrapping_sub(1);

            let byte_pos = chunk_start + bit_pos;
            if byte_pos < processed_up_to {
                continue;
            }
            if byte_pos > processed_up_to {
                last_ccc = 0;
            }

            let (ch, width) = utf8::decode_char_at(bytes, byte_pos);
            processed_up_to = byte_pos + width;

            let cp = ch as u32;
            if cp < safe_below
                || is_cjk_unified(cp)
                || (hangul_safe && (0xAC00..=0xD7A3).contains(&cp))
                || (kana_safe && is_kana(cp))
                || (cp >= 0x10000 && is_supp_safe(cp))
            {
                last_ccc = 0;
                continue;
            }

            let (ccc, qc) = tables::lookup_ccc_qc(ch, qc_shift);
            if ccc != 0 && last_ccc > ccc {
                return IsNormalized::No;
            }
            match qc_value_to_result(qc) {
                IsNormalized::No => return IsNormalized::No,
                IsNormalized::Maybe => result = IsNormalized::Maybe,
                IsNormalized::Yes => {},
            }
            last_ccc = ccc;
        }

        if processed_up_to < chunk_end {
            last_ccc = 0;
            processed_up_to = chunk_end;
        }
        pos = chunk_end;
    }

    // Scalar tail (identical to quick_check_impl; duplicated verbatim so
    // the oracle stays a single self-contained function).
    let tail_start = processed_up_to.max(pos);
    if tail_start > processed_up_to {
        last_ccc = 0;
    }
    let mut tail_pos = tail_start;
    while tail_pos < len {
        let b = bytes[tail_pos];
        if b < 0x80 {
            last_ccc = 0;
            tail_pos += 1;
            continue;
        }
        if utf8::is_continuation_byte(b) {
            tail_pos += 1;
            continue;
        }
        let (ch, width) = utf8::decode_char_at(bytes, tail_pos);
        let cp = ch as u32;
        if cp < safe_below
            || is_cjk_unified(cp)
            || (hangul_safe && (0xAC00..=0xD7A3).contains(&cp))
            || (cp >= 0x10000 && is_supp_safe(cp))
        {
            last_ccc = 0;
            tail_pos += width;
            continue;
        }
        let (ccc, qc) = tables::lookup_ccc_qc(ch, qc_shift);
        if ccc != 0 && last_ccc > ccc {
            return IsNormalized::No;
        }
        match qc_value_to_result(qc) {
            IsNormalized::No => return IsNormalized::No,
            IsNormalized::Maybe => result = IsNormalized::Maybe,
            IsNormalized::Yes => {},
        }
        last_ccc = ccc;
        tail_pos += width;
    }

    result
}

/// Oracle NFC quick-check. Differential-testing only.
#[cfg(feature = "quick_check_oracle")]
pub fn quick_check_nfc_oracle(input: &str) -> IsNormalized {
    quick_check_impl_oracle(input, tables::CCC_QC_NFC_SHIFT, 0xCC, 0x0300, true, true)
}

/// Oracle NFD quick-check. Differential-testing only.
#[cfg(feature = "quick_check_oracle")]
pub fn quick_check_nfd_oracle(input: &str) -> IsNormalized {
    quick_check_impl_oracle(input, tables::CCC_QC_NFD_SHIFT, 0xC3, 0x00C0, false, false)
}

/// Oracle NFKC quick-check. Differential-testing only.
#[cfg(feature = "quick_check_oracle")]
pub fn quick_check_nfkc_oracle(input: &str) -> IsNormalized {
    quick_check_impl_oracle(input, tables::CCC_QC_NFKC_SHIFT, 0xC0, 0x00A0, true, true)
}

/// Oracle NFKD quick-check. Differential-testing only.
#[cfg(feature = "quick_check_oracle")]
pub fn quick_check_nfkd_oracle(input: &str) -> IsNormalized {
    quick_check_impl_oracle(input, tables::CCC_QC_NFKD_SHIFT, 0xC0, 0x00A0, false, false)
}

// ---------------------------------------------------------------------------
// Definitive is_normalized checks (resolve Maybe via full normalization)
// ---------------------------------------------------------------------------
//
// These delegate to the main normalizer for the Maybe case, ensuring the
// quick-check resolution uses the same code path as actual normalization.

/// Definitive NFC check.
pub(crate) fn is_normalized_nfc(input: &str) -> bool {
    match quick_check_nfc(input) {
        IsNormalized::Yes => true,
        IsNormalized::No => false,
        IsNormalized::Maybe => &*crate::nfc().normalize(input) == input,
    }
}

/// Definitive NFD check.
pub(crate) fn is_normalized_nfd(input: &str) -> bool {
    match quick_check_nfd(input) {
        IsNormalized::Yes => true,
        IsNormalized::No => false,
        IsNormalized::Maybe => &*crate::nfd().normalize(input) == input,
    }
}

/// Definitive NFKC check.
pub(crate) fn is_normalized_nfkc(input: &str) -> bool {
    match quick_check_nfkc(input) {
        IsNormalized::Yes => true,
        IsNormalized::No => false,
        IsNormalized::Maybe => &*crate::nfkc().normalize(input) == input,
    }
}

/// Definitive NFKD check.
pub(crate) fn is_normalized_nfkd(input: &str) -> bool {
    match quick_check_nfkd(input) {
        IsNormalized::Yes => true,
        IsNormalized::No => false,
        IsNormalized::Maybe => &*crate::nfkd().normalize(input) == input,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::format;
    use alloc::string::String;

    // ---- ASCII fast path ----

    #[test]
    fn ascii_is_nfc() {
        assert_eq!(quick_check_nfc("Hello, world!"), IsNormalized::Yes);
    }

    #[test]
    fn ascii_is_nfd() {
        assert_eq!(quick_check_nfd("Hello, world!"), IsNormalized::Yes);
    }

    #[test]
    fn ascii_is_nfkc() {
        assert_eq!(quick_check_nfkc("Hello, world!"), IsNormalized::Yes);
    }

    #[test]
    fn ascii_is_nfkd() {
        assert_eq!(quick_check_nfkd("Hello, world!"), IsNormalized::Yes);
    }

    #[test]
    fn empty_string_is_normalized() {
        assert_eq!(quick_check_nfc(""), IsNormalized::Yes);
        assert_eq!(quick_check_nfd(""), IsNormalized::Yes);
        assert_eq!(quick_check_nfkc(""), IsNormalized::Yes);
        assert_eq!(quick_check_nfkd(""), IsNormalized::Yes);
    }

    // ---- NFC checks ----

    #[test]
    fn precomposed_is_nfc_yes() {
        assert_eq!(quick_check_nfc("\u{00E9}"), IsNormalized::Yes);
    }

    #[test]
    fn decomposed_is_not_nfc() {
        let nfd = "e\u{0301}";
        let result = quick_check_nfc(nfd);
        assert!(
            result == IsNormalized::No || result == IsNormalized::Maybe,
            "NFD form must not be Yes for NFC, got {:?}",
            result,
        );
    }

    // ---- NFD checks ----

    #[test]
    fn precomposed_is_not_nfd() {
        assert_eq!(quick_check_nfd("\u{00E9}"), IsNormalized::No);
    }

    // ---- CCC ordering ----

    #[test]
    fn wrong_ccc_order_is_no() {
        let bad_order = "a\u{0301}\u{0327}"; // acute(230) then cedilla(202)
        assert_eq!(quick_check_nfc(bad_order), IsNormalized::No);
        assert_eq!(quick_check_nfd(bad_order), IsNormalized::No);
    }

    #[test]
    fn correct_ccc_order_not_rejected() {
        // Use Hebrew accents which are NFC_QC=Yes but have non-zero CCC.
        // U+0591 HEBREW ACCENT ETNAHTA (CCC=220), U+05A1 HEBREW ACCENT PAZER (CCC=230)
        let good_order = "a\u{0591}\u{05A1}";
        let result = quick_check_nfc(good_order);
        assert_ne!(result, IsNormalized::No);
    }

    // ---- Range fast path tests ----

    #[test]
    fn latin1_supplement_is_nfc() {
        // U+00C0..U+00FF are all NFC_QC=Yes
        let latin1 = "\u{00C0}\u{00E9}\u{00F6}\u{00FC}\u{00FF}";
        assert_eq!(quick_check_nfc(latin1), IsNormalized::Yes);
    }

    #[test]
    fn latin_extended_is_nfc() {
        // U+0100..U+02FF are all NFC_QC=Yes
        let extended = "\u{0100}\u{017E}\u{0250}\u{02FF}";
        assert_eq!(quick_check_nfc(extended), IsNormalized::Yes);
    }

    #[test]
    fn cjk_is_nfc() {
        let cjk = "\u{4E00}\u{9FFF}\u{3400}\u{4DBF}";
        assert_eq!(quick_check_nfc(cjk), IsNormalized::Yes);
    }

    #[test]
    fn hangul_syllable_is_nfc() {
        let hangul = "\u{AC00}\u{D7A3}";
        assert_eq!(quick_check_nfc(hangul), IsNormalized::Yes);
    }

    #[test]
    fn hangul_syllable_is_not_nfd() {
        let hangul = "\u{AC00}";
        assert_eq!(quick_check_nfd(hangul), IsNormalized::No);
    }

    #[test]
    fn latin1_is_not_nfd() {
        // U+00C0 decomposes in NFD
        assert_eq!(quick_check_nfd("\u{00C0}"), IsNormalized::No);
    }

    #[test]
    fn nbsp_is_not_nfkc() {
        // U+00A0 (NBSP) → U+0020 (SPACE) in NFKC
        assert_eq!(quick_check_nfkc("\u{00A0}"), IsNormalized::No);
    }

    // ---- is_normalized definitive checks ----

    #[test]
    fn is_normalized_nfc_ascii() {
        assert!(is_normalized_nfc("Hello"));
    }

    #[test]
    fn is_normalized_nfc_precomposed() {
        assert!(is_normalized_nfc("\u{00E9}"));
    }

    #[test]
    fn is_normalized_nfd_decomposed() {
        assert!(is_normalized_nfd("e\u{0301}"));
    }

    #[test]
    fn is_normalized_nfc_rejects_nfd() {
        assert!(!is_normalized_nfc("e\u{0301}"));
    }

    #[test]
    fn is_normalized_nfd_rejects_nfc() {
        assert!(!is_normalized_nfd("\u{00E9}"));
    }

    #[test]
    fn safe_lead_interleaved_with_combining_marks_across_chunk() {
        // 128 bytes spanning two SIMD chunks.
        // Pattern: CJK ideograph (3 bytes, lead 0xE4..=0xE9, safe-lead) +
        //          'a' (1 byte, ASCII) +
        //          U+0591 HEBREW ACCENT ETNAHTA (CCC=220, NFC_QC=Yes, lead 0xD6 -> decode path).
        // The U+0591 must be observed after a safe-lead reset of last_ccc so that
        // the *next* non-zero CCC mark is accepted as non-decreasing.
        //
        // 16 repetitions of (CJK=3 + 'a'=1 + U+0591=2 + 'b'=1 + 'b'=1) = 16 * 8 = 128 bytes.
        let unit = "\u{4E2D}a\u{0591}bb";
        let s: String = unit.repeat(16);
        assert_eq!(s.len(), 128);
        // All code points are NFC_QC=Yes with monotonic (or zero) CCC, so NFC=Yes.
        assert_eq!(quick_check_nfc(&s), IsNormalized::Yes);
        // NFD: U+0591 is NFD_QC=Yes, CJK Unified is NFD_QC=Yes, ASCII is safe.
        assert_eq!(quick_check_nfd(&s), IsNormalized::Yes);
        assert_eq!(quick_check_nfkc(&s), IsNormalized::Yes);
        assert_eq!(quick_check_nfkd(&s), IsNormalized::Yes);
    }

    #[test]
    fn safe_lead_then_out_of_order_combining_is_no() {
        // Regression: if the safe-lead short-circuit fails to set last_ccc=0,
        // a subsequent same-position CCC check could mis-order. Build an input
        // where CJK (CCC=0 safe-lead) is followed by a correctly-ordered
        // combining sequence, then a mis-ordered one; expect No.
        // U+0301 ACUTE (CCC=230), U+0327 CEDILLA (CCC=202).
        let unit = "\u{4E2D}a\u{0301}\u{0327}"; // bad order after safe-lead + ASCII
        let padding = "x".repeat(64); // force >= 64-byte path
        let s = format!("{}{}", padding, unit);
        assert!(s.len() >= 64);
        assert_eq!(quick_check_nfc(&s), IsNormalized::No);
    }

    #[cfg(feature = "quick_check_oracle")]
    #[test]
    fn oracle_matches_fastpath_on_fixed_input() {
        let s = "\u{4E2D}a\u{0591}bb".repeat(16);
        assert_eq!(quick_check_nfc(&s), super::quick_check_nfc_oracle(&s));
        assert_eq!(quick_check_nfd(&s), super::quick_check_nfd_oracle(&s));
        assert_eq!(quick_check_nfkc(&s), super::quick_check_nfkc_oracle(&s));
        assert_eq!(quick_check_nfkd(&s), super::quick_check_nfkd_oracle(&s));
    }
}