diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-09-24 08:38:48 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-09-25 09:29:23 -0700 |
| commit | d5f2d3b1773d70bf3d32b94ad2a2bf3125bf743e (patch) | |
| tree | 58a095c35f177353b81957655b531f3768d91550 /src/libcollections | |
| parent | 8fe79bdfdacb2f5914971bd1a0b63b9577afbf6a (diff) | |
std: Update MatchIndices to return a subslice
This commit updates the `MatchIndices` and `RMatchIndices` iterators to follow the same pattern as the `chars` and `char_indices` iterators. The `matches` iterator currently yield `&str` elements, so the `MatchIndices` iterator now yields the index of the match as well as the `&str` that matched (instead of start/end indexes). cc #27743
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/str.rs | 72 |
1 files changed, 30 insertions, 42 deletions
diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 2f74ab5026e..a3d38bdd1b3 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -1157,25 +1157,21 @@ impl str { core_str::StrExt::rmatches(self, pat) } - /// An iterator over the start and end indices of the disjoint matches - /// of a pattern within `self`. + /// An iterator over the disjoint matches of a pattern within `self` as well + /// as the index that the match starts at. /// /// For matches of `pat` within `self` that overlap, only the indices - /// corresponding to the first - /// match are returned. + /// corresponding to the first match are returned. /// - /// The pattern can be a simple `&str`, `char`, or a closure that - /// determines if a character matches. - /// Additional libraries might provide more complex patterns like - /// regular expressions. + /// The pattern can be a simple `&str`, `char`, or a closure that determines + /// if a character matches. Additional libraries might provide more complex + /// patterns like regular expressions. /// /// # Iterator behavior /// /// The returned iterator will be double ended if the pattern allows a - /// reverse search - /// and forward/reverse search yields the same elements. This is true for, - /// eg, `char` but not - /// for `&str`. + /// reverse search and forward/reverse search yields the same elements. This + /// is true for, eg, `char` but not for `&str`. /// /// If the pattern allows a reverse search but its results might differ /// from a forward search, `rmatch_indices()` can be used. @@ -1185,42 +1181,36 @@ impl str { /// ``` /// #![feature(str_match_indices)] /// - /// let v: Vec<(usize, usize)> = "abcXXXabcYYYabc".match_indices("abc").collect(); - /// assert_eq!(v, [(0, 3), (6, 9), (12, 15)]); + /// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect(); + /// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]); /// - /// let v: Vec<(usize, usize)> = "1abcabc2".match_indices("abc").collect(); - /// assert_eq!(v, [(1, 4), (4, 7)]); + /// let v: Vec<_> = "1abcabc2".match_indices("abc").collect(); + /// assert_eq!(v, [(1, "abc"), (4, "abc")]); /// - /// let v: Vec<(usize, usize)> = "ababa".match_indices("aba").collect(); - /// assert_eq!(v, [(0, 3)]); // only the first `aba` + /// let v: Vec<_> = "ababa".match_indices("aba").collect(); + /// assert_eq!(v, [(0, "aba")]); // only the first `aba` /// ``` #[unstable(feature = "str_match_indices", reason = "might have its iterator type changed", issue = "27743")] - // NB: Right now MatchIndices yields `(usize, usize)`, but it would - // be more consistent with `matches` and `char_indices` to return `(usize, &str)` pub fn match_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> MatchIndices<'a, P> { core_str::StrExt::match_indices(self, pat) } - /// An iterator over the start and end indices of the disjoint matches of - /// a pattern within - /// `self`, yielded in reverse order. + /// An iterator over the disjoint matches of a pattern within `self`, + /// yielded in reverse order along with the index of the match. /// /// For matches of `pat` within `self` that overlap, only the indices - /// corresponding to the last - /// match are returned. + /// corresponding to the last match are returned. /// - /// The pattern can be a simple `&str`, `char`, or a closure that - /// determines if a character matches. - /// Additional libraries might provide more complex patterns like - /// regular expressions. + /// The pattern can be a simple `&str`, `char`, or a closure that determines + /// if a character matches. Additional libraries might provide more complex + /// patterns like regular expressions. /// /// # Iterator behavior /// - /// The returned iterator requires that the pattern supports a - /// reverse search, - /// and it will be double ended if a forward/reverse search yields + /// The returned iterator requires that the pattern supports a reverse + /// search, and it will be double ended if a forward/reverse search yields /// the same elements. /// /// For iterating from the front, `match_indices()` can be used. @@ -1230,20 +1220,18 @@ impl str { /// ``` /// #![feature(str_match_indices)] /// - /// let v: Vec<(usize, usize)> = "abcXXXabcYYYabc".rmatch_indices("abc").collect(); - /// assert_eq!(v, [(12, 15), (6, 9), (0, 3)]); + /// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect(); + /// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]); /// - /// let v: Vec<(usize, usize)> = "1abcabc2".rmatch_indices("abc").collect(); - /// assert_eq!(v, [(4, 7), (1, 4)]); + /// let v: Vec<_> = "1abcabc2".rmatch_indices("abc").collect(); + /// assert_eq!(v, [(4, "abc"), (1, "abc")]); /// - /// let v: Vec<(usize, usize)> = "ababa".rmatch_indices("aba").collect(); - /// assert_eq!(v, [(2, 5)]); // only the last `aba` + /// let v: Vec<_> = "ababa".rmatch_indices("aba").collect(); + /// assert_eq!(v, [(2, "aba")]); // only the last `aba` /// ``` #[unstable(feature = "str_match_indices", reason = "might have its iterator type changed", issue = "27743")] - // NB: Right now RMatchIndices yields `(usize, usize)`, but it would - // be more consistent with `rmatches` and `char_indices` to return `(usize, &str)` pub fn rmatch_indices<'a, P: Pattern<'a>>(&'a self, pat: P) -> RMatchIndices<'a, P> where P::Searcher: ReverseSearcher<'a> { @@ -1416,10 +1404,10 @@ impl str { pub fn replace(&self, from: &str, to: &str) -> String { let mut result = String::new(); let mut last_end = 0; - for (start, end) in self.match_indices(from) { + for (start, part) in self.match_indices(from) { result.push_str(unsafe { self.slice_unchecked(last_end, start) }); result.push_str(to); - last_end = end; + last_end = start + part.len(); } result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) }); result |
