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

Don't split characters into bytes for processing #109

Closed
wants to merge 2 commits into from
Closed
Changes from all 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: 17 additions & 21 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,19 +904,17 @@ fn process_text<'input>(
text_buffer.push_raw(c);
is_as_is = false;
} else {
text_buffer.push_from_text(c, stream.at_end());
text_buffer.push_from_text(c as char, stream.at_end());
}
}
NextChunk::Char(c) => {
for b in CharToBytes::new(c) {
if loop_detector.depth > 0 {
text_buffer.push_from_text(b, stream.at_end());
} else {
// Characters not from entity should be added as is.
// Not sure why... At least `lxml` produces the same result.
text_buffer.push_raw(b);
is_as_is = true;
}
if loop_detector.depth > 0 {
text_buffer.push_from_text(c, stream.at_end());
} else {
// Characters not from entity should be added as is.
// Not sure why... At least `lxml` produces the same result.
text_buffer.push(c);
is_as_is = true;
}
}
NextChunk::Text(fragment) => {
Expand Down Expand Up @@ -978,9 +976,7 @@ fn process_cdata<'input>(
let mut text_buffer = String::with_capacity(32);
let count = text.as_str().chars().count();
for (i, c) in text.as_str().chars().enumerate() {
for b in CharToBytes::new(c) {
text_buffer.push_from_text(b, i + 1 == count);
}
text_buffer.push_from_text(c, i + 1 == count);
}

if !text_buffer.is_empty() {
Expand Down Expand Up @@ -1253,7 +1249,7 @@ impl Iterator for CharToBytes {
trait StringExt {
fn push_raw(&mut self, c: u8);
fn push_from_attr(&mut self, current: u8, next: Option<u8>);
fn push_from_text(&mut self, c: u8, at_end: bool);
fn push_from_text(&mut self, c: char, at_end: bool);
}

impl StringExt for String {
Expand All @@ -1280,20 +1276,20 @@ impl StringExt for String {
// Translate \r\n and any \r that is not followed by \n into a single \n character.
//
// https://www.w3.org/TR/xml/#sec-line-ends
fn push_from_text(&mut self, c: u8, at_end: bool) {
if self.as_bytes().last() == Some(&b'\r') {
fn push_from_text(&mut self, c: char, at_end: bool) {
if self.chars().last() == Some('\r') {
self.pop();
self.push('\n');

if at_end && c == b'\r' {
if at_end && c == '\r' {
self.push('\n');
} else if c != b'\n' {
self.push(c as char);
} else if c != '\n' {
self.push(c);
}
} else if at_end && c == b'\r' {
} else if at_end && c == '\r' {
self.push('\n');
} else {
self.push(c as char);
self.push(c);
}
}
}