From 2ea0410f090ae06d18f5535e62e888e49a4f21a7 Mon Sep 17 00:00:00 2001 From: Soveu Date: Fri, 30 Apr 2021 16:13:00 +0200 Subject: [PATCH 1/2] str::is_char_boundary - slight optimization --- library/core/src/str/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 95dd54976b2c0..e7970bf5c8f13 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -195,11 +195,11 @@ impl str { // 0 and len are always ok. // Test for 0 explicitly so that it can optimize out the check // easily and skip reading string data for that case. - if index == 0 || index == self.len() { + if index == 0 { return true; } match self.as_bytes().get(index) { - None => false, + None => index == self.len(), // This is bit magic equivalent to: b < 128 || b >= 192 Some(&b) => (b as i8) >= -0x40, } From 7bd9d9f1e995272494854c78dd60fcd8e06487f8 Mon Sep 17 00:00:00 2001 From: Soveu Date: Fri, 30 Apr 2021 20:51:30 +0200 Subject: [PATCH 2/2] str::is_char_boundary - few comments --- library/core/src/str/mod.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index e7970bf5c8f13..065acd3f38bb2 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -192,14 +192,26 @@ impl str { #[stable(feature = "is_char_boundary", since = "1.9.0")] #[inline] pub fn is_char_boundary(&self, index: usize) -> bool { - // 0 and len are always ok. + // 0 is always ok. // Test for 0 explicitly so that it can optimize out the check // easily and skip reading string data for that case. + // Note that optimizing `self.get(..index)` relies on this. if index == 0 { return true; } + match self.as_bytes().get(index) { + // For `None` we have two options: + // + // - index == self.len() + // Empty strings are valid, so return true + // - index > self.len() + // In this case return false + // + // The check is placed exactly here, because it improves generated + // code on higher opt-levels. See PR #84751 for more details. None => index == self.len(), + // This is bit magic equivalent to: b < 128 || b >= 192 Some(&b) => (b as i8) >= -0x40, }