Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/alloc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![feature(map_first_last)]
#![feature(new_uninit)]
#![feature(pattern)]
#![feature(str_split_once)]
#![feature(trusted_len)]
#![feature(try_reserve)]
#![feature(unboxed_closures)]
Expand Down
24 changes: 24 additions & 0 deletions library/alloc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,30 @@ fn test_rsplitn() {
assert_eq!(split, ["mb\n", "\nMäry häd ä little lämb\nLittle l"]);
}

#[test]
fn test_split_once() {
assert_eq!("".split_once("->"), None);
assert_eq!("-".split_once("->"), None);
assert_eq!("->".split_once("->"), Some(("", "")));
assert_eq!("a->".split_once("->"), Some(("a", "")));
assert_eq!("->b".split_once("->"), Some(("", "b")));
assert_eq!("a->b".split_once("->"), Some(("a", "b")));
assert_eq!("a->b->c".split_once("->"), Some(("a", "b->c")));
assert_eq!("---".split_once("--"), Some(("", "-")));
}

#[test]
fn test_rsplit_once() {
assert_eq!("".rsplit_once("->"), None);
assert_eq!("-".rsplit_once("->"), None);
assert_eq!("->".rsplit_once("->"), Some(("", "")));
assert_eq!("a->".rsplit_once("->"), Some(("a", "")));
assert_eq!("->b".rsplit_once("->"), Some(("", "b")));
assert_eq!("a->b".rsplit_once("->"), Some(("a", "b")));
assert_eq!("a->b->c".rsplit_once("->"), Some(("a->b", "c")));
assert_eq!("---".rsplit_once("--"), Some(("-", "")));
}

#[test]
fn test_split_whitespace() {
let data = "\n \tMäry häd\tä little lämb\nLittle lämb\n";
Expand Down
41 changes: 41 additions & 0 deletions library/core/src/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3610,6 +3610,47 @@ impl str {
RSplitN(self.splitn(n, pat).0)
}

/// Splits the string on the first occurrence of the specified delimiter and
/// returns prefix before delimiter and suffix after delimiter.
///
/// # Examples
///
/// ```
/// #![feature(str_split_once)]
///
/// assert_eq!("cfg".split_once('='), None);
/// assert_eq!("cfg=foo".split_once('='), Some(("cfg", "foo")));
/// assert_eq!("cfg=foo=bar".split_once('='), Some(("cfg", "foo=bar")));
/// ```
#[unstable(feature = "str_split_once", reason = "newly added", issue = "74773")]
#[inline]
pub fn split_once<'a, P: Pattern<'a>>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)> {
let (start, end) = delimiter.into_searcher(self).next_match()?;
Some((&self[..start], &self[end..]))
}

/// Splits the string on the last occurrence of the specified delimiter and
/// returns prefix before delimiter and suffix after delimiter.
///
/// # Examples
///
/// ```
/// #![feature(str_split_once)]
///
/// assert_eq!("cfg".rsplit_once('='), None);
/// assert_eq!("cfg=foo".rsplit_once('='), Some(("cfg", "foo")));
/// assert_eq!("cfg=foo=bar".rsplit_once('='), Some(("cfg=foo", "bar")));
/// ```
#[unstable(feature = "str_split_once", reason = "newly added", issue = "74773")]
#[inline]
pub fn rsplit_once<'a, P>(&'a self, delimiter: P) -> Option<(&'a str, &'a str)>
where
P: Pattern<'a, Searcher: ReverseSearcher<'a>>,
{
let (start, end) = delimiter.into_searcher(self).next_match_back()?;
Some((&self[..start], &self[end..]))
}

/// An iterator over the disjoint matches of a pattern within the given string
/// slice.
///
Expand Down