File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Expand file tree Collapse file tree 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 {
4141
4242impl fmt:: Debug for Buf {
4343 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
44- fmt:: Debug :: fmt ( self . as_slice ( ) , f)
44+ fmt:: Debug :: fmt ( & self . inner , f)
4545 }
4646}
4747
4848impl fmt:: Display for Buf {
4949 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
50- fmt:: Display :: fmt ( self . as_slice ( ) , f)
50+ fmt:: Display :: fmt ( & self . inner , f)
5151 }
5252}
5353
Original file line number Diff line number Diff line change @@ -169,6 +169,18 @@ 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+ fmt:: Display :: fmt ( s, formatter)
178+ } else {
179+ fmt:: Display :: fmt ( & * * self , formatter)
180+ }
181+ }
182+ }
183+
172184impl Wtf8Buf {
173185 /// Creates a new, empty WTF-8 string.
174186 #[ inline]
@@ -262,6 +274,18 @@ impl Wtf8Buf {
262274 unsafe { Wtf8 :: from_mut_bytes_unchecked ( & mut self . bytes ) }
263275 }
264276
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+
265289 /// Reserves capacity for at least `additional` more bytes to be inserted
266290 /// in the given `Wtf8Buf`.
267291 /// The collection may reserve more space to avoid frequent reallocations.
You can’t perform that action at this time.
0 commit comments