Rename and invert sense of Mode
predicates.
I find it easier if they describe what's allowed, rather than what's forbidden. Also, consistent naming makes them easier to understand.
This commit is contained in:
parent
a1c07214f0
commit
5e5aa6d556
@ -195,29 +195,29 @@ pub fn in_double_quotes(self) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Non-byte literals should have `\xXX` escapes that are within the ASCII range.
|
/// Are `\x80`..`\xff` allowed?
|
||||||
fn ascii_escapes_should_be_ascii(self) -> bool {
|
fn allow_high_bytes(self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Char | Str => true,
|
Char | Str => false,
|
||||||
Byte | ByteStr | CStr => false,
|
Byte | ByteStr | CStr => true,
|
||||||
RawStr | RawByteStr | RawCStr => unreachable!(),
|
RawStr | RawByteStr | RawCStr => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether characters within the literal must be within the ASCII range.
|
/// Are unicode (non-ASCII) chars allowed?
|
||||||
#[inline]
|
#[inline]
|
||||||
fn chars_should_be_ascii(self) -> bool {
|
fn allow_unicode_chars(self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Byte | ByteStr | RawByteStr => true,
|
Byte | ByteStr | RawByteStr => false,
|
||||||
Char | Str | RawStr | CStr | RawCStr => false,
|
Char | Str | RawStr | CStr | RawCStr => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Byte literals do not allow unicode escape.
|
/// Are unicode escapes (`\u`) allowed?
|
||||||
fn is_unicode_escape_disallowed(self) -> bool {
|
fn allow_unicode_escapes(self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Byte | ByteStr => true,
|
Byte | ByteStr => false,
|
||||||
Char | Str | CStr => false,
|
Char | Str | CStr => true,
|
||||||
RawByteStr | RawStr | RawCStr => unreachable!(),
|
RawByteStr | RawStr | RawCStr => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -255,25 +255,21 @@ fn scan_escape<T: From<char> + From<u8>>(
|
|||||||
|
|
||||||
let value = (hi * 16 + lo) as u8;
|
let value = (hi * 16 + lo) as u8;
|
||||||
|
|
||||||
return if mode.ascii_escapes_should_be_ascii() && !value.is_ascii() {
|
return if !mode.allow_high_bytes() && !value.is_ascii() {
|
||||||
Err(EscapeError::OutOfRangeHexEscape)
|
Err(EscapeError::OutOfRangeHexEscape)
|
||||||
} else {
|
} else {
|
||||||
// This may be a high byte, but that will only happen if `T` is
|
// This may be a high byte, but that will only happen if `T` is
|
||||||
// `MixedUnit`, because of the `ascii_escapes_should_be_ascii`
|
// `MixedUnit`, because of the `allow_high_bytes` check above.
|
||||||
// check above.
|
|
||||||
Ok(T::from(value as u8))
|
Ok(T::from(value as u8))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
'u' => return scan_unicode(chars, mode.is_unicode_escape_disallowed()).map(T::from),
|
'u' => return scan_unicode(chars, mode.allow_unicode_escapes()).map(T::from),
|
||||||
_ => return Err(EscapeError::InvalidEscape),
|
_ => return Err(EscapeError::InvalidEscape),
|
||||||
};
|
};
|
||||||
Ok(T::from(res))
|
Ok(T::from(res))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_unicode(
|
fn scan_unicode(chars: &mut Chars<'_>, allow_unicode_escapes: bool) -> Result<char, EscapeError> {
|
||||||
chars: &mut Chars<'_>,
|
|
||||||
is_unicode_escape_disallowed: bool,
|
|
||||||
) -> Result<char, EscapeError> {
|
|
||||||
// We've parsed '\u', now we have to parse '{..}'.
|
// We've parsed '\u', now we have to parse '{..}'.
|
||||||
|
|
||||||
if chars.next() != Some('{') {
|
if chars.next() != Some('{') {
|
||||||
@ -301,7 +297,7 @@ fn scan_unicode(
|
|||||||
|
|
||||||
// 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 is_unicode_escape_disallowed {
|
if !allow_unicode_escapes {
|
||||||
return Err(EscapeError::UnicodeEscapeInByte);
|
return Err(EscapeError::UnicodeEscapeInByte);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,12 +323,8 @@ fn scan_unicode(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn ascii_check(c: char, chars_should_be_ascii: bool) -> Result<char, EscapeError> {
|
fn ascii_check(c: char, allow_unicode_chars: bool) -> Result<char, EscapeError> {
|
||||||
if chars_should_be_ascii && !c.is_ascii() {
|
if allow_unicode_chars || c.is_ascii() { Ok(c) } else { Err(EscapeError::NonAsciiCharInByte) }
|
||||||
Err(EscapeError::NonAsciiCharInByte)
|
|
||||||
} else {
|
|
||||||
Ok(c)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
|
fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
|
||||||
@ -341,7 +333,7 @@ fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, Esca
|
|||||||
'\\' => scan_escape(chars, mode),
|
'\\' => scan_escape(chars, mode),
|
||||||
'\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar),
|
'\n' | '\t' | '\'' => Err(EscapeError::EscapeOnlyChar),
|
||||||
'\r' => Err(EscapeError::BareCarriageReturn),
|
'\r' => Err(EscapeError::BareCarriageReturn),
|
||||||
_ => ascii_check(c, mode.chars_should_be_ascii()),
|
_ => ascii_check(c, mode.allow_unicode_chars()),
|
||||||
}?;
|
}?;
|
||||||
if chars.next().is_some() {
|
if chars.next().is_some() {
|
||||||
return Err(EscapeError::MoreThanOneChar);
|
return Err(EscapeError::MoreThanOneChar);
|
||||||
@ -356,7 +348,7 @@ fn unescape_non_raw_common<F, T: From<char> + From<u8>>(src: &str, mode: Mode, c
|
|||||||
F: FnMut(Range<usize>, Result<T, EscapeError>),
|
F: FnMut(Range<usize>, Result<T, EscapeError>),
|
||||||
{
|
{
|
||||||
let mut chars = src.chars();
|
let mut chars = src.chars();
|
||||||
let chars_should_be_ascii = mode.chars_should_be_ascii(); // get this outside the loop
|
let allow_unicode_chars = mode.allow_unicode_chars(); // get this outside the loop
|
||||||
|
|
||||||
// The `start` and `end` computation here is complicated because
|
// The `start` and `end` computation here is complicated because
|
||||||
// `skip_ascii_whitespace` makes us to skip over chars without counting
|
// `skip_ascii_whitespace` makes us to skip over chars without counting
|
||||||
@ -381,7 +373,7 @@ fn unescape_non_raw_common<F, T: From<char> + From<u8>>(src: &str, mode: Mode, c
|
|||||||
}
|
}
|
||||||
'"' => Err(EscapeError::EscapeOnlyChar),
|
'"' => Err(EscapeError::EscapeOnlyChar),
|
||||||
'\r' => Err(EscapeError::BareCarriageReturn),
|
'\r' => Err(EscapeError::BareCarriageReturn),
|
||||||
_ => ascii_check(c, chars_should_be_ascii).map(T::from),
|
_ => ascii_check(c, allow_unicode_chars).map(T::from),
|
||||||
};
|
};
|
||||||
let end = src.len() - chars.as_str().len();
|
let end = src.len() - chars.as_str().len();
|
||||||
callback(start..end, res);
|
callback(start..end, res);
|
||||||
@ -423,7 +415,7 @@ fn check_raw_common<F>(src: &str, mode: Mode, callback: &mut F)
|
|||||||
F: FnMut(Range<usize>, Result<char, EscapeError>),
|
F: FnMut(Range<usize>, Result<char, EscapeError>),
|
||||||
{
|
{
|
||||||
let mut chars = src.chars();
|
let mut chars = src.chars();
|
||||||
let chars_should_be_ascii = mode.chars_should_be_ascii(); // get this outside the loop
|
let allow_unicode_chars = mode.allow_unicode_chars(); // get this outside the loop
|
||||||
|
|
||||||
// The `start` and `end` computation here matches the one in
|
// The `start` and `end` computation here matches the one in
|
||||||
// `unescape_non_raw_common` for consistency, even though this function
|
// `unescape_non_raw_common` for consistency, even though this function
|
||||||
@ -432,7 +424,7 @@ fn check_raw_common<F>(src: &str, mode: Mode, callback: &mut F)
|
|||||||
let start = src.len() - chars.as_str().len() - c.len_utf8();
|
let start = src.len() - chars.as_str().len() - c.len_utf8();
|
||||||
let res = match c {
|
let res = match c {
|
||||||
'\r' => Err(EscapeError::BareCarriageReturnInRawString),
|
'\r' => Err(EscapeError::BareCarriageReturnInRawString),
|
||||||
_ => ascii_check(c, chars_should_be_ascii),
|
_ => ascii_check(c, allow_unicode_chars),
|
||||||
};
|
};
|
||||||
let end = src.len() - chars.as_str().len();
|
let end = src.len() - chars.as_str().len();
|
||||||
callback(start..end, res);
|
callback(start..end, res);
|
||||||
|
Loading…
Reference in New Issue
Block a user