@@ -152,7 +152,9 @@ impl UnicodeTruncateStr for str {
152
152
let ( byte_index, new_width) = self
153
153
. char_indices ( )
154
154
// map to byte index and the width of char start at the index
155
- . map ( |( byte_index, char) | ( byte_index, char. width ( ) . unwrap_or ( 0 ) ) )
155
+ // control characters treated as of width 1
156
+ // https://github.com/unicode-rs/unicode-width/pull/45
157
+ . map ( |( byte_index, char) | ( byte_index, char. width ( ) . unwrap_or ( 1 ) ) )
156
158
// chain a final element representing the position past the last char
157
159
. chain ( core:: iter:: once ( ( self . len ( ) , 0 ) ) )
158
160
// fold to byte index and the width up to the index
@@ -182,7 +184,9 @@ impl UnicodeTruncateStr for str {
182
184
// instead of start checking from the start do so from the end
183
185
. rev ( )
184
186
// map to byte index and the width of char start at the index
185
- . map ( |( byte_index, char) | ( byte_index, char. width ( ) . unwrap_or ( 0 ) ) )
187
+ // control characters treated as of width 1
188
+ // https://github.com/unicode-rs/unicode-width/pull/45
189
+ . map ( |( byte_index, char) | ( byte_index, char. width ( ) . unwrap_or ( 1 ) ) )
186
190
// skip any position with zero width, the cut won't happen at these points
187
191
// this also helps with not including zero width char at the beginning
188
192
. filter ( |& ( _, char_width) | char_width > 0 )
@@ -223,7 +227,9 @@ impl UnicodeTruncateStr for str {
223
227
224
228
let from_start = self
225
229
. char_indices ( )
226
- . map ( |( byte_index, char) | ( byte_index, char. width ( ) . unwrap_or ( 0 ) ) )
230
+ // control characters treated as of width 1
231
+ // https://github.com/unicode-rs/unicode-width/pull/45
232
+ . map ( |( byte_index, char) | ( byte_index, char. width ( ) . unwrap_or ( 1 ) ) )
227
233
// skip any position with zero width, the cut won't happen at these points
228
234
// this also helps with removing zero width char at the beginning
229
235
. filter ( |& ( _, char_width) | char_width > 0 )
@@ -242,7 +248,9 @@ impl UnicodeTruncateStr for str {
242
248
243
249
let from_end = self
244
250
. char_indices ( )
245
- . map ( |( byte_index, char) | ( byte_index, char. width ( ) . unwrap_or ( 0 ) ) )
251
+ // control characters treated as of width 1
252
+ // https://github.com/unicode-rs/unicode-width/pull/45
253
+ . map ( |( byte_index, char) | ( byte_index, char. width ( ) . unwrap_or ( 1 ) ) )
246
254
// skip any position with zero width, the cut won't happen at these points
247
255
// this also helps with keeping zero width char at the end
248
256
. filter ( |& ( _, char_width) | char_width > 0 )
@@ -511,6 +519,13 @@ mod tests {
511
519
( "b\u{0306} y\u{0306} " , 2 )
512
520
) ;
513
521
}
522
+
523
+ #[ test]
524
+ fn control_char ( ) {
525
+ assert_eq ! ( "\u{0019} " . width( ) , 1 ) ;
526
+ assert_eq ! ( '\u{0019}' . width( ) , None ) ;
527
+ assert_eq ! ( "\u{0019} " . unicode_truncate( 2 ) , ( "\u{0019} " , 1 ) ) ;
528
+ }
514
529
}
515
530
516
531
#[ test]
0 commit comments