Use unescape_unicode
for raw C string literals.
They can't contain `\x` escapes, which means they can't contain high bytes, which means we can used `unescape_unicode` instead of `unescape_mixed` to unescape them. This avoids unnecessary used of `MixedUnit`.
This commit is contained in:
parent
86f371ed59
commit
6be2e5623c
2 changed files with 15 additions and 20 deletions
|
@ -97,7 +97,13 @@ where
|
||||||
}
|
}
|
||||||
Str | ByteStr => unescape_non_raw_common(src, mode, callback),
|
Str | ByteStr => unescape_non_raw_common(src, mode, callback),
|
||||||
RawStr | RawByteStr => check_raw_common(src, mode, callback),
|
RawStr | RawByteStr => check_raw_common(src, mode, callback),
|
||||||
CStr | RawCStr => unreachable!(),
|
RawCStr => check_raw_common(src, mode, &mut |r, mut result| {
|
||||||
|
if let Ok('\0') = result {
|
||||||
|
result = Err(EscapeError::NulInCStr);
|
||||||
|
}
|
||||||
|
callback(r, result)
|
||||||
|
}),
|
||||||
|
CStr => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,24 +147,13 @@ where
|
||||||
F: FnMut(Range<usize>, Result<MixedUnit, EscapeError>),
|
F: FnMut(Range<usize>, Result<MixedUnit, EscapeError>),
|
||||||
{
|
{
|
||||||
match mode {
|
match mode {
|
||||||
CStr => {
|
CStr => unescape_non_raw_common(src, mode, &mut |r, mut result| {
|
||||||
unescape_non_raw_common(src, mode, &mut |r, mut result| {
|
if let Ok(MixedUnit::Char('\0')) = result {
|
||||||
if let Ok(MixedUnit::Char('\0')) = result {
|
result = Err(EscapeError::NulInCStr);
|
||||||
result = Err(EscapeError::NulInCStr);
|
}
|
||||||
}
|
callback(r, result)
|
||||||
callback(r, result)
|
}),
|
||||||
});
|
Char | Byte | Str | RawStr | ByteStr | RawByteStr | RawCStr => unreachable!(),
|
||||||
}
|
|
||||||
RawCStr => {
|
|
||||||
check_raw_common(src, mode, &mut |r, mut result| {
|
|
||||||
if let Ok('\0') = result {
|
|
||||||
result = Err(EscapeError::NulInCStr);
|
|
||||||
}
|
|
||||||
// High bytes aren't possible in raw strings.
|
|
||||||
callback(r, result.map(MixedUnit::Char))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Char | Byte | Str | RawStr | ByteStr | RawByteStr => unreachable!(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -472,7 +472,7 @@ impl<'sess, 'src> StringReader<'sess, 'src> {
|
||||||
if let Some(n_hashes) = n_hashes {
|
if let Some(n_hashes) = n_hashes {
|
||||||
let n = u32::from(n_hashes);
|
let n = u32::from(n_hashes);
|
||||||
let kind = token::CStrRaw(n_hashes);
|
let kind = token::CStrRaw(n_hashes);
|
||||||
self.cook_mixed(kind, Mode::RawCStr, start, end, 3 + n, 1 + n) // cr##" "##
|
self.cook_unicode(kind, Mode::RawCStr, start, end, 3 + n, 1 + n) // cr##" "##
|
||||||
} else {
|
} else {
|
||||||
self.report_raw_str_error(start, 2);
|
self.report_raw_str_error(start, 2);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue