Skip to content

Commit 0c87288

Browse files
committed
Auto merge of #89219 - nickkuk:str_split_once_get_unchecked, r=Mark-Simulacrum
Use get_unchecked in str::[r]split_once This PR removes indices checking in `str::split_once` and `str::rsplit_once` methods.
2 parents 9e8356c + a35aaa2 commit 0c87288

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Diff for: library/core/src/str/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,8 @@ impl str {
15361536
#[inline]
15371537
pub fn split_once<'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> {
15381538
let (start, end) = delimiter.into_searcher(self).next_match()?;
1539-
Some((&self[..start], &self[end..]))
1539+
// SAFETY: `Searcher` is known to return valid indices.
1540+
unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
15401541
}
15411542

15421543
/// Splits the string on the last occurrence of the specified delimiter and
@@ -1556,7 +1557,8 @@ impl str {
15561557
P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
15571558
{
15581559
let (start, end) = delimiter.into_searcher(self).next_match_back()?;
1559-
Some((&self[..start], &self[end..]))
1560+
// SAFETY: `Searcher` is known to return valid indices.
1561+
unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
15601562
}
15611563

15621564
/// An iterator over the disjoint matches of a pattern within the given string

0 commit comments

Comments
 (0)