Skip to content

Commit 325865c

Browse files
committed
Add fast-path early returns in highlighters
- Skip regex matching when input lacks required separator characters (-, /, :) for date, time, and UUID highlighters
1 parent 3fdbc1b commit 325865c

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/core/highlighters/date_dash.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ impl DateDashHighlighter {
117117

118118
impl Highlight for DateDashHighlighter {
119119
fn apply<'a>(&self, input: &'a str) -> Cow<'a, str> {
120+
if !input.as_bytes().contains(&b'-') && !input.as_bytes().contains(&b'/') {
121+
return Cow::Borrowed(input);
122+
}
123+
120124
let mut it = self.regex.captures_iter(input).peekable();
121125
if it.peek().is_none() {
122126
return Cow::Borrowed(input);

src/core/highlighters/date_time.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ impl TimeHighlighter {
7676

7777
impl Highlight for TimeHighlighter {
7878
fn apply<'a>(&self, input: &'a str) -> Cow<'a, str> {
79-
let mut it = self.regex.captures_iter(input).peekable();
80-
if it.peek().is_none() {
79+
if !input.as_bytes().contains(&b':') {
8180
return Cow::Borrowed(input);
8281
}
8382

src/core/highlighters/uuid.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ impl UuidHighlighter {
3939

4040
impl Highlight for UuidHighlighter {
4141
fn apply<'a>(&self, input: &'a str) -> Cow<'a, str> {
42+
if !input.as_bytes().contains(&b'-') {
43+
return Cow::Borrowed(input);
44+
}
45+
4246
self.regex.replace_all(input, |caps: &Captures<'_>| {
4347
let matched = &caps[0];
4448
let mut buf = String::with_capacity(matched.len() + 32);

0 commit comments

Comments
 (0)