@@ -563,23 +563,40 @@ impl fmt::Debug for Wtf8 {
563
563
}
564
564
565
565
impl fmt:: Display for Wtf8 {
566
- fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
567
- let wtf8_bytes = & self . bytes ;
568
- let mut pos = 0 ;
569
- loop {
570
- match self . next_surrogate ( pos) {
571
- Some ( ( surrogate_pos, _) ) => {
572
- formatter. write_str ( unsafe {
573
- str:: from_utf8_unchecked ( & wtf8_bytes[ pos..surrogate_pos] )
574
- } ) ?;
575
- formatter. write_str ( UTF8_REPLACEMENT_CHARACTER ) ?;
576
- pos = surrogate_pos + 3 ;
577
- }
578
- None => {
579
- let s = unsafe { str:: from_utf8_unchecked ( & wtf8_bytes[ pos..] ) } ;
580
- if pos == 0 { return s. fmt ( formatter) } else { return formatter. write_str ( s) }
581
- }
582
- }
566
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
567
+ // Corresponds to `Formatter::pad`, but for `Wtf8` instead of `str`.
568
+
569
+ // Make sure there's a fast path up front.
570
+ if f. options ( ) . get_width ( ) . is_none ( ) && f. options ( ) . get_precision ( ) . is_none ( ) {
571
+ return self . write_lossy ( f) ;
572
+ }
573
+
574
+ // The `precision` field can be interpreted as a maximum width for the
575
+ // string being formatted.
576
+ let max_code_point_count = f. options ( ) . get_precision ( ) . unwrap_or ( usize:: MAX ) ;
577
+ let mut iter = self . code_points ( ) ;
578
+ let code_point_count = iter. by_ref ( ) . take ( max_code_point_count) . count ( ) ;
579
+
580
+ // If our string is longer than the maximum width, truncate it and
581
+ // handle other flags in terms of the truncated string.
582
+ let byte_len = self . len ( ) - iter. as_slice ( ) . len ( ) ;
583
+ // SAFETY: The index is derived from the offset of `.code_points()`,
584
+ // which is guaranteed to be in-bounds and between character boundaries.
585
+ let s = unsafe { Wtf8 :: from_bytes_unchecked ( self . bytes . get_unchecked ( ..byte_len) ) } ;
586
+
587
+ // The `width` field is more of a minimum width parameter at this point.
588
+ if let Some ( width) = f. options ( ) . get_width ( )
589
+ && code_point_count < width
590
+ {
591
+ // If we're under the minimum width, then fill up the minimum width
592
+ // with the specified string + some alignment.
593
+ let post_padding = f. padding ( width - code_point_count, fmt:: Alignment :: Left ) ?;
594
+ s. write_lossy ( f) ?;
595
+ post_padding. write ( f)
596
+ } else {
597
+ // If we're over the minimum width or there is no minimum width, we
598
+ // can just emit the string.
599
+ s. write_lossy ( f)
583
600
}
584
601
}
585
602
}
@@ -696,6 +713,19 @@ impl Wtf8 {
696
713
}
697
714
}
698
715
716
+ /// Writes the string as lossy UTF-8 like [`Wtf8::to_string_lossy`].
717
+ /// It ignores formatter flags.
718
+ fn write_lossy ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
719
+ let wtf8_bytes = & self . bytes ;
720
+ let mut pos = 0 ;
721
+ while let Some ( ( surrogate_pos, _) ) = self . next_surrogate ( pos) {
722
+ f. write_str ( unsafe { str:: from_utf8_unchecked ( & wtf8_bytes[ pos..surrogate_pos] ) } ) ?;
723
+ f. write_str ( UTF8_REPLACEMENT_CHARACTER ) ?;
724
+ pos = surrogate_pos + 3 ;
725
+ }
726
+ f. write_str ( unsafe { str:: from_utf8_unchecked ( & wtf8_bytes[ pos..] ) } )
727
+ }
728
+
699
729
/// Converts the WTF-8 string to potentially ill-formed UTF-16
700
730
/// and return an iterator of 16-bit code units.
701
731
///
@@ -980,6 +1010,16 @@ impl<'a> Iterator for Wtf8CodePoints<'a> {
980
1010
}
981
1011
}
982
1012
1013
+ impl < ' a > Wtf8CodePoints < ' a > {
1014
+ /// Views the underlying data as a subslice of the original data.
1015
+ #[ inline]
1016
+ pub fn as_slice ( & self ) -> & Wtf8 {
1017
+ // SAFETY: `Wtf8CodePoints` is only made from a `Wtf8Str`, which
1018
+ // guarantees the iter is valid WTF-8.
1019
+ unsafe { Wtf8 :: from_bytes_unchecked ( self . bytes . as_slice ( ) ) }
1020
+ }
1021
+ }
1022
+
983
1023
/// Generates a wide character sequence for potentially ill-formed UTF-16.
984
1024
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
985
1025
#[ derive( Clone ) ]
0 commit comments