i18n-next 0.2.0

i18n for Rust and Tauri on the same locale files as nuxt-i18n-micro: pages, plurals, Intl-compatible formats, no runtime dependencies
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! Plurals (DESIGN 5, 1.1): CLDR cardinal rules generated into [`rules`], a tag resolved the
//! way `Intl.PluralRules.supportedLocalesOf` resolves it, and the form selection of
//! `@i18n-micro/core` `pluralForm` (`core/src/text.ts`).
//!
//! - A tag is the locale's `iso`, else its code. A tag `Intl` would reject (`en_US`, a RangeError)
//!   or not support (`ua`, `xx`) has no rules: such a locale takes the index rule, never a host
//!   default. A supported tag resolves like `BestAvailableLocale`: aliases first (`iw` → `he`,
//!   `620` → `PT`), then region and script are dropped until a known locale is left
//!   (`de-AT` → `de`, `pt-Latn-PT` → `pt`, `pt-PT` stays).
//! - Per rule set, the facts `localePlural` keeps per locale (categories of the counts 0..199,
//!   which of them several counts share) are computed on first use from the rule itself and
//!   cached: 200 rule calls once, 0.46–1.1 µs (bench `plural/compute/*`), against 8 KB of
//!   tables (200 bytes × 40 rule sets) in every binary. A count under 200 then reads the
//!   table (0.6 ns), a larger one calls the rule (1.5–7.7 ns).
//!
//! Self-contained (no `crate::` paths): tests and benches include it with `#[path]`.

// ponytail: every rule set and locale is compiled in (~40 functions, ~16 KB); `I18N_NEXT_LOCALES`
// selects the CLDR number data only (`build.rs`, M5). The micro build has plurals without
// `format`, so the rules stay whole; filter them too if a micro app ever needs the bytes.
#[rustfmt::skip]
#[allow(
    clippy::nonminimal_bool,
    clippy::manual_range_patterns,
    reason = "generated: negations stay `!(..)` (spike S8), lists stay as CLDR writes them"
)]
mod rules;

use std::sync::OnceLock;

/// A CLDR plural category, in template order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub(crate) enum Category {
    Zero,
    One,
    Two,
    Few,
    Many,
    Other,
}

const CATEGORIES: [Category; 6] = [
    Category::Zero,
    Category::One,
    Category::Two,
    Category::Few,
    Category::Many,
    Category::Other,
];

/// The operands of UTS 35 plural rules: `n` absolute value, `i` integer digits, `v` / `w`
/// visible fraction digits with / without trailing zeros, `f` / `t` those digits as a
/// number, `e` / `c` the compact exponent (always 0 here: no compact notation).
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Operands {
    pub(crate) n: f64,
    pub(crate) i: u64,
    pub(crate) v: u64,
    pub(crate) w: u64,
    pub(crate) f: u64,
    pub(crate) t: u64,
    pub(crate) e: u64,
    pub(crate) c: u64,
}

/// ICU keeps the integer digits of a plural operand modulo 10^18 (`DecimalQuantity::toLong`
/// truncates), so `1e18` has `i = 0` and is `one` in French, as in every browser.
const I_MOD: u64 = 1_000_000_000_000_000_000;

impl Operands {
    /// A whole number up to 2^53 (a larger count is rounded to an `f64` first, as JS holds
    /// it, and read by [`Operands::whole_f64`]): `n` is exact, `i` is `n`.
    pub(crate) fn int(n: u64) -> Operands {
        Operands {
            n: n as f64,
            i: n,
            v: 0,
            w: 0,
            f: 0,
            t: 0,
            e: 0,
            c: 0,
        }
    }

