Skip to content

Commit 887bcf6

Browse files
committed
const fn str::is_char_boundary
1 parent 36a90d7 commit 887bcf6

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

core/src/str/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ impl str {
185185
/// ```
186186
#[must_use]
187187
#[stable(feature = "is_char_boundary", since = "1.9.0")]
188+
#[rustc_const_unstable(feature = "const_is_char_boundary", issue = "131516")]
188189
#[inline]
189-
pub fn is_char_boundary(&self, index: usize) -> bool {
190+
pub const fn is_char_boundary(&self, index: usize) -> bool {
190191
// 0 is always ok.
191192
// Test for 0 explicitly so that it can optimize out the check
192193
// easily and skip reading string data for that case.
@@ -195,8 +196,8 @@ impl str {
195196
return true;
196197
}
197198

198-
match self.as_bytes().get(index) {
199-
// For `None` we have two options:
199+
if index >= self.len() {
200+
// For `true` we have two options:
200201
//
201202
// - index == self.len()
202203
// Empty strings are valid, so return true
@@ -205,9 +206,9 @@ impl str {
205206
//
206207
// The check is placed exactly here, because it improves generated
207208
// code on higher opt-levels. See PR #84751 for more details.
208-
None => index == self.len(),
209-
210-
Some(&b) => b.is_utf8_char_boundary(),
209+
index == self.len()
210+
} else {
211+
self.as_bytes()[index].is_utf8_char_boundary()
211212
}
212213
}
213214

0 commit comments

Comments
 (0)