Skip to content

Commit a35aaa2

Browse files
committed
Use get_unchecked in str::[r]split_once
1 parent 197fc85 commit a35aaa2

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

library/core/src/str/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,8 @@ impl str {
15241524
#[inline]
15251525
pub fn split_once<'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> {
15261526
let (start, end) = delimiter.into_searcher(self).next_match()?;
1527-
Some((&self[..start], &self[end..]))
1527+
// SAFETY: `Searcher` is known to return valid indices.
1528+
unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
15281529
}
15291530

15301531
/// Splits the string on the last occurrence of the specified delimiter and
@@ -1544,7 +1545,8 @@ impl str {
15441545
P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
15451546
{
15461547
let (start, end) = delimiter.into_searcher(self).next_match_back()?;
1547-
Some((&self[..start], &self[end..]))
1548+
// SAFETY: `Searcher` is known to return valid indices.
1549+
unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
15481550
}
15491551

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

0 commit comments

Comments
 (0)