File tree 2 files changed +38
-9
lines changed
2 files changed +38
-9
lines changed Original file line number Diff line number Diff line change @@ -2357,10 +2357,26 @@ fn utf8_char_counts() {
2357
2357
2358
2358
assert ! ( !target. starts_with( " " ) && !target. ends_with( " " ) ) ;
2359
2359
let expected_count = tmpl_char_count * repeat;
2360
+
2360
2361
assert_eq ! (
2361
2362
expected_count,
2362
2363
target. chars( ) . count( ) ,
2363
- "wrong count for `{:?}.repeat({})` (padding: `{:?}`)" ,
2364
+ "wrong count for `{:?}.repeat({}).count()` (padding: `{:?}`)" ,
2365
+ tmpl_str,
2366
+ repeat,
2367
+ ( pad_start. len( ) , pad_end. len( ) ) ,
2368
+ ) ;
2369
+
2370
+ // `.advance_by(n)` can also be used to count chars.
2371
+ let mut iter = target. chars ( ) ;
2372
+ let remaining = match iter. advance_by ( usize:: MAX ) {
2373
+ Ok ( ( ) ) => 0 ,
2374
+ Err ( remaining) => remaining. get ( ) ,
2375
+ } ;
2376
+ assert_eq ! (
2377
+ expected_count,
2378
+ usize :: MAX - remaining,
2379
+ "wrong count for `{:?}.repeat({}).advance_by(usize::MAX)` (padding: `{:?}`)" ,
2364
2380
tmpl_str,
2365
2381
repeat,
2366
2382
( pad_start. len( ) , pad_end. len( ) ) ,
Original file line number Diff line number Diff line change @@ -48,27 +48,40 @@ macro_rules! define_benches {
48
48
}
49
49
50
50
define_benches ! {
51
- fn case00_libcore ( s: & str ) {
52
- libcore ( s)
51
+ fn case00_chars_count ( s: & str ) {
52
+ chars_count ( s)
53
53
}
54
54
55
- fn case01_filter_count_cont_bytes( s: & str ) {
55
+ fn case01_chars_advance_by( s: & str ) {
56
+ chars_advance_by( s)
57
+ }
58
+
59
+ fn case02_filter_count_cont_bytes( s: & str ) {
56
60
filter_count_cont_bytes( s)
57
61
}
58
62
59
- fn case02_iter_increment ( s: & str ) {
60
- iterator_increment ( s)
63
+ fn case03_iter_chars_increment ( s: & str ) {
64
+ iter_chars_increment ( s)
61
65
}
62
66
63
- fn case03_manual_char_len ( s: & str ) {
67
+ fn case04_manual_char_len ( s: & str ) {
64
68
manual_char_len( s)
65
69
}
66
70
}
67
71
68
- fn libcore ( s : & str ) -> usize {
72
+ fn chars_count ( s : & str ) -> usize {
69
73
s. chars ( ) . count ( )
70
74
}
71
75
76
+ fn chars_advance_by ( s : & str ) -> usize {
77
+ let mut iter = s. chars ( ) ;
78
+ let remaining = match iter. advance_by ( usize:: MAX ) {
79
+ Ok ( ( ) ) => 0 ,
80
+ Err ( remaining) => remaining. get ( ) ,
81
+ } ;
82
+ usize:: MAX - remaining
83
+ }
84
+
72
85
#[ inline]
73
86
fn utf8_is_cont_byte ( byte : u8 ) -> bool {
74
87
( byte as i8 ) < -64
@@ -78,7 +91,7 @@ fn filter_count_cont_bytes(s: &str) -> usize {
78
91
s. as_bytes ( ) . iter ( ) . filter ( |& & byte| !utf8_is_cont_byte ( byte) ) . count ( )
79
92
}
80
93
81
- fn iterator_increment ( s : & str ) -> usize {
94
+ fn iter_chars_increment ( s : & str ) -> usize {
82
95
let mut c = 0 ;
83
96
for _ in s. chars ( ) {
84
97
c += 1 ;
You can’t perform that action at this time.
0 commit comments