File tree 2 files changed +26
-2
lines changed
2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -41,13 +41,13 @@ impl AsInner<Wtf8> for Buf {
41
41
42
42
impl fmt:: Debug for Buf {
43
43
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
44
- fmt:: Debug :: fmt ( self . as_slice ( ) , f)
44
+ fmt:: Debug :: fmt ( & self . inner , f)
45
45
}
46
46
}
47
47
48
48
impl fmt:: Display for Buf {
49
49
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
50
- fmt:: Display :: fmt ( self . as_slice ( ) , f)
50
+ fmt:: Display :: fmt ( & self . inner , f)
51
51
}
52
52
}
53
53
Original file line number Diff line number Diff line change @@ -169,6 +169,18 @@ 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
+ fmt:: Display :: fmt ( s, formatter)
178
+ } else {
179
+ fmt:: Display :: fmt ( & * * self , formatter)
180
+ }
181
+ }
182
+ }
183
+
172
184
impl Wtf8Buf {
173
185
/// Creates a new, empty WTF-8 string.
174
186
#[ inline]
@@ -262,6 +274,18 @@ impl Wtf8Buf {
262
274
unsafe { Wtf8 :: from_mut_bytes_unchecked ( & mut self . bytes ) }
263
275
}
264
276
277
+ /// Converts the string to UTF-8 without validation, if it was created from
278
+ /// valid UTF-8.
279
+ #[ inline]
280
+ fn as_known_utf8 ( & self ) -> Option < & str > {
281
+ if self . is_known_utf8 {
282
+ // SAFETY: The buffer is known to be valid UTF-8.
283
+ Some ( unsafe { str:: from_utf8_unchecked ( self . as_bytes ( ) ) } )
284
+ } else {
285
+ None
286
+ }
287
+ }
288
+
265
289
/// Reserves capacity for at least `additional` more bytes to be inserted
266
290
/// in the given `Wtf8Buf`.
267
291
/// The collection may reserve more space to avoid frequent reallocations.
You can’t perform that action at this time.
0 commit comments