Skip to content

Commit

Permalink
refactor: optimize newline normalization by using `lazy_regex::regex!…
Browse files Browse the repository at this point in the history
…` and returning `Cow<str>` to reduce allocations
  • Loading branch information
gvozdvmozgu authored and benfdking committed Sep 20, 2024
1 parent ad82aec commit 02da258
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions crates/lib/src/core/linter/core.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::borrow::Cow;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use ahash::{AHashMap, AHashSet};
use itertools::Itertools;
use regex::Regex;
use smol_str::{SmolStr, ToSmolStr};
use sqruff_lib_core::dialects::base::Dialect;
use sqruff_lib_core::errors::{
Expand Down Expand Up @@ -334,20 +334,20 @@ impl Linter {
/// Template the file.
pub fn render_string(
&self,
in_str: &str,
f_name: String,
sql: &str,
filename: String,
config: &FluffConfig,
) -> Result<RenderedFile, SQLFluffUserError> {
let in_str = Self::normalise_newlines(in_str);
let sql = Self::normalise_newlines(sql);

if let Some(error) = config.verify_dialect_specified() {
return Err(error);
}

let templater_violations = vec![];
let templated_file = match self.templater.process(
in_str.as_str(),
f_name.as_str(),
sql.as_ref(),
filename.as_str(),
Some(config),
self.formatter.as_ref(),
) {
Expand All @@ -362,8 +362,8 @@ impl Linter {
Ok(RenderedFile {
templated_file,
templater_violations,
f_name: f_name.to_owned(),
source_str: f_name.to_owned(),
f_name: filename.to_owned(),
source_str: filename.to_owned(),
})
}

Expand Down Expand Up @@ -469,9 +469,8 @@ impl Linter {
}

/// Normalise newlines to unix-style line endings.
fn normalise_newlines(string: &str) -> String {
let re = Regex::new(r"\r\n|\r").unwrap();
re.replace_all(string, "\n").to_string()
fn normalise_newlines(string: &str) -> Cow<str> {
lazy_regex::regex!("\r\n|\r").replace_all(string, "\n")
}

// Return a set of sql file paths from a potentially more ambiguous path string.
Expand Down

0 comments on commit 02da258

Please sign in to comment.