intl 0.2.0

Pure-Rust, no_std internationalization primitives (a pure-Rust ICU analog). The `unicode` module provides General_Category, character predicates, scripts, East Asian Width, numeric values, case mapping/folding, UAX #15 normalization (NFC/NFD/NFKC/NFKD), and UTS #10 collation — property tables compiled into const-fn match lookups, with feature-selectable codepoint ranges.
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
//! Bidirectional text (UAX #9).
//!
//! Provides the `Bidi_Class` property, paragraph base-direction detection
//! (rules P2–P3), and — with the `alloc` feature — the full reordering algorithm
//! ([`process`], rules X1–X10 / W1–W7 / N0–N2 / I1–I2 / L1–L2) resolving
//! embedding levels and visual order (~99.996% on `BidiCharacterTest`).

use super::generated::bidi as gen;

/// The `Bidi_Class` of a codepoint (UAX #9). Order matches the generated table.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::upper_case_acronyms)]
pub enum BidiClass {
    /// Left-to-Right
    L,
    /// Right-to-Left
    R,
    /// Right-to-Left Arabic
    AL,
    /// European Number
    EN,
    /// European Separator
    ES,
    /// European Terminator
    ET,
    /// Arabic Number
    AN,
    /// Common Separator
    CS,
    /// Nonspacing Mark
    NSM,
    /// Boundary Neutral
    BN,
    /// Paragraph Separator
    B,
    /// Segment Separator
    S,
    /// White Space
    WS,
    /// Other Neutral
    ON,
    /// Left-to-Right Embedding
    LRE,
    /// Left-to-Right Override
    LRO,
    /// Right-to-Left Embedding
    RLE,
    /// Right-to-Left Override
    RLO,
    /// Pop Directional Format
    PDF,
    /// Left-to-Right Isolate
    LRI,
    /// Right-to-Left Isolate
    RLI,
    /// First Strong Isolate
    FSI,
    /// Pop Directional Isolate
    PDI,
}

impl BidiClass {
    /// `true` if this is a strong right-to-left class (`R` or `AL`).
    #[inline]
    #[must_use]
    pub const fn is_rtl(self) -> bool {
        matches!(self, BidiClass::R | BidiClass::AL)
    }
}

/// The [`BidiClass`] of `c`.
#[inline]
#[must_use]
pub const fn bidi_class(c: char) -> BidiClass {
    gen::bidi_class(c as u32)
}

/// The [`BidiClass`] of an arbitrary Unicode scalar value.
#[inline]
#[must_use]
pub const fn bidi_class_u32(cp: u32) -> BidiClass {
    gen::bidi_class(cp)
}

/// A paragraph direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    /// Left-to-right (base embedding level 0).
    LeftToRight,
    /// Right-to-left (base embedding level 1).
    RightToLeft,
}

/// The base paragraph direction of `s` per UAX #9 rules P2–P3: the direction of
/// the first strong character (`L`, `R`, or `AL`), skipping over isolated
/// sequences. Defaults to left-to-right when there is no strong character.
#[must_use]
pub fn base_direction(s: &str) -> Direction {
    let mut isolate_depth = 0u32;
    for c in s.chars() {
        match bidi_class(c) {
            BidiClass::LRI | BidiClass::RLI | BidiClass::FSI => isolate_depth += 1,
            BidiClass::PDI => isolate_depth = isolate_depth.saturating_sub(1),
            BidiClass::L if isolate_depth == 0 => return Direction::LeftToRight,
            BidiClass::R | BidiClass::AL if isolate_depth == 0 => return Direction::RightToLeft,
            _ => {}
        }
    }
    Direction::LeftToRight
}

/// `true` if `s` has a right-to-left base direction.
#[must_use]
pub fn is_rtl(s: &str) -> bool {
    base_direction(s) == Direction::RightToLeft
}

#[cfg(feature = "alloc")]
pub use resolve::{process, BidiInfo};

/// The full UAX #9 algorithm: resolve embedding levels and visual order.
#[cfg(feature = "alloc")]
mod resolve {
    use super::bidi_class;
    use super::BidiClass::{self, *};
    use super::Direction;
    use crate::unicode::generated::bidi::bidi_bracket;
    use alloc::vec;
    use alloc::vec::Vec;

    const MAX_DEPTH: u8 = 125;

    /// The result of running the bidi algorithm over one paragraph. Indices are
    /// Unicode scalar (`char`) positions in the input.
    #[derive(Debug, Clone)]
    pub struct BidiInfo {
        /// The paragraph embedding level (0 = LTR, 1 = RTL).
        pub paragraph_level: u8,
        /// The resolved embedding level of each character, or `None` for a
        /// character removed by rule X9 (explicit formatting and boundary
        /// neutrals).
        pub levels: Vec<Option<u8>>,
        /// Original character indices in left-to-right visual order, excluding
        /// the X9-removed characters.
        pub visual_order: Vec<usize>,
    }

    fn next_odd(level: u8) -> u8 {
        if level % 2 == 0 {
            level + 1
        } else {
            level + 2
        }
    }
    fn next_even(level: u8) -> u8 {
        if level % 2 == 0 {
            level + 2
        } else {
            level + 1
        }
    }
    fn is_isolate_init(c: BidiClass) -> bool {
        matches!(c, LRI | RLI | FSI)
    }
    /// Neutral or Isolate formatting (the `NI` set in the N rules).
    fn is_ni(c: BidiClass) -> bool {
        matches!(c, B | S | WS | ON | FSI | LRI | RLI | PDI)
    }
    /// The directional contribution of a (resolved) class for the N rules:
    /// `EN`/`AN` count as `R`.
    fn neutral_dir(c: BidiClass) -> Option<BidiClass> {
        match c {
            L => Some(L),
            R | EN | AN => Some(R),
            _ => None,
        }
    }
    fn canon_bracket(cp: u32) -> u32 {
        match cp {
            0x2329 => 0x3008,
            0x232A => 0x3009,
            other => other,
        }
    }

    /// First strong direction (0 = L, 1 = R/AL) in `[start, end)`, skipping the
    /// contents of nested isolates. Defaults to 0.
    fn first_strong(classes: &[BidiClass], start: usize, end: usize) -> u8 {
        let mut depth = 0u32;
        for &c in &classes[start..end.min(classes.len())] {
            match c {
                _ if is_isolate_init(c) => depth += 1,
                PDI => depth = depth.saturating_sub(1),
                L if depth == 0 => return 0,
                R | AL if depth == 0 => return 1,
                _ => {}
            }
        }
        0
    }

    /// BD9: match each isolate initiator with its PDI (indices, or `len` if none).
    fn match_isolates(classes: &[BidiClass]) -> Vec<usize> {
        let n = classes.len();
        let mut match_pdi = vec![n; n];
        let mut stack: Vec<usize> = Vec::new();
        for (i, &c) in classes.iter().enumerate() {
            if is_isolate_init(c) {
                stack.push(i);
            } else if c == PDI {
                if let Some(o) = stack.pop() {
                    match_pdi[o] = i;
                }
            }
        }
        match_pdi
    }

