Skip to content

Commit d3fa0ac

Browse files
committed
Avoid recompiling regexes on every use
This should speed things up even if --no-sanitize isn't passed.
1 parent 7f3a19a commit d3fa0ac

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/utils.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use nom_sql::{
99
Literal, LiteralExpression, Operator, SqlQuery, TableKey, UpdateStatement,
1010
};
1111
use regex::Regex;
12+
use std::borrow::Cow;
1213
use std::collections::HashMap;
1314

1415
lazy_static! {
@@ -45,12 +46,20 @@ lazy_static! {
4546
vec![("lockstatus", "1")],
4647
),
4748
];
49+
pub(crate) static ref COMMENTS: Vec<(Regex, &'static str)> = vec![
50+
(Regex::new(r"(?s)/\*.*\*/").unwrap(), ""),
51+
(Regex::new(r"--.*\n").unwrap(), "\n"),
52+
];
53+
pub(crate) static ref COLLAPSE_SPACES: (Regex, &'static str) =
54+
(Regex::new(r" +").unwrap(), " ");
4855
}
4956

5057
pub(crate) fn sanitize_query(query: &str) -> String {
51-
let query = Regex::new(r"(?s)/\*.*\*/").unwrap().replace_all(query, "");
52-
let query = Regex::new(r"--.*\n").unwrap().replace_all(&query, "\n");
53-
let query = Regex::new(r" +").unwrap().replace_all(&query, " ");
58+
let query = Cow::from(query);
59+
for &(ref pattern, replacement) in &*COMMENTS {
60+
pattern.replace_all(&query, replacement);
61+
}
62+
let query = COLLAPSE_SPACES.0.replace_all(&query, COLLAPSE_SPACES.1);
5463
let query = query.replace('"', "'");
5564
let query = query.trim();
5665
query.to_owned()

0 commit comments

Comments
 (0)