Skip to content

add as_str method to more str-iterators #34204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
22 changes: 22 additions & 0 deletions src/libcore/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, position and end have no relation and position > end is a legal state.

},
}
}

#[inline]
fn next(&mut self) -> SearchStep {
match self.searcher {
Expand Down