summaryrefslogtreecommitdiff
path: root/libbindgen/src/regex_set.rs
diff options
context:
space:
mode:
authorJeremy Fitzhardinge <jsgf@fb.com>2017-01-05 23:59:47 -0800
committerJeremy Fitzhardinge <jsgf@fb.com>2017-01-06 15:16:54 -0800
commit51f5162f250a5ec10bc5ba8b91b60b45762bc999 (patch)
tree6d3a87c900b8eab0257c6889673caac2d334e47f /libbindgen/src/regex_set.rs
parent40254959b2e009de30836faf4edf7fecae34cc7f (diff)
Update to regex 0.2; use regex::RegexSet for matching
Rather than implementing a private RegexSet, implement it in terms of regex::RegexSet, which should be more efficient. Retain the existing RegexSet module and just reimplement its internals, in order to preserve the external API.
Diffstat (limited to 'libbindgen/src/regex_set.rs')
-rw-r--r--libbindgen/src/regex_set.rs55
1 files changed, 29 insertions, 26 deletions
diff --git a/libbindgen/src/regex_set.rs b/libbindgen/src/regex_set.rs
index 8747d285..dbdb6565 100644
--- a/libbindgen/src/regex_set.rs
+++ b/libbindgen/src/regex_set.rs
@@ -1,7 +1,6 @@
//! A type that represents the union of a set of regular expressions.
-use regex::Regex;
-use std::borrow::Borrow;
+use regex::RegexSet as RxSet;
// Yeah, I'm aware this is sorta crappy, should be cheaper to compile a regex
// ORing all the patterns, I guess...
@@ -9,7 +8,8 @@ use std::borrow::Borrow;
/// A dynamic set of regular expressions.
#[derive(Debug)]
pub struct RegexSet {
- items: Vec<Regex>,
+ items: Vec<String>,
+ set: Option<RxSet>,
}
impl RegexSet {
@@ -19,41 +19,43 @@ impl RegexSet {
}
/// Extend this set with every regex in the iterator.
- pub fn extend<I>(&mut self, iter: I)
- where I: IntoIterator<Item = String>,
+ pub fn extend<I, S>(&mut self, iter: I)
+ where I: IntoIterator<Item = S>,
+ S: AsRef<str>
{
for s in iter.into_iter() {
- self.insert(&s)
+ self.insert(s)
}
}
/// Insert a new regex into this set.
- pub fn insert<S>(&mut self, string: &S)
- where S: Borrow<str>,
+ pub fn insert<S>(&mut self, string: S)
+ where S: AsRef<str>
{
- let s = string.borrow();
- match Regex::new(&format!("^{}$", s)) {
- Ok(r) => {
- self.items.push(r);
- }
- Err(err) => {
- warn!("Invalid pattern provided: {}, {:?}", s, err);
- }
+ self.items.push(format!("^{}$", string.as_ref()));
+ self.set = None;
+ }
+
+ /// Construct a RegexSet from the set of entries we've accumulated.
+ ///
+ /// Must be called before calling `matches()`, or it will always return
+ /// false.
+ pub fn build(&mut self) {
+ self.set = match RxSet::new(&self.items) {
+ Ok(x) => Some(x),
+ Err(e) => {
+ error!("Invalid regex in {:?}: {:?}", self.items, e);
+ None
+ },
}
}
/// Does the given `string` match any of the regexes in this set?
- pub fn matches<S>(&self, string: &S) -> bool
- where S: Borrow<str>,
+ pub fn matches<S>(&self, string: S) -> bool
+ where S: AsRef<str>
{
- let s = string.borrow();
- for r in &self.items {
- if r.is_match(s) {
- return true;
- }
- }
-
- false
+ let s = string.as_ref();
+ self.set.as_ref().map(|set| set.is_match(s)).unwrap_or(false)
}
}
@@ -61,6 +63,7 @@ impl Default for RegexSet {
fn default() -> Self {
RegexSet {
items: vec![],
+ set: None,
}
}
}