1
Fork 0

Use Mode less.

It's passed to numerous places where we just need an `is_byte` bool.
Passing the bool avoids the need for some assertions.

Also rename `is_bytes()` as `is_byte()`, to better match `Mode::Byte`,
`Mode::ByteStr`, and `Mode::RawByteStr`.
This commit is contained in:
Nicholas Nethercote 2022-11-03 13:35:49 +11:00
parent 84ca2c3bab
commit 34b32b0dac
2 changed files with 29 additions and 31 deletions

View file

@ -85,14 +85,16 @@ where
match mode { match mode {
Mode::Char | Mode::Byte => { Mode::Char | Mode::Byte => {
let mut chars = src.chars(); let mut chars = src.chars();
let result = unescape_char_or_byte(&mut chars, mode); let result = unescape_char_or_byte(&mut chars, mode == Mode::Byte);
// The Chars iterator moved forward. // The Chars iterator moved forward.
callback(0..(src.len() - chars.as_str().len()), result); callback(0..(src.len() - chars.as_str().len()), result);
} }
Mode::Str | Mode::ByteStr => unescape_str_or_byte_str(src, mode, callback), Mode::Str | Mode::ByteStr => unescape_str_or_byte_str(src, mode == Mode::ByteStr, callback),
// NOTE: Raw strings do not perform any explicit character escaping, here we // NOTE: Raw strings do not perform any explicit character escaping, here we
// only translate CRLF to LF and produce errors on bare CR. // only translate CRLF to LF and produce errors on bare CR.
Mode::RawStr | Mode::RawByteStr => unescape_raw_str_or_raw_byte_str(src, mode, callback), Mode::RawStr | Mode::RawByteStr => {
unescape_raw_str_or_raw_byte_str(src, mode == Mode::RawByteStr, callback)
}
} }
} }
@ -103,7 +105,7 @@ pub fn unescape_byte_literal<F>(src: &str, mode: Mode, callback: &mut F)
where where
F: FnMut(Range<usize>, Result<u8, EscapeError>), F: FnMut(Range<usize>, Result<u8, EscapeError>),
{ {
debug_assert!(mode.is_bytes()); debug_assert!(mode.is_byte());
unescape_literal(src, mode, &mut |range, result| { unescape_literal(src, mode, &mut |range, result| {
callback(range, result.map(byte_from_char)); callback(range, result.map(byte_from_char));
}) })
@ -113,15 +115,14 @@ where
/// unescaped char or an error /// unescaped char or an error
pub fn unescape_char(src: &str) -> Result<char, (usize, EscapeError)> { pub fn unescape_char(src: &str) -> Result<char, (usize, EscapeError)> {
let mut chars = src.chars(); let mut chars = src.chars();
unescape_char_or_byte(&mut chars, Mode::Char) unescape_char_or_byte(&mut chars, false).map_err(|err| (src.len() - chars.as_str().len(), err))
.map_err(|err| (src.len() - chars.as_str().len(), err))
} }
/// Takes a contents of a byte literal (without quotes), and returns an /// Takes a contents of a byte literal (without quotes), and returns an
/// unescaped byte or an error. /// unescaped byte or an error.
pub fn unescape_byte(src: &str) -> Result<u8, (usize, EscapeError)> { pub fn unescape_byte(src: &str) -> Result<u8, (usize, EscapeError)> {
let mut chars = src.chars(); let mut chars = src.chars();
unescape_char_or_byte(&mut chars, Mode::Byte) unescape_char_or_byte(&mut chars, true)
.map(byte_from_char) .map(byte_from_char)
.map_err(|err| (src.len() - chars.as_str().len(), err)) .map_err(|err| (src.len() - chars.as_str().len(), err))
} }
@ -145,7 +146,7 @@ impl Mode {
} }
} }
pub fn is_bytes(self) -> bool { pub fn is_byte(self) -> bool {
match self { match self {
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true, Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
Mode::Char | Mode::Str | Mode::RawStr => false, Mode::Char | Mode::Str | Mode::RawStr => false,
@ -153,7 +154,7 @@ impl Mode {
} }
} }
fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> { fn scan_escape(chars: &mut Chars<'_>, is_byte: bool) -> Result<char, EscapeError> {
// Previous character was '\\', unescape what follows. // Previous character was '\\', unescape what follows.
let res = match chars.next().ok_or(EscapeError::LoneSlash)? { let res = match chars.next().ok_or(EscapeError::LoneSlash)? {
'"' => '"', '"' => '"',
@ -176,7 +177,7 @@ fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
let value = hi * 16 + lo; let value = hi * 16 + lo;
// For a non-byte literal verify that it is within ASCII range. // For a non-byte literal verify that it is within ASCII range.
if !mode.is_bytes() && !is_ascii(value) { if !is_byte && !is_ascii(value) {
return Err(EscapeError::OutOfRangeHexEscape); return Err(EscapeError::OutOfRangeHexEscape);
} }
let value = value as u8; let value = value as u8;
@ -212,7 +213,7 @@ fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
// Incorrect syntax has higher priority for error reporting // Incorrect syntax has higher priority for error reporting
// than unallowed value for a literal. // than unallowed value for a literal.
if mode.is_bytes() { if is_byte {
return Err(EscapeError::UnicodeEscapeInByte); return Err(EscapeError::UnicodeEscapeInByte);
} }
@ -244,8 +245,8 @@ fn scan_escape(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
} }
#[inline] #[inline]
fn ascii_check(c: char, mode: Mode) -> Result<char, EscapeError> { fn ascii_check(c: char, is_byte: bool) -> Result<char, EscapeError> {
if mode.is_bytes() && !c.is_ascii() { if is_byte && !c.is_ascii() {
// Byte literal can't be a non-ascii character. // Byte literal can't be a non-ascii character.
Err(EscapeError::NonAsciiCharInByte) Err(EscapeError::NonAsciiCharInByte)
} else { } else {
@ -253,14 +254,13 @@ fn ascii_check(c: char, mode: Mode) -> Result<char, EscapeError> {
} }
} }
fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> { fn unescape_char_or_byte(chars: &mut Chars<'_>, is_byte: bool) -> Result<char, EscapeError> {
debug_assert!(mode == Mode::Char || mode == Mode::Byte);
let c = chars.next().ok_or(EscapeError::ZeroChars)?; let c = chars.next().ok_or(EscapeError::ZeroChars)?;
let res = match c { let res = match c {
'\\' => scan_escape(chars, mode), '\\' => scan_escape(chars, is_byte),
'\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar), '\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar),
'\r' => Err(EscapeError::BareCarriageReturn), '\r' => Err(EscapeError::BareCarriageReturn),
_ => ascii_check(c, mode), _ => ascii_check(c, is_byte),
}?; }?;
if chars.next().is_some() { if chars.next().is_some() {
return Err(EscapeError::MoreThanOneChar); return Err(EscapeError::MoreThanOneChar);
@ -270,11 +270,10 @@ fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, Esca
/// Takes a contents of a string literal (without quotes) and produces a /// Takes a contents of a string literal (without quotes) and produces a
/// sequence of escaped characters or errors. /// sequence of escaped characters or errors.
fn unescape_str_or_byte_str<F>(src: &str, mode: Mode, callback: &mut F) fn unescape_str_or_byte_str<F>(src: &str, is_byte: bool, callback: &mut F)
where where
F: FnMut(Range<usize>, Result<char, EscapeError>), F: FnMut(Range<usize>, Result<char, EscapeError>),
{ {
debug_assert!(mode == Mode::Str || mode == Mode::ByteStr);
let mut chars = src.chars(); let mut chars = src.chars();
// The `start` and `end` computation here is complicated because // The `start` and `end` computation here is complicated because
@ -293,14 +292,14 @@ where
skip_ascii_whitespace(&mut chars, start, callback); skip_ascii_whitespace(&mut chars, start, callback);
continue; continue;
} }
_ => scan_escape(&mut chars, mode), _ => scan_escape(&mut chars, is_byte),
} }
} }
'\n' => Ok('\n'), '\n' => Ok('\n'),
'\t' => Ok('\t'), '\t' => Ok('\t'),
'"' => Err(EscapeError::EscapeOnlyChar), '"' => Err(EscapeError::EscapeOnlyChar),
'\r' => Err(EscapeError::BareCarriageReturn), '\r' => Err(EscapeError::BareCarriageReturn),
_ => ascii_check(c, mode), _ => ascii_check(c, is_byte),
}; };
let end = src.len() - chars.as_str().len(); let end = src.len() - chars.as_str().len();
callback(start..end, result); callback(start..end, result);
@ -337,11 +336,10 @@ where
/// sequence of characters or errors. /// sequence of characters or errors.
/// NOTE: Raw strings do not perform any explicit character escaping, here we /// NOTE: Raw strings do not perform any explicit character escaping, here we
/// only translate CRLF to LF and produce errors on bare CR. /// only translate CRLF to LF and produce errors on bare CR.
fn unescape_raw_str_or_raw_byte_str<F>(src: &str, mode: Mode, callback: &mut F) fn unescape_raw_str_or_raw_byte_str<F>(src: &str, is_byte: bool, callback: &mut F)
where where
F: FnMut(Range<usize>, Result<char, EscapeError>), F: FnMut(Range<usize>, Result<char, EscapeError>),
{ {
debug_assert!(mode == Mode::RawStr || mode == Mode::RawByteStr);
let mut chars = src.chars(); let mut chars = src.chars();
// The `start` and `end` computation here matches the one in // The `start` and `end` computation here matches the one in
@ -351,7 +349,7 @@ where
let start = src.len() - chars.as_str().len() - c.len_utf8(); let start = src.len() - chars.as_str().len() - c.len_utf8();
let result = match c { let result = match c {
'\r' => Err(EscapeError::BareCarriageReturnInRawString), '\r' => Err(EscapeError::BareCarriageReturnInRawString),
c if mode.is_bytes() && !c.is_ascii() => Err(EscapeError::NonAsciiCharInByteString), c if is_byte && !c.is_ascii() => Err(EscapeError::NonAsciiCharInByteString),
c => Ok(c), c => Ok(c),
}; };
let end = src.len() - chars.as_str().len(); let end = src.len() - chars.as_str().len();

View file

@ -108,7 +108,7 @@ pub(crate) fn emit_unescape_error(
} }
if !has_help { if !has_help {
let (prefix, msg) = if mode.is_bytes() { let (prefix, msg) = if mode.is_byte() {
("b", "if you meant to write a byte string literal, use double quotes") ("b", "if you meant to write a byte string literal, use double quotes")
} else { } else {
("", "if you meant to write a `str` literal, use double quotes") ("", "if you meant to write a `str` literal, use double quotes")
@ -142,7 +142,7 @@ pub(crate) fn emit_unescape_error(
EscapeError::EscapeOnlyChar => { EscapeError::EscapeOnlyChar => {
let (c, char_span) = last_char(); let (c, char_span) = last_char();
let msg = if mode.is_bytes() { let msg = if mode.is_byte() {
"byte constant must be escaped" "byte constant must be escaped"
} else { } else {
"character constant must be escaped" "character constant must be escaped"
@ -182,11 +182,11 @@ pub(crate) fn emit_unescape_error(
let (c, span) = last_char(); let (c, span) = last_char();
let label = let label =
if mode.is_bytes() { "unknown byte escape" } else { "unknown character escape" }; if mode.is_byte() { "unknown byte escape" } else { "unknown character escape" };
let ec = escaped_char(c); let ec = escaped_char(c);
let mut diag = handler.struct_span_err(span, &format!("{}: `{}`", label, ec)); let mut diag = handler.struct_span_err(span, &format!("{}: `{}`", label, ec));
diag.span_label(span, label); diag.span_label(span, label);
if c == '{' || c == '}' && !mode.is_bytes() { if c == '{' || c == '}' && !mode.is_byte() {
diag.help( diag.help(
"if used in a formatting string, curly braces are escaped with `{{` and `}}`", "if used in a formatting string, curly braces are escaped with `{{` and `}}`",
); );
@ -196,7 +196,7 @@ pub(crate) fn emit_unescape_error(
version control settings", version control settings",
); );
} else { } else {
if !mode.is_bytes() { if !mode.is_byte() {
diag.span_suggestion( diag.span_suggestion(
span_with_quotes, span_with_quotes,
"if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal", "if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal",
@ -231,7 +231,7 @@ pub(crate) fn emit_unescape_error(
.emit(); .emit();
} }
EscapeError::NonAsciiCharInByte => { EscapeError::NonAsciiCharInByte => {
assert!(mode.is_bytes()); assert!(mode.is_byte());
let (c, span) = last_char(); let (c, span) = last_char();
let mut err = handler.struct_span_err(span, "non-ASCII character in byte constant"); let mut err = handler.struct_span_err(span, "non-ASCII character in byte constant");
let postfix = if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 { let postfix = if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 {
@ -271,7 +271,7 @@ pub(crate) fn emit_unescape_error(
err.emit(); err.emit();
} }
EscapeError::NonAsciiCharInByteString => { EscapeError::NonAsciiCharInByteString => {
assert!(mode.is_bytes()); assert!(mode.is_byte());
let (c, span) = last_char(); let (c, span) = last_char();
let postfix = if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 { let postfix = if unicode_width::UnicodeWidthChar::width(c).unwrap_or(1) == 0 {
format!(" but is {:?}", c) format!(" but is {:?}", c)