From a35aaa21080ddb3e3dc0a4778d4ced863b2815a7 Mon Sep 17 00:00:00 2001 From: nickkuk Date: Fri, 24 Sep 2021 16:06:50 +0500 Subject: [PATCH] Use get_unchecked in str::[r]split_once --- library/core/src/str/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 607fb627605..b941410139e 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -1524,7 +1524,8 @@ pub fn rsplitn<'a, P>(&'a self, n: usize, pat: P) -> RSplitN<'a, P> #[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 @@ -1544,7 +1545,8 @@ pub fn rsplit_once<'a, P>(&'a self, delimiter: P) -> Option<(&'a str, &'a 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