From 5a3aa2f73cbb08c6e41418c5378791fa24a66146 Mon Sep 17 00:00:00 2001 From: Ulrik Sverdrup Date: Sat, 19 Nov 2016 23:18:43 +0100 Subject: str: Improve .chars().count() Use a simpler loop to count the `char` of a string: count the number of non-continuation bytes. Use `count += ` which the compiler understands well and can apply loop optimizations to. --- src/libcore/str/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/libcore/str') diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 196750254af..7f91da53142 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -424,6 +424,17 @@ 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 + } + #[inline] fn size_hint(&self) -> (usize, Option) { let len = self.iter.len(); @@ -501,6 +512,11 @@ impl<'a> Iterator for CharIndices<'a> { } } + #[inline] + fn count(self) -> usize { + self.iter.count() + } + #[inline] fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() -- cgit 1.4.1-3-g733a5