about summary refs log tree commit diff
path: root/src/libcore/str
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-09-24 08:38:48 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-09-25 09:29:23 -0700
commitd5f2d3b1773d70bf3d32b94ad2a2bf3125bf743e (patch)
tree58a095c35f177353b81957655b531f3768d91550 /src/libcore/str
parent8fe79bdfdacb2f5914971bd1a0b63b9577afbf6a (diff)
downloadrust-d5f2d3b1773d70bf3d32b94ad2a2bf3125bf743e.tar.gz
rust-d5f2d3b1773d70bf3d32b94ad2a2bf3125bf743e.zip
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/libcore/str')
-rw-r--r--src/libcore/str/mod.rs14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index 69ebcb1ab7e..affa8918884 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -729,15 +729,19 @@ struct MatchIndicesInternal<'a, P: Pattern<'a>>(P::Searcher);
 
 impl<'a, P: Pattern<'a>> MatchIndicesInternal<'a, P> {
     #[inline]
-    fn next(&mut self) -> Option<(usize, usize)> {
-        self.0.next_match()
+    fn next(&mut self) -> Option<(usize, &'a str)> {
+        self.0.next_match().map(|(start, end)| unsafe {
+            (start, self.0.haystack().slice_unchecked(start, end))
+        })
     }
 
     #[inline]
-    fn next_back(&mut self) -> Option<(usize, usize)>
+    fn next_back(&mut self) -> Option<(usize, &'a str)>
         where P::Searcher: ReverseSearcher<'a>
     {
-        self.0.next_match_back()
+        self.0.next_match_back().map(|(start, end)| unsafe {
+            (start, self.0.haystack().slice_unchecked(start, end))
+        })
     }
 }
 
@@ -753,7 +757,7 @@ generate_pattern_iterators! {
                    reason = "type may be removed or have its iterator impl changed",
                    issue = "27743")]
     internal:
-        MatchIndicesInternal yielding ((usize, usize));
+        MatchIndicesInternal yielding ((usize, &'a str));
     delegate double ended;
 }