    /// A whole `f64` past 2^53, as `Intl.PluralRules` (ICU) reads it: `n` is the double
    /// itself; `i` is the last 18 of its shortest decimal digits, turned into a double
    /// (`getPluralOperand` returns one) and taken modulo as that double. `2^61` is
    /// `2305843009213694000`: `i` = 305843009213694000 → 305843009213694016, so `i % 10`
    /// is 6, not the 2 of the exact value nor the 0 of the digits.
    pub(crate) fn whole_f64(x: f64) -> Operands {
        let x = x.abs();
        let last18 = match shortest_whole(x) {
            Some((digits, zeros)) => match 10u64.checked_pow(zeros) {
                Some(p) if zeros < 18 => (digits % (I_MOD / p)) * p,
                _ => 0,
            },
            // From 2^126 up: at most 17 significant digits of 38 or more, so the last 18
            // are zeros.
            None => 0,
        };
        Operands {
            n: x,
            i: last18 as f64 as u64,
            ..Operands::int(0)
        }
    }

    /// A decimal as written (`"1.50"`): `v`, `w`, `f`, `t` from its fraction digits.
    #[cfg(test)]
    #[allow(
        dead_code,
        reason = "used by tests that include this module with #[path]"
    )]
    pub(crate) fn parse(s: &str) -> Operands {
        let (int, frac) = s.split_once('.').unwrap_or((s, ""));
        let digits = |d: &str| d.bytes().fold(0u64, |a, b| a * 10 + u64::from(b - b'0'));
        let trimmed = frac.trim_end_matches('0');
        Operands {
            n: s.parse().unwrap_or(0.0),
            i: digits(int) % I_MOD,
            v: frac.len() as u64,
            w: trimmed.len() as u64,
            f: digits(frac),
            t: digits(trimmed),
            e: 0,
            c: 0,
        }
    }
}

/// The shortest round-trip digits of a whole `f64` `x` (2^53 ≤ x < 2^126), as JS
/// `Number::toString` and Rust `{}` choose them: `(digits, zeros)` for `digits · 10^zeros`,
/// with no trailing zero in `digits`. Below 2^53, `x` itself; `None` from 2^126 up.
///
/// Integer arithmetic, no `core::fmt`: `x` is `M · 2^e` (`e ≥ 1`), every value in its
/// rounding interval rounds to it (ties to even `M`), so the answer is the multiple of the
/// largest power of 10 inside the interval, the closer one to `x` when there are two
/// (ECMA-262: then the even one). Checked against `{}` (`tests/plural_props.rs`).
pub(crate) fn shortest_whole(x: f64) -> Option<(u64, u32)> {
    const SAFE: f64 = 9_007_199_254_740_992.0;
    const MAX: f64 = 85_070_591_730_234_615_865_843_651_857_942_052_864.0; // 2^126
    if x.is_nan() || x >= MAX || x.fract() != 0.0 || x < 0.0 {
        return None;
    }
    if x < SAFE {
        return Some((x as u64, 0));
    }
    let bits = x.to_bits();
    let fraction = bits & ((1 << 52) - 1);
    let e = ((bits >> 52) & 0x7ff) as u32 - 1075;
    let v = x as u128;
    let up = 1u128 << e;
    // At a power of two the double below is closer: half the gap.
    let down = if fraction == 0 { up / 2 } else { up };
    let even = fraction & 1 == 0;
    // Doubled bounds: `y` rounds to `x` when `2v - down ≤ 2y ≤ 2v + up` (strict unless even).
    let (lo, hi) = (2 * v - down, 2 * v + up);
    let inside = |y: u128| {
        let d = 2 * y;
        if even {
            lo <= d && d <= hi
        } else {
            lo < d && d < hi
        }
    };
    let (mut best, mut zeros, mut p) = (v, 0u32, 1u128);
    loop {
        let next = p * 10;
        let below = v / next * next;
        let above = below + next;
        let pick = match (inside(below), inside(above)) {
            (false, false) => break,
            (true, false) => below,
            (false, true) => above,
            (true, true) => match (v - below).cmp(&(above - v)) {
                std::cmp::Ordering::Less => below,
                std::cmp::Ordering::Greater => above,
                std::cmp::Ordering::Equal if (below / next).is_multiple_of(2) => below,
                std::cmp::Ordering::Equal => above,
            },
        };
        best = pick;
        zeros += 1;
        p = next;
    }
    u64::try_from(best / p).ok().map(|d| (d, zeros))
}

