Skip to content

Commit aa932fd

Browse files
committed
Allow � values
Do not return error on parsing preperly escaped zero values. Add simple test to check it.
1 parent 0c6eeb0 commit aa932fd

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

src/escapei.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use pretty_assertions::assert_eq;
1010
/// Error for XML escape / unescape.
1111
#[derive(Debug)]
1212
pub enum EscapeError {
13-
/// Entity with Null character
14-
EntityWithNull(Range<usize>),
1513
/// Unrecognized escape symbol
1614
UnrecognizedSymbol(Range<usize>, String),
1715
/// Cannot find `;` after `&`
@@ -31,11 +29,6 @@ pub enum EscapeError {
3129
impl std::fmt::Display for EscapeError {
3230
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3331
match self {
34-
EscapeError::EntityWithNull(e) => write!(
35-
f,
36-
"Error while escaping character at range {:?}: Null character entity not allowed",
37-
e
38-
),
3932
EscapeError::UnrecognizedSymbol(rge, res) => write!(
4033
f,
4134
"Error while escaping character at range {:?}: Unrecognized escape symbol: {:?}",
@@ -183,7 +176,7 @@ where
183176
let pat = &raw[start + 1..end];
184177
if pat.starts_with('#') {
185178
let entity = &pat[1..]; // starts after the #
186-
let codepoint = parse_number(entity, start..end)?;
179+
let codepoint = parse_number(entity)?;
187180
unescaped.push_str(codepoint.encode_utf8(&mut [0u8; 4]));
188181
} else if let Some(value) = named_entity(pat) {
189182
unescaped.push_str(value);
@@ -1690,15 +1683,12 @@ fn named_entity(name: &str) -> Option<&str> {
16901683
Some(s)
16911684
}
16921685

1693-
fn parse_number(bytes: &str, range: Range<usize>) -> Result<char, EscapeError> {
1686+
fn parse_number(bytes: &str) -> Result<char, EscapeError> {
16941687
let code = if bytes.starts_with('x') {
16951688
parse_hexadecimal(&bytes[1..])
16961689
} else {
16971690
parse_decimal(bytes)
16981691
}?;
1699-
if code == 0 {
1700-
return Err(EscapeError::EntityWithNull(range));
1701-
}
17021692
match std::char::from_u32(code) {
17031693
Some(c) => Ok(c),
17041694
None => Err(EscapeError::InvalidCodepoint(code)),
@@ -1745,6 +1735,8 @@ fn test_unescape() {
17451735
assert_eq!(unescape("&lt;test&gt;").unwrap(), "<test>");
17461736
assert_eq!(unescape("&#x30;").unwrap(), "0");
17471737
assert_eq!(unescape("&#48;").unwrap(), "0");
1738+
assert_eq!(unescape("&#0;").unwrap(), "\0");
1739+
assert_eq!(unescape("&#x0;").unwrap(), "\0");
17481740
assert!(unescape("&foo;").is_err());
17491741
}
17501742

0 commit comments

Comments
 (0)