Skip to content

Commit a10c43a

Browse files
committed
Added the str::*_mut methods using Needle API.
1 parent f1bedc3 commit a10c43a

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed

src/liballoc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(try_reserve)]
1010
#![feature(unboxed_closures)]
1111
#![feature(vecdeque_rotate)]
12+
#![feature(mut_str_needle_methods)]
1213

1314
use std::hash::{Hash, Hasher};
1415
use std::collections::hash_map::DefaultHasher;

src/liballoc/tests/str.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,3 +1709,38 @@ fn different_str_pattern_forwarding_lifetimes() {
17091709

17101710
foo("x");
17111711
}
1712+
1713+
#[test]
1714+
fn test_mut_str() {
1715+
use std::ops::Range;
1716+
1717+
let mut s = String::from("a1b2c3d4e");
1718+
{
1719+
let res: &mut str = s.trim_matches_mut(|c: char| c.is_ascii_alphabetic());
1720+
assert_eq!(res, "1b2c3d4");
1721+
}
1722+
{
1723+
let res: Vec<&mut str> = s.split_mut(|c: char| c.is_ascii_digit()).collect();
1724+
assert_eq!(res, vec!["a", "b", "c", "d", "e"]);
1725+
}
1726+
{
1727+
let res: Vec<(Range<usize>, &mut str)> = s.match_ranges_mut(|c: char| c.is_ascii_digit()).collect();
1728+
let res = res.into_iter().map(|(r, ss)| (r, &*ss)).collect::<Vec<_>>();
1729+
assert_eq!(res, vec![
1730+
(1..2, "1"),
1731+
(3..4, "2"),
1732+
(5..6, "3"),
1733+
(7..8, "4"),
1734+
]);
1735+
}
1736+
{
1737+
let res: Vec<(Range<usize>, &mut str)> = s.rmatch_ranges_mut(|c: char| c.is_ascii_digit()).collect();
1738+
let res = res.into_iter().map(|(r, ss)| (r, &*ss)).collect::<Vec<_>>();
1739+
assert_eq!(res, vec![
1740+
(7..8, "4"),
1741+
(5..6, "3"),
1742+
(3..4, "2"),
1743+
(1..2, "1"),
1744+
]);
1745+
}
1746+
}

src/libcore/str/mod.rs

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,6 +3011,101 @@ impl str {
30113011
ext::rsplitn(self, n, pat)
30123012
}
30133013

