Skip to content

Commit bc0dbd2

Browse files
committed
Use correct encoding when generate EndEventMismatch error
1 parent 395e56c commit bc0dbd2

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/reader/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ enum ParseState {
305305
/// State after seeing the `<` symbol. Depending on the next symbol all other
306306
/// events (except `StartText`) could be generated.
307307
///
308-
/// After generating ane event the reader moves to the `ClosedTag` state.
308+
/// After generating one event the reader moves to the `ClosedTag` state.
309309
OpenedTag,
310310
/// State in which reader searches the `<` symbol of a markup. All bytes before
311311
/// that symbol will be returned in the [`Event::Text`] event. After that

src/reader/parser.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::str::from_utf8;
2-
31
#[cfg(feature = "encoding")]
42
use encoding_rs::UTF_8;
53

@@ -84,7 +82,7 @@ impl Parser {
8482
}
8583

8684
let content = if self.trim_text_end {
87-
// Skip the ending '<
85+
// Skip the ending '<'
8886
let len = bytes
8987
.iter()
9088
.rposition(|&b| !is_whitespace(b))
@@ -143,9 +141,8 @@ impl Parser {
143141
}
144142
}
145143

146-
/// reads `BytesElement` starting with a `/`,
147-
/// if `self.check_end_names`, checks that element matches last opened element
148-
/// return `End` event
144+
/// Wraps content of `buf` into the [`Event::End`] event. Does the check that
145+
/// end name matches the last opened start name if `self.check_end_names` is set.
149146
pub fn read_end<'b>(&mut self, buf: &'b [u8]) -> Result<Event<'b>> {
150147
// XML standard permits whitespaces after the markup name in closing tags.
151148
// Let's strip them from the buffer before comparing tag names.
@@ -160,24 +157,26 @@ impl Parser {
160157
&buf[1..]
161158
};
162159
if self.check_end_names {
163-
let mismatch_err = |expected: &[u8], found: &[u8], offset: &mut usize| {
160+
let decoder = self.decoder();
161+
let mismatch_err = |expected: String, found: &[u8], offset: &mut usize| {
164162
*offset -= buf.len();
165163
Err(Error::EndEventMismatch {
166-
expected: from_utf8(expected).unwrap_or("").to_owned(),
167-
found: from_utf8(found).unwrap_or("").to_owned(),
164+
expected,
165+
found: decoder.decode(found).unwrap_or_default().into_owned(),
168166
})
169167
};
170168
match self.opened_starts.pop() {
171169
Some(start) => {
172170
let expected = &self.opened_buffer[start..];
173171
if name != expected {
172+
let expected = decoder.decode(expected).unwrap_or_default().into_owned();
174173
mismatch_err(expected, name, &mut self.offset)
175174
} else {
176175
self.opened_buffer.truncate(start);
177176
Ok(Event::End(BytesEnd::wrap(name.into())))
178177
}
179178
}
180-
None => mismatch_err(b"", &buf[1..], &mut self.offset),
179+
None => mismatch_err("".to_string(), &buf[1..], &mut self.offset),
181180
}
182181
} else {
183182
Ok(Event::End(BytesEnd::wrap(name.into())))

0 commit comments

Comments
 (0)