Skip to content

Commit 40f2caa

Browse files
committed
XXX: Tweak Mode.
use the from_token_lit order instead? - Add `use Mode::*` to avoid all the qualifiers. - Reorder the variants. The existing order makes no particular sense, which has bugged me for some time. I've chosen the order that makes the most sense to me: the singles first, then the non-raw strings, then the raw strings.
1 parent f9f5555 commit 40f2caa

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

compiler/rustc_lexer/src/unescape.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use std::ops::Range;
55
use std::str::Chars;
66

7+
use Mode::*;
8+
79
#[cfg(test)]
810
mod tests;
911

@@ -84,15 +86,14 @@ where
8486
F: FnMut(Range<usize>, Result<char, EscapeError>),
8587
{
8688
match mode {
87-
Mode::Char | Mode::Byte => {
89+
Char | Byte => {
8890
let mut chars = src.chars();
8991
let res = unescape_char_or_byte(&mut chars, mode);
9092
callback(0..(src.len() - chars.as_str().len()), res);
9193
}
92-
Mode::Str | Mode::ByteStr => unescape_str_common(src, mode, callback),
93-
94-
Mode::RawStr | Mode::RawByteStr => unescape_raw_str_or_raw_byte_str(src, mode, callback),
95-
Mode::CStr | Mode::RawCStr => unreachable!(),
94+
Str | ByteStr => unescape_str_common(src, mode, callback),
95+
RawStr | RawByteStr => unescape_raw_str_or_raw_byte_str(src, mode, callback),
96+
CStr | RawCStr => unreachable!(),
9697
}
9798
}
9899

@@ -118,84 +119,86 @@ pub fn unescape_c_string<F>(src: &str, mode: Mode, callback: &mut F)
118119
where
119120
F: FnMut(Range<usize>, Result<CStrUnit, EscapeError>),
120121
{
121-
if mode == Mode::RawCStr {
122-
unescape_raw_str_or_raw_byte_str(src, mode, &mut |r, result| {
123-
callback(r, result.map(CStrUnit::Char))
124-
});
125-
} else {
126-
unescape_str_common(src, mode, callback);
122+
match mode {
123+
CStr => {
124+
unescape_str_common(src, mode, callback);
125+
}
126+
RawCStr => {
127+
unescape_raw_str_or_raw_byte_str(src, mode, &mut |r, result| {
128+
callback(r, result.map(CStrUnit::Char))
129+
});
130+
}
131+
Char | Byte | Str | ByteStr | RawStr | RawByteStr => unreachable!(),
127132
}
128133
}
129134

130135
/// Takes a contents of a char literal (without quotes), and returns an
131136
/// unescaped char or an error.
132137
pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
133-
unescape_char_or_byte(&mut src.chars(), Mode::Char)
138+
unescape_char_or_byte(&mut src.chars(), Char)
134139
}
135140

136141
/// Takes a contents of a byte literal (without quotes), and returns an
137142
/// unescaped byte or an error.
138143
pub fn unescape_byte(src: &str) -> Result<u8, EscapeError> {
139-
unescape_char_or_byte(&mut src.chars(), Mode::Byte).map(byte_from_char)
144+
unescape_char_or_byte(&mut src.chars(), Byte).map(byte_from_char)
140145
}
141146

142147
/// What kind of literal do we parse.
143148
#[derive(Debug, Clone, Copy, PartialEq)]
144149
pub enum Mode {
145150
Char,
146-
Str,
151+
147152
Byte,
153+
154+
Str,
148155
ByteStr,
156+
CStr,
157+
149158
RawStr,
150159
RawByteStr,
151-
CStr,
152160
RawCStr,
153161
}
154162

155163
impl Mode {
156164
pub fn in_double_quotes(self) -> bool {
157165
match self {
158-
Mode::Str
159-
| Mode::ByteStr
160-
| Mode::RawStr
161-
| Mode::RawByteStr
162-
| Mode::CStr
163-
| Mode::RawCStr => true,
164-
Mode::Char | Mode::Byte => false,
166+
Str | ByteStr | CStr | RawStr | RawByteStr | RawCStr => true,
167+
Char | Byte => false,
165168
}
166169
}
167170

168171
/// Non-byte literals should have `\xXX` escapes that are within the ASCII range.
169172
fn ascii_escapes_should_be_ascii(self) -> bool {
170173
match self {
171-
Mode::Char | Mode::Str => true,
172-
Mode::Byte | Mode::ByteStr | Mode::CStr => false,
173-
Mode::RawStr | Mode::RawByteStr | Mode::RawCStr => unreachable!(),
174+
Char | Str => true,
175+
Byte | ByteStr | CStr => false,
176+
RawStr | RawByteStr | RawCStr => unreachable!(),
174177
}
175178
}
176179

177-
/// Whether characters within the literal must be within the ASCII range
180+
/// Whether characters within the literal must be within the ASCII range.
178181
#[inline]
179182
fn chars_should_be_ascii(self) -> bool {
180183
match self {
181-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
182-
Mode::Char | Mode::Str | Mode::RawStr | Mode::CStr | Mode::RawCStr => false,
184+
Byte | ByteStr | RawByteStr => true,
185+
Char | Str | CStr | RawStr | RawCStr => false,
183186
}
184187
}
185188

186189
/// Byte literals do not allow unicode escape.
187190
fn is_unicode_escape_disallowed(self) -> bool {
188191
match self {
189-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
190-
Mode::Char | Mode::Str | Mode::RawStr | Mode::CStr | Mode::RawCStr => false,
192+
Byte | ByteStr | RawByteStr => true,
193+
Char | Str | CStr | RawStr | RawCStr => false,
191194
}
192195
}
193196

194197
pub fn prefix_noraw(self) -> &'static str {
195198
match self {
196-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => "b",
197-
Mode::CStr | Mode::RawCStr => "c",
198-
Mode::Char | Mode::Str | Mode::RawStr => "",
199+
Char | Str | RawStr => "",
200+
Byte | ByteStr | RawByteStr => "b",
201+
CStr | RawCStr => "c",
199202
}
200203
}
201204
}
@@ -410,7 +413,7 @@ where
410413
#[inline]
411414
pub fn byte_from_char(c: char) -> u8 {
412415
let res = c as u32;
413-
debug_assert!(res <= u8::MAX as u32, "guaranteed because of Mode::ByteStr");
416+
debug_assert!(res <= u8::MAX as u32, "guaranteed because of ByteStr");
414417
res as u8
415418
}
416419

0 commit comments

Comments
 (0)