/// A count `pluralForm` reads by category: whole and not negative. Past 2^53 a count is an
/// `f64` and keeps its own spelling (see [`Operands::whole_f64`]).
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum Whole {
    /// 0..=2^53.
    Int(u64),
    Big(f64),
}

/// A category list in template order.
#[derive(Debug, Clone, Copy)]
struct Cats {
    list: [Category; 6],
    len: usize,
}

impl Cats {
    fn from_mask(mask: u8) -> Cats {
        let mut cats = Cats {
            list: [Category::Other; 6],
            len: 0,
        };
        for c in CATEGORIES {
            if mask & (1 << c as u8) != 0
                && let Some(slot) = cats.list.get_mut(cats.len)
            {
                *slot = c;
                cats.len += 1;
            }
        }
        cats
    }

    fn position(&self, c: Category) -> Option<usize> {
        self.list.iter().take(self.len).position(|&x| x == c)
    }
}

/// `CATEGORY_PROBE` of `text.ts`: counts below it are read from a table.
const PROBE: usize = 200;

/// What `localePlural` of `text.ts` keeps per locale, here per rule set.
#[derive(Debug)]
pub(crate) struct Plural {
    select: fn(&Operands) -> Category,
    /// Every category of the rule set (`pluralCategories`).
    all: Cats,
    /// Categories counts under [`PROBE`] reach, without `zero`.
    named: Cats,
    /// Bit `k`: several counts under [`PROBE`] share `named[k]`, so its form must show `{count}`.
    shared: u8,
    small: [Category; PROBE],
}

impl Plural {
    fn new(rule: &rules::Rule) -> Plural {
        let mut small = [Category::Other; PROBE];
        let mut seen = [0usize; 6];
        for (n, slot) in small.iter_mut().enumerate() {
            *slot = (rule.select)(&Operands::int(n as u64));
            if let Some(k) = seen.get_mut(*slot as usize) {
                *k += 1;
            }
        }
        let reached = seen
            .iter()
            .enumerate()
            .filter(|&(c, &k)| k > 0 && c != Category::Zero as usize)
            .fold(0u8, |m, (c, _)| m | 1 << c);
        let named = Cats::from_mask(reached);
        let shared = (0..named.len)
            .filter(|&k| {
                let c = named.list.get(k).map_or(0, |&c| c as usize);
                seen.get(c).is_some_and(|&n| n > 1)
            })
            .fold(0u8, |m, k| m | 1 << k);
        Plural {
            select: rule.select,
            all: Cats::from_mask(rule.categories),
            named,
            shared,
            small,
        }
    }

    /// Names of the categories small counts reach, without `zero` (`check()` messages).
    pub(crate) fn named_names(&self) -> Vec<&'static str> {
        self.named
            .list
            .iter()
            .take(self.named.len)
            .map(|c| match c {
                Category::Zero => "zero",
                Category::One => "one",
                Category::Two => "two",
                Category::Few => "few",
                Category::Many => "many",
                Category::Other => "other",
            })
            .collect()
    }

    /// The category of a whole count.
    pub(crate) fn category(&self, count: Whole) -> Category {
        match count {
            Whole::Int(n) => match self.small.get(n as usize) {
                Some(&c) => c,
                None => (self.select)(&Operands::int(n)),
            },
            Whole::Big(x) => (self.select)(&Operands::whole_f64(x)),
        }
    }
}

static DATA: [OnceLock<Plural>; rules::RULES.len()] =
    [const { OnceLock::new() }; rules::RULES.len()];

