Skip to content

Instantly share code, notes, and snippets.

@reem
Created June 16, 2014 21:08
Show Gist options
  • Save reem/08b7ca26fb1e1aec3abb to your computer and use it in GitHub Desktop.
Save reem/08b7ca26fb1e1aec3abb to your computer and use it in GitHub Desktop.
#![feature(phase, macro_rules)]
extern crate regex;
#[phase(plugin)] extern crate regex_macros;
macro_rules! matching(
($($a:tt),+) => (
regex!(concat!("^", glob_to_regex!($($a))+, "$"))
);
)
macro_rules! glob_to_regex(
(Segment($a:ident), $r:tt*) => (
concat!("/", stringify!($a), "/", glob_to_regex!($r)),
);
(Binding($a:ident), $r:tt*) => (
concat!("/([a-zA-Z0-9_-]*)/", glob_to_regex!($r)),
);
(DoubleStar, $r:tt*) => (
concat!("/[/a-zA-Z0-9_-]*", glob_to_regex($r))
);
(Star, $r:tt*) => (
concat!("/[a-zA-Z0-9_-]*/", glob_to_regex($r)),
);
)
fn main() {
let reg = matching!(Segment(users), Star);
println!("{}", reg.is_match("/users/hello"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment