From efc9896da7901421b4e687d377e0cb75e2e99c2a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 21 Dec 2021 18:58:55 -0500 Subject: slight improvement for regex matching --- Cargo.lock | 1 + Cargo.toml | 1 + src/2020/2/mod.rs | 4 ++-- src/2020/7/mod.rs | 12 ++++-------- src/2020/8/mod.rs | 4 ++-- src/2021/17/mod.rs | 9 +++++---- src/2021/5/mod.rs | 3 +-- src/main.rs | 3 +++ src/regex.rs | 8 ++++++++ 9 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 src/regex.rs diff --git a/Cargo.lock b/Cargo.lock index 6420581..403b53b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,7 @@ version = "0.1.0" dependencies = [ "ahash", "anyhow", + "lazy_static", "paw", "priority-queue", "regex", diff --git a/Cargo.toml b/Cargo.toml index cccaac3..e863961 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" [dependencies] ahash = "0.7.6" anyhow = "1.0.51" +lazy_static = "1.4.0" paw = "1.0.0" priority-queue = "1.2.1" regex = "1.5.4" diff --git a/src/2020/2/mod.rs b/src/2020/2/mod.rs index 75cc4f1..a2ff9df 100644 --- a/src/2020/2/mod.rs +++ b/src/2020/2/mod.rs @@ -9,9 +9,9 @@ pub struct Line { impl Line { fn parse(line: &str) -> Result { - let rx = Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap(); let captures = - rx.captures(line).context("line failed to match regex")?; + regex_captures!(r"^([0-9]+)-([0-9]+) (.): (.*)$", line) + .context("line failed to match regex")?; let c = captures .get(3) .unwrap() diff --git a/src/2020/7/mod.rs b/src/2020/7/mod.rs index 6199ad0..2c8a1a7 100644 --- a/src/2020/7/mod.rs +++ b/src/2020/7/mod.rs @@ -28,11 +28,7 @@ pub fn part2(graph: Graph) -> Result { } fn parse_line(line: &str) -> Result<(String, Vec<(i64, String)>)> { - let main_rx = Regex::new(r"^(.*) bags contain (.*)\.$").unwrap(); - let contents_rx = Regex::new(r"^([0-9]+) (.*) bags?").unwrap(); - - let captures = main_rx - .captures(line) + let captures = regex_captures!(r"^(.*) bags contain (.*)\.$", line) .context("line failed to match regex")?; let color = captures.get(1).unwrap().as_str(); let contents = captures.get(2).unwrap().as_str(); @@ -44,9 +40,9 @@ fn parse_line(line: &str) -> Result<(String, Vec<(i64, String)>)> { contents .split(", ") .map(|s| { - let captures = contents_rx - .captures(s) - .context("line failed to match regex")?; + let captures = + regex_captures!(r"^([0-9]+) (.*) bags?", s) + .context("line failed to match regex")?; Ok(( captures .get(1) diff --git a/src/2020/8/mod.rs b/src/2020/8/mod.rs index d1908cd..af08f5c 100644 --- a/src/2020/8/mod.rs +++ b/src/2020/8/mod.rs @@ -30,8 +30,8 @@ impl std::str::FromStr for Op { type Err = Error; fn from_str(s: &str) -> std::result::Result { - let rx = Regex::new(r"^([^ ]*) ((?:-|\+)[0-9]+)$").unwrap(); - let captures = rx.captures(s).context("failed to parse line")?; + let captures = regex_captures!(r"^([^ ]*) ((?:-|\+)[0-9]+)$", s) + .context("failed to parse line")?; let ty = captures.get(1).unwrap().as_str().parse()?; let arg = captures .get(2) diff --git a/src/2021/17/mod.rs b/src/2021/17/mod.rs index 2bc54fc..b3bf5ad 100644 --- a/src/2021/17/mod.rs +++ b/src/2021/17/mod.rs @@ -40,11 +40,12 @@ fn fire( pub fn parse( fh: File, ) -> Result<(std::ops::RangeInclusive, std::ops::RangeInclusive)> { - let rx = - Regex::new(r"target area: x=(-?\d+)..(-?\d+), y=(-?\d+)..(-?\d+)") - .unwrap(); let line = parse::lines(fh).next().unwrap(); - let captures = rx.captures(&line).unwrap(); + let captures = regex_captures!( + r"target area: x=(-?\d+)..(-?\d+), y=(-?\d+)..(-?\d+)", + &line, + ) + .unwrap(); let xrange: std::ops::RangeInclusive = captures[1].parse().unwrap()..=captures[2].parse().unwrap(); let yrange: std::ops::RangeInclusive = diff --git a/src/2021/5/mod.rs b/src/2021/5/mod.rs index f33a9c8..6b413b0 100644 --- a/src/2021/5/mod.rs +++ b/src/2021/5/mod.rs @@ -73,9 +73,8 @@ impl Map { } pub fn parse(fh: File) -> Result>> { - let rx = Regex::new("^(\\d+),(\\d+) -> (\\d+),(\\d+)$")?; Ok(parse::lines(fh).map(move |line| { - rx.captures(&line) + regex_captures!(r"^(\d+),(\d+) -> (\d+),(\d+)$", &line) .unwrap() .iter() .skip(1) diff --git a/src/main.rs b/src/main.rs index aa6ad88..d5b4516 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,9 @@ #![allow(clippy::collapsible_if)] #![allow(clippy::comparison_chain)] +#[macro_use] +pub mod regex; + pub mod grid; pub mod parse; pub mod prelude; diff --git a/src/regex.rs b/src/regex.rs new file mode 100644 index 0000000..56ce19f --- /dev/null +++ b/src/regex.rs @@ -0,0 +1,8 @@ +macro_rules! regex_captures { + ($rx:expr, $s:expr $(,)?) => {{ + lazy_static::lazy_static! { + static ref RX: Regex = regex::Regex::new($rx).unwrap(); + } + RX.captures($s) + }}; +} -- cgit v1.2.3-54-g00ecf