3014+
// FIXME: Someone should enhance the docs before stabilizing.
3015+
3016+
/// An iterator over substrings of this mutable string slice, separated by
3017+
/// characters matched by a pattern.
3018+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3019+
#[inline]
3020+
pub fn split_mut<'a, P>(&'a mut self, pat: P) -> ext::Split<&'a mut str, P::Searcher>
3021+
where
3022+
P: Needle<&'a mut str>,
3023+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3024+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3025+
{
3026+
ext::split(self, pat)
3027+
}
3028+
3029+
/// An iterator over substrings of the given mutable string slice, separated by
3030+
/// characters matched by a pattern and yielded in reverse order.
3031+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3032+
#[inline]
3033+
pub fn rsplit_mut<'a, P>(&'a mut self, pat: P) -> ext::RSplit<&'a mut str, P::Searcher>
3034+
where
3035+
P: Needle<&'a mut str>,
3036+
P::Searcher: ReverseSearcher<str>,
3037+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3038+
{
3039+
ext::rsplit(self, pat)
3040+
}
3041+
3042+
/// An iterator over substrings of the given mutable string slice, separated by
3043+
/// characters matched by a pattern.
3044+
///
3045+
/// Equivalent to [`split_mut`], except that the trailing substring
3046+
/// is skipped if empty.
3047+
///
3048+
/// [`split_mut`]: #method.split_mut
3049+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3050+
#[inline]
3051+
pub fn split_terminator_mut<'a, P>(&'a mut self, pat: P)
3052+
-> ext::SplitTerminator<&'a mut str, P::Searcher>
3053+
where
3054+
P: Needle<&'a mut str>,
3055+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3056+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3057+
{
3058+
ext::split_terminator(self, pat)
3059+
}
3060+
3061+
/// An iterator over substrings of the given mutable string slice, separated by
3062+
/// characters matched by a pattern and yielded in reverse order.
3063+
///
3064+
/// Equivalent to [`rsplit_mut`], except that the trailing substring
3065+
/// is skipped if empty.
3066+
///
3067+
/// [`rsplit_mut`]: #method.rsplit_mut
3068+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3069+
#[inline]
3070+
pub fn rsplit_terminator_mut<'a, P>(&'a mut self, pat: P)
3071+
-> ext::RSplitTerminator<&'a mut str, P::Searcher>
3072+
where
3073+
P: Needle<&'a mut str>,
3074+
P::Searcher: ReverseSearcher<str>,
3075+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3076+
{
3077+
ext::rsplit_terminator(self, pat)
3078+
}
3079+
3080+
/// An iterator over substrings of the given mutable string slice, separated by a
3081+
/// pattern, restricted to returning at most `n` items.
3082+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3083+
#[inline]
3084+
pub fn splitn_mut<'a, P>(&'a mut self, n: usize, pat: P)
3085+
-> ext::SplitN<&'a mut str, P::Searcher>
3086+
where
3087+
P: Needle<&'a mut str>,
3088+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3089+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3090+
{
3091+
ext::splitn(self, n, pat)
3092+
}
3093+
3094+
/// An iterator over substrings of this mutable string slice, separated by a
3095+
/// pattern, starting from the end of the string, restricted to returning
3096+
/// at most `n` items.
3097+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3098+
#[inline]
3099+
pub fn rsplitn_mut<'a, P>(&'a mut self, n: usize, pat: P)
3100+
-> ext::RSplitN<&'a mut str, P::Searcher>
3101+
where
3102+
P: Needle<&'a mut str>,
3103+
P::Searcher: ReverseSearcher<str>,
3104+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3105+
{
3106+
ext::rsplitn(self, n, pat)
3107+
}
3108+
30143109
/// An iterator over the disjoint matches of a pattern within the given string
30153110
/// slice.
30163111
///
@@ -3274,6 +3369,88 @@ impl str {
32743369
ext::rmatch_ranges(self, pat)
32753370
}
32763371

3372+
/// An iterator over the disjoint matches of a pattern within the given
3373+
/// mutable string slice.
3374+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3375+
#[inline]
3376+
pub fn matches_mut<'a, P>(&'a mut self, pat: P) -> ext::Matches<&'a mut str, P::Searcher>
3377+
where
3378+
P: Needle<&'a mut str>,
3379+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3380+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3381+
{
3382+
ext::matches(self, pat)
3383+
}
3384+
3385+
/// An iterator over the disjoint matches of a pattern within this
3386+
/// mutable string slice, yielded in reverse order.
3387+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3388+
#[inline]
3389+
pub fn rmatches_mut<'a, P>(&'a mut self, pat: P) -> ext::RMatches<&'a mut str, P::Searcher>
3390+
where
3391+
P: Needle<&'a mut str>,
3392+
P::Searcher: ReverseSearcher<str>,
3393+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3394+
{
3395+
ext::rmatches(self, pat)
3396+
}
3397+
3398+
/// An iterator over the disjoint matches of a pattern within this mutable string
3399+
/// slice as well as the index that the match starts at.
3400+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3401+
#[inline]
3402+
pub fn match_indices_mut<'a, P>(&'a mut self, pat: P)
3403+
-> ext::MatchIndices<&'a mut str, P::Searcher>
3404+
where
3405+
P: Needle<&'a mut str>,
3406+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3407+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3408+
{
3409+
ext::match_indices(self, pat)
3410+
}
3411+
3412+
/// An iterator over the disjoint matches of a pattern within this mutable string slice,
3413+
/// yielded in reverse order along with the index of the match.
3414+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3415+
#[inline]
3416+
pub fn rmatch_indices_mut<'a, P>(&'a mut self, pat: P)
3417+
-> ext::RMatchIndices<&'a mut str, P::Searcher>
3418+
where
3419+
P: Needle<&'a mut str>,
3420+
P::Searcher: ReverseSearcher<str>,
3421+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3422+
{
3423+
ext::rmatch_indices(self, pat)
3424+
}
3425+
3426+
/// An iterator over the disjoint matches of a pattern within this mutable string
3427+
/// slice as well as the range that the match covers.
3428+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3429+
#[inline]
3430+
pub fn match_ranges_mut<'a, P>(&'a mut self, pat: P)
3431+
-> ext::MatchRanges<&'a mut str, P::Searcher>
3432+
where
3433+
P: Needle<&'a mut str>,
3434+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3435+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3436+
{
3437+
ext::match_ranges(self, pat)
3438+
}
3439+
3440+
/// An iterator over the disjoint matches of a pattern within this mutable string slice,
3441+
/// yielded in reverse order along with the range of the match.
3442+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3443+
#[inline]
3444+
pub fn rmatch_ranges_mut<'a, P>(&'a mut self, pat: P)
3445+
-> ext::RMatchRanges<&'a mut str, P::Searcher>
3446+
where
3447+
P: Needle<&'a mut str>,
3448+
P::Searcher: ReverseSearcher<str>,
3449+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3450+
{
3451+
ext::rmatch_ranges(self, pat)
3452+
}
3453+
32773454
/// Returns a string slice with leading and trailing whitespace removed.
32783455
///
32793456
/// 'Whitespace' is defined according to the terms of the Unicode Derived
@@ -3647,6 +3824,63 @@ impl str {
36473824
self.trim_end_matches(pat)
36483825
}
36493826

