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
use std::path::Path;
use std::str::FromStr;
use once_cell::sync::Lazy;
use regex::Regex;
use crate::common::{Codepoint, CodepointIter, UcdFile, UcdFileByCodepoint};
use crate::error::Error;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CaseFold {
pub codepoint: Codepoint,
pub status: CaseStatus,
pub mapping: Vec<Codepoint>,
}
impl UcdFile for CaseFold {
fn relative_file_path() -> &'static Path {
Path::new("CaseFolding.txt")
}
}
impl UcdFileByCodepoint for CaseFold {
fn codepoints(&self) -> CodepointIter {
self.codepoint.into_iter()
}
}
impl FromStr for CaseFold {
type Err = Error;
fn from_str(line: &str) -> Result<CaseFold, Error> {
static PARTS: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
^
\s*(?P<codepoint>[^\s;]+)\s*;
\s*(?P<status>[^\s;]+)\s*;
\s*(?P<mapping>[^;]+)\s*;
",
)
.unwrap()
});
let caps = match PARTS.captures(line.trim()) {
Some(caps) => caps,
None => return err!("invalid CaseFolding line: '{}'", line),
};
let mut mapping = vec![];
for cp in caps["mapping"].split_whitespace() {
mapping.push(cp.parse()?);
}
Ok(CaseFold {
codepoint: caps["codepoint"].parse()?,
status: caps["status"].parse()?,
mapping,
})
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CaseStatus {
Common,
Full,
Simple,
Special,
}
impl Default for CaseStatus {
fn default() -> CaseStatus {
CaseStatus::Common
}
}
impl CaseStatus {
pub fn is_fixed(&self) -> bool {
*self != CaseStatus::Full
}
}
impl FromStr for CaseStatus {
type Err = Error;
fn from_str(s: &str) -> Result<CaseStatus, Error> {
match s {
"C" => Ok(CaseStatus::Common),
"F" => Ok(CaseStatus::Full),
"S" => Ok(CaseStatus::Simple),
"T" => Ok(CaseStatus::Special),
_ => err!(
"unrecognized case status: '{}' \
(must be one of C, F, S or T)",
s
),
}
}
}
#[cfg(test)]
mod tests {
use super::{CaseFold, CaseStatus};
#[test]
fn parse_common() {
let line =
"0150; C; 0151; # LATIN CAPITAL LETTER O WITH DOUBLE ACUTE\n";
let row: CaseFold = line.parse().unwrap();
assert_eq!(row.codepoint, 0x0150);
assert_eq!(row.status, CaseStatus::Common);
assert_eq!(row.mapping, vec![0x0151]);
}
#[test]
fn parse_full() {
let line = "03B0; F; 03C5 0308 0301; # GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS\n";
let row: CaseFold = line.parse().unwrap();
assert_eq!(row.codepoint, 0x03B0);
assert_eq!(row.status, CaseStatus::Full);
assert_eq!(row.mapping, vec![0x03C5, 0x0308, 0x0301]);
}
#[test]
fn parse_simple() {
let line = "1F8F; S; 1F87; # GREEK CAPITAL LETTER ALPHA WITH DASIA AND PERISPOMENI AND PROSGEGRAMMENI\n";
let row: CaseFold = line.parse().unwrap();
assert_eq!(row.codepoint, 0x1F8F);
assert_eq!(row.status, CaseStatus::Simple);
assert_eq!(row.mapping, vec![0x1F87]);
}
#[test]
fn parse_special() {
let line = "0049; T; 0131; # LATIN CAPITAL LETTER I\n";
let row: CaseFold = line.parse().unwrap();
assert_eq!(row.codepoint, 0x0049);
assert_eq!(row.status, CaseStatus::Special);
assert_eq!(row.mapping, vec![0x0131]);
}
}