@@ -2097,31 +2097,40 @@ where
2097
2097
W : ?Sized + io:: Write ,
2098
2098
F : ?Sized + Formatter ,
2099
2099
{
2100
- let bytes = value. as_bytes ( ) ;
2100
+ let mut bytes = value. as_bytes ( ) ;
2101
2101
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 ( ) ;
2103
2106
2104
- for ( i, & byte) in bytes. iter ( ) . enumerate ( ) {
2105
2107
let escape = ESCAPE [ byte as usize ] ;
2108
+
2109
+ i += 1 ;
2106
2110
if escape == 0 {
2107
2111
continue ;
2108
2112
}
2109
2113
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) ) ;
2112
2121
}
2113
2122
2114
2123
let char_escape = CharEscape :: from_escape_table ( escape, byte) ;
2115
2124
tri ! ( formatter. write_char_escape( writer, char_escape) ) ;
2116
-
2117
- start = i + 1 ;
2118
2125
}
2119
2126
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 ( ) {
2121
2130
return Ok ( ( ) ) ;
2122
2131
}
2123
2132
2124
- formatter. write_string_fragment ( writer, & value [ start.. ] )
2133
+ formatter. write_string_fragment ( writer, string_run )
2125
2134
}
2126
2135
2127
2136
const BB : u8 = b'b' ; // \x08
0 commit comments