3827+
/// Returns a mutable string slice with leading and trailing whitespace removed.
3828+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3829+
#[inline]
3830+
pub fn trim_mut(&mut self) -> &mut str {
3831+
self.trim_matches_mut(|c: char| c.is_whitespace())
3832+
}
3833+
3834+
/// Returns a mutable string slice with leading whitespace removed.
3835+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3836+
#[inline]
3837+
pub fn trim_start_mut(&mut self) -> &mut str {
3838+
self.trim_start_matches_mut(|c: char| c.is_whitespace())
3839+
}
3840+
3841+
/// Returns a mutable string slice with trailing whitespace removed.
3842+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3843+
#[inline]
3844+
pub fn trim_end_mut(&mut self) -> &mut str {
3845+
self.trim_end_matches_mut(|c: char| c.is_whitespace())
3846+
}
3847+
3848+
/// Returns a mutable string slice with all prefixes and suffixes that match a
3849+
/// pattern repeatedly removed.
3850+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3851+
#[inline]
3852+
pub fn trim_matches_mut<'a, P: Needle<&'a mut str>>(&'a mut self, pat: P) -> &'a mut str
3853+
where
3854+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3855+
P::Consumer: DoubleEndedConsumer<str>,
3856+
{
3857+
ext::trim(self, pat)
3858+
}
3859+
3860+
/// Returns a mutable string slice with all prefixes that match a pattern
3861+
/// repeatedly removed.
3862+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3863+
#[inline]
3864+
pub fn trim_start_matches_mut<'a, P: Needle<&'a mut str>>(&'a mut self, pat: P) -> &'a mut str
3865+
where
3866+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3867+
P::Consumer: Consumer<str>, // FIXME: RFC 2089
3868+
{
3869+
ext::trim_start(self, pat)
3870+
}
3871+
3872+
/// Returns a mutable string slice with all suffixes that match a pattern
3873+
/// repeatedly removed.
3874+
#[unstable(feature = "mut_str_needle_methods", issue = "56345")]
3875+
#[inline]
3876+
pub fn trim_end_matches_mut<'a, P: Needle<&'a mut str>>(&'a mut self, pat: P) -> &'a mut str
3877+
where
3878+
P::Searcher: Searcher<str>, // FIXME: RFC 2089
3879+
P::Consumer: ReverseConsumer<str>,
3880+
{
3881+
ext::trim_end(self, pat)
3882+
}
3883+
36503884
/// Parses this string slice into another type.
36513885
///
36523886
/// Because `parse` is so general, it can cause problems with type

0 commit comments

Comments
 (0)