summaryrefslogtreecommitdiff
path: root/libbindgen/src/regex_set.rs
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2016-11-15 12:48:14 -0600
committerGitHub <noreply@github.com>2016-11-15 12:48:14 -0600
commit6b285e617ede6d3dbd6d2d0129ac81bc992a1593 (patch)
treeff4d1b4d5f44366960c0c5d9621da6cde4cc5468 /libbindgen/src/regex_set.rs
parent6e78bb8d56d875619d20e343d0f3109e2d6b6841 (diff)
parent073b12ff35a8ec6314d655804915f177ce453227 (diff)
Auto merge of #204 - jdub:logless-library, r=emilio
Split off bindgen library into a sub-crate - Unfortunately there's a dependency on log via syntex_errors, so we don't get rid of it all - I can't find a more sensible way to set dependencies based on whether you're building the lib or bin - So `--no-default-features` means you need to know what you're doing, as only the lib will build without the logging crates for now - The replacement log macros are pretty gross too, but they show a proof of concept ;-)
Diffstat (limited to 'libbindgen/src/regex_set.rs')
-rw-r--r--libbindgen/src/regex_set.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/libbindgen/src/regex_set.rs b/libbindgen/src/regex_set.rs
new file mode 100644
index 00000000..93130590
--- /dev/null
+++ b/libbindgen/src/regex_set.rs
@@ -0,0 +1,66 @@
+//! A type that represents the union of a set of regular expressions.
+
+use regex::Regex;
+use std::borrow::Borrow;
+
+// Yeah, I'm aware this is sorta crappy, should be cheaper to compile a regex
+// ORing all the patterns, I guess...
+
+/// A dynamic set of regular expressions.
+#[derive(Debug)]
+pub struct RegexSet {
+ items: Vec<Regex>,
+}
+
+impl RegexSet {
+ /// Is this set empty?
+ pub fn is_empty(&self) -> bool {
+ self.items.is_empty()
+ }
+
+ /// Extend this set with every regex in the iterator.
+ pub fn extend<I>(&mut self, iter: I)
+ where I: IntoIterator<Item = String>,
+ {
+ for s in iter.into_iter() {
+ self.insert(&s)
+ }
+ }
+
+ /// Insert a new regex into this set.
+ pub fn insert<S>(&mut self, string: &S)
+ where S: Borrow<str>,
+ {
+ let s = string.borrow();
+ match Regex::new(&format!("^{}$", s)) {
+ Ok(r) => {
+ self.items.push(r);
+ }
+ Err(err) => {
+ error!("Invalid pattern provided: {}, {:?}", s, err);
+ }
+ }
+ }
+
+ /// Does the given `string` match any of the regexes in this set?
+ pub fn matches<S>(&self, string: &S) -> bool
+ where S: Borrow<str>,
+ {
+ let s = string.borrow();
+ for r in &self.items {
+ if r.is_match(s) {
+ return true;
+ }
+ }
+
+ false
+ }
+}
+
+impl Default for RegexSet {
+ fn default() -> Self {
+ RegexSet {
+ items: vec![],
+ }
+ }
+}