File tree 2 files changed +25
-2
lines changed
2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -36,13 +36,13 @@ impl AsInner<Wtf8> for Buf {
36
36
37
37
impl fmt:: Debug for Buf {
38
38
fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
39
- fmt:: Debug :: fmt ( self . as_slice ( ) , formatter)
39
+ fmt:: Debug :: fmt ( & self . inner , formatter)
40
40
}
41
41
}
42
42
43
43
impl fmt:: Display for Buf {
44
44
fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
45
- fmt:: Display :: fmt ( self . as_slice ( ) , formatter)
45
+ fmt:: Display :: fmt ( & self . inner , formatter)
46
46
}
47
47
}
48
48
Original file line number Diff line number Diff line change @@ -166,6 +166,17 @@ impl fmt::Debug for Wtf8Buf {
166
166
}
167
167
}
168
168
169
+ /// Formats the string with unpaired surrogates substituted with the replacement
170
+ /// character, U+FFFD.
171
+ impl fmt:: Display for Wtf8Buf {
172
+ fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
173
+ if let Some ( s) = self . as_known_utf8 ( ) {
174
+ return fmt:: Display :: fmt ( s, formatter) ;
175
+ }
176
+ fmt:: Display :: fmt ( & * * self , formatter)
177
+ }
178
+ }
179
+
169
180
impl Wtf8Buf {
170
181
/// Creates a new, empty WTF-8 string.
171
182
#[ inline]
@@ -258,6 +269,18 @@ impl Wtf8Buf {
258
269
unsafe { Wtf8 :: from_mut_bytes_unchecked ( & mut self . bytes ) }
259
270
}
260
271
272
+ /// Converts the string to UTF-8 without validation, if it was created from
273
+ /// valid UTF-8.
274
+ #[ inline]
275
+ fn as_known_utf8 ( & self ) -> Option < & str > {
276
+ if self . is_known_utf8 {
277
+ // SAFETY: The buffer is known to be valid UTF-8.
278
+ Some ( unsafe { str:: from_utf8_unchecked ( self . as_bytes ( ) ) } )
279
+ } else {
280
+ None
281
+ }
282
+ }
283
+
261
284
/// Reserves capacity for at least `additional` more bytes to be inserted
262
285
/// in the given `Wtf8Buf`.
263
286
/// The collection may reserve more space to avoid frequent reallocations.
You can’t perform that action at this time.
0 commit comments