normy 0.1.4

Ultra-fast, zero-copy text normalization for Rust NLP pipelines & tokenizers
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
pub mod data;

use crate::{ENG, LANG_TABLE};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Lang {
    pub code: &'static str,
    pub name: &'static str,
}

impl Lang {
    #[inline(always)]
    pub const fn code(&self) -> &'static str {
        self.code
    }
    #[inline(always)]
    pub const fn name(&self) -> &'static str {
        self.name
    }
}

pub const DEFAULT_LANG: Lang = ENG;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SegmentRule {
    WesternToScript,
    ScriptToWestern,
    CJKIdeographUnigram,
}

#[derive(Clone, Copy, Debug)]
pub struct LangEntry {
    // === Precomputed Boolean Flags (Hot Path - First Cache Line) ===
    has_case_map: bool,
    has_fold_map: bool,
    has_transliterate_map: bool,
    has_pre_composed_to_base_map: bool,
    has_spacing_diacritics: bool,
    has_segment_rules: bool,

    // Derived properties
    has_one_to_one_folds: bool,
    has_one_to_one_transliterate: bool,

    // Already boolean from definition
    needs_segmentation: bool,
    unigram_cjk: bool,

    // === Data Arrays (Second Cache Line+) ===
    code: &'static str,
    case_map: &'static [(char, char)],
    fold_map: &'static [(char, &'static str)],
    pre_composed_to_base_map: &'static [(char, char)],
    pre_composed_to_base_char_slice: &'static [char],
    spacing_diacritics: &'static [char],
    transliterate_map: &'static [(char, &'static str)],
    transliterate_char_slice: &'static [char],
    segment_rules: &'static [SegmentRule],
}

impl LangEntry {
    // ============================================================
    // CATEGORY 1: Boolean Queries - Pattern: has_*
    // ============================================================

    #[inline(always)]
    pub fn has_case_map(&self) -> bool {
        self.has_case_map
    }

    #[inline(always)]
    pub fn has_fold_map(&self) -> bool {
        self.has_fold_map
    }

    #[inline(always)]
    pub fn has_transliterate_map(&self) -> bool {
        self.has_transliterate_map
    }

    #[inline(always)]
    pub fn has_pre_composed_to_base_map(&self) -> bool {
        self.has_pre_composed_to_base_map
    }

    #[inline(always)]
    pub fn has_spacing_diacritics(&self) -> bool {
        self.has_spacing_diacritics
    }

    #[inline(always)]
    pub fn has_segment_rules(&self) -> bool {
        self.has_segment_rules
    }

    #[inline(always)]
    pub fn has_one_to_one_folds(&self) -> bool {
        self.has_one_to_one_folds
    }

    #[inline(always)]
    pub fn has_one_to_one_transliterate(&self) -> bool {
        self.has_one_to_one_transliterate
    }

    #[inline(always)]
    pub fn has_pre_composed_to_base_map_or_spacing_diacritics(&self) -> bool {
        self.has_pre_composed_to_base_map || self.has_spacing_diacritics
    }

    // Semantic queries (keep existing names)
    #[inline(always)]
    pub fn needs_segmentation(&self) -> bool {
        self.needs_segmentation
    }

    #[inline(always)]
    pub fn needs_unigram_cjk(&self) -> bool {
        self.unigram_cjk
    }

    // ============================================================
    // CATEGORY 2: Character Checks - Pattern: is_*
    // ============================================================

    #[inline(always)]
    pub fn is_spacing_diacritic(&self, c: char) -> bool {
        self.spacing_diacritics.contains(&c)
    }

    #[inline(always)]
    pub fn is_transliterable(&self, c: char) -> bool {
        self.transliterate_char_slice.contains(&c)
    }

    #[inline(always)]
    pub fn is_pre_composed_to_base_char(&self, c: char) -> bool {
        self.pre_composed_to_base_char_slice.contains(&c)
    }

    // ============================================================
    // CATEGORY 3: Text Analysis - Pattern: needs_*
    // ============================================================

    #[inline(always)]
    pub fn needs_case_fold(&self, c: char) -> bool {
        self.fold_map.iter().any(|(from, _)| *from == c)
            || self.case_map.iter().any(|(from, _)| *from == c)
            || c.to_lowercase().next() != Some(c)
    }

