about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZachary S <zasample18+github@gmail.com>2024-10-23 14:22:32 -0500
committerZachary S <zasample18+github@gmail.com>2024-10-23 14:22:32 -0500
commit8ee548fcf01ea4aa5b17f6daef6f2f86c2f6fde5 (patch)
tree123a7f254bee012fd1a0adb10347b72bc3976146
parent8d94e06ec9758b5c03ea77bb5dab22a1a76bc261 (diff)
downloadrust-8ee548fcf01ea4aa5b17f6daef6f2f86c2f6fde5.tar.gz
rust-8ee548fcf01ea4aa5b17f6daef6f2f86c2f6fde5.zip
const fn str::is_char_boundary
-rw-r--r--library/core/src/str/mod.rs13
1 files changed, 7 insertions, 6 deletions
diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs
index e93c52f2799..e5b8e015aea 100644
--- a/library/core/src/str/mod.rs
+++ b/library/core/src/str/mod.rs
@@ -185,8 +185,9 @@ impl str {
     /// ```
     #[must_use]
     #[stable(feature = "is_char_boundary", since = "1.9.0")]
+    #[rustc_const_unstable(feature = "const_is_char_boundary", issue = "131516")]
     #[inline]
-    pub fn is_char_boundary(&self, index: usize) -> bool {
+    pub const fn is_char_boundary(&self, index: usize) -> bool {
         // 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.
@@ -195,8 +196,8 @@ impl str {
             return true;
         }
 
-        match self.as_bytes().get(index) {
-            // For `None` we have two options:
+        if index >= self.len() {
+            // For `true` we have two options:
             //
             // - index == self.len()
             //   Empty strings are valid, so return true
@@ -205,9 +206,9 @@ impl str {
             //
             // 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(),
-
-            Some(&b) => b.is_utf8_char_boundary(),
+            index == self.len()
+        } else {
+            self.as_bytes()[index].is_utf8_char_boundary()
         }
     }