/// The rules of a tag, `None` for one `Intl.PluralRules` rejects or does not support.
pub(crate) fn for_tag(tag: &str) -> Option<&'static Plural> {
    let i = rule_index(tag)?;
    let slot = DATA.get(i)?;
    if let Some(p) = slot.get() {
        return Some(p);
    }
    let p = compute(i)?;
    Some(slot.get_or_init(|| p))
}

/// The data of rule set `i`, computed afresh (what [`for_tag`] caches; benches time it).
pub(crate) fn compute(i: usize) -> Option<Plural> {
    rules::RULES.get(i).map(Plural::new)
}

/// The select function and category mask of rule set `i` (tests).
#[cfg(test)]
#[allow(
    dead_code,
    reason = "used by tests that include this module with #[path]"
)]
pub(crate) fn rule(i: usize) -> (fn(&Operands) -> Category, u8) {
    (rules::RULES[i].select, rules::RULES[i].categories)
}

/// The rule set of a locale `Intl` knows by this exact name (`pt-PT`, `de`).
pub(crate) fn locale_rule(name: &str) -> Option<usize> {
    let at = find(rules::LOCALES, &rules::LOCALES_ENDS, name)?;
    rules::LOCALE_RULES.get(at).map(|&r| usize::from(r))
}

/// Entry `i` of a concatenated string table.
fn nth(table: &'static str, ends: &[u16], i: usize) -> &'static str {
    let start = i
        .checked_sub(1)
        .and_then(|j| ends.get(j))
        .map_or(0, |&e| usize::from(e));
    let end = ends.get(i).map_or(start, |&e| usize::from(e));
    table.get(start..end).unwrap_or("")
}

/// Binary search of `key` in a sorted concatenated string table.
fn find(table: &'static str, ends: &[u16], key: &str) -> Option<usize> {
    let (mut lo, mut hi) = (0, ends.len());
    while lo < hi {
        let mid = lo + (hi - lo) / 2;
        match nth(table, ends, mid).as_bytes().cmp(key.as_bytes()) {
            std::cmp::Ordering::Less => lo = mid + 1,
            std::cmp::Ordering::Greater => hi = mid,
            std::cmp::Ordering::Equal => return Some(mid),
        }
    }
    None
}

fn alpha(s: &str) -> bool {
    s.bytes().all(|b| b.is_ascii_alphabetic())
}

fn digit(s: &str) -> bool {
    s.bytes().all(|b| b.is_ascii_digit())
}

fn alnum(s: &str) -> bool {
    s.bytes().all(|b| b.is_ascii_alphanumeric())
}

/// A short ASCII string on the stack.
#[derive(Clone, Copy)]
pub(crate) struct Buf<const N: usize> {
    bytes: [u8; N],
    len: usize,
}

impl<const N: usize> Buf<N> {
    pub(crate) const fn new() -> Self {
        Buf {
            bytes: [0; N],
            len: 0,
        }
    }

    pub(crate) fn push(&mut self, s: &str, case: impl Fn(u8) -> u8) {
        for b in s.bytes() {
            if let Some(slot) = self.bytes.get_mut(self.len) {
                *slot = case(b);
                self.len += 1;
            }
        }
    }

    pub(crate) fn of(s: &str, case: impl Fn(u8) -> u8) -> Self {
        let mut buf = Self::new();
        buf.push(s, case);
        buf
    }

    /// Bytes held.
    #[cfg(feature = "format")]
    pub(crate) fn len(&self) -> usize {
        self.len
    }

    /// Keeps the first `len` bytes.
    #[cfg(feature = "format")]
    pub(crate) fn truncate(&mut self, len: usize) {
        self.len = self.len.min(len);
    }

    /// `latn` → `Latn` (a script subtag).
    #[cfg(feature = "format")]
    pub(crate) fn title(s: &str) -> Self {
        let mut b = Self::of(s, lower);
        if let Some(first) = b.bytes.first_mut() {
            *first = first.to_ascii_uppercase();
        }
        b
    }

    pub(crate) fn as_str(&self) -> &str {
        std::str::from_utf8(self.bytes.get(..self.len).unwrap_or(&[])).unwrap_or("")
    }
}

fn lower(b: u8) -> u8 {
    b.to_ascii_lowercase()
}

fn upper(b: u8) -> u8 {
    b.to_ascii_uppercase()
}

/// The parts of a well-formed tag that pick a rule.
struct Tag<'a> {
    language: Buf<8>,
    script: Option<&'a str>,
    region: Option<&'a str>,
    /// The replacement of the first variant with a `language-variant` alias
    /// (`aa-1996-saaho` → `ssy`), looked up as each variant is read.
    variant_alias: Option<&'static str>,
    /// The variants as written (`1996-saaho`), `-`-separated.
    #[cfg_attr(not(feature = "format"), allow(dead_code))]
    variants: Option<&'a str>,
}

