diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 5fc15fae5199b..04ed9805999e4 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -727,6 +727,17 @@ macro_rules! generate_pattern_iterators { } } + impl<'a, P: Pattern<'a>> $reverse_iterator<'a, P> { + /// View the underlying data as a subslice of the original data. + /// + /// This has the same lifetime as the original slice, and so the + /// iterator can continue to be used while this exists. + $(#[$common_stability_attribute])* + pub fn as_str(&self) -> &'a str { + self.0.as_str() + } + } + generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*, $forward_iterator, $reverse_iterator, $iterty); @@ -817,6 +828,15 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> { } } + #[inline] + fn as_str(&self) -> &'a str { + if self.finished { + "" + } else { + unsafe { self.matcher.haystack().slice_unchecked(self.start, self.end) } + } + } + #[inline] fn next_back(&mut self) -> Option<&'a str> where P::Searcher: ReverseSearcher<'a> @@ -912,6 +932,11 @@ impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> { } } + #[inline] + fn as_str(&self) -> &'a str { + self.iter.as_str() + } + #[inline] fn next_back(&mut self) -> Option<&'a str> where P::Searcher: ReverseSearcher<'a> @@ -965,6 +990,11 @@ impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> { }) } + #[inline] + fn as_str(&self) -> &'a str { + self.0.as_str() + } + #[inline] fn next_back(&mut self) -> Option<(usize, &'a str)> where P::Searcher: ReverseSearcher<'a> @@ -1017,6 +1047,11 @@ impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> { }) } + #[inline] + fn as_str(&self) -> &'a str { + self.0.as_str() + } + #[inline] fn next_back(&mut self) -> Option<&'a str> where P::Searcher: ReverseSearcher<'a> diff --git a/src/libcore/str/pattern.rs b/src/libcore/str/pattern.rs index b803539e12b1a..ee0b7b52109b0 100644 --- a/src/libcore/str/pattern.rs +++ b/src/libcore/str/pattern.rs @@ -108,6 +108,9 @@ pub unsafe trait Searcher<'a> { /// Will always return the same `&str` fn haystack(&self) -> &'a str; + /// Getter for the rest of the iterator + fn as_str(&self) -> &'a str; + /// Performs the next search step starting from the front. /// /// - Returns `Match(a, b)` if `haystack[a..b]` matches the pattern. @@ -305,6 +308,10 @@ unsafe impl<'a, C: CharEq> Searcher<'a> for CharEqSearcher<'a, C> { self.haystack } + fn as_str(&self) -> &'a str { + self.char_indices.as_str() + } + #[inline] fn next(&mut self) -> SearchStep { let s = &mut self.char_indices; @@ -383,6 +390,10 @@ macro_rules! searcher_methods { self.0.haystack() } #[inline] + fn as_str(&self) -> &'a str { + self.0.as_str() + } + #[inline] fn next(&mut self) -> SearchStep { self.0.next() } @@ -596,6 +607,17 @@ impl<'a, 'b> StrSearcher<'a, 'b> { unsafe impl<'a, 'b> Searcher<'a> for StrSearcher<'a, 'b> { fn haystack(&self) -> &'a str { self.haystack } + fn as_str(&self) -> &'a str { + match self.searcher { + StrSearcherImpl::Empty(ref searcher) => { + &self.haystack[searcher.position..searcher.end] + }, + StrSearcherImpl::TwoWay(ref searcher) => { + &self.haystack[searcher.position..searcher.end] + }, + } + } + #[inline] fn next(&mut self) -> SearchStep { match self.searcher {