-
Notifications
You must be signed in to change notification settings - Fork 267
Add normalize_line_end for unescape and test #807
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
4f5ce0d
45a63c5
1c2ddc5
139b2cb
6962ae3
11320ad
0bb282e
f970370
d11c756
d3ee1ad
cbf200f
e4f83a5
cf99a45
628dce7
44df9bf
4e1eefc
ecfeefc
d880edc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
@@ -266,7 +266,7 @@ where | |
| unescaped = Some(String::with_capacity(raw.len())); | ||
| } | ||
| let unescaped = unescaped.as_mut().expect("initialized"); | ||
| unescaped.push_str(&raw[last_end..start]); | ||
| unescaped.push_str(&normalize_line_end(&raw[last_end..start])); | ||
|
||
|
|
||
| // search for character correctness | ||
| let pat = &raw[start + 1..end]; | ||
|
|
@@ -290,14 +290,42 @@ where | |
|
|
||
| if let Some(mut unescaped) = unescaped { | ||
| if let Some(raw) = raw.get(last_end..) { | ||
| unescaped.push_str(raw); | ||
| unescaped.push_str(&normalize_line_end(raw)); | ||
| } | ||
| Ok(Cow::Owned(unescaped)) | ||
| } else { | ||
| Ok(Cow::Borrowed(raw)) | ||
| } | ||
| } | ||
|
|
||
| /// Normalize the line end, replace \r or \r\n with \n. | ||
| #[inline] | ||
| fn normalize_line_end(input: &str) -> Cow<str> { | ||
| 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.
Outdated
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.into() | ||
| } | ||
| } | ||
|
|
||
| /// Resolves predefined XML entities or all HTML5 entities depending on the feature | ||
| /// [`escape-html`](https://docs.rs/quick-xml/latest/quick_xml/#escape-html). | ||
| /// | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.