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 @@ -169,6 +169,17 @@ impl fmt::Debug for Wtf8Buf {
169
169
}
170
170
}
171
171
172
+ /// Formats the string with unpaired surrogates substituted with the replacement
173
+ /// character, U+FFFD.
174
+ impl fmt:: Display for Wtf8Buf {
175
+ fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
176
+ if let Some ( s) = self . as_known_utf8 ( ) {
177
+ return fmt:: Display :: fmt ( s, formatter) ;
178
+ }
179
+ fmt:: Display :: fmt ( & * * self , formatter)
180
+ }
181
+ }
182
+
172
183
impl Wtf8Buf {
173
184
/// Creates a new, empty WTF-8 string.
174
185
#[ inline]
@@ -262,6 +273,18 @@ impl Wtf8Buf {
262
273
unsafe { Wtf8 :: from_mut_bytes_unchecked ( & mut self . bytes ) }
263
274
}
264
275
276
+ /// Converts the string to UTF-8 without validation, if it was created from
277
+ /// valid UTF-8.
278
+ #[ inline]
279
+ fn as_known_utf8 ( & self ) -> Option < & str > {
280
+ if self . is_known_utf8 {
281
+ // SAFETY: The buffer is known to be valid UTF-8.
282
+ Some ( unsafe { str:: from_utf8_unchecked ( self . as_bytes ( ) ) } )
283
+ } else {
284
+ None
285
+ }
286
+ }
287
+
265
288
/// Reserves capacity for at least `additional` more bytes to be inserted
266
289
/// in the given `Wtf8Buf`.
267
290
/// The collection may reserve more space to avoid frequent reallocations.
You can’t perform that action at this time.
0 commit comments