Skip to content

Commit

Permalink
perf: not capture on bash regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Rational-Curiosity committed May 6, 2023
1 parent ee67777 commit ce11fa1
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ impl<'a> State<'a> {
let (name, pattern, matching) = first_match;
let text = matching.as_str();

if let Some(captures) = pattern.captures(text) {
// Never hint or broke bash color sequences, but process it
if *name != "bash" {
let captures = pattern.captures(text).expect("No matching?");
let captures: Vec<(&str, usize)> = if let Some(capture) = captures.name("match") {
[(capture.as_str(), capture.start())].to_vec()
} else if captures.len() > 1 {
Expand All @@ -124,28 +126,23 @@ impl<'a> State<'a> {
[(matching.as_str(), 0)].to_vec()
};

// Never hint or broke bash color sequences, but process it
if *name != "bash" {
for (subtext, substart) in captures.iter() {
let start = offset + matching.start() + *substart;
let end = start + subtext.len();
matches.push(Match {
start,
end,
prev_end,
pattern: name,
text: subtext,
hint: None,
});
prev_end = end;
}
for (subtext, substart) in captures.iter() {
let start = offset + matching.start() + *substart;
let end = start + subtext.len();
matches.push(Match {
start,
end,
prev_end,
pattern: name,
text: subtext,
hint: None,
});
prev_end = end;
}

chunk = chunk.get(matching.end()..).expect("Unknown chunk");
offset += matching.end();
} else {
panic!("No matching?");
}

chunk = chunk.get(matching.end()..).expect("Unknown chunk");
offset += matching.end();
} else {
break;
}
Expand Down

0 comments on commit ce11fa1

Please sign in to comment.