/// A variant subtag: 5..8 alphanumerics, or 4 starting with a digit.
fn variant(s: &str) -> bool {
    alnum(s)
        && (matches!(s.len(), 5..=8)
            || (s.len() == 4 && s.as_bytes().first().is_some_and(u8::is_ascii_digit)))
}

/// Whether `v` repeats (ASCII case aside) one of the `-`-separated subtags of `before`.
fn repeats(before: &str, v: &str) -> bool {
    before.split('-').any(|p| p.eq_ignore_ascii_case(v))
}

/// Byte offset of `part` (a slice of `tag`) in `tag`.
fn offset(tag: &str, part: &str) -> usize {
    part.as_ptr() as usize - tag.as_ptr() as usize
}

/// `unicode_locale_id` of UTS 35 as ECMA-402 `IsStructurallyValidLanguageTag` checks it:
/// `None` where `Intl` throws a RangeError (`en_US`, `root`, `i-klingon`, a repeated variant
/// or singleton). Extensions are checked by shape only (singleton, then subtags of 2..8),
/// without a buffer: a tag of any length is read in place, with no allocation.
///
/// Known difference: ICU stops checking a `-u-` extension after a repeated key
/// (`ru-u-rg-rg-a1` is accepted as `ru-u-rg`); this follows the grammar and rejects it
/// (`fixtures/compat/README.md`).
fn parse(tag: &str) -> Option<Tag<'_>> {
    let mut parts = tag.split('-').peekable();
    let language = parts.next()?;
    if !(alpha(language) && matches!(language.len(), 2 | 3 | 5..=8)) {
        return None;
    }
    let mut out = Tag {
        language: Buf::of(language, lower),
        script: None,
        region: None,
        variant_alias: None,
        variants: None,
    };
    // Where the variants start in `tag`: a new one is checked against those before it.
    let mut variants_at = None;
    let mut stage = 0;
    while let Some(&s) = parts.peek() {
        if s.len() == 1 {
            break;
        }
        parts.next();
        if stage == 0 && s.len() == 4 && alpha(s) {
            out.script = Some(s);
            stage = 1;
        } else if stage <= 1 && ((s.len() == 2 && alpha(s)) || (s.len() == 3 && digit(s))) {
            out.region = Some(s);
            stage = 2;
        } else if variant(s) {
            let at = offset(tag, s);
            let start = *variants_at.get_or_insert(at);
            if repeats(tag.get(start..at).unwrap_or(""), s) {
                return None;
            }
            out.variants = tag.get(start..at + s.len());
            if out.variant_alias.is_none() {
                let mut key = Buf::<17>::of(out.language.as_str(), lower);
                key.push("-", lower);
                key.push(s, lower);
                out.variant_alias = language_alias(key.as_str(), tag);
            }
            stage = 2;
        } else {
            return None;
        }
    }
    // Extensions and private use; a singleton is a digit (0..9) or a letter (10..35).
    let mut singletons = 0u64;
    while let Some(s) = parts.next() {
        let c = *s.as_bytes().first()?;
        if s.len() != 1 || !c.is_ascii_alphanumeric() {
            return None;
        }
        let c = c.to_ascii_lowercase();
        if c == b'x' {
            let mut any = false;
            for p in parts.by_ref() {
                if p.is_empty() || p.len() > 8 || !alnum(p) {
                    return None;
                }
                any = true;
            }
            return any.then_some(out);
        }
        let slot = if c.is_ascii_digit() {
            c - b'0'
        } else {
            c - b'a' + 10
        };
        let bit = 1u64 << slot;
        if singletons & bit != 0 {
            return None;
        }
        singletons |= bit;
        // The subtags up to the next singleton, as one slice of `tag`.
        let (mut start, mut end) = (None, 0);
        while let Some(&p) = parts.peek() {
            if p.len() == 1 {
                break;
            }
            if !(matches!(p.len(), 2..=8) && alnum(p)) {
                return None;
            }
            let at = offset(tag, p);
            start.get_or_insert(at);
            end = at + p.len();
            parts.next();
        }
        let ext = start.and_then(|s| tag.get(s..end)).unwrap_or("");
        let valid = match c {
            b'u' => unicode_extension(ext),
            b't' => transformed_extension(ext),
            _ => !ext.is_empty(),
        };
        if !valid {
            return None;
        }
    }
    Some(out)
}

