Skip to content

Commit

Permalink
lazy_static regexen
Browse files Browse the repository at this point in the history
  • Loading branch information
bergey committed Dec 27, 2022
1 parent 7ab1700 commit 9c63ce0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
clap = { version = "4.0.29", features = ["derive"] }
lazy_static = "1.4.0"
nom = "7.1.1"
nom_locate = "4.0.0"
regex = "1"
Expand Down
17 changes: 11 additions & 6 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use ::regex::Regex;
use crate::regex;
use crate::regex::parser::{Input};
use crate::regex::equivalent::Equivalent;

use ::regex::Regex;
use std::io;
use lazy_static::lazy_static;

use nom;
use nom::{Finish, IResult};
Expand Down Expand Up @@ -73,17 +75,20 @@ fn take_until<'a>(sep: char, s: Input) -> Progress<String> {
Ok((s, vec.into_iter().collect()))
}

lazy_static! {
static ref DOLLAR: Regex = Regex::new(r"\$").unwrap();
static ref BACKSLASH_DIGITS: Regex = Regex::new(r"\\([0-9]+)").unwrap();
}

// convert sed \1 syntax to regex crate $1 and escape $
pub fn clean_replacement(mut s: String) -> String {
let mut dest = String::new();

// TODO static regexen
let dollar = Regex::new(r"\$").unwrap();
let changed = regex::replace_all(&dollar, &s, &mut dest, "$$$$");
let changed = regex::replace_all(&*DOLLAR, &s, &mut dest, "$$$$");
if changed {std::mem::swap(&mut s, &mut dest)}

let backslash_digits = Regex::new(r"\\([0-9]+)").unwrap();
let changed = regex::replace_all(&backslash_digits, &s, &mut dest, r"$${$1}");
let changed = regex::replace_all(&*BACKSLASH_DIGITS, &s, &mut dest, r"$${$1}");
if changed {std::mem::swap(&mut s, &mut dest)}

s
Expand Down Expand Up @@ -157,7 +162,7 @@ pub fn parse_command<'a>(s: Input) -> Progress<Command> {
Some(_) => {
let (s, addr) = parse_address(s)?;
Ok((s, Some(addr)))
}
}
}
}
}?;
Expand Down

0 comments on commit 9c63ce0

Please sign in to comment.