Skip to content

Commit

Permalink
add str::SplitInclusive::as_str method
Browse files Browse the repository at this point in the history
This commit entroduces `core::str::SplitInclusive::as_str` method similar to
`core::str::Split::as_str`, but under different gate -
"str_split_inclusive_as_str" (this is done so because `SplitInclusive` is
itself unstable).
  • Loading branch information
WaffleLapkin committed Oct 1, 2020
1 parent 4747215 commit 076514c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
#![feature(std_internals)]
#![feature(stmt_expr_attributes)]
#![feature(str_split_as_str)]
#![feature(str_split_inclusive_as_str)]
#![feature(transparent_unions)]
#![feature(unboxed_closures)]
#![feature(unsized_locals)]
Expand Down
26 changes: 23 additions & 3 deletions library/core/src/str/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,9 +699,7 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
}

// SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
unsafe {
self.matcher.haystack().get_unchecked(self.start..self.end)
}
unsafe { self.matcher.haystack().get_unchecked(self.start..self.end) }
}
}

Expand Down Expand Up @@ -1278,6 +1276,28 @@ impl<'a, P: Pattern<'a, Searcher: ReverseSearcher<'a>>> DoubleEndedIterator
#[unstable(feature = "split_inclusive", issue = "72360")]
impl<'a, P: Pattern<'a>> FusedIterator for SplitInclusive<'a, P> {}

impl<'a, P: Pattern<'a>> SplitInclusive<'a, P> {
/// Returns remainder of the splitted string
///
/// # Examples
///
/// ```
/// #![feature(str_split_inclusive_as_str)]
/// #![feature(split_inclusive)]
/// let mut split = "Mary had a little lamb".split_inclusive(' ');
/// 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_inclusive_as_str", issue = "none")]
pub fn as_str(&self) -> &'a str {
self.0.as_str()
}
}

/// An iterator of [`u16`] over the string encoded as UTF-16.
///
/// This struct is created by the [`encode_utf16`] method on [`str`].
Expand Down

0 comments on commit 076514c

Please sign in to comment.