Skip to content

Commit 1d031fa

Browse files
committed
Tweak Mode.
- 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 an order that makes sense to me.
1 parent f9f5555 commit 1d031fa

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

compiler/rustc_lexer/src/unescape.rs

+38-34
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,87 @@ 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 | RawStr | ByteStr | 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,
148-
ByteStr,
153+
154+
Str,
149155
RawStr,
156+
157+
ByteStr,
150158
RawByteStr,
159+
151160
CStr,
152161
RawCStr,
153162
}
154163

155164
impl Mode {
156165
pub fn in_double_quotes(self) -> bool {
157166
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,
167+
Str | RawStr | ByteStr | RawByteStr | CStr | RawCStr => true,
168+
Char | Byte => false,
165169
}
166170
}
167171

168172
/// Non-byte literals should have `\xXX` escapes that are within the ASCII range.
169173
fn ascii_escapes_should_be_ascii(self) -> bool {
170174
match self {
171-
Mode::Char | Mode::Str => true,
172-
Mode::Byte | Mode::ByteStr | Mode::CStr => false,
173-
Mode::RawStr | Mode::RawByteStr | Mode::RawCStr => unreachable!(),
175+
Char | Str => true,
176+
Byte | ByteStr | CStr => false,
177+
RawStr | RawByteStr | RawCStr => unreachable!(),
174178
}
175179
}
176180

177-
/// Whether characters within the literal must be within the ASCII range
181+
/// Whether characters within the literal must be within the ASCII range.
178182
#[inline]
179183
fn chars_should_be_ascii(self) -> bool {
180184
match self {
181-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
182-
Mode::Char | Mode::Str | Mode::RawStr | Mode::CStr | Mode::RawCStr => false,
185+
Byte | ByteStr | RawByteStr => true,
186+
Char | Str | RawStr | CStr | RawCStr => false,
183187
}
184188
}
185189

186190
/// Byte literals do not allow unicode escape.
187191
fn is_unicode_escape_disallowed(self) -> bool {
188192
match self {
189-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
190-
Mode::Char | Mode::Str | Mode::RawStr | Mode::CStr | Mode::RawCStr => false,
193+
Byte | ByteStr | RawByteStr => true,
194+
Char | Str | RawStr | CStr | RawCStr => false,
191195
}
192196
}
193197

194198
pub fn prefix_noraw(self) -> &'static str {
195199
match self {
196-
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => "b",
197-
Mode::CStr | Mode::RawCStr => "c",
198-
Mode::Char | Mode::Str | Mode::RawStr => "",
200+
Char | Str | RawStr => "",
201+
Byte | ByteStr | RawByteStr => "b",
202+
CStr | RawCStr => "c",
199203
}
200204
}
201205
}
@@ -410,7 +414,7 @@ where
410414
#[inline]
411415
pub fn byte_from_char(c: char) -> u8 {
412416
let res = c as u32;
413-
debug_assert!(res <= u8::MAX as u32, "guaranteed because of Mode::ByteStr");
417+
debug_assert!(res <= u8::MAX as u32, "guaranteed because of ByteStr");
414418
res as u8
415419
}
416420

0 commit comments

Comments
 (0)