Skip to content

Commit 7070f1e

Browse files
committed
re-use Regex instances
1 parent 2374343 commit 7070f1e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

ci/sembr/src/main.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::path::PathBuf;
2+
use std::sync::LazyLock;
23
use std::{fs, process};
34

45
use anyhow::Result;
@@ -15,6 +16,9 @@ struct Cli {
1516
line_length_limit: usize,
1617
}
1718

19+
static REGEX_SPLIT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(\.|\?|;|!)\s+").unwrap());
20+
static REGEX_IGNORE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(\d\.|\-|\*)\s+").unwrap());
21+
1822
fn main() -> Result<()> {
1923
let cli = Cli::parse();
2024
let mut compliant = Vec::new();
@@ -83,8 +87,6 @@ fn comply(content: &str) -> Result<String> {
8387
let content: Vec<_> = content.lines().map(std::borrow::ToOwned::to_owned).collect();
8488
let mut new_content = content.clone();
8589
let mut new_n = 0;
86-
let split_re = Regex::new(r"(\.|\?|;|!)\s+")?;
87-
let ignore_re = Regex::new(r"(\d\.|\-|\*)\s+").unwrap();
8890
let mut in_code_block = false;
8991
for (n, line) in content.into_iter().enumerate() {
9092
if n != 0 {
@@ -93,13 +95,13 @@ fn comply(content: &str) -> Result<String> {
9395
if line.trim().starts_with("```") {
9496
in_code_block = !in_code_block;
9597
}
96-
if ignore(&line, in_code_block, &ignore_re) {
98+
if ignore(&line, in_code_block, &REGEX_IGNORE) {
9799
continue;
98100
}
99-
if split_re.is_match(&line) {
101+
if REGEX_SPLIT.is_match(&line) {
100102
let indent = line.find(|ch: char| !ch.is_whitespace()).unwrap();
101103
let new_lines: Vec<_> = line
102-
.split_inclusive(&split_re)
104+
.split_inclusive(&*REGEX_SPLIT)
103105
.map(|portion| format!("{:indent$}{}", "", portion.trim()))
104106
.collect();
105107
new_content.splice(new_n..=new_n, new_lines.clone());
@@ -113,8 +115,6 @@ fn reduce_overlong_lines(content: &str, limit: usize) -> Result<String> {
113115
let content: Vec<_> = content.lines().map(std::borrow::ToOwned::to_owned).collect();
114116
let mut new_content = content.clone();
115117
let mut new_n = 0;
116-
let ignore_re = Regex::new(r"(\d\.|\-|\*)\s+").unwrap();
117-
let split_re = Regex::new(r"(\.|\?|;|!)\s+")?;
118118
let mut in_code_block = false;
119119
let mut skip_next = false;
120120
for (n, line) in content.iter().enumerate() {
@@ -128,13 +128,13 @@ fn reduce_overlong_lines(content: &str, limit: usize) -> Result<String> {
128128
if line.trim().starts_with("```") {
129129
in_code_block = !in_code_block;
130130
}
131-
if ignore(line, in_code_block, &ignore_re) || split_re.is_match(line) {
131+
if ignore(line, in_code_block, &REGEX_IGNORE) || REGEX_SPLIT.is_match(line) {
132132
continue;
133133
}
134134
let Some(next_line) = content.get(n + 1) else {
135135
continue;
136136
};
137-
if ignore(next_line, in_code_block, &ignore_re) || line.trim().ends_with('.') {
137+
if ignore(next_line, in_code_block, &REGEX_IGNORE) || line.trim().ends_with('.') {
138138
continue;
139139
}
140140
if line.len() + next_line.len() < limit {

0 commit comments

Comments
 (0)