Skip to content

Commit 0b923d3

Browse files
committed
add str::{Split,RSplit}::as_str methods
This commit introduses 2 methods under "str_split_as_str" gate with common signature of `&Split<'a, _> -> &'a str'`. Both of them work like `Chars::as_str` - return unyield part of the inner string.
1 parent 9cba260 commit 0b923d3

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#![feature(staged_api)]
126126
#![feature(std_internals)]
127127
#![feature(stmt_expr_attributes)]
128+
#![feature(str_split_as_str)]
128129
#![feature(transparent_unions)]
129130
#![feature(unboxed_closures)]
130131
#![feature(unsized_locals)]

library/core/src/str/iter.rs

+55
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,19 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
690690
},
691691
}
692692
}
693+
694+
#[inline]
695+
fn as_str(&self) -> &'a str {
696+
// `Self::get_end` doesn't change `self.start`
697+
if self.finished {
698+
return "";
699+
}
700+
701+
// SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
702+
unsafe {
703+
self.matcher.haystack().get_unchecked(self.start..self.end)
704+
}
705+
}
693706
}
694707

695708
generate_pattern_iterators! {
@@ -710,6 +723,48 @@ generate_pattern_iterators! {
710723
delegate double ended;
711724
}
712725

726+
impl<'a, P: Pattern<'a>> Split<'a, P> {
727+
/// Returns remainder of the splitted string
728+
///
729+
/// # Examples
730+
///
731+
/// ```
732+
/// #![feature(str_split_as_str)]
733+
/// let mut split = "Mary had a little lamb".split(' ');
734+
/// assert_eq!(split.as_str(), "Mary had a little lamb");
735+
/// split.next();
736+
/// assert_eq!(split.as_str(), "had a little lamb");
737+
/// split.by_ref().for_each(drop);
738+
/// assert_eq!(split.as_str(), "");
739+
/// ```
740+
#[inline]
741+
#[unstable(feature = "str_split_as_str", issue = "none")]
742+
pub fn as_str(&self) -> &'a str {
743+
self.0.as_str()
744+
}
745+
}
746+
747+
impl<'a, P: Pattern<'a>> RSplit<'a, P> {
748+
/// Returns remainder of the splitted string
749+
///
750+
/// # Examples
751+
///
752+
/// ```
753+
/// #![feature(str_split_as_str)]
754+
/// let mut split = "Mary had a little lamb".rsplit(' ');
755+
/// assert_eq!(split.as_str(), "Mary had a little lamb");
756+
/// split.next();
757+
/// assert_eq!(split.as_str(), "Mary had a little");
758+
/// split.by_ref().for_each(drop);
759+
/// assert_eq!(split.as_str(), "");
760+
/// ```
761+
#[inline]
762+
#[unstable(feature = "str_split_as_str", issue = "none")]
763+
pub fn as_str(&self) -> &'a str {
764+
self.0.as_str()
765+
}
766+
}
767+
713768
generate_pattern_iterators! {
714769
forward:
715770
/// Created with the method [`split_terminator`].

0 commit comments

Comments
 (0)