@@ -6,6 +6,8 @@ use std::str::Utf8Error;
66#[ cfg( feature = "encoding" ) ]
77use encoding_rs:: { DecoderResult , Encoding , UTF_16BE , UTF_16LE , UTF_8 } ;
88
9+ use crate :: escape:: normalize_eols;
10+
911/// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-8.
1012/// See <https://unicode.org/faq/utf_bom.html#bom1>
1113pub ( crate ) const UTF8_BOM : & [ u8 ] = & [ 0xEF , 0xBB , 0xBF ] ;
@@ -145,9 +147,20 @@ impl Decoder {
145147 bytes : & Cow < ' b , [ u8 ] > ,
146148 ) -> Result < Cow < ' b , str > , EncodingError > {
147149 match bytes {
148- Cow :: Borrowed ( bytes) => self . decode ( bytes) ,
150+ Cow :: Borrowed ( bytes) => {
151+ let text = self . decode ( bytes) ?;
152+ match normalize_eols ( & text) {
153+ // If text borrowed after normalization that means that it's not changed
154+ Cow :: Borrowed ( _) => Ok ( text) ,
155+ Cow :: Owned ( s) => Ok ( Cow :: Owned ( s) ) ,
156+ }
157+ }
149158 // Convert to owned, because otherwise Cow will be bound with wrong lifetime
150- Cow :: Owned ( bytes) => Ok ( self . decode ( bytes) ?. into_owned ( ) . into ( ) ) ,
159+ Cow :: Owned ( bytes) => {
160+ let text = self . decode ( bytes) ?;
161+ let text = normalize_eols ( & text) ;
162+ Ok ( text. into_owned ( ) . into ( ) )
163+ }
151164 }
152165 }
153166}
0 commit comments