Rename some unescaping functions.

`unescape_raw_str_or_raw_byte_str` only does checking, no unescaping.
And it also now handles C string literals.

`unescape_raw_str` is used for all the non-raw strings.
This commit is contained in:
Nicholas Nethercote 2023-12-13 12:50:39 +11:00
parent a50efe2653
commit b900eb7317

View File

@ -92,8 +92,8 @@ pub fn unescape_literal<F>(src: &str, mode: Mode, callback: &mut F)
let res = unescape_char_or_byte(&mut chars, mode); let res = unescape_char_or_byte(&mut chars, mode);
callback(0..(src.len() - chars.as_str().len()), res); callback(0..(src.len() - chars.as_str().len()), res);
} }
Str | ByteStr => unescape_str_common(src, mode, callback), Str | ByteStr => unescape_non_raw_common(src, mode, callback),
RawStr | RawByteStr => unescape_raw_str_or_raw_byte_str(src, mode, callback), RawStr | RawByteStr => check_raw_common(src, mode, callback),
CStr | RawCStr => unreachable!(), CStr | RawCStr => unreachable!(),
} }
} }
@ -122,12 +122,10 @@ pub fn unescape_c_string<F>(src: &str, mode: Mode, callback: &mut F)
{ {
match mode { match mode {
CStr => { CStr => {
unescape_str_common(src, mode, callback); unescape_non_raw_common(src, mode, callback);
} }
RawCStr => { RawCStr => {
unescape_raw_str_or_raw_byte_str(src, mode, &mut |r, result| { check_raw_common(src, mode, &mut |r, result| callback(r, result.map(CStrUnit::Char)));
callback(r, result.map(CStrUnit::Char))
});
} }
Char | Byte | Str | RawStr | ByteStr | RawByteStr => unreachable!(), Char | Byte | Str | RawStr | ByteStr | RawByteStr => unreachable!(),
} }
@ -325,7 +323,7 @@ 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_common<F, T: From<u8> + From<char>>(src: &str, mode: Mode, callback: &mut F) fn unescape_non_raw_common<F, T: From<u8> + From<char>>(src: &str, mode: Mode, callback: &mut F)
where where
F: FnMut(Range<usize>, Result<T, EscapeError>), F: FnMut(Range<usize>, Result<T, EscapeError>),
{ {
@ -392,7 +390,7 @@ fn skip_ascii_whitespace<F>(chars: &mut Chars<'_>, start: usize, callback: &mut
/// 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 produce errors on bare CR. /// only produce errors on bare CR.
fn unescape_raw_str_or_raw_byte_str<F>(src: &str, mode: Mode, callback: &mut F) fn check_raw_common<F>(src: &str, mode: Mode, callback: &mut F)
where where
F: FnMut(Range<usize>, Result<char, EscapeError>), F: FnMut(Range<usize>, Result<char, EscapeError>),
{ {
@ -400,7 +398,7 @@ fn unescape_raw_str_or_raw_byte_str<F>(src: &str, mode: Mode, callback: &mut F)
let chars_should_be_ascii = mode.chars_should_be_ascii(); // get this outside the loop let chars_should_be_ascii = mode.chars_should_be_ascii(); // 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_str_common` for consistency, even though this function // `unescape_non_raw_common` for consistency, even though this function
// doesn't have to worry about skipping any chars. // doesn't have to worry about skipping any chars.
while let Some(c) = chars.next() { while let Some(c) = chars.next() {
let start = src.len() - chars.as_str().len() - c.len_utf8(); let start = src.len() - chars.as_str().len() - c.len_utf8();