mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-30 18:53:39 +00:00
Add as_str
method for split whitespace str iterators
This commit adds `as_str` methods to `SplitWhitespace` and `SplitAsciiWhitespace` str iterators. The methods return the remainder, similar to `as_str` methods on `Chars` and other split iterators. This commit also makes fields of some iterators `pub(crate)`.
This commit is contained in:
parent
cecdb181ad
commit
12d6238f4d
@ -13,7 +13,8 @@ use crate::ops::Try;
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[derive(Clone)]
|
||||
pub struct Filter<I, P> {
|
||||
iter: I,
|
||||
// Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods
|
||||
pub(crate) iter: I,
|
||||
predicate: P,
|
||||
}
|
||||
impl<I, P> Filter<I, P> {
|
||||
|
@ -57,7 +57,8 @@ use crate::ops::Try;
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[derive(Clone)]
|
||||
pub struct Map<I, F> {
|
||||
iter: I,
|
||||
// Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods
|
||||
pub(crate) iter: I,
|
||||
f: F,
|
||||
}
|
||||
|
||||
|
@ -335,9 +335,11 @@ pub struct Split<'a, T: 'a, P>
|
||||
where
|
||||
P: FnMut(&T) -> bool,
|
||||
{
|
||||
v: &'a [T],
|
||||
// Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods
|
||||
pub(crate) v: &'a [T],
|
||||
pred: P,
|
||||
finished: bool,
|
||||
// Used for `SplitAsciiWhitespace` `as_str` method
|
||||
pub(crate) finished: bool,
|
||||
}
|
||||
|
||||
impl<'a, T: 'a, P: FnMut(&T) -> bool> Split<'a, T, P> {
|
||||
|
@ -1203,6 +1203,30 @@ impl<'a> DoubleEndedIterator for SplitWhitespace<'a> {
|
||||
#[stable(feature = "fused", since = "1.26.0")]
|
||||
impl FusedIterator for SplitWhitespace<'_> {}
|
||||
|
||||
impl<'a> SplitWhitespace<'a> {
|
||||
/// Returns remainder of the splitted string
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(str_split_whitespace_as_str)]
|
||||
///
|
||||
/// let mut split = "Mary had a little lamb".split_whitespace();
|
||||
/// 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_whitespace_as_str", issue = "77998")]
|
||||
pub fn as_str(&self) -> &'a str {
|
||||
self.inner.iter.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
|
||||
impl<'a> Iterator for SplitAsciiWhitespace<'a> {
|
||||
type Item = &'a str;
|
||||
@ -1234,6 +1258,37 @@ impl<'a> DoubleEndedIterator for SplitAsciiWhitespace<'a> {
|
||||
#[stable(feature = "split_ascii_whitespace", since = "1.34.0")]
|
||||
impl FusedIterator for SplitAsciiWhitespace<'_> {}
|
||||
|
||||
impl<'a> SplitAsciiWhitespace<'a> {
|
||||
/// Returns remainder of the splitted string
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(str_split_whitespace_as_str)]
|
||||
///
|
||||
/// let mut split = "Mary had a little lamb".split_ascii_whitespace();
|
||||
/// 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_whitespace_as_str", issue = "77998")]
|
||||
pub fn as_str(&self) -> &'a str {
|
||||
if self.inner.iter.iter.finished {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Safety:
|
||||
//
|
||||
// Slice is created from str.
|
||||
unsafe { crate::str::from_utf8_unchecked(&self.inner.iter.iter.v) }
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "split_inclusive", since = "1.51.0")]
|
||||
impl<'a, P: Pattern<'a>> Iterator for SplitInclusive<'a, P> {
|
||||
type Item = &'a str;
|
||||
|
Loading…
Reference in New Issue
Block a user