Skip to content

Commit

Permalink
Made match_indices use the generic pattern API
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimundi committed Feb 19, 2015
1 parent bc09c1d commit 13ea906
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
52 changes: 28 additions & 24 deletions src/libcore/str/mod.rs
Expand Up @@ -35,6 +35,7 @@ use raw::{Repr, Slice};
use result::Result::{self, Ok, Err};
use slice::{self, SliceExt};
use usize;
use clone::Clone;

pub use self::pattern::{Pattern, Matcher, ReverseMatcher, DoubleEndedMatcher};

Expand Down Expand Up @@ -933,41 +934,48 @@ impl Searcher {

#[derive(Clone)]
#[unstable(feature = "core", reason = "type may be removed")]
struct OldMatchIndices<'a> {
struct OldMatchIndices<'a, 'b> {
// constants
haystack: &'a str,
needle: &'a str,
needle: &'b str,
searcher: Searcher
}

/// An iterator over the start and end indices of the matches of a
/// substring within a larger string
#[derive(Clone)]
#[unstable(feature = "core", reason = "type may be removed")]
pub struct MatchIndices<'a>(OldMatchIndices<'a>);
pub struct MatchIndices<'a, P: Pattern<'a>>(P::Matcher);

#[stable]
impl<'a> Iterator for MatchIndices<'a> {
impl<'a, P: Pattern<'a>> Iterator for MatchIndices<'a, P> {
type Item = (uint, uint);

#[inline]
fn next(&mut self) -> Option<(uint, uint)> {
self.0.next()
Matcher::next(&mut self.0)
}
}

/// An iterator over the substrings of a string separated by a given
/// search string
#[derive(Clone)]
#[unstable(feature = "core", reason = "type may be removed")]
pub struct SplitStr<'a> {
it: OldMatchIndices<'a>,
pub struct SplitStr<'a, 'b> {
it: pattern::StrMatcher<'a, 'b>,
last_end: uint,
finished: bool
}

impl<'a, 'b> Clone for SplitStr<'a, 'b> {
fn clone(&self) -> Self {
SplitStr {
it: Clone::clone(&self.it),
..*self
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Iterator for OldMatchIndices<'a> {
impl<'a, 'b> Iterator for OldMatchIndices<'a, 'b> {
type Item = (uint, uint);

#[inline]
Expand Down Expand Up @@ -998,22 +1006,22 @@ impl<'a> Iterator for OldMatchIndices<'a> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Iterator for SplitStr<'a> {
impl<'a, 'b> Iterator for SplitStr<'a, 'b> {
type Item = &'a str;

#[inline]
fn next(&mut self) -> Option<&'a str> {
if self.finished { return None; }

match self.it.next() {
let haystack = Matcher::haystack(&self.it);
match Matcher::next(&mut self.it) {
Some((from, to)) => {
let ret = Some(&self.it.haystack[self.last_end .. from]);
let ret = Some(&haystack[self.last_end..from]);
self.last_end = to;
ret
}
None => {
self.finished = true;
Some(&self.it.haystack[self.last_end .. self.it.haystack.len()])
Some(&haystack[self.last_end..])
}
}
}
Expand Down Expand Up @@ -1375,8 +1383,8 @@ pub trait StrExt {
fn splitn<'a, P: CharEq>(&'a self, count: uint, pat: P) -> SplitN<'a, P>;
fn split_terminator<'a, P: CharEq>(&'a self, pat: P) -> SplitTerminator<'a, P>;
fn rsplitn<'a, P: CharEq>(&'a self, count: uint, pat: P) -> RSplitN<'a, P>;
fn match_indices<'a>(&'a self, sep: &'a str) -> MatchIndices<'a>;
fn split_str<'a>(&'a self, pat: &'a str) -> SplitStr<'a>;
fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P>;
fn split_str<'a, 'b>(&'a self, pat: &'b str) -> SplitStr<'a, 'b>;
fn lines<'a>(&'a self) -> Lines<'a>;
fn lines_any<'a>(&'a self) -> LinesAny<'a>;
fn char_len(&self) -> uint;
Expand Down Expand Up @@ -1478,16 +1486,12 @@ impl StrExt for str {
}

#[inline]
fn match_indices<'a>(&'a self, sep: &'a str) -> MatchIndices<'a> {
MatchIndices(OldMatchIndices {
haystack: self,
needle: sep,
searcher: Searcher::new(self.as_bytes(), sep.as_bytes())
})
fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
MatchIndices(pat.into_matcher(self))
}

#[inline]
fn split_str<'a>(&'a self, sep: &'a str) -> SplitStr<'a> {
fn split_str<'a, 'b>(&'a self, sep: &'b str) -> SplitStr<'a, 'b> {
SplitStr {
it: self.match_indices(sep).0,
last_end: 0,
Expand Down
11 changes: 6 additions & 5 deletions src/libcore/str/pattern.rs
Expand Up @@ -84,13 +84,14 @@ impl<'a, C: CharEq> DoubleEndedMatcher<'a> for CharEqMatcher<'a, C> {}

// Impl for &str

struct StrMatcher<'a>(super::OldMatchIndices<'a>);
#[derive(Clone)]
pub struct StrMatcher<'a, 'b>(super::OldMatchIndices<'a, 'b>);

impl<'a> Pattern<'a> for &'a str {
type Matcher = StrMatcher<'a>;
impl<'a, 'b> Pattern<'a> for &'b str {
type Matcher = StrMatcher<'a, 'b>;

#[inline]
fn into_matcher(self, haystack: &'a str) -> StrMatcher<'a> {
fn into_matcher(self, haystack: &'a str) -> StrMatcher<'a, 'b> {
let mi = super::OldMatchIndices {
haystack: haystack,
needle: self,
Expand All @@ -100,7 +101,7 @@ impl<'a> Pattern<'a> for &'a str {
}
}

unsafe impl<'a> Matcher<'a> for StrMatcher<'a> {
unsafe impl<'a, 'b> Matcher<'a> for StrMatcher<'a, 'b> {
#[inline]
fn haystack(&self) -> &'a str {
self.0.haystack
Expand Down

0 comments on commit 13ea906

Please sign in to comment.