From 763b1322680e2dbb8dce9d4ae5a2fffa32a18609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BClker?= Date: Mon, 1 Jul 2024 00:14:26 +0200 Subject: [PATCH] sl-std: Add str::rsplit_once --- crates/sl-std/src/ascii/str.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/sl-std/src/ascii/str.rs b/crates/sl-std/src/ascii/str.rs index 10bd394d..917f077e 100644 --- a/crates/sl-std/src/ascii/str.rs +++ b/crates/sl-std/src/ascii/str.rs @@ -1,4 +1,4 @@ -use super::{AsciiCharExt, NotAscii, ReverseSearcher, Searcher, String}; +use super::{AsciiCharExt, NotAscii, Pattern, ReverseSearcher, Searcher, String}; use std::{ascii::Char, fmt, iter::FusedIterator, ops, slice::SliceIndex}; /// A borrowed [String] @@ -382,8 +382,9 @@ impl Str { /// assert_eq!(haystack.rfind(ascii::Char::SmallX), None) /// ``` #[must_use] - pub fn rfind<'a, P: super::Pattern<'a>>(&'a self, pattern: P) -> Option + pub fn rfind<'a, P>(&'a self, pattern: P) -> Option where + P: Pattern<'a>, P::Searcher: ReverseSearcher<'a>, { pattern @@ -400,6 +401,19 @@ impl Str { Some(parts) } + /// Splits the string on the last occurrence of the specified delimiter and + /// returns prefix before delimiter and suffix after delimiter. + #[inline] + #[must_use] + pub fn rsplit_once<'a, P>(&'a self, delimiter: P) -> Option<(&Self, &Self)> + where + P: Pattern<'a>, + P::Searcher: ReverseSearcher<'a>, + { + let (start, end) = delimiter.into_searcher(self).next_match_back()?; + Some((&self[..start], &self[end..])) + } + #[inline] #[must_use] pub fn split_at(&self, index: usize) -> (&Self, &Self) {