Skip to content

Commit 13ea906

Browse files
committed
Made match_indices use the generic pattern API
1 parent bc09c1d commit 13ea906

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

src/libcore/str/mod.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use raw::{Repr, Slice};
3535
use result::Result::{self, Ok, Err};
3636
use slice::{self, SliceExt};
3737
use usize;
38+
use clone::Clone;
3839

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

@@ -933,41 +934,48 @@ impl Searcher {
933934

934935
#[derive(Clone)]
935936
#[unstable(feature = "core", reason = "type may be removed")]
936-
struct OldMatchIndices<'a> {
937+
struct OldMatchIndices<'a, 'b> {
937938
// constants
938939
haystack: &'a str,
939-
needle: &'a str,
940+
needle: &'b str,
940941
searcher: Searcher
941942
}
942943

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

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

953953
#[inline]
954954
fn next(&mut self) -> Option<(uint, uint)> {
955-
self.0.next()
955+
Matcher::next(&mut self.0)
956956
}
957957
}
958958

959959
/// An iterator over the substrings of a string separated by a given
960960
/// search string
961-
#[derive(Clone)]
962961
#[unstable(feature = "core", reason = "type may be removed")]
963-
pub struct SplitStr<'a> {
964-
it: OldMatchIndices<'a>,
962+
pub struct SplitStr<'a, 'b> {
963+
it: pattern::StrMatcher<'a, 'b>,
965964
last_end: uint,
966965
finished: bool
967966
}
968967

968+
impl<'a, 'b> Clone for SplitStr<'a, 'b> {
969+
fn clone(&self) -> Self {
970+
SplitStr {
971+
it: Clone::clone(&self.it),
972+
..*self
973+
}
974+
}
975+
}
976+
969977
#[stable(feature = "rust1", since = "1.0.0")]
970-
impl<'a> Iterator for OldMatchIndices<'a> {
978+
impl<'a, 'b> Iterator for OldMatchIndices<'a, 'b> {
971979
type Item = (uint, uint);
972980

973981
#[inline]
@@ -998,22 +1006,22 @@ impl<'a> Iterator for OldMatchIndices<'a> {
9981006
}
9991007

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

10041012
#[inline]
10051013
fn next(&mut self) -> Option<&'a str> {
10061014
if self.finished { return None; }
1007-
1008-
match self.it.next() {
1015+
let haystack = Matcher::haystack(&self.it);
1016+
match Matcher::next(&mut self.it) {
10091017
Some((from, to)) => {
1010-
let ret = Some(&self.it.haystack[self.last_end .. from]);
1018+
let ret = Some(&haystack[self.last_end..from]);
10111019
self.last_end = to;
10121020
ret
10131021
}
10141022
None => {
10151023
self.finished = true;
1016-
Some(&self.it.haystack[self.last_end .. self.it.haystack.len()])
1024+
Some(&haystack[self.last_end..])
10171025
}
10181026
}
10191027
}
@@ -1375,8 +1383,8 @@ pub trait StrExt {
13751383
fn splitn<'a, P: CharEq>(&'a self, count: uint, pat: P) -> SplitN<'a, P>;
13761384
fn split_terminator<'a, P: CharEq>(&'a self, pat: P) -> SplitTerminator<'a, P>;
13771385
fn rsplitn<'a, P: CharEq>(&'a self, count: uint, pat: P) -> RSplitN<'a, P>;
1378-
fn match_indices<'a>(&'a self, sep: &'a str) -> MatchIndices<'a>;
1379-
fn split_str<'a>(&'a self, pat: &'a str) -> SplitStr<'a>;
1386+
fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P>;
1387+
fn split_str<'a, 'b>(&'a self, pat: &'b str) -> SplitStr<'a, 'b>;
13801388
fn lines<'a>(&'a self) -> Lines<'a>;
13811389
fn lines_any<'a>(&'a self) -> LinesAny<'a>;
13821390
fn char_len(&self) -> uint;
@@ -1478,16 +1486,12 @@ impl StrExt for str {
14781486
}
14791487

14801488
#[inline]
1481-
fn match_indices<'a>(&'a self, sep: &'a str) -> MatchIndices<'a> {
1482-
MatchIndices(OldMatchIndices {
1483-
haystack: self,
1484-
needle: sep,
1485-
searcher: Searcher::new(self.as_bytes(), sep.as_bytes())
1486-
})
1489+
fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> {
1490+
MatchIndices(pat.into_matcher(self))
14871491
}
14881492

14891493
#[inline]
1490-
fn split_str<'a>(&'a self, sep: &'a str) -> SplitStr<'a> {
1494+
fn split_str<'a, 'b>(&'a self, sep: &'b str) -> SplitStr<'a, 'b> {
14911495
SplitStr {
14921496
it: self.match_indices(sep).0,
14931497
last_end: 0,

src/libcore/str/pattern.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,14 @@ impl<'a, C: CharEq> DoubleEndedMatcher<'a> for CharEqMatcher<'a, C> {}
8484

8585
// Impl for &str
8686

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

89-
impl<'a> Pattern<'a> for &'a str {
90-
type Matcher = StrMatcher<'a>;
90+
impl<'a, 'b> Pattern<'a> for &'b str {
91+
type Matcher = StrMatcher<'a, 'b>;
9192

9293
#[inline]
93-
fn into_matcher(self, haystack: &'a str) -> StrMatcher<'a> {
94+
fn into_matcher(self, haystack: &'a str) -> StrMatcher<'a, 'b> {
9495
let mi = super::OldMatchIndices {
9596
haystack: haystack,
9697
needle: self,
@@ -100,7 +101,7 @@ impl<'a> Pattern<'a> for &'a str {
100101
}
101102
}
102103

103-
unsafe impl<'a> Matcher<'a> for StrMatcher<'a> {
104+
unsafe impl<'a, 'b> Matcher<'a> for StrMatcher<'a, 'b> {
104105
#[inline]
105106
fn haystack(&self) -> &'a str {
106107
self.0.haystack

0 commit comments

Comments
 (0)