Skip to content

Commit 3fcdbf5

Browse files
committed
add as_str method to more str-iterators
1 parent 763f923 commit 3fcdbf5

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/libcore/str/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,17 @@ macro_rules! generate_pattern_iterators {
727727
}
728728
}
729729

730+
impl<'a, P: Pattern<'a>> $reverse_iterator<'a, P> {
731+
/// View the underlying data as a subslice of the original data.
732+
///
733+
/// This has the same lifetime as the original slice, and so the
734+
/// iterator can continue to be used while this exists.
735+
$(#[$common_stability_attribute])*
736+
pub fn as_str(&self) -> &'a str {
737+
self.0.as_str()
738+
}
739+
}
740+
730741
generate_pattern_iterators!($($t)* with $(#[$common_stability_attribute])*,
731742
$forward_iterator,
732743
$reverse_iterator, $iterty);
@@ -817,6 +828,15 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
817828
}
818829
}
819830

831+
#[inline]
832+
fn as_str(&self) -> &'a str {
833+
if self.finished {
834+
""
835+
} else {
836+
unsafe { self.matcher.haystack().slice_unchecked(self.start, self.end) }
837+
}
838+
}
839+
820840
#[inline]
821841
fn next_back(&mut self) -> Option<&'a str>
822842
where P::Searcher: ReverseSearcher<'a>
@@ -912,6 +932,11 @@ impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
912932
}
913933
}
914934

935+
#[inline]
936+
fn as_str(&self) -> &'a str {
937+
self.iter.as_str()
938+
}
939+
915940
#[inline]
916941
fn next_back(&mut self) -> Option<&'a str>
917942
where P::Searcher: ReverseSearcher<'a>
@@ -965,6 +990,11 @@ impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
965990
})
966991
}
967992

993+
#[inline]
994+
fn as_str(&self) -> &'a str {
995+
self.0.as_str()
996+
}
997+
968998
#[inline]
969999
fn next_back(&mut self) -> Option<(usize, &'a str)>
9701000
where P::Searcher: ReverseSearcher<'a>
@@ -1017,6 +1047,11 @@ impl<'a, P: Pattern<'a>> MatchesInternal<'a, P> {
10171047
})
10181048
}
10191049

1050+
#[inline]
1051+
fn as_str(&self) -> &'a str {
1052+
self.0.as_str()
1053+
}
1054+
10201055
#[inline]
10211056
fn next_back(&mut self) -> Option<&'a str>
10221057
where P::Searcher: ReverseSearcher<'a>

src/libcore/str/pattern.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ pub unsafe trait Searcher<'a> {
108108
/// Will always return the same `&str`
109109
fn haystack(&self) -> &'a str;
110110

111+
/// Getter for the rest of the iterator
112+
fn as_str(&self) -> &'a str;
113+
111114
/// Performs the next search step starting from the front.
112115
///
113116
/// - 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> {
305308
self.haystack
306309
}
307310

311+
fn as_str(&self) -> &'a str {
312+
self.char_indices.as_str()
313+
}
314+
308315
#[inline]
309316
fn next(&mut self) -> SearchStep {
310317
let s = &mut self.char_indices;
@@ -383,6 +390,10 @@ macro_rules! searcher_methods {
383390
self.0.haystack()
384391
}
385392
#[inline]
393+
fn as_str(&self) -> &'a str {
394+
self.0.as_str()
395+
}
396+
#[inline]
386397
fn next(&mut self) -> SearchStep {
387398
self.0.next()
388399
}
@@ -596,6 +607,17 @@ impl<'a, 'b> StrSearcher<'a, 'b> {
596607
unsafe impl<'a, 'b> Searcher<'a> for StrSearcher<'a, 'b> {
597608
fn haystack(&self) -> &'a str { self.haystack }
598609

610+
fn as_str(&self) -> &'a str {
611+
match self.searcher {
612+
StrSearcherImpl::Empty(ref searcher) => {
613+
&self.haystack[searcher.position..searcher.end]
614+
},
615+
StrSearcherImpl::TwoWay(ref searcher) => {
616+
&self.haystack[searcher.position..searcher.end]
617+
},
618+
}
619+
}
620+
599621
#[inline]
600622
fn next(&mut self) -> SearchStep {
601623
match self.searcher {

0 commit comments

Comments
 (0)