/// `unicode_locale_extensions` after `-u` (`ext`: its subtags joined by `-`): attributes
/// (3..8), then keywords: a key (alphanumeric + letter) and its type subtags (3..8).
/// [`parse`] has checked every subtag is 2..8 alphanumerics, so only a key's second
/// character is left to check.
fn unicode_extension(ext: &str) -> bool {
    !ext.is_empty()
        && ext
            .split('-')
            .all(|p| p.len() != 2 || p.as_bytes().get(1).is_some_and(u8::is_ascii_alphabetic))
}

/// `transformed_extensions` after `-t` (`ext`: its subtags joined by `-`): an optional
/// language id (its variants unique, as in the tag itself), then fields: a key (letter +
/// digit) and one or more values (3..8).
fn transformed_extension(ext: &str) -> bool {
    if ext.is_empty() {
        return false;
    }
    let mut it = ext.split('-').peekable();
    let mut lang = false;
    if it
        .next_if(|l| alpha(l) && matches!(l.len(), 2 | 3 | 5..=8))
        .is_some()
    {
        lang = true;
        let _ = it.next_if(|s| s.len() == 4 && alpha(s));
        let _ = it.next_if(|s| (s.len() == 2 && alpha(s)) || (s.len() == 3 && digit(s)));
        let mut variants_at = None;
        while let Some(v) = it.next_if(|s| variant(s)) {
            let at = offset(ext, v);
            let start = *variants_at.get_or_insert(at);
            if repeats(ext.get(start..at).unwrap_or(""), v) {
                return false;
            }
        }
    }
    let mut fields = false;
    while let Some(key) = it.next() {
        let b = key.as_bytes();
        if !(b.len() == 2 && b[0].is_ascii_alphabetic() && b[1].is_ascii_digit()) {
            return false;
        }
        let value = |s: &&str| (3..=8).contains(&s.len());
        if it.next_if(value).is_none() {
            return false;
        }
        while it.next_if(value).is_some() {}
        fields = true;
    }
    lang || fields
}

/// Looks `key` up in the sorted region alias table.
fn region_alias(key: &str) -> Option<&'static str> {
    rules::REGION_ALIASES
        .iter()
        .find(|(k, _)| *k == key)
        .map(|(_, to)| *to)
}

