1
Fork 0

Simplify unescape_{char,byte}.

The `usize` isn't needed in the error case.
This commit is contained in:
Nicholas Nethercote 2022-11-08 15:59:19 +11:00
parent 43d21b535f
commit d6c97a32b4
2 changed files with 9 additions and 17 deletions

View file

@ -94,19 +94,15 @@ where
} }
/// Takes a contents of a char literal (without quotes), and returns an /// Takes a contents of a char literal (without quotes), and returns an
/// 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, EscapeError> {
let mut chars = src.chars(); unescape_char_or_byte(&mut src.chars(), false)
unescape_char_or_byte(&mut chars, false).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, EscapeError> {
let mut chars = src.chars(); unescape_char_or_byte(&mut src.chars(), true).map(byte_from_char)
unescape_char_or_byte(&mut chars, true)
.map(byte_from_char)
.map_err(|err| (src.len() - chars.as_str().len(), err))
} }
/// What kind of literal do we parse. /// What kind of literal do we parse.

View file

@ -3,8 +3,7 @@ use super::*;
#[test] #[test]
fn test_unescape_char_bad() { fn test_unescape_char_bad() {
fn check(literal_text: &str, expected_error: EscapeError) { fn check(literal_text: &str, expected_error: EscapeError) {
let actual_result = unescape_char(literal_text).map_err(|(_offset, err)| err); assert_eq!(unescape_char(literal_text), Err(expected_error));
assert_eq!(actual_result, Err(expected_error));
} }
check("", EscapeError::ZeroChars); check("", EscapeError::ZeroChars);
@ -68,8 +67,7 @@ fn test_unescape_char_bad() {
#[test] #[test]
fn test_unescape_char_good() { fn test_unescape_char_good() {
fn check(literal_text: &str, expected_char: char) { fn check(literal_text: &str, expected_char: char) {
let actual_result = unescape_char(literal_text); assert_eq!(unescape_char(literal_text), Ok(expected_char));
assert_eq!(actual_result, Ok(expected_char));
} }
check("a", 'a'); check("a", 'a');
@ -149,8 +147,7 @@ fn test_unescape_str_good() {
#[test] #[test]
fn test_unescape_byte_bad() { fn test_unescape_byte_bad() {
fn check(literal_text: &str, expected_error: EscapeError) { fn check(literal_text: &str, expected_error: EscapeError) {
let actual_result = unescape_byte(literal_text).map_err(|(_offset, err)| err); assert_eq!(unescape_byte(literal_text), Err(expected_error));
assert_eq!(actual_result, Err(expected_error));
} }
check("", EscapeError::ZeroChars); check("", EscapeError::ZeroChars);
@ -219,8 +216,7 @@ fn test_unescape_byte_bad() {
#[test] #[test]
fn test_unescape_byte_good() { fn test_unescape_byte_good() {
fn check(literal_text: &str, expected_byte: u8) { fn check(literal_text: &str, expected_byte: u8) {
let actual_result = unescape_byte(literal_text); assert_eq!(unescape_byte(literal_text), Ok(expected_byte));
assert_eq!(actual_result, Ok(expected_byte));
} }
check("a", b'a'); check("a", b'a');