Skip to main content

atmos/os_lib/ecma/
collate.rs

1//! 文字列の照合(`String.prototype.localeCompare`・`Intl.Collator`)。
2//!
3//! CLDR の root の照合(Chrome の既定)の近似。Unicode の照合の表は持たないので、文字の種類(空白・記号・数字・文字)と
4//! ラテン文字のアクセント・大文字小文字だけを見る。JS の値に依存しない純粋な関数(ホスト試験は `tests/src/test_js_collate.rs`)。
5
6use alloc::vec::Vec;
7use core::cmp::Ordering;
8
9/// U+00C0〜U+017F の基の英字(小文字。`.` は基の英字を持たない文字)。Unicode の分解(NFD)の先頭の文字から作った表。
10const LATIN_BASE: &[u8; 192] = b"aaaaaa.ceeeeiiii.nooooo..uuuuy..aaaaaa.ceeeeiiii.nooooo..uuuuy.yaaaaaaccccccccdd..eeeeeeeeeegggggggghh..iiiiiiiii...jjkk.llllll....nnnnnn...oooooo..rrrrrrsssssssstttt..uuuuuuuuuuuuwwyyyzzzzzz.";
11
12/// 照合の 1 要素(第 1 の重み・第 2〔アクセント〕・第 3〔大文字小文字〕)。
13type CollElem = (u32, u32, u8);
14
15/// 1 文字の照合要素を `out` へ足す(CLDR の root の照合の近似)。
16/// 第 1 の重みの並び: 空白 < 句読点・記号 < 数字 < ラテン文字 < その他の文字(符号位置の順)< U+FFFD(最後)。
17/// 結合文字(U+0300〜U+036F)は第 1 の重みを持たず、前の文字のアクセント(第 2)になる。
18/// `phonebook` が真のときはドイツ語電話帳順(`ä/Ä -> ae`, `ö/Ö -> oe`, `ü/Ü -> ue`)で展開する。
19fn coll_push_opts(c: char, out: &mut Vec<CollElem>, phonebook: bool) {
20    let u = c as u32;
21    let upper = c.is_uppercase() as u8;
22    let latin = |b: u8, acc: u32, out: &mut Vec<CollElem>| out.push((0x4000_0000 + (b - b'a') as u32, acc, upper));
23    if (0x300..=0x36F).contains(&u) {
24        if phonebook && u == 0x0308 {
25            if let Some(last) = out.last() {
26                let is_aou = matches!(last.0, 0x4000_0000 | 0x4000_000E | 0x4000_0014);
27                if is_aou && last.1 == 0 {
28                    let prev_upper = last.2;
29                    out.push((0x4000_0000 + (b'e' - b'a') as u32, 1, prev_upper));
30                    return;
31                }
32            }
33        }
34        if let Some(last) = out.last_mut() {
35            last.1 = last.1.wrapping_mul(31).wrapping_add(u);
36        }
37        return;
38    }
39    if c.is_ascii_alphabetic() {
40        latin(c.to_ascii_lowercase() as u8, 0, out);
41        return;
42    }
43    if (0xC0..0x180).contains(&u) {
44        if phonebook {
45            match u {
46                0xC4 | 0xE4 => {
47                    latin(b'a', 0, out);
48                    latin(b'e', 1, out);
49                    return;
50                }
51                0xD6 | 0xF6 => {
52                    latin(b'o', 0, out);
53                    latin(b'e', 1, out);
54                    return;
55                }
56                0xDC | 0xFC => {
57                    latin(b'u', 0, out);
58                    latin(b'e', 1, out);
59                    return;
60                }
61                _ => {}
62            }
63        }
64        match u {
65            0xC6 | 0xE6 => {
66                latin(b'a', 0, out);
67                latin(b'e', 0, out);
68                return;
69            }
70            0xDF => {
71                latin(b's', 0, out);
72                latin(b's', 0, out);
73                return;
74            }
75            0x152 | 0x153 => {
76                latin(b'o', 0, out);
77                latin(b'e', 0, out);
78                return;
79            }
80            0x132 | 0x133 => {
81                latin(b'i', 0, out);
82                latin(b'j', 0, out);
83                return;
84            }
85            0xD0 | 0xF0 | 0x110 | 0x111 => return latin(b'd', 1, out),
86            0xD8 | 0xF8 => return latin(b'o', 1, out),
87            0x131 => return latin(b'i', 1, out),
88            0x13F..=0x142 => return latin(b'l', 1, out),
89            0x126 | 0x127 => return latin(b'h', 1, out),
90            0x166 | 0x167 => return latin(b't', 1, out),
91            0x149..=0x14B => return latin(b'n', 1, out),
92            0x138 => return latin(b'k', 1, out),
93            0x17F => return latin(b's', 1, out),
94            _ => {}
95        }
96        let b = LATIN_BASE[(u - 0xC0) as usize];
97        if b != b'.' {
98            return latin(b, u, out);
99        }
100    }
101    let w = if u == 0xFFFD {
102        0xFFFF_FFFF
103    } else if c.is_whitespace() {
104        0x1000_0000 + u
105    } else if c.is_ascii_digit() {
106        0x3000_0000 + (u - '0' as u32)
107    } else if c.is_alphanumeric() {
108        0x5000_0000 + u
109    } else if c.is_control() {
110        return;
111    } else {
112        0x2000_0000 + u
113    };
114    out.push((w, 0, upper));
115}
116
117/// `String.prototype.localeCompare`・`Intl.Collator` の比較(CLDR の root の照合の近似)。
118/// 第 1 の重み(文字の種類と基の文字)→ 第 2(アクセント)→ 第 3(小文字が先)の順に比べる。
119/// 【2026-11-17】以前は符号位置の比較で、`"Hello".localeCompare("b")` が -1(大文字が小文字より前)、
120/// `"é" < "f"` が偽だった(Chrome は 1・真)。
121pub fn locale_compare(a: &str, b: &str) -> Ordering {
122    locale_compare_with_options(a, b, false)
123}
124
125/// ロケール依存オプション付きの文字列照合。`phonebook` が真のときは `-u-co-phonebk` 規則を使う。
126pub fn locale_compare_with_options(a: &str, b: &str, phonebook: bool) -> Ordering {
127    if a == b {
128        return Ordering::Equal;
129    }
130    let mut ea: Vec<CollElem> = Vec::with_capacity(a.len());
131    let mut eb: Vec<CollElem> = Vec::with_capacity(b.len());
132    for c in a.chars() {
133        coll_push_opts(c, &mut ea, phonebook);
134    }
135    for c in b.chars() {
136        coll_push_opts(c, &mut eb, phonebook);
137    }
138    let level = |f: fn(&CollElem) -> u32| ea.iter().map(f).cmp(eb.iter().map(f));
139    level(|e| e.0)
140        .then_with(|| level(|e| e.1))
141        .then_with(|| level(|e| e.2 as u32))
142}