Skip to content

Commit

Permalink
Avoid recompiling regexes on every use
Browse files Browse the repository at this point in the history
This should speed things up even if --no-sanitize isn't passed.
  • Loading branch information
ms705 committed Jun 18, 2018
1 parent 7f3a19a commit d3fa0ac
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use nom_sql::{
Literal, LiteralExpression, Operator, SqlQuery, TableKey, UpdateStatement,
};
use regex::Regex;
use std::borrow::Cow;
use std::collections::HashMap;

lazy_static! {
Expand Down Expand Up @@ -45,12 +46,20 @@ lazy_static! {
vec![("lockstatus", "1")],
),
];
pub(crate) static ref COMMENTS: Vec<(Regex, &'static str)> = vec![
(Regex::new(r"(?s)/\*.*\*/").unwrap(), ""),
(Regex::new(r"--.*\n").unwrap(), "\n"),
];
pub(crate) static ref COLLAPSE_SPACES: (Regex, &'static str) =
(Regex::new(r" +").unwrap(), " ");
}

pub(crate) fn sanitize_query(query: &str) -> String {
let query = Regex::new(r"(?s)/\*.*\*/").unwrap().replace_all(query, "");
let query = Regex::new(r"--.*\n").unwrap().replace_all(&query, "\n");
let query = Regex::new(r" +").unwrap().replace_all(&query, " ");
let query = Cow::from(query);
for &(ref pattern, replacement) in &*COMMENTS {
pattern.replace_all(&query, replacement);
}
let query = COLLAPSE_SPACES.0.replace_all(&query, COLLAPSE_SPACES.1);
let query = query.replace('"', "'");
let query = query.trim();
query.to_owned()
Expand Down

0 comments on commit d3fa0ac

Please sign in to comment.