@@ -2097,31 +2097,40 @@ where
20972097 W : ?Sized + io:: Write ,
20982098 F : ?Sized + Formatter ,
20992099{
2100- let bytes = value. as_bytes ( ) ;
2100+ let mut bytes = value. as_bytes ( ) ;
21012101
2102- let mut start = 0 ;
2102+ let mut i = 0 ;
2103+ while i < bytes. len ( ) {
2104+ let ( string_run, rest) = bytes. split_at ( i) ;
2105+ let ( & byte, rest) = rest. split_first ( ) . unwrap ( ) ;
21032106
2104- for ( i, & byte) in bytes. iter ( ) . enumerate ( ) {
21052107 let escape = ESCAPE [ byte as usize ] ;
2108+
2109+ i += 1 ;
21062110 if escape == 0 {
21072111 continue ;
21082112 }
21092113
2110- if start < i {
2111- tri ! ( formatter. write_string_fragment( writer, & value[ start..i] ) ) ;
2114+ bytes = rest;
2115+ i = 0 ;
2116+
2117+ // safety: string_run is a valid utf8 string, since we only split on ascii sequences
2118+ let string_run = unsafe { core:: str:: from_utf8_unchecked ( string_run) } ;
2119+ if !string_run. is_empty ( ) {
2120+ tri ! ( formatter. write_string_fragment( writer, string_run) ) ;
21122121 }
21132122
21142123 let char_escape = CharEscape :: from_escape_table ( escape, byte) ;
21152124 tri ! ( formatter. write_char_escape( writer, char_escape) ) ;
2116-
2117- start = i + 1 ;
21182125 }
21192126
2120- if start == bytes. len ( ) {
2127+ // safety: bytes is a valid utf8 string, since we only split on ascii sequences
2128+ let string_run = unsafe { core:: str:: from_utf8_unchecked ( bytes) } ;
2129+ if string_run. is_empty ( ) {
21212130 return Ok ( ( ) ) ;
21222131 }
21232132
2124- formatter. write_string_fragment ( writer, & value [ start.. ] )
2133+ formatter. write_string_fragment ( writer, string_run )
21252134}
21262135
21272136const BB : u8 = b'b' ; // \x08
0 commit comments