Only compile the newline regexp once

pull/25/head
Wilfred Hughes 2021-07-25 12:56:02 +07:00
parent e36a2e8c96
commit 6d5704aed9
3 changed files with 7 additions and 2 deletions

1
Cargo.lock generated

@ -93,6 +93,7 @@ dependencies = [
"colored",
"diff",
"itertools",
"lazy_static",
"pretty_assertions",
"regex",
"rust-embed",

@ -20,6 +20,7 @@ toml = "0.5.8"
rust-embed = "5.9.0"
rustc-hash = "1.1.0"
strsim = "0.10.0"
lazy_static = "1.4.0"
[dev-dependencies]
pretty_assertions = "0.6.1"

@ -1,6 +1,7 @@
use crate::intervals::Interval;
use crate::positions::SingleLineSpan;
use crate::syntax::{aligned_lines, MatchKind, MatchedPos};
use lazy_static::lazy_static;
use regex::Regex;
use std::cmp::{max, min, Ordering};
use std::collections::HashMap;
@ -474,8 +475,10 @@ pub struct NewlinePositions {
impl From<&str> for NewlinePositions {
fn from(s: &str) -> Self {
let newline_re = Regex::new("\n").unwrap();
let mut positions: Vec<_> = newline_re.find_iter(s).map(|mat| mat.end()).collect();
lazy_static! {
static ref NEWLINE_RE: Regex = Regex::new("\n").unwrap();
}
let mut positions: Vec<_> = NEWLINE_RE.find_iter(s).map(|mat| mat.end()).collect();
positions.insert(0, 0);
NewlinePositions {