    #[inline(always)]
    pub fn needs_lowercase(&self, c: char) -> bool {
        // Check uppercase FIRST (most common when work is needed)
        if c.to_lowercase().next() != Some(c) {
            return true;
        }

        // Then check custom mappings with direct comparisons for small maps
        match self.case_map.len() {
            0 => false,
            1 => c == self.case_map[0].0,
            2 => c == self.case_map[0].0 || c == self.case_map[1].0,
            3 => c == self.case_map[0].0 || c == self.case_map[1].0 || c == self.case_map[2].0,
            _ => self.case_map.iter().any(|(from, _)| *from == c),
        }
    }

    #[inline(always)]
    pub fn needs_pre_composed_to_base_map_or_spacing_diacritics_removal(&self, text: &str) -> bool {
        if self.has_pre_composed_to_base_map {
            text.chars()
                .any(|c| self.pre_composed_to_base_char_slice.contains(&c))
        } else {
            text.chars().any(|c| self.spacing_diacritics.contains(&c))
        }
    }

    // ============================================================
    // CATEGORY 4: Data Accessors - Pattern: direct_name()
    // ============================================================

    #[inline(always)]
    pub fn code(&self) -> &'static str {
        self.code
    }

    #[inline(always)]
    pub fn case_map(&self) -> &'static [(char, char)] {
        self.case_map
    }

    #[inline(always)]
    pub fn fold_map(&self) -> &'static [(char, &'static str)] {
        self.fold_map
    }

    #[inline(always)]
    pub fn transliterate_map(&self) -> &'static [(char, &'static str)] {
        self.transliterate_map
    }

    #[inline(always)]
    pub fn pre_composed_to_base_map(&self) -> &'static [(char, char)] {
        self.pre_composed_to_base_map
    }

    #[inline(always)]
    pub fn spacing_diacritics(&self) -> &'static [char] {
        self.spacing_diacritics
    }

    #[inline(always)]
    pub fn segment_rules(&self) -> &'static [SegmentRule] {
        self.segment_rules
    }

    #[inline(always)]
    pub fn transliterate_char_slice(&self) -> &'static [char] {
        self.transliterate_char_slice
    }

    #[inline(always)]
    pub fn pre_composed_to_base_char_slice(&self) -> &'static [char] {
        self.pre_composed_to_base_char_slice
    }

    // Finds a language-specific case map entry for a character.
    #[inline(always)]
    pub fn find_case_map(&self, c: char) -> Option<char> {
        self.case_map
            .iter()
            .find(|(from, _)| *from == c)
            .map(|(_, to)| *to)
    }

    // Finds a language-specific fold map entry for a character.
    #[inline(always)]
    pub fn find_fold_map(&self, c: char) -> Option<&'static str> {
        self.fold_map
            .iter()
            .find(|(from, _)| *from == c)
            .map(|(_, to)| *to)
    }

    #[inline(always)]
    pub fn find_transliterate_map(&self, c: char) -> Option<&'static str> {
        self.transliterate_map
            .iter()
            .find(|(from, _)| *from == c)
            .map(|(_, to)| *to)
    }

    #[inline(always)]
    pub fn find_pre_composed_to_base_map(&self, c: char) -> Option<char> {
        self.pre_composed_to_base_map
            .iter()
            .find(|(from, _)| *from == c)
            .map(|(_, to)| *to)
    }

    // ============================================================
    // CATEGORY 5: Transformations - Pattern: apply_* or get_*
    // ============================================================

    #[inline(always)]
    pub fn apply_case_fold(&self, c: char) -> Option<char> {
        if let Some(to) = self
            .fold_map
            .iter()
            .find(|(from, _)| *from == c)
            .map(|(_, to)| *to)
        {
            if self.has_one_to_one_folds {
                Some(to.chars().next().unwrap_or(c)) // Safe: we know it's 1 char
            } else {
                None
            }
        } else if let Some(to) = self
            .case_map
            .iter()
            .find(|(from, _)| *from == c)
            .map(|(_, to)| *to)
        {
            Some(to)
        } else {
            c.to_lowercase().next()
        }
    }

    #[inline(always)]
    pub fn apply_lowercase(&self, c: char) -> char {
        if let Some(to) = self
            .case_map
            .iter()
            .find(|(from, _)| *from == c)
            .map(|(_, to)| *to)
        {
            to
        } else {
            c.to_lowercase().next().unwrap_or(c)
        }
    }

    // ============================================================
    // CATEGORY 6: Capacity Hints - Pattern: hint_capacity_*
    // ============================================================

    #[inline]
    pub fn hint_capacity_fold(&self, text: &str) -> (usize, usize) {
        // Check flags once. No need to worry about has_one_to_one_folds.
        if !self.has_fold_map {
            return (0, 0);
        }

        let fold_map = self.fold_map;

        // --- Single-Pass, Unified Logic ---
        let mut num_folds = 0;
        let mut extra_bytes = 0;

        for c in text.chars() {
            if let Some(to) = fold_map
                .iter()
                .find(|(from, _)| *from == c)
                .map(|(_, to)| *to)
            {
                num_folds += 1;

                let from_len = c.len_utf8();
                let to_len = to.len();

                if to_len > from_len {
                    extra_bytes += to_len - from_len;
                }
            }
        }

        (num_folds, extra_bytes)
    }

    #[inline]
    pub fn hint_capacity_transliterate(&self, text: &str) -> (usize, usize) {
        if !self.has_transliterate_map {
            return (0, 0);
        }

        let map = self.transliterate_map;

        // --- Single-Pass, Unified Logic ---
        let mut num_transformations = 0;
        let mut extra_bytes = 0;

        for c in text.chars() {
            if let Some(to) = map.iter().find(|(from, _)| *from == c).map(|(_, to)| *to) {
                num_transformations += 1;

                let from_len = c.len_utf8();
                let to_len = to.len();

                if to_len > from_len {
                    extra_bytes += to_len - from_len;
                }
            }
        }

        (num_transformations, extra_bytes)
    }

    // ============================================================
    // CATEGORY 7: Setters - Pattern: set_*
    // Updates primary field and all dependent derived fields
    // ============================================================

    // Sets the case_map and updates has_case_map flag
    #[inline]
    pub fn set_case_map(&mut self, case_map: &'static [(char, char)]) {
        self.case_map = case_map;
        self.has_case_map = !case_map.is_empty();
    }

    // Sets the fold_map and updates all related fields
    #[inline]
    pub fn set_fold_map(&mut self, fold_map: &'static [(char, &'static str)]) {
        self.fold_map = fold_map;
        self.has_fold_map = !fold_map.is_empty();

        if !fold_map.is_empty() {
            self.has_one_to_one_folds = fold_map.iter().all(|(_, to)| to.chars().count() == 1);
        } else {
            self.has_one_to_one_folds = false;
        }
    }

    // Sets the transliterate_map and updates all related fields
    #[inline]
    pub fn set_transliterate_map(&mut self, transliterate_map: &'static [(char, &'static str)]) {
        self.transliterate_map = transliterate_map;
        self.has_transliterate_map = !transliterate_map.is_empty();

        if !transliterate_map.is_empty() {
            self.has_one_to_one_transliterate = transliterate_map
                .iter()
                .all(|(_, to)| to.chars().count() == 1);
        } else {
            self.has_one_to_one_transliterate = false;
        }
    }

    // Helper to set the transliterate_char_slice directly
    #[inline]
    pub fn set_transliterate_char_slice(&mut self, slice: &'static [char]) {
        self.transliterate_char_slice = slice;
    }

    // Sets the pre_composed_to_base_map and updates all related fields
    #[inline]
    pub fn set_pre_composed_to_base_map(&mut self, map: &'static [(char, char)]) {
        self.pre_composed_to_base_map = map;
        self.has_pre_composed_to_base_map = !map.is_empty();
    }

    // Helper to set the pre_composed_to_base_char_slice directly
    #[inline]
    pub fn set_pre_composed_to_base_char_slice(&mut self, slice: &'static [char]) {
        self.pre_composed_to_base_char_slice = slice;
    }

    // Sets the spacing_diacritics and updates all related fields
    #[inline]
    pub fn set_spacing_diacritics(&mut self, diacritics: &'static [char]) {
        self.spacing_diacritics = diacritics;
        self.has_spacing_diacritics = !diacritics.is_empty();
    }

    // Sets the needs_segmentation flag
    #[inline]
    pub fn set_needs_segmentation(&mut self, needs: bool) {
        self.needs_segmentation = needs;
    }

    // Sets the segment_rules and updates related fields
    #[inline]
    pub fn set_segment_rules(&mut self, rules: &'static [SegmentRule]) {
        self.segment_rules = rules;
        self.has_segment_rules = !rules.is_empty();

        // Auto-detect if CJK unigram rule is present
        self.unigram_cjk = rules.contains(&SegmentRule::CJKIdeographUnigram);
    }

    // Sets the unigram_cjk flag directly
    #[inline]
    pub fn set_unigram_cjk(&mut self, unigram: bool) {
        self.unigram_cjk = unigram;
    }
}

