1- use std:: str:: from_utf8;
2-
31#[ cfg( feature = "encoding" ) ]
42use 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