Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normalize_line_end for unescape and test #807

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/escape.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Manage xml character escapes

use memchr::memchr2_iter;
use memchr::{memchr2_iter, memchr_iter};
use std::borrow::Cow;
use std::num::ParseIntError;
use std::ops::Range;
Expand Down Expand Up @@ -288,13 +288,43 @@ where
}
}

if let Some(mut unescaped) = unescaped {
let res = if let Some(mut unescaped) = unescaped {
if let Some(raw) = raw.get(last_end..) {
unescaped.push_str(raw);
}
Ok(Cow::Owned(unescaped))
Cow::Owned(unescaped)
} else {
Ok(Cow::Borrowed(raw))
Cow::Borrowed(raw)
};
Ok(normalize_line_end(res))
}

/// Normalize the line end, replace \r or \r\n with \n.
#[inline]
fn normalize_line_end<'a>(input: impl Into<Cow<'a, str>>) -> Cow<'a, str> {
yorkz1994 marked this conversation as resolved.
Show resolved Hide resolved
let input = input.into();
let bytes = input.as_bytes();
let mut normalized = None;
let mut start = 0;
let iter = memchr_iter(b'\r', bytes);
for i in iter {
if normalized.is_none() {
normalized = Some(String::with_capacity(input.len()))
}
let normalized = normalized.as_mut().expect("initialized");
normalized.push_str(&input[start..i]);
normalized.push('\n');
start = i + 1;
if matches!(bytes.get(start), Some(&c) if c == b'\n') {
yorkz1994 marked this conversation as resolved.
Show resolved Hide resolved
// \n right after \r, \r\n case, skip \n because we have already replaced \r with \n
start += 1;
}
}
if let Some(mut normalized) = normalized {
normalized.push_str(&input[start..]);
Cow::Owned(normalized)
} else {
input
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ fn unescape() {
);
}

#[test]
fn unescape_line_end() {
let unchanged = escape::unescape("test\n");
// assert_eq does not check that Cow is borrowed, but we explicitly use Cow
// because it influences diff
// TODO: use assert_matches! when stabilized and other features will bump MSRV
assert_eq!(unchanged, Ok(Cow::Borrowed("test\n")));
assert!(matches!(unchanged, Ok(Cow::Borrowed(_))));

assert_eq!(
escape::unescape("&lt;&amp;test&apos;\r&quot;\r\n&gt;\r\n\r"),
Ok("<&test'\n\"\n>\n\n".into())
);
assert_eq!(escape::unescape("&#x30;\r\r\n"), Ok("0\n\n".into()));
assert_eq!(escape::unescape("\r&#48;\n\r\r"), Ok("\n0\n\n\n".into()));
assert_eq!(
escape::unescape("\r\n&foo;\n"),
Err(EscapeError::UnrecognizedEntity(3..6, "foo".into()))
);
}

/// XML allows any number of leading zeroes. That is not explicitly mentioned
/// in the specification, but enforced by the conformance test suite
/// (https://www.w3.org/XML/Test/)
Expand Down
Loading