Debian Codegraph @

debian/rust-vendor/icu_segmenter/src/complex/script.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use crate::provider::ComplexScript;
6
7 // TODO: Use data provider
8 pub(crate) fn get_complex_script(codepoint: u32) -> ComplexScript {
9 // For Thai, Burmese, Lao and Khmer, these are the intersections
10 // of lb=SA with the respective Script
11 match codepoint {
12 0xE01..=0xE3A | 0xE40..=0x0E4E => ComplexScript::Thai,
13 0x0E81
14 | 0x0E82
15 | 0x0E84
16 | 0x0E86..=0x0E8A
17 | 0x0E8C..0x0EA3
18 | 0x0EA5
19 | 0x0EA7..=0x0EBD
20 | 0x0EC0..=0x0EC4
21 | 0x0EC6
22 | 0x0EC8..=0x0ECE
23 | 0x0EDC..=0x0EDF => ComplexScript::Lao,
24 0x1000..=0x103F
25 | 0x1050..=0x108F
26 | 0x109A..=0x109F
27 | 0xA9E0..=0xA9EF
28 | 0xA9FA..=0xA9FE
29 | 0xAA60..=0xAA7F => ComplexScript::Myanmar,
30 0x1780..=0x17d3 | 0x17d7 | 0x17dc | 0x17dd => ComplexScript::Khmer,
31 0x2E80..=0x2E99
32 | 0x2E9B..=0x2EF3
33 | 0x2F00..=0x2FD5
34 | 0x3005
35 | 0x3007
36 | 0x3021..=0x3029
37 | 0x3038..=0x303B
38 | 0x3041..=0x3096
39 | 0x309D..=0x309F
40 | 0x3400..=0x4DBF
41 | 0x4E00..=0x9FFF
42 | 0xF900..=0xFA6D
43 | 0xFA70..=0xFAD9
44 | 0x00016FE2
45 | 0x00016FE3
46 | 0x00016FF0..=0x00016FF6
47 | 0x0001B001..=0x0001B11F
48 | 0x0001B132
49 | 0x0001B150..=0x0001B152
50 | 0x0001F200
51 | 0x00020000..=0x0002A6DF
52 | 0x0002A700..=0x0002B81D
53 | 0x0002B820..=0x0002CEAD
54 | 0x0002CEB0..=0x0002EBE0
55 | 0x0002EBF0..=0x0002EE5D
56 | 0x0002F800..=0x0002FA1D
57 | 0x00030000..=0x0003134A
58 | 0x00031350..=0x00033479 => ComplexScript::ChineseOrJapanese,
59 _ => ComplexScript::None,
60 }
61 }
62
63 /// This struct is an iterator that returns the string per complex script from the
64 /// given string.
65 pub(super) struct ComplexScriptIterator<'s> {
66 rest: &'s str,
67 }
68
69 impl<'s> ComplexScriptIterator<'s> {
70 pub(super) fn new(input: &'s str) -> Self {
71 Self { rest: input }
72 }
73 }
74
75 impl<'s> Iterator for ComplexScriptIterator<'s> {
76 type Item = (&'s str, ComplexScript);
77
78 fn next(&mut self) -> Option<Self::Item> {
79 let mut indices = self.rest.char_indices();
80 let lang = get_complex_script(indices.next()?.1 as u32);
81 match indices.find(|&(_, ch)| get_complex_script(ch as u32) != lang) {
82 Some((i, _)) => {
83 let (result, rest) = self.rest.split_at(i);
84 self.rest = rest;
85 Some((result, lang))
86 }
87 None => Some((core::mem::take(&mut self.rest), lang)),
88 }
89 }
90 }
91
92 pub(super) struct ComplexScriptIteratorUtf16<'s> {
93 rest: &'s [u16],
94 }
95
96 impl<'s> ComplexScriptIteratorUtf16<'s> {
97 pub(super) fn new(input: &'s [u16]) -> Self {
98 Self { rest: input }
99 }
100 }
101
102 impl<'s> Iterator for ComplexScriptIteratorUtf16<'s> {
103 type Item = (&'s [u16], ComplexScript);
104
105 fn next(&mut self) -> Option<Self::Item> {
106 let lang = get_complex_script(*self.rest.first()? as u32);
107 match self
108 .rest
109 .iter()
110 .position(|&ch| get_complex_script(ch as u32) != lang)
111 {
112 Some(i) => {
113 let (result, rest) = self.rest.split_at(i);
114 self.rest = rest;
115 Some((result, lang))
116 }
117 None => Some((core::mem::take(&mut self.rest), lang)),
118 }
119 }
120 }
121
122 #[cfg(test)]
123 mod tests {
124 use super::*;
125
126 #[test]
127 fn test_thai_only() {
128 let s = "ภาษาไทยภาษาไทย";
129 let utf16: Vec<u16> = s.encode_utf16().collect();
130 let mut iter = ComplexScriptIteratorUtf16::new(&utf16);
131 assert_eq!(
132 iter.next(),
133 Some((utf16.as_slice(), ComplexScript::Thai)),
134 "Thai script only with UTF-16"
135 );
136 let mut iter = ComplexScriptIterator::new(s);
137 assert_eq!(
138 iter.next(),
139 Some((s, ComplexScript::Thai)),
140 "Thai script only with UTF-8"
141 );
142 assert_eq!(iter.next(), None, "Iterator for UTF-8 is finished");
143 }
144
145 #[test]
146 fn test_myanmar_only() {
147 let s = "မြန်မာစာမြန်မာစာမြန်မာစာ";
148 let utf16: Vec<u16> = s.encode_utf16().collect();
149 let mut iter = ComplexScriptIteratorUtf16::new(&utf16);
150 assert_eq!(
151 iter.next(),
152 Some((utf16.as_slice(), ComplexScript::Myanmar)),
153 "Myanmar script only with UTF-16"
154 );
155 let mut iter = ComplexScriptIterator::new(s);
156 assert_eq!(
157 iter.next(),
158 Some((s, ComplexScript::Myanmar)),
159 "Myanmar script only with UTF-8"
160 );
161 assert_eq!(iter.next(), None, "Iterator for UTF-8 is finished");
162 }
163
164 #[test]
165 fn test_combine() {
166 const TEST_STR_THAI: &str = "ภาษาไทยภาษาไทย";
167 const TEST_STR_MYANMAR: &str = "ဗမာနွယ်ဘာသာစကားမျာ";
168 let s = format!("{TEST_STR_THAI}{TEST_STR_MYANMAR}");
169 let utf16: Vec<u16> = s.encode_utf16().collect();
170 let thai_utf16: Vec<u16> = TEST_STR_THAI.encode_utf16().collect();
171 let myanmar_utf16: Vec<u16> = TEST_STR_MYANMAR.encode_utf16().collect();
172
173 let mut iter = ComplexScriptIteratorUtf16::new(&utf16);
174 assert_eq!(
175 iter.next(),
176 Some((thai_utf16.as_slice(), ComplexScript::Thai)),
177 "Thai script with UTF-16 at first"
178 );
179 assert_eq!(
180 iter.next(),
181 Some((myanmar_utf16.as_slice(), ComplexScript::Myanmar)),
182 "Myanmar script with UTF-16 at second"
183 );
184 assert_eq!(iter.next(), None, "Iterator for UTF-16 is finished");
185
186 let mut iter = ComplexScriptIterator::new(&s);
187 assert_eq!(
188 iter.next(),
189 Some((TEST_STR_THAI, ComplexScript::Thai)),
190 "Thai script with UTF-8 at first"
191 );
192 assert_eq!(
193 iter.next(),
194 Some((TEST_STR_MYANMAR, ComplexScript::Myanmar)),
195 "Myanmar script with UTF-8 at second"
196 );
197 assert_eq!(iter.next(), None, "Iterator for UTF-8 is finished");
198 }
199 }