Skip to content

Commit 6e9dc7d

Browse files
committed
Add str::[r]split_once
This is useful for quick&dirty parsing of key: value config pairs
1 parent 1454bbd commit 6e9dc7d

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

library/alloc/tests/lib.rs

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

library/alloc/tests/str.rs

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

1321+
#[test]
1322+
fn test_split_once() {
1323+
assert_eq!("".split_once("->"), None);
1324+
assert_eq!("-".split_once("->"), None);
1325+
assert_eq!("->".split_once("->"), Some(("", "")));
1326+
assert_eq!("a->".split_once("->"), Some(("a", "")));
1327+
assert_eq!("->b".split_once("->"), Some(("", "b")));
1328+
assert_eq!("a->b".split_once("->"), Some(("a", "b")));
1329+
assert_eq!("a->b->c".split_once("->"), Some(("a", "b->c")));
1330+
assert_eq!("---".split_once("--"), Some(("", "-")));
1331+
}
1332+
1333+
#[test]
1334+
fn test_rsplit_once() {
1335+
assert_eq!("".rsplit_once("->"), None);
1336+
assert_eq!("-".rsplit_once("->"), None);
1337+
assert_eq!("->".rsplit_once("->"), Some(("", "")));
1338+
assert_eq!("a->".rsplit_once("->"), Some(("a", "")));
1339+
assert_eq!("->b".rsplit_once("->"), Some(("", "b")));
1340+
assert_eq!("a->b".rsplit_once("->"), Some(("a", "b")));
1341+
assert_eq!("a->b->c".rsplit_once("->"), Some(("a->b", "c")));
1342+
assert_eq!("---".rsplit_once("--"), Some(("-", "")));
1343+
}
1344+
13211345
#[test]
13221346
fn test_split_whitespace() {
13231347
let data = "\n \tMäry häd\tä little lämb\nLittle lämb\n";

library/core/src/str/mod.rs

+41
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,47 @@ impl str {
36103610
RSplitN(self.splitn(n, pat).0)
36113611
}
36123612

3613+
/// Splits the string on the first occurrence of the specified delimiter and
3614+
/// returns prefix before delimiter and suffix after delimiter.
3615+
///
3616+
/// # Examples
3617+
///
3618+
/// ```
3619+
/// #![feature(str_split_once)]
3620+
///
3621+
/// assert_eq!("cfg".split_once('='), None);
3622+
/// assert_eq!("cfg=foo".split_once('='), Some(("cfg", "foo")));
3623+
/// assert_eq!("cfg=foo=bar".split_once('='), Some(("cfg", "foo=bar")));
3624+
/// ```
3625+
#[unstable(feature = "str_split_once", reason = "newly added", issue = "74773")]
3626+
#[inline]
3627+
pub fn split_once<'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> {
3628+
let (start, end) = delimiter.into_searcher(self).next_match()?;
3629+
Some((&self[..start], &self[end..]))
3630+
}
3631+
3632+
/// Splits the string on the last occurrence of the specified delimiter and
3633+
/// returns prefix before delimiter and suffix after delimiter.
3634+
///
3635+
/// # Examples
3636+
///
3637+
/// ```
3638+
/// #![feature(str_split_once)]
3639+
///
3640+
/// assert_eq!("cfg".rsplit_once('='), None);
3641+
/// assert_eq!("cfg=foo".rsplit_once('='), Some(("cfg", "foo")));
3642+
/// assert_eq!("cfg=foo=bar".rsplit_once('='), Some(("cfg=foo", "bar")));
3643+
/// ```
3644+
#[unstable(feature = "str_split_once", reason = "newly added", issue = "74773")]
3645+
#[inline]
3646+
pub fn rsplit_once<'a, P>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)>
3647+
where
3648+
P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
3649+
{
3650+
let (start, end) = delimiter.into_searcher(self).next_match_back()?;
3651+
Some((&self[..start], &self[end..]))
3652+
}
3653+
36133654
/// An iterator over the disjoint matches of a pattern within the given string
36143655
/// slice.
36153656
///

0 commit comments

Comments
 (0)