about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-10-10 12:29:48 +0000
committerbors <bors@rust-lang.org>2021-10-10 12:29:48 +0000
commit0c87288f92b7e6365d61cfbcbc453ea4c696c030 (patch)
tree32ccf940f6499768f0e91555f4d327276adda178
parent9e8356c6adf119f983651d533d2b307544086cf9 (diff)
parenta35aaa21080ddb3e3dc0a4778d4ced863b2815a7 (diff)
downloadrust-0c87288f92b7e6365d61cfbcbc453ea4c696c030.tar.gz
rust-0c87288f92b7e6365d61cfbcbc453ea4c696c030.zip
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.
-rw-r--r--library/core/src/str/mod.rs6
1 files changed, 4 insertions, 2 deletions
diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs
index f25ab52cfa0..a50b65ec6af 100644
--- a/library/core/src/str/mod.rs
+++ b/library/core/src/str/mod.rs
@@ -1536,7 +1536,8 @@ impl str {
     #[inline]
     pub fn split_once<'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> {
         let (start, end) = delimiter.into_searcher(self).next_match()?;
-        Some((&self[..start], &self[end..]))
+        // SAFETY: `Searcher` is known to return valid indices.
+        unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
     }
 
     /// Splits the string on the last occurrence of the specified delimiter and
@@ -1556,7 +1557,8 @@ impl str {
         P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
     {
         let (start, end) = delimiter.into_searcher(self).next_match_back()?;
-        Some((&self[..start], &self[end..]))
+        // SAFETY: `Searcher` is known to return valid indices.
+        unsafe { Some((self.get_unchecked(..start), self.get_unchecked(end..))) }
     }
 
     /// An iterator over the disjoint matches of a pattern within the given string