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.
This commit is contained in:
parent
9cba260df0
commit
0b923d3ca0
|
@ -125,6 +125,7 @@
|
||||||
#![feature(staged_api)]
|
#![feature(staged_api)]
|
||||||
#![feature(std_internals)]
|
#![feature(std_internals)]
|
||||||
#![feature(stmt_expr_attributes)]
|
#![feature(stmt_expr_attributes)]
|
||||||
|
#![feature(str_split_as_str)]
|
||||||
#![feature(transparent_unions)]
|
#![feature(transparent_unions)]
|
||||||
#![feature(unboxed_closures)]
|
#![feature(unboxed_closures)]
|
||||||
#![feature(unsized_locals)]
|
#![feature(unsized_locals)]
|
||||||
|
|
|
@ -690,6 +690,19 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn as_str(&self) -> &'a str {
|
||||||
|
// `Self::get_end` doesn't change `self.start`
|
||||||
|
if self.finished {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
|
||||||
|
unsafe {
|
||||||
|
self.matcher.haystack().get_unchecked(self.start..self.end)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_pattern_iterators! {
|
generate_pattern_iterators! {
|
||||||
|
@ -710,6 +723,48 @@ generate_pattern_iterators! {
|
||||||
delegate double ended;
|
delegate double ended;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, P: Pattern<'a>> Split<'a, P> {
|
||||||
|
/// Returns remainder of the splitted string
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #![feature(str_split_as_str)]
|
||||||
|
/// let mut split = "Mary had a little lamb".split(' ');
|
||||||
|
/// assert_eq!(split.as_str(), "Mary had a little lamb");
|
||||||
|
/// split.next();
|
||||||
|
/// assert_eq!(split.as_str(), "had a little lamb");
|
||||||
|
/// split.by_ref().for_each(drop);
|
||||||
|
/// assert_eq!(split.as_str(), "");
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
#[unstable(feature = "str_split_as_str", issue = "none")]
|
||||||
|
pub fn as_str(&self) -> &'a str {
|
||||||
|
self.0.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, P: Pattern<'a>> RSplit<'a, P> {
|
||||||
|
/// Returns remainder of the splitted string
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #![feature(str_split_as_str)]
|
||||||
|
/// let mut split = "Mary had a little lamb".rsplit(' ');
|
||||||
|
/// assert_eq!(split.as_str(), "Mary had a little lamb");
|
||||||
|
/// split.next();
|
||||||
|
/// assert_eq!(split.as_str(), "Mary had a little");
|
||||||
|
/// split.by_ref().for_each(drop);
|
||||||
|
/// assert_eq!(split.as_str(), "");
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
#[unstable(feature = "str_split_as_str", issue = "none")]
|
||||||
|
pub fn as_str(&self) -> &'a str {
|
||||||
|
self.0.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
generate_pattern_iterators! {
|
generate_pattern_iterators! {
|
||||||
forward:
|
forward:
|
||||||
/// Created with the method [`split_terminator`].
|
/// Created with the method [`split_terminator`].
|
||||||
|
|
Loading…
Reference in New Issue