From 076514c8a82591547116a7a2212c4de4bdc56f76 Mon Sep 17 00:00:00 2001 From: Waffle Date: Thu, 1 Oct 2020 23:39:20 +0300 Subject: [PATCH] add `str::SplitInclusive::as_str` method 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). --- library/core/src/lib.rs | 1 + library/core/src/str/iter.rs | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index c7588f618e1..8f1826a47e1 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -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)] diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index dc2c2e0168f..2da390b2b2b 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -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) } } } @@ -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`].