pub fn get_lang_entry_by_code(code: &str) -> Option<&'static LangEntry> {
    LANG_TABLE.get(&code.to_ascii_uppercase())
}

#[cfg(test)]
mod tests {
    use crate::{all_langs, lang::get_lang_entry_by_code};

    // Helper for concise test access
    fn lang(code: &str) -> &'static crate::lang::LangEntry {
        get_lang_entry_by_code(code).unwrap()
    }

    // ============================================================
    // CATEGORY 1: Language-Specific Behavior Tests
    // ============================================================

    #[test]
    fn turkish_case_folding() {
        let tur = lang("TUR");

        // Turkish-specific: İ→i, I→ı
        assert_eq!(tur.apply_case_fold('İ'), Some('i'));
        assert_eq!(tur.apply_case_fold('I'), Some('ı'));
        assert_eq!(tur.apply_lowercase('İ'), 'i');
        assert_eq!(tur.apply_lowercase('I'), 'ı');

        // Metadata checks
        assert!(tur.has_one_to_one_folds());
        assert!(!tur.case_map().is_empty(), "Turkish has custom case_map");
    }

    #[test]
    fn german_multi_char_folding() {
        let deu = lang("DEU");

        // German: ẞ→ß (lowercase) vs ẞ→ss (case fold)
        assert_eq!(deu.apply_lowercase(''), 'ß');
        assert_eq!(deu.apply_case_fold(''), None, "ẞ→ss is multi-char");
        assert_eq!(deu.apply_case_fold('ß'), None, "ß→ss is multi-char");

        // Metadata
        assert!(!deu.has_one_to_one_folds());
    }

    #[test]
    fn dutch_case_fold_ligatures() {
        let nld = lang("NLD");

        // IJ→ij is multi-char fold
        assert_eq!(nld.apply_case_fold('IJ'), None);
        assert_eq!(nld.apply_case_fold('ij'), None);

        // Metadata
        assert!(!nld.has_one_to_one_folds());
    }

    #[test]
    fn arabic_diacritics() {
        let ara = lang("ARA");

        assert!(ara.has_spacing_diacritics());
        assert!(ara.is_spacing_diacritic('\u{064E}'), "َ (fatha)");
        assert!(!ara.is_spacing_diacritic('ا'), "Base letter ا");
    }

    #[test]
    fn english_basic_case_folding() {
        let eng = lang("ENG");

        assert_eq!(eng.apply_case_fold('A'), Some('a'));
        assert_eq!(eng.apply_case_fold('Z'), Some('z'));
        assert_eq!(eng.apply_lowercase('A'), 'a');

        assert!(eng.has_one_to_one_folds());
        assert!(eng.case_map().is_empty(), "English uses Unicode defaults");
    }

    // ============================================================
    // CATEGORY 2: Cross-Language Consistency Tests
    // ============================================================

    #[test]
    fn segmentation_languages() {
        // Languages requiring word segmentation
        let needs_seg = ["JPN", "ZHO", "KOR", "THA", "MYA", "KHM"];
        for code in needs_seg {
            assert!(
                lang(code).needs_segmentation(),
                "{} needs segmentation",
                code
            );
        }

        // Languages with space-delimited words
        let no_seg = ["ENG", "TUR", "DEU", "FRA", "ARA"];
        for code in no_seg {
            assert!(
                !lang(code).needs_segmentation(),
                "{} doesn't need segmentation",
                code
            );
        }
    }

    #[test]
    fn case_map_exclusivity() {
        // Only Turkish has custom case_map (for İ/I)
        assert!(!lang("TUR").case_map().is_empty());

        // All others use Unicode defaults
        for code in ["ENG", "DEU", "FRA", "ARA", "NLD", "POL"] {
            assert!(
                lang(code).case_map().is_empty(),
                "{} should use Unicode case",
                code
            );
        }
    }

    // ============================================================
    // CATEGORY 3: Metadata Consistency Tests
    // ============================================================

    #[test]
    fn all_languages_metadata_valid() {
        for lang_info in all_langs() {
            let entry = lang(lang_info.code());

            // has_one_to_one_folds correctness
            if entry.has_one_to_one_folds() {
                for (from, to) in entry.fold_map() {
                    assert_eq!(
                        to.chars().count(),
                        1,
                        "{}: {} → {} violates one_to_one",
                        lang_info.code(),
                        from,
                        to
                    );
                }
            }

            // Spacing diacritics consistency
            if entry.has_spacing_diacritics() {
                assert!(
                    !entry.spacing_diacritics().is_empty(),
                    "{}: has_spacing_diacritics but field is None",
                    lang_info.code()
                );
            }
        }
    }

    #[test]
    fn fold_targets_already_lowercase() {
        // Fold targets must be idempotent (already in lowercase form)
        let test_langs = ["TUR", "DEU", "NLD", "FRA", "POL", "CES"];

        for code in test_langs {
            for (_, to) in lang(code).fold_map() {
                let target_lower: String = to.chars().flat_map(|c| c.to_lowercase()).collect();
                assert_eq!(
                    *to, target_lower,
                    "{}: fold target '{}' not lowercase",
                    code, to
                );
            }
        }
    }

    // ============================================================
    // CATEGORY 4: API Contract Tests
    // ============================================================

    #[test]
    fn apply_case_fold_rejects_multi_char() {
        // Case fold returns None for multi-char expansions
        assert_eq!(lang("DEU").apply_case_fold('ß'), None, "ß→ss");
        assert_eq!(lang("DEU").apply_case_fold(''), None, "ẞ→ss");
        assert_eq!(lang("NLD").apply_case_fold('IJ'), None, "IJ→ij");

        // But accepts one-to-one
        assert_eq!(lang("TUR").apply_case_fold('İ'), Some('i'));
        assert_eq!(lang("DEU").apply_case_fold('A'), Some('a'));
    }

    #[test]
    fn apply_lowercase_always_succeeds() {
        // Lowercase is always 1→1, never fails
        assert_eq!(lang("TUR").apply_lowercase('İ'), 'i');
        assert_eq!(lang("DEU").apply_lowercase(''), 'ß', "ẞ→ß, NOT →ss");
        assert_eq!(lang("ARA").apply_lowercase('ا'), 'ا', "Already lowercase");

        // Preserves grapheme count
        let text = "İIıiẞßABC";
        for c in text.chars() {
            let result = lang("TUR").apply_lowercase(c);
            assert_eq!(result.to_string().chars().count(), 1);
        }
    }

    #[test]
    fn fold_vs_lowercase_difference() {
        // Key difference: lowercase is 1→1, fold can be 1→n

        // German ẞ: lowercase→ß (1→1), fold→ss (1→2)
        assert_eq!(lang("DEU").apply_lowercase(''), 'ß');
        assert_eq!(lang("DEU").apply_case_fold(''), None);

        // This is why: DEU supports lowercase but not apply_case_fold
        assert!(!lang("DEU").has_one_to_one_folds());
    }

    #[test]
    fn get_lang_entry_by_code_case_insensitive() {
        assert!(get_lang_entry_by_code("TUR").is_some());
        assert!(get_lang_entry_by_code("tur").is_some());
        assert!(get_lang_entry_by_code("Tur").is_some());
        assert!(get_lang_entry_by_code("XXX").is_none());
    }

    // ============================================================
    // CATEGORY 5: Performance Characteristics Tests
    // ============================================================

    #[test]
    fn needs_case_fold_is_fast() {
        // Verify O(1) or O(log n) lookup performance
        let text = "A".repeat(1000);
        let start = std::time::Instant::now();

        let count = text
            .chars()
            .filter(|&c| lang("ENG").needs_case_fold(c))
            .count();

        let elapsed = start.elapsed();
        assert_eq!(count, 1000);
        assert!(elapsed.as_millis() < 10, "Should be sub-millisecond");
    }

    #[test]
    fn one_to_one_preserves_grapheme_count() {
        let cases = [
            ("ABCabc", "ENG"), // ASCII
            ("éÉèÈ", "FRA"),   // Latin accents
            ("İIıi", "TUR"),   // Turkish dotted-I
        ];

        for (text, code) in cases {
            let entry = lang(code);
            let folded: String = text
                .chars()
                .filter_map(|c| entry.apply_case_fold(c))
                .collect();

            assert_eq!(
                text.chars().count(),
                folded.chars().count(),
                "{} grapheme count changed for {}",
                code,
                text
            );
        }
    }

    // ============================================================
    // CATEGORY 6: Edge Cases
    // ============================================================

    #[test]
    fn empty_text_operations() {
        let entry = lang("ENG");

        assert_eq!(entry.hint_capacity_fold(""), (0, 0));
        assert_eq!(entry.hint_capacity_transliterate(""), (0, 0));
        assert!(!entry.needs_pre_composed_to_base_map_or_spacing_diacritics_removal(""));
    }

    #[test]
    fn ascii_fast_paths() {
        let entry = lang("ENG");

        // ASCII lowercase should be fast rejection
        assert!(!entry.needs_case_fold('a'));
        assert!(!entry.needs_lowercase('a'));

        // ASCII uppercase needs folding
        assert!(entry.needs_case_fold('A'));
        assert!(entry.needs_lowercase('A'));
    }
}