File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 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 {
3636
3737impl fmt:: Debug for Buf {
3838 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
39- fmt:: Debug :: fmt ( self . as_slice ( ) , formatter)
39+ fmt:: Debug :: fmt ( & self . inner , formatter)
4040 }
4141}
4242
4343impl fmt:: Display for Buf {
4444 fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
45- fmt:: Display :: fmt ( self . as_slice ( ) , formatter)
45+ fmt:: Display :: fmt ( & self . inner , formatter)
4646 }
4747}
4848
Original file line number Diff line number Diff line change @@ -169,6 +169,17 @@ impl fmt::Debug for Wtf8Buf {
169169 }
170170}
171171
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+
172183impl Wtf8Buf {
173184 /// Creates a new, empty WTF-8 string.
174185 #[ inline]
@@ -262,6 +273,18 @@ impl Wtf8Buf {
262273 unsafe { Wtf8 :: from_mut_bytes_unchecked ( & mut self . bytes ) }
263274 }
264275
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+
265288 /// Reserves capacity for at least `additional` more bytes to be inserted
266289 /// in the given `Wtf8Buf`.
267290 /// The collection may reserve more space to avoid frequent reallocations.
You can’t perform that action at this time.
0 commit comments