Skip to content

Commit 9badc33

Browse files
committed
Add str::strip_prefix and str::strip_suffix
1 parent e41ced3 commit 9badc33

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

src/libcore/str/mod.rs

+68-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![stable(feature = "rust1", since = "1.0.0")]
99

1010
use self::pattern::Pattern;
11-
use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
11+
use self::pattern::{Searcher, SearchStep, ReverseSearcher, DoubleEndedSearcher};
1212

1313
use crate::char;
1414
use crate::fmt::{self, Write};
@@ -3791,6 +3791,73 @@ impl str {
37913791
}
37923792
}
37933793

3794+
/// Returns a string slice with the prefix removed.
3795+
///
3796+
/// If the string starts with the pattern `prefix`, `Some` is returned with the substring where
3797+
/// the prefix is removed. Unlike `trim_start_matches`, this method removes the prefix exactly
3798+
/// once.
3799+
///
3800+
/// If the string does not start with `prefix`, it is removed.
3801+
///
3802+
/// # Examples
3803+
///
3804+
/// ```
3805+
/// #![feature(str_strip)]
3806+
///
3807+
/// assert_eq!("foobar".strip_prefix("foo"), Some("bar"));
3808+
/// assert_eq!("foobar".strip_prefix("bar"), None);
3809+
/// assert_eq!("foofoo".strip_prefix("foo"), Some("foo"));
3810+
/// ```
3811+
#[must_use = "this returns the remaining substring as a new slice, \
3812+
without modifying the original"]
3813+
#[unstable(feature = "str_strip", reason = "newly added", issue = "0")]
3814+
pub fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str> {
3815+
let mut matcher = prefix.into_searcher(self);
3816+
if let SearchStep::Match(start, len) = matcher.next() {
3817+
debug_assert_eq!(start, 0);
3818+
unsafe {
3819+
Some(self.get_unchecked(len..))
3820+
}
3821+
} else {
3822+
None
3823+
}
3824+
}
3825+
3826+
/// Returns a string slice with the suffix removed.
3827+
///
3828+
/// If the string starts with the pattern `suffix`, `Some` is returned with the substring where
3829+
/// the suffix is removed. Unlike `trim_end_matches`, this method removes the suffix exactly
3830+
/// once.
3831+
///
3832+
/// If the string does not start with `suffix`, it is removed.
3833+
///
3834+
/// # Examples
3835+
///
3836+
/// ```
3837+
/// #![feature(str_strip)]
3838+
/// assert_eq!("barfoo".strip_suffix("foo"), Some("bar"));
3839+
/// assert_eq!("barfoo".strip_suffix("bar"), None);
3840+
/// assert_eq!("foofoo".strip_suffix("foo"), Some("foo"));
3841+
/// ```
3842+
#[must_use = "this returns the remaining substring as a new slice, \
3843+
without modifying the original"]
3844+
#[unstable(feature = "str_strip", reason = "newly added", issue = "0")]
3845+
pub fn strip_suffix<'a, P>(&'a self, suffix: P) -> Option<&'a str>
3846+
where
3847+
P: Pattern<'a>,
3848+
<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,
3849+
{
3850+
let mut matcher = suffix.into_searcher(self);
3851+
if let SearchStep::Match(start, end) = matcher.next_back() {
3852+
debug_assert_eq!(end, self.len());
3853+
unsafe {
3854+
Some(self.get_unchecked(..start))
3855+
}
3856+
} else {
3857+
None
3858+
}
3859+
}
3860+
37943861
/// Returns a string slice with all suffixes that match a pattern
37953862
/// repeatedly removed.
37963863
///

0 commit comments

Comments
 (0)