/// The replacement of a language alias key, unless `tag` is that key itself, bare and
/// lowercase, and V8 passes such a tag through (`bh`).
fn language_alias(key: &str, tag: &str) -> Option<&'static str> {
    let at = find(rules::ALIAS_KEYS, &rules::ALIAS_KEYS_ENDS, key)?;
    let bare = rules::ALIAS_BARE.get(at).copied().unwrap_or(true);
    (bare || key != tag).then(|| nth(rules::ALIAS_VALUES, &rules::ALIAS_VALUES_ENDS, at))
}

/// `f` of the language subtag `Intl` canonicalizes `tag` to (`iw` → `he`), lowercase; `None`
/// when `Intl` rejects the tag (the case modifiers of linked messages, `linked/case.rs`).
pub(crate) fn canonical_language<R>(tag: &str, f: impl FnOnce(&str) -> R) -> Option<R> {
    let t = parse(tag)?;
    let replacement = t
        .variant_alias
        .or_else(|| language_alias(t.language.as_str(), tag));
    Some(match replacement {
        Some(r) => f(Buf::<8>::of(r.split('-').next().unwrap_or(""), lower).as_str()),
        None => f(t.language.as_str()),
    })
}

/// The parts of a tag the number formats read (`format/locale.rs` applies its own aliases).
#[cfg(feature = "format")]
pub(crate) struct Subtags<'a> {
    /// lowercase
    pub(crate) language: Buf<8>,
    pub(crate) script: Option<&'a str>,
    pub(crate) region: Option<&'a str>,
    /// `-`-separated, as written
    pub(crate) variants: Option<&'a str>,
}

/// The language, script, region and variants of a tag, `None` when `Intl` rejects it.
#[cfg(feature = "format")]
pub(crate) fn subtags(tag: &str) -> Option<Subtags<'_>> {
    let t = parse(tag)?;
    Some(Subtags {
        language: t.language,
        script: t.script,
        region: t.region,
        variants: t.variants,
    })
}

/// The category of `o` under rule set `rule` (`plural::RULES` index; none: `other`).
#[cfg(feature = "format")]
pub(crate) fn select_rule(rule: u8, o: &Operands) -> Category {
    match rules::RULES.get(usize::from(rule)) {
        Some(r) => (r.select)(o),
        None => Category::Other,
    }
}

/// The rule set `Intl.PluralRules` uses for `tag`, `None` for the index rule.
pub(crate) fn rule_index(tag: &str) -> Option<usize> {
    let t = parse(tag)?;
    let mut language = t.language;
    let mut script = t.script.map(|s| {
        let mut b = Buf::<4>::of(s, lower);
        if let Some(first) = b.bytes.first_mut() {
            *first = first.to_ascii_uppercase();
        }
        b
    });
    let mut region = t.region.map(|r| Buf::<3>::of(r, upper));
    // A `language-variant` alias first (`art-lojban`), then the language alone (`iw`).
    let replacement = t
        .variant_alias
        .or_else(|| language_alias(language.as_str(), tag));
    if let Some(r) = replacement {
        let mut parts = r.split('-');
        language = Buf::of(parts.next().unwrap_or(""), lower);
        for p in parts {
            if p.len() == 4 && alpha(p) && script.is_none() {
                script = Some(Buf::of(p, |b| b));
            } else if (p.len() == 2 || p.len() == 3) && region.is_none() {
                region = Some(Buf::of(p, upper));
            }
        }
    }
    if let Some(r) = &region
        && let Some(to) = region_alias(r.as_str())
    {
        region = Some(Buf::of(to, upper));
    }
    // BestAvailableLocale: the longest known prefix of language[-script][-region].
    let mut candidate = Buf::<17>::of(language.as_str(), |b| b);
    let base = candidate.len;
    if let Some(s) = &script {
        candidate.push("-", |b| b);
        candidate.push(s.as_str(), |b| b);
    }
    let with_script = candidate.len;
    if let Some(r) = &region {
        candidate.push("-", |b| b);
        candidate.push(r.as_str(), |b| b);
        if let Some(i) = locale_rule(candidate.as_str()) {
            return Some(i);
        }
    }
    candidate.len = with_script;
    if with_script > base
        && let Some(i) = locale_rule(candidate.as_str())
    {
        return Some(i);
    }
    candidate.len = base;
    locale_rule(candidate.as_str())
}