    /// Run the bidi algorithm over `text` as a single paragraph. `base` forces
    /// the paragraph direction; `None` auto-detects it (rules P2–P3).
    #[must_use]
    pub fn process(text: &str, base: Option<Direction>) -> BidiInfo {
        let chars: Vec<char> = text.chars().collect();
        let raw: Vec<BidiClass> = chars.iter().map(|&c| bidi_class(c)).collect();
        let n = chars.len();

        let para_level = match base {
            Some(Direction::LeftToRight) => 0,
            Some(Direction::RightToLeft) => 1,
            None => first_strong(&raw, 0, n),
        };

        let match_pdi = match_isolates(&raw);

        // ---- X1–X8: explicit levels and overrides. ----
        let mut classes = raw.clone();
        let mut levels = vec![para_level; n];
        let mut removed = vec![false; n];
        struct Entry {
            level: u8,
            ov: Option<BidiClass>,
            iso: bool,
        }
        let mut stack = vec![Entry {
            level: para_level,
            ov: None,
            iso: false,
        }];
        let (mut oic, mut oec, mut vic) = (0u32, 0u32, 0u32);
        for i in 0..n {
            let c = raw[i];
            match c {
                RLE | LRE | RLO | LRO => {
                    levels[i] = stack.last().unwrap().level;
                    removed[i] = true;
                    let new = if matches!(c, RLE | RLO) {
                        next_odd(stack.last().unwrap().level)
                    } else {
                        next_even(stack.last().unwrap().level)
                    };
                    if new <= MAX_DEPTH && oic == 0 && oec == 0 {
                        let ov = match c {
                            RLO => Some(R),
                            LRO => Some(L),
                            _ => None,
                        };
                        stack.push(Entry {
                            level: new,
                            ov,
                            iso: false,
                        });
                    } else if oic == 0 {
                        oec += 1;
                    }
                }
                RLI | LRI | FSI => {
                    levels[i] = stack.last().unwrap().level;
                    let rtl = match c {
                        RLI => true,
                        LRI => false,
                        _ => first_strong(&raw, i + 1, match_pdi[i]) == 1,
                    };
                    let new = if rtl {
                        next_odd(stack.last().unwrap().level)
                    } else {
                        next_even(stack.last().unwrap().level)
                    };
                    if new <= MAX_DEPTH && oic == 0 && oec == 0 {
                        vic += 1;
                        stack.push(Entry {
                            level: new,
                            ov: None,
                            iso: true,
                        });
                    } else {
                        oic += 1;
                    }
                }
                PDI => {
                    if oic > 0 {
                        oic -= 1;
                    } else if vic != 0 {
                        oec = 0;
                        while !stack.last().unwrap().iso {
                            stack.pop();
                        }
                        stack.pop();
                        vic -= 1;
                    }
                    levels[i] = stack.last().unwrap().level;
                }
                PDF => {
                    if oic > 0 {
                    } else if oec > 0 {
                        oec -= 1;
                    } else if !stack.last().unwrap().iso && stack.len() >= 2 {
                        stack.pop();
                    }
                    levels[i] = stack.last().unwrap().level;
                    removed[i] = true;
                }
                B => {
                    levels[i] = para_level;
                }
                BN => {
                    levels[i] = stack.last().unwrap().level;
                    removed[i] = true;
                }
                _ => {
                    let top = stack.last().unwrap();
                    levels[i] = top.level;
                    if let Some(ov) = top.ov {
                        classes[i] = ov;
                    }
                }
            }
        }

        // ---- X10: build isolating run sequences over the non-removed chars. ----
        let reduced: Vec<usize> = (0..n).filter(|&i| !removed[i]).collect();
        let mut runs: Vec<Vec<usize>> = Vec::new();
        for &i in &reduced {
            match runs.last() {
                Some(r) if levels[*r.last().unwrap()] == levels[i] => {
                    runs.last_mut().unwrap().push(i);
                }
                _ => runs.push(vec![i]),
            }
        }
        // Map a run's first index -> run position.
        let mut run_of_start = vec![usize::MAX; n];
        for (ri, r) in runs.iter().enumerate() {
            run_of_start[r[0]] = ri;
        }
        let orig = classes.clone(); // post-X classes, before W mutations
        let mut used = vec![false; runs.len()];
        let mut sequences: Vec<Vec<usize>> = Vec::new();
        for r in 0..runs.len() {
            if used[r] {
                continue;
            }
            // A continuation run (starts with a matched PDI) is appended, not started.
            let first = runs[r][0];
            if classes[first] == PDI && match_pdi.contains(&first) {
                continue;
            }
            let mut seq = Vec::new();
            let mut cur = r;
            loop {
                used[cur] = true;
                seq.extend_from_slice(&runs[cur]);
                let last = *runs[cur].last().unwrap();
                if is_isolate_init(classes[last]) && match_pdi[last] < n {
                    let nr = run_of_start[match_pdi[last]];
                    if nr != usize::MAX {
                        cur = nr;
                        continue;
                    }
                }
                break;
            }
            sequences.push(seq);
        }

        // ---- W, N, I rules per isolating run sequence. ----
        // The I rules overwrite `levels` with resolved levels, so sos/eos must
        // read the embedding levels from this snapshot, not the live array.
        let elevels = levels.clone();
        for seq in &sequences {
            resolve_sequence(
                seq,
                &chars,
                &orig,
                &mut classes,
                &mut levels,
                &elevels,
                para_level,
                n,
                &removed,
            );
        }

        // ---- L1: reset separators and trailing whitespace to the paragraph level. ----
        let mut reset_from = n;
        for i in 0..n {
            if removed[i] {
                continue;
            }
            match raw[i] {
                S | B => {
                    levels[i] = para_level;
                    for j in reset_from..i {
                        if !removed[j] {
                            levels[j] = para_level;
                        }
                    }
                    reset_from = n;
                }
                WS | FSI | LRI | RLI | PDI => {
                    if reset_from == n {
                        reset_from = i;
                    }
                }
                _ => reset_from = n,
            }
        }
        for j in reset_from..n {
            if !removed[j] {
                levels[j] = para_level;
            }
        }

        // ---- L2: reverse contiguous runs to produce visual order. ----
        let visible: Vec<usize> = (0..n).filter(|&i| !removed[i]).collect();
        let lv: Vec<u8> = visible.iter().map(|&i| levels[i]).collect();
        let mut order: Vec<usize> = (0..visible.len()).collect();
        let max_level = lv.iter().copied().max().unwrap_or(0);
        let min_odd = lv
            .iter()
            .copied()
            .filter(|l| l % 2 == 1)
            .min()
            .unwrap_or(max_level + 1);
        let mut level = max_level;
        while level >= min_odd {
            let mut i = 0;
            while i < order.len() {
                if lv[order[i]] >= level {
                    let start = i;
                    while i < order.len() && lv[order[i]] >= level {
                        i += 1;
                    }
                    order[start..i].reverse();
                } else {
                    i += 1;
                }
            }
            if level == 0 {
                break;
            }
            level -= 1;
        }
        let visual_order: Vec<usize> = order.iter().map(|&p| visible[p]).collect();

        let out_levels: Vec<Option<u8>> = (0..n)
            .map(|i| if removed[i] { None } else { Some(levels[i]) })
            .collect();
        BidiInfo {
            paragraph_level: para_level,
            levels: out_levels,
            visual_order,
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn resolve_sequence(
        seq: &[usize],
        chars: &[char],
        orig: &[BidiClass],
        classes: &mut [BidiClass],
        levels: &mut [u8],
        elevels: &[u8],
        para_level: u8,
        n: usize,
        removed: &[bool],
    ) {
        let len = seq.len();
        if len == 0 {
            return;
        }
        let seq_level = elevels[seq[0]];
        let e = if seq_level % 2 == 0 { L } else { R };

        // sos / eos (X10): compare the sequence level with the adjacent embedding
        // levels (from the snapshot, since the live levels are being resolved).
        let first = seq[0];
        let prev_level = (0..first)
            .rev()
            .find(|&j| !removed[j])
            .map_or(para_level, |j| elevels[j]);
        let sos = if seq_level.max(prev_level) % 2 == 1 {
            R
        } else {
            L
        };
        let last = seq[len - 1];
        let next_level = if is_isolate_init(classes[last]) {
            para_level // an isolate initiator with no matching PDI
        } else {
            (last + 1..n)
                .find(|&j| !removed[j])
                .map_or(para_level, |j| elevels[j])
        };
        let eos = if seq_level.max(next_level) % 2 == 1 {
            R
        } else {
            L
        };

        // Working classes for this sequence.
        let mut cls: Vec<BidiClass> = seq.iter().map(|&i| classes[i]).collect();

        // W1: NSM -> type of previous char (sos at start; ON after isolates).
        let mut prev = sos;
        for c in cls.iter_mut() {
            if *c == NSM {
                *c = if matches!(prev, LRI | RLI | FSI | PDI) {
                    ON
                } else {
                    prev
                };
            }
            prev = *c;
        }
        // W2: EN -> AN if the last strong type is AL.
        let mut strong = sos;
        for c in cls.iter_mut() {
            match *c {
                R | L | AL => strong = *c,
                EN if strong == AL => *c = AN,
                _ => {}
            }
        }
        // W3: AL -> R.
        for c in cls.iter_mut() {
            if *c == AL {
                *c = R;
            }
        }
        // W4: a single ES between EN, or CS between EN/AN, joins the numbers.
        for k in 1..len.saturating_sub(1) {
            if cls[k] == ES && cls[k - 1] == EN && cls[k + 1] == EN {
                cls[k] = EN;
            } else if cls[k] == CS {
                if cls[k - 1] == EN && cls[k + 1] == EN {
                    cls[k] = EN;
                } else if cls[k - 1] == AN && cls[k + 1] == AN {
                    cls[k] = AN;
                }
            }
        }
        // W5: a run of ET adjacent to EN becomes EN.
        let mut k = 0;
        while k < len {
            if cls[k] == ET {
                let start = k;
                while k < len && cls[k] == ET {
                    k += 1;
                }
                let before = start > 0 && cls[start - 1] == EN;
                let after = k < len && cls[k] == EN;
                if before || after {
                    for c in cls.iter_mut().take(k).skip(start) {
                        *c = EN;
                    }
                }
            } else {
                k += 1;
            }
        }
        // W6: remaining separators / terminators become ON.
        for c in cls.iter_mut() {
            if matches!(*c, ES | ET | CS) {
                *c = ON;
            }
        }
        // W7: EN -> L if the last strong type is L.
        let mut strong = sos;
        for c in cls.iter_mut() {
            match *c {
                R | L => strong = *c,
                EN if strong == L => *c = L,
                _ => {}
            }
        }

        // N0: paired brackets (BD16).
        resolve_brackets(seq, chars, orig, &mut cls, e, sos);

        // N1 / N2: resolve runs of neutrals.
        let mut k = 0;
        while k < len {
            if is_ni(cls[k]) {
                let start = k;
                while k < len && is_ni(cls[k]) {
                    k += 1;
                }
                let before = if start == 0 {
                    sos
                } else {
                    neutral_dir(cls[start - 1]).unwrap_or(sos)
                };
                let after = if k == len {
                    eos
                } else {
                    neutral_dir(cls[k]).unwrap_or(eos)
                };
                let set = if before == after { before } else { e };
                for c in cls.iter_mut().take(k).skip(start) {
                    *c = set;
                }
            } else {
                k += 1;
            }
        }

        // I1 / I2: implicit levels, then write back.
        for (k, &i) in seq.iter().enumerate() {
            let add = if seq_level % 2 == 0 {
                match cls[k] {
                    R => 1,
                    AN | EN => 2,
                    _ => 0,
                }
            } else {
                match cls[k] {
                    L | EN | AN => 1,
                    _ => 0,
                }
            };
            levels[i] = seq_level + add;
            classes[i] = cls[k];
        }
    }

    /// N0: resolve the direction of paired brackets within a sequence.
    fn resolve_brackets(
        seq: &[usize],
        chars: &[char],
        orig: &[BidiClass],
        cls: &mut [BidiClass],
        e: BidiClass,
        sos: BidiClass,
    ) {
        let len = seq.len();
        let mut stack: Vec<(u32, usize)> = Vec::new();
        let mut pairs: Vec<(usize, usize)> = Vec::new();
        for k in 0..len {
            if cls[k] != ON {
                continue;
            }
            let (paired, ty) = bidi_bracket(chars[seq[k]] as u32);
            if ty == 1 {
                if stack.len() == 63 {
                    break;
                }
                stack.push((canon_bracket(paired), k));
            } else if ty == 2 {
                let cc = canon_bracket(chars[seq[k]] as u32);
                if let Some(si) = (0..stack.len()).rev().find(|&si| stack[si].0 == cc) {
                    pairs.push((stack[si].1, k));
                    stack.truncate(si);
                }
            }
        }
        pairs.sort_unstable_by_key(|p| p.0);

        let o = if e == L { R } else { L };
        for (open_k, close_k) in pairs {
            let mut found_e = false;
            let mut found_o = false;
            for c in cls.iter().take(close_k).skip(open_k + 1) {
                if let Some(d) = neutral_dir(*c) {
                    if d == e {
                        found_e = true;
                        break;
                    }
                    found_o = true;
                }
            }
            let set = if found_e {
                Some(e)
            } else if found_o {
                let before = (0..open_k)
                    .rev()
                    .find_map(|m| neutral_dir(cls[m]))
                    .unwrap_or(sos);
                Some(if before == o { o } else { e })
            } else {
                None
            };
            if let Some(dir) = set {
                cls[open_k] = dir;
                cls[close_k] = dir;
                // Trailing characters originally NSM take the bracket's direction.
                for &bk in &[open_k, close_k] {
                    let mut m = bk + 1;
                    while m < len && orig[seq[m]] == NSM {
                        cls[m] = dir;
                        m += 1;
                    }
                }
            }
        }
    }
}