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
pub fn character_name_normalize(string: &mut String) {
let bytes = unsafe {
string.as_mut_vec()
};
let len = character_name_normalize_bytes(bytes).len();
bytes.truncate(len);
}
fn character_name_normalize_bytes(slice: &mut [u8]) -> &mut [u8] {
let mut next_write = 0;
let mut prev_space = true;
for i in 0..slice.len() {
let b = slice[i];
if b == b' ' {
prev_space = true;
continue;
} else if b == b'_' {
} else if b == b'-' {
let mut keep_hyphen = prev_space || slice.get(i+1) == Some(&b' ');
let rest_e = slice[i+1..] == b"E"[..] || slice[i+1..] == b"e"[..];
if !keep_hyphen && rest_e {
keep_hyphen = slice[..next_write] == b"hanguljungseongo"[..];
}
if keep_hyphen {
slice[next_write] = b;
next_write += 1;
}
} else if b'A' <= b && b <= b'Z' {
slice[next_write] = b + (b'a' - b'A');
next_write += 1;
} else if b <= 0x7F {
slice[next_write] = b;
next_write += 1;
}
prev_space = false;
}
&mut slice[..next_write]
}
pub fn symbolic_name_normalize(string: &mut String) {
let bytes = unsafe {
string.as_mut_vec()
};
let len = symbolic_name_normalize_bytes(bytes).len();
bytes.truncate(len);
}
fn symbolic_name_normalize_bytes(slice: &mut [u8]) -> &mut [u8] {
let mut start = 0;
if slice.len() >= 2 {
let starts_with_is =
slice[0..2] == b"is"[..]
|| slice[0..2] == b"IS"[..]
|| slice[0..2] == b"iS"[..]
|| slice[0..2] == b"Is"[..];
if starts_with_is {
start = 2;
}
}
let mut next_write = 0;
for i in start..slice.len() {
let b = slice[i];
if b == b' ' || b == b'_' || b == b'-' {
continue;
} else if b'A' <= b && b <= b'Z' {
slice[next_write] = b + (b'a' - b'A');
next_write += 1;
} else if b <= 0x7F {
slice[next_write] = b;
next_write += 1;
}
}
&mut slice[..next_write]
}
#[cfg(test)]
mod tests {
use super::{
character_name_normalize, character_name_normalize_bytes,
symbolic_name_normalize, symbolic_name_normalize_bytes,
};
fn char_norm(s: &str) -> String {
let mut s = s.to_string();
character_name_normalize(&mut s);
s
}
fn sym_norm(s: &str) -> String {
let mut s = s.to_string();
symbolic_name_normalize(&mut s);
s
}
#[test]
fn char_normalize() {
assert_eq!(char_norm("HANGUL JUNGSEONG O-E"), "hanguljungseongo-e");
assert_eq!(char_norm("zero-width space"), "zerowidthspace");
assert_eq!(char_norm("zerowidthspace"), "zerowidthspace");
assert_eq!(char_norm("ZERO WIDTH SPACE"), "zerowidthspace");
assert_eq!(char_norm("TIBETAN MARK TSA -PHRU"), "tibetanmarktsa-phru");
}
#[test]
fn sym_normalize() {
assert_eq!(sym_norm("Line_Break"), "linebreak");
assert_eq!(sym_norm("Line-break"), "linebreak");
assert_eq!(sym_norm("linebreak"), "linebreak");
assert_eq!(sym_norm("BA"), "ba");
assert_eq!(sym_norm("ba"), "ba");
assert_eq!(sym_norm("Greek"), "greek");
assert_eq!(sym_norm("isGreek"), "greek");
assert_eq!(sym_norm("IS_Greek"), "greek");
}
#[test]
fn valid_utf8_character() {
let mut x = b"abc\xFFxyz".to_vec();
let y = character_name_normalize_bytes(&mut x);
assert_eq!(y, b"abcxyz");
}
#[test]
fn valid_utf8_symbolic() {
let mut x = b"abc\xFFxyz".to_vec();
let y = symbolic_name_normalize_bytes(&mut x);
assert_eq!(y, b"abcxyz");
}
}