From 328abfb9431c486ab2d0b9ebd7f6115805f613de Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 26 Jan 2021 11:14:57 +0000 Subject: [PATCH 1/4] Slight simplification of chars().count() --- library/core/src/str/iter.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 8b952eab2946d..c37843434781a 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -47,12 +47,13 @@ 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; + let mut char_count = 0; for &byte in self.iter { - cont_bytes += utf8_is_cont_byte(byte) as usize; + if !utf8_is_cont_byte(byte) { + char_count += 1; + } } - bytes_len - cont_bytes + char_count } #[inline] From 425a70a460abac1182a792c6c2af6dff69663c3c Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 26 Jan 2021 11:26:58 +0000 Subject: [PATCH 2/4] Removing if so it's more like the previous implementation. --- library/core/src/str/iter.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index c37843434781a..34c31d599a318 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -49,9 +49,7 @@ impl<'a> Iterator for Chars<'a> { // length in `char` is equal to the number of non-continuation bytes let mut char_count = 0; for &byte in self.iter { - if !utf8_is_cont_byte(byte) { - char_count += 1; - } + char_count += !utf8_is_cont_byte(byte) as usize; } char_count } From c07e5585b312aaa76f7bd35e60cf8e8d9164369c Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 26 Jan 2021 11:36:02 +0000 Subject: [PATCH 3/4] Let's try the most idiomatic way. --- library/core/src/str/iter.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 34c31d599a318..0c74f6e45a784 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -47,11 +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 mut char_count = 0; - for &byte in self.iter { - char_count += !utf8_is_cont_byte(byte) as usize; - } - char_count + self.iter.map(|&byte| !utf8_is_cont_byte(byte) as usize).sum() } #[inline] From a623ea5301737507a8229c2ddae74b20f727bd1b Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 26 Jan 2021 21:51:18 +0000 Subject: [PATCH 4/4] Same instructions, but simpler. --- library/core/src/str/iter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 0c74f6e45a784..83f484dc570c4 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -47,7 +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 - self.iter.map(|&byte| !utf8_is_cont_byte(byte) as usize).sum() + self.iter.filter(|&&byte| !utf8_is_cont_byte(byte)).count() } #[inline]