Skip to content

Commit 3e36cff

Browse files
committed
find/find_iter now return a Match instead of (usize, usize).
This also removes Captures.{at,pos} and replaces it with Captures.get, which now returns a Match. Similarly, Captures.name returns a Match as well.
1 parent 553fbad commit 3e36cff

12 files changed

+267
-168
lines changed

examples/shootout-regex-dna-cheat.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ fn replace_all(text: &str, substs: Vec<(u8, &str)>) -> String {
7878
let re = regex!(&alternates.join("|"));
7979
let mut new = String::with_capacity(text.len());
8080
let mut last_match = 0;
81-
for (s, e) in re.find_iter(text) {
82-
new.push_str(&text[last_match..s]);
83-
new.push_str(replacements[text.as_bytes()[s] as usize]);
84-
last_match = e;
81+
for m in re.find_iter(text) {
82+
new.push_str(&text[last_match..m.start()]);
83+
new.push_str(replacements[text.as_bytes()[m.start()] as usize]);
84+
last_match = m.end();
8585
}
8686
new.push_str(&text[last_match..]);
8787
new

examples/shootout-regex-dna-single-cheat.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ fn replace_all(text: &str, substs: Vec<(u8, &str)>) -> String {
6363
let re = regex!(&alternates.join("|"));
6464
let mut new = String::with_capacity(text.len());
6565
let mut last_match = 0;
66-
for (s, e) in re.find_iter(text) {
67-
new.push_str(&text[last_match..s]);
68-
new.push_str(replacements[text.as_bytes()[s] as usize]);
69-
last_match = e;
66+
for m in re.find_iter(text) {
67+
new.push_str(&text[last_match..m.start()]);
68+
new.push_str(replacements[text.as_bytes()[m.start()] as usize]);
69+
last_match = m.end();
7070
}
7171
new.push_str(&text[last_match..]);
7272
new

src/expand.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ pub fn expand_str(
3434
};
3535
replacement = &replacement[cap_ref.end..];
3636
match cap_ref.cap {
37-
Ref::Number(i) => dst.push_str(caps.at(i).unwrap_or("")),
38-
Ref::Named(name) => dst.push_str(caps.name(name).unwrap_or("")),
37+
Ref::Number(i) => {
38+
dst.push_str(
39+
caps.get(i).map(|m| m.as_str()).unwrap_or(""));
40+
}
41+
Ref::Named(name) => {
42+
dst.push_str(
43+
caps.name(name).map(|m| m.as_str()).unwrap_or(""));
44+
}
3945
}
4046
}
4147
dst.push_str(replacement);
@@ -70,8 +76,14 @@ pub fn expand_bytes(
7076
};
7177
replacement = &replacement[cap_ref.end..];
7278
match cap_ref.cap {
73-
Ref::Number(i) => dst.extend(caps.at(i).unwrap_or(b"")),
74-
Ref::Named(name) => dst.extend(caps.name(name).unwrap_or(b"")),
79+
Ref::Number(i) => {
80+
dst.extend(
81+
caps.get(i).map(|m| m.as_bytes()).unwrap_or(b""));
82+
}
83+
Ref::Named(name) => {
84+
dst.extend(
85+
caps.name(name).map(|m| m.as_bytes()).unwrap_or(b""));
86+
}
7587
}
7688
}
7789
dst.extend(replacement);

src/lib.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@
107107
//! let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
108108
//! let text = "2012-03-14, 2013-01-01 and 2014-07-05";
109109
//! for cap in re.captures_iter(text) {
110-
//! println!("Month: {} Day: {} Year: {}",
111-
//! cap.at(2).unwrap_or(""), cap.at(3).unwrap_or(""),
112-
//! cap.at(1).unwrap_or(""));
110+
//! println!("Month: {} Day: {} Year: {}", &cap[2], &cap[3], &cap[1]);
113111
//! }
114112
//! // Output:
115113
//! // Month: 03 Day: 14 Year: 2012
@@ -225,7 +223,8 @@
225223
//! # extern crate regex; use regex::Regex;
226224
//! # fn main() {
227225
//! let re = Regex::new(r"(?i)Δ+").unwrap();
228-
//! assert_eq!(re.find("ΔδΔ"), Some((0, 6)));
226+
//! let mat = re.find("ΔδΔ").unwrap();
227+
//! assert_eq!((mat.start(), mat.end()), (0, 6));
229228
//! # }
230229
//! ```
231230
//!
@@ -237,7 +236,8 @@
237236
//! # extern crate regex; use regex::Regex;
238237
//! # fn main() {
239238
//! let re = Regex::new(r"[\pN\p{Greek}\p{Cherokee}]+").unwrap();
240-
//! assert_eq!(re.find("abcΔᎠβⅠᏴγδⅡxyz"), Some((3, 23)));
239+
//! let mat = re.find("abcΔᎠβⅠᏴγδⅡxyz").unwrap();
240+
//! assert_eq!((mat.start(), mat.end()), (3, 23));
241241
//! # }
242242
//! ```
243243
//!
@@ -348,7 +348,7 @@
348348
//! # fn main() {
349349
//! let re = Regex::new(r"(?i)a+(?-i)b+").unwrap();
350350
//! let cap = re.captures("AaAaAbbBBBb").unwrap();
351-
//! assert_eq!(cap.at(0), Some("AaAaAbb"));
351+
//! assert_eq!(&cap[0], "AaAaAbb");
352352
//! # }
353353
//! ```
354354
//!
@@ -363,7 +363,7 @@
363363
//! # fn main() {
364364
//! let re = Regex::new(r"(?-u:\b).+(?-u:\b)").unwrap();
365365
//! let cap = re.captures("$$abc$$").unwrap();
366-
//! assert_eq!(cap.at(0), Some("abc"));
366+
//! assert_eq!(&cap[0], "abc");
367367
//! # }
368368
//! ```
369369
//!
@@ -492,7 +492,7 @@ let text = b"foo\x00bar\x00baz\x00";
492492
// The unwrap is OK here since a match requires the `cstr` capture to match.
493493
let cstrs: Vec<&[u8]> =
494494
re.captures_iter(text)
495-
.map(|c| c.name("cstr").unwrap())
495+
.map(|c| c.name("cstr").unwrap().as_bytes())
496496
.collect();
497497
assert_eq!(vec![&b"foo"[..], &b"bar"[..], &b"baz"[..]], cstrs);
498498
```
@@ -514,10 +514,11 @@ let caps = re.captures(text).unwrap();
514514
// Notice that despite the `.*` at the end, it will only match valid UTF-8
515515
// because Unicode mode was enabled with the `u` flag. Without the `u` flag,
516516
// the `.*` would match the rest of the bytes.
517-
assert_eq!((7, 10), caps.pos(1).unwrap());
517+
let mat = caps.get(1).unwrap();
518+
assert_eq!((7, 10), (mat.start(), mat.end()));
518519
519520
// If there was a match, Unicode mode guarantees that `title` is valid UTF-8.
520-
let title = str::from_utf8(caps.at(1).unwrap()).unwrap();
521+
let title = str::from_utf8(&caps[1]).unwrap();
521522
assert_eq!("☃", title);
522523
```
523524

0 commit comments

Comments
 (0)