Skip to content

Commit

Permalink
Replace regex_macros with lazy_static
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Geal committed Feb 23, 2016
1 parent c67afc7 commit ab13eff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ core = []
nightly = []
default = ["stream"]
regexp = ["regex"]
regexp_macros = ["regexp", "regex_macros"]
regexp_macros = ["regexp", "lazy_static"]
stream = []

[dependencies.regex]
version = "^0.1.41"
optional = true

[dependencies.regex_macros]
version = "^0.1.21"
[dependencies.lazy_static]
version = "^0.1.15"
optional = true

4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@
#![cfg_attr(feature = "core", feature(no_std))]
#![cfg_attr(feature = "core", feature(collections))]
#![cfg_attr(feature = "core", no_std)]
#![cfg_attr(feature = "regexp_macros", feature(plugin))]
#![cfg_attr(feature = "regexp_macros", plugin(regex_macros))]
#![cfg_attr(feature = "nightly", feature(test))]

#[cfg(feature = "core")]
extern crate collections;
#[cfg(feature = "regexp")]
extern crate regex;
#[cfg(feature = "regexp_macros")]
#[macro_use] extern crate lazy_static;
#[cfg(feature = "nightly")]
extern crate test;

Expand Down
31 changes: 21 additions & 10 deletions src/regexp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#[doc(hidden)]
#[macro_export]
macro_rules! regex (
($re: ident, $s:expr) => (
lazy_static! {
static ref $re: ::regex::Regex = ::regex::Regex::new($s).unwrap();
}
);
);


/// `re_match!(regexp) => &[T] -> IResult<&[T], &[T]>`
/// Returns the whole input if a match is found
///
Expand Down Expand Up @@ -27,8 +38,8 @@ macro_rules! re_match_static (
($i:expr, $re:expr) => (
{
use $crate::InputLength;
let re = regex!($re);
if re.is_match($i) {
regex!(RE, $re);
if RE.is_match($i) {
$crate::IResult::Done(&$i[$i.input_len()..], $i)
} else {
$crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpMatch))
Expand Down Expand Up @@ -65,8 +76,8 @@ macro_rules! re_find (
macro_rules! re_find_static (
($i:expr, $re:expr) => (
{
let re = regex!($re);
if let Some((begin, end)) = re.find($i) {
regex!(RE, $re);
if let Some((begin, end)) = RE.find($i) {
$crate::IResult::Done(&$i[end..], &$i[begin..end])
} else {
$crate::IResult::Error($crate::Err::Code($crate::ErrorKind::RegexpFind))
Expand Down Expand Up @@ -108,8 +119,8 @@ macro_rules! re_matches (
macro_rules! re_matches_static (
($i:expr, $re:expr) => (
{
let re = regex!($re);
let v: Vec<&str> = re.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect();
regex!(RE, $re);
let v: Vec<&str> = RE.find_iter($i).map(|(begin,end)| &$i[begin..end]).collect();
if v.len() != 0 {
let offset = {
let end = v.last().unwrap();
Expand Down Expand Up @@ -155,8 +166,8 @@ macro_rules! re_capture (
macro_rules! re_capture_static (
($i:expr, $re:expr) => (
{
let re = regex!($re);
if let Some(c) = re.captures($i) {
regex!(RE, $re);
if let Some(c) = RE.captures($i) {
let v:Vec<&str> = c.iter_pos().filter(|el| el.is_some()).map(|el| el.unwrap()).map(|(begin,end)| &$i[begin..end]).collect();
let offset = {
let end = v.last().unwrap();
Expand Down Expand Up @@ -202,8 +213,8 @@ macro_rules! re_captures (
macro_rules! re_captures_static (
($i:expr, $re:expr) => (
{
let re = regex!($re);
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();
regex!(RE, $re);
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();
if v.len() != 0 {
let offset = {
let end = v.last().unwrap().last().unwrap();
Expand Down

0 comments on commit ab13eff

Please sign in to comment.