Skip to content

Commit bb2687d

Browse files
committed
Add str::[r]split_once
This is useful for quick&dirty parsing of key: value config pairs
1 parent 59c1db0 commit bb2687d

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/liballoc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(map_first_last)]
66
#![feature(new_uninit)]
77
#![feature(pattern)]
8+
#![feature(str_split_once)]
89
#![feature(trusted_len)]
910
#![feature(try_reserve)]
1011
#![feature(unboxed_closures)]

src/liballoc/tests/str.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,30 @@ fn test_rsplitn() {
12751275
assert_eq!(split, ["mb\n", "\nMäry häd ä little lämb\nLittle l"]);
12761276
}
12771277

1278+
#[test]
1279+
fn test_split_once() {
1280+
assert_eq!("".split_once("->"), None);
1281+
assert_eq!("-".split_once("->"), None);
1282+
assert_eq!("->".split_once("->"), Some(("", "")));
1283+
assert_eq!("a->".split_once("->"), Some(("a", "")));
1284+
assert_eq!("->b".split_once("->"), Some(("", "b")));
1285+
assert_eq!("a->b".split_once("->"), Some(("a", "b")));
1286+
assert_eq!("a->b->c".split_once("->"), Some(("a", "b->c")));
1287+
assert_eq!("---".split_once("--"), Some(("", "-")));
1288+
}
1289+
1290+
#[test]
1291+
fn test_rsplit_once() {
1292+
assert_eq!("".rsplit_once("->"), None);
1293+
assert_eq!("-".rsplit_once("->"), None);
1294+
assert_eq!("->".rsplit_once("->"), Some(("", "")));
1295+
assert_eq!("a->".rsplit_once("->"), Some(("a", "")));
1296+
assert_eq!("->b".rsplit_once("->"), Some(("", "b")));
1297+
assert_eq!("a->b".rsplit_once("->"), Some(("a", "b")));
1298+
assert_eq!("a->b->c".rsplit_once("->"), Some(("a->b", "c")));
1299+
assert_eq!("---".rsplit_once("--"), Some(("-", "")));
1300+
}
1301+
12781302
#[test]
12791303
fn test_split_whitespace() {
12801304
let data = "\n \tMäry häd\tä little lämb\nLittle lämb\n";

src/libcore/str/mod.rs

+41
Original file line numberDiff line numberDiff line change
@@ -3427,6 +3427,47 @@ impl str {
34273427
RSplitN(self.splitn(n, pat).0)
34283428
}
34293429

3430+
/// Splits the string on the first occurrence of the specified delimiter and
3431+
/// returns prefix before delimiter and suffix after delimiter.
3432+
///
3433+
/// # Examples
3434+
///
3435+
/// ```
3436+
/// #![feature(str_split_once)]
3437+
///
3438+
/// assert_eq!("cfg".split_once('='), None);
3439+
/// assert_eq!("cfg=foo".split_once('='), Some(("cfg", "foo")));
3440+
/// assert_eq!("cfg=foo=bar".split_once('='), Some(("cfg", "foo=bar")));
3441+
/// ```
3442+
#[unstable(feature = "str_split_once", reason = "newly added", issue = "74773")]
3443+
#[inline]
3444+
pub fn split_once<'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> {
3445+
let (start, end) = delimiter.into_searcher(self).next_match()?;
3446+
Some((&self[..start], &self[end..]))
3447+
}
3448+
3449+
/// Splits the string on the last occurrence of the specified delimiter and
3450+
/// returns prefix before delimiter and suffix after delimiter.
3451+
///
3452+
/// # Examples
3453+
///
3454+
/// ```
3455+
/// #![feature(str_split_once)]
3456+
///
3457+
/// assert_eq!("cfg".rsplit_once('='), None);
3458+
/// assert_eq!("cfg=foo".rsplit_once('='), Some(("cfg", "foo")));
3459+
/// assert_eq!("cfg=foo=bar".rsplit_once('='), Some(("cfg=foo", "bar")));
3460+
/// ```
3461+
#[unstable(feature = "str_split_once", reason = "newly added", issue = "74773")]
3462+
#[inline]
3463+
pub fn rsplit_once<'a, P>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)>
3464+
where
3465+
P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3466+
{
3467+
let (start, end) = delimiter.into_searcher(self).next_match_back()?;
3468+
Some((&self[..start], &self[end..]))
3469+
}
3470+
34303471
/// An iterator over the disjoint matches of a pattern within the given string
34313472
/// slice.
34323473
///

0 commit comments

Comments
 (0)