Skip to content

Commit

Permalink
find/find_iter now return a Match instead of (usize, usize).
Browse files Browse the repository at this point in the history
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.

Fixes #276
  • Loading branch information
BurntSushi committed Dec 30, 2016
1 parent 2805811 commit 384e937
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 167 deletions.
8 changes: 4 additions & 4 deletions examples/shootout-regex-dna-cheat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ fn replace_all(text: &str, substs: Vec<(u8, &str)>) -> String {
let re = regex!(&alternates.join("|"));
let mut new = String::with_capacity(text.len());
let mut last_match = 0;
for (s, e) in re.find_iter(text) {
new.push_str(&text[last_match..s]);
new.push_str(replacements[text.as_bytes()[s] as usize]);
last_match = e;
for m in re.find_iter(text) {
new.push_str(&text[last_match..m.start()]);
new.push_str(replacements[text.as_bytes()[m.start()] as usize]);
last_match = m.end();
}
new.push_str(&text[last_match..]);
new
Expand Down
8 changes: 4 additions & 4 deletions examples/shootout-regex-dna-single-cheat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ fn replace_all(text: &str, substs: Vec<(u8, &str)>) -> String {
let re = regex!(&alternates.join("|"));
let mut new = String::with_capacity(text.len());
let mut last_match = 0;
for (s, e) in re.find_iter(text) {
new.push_str(&text[last_match..s]);
new.push_str(replacements[text.as_bytes()[s] as usize]);
last_match = e;
for m in re.find_iter(text) {
new.push_str(&text[last_match..m.start()]);
new.push_str(replacements[text.as_bytes()[m.start()] as usize]);
last_match = m.end();
}
new.push_str(&text[last_match..]);
new
Expand Down
20 changes: 16 additions & 4 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ pub fn expand_str(
};
replacement = &replacement[cap_ref.end..];
match cap_ref.cap {
Ref::Number(i) => dst.push_str(caps.at(i).unwrap_or("")),
Ref::Named(name) => dst.push_str(caps.name(name).unwrap_or("")),
Ref::Number(i) => {
dst.push_str(
caps.get(i).map(|m| m.as_str()).unwrap_or(""));
}
Ref::Named(name) => {
dst.push_str(
caps.name(name).map(|m| m.as_str()).unwrap_or(""));
}
}
}
dst.push_str(replacement);
Expand Down Expand Up @@ -70,8 +76,14 @@ pub fn expand_bytes(
};
replacement = &replacement[cap_ref.end..];
match cap_ref.cap {
Ref::Number(i) => dst.extend(caps.at(i).unwrap_or(b"")),
Ref::Named(name) => dst.extend(caps.name(name).unwrap_or(b"")),
Ref::Number(i) => {
dst.extend(
caps.get(i).map(|m| m.as_bytes()).unwrap_or(b""));
}
Ref::Named(name) => {
dst.extend(
caps.name(name).map(|m| m.as_bytes()).unwrap_or(b""));
}
}
}
dst.extend(replacement);
Expand Down
23 changes: 12 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@
//! let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
//! let text = "2012-03-14, 2013-01-01 and 2014-07-05";
//! for cap in re.captures_iter(text) {
//! println!("Month: {} Day: {} Year: {}",
//! cap.at(2).unwrap_or(""), cap.at(3).unwrap_or(""),
//! cap.at(1).unwrap_or(""));
//! println!("Month: {} Day: {} Year: {}", &cap[2], &cap[3], &cap[1]);
//! }
//! // Output:
//! // Month: 03 Day: 14 Year: 2012
Expand Down Expand Up @@ -225,7 +223,8 @@
//! # extern crate regex; use regex::Regex;
//! # fn main() {
//! let re = Regex::new(r"(?i)Δ+").unwrap();
//! assert_eq!(re.find("ΔδΔ"), Some((0, 6)));
//! let mat = re.find("ΔδΔ").unwrap();
//! assert_eq!((mat.start(), mat.end()), (0, 6));
//! # }
//! ```
//!
Expand All @@ -237,7 +236,8 @@
//! # extern crate regex; use regex::Regex;
//! # fn main() {
//! let re = Regex::new(r"[\pN\p{Greek}\p{Cherokee}]+").unwrap();
//! assert_eq!(re.find("abcΔᎠβⅠᏴγδⅡxyz"), Some((3, 23)));
//! let mat = re.find("abcΔᎠβⅠᏴγδⅡxyz").unwrap();
//! assert_eq!((mat.start(), mat.end()), (3, 23));
//! # }
//! ```
//!
Expand Down Expand Up @@ -348,7 +348,7 @@
//! # fn main() {
//! let re = Regex::new(r"(?i)a+(?-i)b+").unwrap();
//! let cap = re.captures("AaAaAbbBBBb").unwrap();
//! assert_eq!(cap.at(0), Some("AaAaAbb"));
//! assert_eq!(&cap[0], "AaAaAbb");
//! # }
//! ```
//!
Expand All @@ -363,7 +363,7 @@
//! # fn main() {
//! let re = Regex::new(r"(?-u:\b).+(?-u:\b)").unwrap();
//! let cap = re.captures("$$abc$$").unwrap();
//! assert_eq!(cap.at(0), Some("abc"));
//! assert_eq!(&cap[0], "abc");
//! # }
//! ```
//!
Expand Down Expand Up @@ -462,7 +462,7 @@ pub use re_builder::unicode::*;
pub use re_set::unicode::*;
pub use re_trait::{Locations, SubCapturesPosIter};
pub use re_unicode::{
Regex, Captures, SubCapturesIter, SubCapturesNamedIter,
Regex, Match, Captures, SubCapturesIter, SubCapturesNamedIter,
CaptureNamesIter, CapturesIter, FindIter,
Replacer, NoExpand, SplitsIter, SplitsNIter,
quote,
Expand Down Expand Up @@ -492,7 +492,7 @@ let text = b"foo\x00bar\x00baz\x00";
// The unwrap is OK here since a match requires the `cstr` capture to match.
let cstrs: Vec<&[u8]> =
re.captures_iter(text)
.map(|c| c.name("cstr").unwrap())
.map(|c| c.name("cstr").unwrap().as_bytes())
.collect();
assert_eq!(vec![&b"foo"[..], &b"bar"[..], &b"baz"[..]], cstrs);
```
Expand All @@ -514,10 +514,11 @@ let caps = re.captures(text).unwrap();
// Notice that despite the `.*` at the end, it will only match valid UTF-8
// because Unicode mode was enabled with the `u` flag. Without the `u` flag,
// the `.*` would match the rest of the bytes.
assert_eq!((7, 10), caps.pos(1).unwrap());
let mat = caps.get(1).unwrap();
assert_eq!((7, 10), (mat.start(), mat.end()));
// If there was a match, Unicode mode guarantees that `title` is valid UTF-8.
let title = str::from_utf8(caps.at(1).unwrap()).unwrap();
let title = str::from_utf8(&caps[1]).unwrap();
assert_eq!("☃", title);
```
Expand Down
Loading

0 comments on commit 384e937

Please sign in to comment.