Skip to content

Commit ab13eff

Browse files
committed
Replace regex_macros with lazy_static
Since rust-lang/regex#164 the "dynamic" regex generated is faster than what the `regex!` macro produces. Still, to avoid the overhead of recreating the regex everytime, we use lazy_static to initialize it once at first use
1 parent c67afc7 commit ab13eff

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ core = []
2525
nightly = []
2626
default = ["stream"]
2727
regexp = ["regex"]
28-
regexp_macros = ["regexp", "regex_macros"]
28+
regexp_macros = ["regexp", "lazy_static"]
2929
stream = []
3030

3131
[dependencies.regex]
3232
version = "^0.1.41"
3333
optional = true
3434

35-
[dependencies.regex_macros]
36-
version = "^0.1.21"
35+
[dependencies.lazy_static]
36+
version = "^0.1.15"
3737
optional = true
3838

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@
8787
#![cfg_attr(feature = "core", feature(no_std))]
8888
#![cfg_attr(feature = "core", feature(collections))]
8989
#![cfg_attr(feature = "core", no_std)]
90-
#![cfg_attr(feature = "regexp_macros", feature(plugin))]
91-
#![cfg_attr(feature = "regexp_macros", plugin(regex_macros))]
9290
#![cfg_attr(feature = "nightly", feature(test))]
9391

9492
#[cfg(feature = "core")]
9593
extern crate collections;
9694
#[cfg(feature = "regexp")]
9795
extern crate regex;
96+
#[cfg(feature = "regexp_macros")]
97+
#[macro_use] extern crate lazy_static;
9898
#[cfg(feature = "nightly")]
9999
extern crate test;
100100

src/regexp.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
#[doc(hidden)]
2+
#[macro_export]
3+
macro_rules! regex (
4+
($re: ident, $s:expr) => (
5+
lazy_static! {
6+
static ref $re: ::regex::Regex = ::regex::Regex::new($s).unwrap();
7+
}
8+
);
9+
);
10+
11+
112
/// `re_match!(regexp) => &[T] -> IResult<&[T], &[T]>`
213
/// Returns the whole input if a match is found
314
///
@@ -27,8 +38,8 @@ macro_rules! re_match_static (
2738
($i:expr, $re:expr) => (
2839
{
2940
use $crate::InputLength;
30-
let re = regex!($re);
31-
if re.is_match($i) {
41+
regex!(RE, $re);
42+
if RE.is_match($i) {
3243
$crate::IResult::Done(&$i[$i.input_len()..], $i)
3344
} else {
3445
$crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch))
@@ -65,8 +76,8 @@ macro_rules! re_find (
6576
macro_rules! re_find_static (
6677
($i:expr, $re:expr) => (
6778
{
68-
let re = regex!($re);
69-
if let Some((begin, end)) = re.find($i) {
79+
regex!(RE, $re);
80+
if let Some((begin, end)) = RE.find($i) {
7081
$crate::IResult::Done(&$i[end..], &$i[begin..end])
7182
} else {
7283
$crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind))
@@ -108,8 +119,8 @@ macro_rules! re_matches (
108119
macro_rules! re_matches_static (
109120
($i:expr, $re:expr) => (
110121
{
111-
let re = regex!($re);
112-
let v: Vec<&str> = re.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect();
122+
regex!(RE, $re);
123+
let v: Vec<&str> = RE.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect();
113124
if v.len() != 0 {
114125
let offset = {
115126
let end = v.last().unwrap();
@@ -155,8 +166,8 @@ macro_rules! re_capture (
155166
macro_rules! re_capture_static (
156167
($i:expr, $re:expr) => (
157168
{
158-
let re = regex!($re);
159-
if let Some(c) = re.captures($i) {
169+
regex!(RE, $re);
170+
if let Some(c) = RE.captures($i) {
160171
let v:Vec<&str> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect();
161172
let offset = {
162173
let end = v.last().unwrap();
@@ -202,8 +213,8 @@ macro_rules! re_captures (
202213
macro_rules! re_captures_static (
203214
($i:expr, $re:expr) => (
204215
{
205-
let re = regex!($re);
206-
let v:Vec<Vec<&str>> = re.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect();
216+
regex!(RE, $re);
217+
let v:Vec<Vec<&str>> = RE.captures_iter($i).map(|c| c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect()).collect();
207218
if v.len() != 0 {
208219
let offset = {
209220
let end = v.last().unwrap().last().unwrap();

0 commit comments

Comments
 (0)