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
pub fn extract(text: &str) -> Option<char> {
let text = text.trim_end();
let text = if let Some(text) = text.strip_prefix('L') {
// wide char literal
text
} else if let Some(text) = text.strip_prefix('u') {
// UTF-16 char literals
text
} else {
// UTF-32 char literal
text.strip_prefix('U').unwrap_or(text)
};
if text.starts_with('\'') && text.ends_with('\'') {
let text = &text[1..text.len() - 1];
#[derive(Clone, Copy)]
enum State {
// In character
Chr,
// Escape sequence
Esc,
// Oct char (code, left)
Oct(u8, u8),
// Hex char (code, left, width)
Hex(u32, u8, u8),
}
let mut out = None;
let mut state = State::Chr;
for chr in text.chars() {
loop {
if out.is_some() {
return None;
}
match state {
State::Chr => match chr {
'\\' => {
state = State::Esc;
break;
}
_ => out = Some(chr),
},
State::Esc => {
out = Some(match chr {
'a' => '\x07',
'b' => '\x08',
'v' => '\x0b',
'f' => '\x0c',
'n' => '\n',
'r' => '\r',
't' => '\t',
'e' => '\x1b',
'u' => {
state = State::Hex(0, 4, 4);
break;
}
'U' => {
state = State::Hex(0, 8, 8);
break;
}
'x' => {
state = State::Hex(0, 2, 2);
break;
}
'0'..='7' => {
state = State::Oct(chr as u8 - b'0', 2);
break;
}
_ => chr,
})
}
State::Hex(r, n, w) => {
if n > 0 {
state = State::Hex(
(r << 4)
| match chr {
'0'..='9' => chr as u32 - b'0' as u32,
'a'..='f' => chr as u32 - (b'a' - 10) as u32,
'A'..='F' => chr as u32 - (b'A' - 10) as u32,
_ => {
out = Some(char::from_u32(r).unwrap_or('\0'));
state = State::Chr;
continue;
}
},
n - 1,
w,
);
break;
} else {
out = Some(char::from_u32(r).unwrap_or('\0'));
state = State::Chr;
continue;
}
}
State::Oct(r, n) => {
if n > 0 {
state = State::Oct(
(r << 3)
| match chr {
'0'..='7' => chr as u8 - b'0',
_ => {
out = Some(r as _);
state = State::Chr;
continue;
}
},
n - 1,
);
break;
} else {
out = Some(r as _);
state = State::Chr;
continue;
}
}
}
state = State::Chr;
break;
}
}
match state {
State::Hex(r, n, w) => {
out = if n < w {
Some(char::from_u32(r).unwrap_or('\0'))
} else {
None
}
}
State::Oct(r, _) => out = Some(r as _),
State::Esc => out = None,
_ => (),
}
out
} else {
None
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn not_a() {
assert!(extract("a").is_none());
assert!(extract("'a").is_none());
assert!(extract("a'").is_none());
assert!(extract("'ab'").is_none());
assert!(extract(r#"'\'"#).is_none());
assert!(extract(r#"'\x'"#).is_none());
assert!(extract(r#"'\u'"#).is_none());
assert!(extract(r#"'\U'"#).is_none());
}
#[test]
fn simple() {
assert_eq!(extract("'a'").unwrap(), 'a');
assert_eq!(extract("'a'\r\n").unwrap(), 'a');
}
#[test]
fn escaped_slash() {
assert_eq!(extract(r#"'\\'"#).unwrap(), '\\');
}
#[test]
fn escaped_newline() {
assert_eq!(extract(r#"'\n'"#).unwrap(), '\n');
}
#[test]
fn escaped_escape() {
assert_eq!(extract(r#"'\e'"#).unwrap(), '\x1b');
}
#[test]
fn escaped_null() {
assert_eq!(extract(r#"'\0'"#).unwrap(), '\0');
}
#[test]
fn escaped_oct() {
assert_eq!(extract(r#"'\01'"#).unwrap(), '\x01');
assert_eq!(extract(r#"'\012'"#).unwrap(), '\n');
assert_eq!(extract(r#"'\007'"#).unwrap(), '\x07');
}
#[test]
fn escaped_hex() {
assert_eq!(extract(r#"'\x0'"#).unwrap(), '\0');
assert_eq!(extract(r#"'\xa'"#).unwrap(), '\x0a');
assert_eq!(extract(r#"'\xA1'"#).unwrap(), '\u{a1}');
assert_eq!(extract(r#"'\xa0'"#).unwrap(), '\u{a0}');
}
#[test]
fn escaped_uni() {
assert_eq!(extract(r#"'\u0'"#).unwrap(), '\0');
assert_eq!(extract(r#"'\ua'"#).unwrap(), '\x0a');
assert_eq!(extract(r#"'\uA1'"#).unwrap(), '\u{a1}');
assert_eq!(extract(r#"'\uabc7'"#).unwrap(), '\u{abc7}');
assert_eq!(extract(r#"'\Uabc70'"#).unwrap(), '\u{abc70}');
assert_eq!(extract(r#"'\U10ffff'"#).unwrap(), '\u{10ffff}');
}
}