mirror of
https://gitee.com/openharmony/third_party_rust_nom
synced 2025-02-17 06:07:59 +00:00
Fix escaped not returning err on failure
This commit is contained in:
parent
d08367129f
commit
fa5b79b613
@ -355,7 +355,7 @@ where
|
||||
}
|
||||
|
||||
/// Returns an input slice containing the first N input elements (Input[..N])
|
||||
///
|
||||
///
|
||||
/// It will return `Err(Err::Error((_, ErrorKind::Eof)))` if the input is shorter than the argument
|
||||
///
|
||||
/// # Example
|
||||
@ -483,6 +483,9 @@ where
|
||||
}
|
||||
} else {
|
||||
let index = input.offset(&i);
|
||||
if index == 0 {
|
||||
return Err(Err::Error(Error::from_error_kind(input, ErrorKind::Escaped)));
|
||||
}
|
||||
return Ok(input.take_split(index));
|
||||
}
|
||||
}
|
||||
@ -597,6 +600,9 @@ where
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if index == 0 {
|
||||
return Err(Err::Error(Error::from_error_kind(remainder, ErrorKind::EscapedTransform)));
|
||||
}
|
||||
return Ok((remainder, res));
|
||||
}
|
||||
}
|
||||
|
18
tests/escaped.rs
Normal file
18
tests/escaped.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use nom::{Err, error::ErrorKind, IResult};
|
||||
use nom::character::complete::digit1;
|
||||
use nom::bytes::complete::{escaped, escaped_transform, tag};
|
||||
use nom::character::complete::one_of;
|
||||
|
||||
fn esc(s: &str) -> IResult<&str, &str> {
|
||||
escaped(digit1, '\\', one_of("\"n\\"))(s)
|
||||
}
|
||||
|
||||
fn esc_trans(s: &str) -> IResult<&str, String> {
|
||||
escaped_transform(digit1, '\\', |i: &str| tag("n")(i))(s)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_escaped() {
|
||||
assert_eq!(esc("abcd"), Err(Err::Error(("abcd", ErrorKind::Escaped))));
|
||||
assert_eq!(esc_trans("abcd"), Err(Err::Error(("abcd", ErrorKind::EscapedTransform))));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user