Skip to content

Commit f0daff2

Browse files
authored
Rollup merge of rust-lang#102347 - nnethercote:unescaping-cleanups, r=matklad
Unescaping cleanups Some minor improvements. r? `@matklad`
2 parents b263b7e + 94cb5e8 commit f0daff2

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

compiler/rustc_lexer/src/unescape.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
// NOTE: Raw strings do not perform any explicit character escaping, here we
9494
// only translate CRLF to LF and produce errors on bare CR.
9595
Mode::RawStr | Mode::RawByteStr => {
96-
unescape_raw_str_or_byte_str(literal_text, mode, callback)
96+
unescape_raw_str_or_raw_byte_str(literal_text, mode, callback)
9797
}
9898
}
9999
}
@@ -105,7 +105,7 @@ pub fn unescape_byte_literal<F>(literal_text: &str, mode: Mode, callback: &mut F
105105
where
106106
F: FnMut(Range<usize>, Result<u8, EscapeError>),
107107
{
108-
assert!(mode.is_bytes());
108+
debug_assert!(mode.is_bytes());
109109
unescape_literal(literal_text, mode, &mut |range, result| {
110110
callback(range, result.map(byte_from_char));
111111
})
@@ -129,7 +129,7 @@ pub fn unescape_byte(literal_text: &str) -> Result<u8, (usize, EscapeError)> {
129129
}
130130

131131
/// What kind of literal do we parse.
132-
#[derive(Debug, Clone, Copy)]
132+
#[derive(Debug, Clone, Copy, PartialEq)]
133133
pub enum Mode {
134134
Char,
135135
Str,
@@ -140,17 +140,13 @@ pub enum Mode {
140140
}
141141

142142
impl Mode {
143-
pub fn in_single_quotes(self) -> bool {
143+
pub fn in_double_quotes(self) -> bool {
144144
match self {
145-
Mode::Char | Mode::Byte => true,
146-
Mode::Str | Mode::ByteStr | Mode::RawStr | Mode::RawByteStr => false,
145+
Mode::Str | Mode::ByteStr | Mode::RawStr | Mode::RawByteStr => true,
146+
Mode::Char | Mode::Byte => false,
147147
}
148148
}
149149

150-
pub fn in_double_quotes(self) -> bool {
151-
!self.in_single_quotes()
152-
}
153-
154150
pub fn is_bytes(self) -> bool {
155151
match self {
156152
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
@@ -184,7 +180,7 @@ fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
184180

185181
let value = hi * 16 + lo;
186182

187-
// For a byte literal verify that it is within ASCII range.
183+
// For a non-byte literal verify that it is within ASCII range.
188184
if !mode.is_bytes() && !is_ascii(value) {
189185
return Err(EscapeError::OutOfRangeHexEscape);
190186
}
@@ -263,6 +259,7 @@ fn ascii_check(first_char: char, mode: Mode) -> Result<char, EscapeError> {
263259
}
264260

265261
fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
262+
debug_assert!(mode == Mode::Char || mode == Mode::Byte);
266263
let first_char = chars.next().ok_or(EscapeError::ZeroChars)?;
267264
let res = match first_char {
268265
'\\' => scan_escape(chars, mode),
@@ -282,7 +279,7 @@ fn unescape_str_or_byte_str<F>(src: &str, mode: Mode, callback: &mut F)
282279
where
283280
F: FnMut(Range<usize>, Result<char, EscapeError>),
284281
{
285-
assert!(mode.in_double_quotes());
282+
debug_assert!(mode == Mode::Str || mode == Mode::ByteStr);
286283
let initial_len = src.len();
287284
let mut chars = src.chars();
288285
while let Some(first_char) = chars.next() {
@@ -344,11 +341,11 @@ where
344341
/// sequence of characters or errors.
345342
/// NOTE: Raw strings do not perform any explicit character escaping, here we
346343
/// only translate CRLF to LF and produce errors on bare CR.
347-
fn unescape_raw_str_or_byte_str<F>(literal_text: &str, mode: Mode, callback: &mut F)
344+
fn unescape_raw_str_or_raw_byte_str<F>(literal_text: &str, mode: Mode, callback: &mut F)
348345
where
349346
F: FnMut(Range<usize>, Result<char, EscapeError>),
350347
{
351-
assert!(mode.in_double_quotes());
348+
debug_assert!(mode == Mode::RawStr || mode == Mode::RawByteStr);
352349
let initial_len = literal_text.len();
353350

354351
let mut chars = literal_text.chars();
@@ -368,7 +365,7 @@ where
368365

369366
fn byte_from_char(c: char) -> u8 {
370367
let res = c as u32;
371-
assert!(res <= u8::MAX as u32, "guaranteed because of Mode::ByteStr");
368+
debug_assert!(res <= u8::MAX as u32, "guaranteed because of Mode::ByteStr");
372369
res as u8
373370
}
374371

0 commit comments

Comments
 (0)