/// What `pluralForm` reads from a template's forms besides their text, found once at load.
#[derive(Debug, Clone)]
pub(crate) struct Shape {
    len: usize,
    /// The first form has a `{`: no categories (`unmarkedIndex`).
    first_brace: bool,
    /// Bit `k`: form `k` has `{count}` (forms past 63 never matter: a locale has ≤ 6).
    counts: u64,
}

impl Shape {
    /// From the forms as split (trimmed or not: neither changes `{` or `{count}`).
    pub(crate) fn new<'a>(forms: impl Iterator<Item = &'a str>) -> Shape {
        let mut shape = Shape {
            len: 0,
            first_brace: false,
            counts: 0,
        };
        for (k, form) in forms.enumerate() {
            if k == 0 {
                shape.first_brace = form.contains('{');
            }
            if k < 64 && form.contains("{count}") {
                shape.counts |= 1 << k;
            }
            shape.len += 1;
        }
        shape
    }

    /// The index of the form `count` picks (`pluralIndex` / `markedIndex` of `text.ts`).
    /// `count` is `None` when negative, fractional or not a number: the last form.
    pub(crate) fn index(
        &self,
        marked: bool,
        count: Option<Whole>,
        plural: Option<&Plural>,
    ) -> usize {
        let last = self.len.saturating_sub(1);
        let Some(count) = count else {
            return last;
        };
        let by_index = || match count {
            Whole::Int(n) => usize::try_from(n).map_or(last, |n| n.min(last)),
            Whole::Big(_) => last,
        };
        let zero = matches!(count, Whole::Int(0));
        if marked {
            if zero {
                return 0;
            }
            let Some(p) = plural else {
                return by_index();
            };
            let cats = if last > p.named.len { p.all } else { p.named };
            return match cats.position(p.category(count)) {
                None => last,
                Some(k) => (k + 1).min(last),
            };
        }
        match plural {
            Some(p) if self.unmarked(p) => {
                if zero {
                    return 0;
                }
                match p.named.position(p.category(count)) {
                    None => last,
                    Some(k) => k + 1,
                }
            }
            _ => by_index(),
        }
    }

    /// What `check()` says of a plural message with this shape in a locale with `plural`
    /// rules (`None`: the index rule, any count of 2 or more forms reads): `None` when `tc`
    /// reads it by category as written; else the form counts it expects and whether only the
    /// shape is off (the count matches, yet `tc` falls back to the index rule).
    ///
    /// Expected: the unmarked shape `zero | <one form per category small counts reach>`
    /// (`ru`: 4, `en`: 3); a `=0 ` marked message also takes one form per category of the
    /// locale (`en`: `=0 none | one | other`).
    pub(crate) fn check(
        &self,
        marked: bool,
        plural: Option<&Plural>,
    ) -> Option<(Vec<usize>, bool)> {
        let p = plural?;
        let named = p.named.len + 1;
        let mut expected = vec![named];
        if marked && p.all.len + 1 != named {
            expected.push(p.all.len + 1);
        }
        if !expected.contains(&self.len) {
            return Some((expected, false));
        }
        let shape_off = !marked && p.named.len >= 3 && !self.unmarked(p);
        shape_off.then_some((expected, true))
    }

    /// `unmarkedIndex` applies: a zero form without `{`, then one form per named category,
    /// each shared one showing `{count}`.
    fn unmarked(&self, p: &Plural) -> bool {
        p.named.len >= 3
            && self.len == p.named.len + 1
            && !self.first_brace
            && (0..p.named.len)
                .all(|k| p.shared & (1 << k) == 0 || self.counts & (1 << (k + 1)) != 0)
    }
}