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
use std::cmp::Ordering;
use std::result;
use ucd_util::{self, PropertyValues};
use hir;
use unicode_tables::age;
use unicode_tables::case_folding_simple::CASE_FOLDING_SIMPLE;
use unicode_tables::general_category;
use unicode_tables::property_bool;
use unicode_tables::property_names::PROPERTY_NAMES;
use unicode_tables::property_values::PROPERTY_VALUES;
use unicode_tables::script;
use unicode_tables::script_extension;
type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
PropertyNotFound,
PropertyValueNotFound,
}
pub fn encode_utf8(character: char, dst: &mut [u8]) -> Option<usize> {
const TAG_CONT: u8 = 0b1000_0000;
const TAG_TWO: u8 = 0b1100_0000;
const TAG_THREE: u8 = 0b1110_0000;
const TAG_FOUR: u8 = 0b1111_0000;
let code = character as u32;
if code <= 0x7F && !dst.is_empty() {
dst[0] = code as u8;
Some(1)
} else if code <= 0x7FF && dst.len() >= 2 {
dst[0] = (code >> 6 & 0x1F) as u8 | TAG_TWO;
dst[1] = (code & 0x3F) as u8 | TAG_CONT;
Some(2)
} else if code <= 0xFFFF && dst.len() >= 3 {
dst[0] = (code >> 12 & 0x0F) as u8 | TAG_THREE;
dst[1] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
dst[2] = (code & 0x3F) as u8 | TAG_CONT;
Some(3)
} else if dst.len() >= 4 {
dst[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR;
dst[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
dst[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
dst[3] = (code & 0x3F) as u8 | TAG_CONT;
Some(4)
} else {
None
}
}
#[derive(Debug)]
pub struct SimpleFoldIter(::std::slice::Iter<'static, char>);
impl Iterator for SimpleFoldIter {
type Item = char;
fn next(&mut self) -> Option<char> {
self.0.next().map(|c| *c)
}
}
pub fn simple_fold(c: char) -> result::Result<SimpleFoldIter, Option<char>> {
CASE_FOLDING_SIMPLE
.binary_search_by_key(&c, |&(c1, _)| c1)
.map(|i| SimpleFoldIter(CASE_FOLDING_SIMPLE[i].1.iter()))
.map_err(|i| {
if i >= CASE_FOLDING_SIMPLE.len() {
None
} else {
Some(CASE_FOLDING_SIMPLE[i].0)
}
})
}
pub fn contains_simple_case_mapping(start: char, end: char) -> bool {
assert!(start <= end);
CASE_FOLDING_SIMPLE
.binary_search_by(|&(c, _)| {
if start <= c && c <= end {
Ordering::Equal
} else if c > end {
Ordering::Greater
} else {
Ordering::Less
}
}).is_ok()
}
#[derive(Debug)]
pub enum ClassQuery<'a> {
OneLetter(char),
Binary(&'a str),
ByValue {
property_name: &'a str,
property_value: &'a str,
},
}
impl<'a> ClassQuery<'a> {
fn canonicalize(&self) -> Result<CanonicalClassQuery> {
match *self {
ClassQuery::OneLetter(c) => self.canonical_binary(&c.to_string()),
ClassQuery::Binary(name) => self.canonical_binary(name),
ClassQuery::ByValue { property_name, property_value } => {
let property_name = normalize(property_name);
let property_value = normalize(property_value);
let canon_name = match canonical_prop(&property_name) {
None => return Err(Error::PropertyNotFound),
Some(canon_name) => canon_name,
};
Ok(match canon_name {
"General_Category" => {
let canon = match canonical_gencat(&property_value) {
None => return Err(Error::PropertyValueNotFound),
Some(canon) => canon,
};
CanonicalClassQuery::GeneralCategory(canon)
}
"Script" => {
let canon = match canonical_script(&property_value) {
None => return Err(Error::PropertyValueNotFound),
Some(canon) => canon,
};
CanonicalClassQuery::Script(canon)
}
_ => {
let vals = match property_values(canon_name) {
None => return Err(Error::PropertyValueNotFound),
Some(vals) => vals,
};
let canon_val = match canonical_value(
vals,
&property_value,
) {
None => return Err(Error::PropertyValueNotFound),
Some(canon_val) => canon_val,
};
CanonicalClassQuery::ByValue {
property_name: canon_name,
property_value: canon_val,
}
}
})
}
}
}
fn canonical_binary(&self, name: &str) -> Result<CanonicalClassQuery> {
let norm = normalize(name);
if let Some(canon) = canonical_prop(&norm) {
return Ok(CanonicalClassQuery::Binary(canon));
}
if let Some(canon) = canonical_gencat(&norm) {
return Ok(CanonicalClassQuery::GeneralCategory(canon));
}
if let Some(canon) = canonical_script(&norm) {
return Ok(CanonicalClassQuery::Script(canon));
}
Err(Error::PropertyNotFound)
}
}
#[derive(Debug, Eq, PartialEq)]
enum CanonicalClassQuery {
Binary(&'static str),
GeneralCategory(&'static str),
Script(&'static str),
ByValue {
property_name: &'static str,
property_value: &'static str,
},
}
pub fn class<'a>(query: ClassQuery<'a>) -> Result<hir::ClassUnicode> {
use self::CanonicalClassQuery::*;
match try!(query.canonicalize()) {
Binary(name) => {
property_set(property_bool::BY_NAME, name)
.map(hir_class)
.ok_or(Error::PropertyNotFound)
}
GeneralCategory("Any") => {
Ok(hir_class(&[('\0', '\u{10FFFF}')]))
}
GeneralCategory("Assigned") => {
let mut cls =
try!(property_set(general_category::BY_NAME, "Unassigned")
.map(hir_class)
.ok_or(Error::PropertyNotFound));
cls.negate();
Ok(cls)
}
GeneralCategory("ASCII") => {
Ok(hir_class(&[('\0', '\x7F')]))
}
GeneralCategory(name) => {
property_set(general_category::BY_NAME, name)
.map(hir_class)
.ok_or(Error::PropertyValueNotFound)
}
Script(name) => {
property_set(script::BY_NAME, name)
.map(hir_class)
.ok_or(Error::PropertyValueNotFound)
}
ByValue { property_name: "Age", property_value } => {
let mut class = hir::ClassUnicode::empty();
for set in try!(ages(property_value)) {
class.union(&hir_class(set));
}
Ok(class)
}
ByValue { property_name: "Script_Extensions", property_value } => {
property_set(script_extension::BY_NAME, property_value)
.map(hir_class)
.ok_or(Error::PropertyValueNotFound)
}
_ => {
Err(Error::PropertyNotFound)
}
}
}
pub fn hir_class(ranges: &[(char, char)]) -> hir::ClassUnicode {
let hir_ranges: Vec<hir::ClassUnicodeRange> = ranges
.iter()
.map(|&(s, e)| hir::ClassUnicodeRange::new(s, e))
.collect();
hir::ClassUnicode::new(hir_ranges)
}
fn canonical_prop(normalized_name: &str) -> Option<&'static str> {
ucd_util::canonical_property_name(PROPERTY_NAMES, normalized_name)
}
fn canonical_gencat(normalized_value: &str) -> Option<&'static str> {
match normalized_value {
"any" => Some("Any"),
"assigned" => Some("Assigned"),
"ascii" => Some("ASCII"),
_ => {
let gencats = property_values("General_Category").unwrap();
canonical_value(gencats, normalized_value)
}
}
}
fn canonical_script(normalized_value: &str) -> Option<&'static str> {
let scripts = property_values("Script").unwrap();
canonical_value(scripts, normalized_value)
}
fn canonical_value(
vals: PropertyValues,
normalized_value: &str,
) -> Option<&'static str> {
ucd_util::canonical_property_value(vals, normalized_value)
}
fn normalize(x: &str) -> String {
let mut x = x.to_string();
ucd_util::symbolic_name_normalize(&mut x);
x
}
fn property_values(
canonical_property_name: &'static str,
) -> Option<PropertyValues>
{
ucd_util::property_values(PROPERTY_VALUES, canonical_property_name)
}
fn property_set(
name_map: &'static [(&'static str, &'static [(char, char)])],
canonical: &'static str,
) -> Option<&'static [(char, char)]> {
name_map
.binary_search_by_key(&canonical, |x| x.0)
.ok()
.map(|i| name_map[i].1)
}
#[derive(Debug)]
struct AgeIter {
ages: &'static [(&'static str, &'static [(char, char)])],
}
fn ages(canonical_age: &str) -> Result<AgeIter> {
const AGES: &'static [(&'static str, &'static [(char, char)])] = &[
("V1_1", age::V1_1),
("V2_0", age::V2_0),
("V2_1", age::V2_1),
("V3_0", age::V3_0),
("V3_1", age::V3_1),
("V3_2", age::V3_2),
("V4_0", age::V4_0),
("V4_1", age::V4_1),
("V5_0", age::V5_0),
("V5_1", age::V5_1),
("V5_2", age::V5_2),
("V6_0", age::V6_0),
("V6_1", age::V6_1),
("V6_2", age::V6_2),
("V6_3", age::V6_3),
("V7_0", age::V7_0),
("V8_0", age::V8_0),
("V9_0", age::V9_0),
("V10_0", age::V10_0),
];
assert_eq!(AGES.len(), age::BY_NAME.len(), "ages are out of sync");
let pos = AGES.iter().position(|&(age, _)| canonical_age == age);
match pos {
None => Err(Error::PropertyValueNotFound),
Some(i) => Ok(AgeIter { ages: &AGES[..i+1] }),
}
}
impl Iterator for AgeIter {
type Item = &'static [(char, char)];
fn next(&mut self) -> Option<&'static [(char, char)]> {
if self.ages.is_empty() {
None
} else {
let set = self.ages[0];
self.ages = &self.ages[1..];
Some(set.1)
}
}
}
#[cfg(test)]
mod tests {
use super::{contains_simple_case_mapping, simple_fold};
#[test]
fn simple_fold_k() {
let xs: Vec<char> = simple_fold('k').unwrap().collect();
assert_eq!(xs, vec!['K', 'K']);
let xs: Vec<char> = simple_fold('K').unwrap().collect();
assert_eq!(xs, vec!['k', 'K']);
let xs: Vec<char> = simple_fold('K').unwrap().collect();
assert_eq!(xs, vec!['K', 'k']);
}
#[test]
fn simple_fold_a() {
let xs: Vec<char> = simple_fold('a').unwrap().collect();
assert_eq!(xs, vec!['A']);
let xs: Vec<char> = simple_fold('A').unwrap().collect();
assert_eq!(xs, vec!['a']);
}
#[test]
fn simple_fold_empty() {
assert_eq!(Some('A'), simple_fold('?').unwrap_err());
assert_eq!(Some('A'), simple_fold('@').unwrap_err());
assert_eq!(Some('a'), simple_fold('[').unwrap_err());
assert_eq!(Some('Ⰰ'), simple_fold('☃').unwrap_err());
}
#[test]
fn simple_fold_max() {
assert_eq!(None, simple_fold('\u{10FFFE}').unwrap_err());
assert_eq!(None, simple_fold('\u{10FFFF}').unwrap_err());
}
#[test]
fn range_contains() {
assert!(contains_simple_case_mapping('A', 'A'));
assert!(contains_simple_case_mapping('Z', 'Z'));
assert!(contains_simple_case_mapping('A', 'Z'));
assert!(contains_simple_case_mapping('@', 'A'));
assert!(contains_simple_case_mapping('Z', '['));
assert!(contains_simple_case_mapping('☃', 'Ⰰ'));
assert!(!contains_simple_case_mapping('[', '['));
assert!(!contains_simple_case_mapping('[', '`'));
assert!(!contains_simple_case_mapping('☃', '☃'));
}
#[test]
fn regression_466() {
use super::{CanonicalClassQuery, ClassQuery};
let q = ClassQuery::OneLetter('C');
assert_eq!(
q.canonicalize().unwrap(),
CanonicalClassQuery::GeneralCategory("Other"));
}
}