Skip to content

Commit

Permalink
Rollup merge of rust-lang#81409 - gilescope:chars_count, r=joshtriplett
Browse files Browse the repository at this point in the history
Slight simplification of chars().count()

Slight simplification: No need to call len(), we can just count the number of non continuation bytes.

I can't see any reason not to do this, can you?
  • Loading branch information
henryboisdequin authored Jan 29, 2021
2 parents 69dd59f + a623ea5 commit 8d41097
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions library/core/src/str/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ impl<'a> Iterator for Chars<'a> {
#[inline]
fn count(self) -> usize {
// length in `char` is equal to the number of non-continuation bytes
let bytes_len = self.iter.len();
let mut cont_bytes = 0;
for &byte in self.iter {
cont_bytes += utf8_is_cont_byte(byte) as usize;
}
bytes_len - cont_bytes
self.iter.filter(|&&byte| !utf8_is_cont_byte(byte)).count()
}

#[inline]
Expand Down

0 comments on commit 8d41097

Please sign in to comment.