Skip to content

Commit

Permalink
Add a regex cache (again; lost my work) #56
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed May 16, 2018
1 parent 6d657ef commit 90bfe6b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/mango/io/fortest/fromstr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use mango::io::typ::Reader;
use regex::Regex;
use mango::io::util::REXCACHE;

/// Implementation of [Reader] that reads from a pre-provided string.
/// Mostly for testing purposes.
Expand All @@ -23,7 +23,11 @@ impl Reader for StringReader {
false
}

fn matches(&mut self, pattern: Regex) -> Option<String> {
unimplemented!() // TODO
fn matches(&mut self, subpattern: String) -> Option<String> {
REXCACHE.with(|rl| {
let mut rexlib = rl.borrow_mut();
// let rex = rexlib.make_or_get(subpattern);
});
Option::None // TODO
}
}
2 changes: 2 additions & 0 deletions src/mango/io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod typ;

pub mod fortest;

pub mod util;
2 changes: 1 addition & 1 deletion src/mango/io/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub trait Reader {
fn equals(&mut self, text: &str) -> bool;

/// Checks whether the code from the current position matches a regex pattern.
fn matches(&mut self, pattern: Regex) -> Option<String>;
fn matches(&mut self, subpattern: String) -> Option<String>;
}

pub trait Writer {
Expand Down
20 changes: 20 additions & 0 deletions src/mango/io/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use regex::Regex;
use std::cell::RefCell;
use std::collections::HashMap;

pub struct RegexCache {
cache: HashMap<String, Regex>,
}

impl RegexCache {
// Not public to prevent having more than one instance.
pub fn new() -> Self {
RegexCache {
cache: HashMap::new(),
}
}
}

thread_local! {
pub static REXCACHE: RefCell<RegexCache> = RefCell::new(RegexCache::new())
}

0 comments on commit 90bfe6b

Please sign in to comment.