Skip to content

Commit

Permalink
sl-std: Add str::rsplit_once
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwuelker committed Jun 30, 2024
1 parent 383033c commit 763b132
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions crates/sl-std/src/ascii/str.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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<usize>
pub fn rfind<'a, P>(&'a self, pattern: P) -> Option<usize>
where
P: Pattern<'a>,
P::Searcher: ReverseSearcher<'a>,
{
pattern
Expand All @@ -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) {
Expand Down

0 comments on commit 763b132

Please sign in to comment.