Skip to content

Commit

Permalink
Update regex-automata to v0.3.6
Browse files Browse the repository at this point in the history
This seems like a sensible first step before looking into alacritty#7097.
  • Loading branch information
chrisduerr authored Aug 27, 2023
1 parent 3330614 commit 73276b6
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 127 deletions.
19 changes: 15 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions alacritty/src/config/ui_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,11 @@ pub struct LazyRegex(Rc<RefCell<LazyRegexVariant>>);

impl LazyRegex {
/// Execute a function with the compiled regex DFAs as parameter.
pub fn with_compiled<T, F>(&self, mut f: F) -> T
pub fn with_compiled<T, F>(&self, f: F) -> Option<T>
where
F: FnMut(&RegexSearch) -> T,
{
f(self.0.borrow_mut().compiled())
self.0.borrow_mut().compiled().map(f)
}
}

Expand All @@ -506,34 +506,37 @@ impl<'de> Deserialize<'de> for LazyRegex {
pub enum LazyRegexVariant {
Compiled(Box<RegexSearch>),
Pattern(String),
Uncompilable,
}

impl LazyRegexVariant {
/// Get a reference to the compiled regex.
///
/// If the regex is not already compiled, this will compile the DFAs and store them for future
/// access.
fn compiled(&mut self) -> &RegexSearch {
fn compiled(&mut self) -> Option<&RegexSearch> {
// Check if the regex has already been compiled.
let regex = match self {
Self::Compiled(regex_search) => return regex_search,
Self::Compiled(regex_search) => return Some(regex_search),
Self::Uncompilable => return None,
Self::Pattern(regex) => regex,
};

// Compile the regex.
let regex_search = match RegexSearch::new(regex) {
Ok(regex_search) => regex_search,
Err(error) => {
error!("hint regex is invalid: {}", error);
RegexSearch::new("").unwrap()
Err(err) => {
error!("could not compile hint regex: {err}");
*self = Self::Uncompilable;
return None;
},
};
*self = Self::Compiled(Box::new(regex_search));

// Return a reference to the compiled DFAs.
match self {
Self::Compiled(dfas) => dfas,
Self::Pattern(_) => unreachable!(),
Self::Compiled(dfas) => Some(dfas),
_ => unreachable!(),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions alacritty/src/display/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,10 @@ pub fn highlighted_at<T>(
});
}

if let Some(bounds) = hint.content.regex.as_ref().and_then(|regex| {
let bounds = hint.content.regex.as_ref().and_then(|regex| {
regex.with_compiled(|regex| regex_match_at(term, point, regex, hint.post_processing))
}) {
});
if let Some(bounds) = bounds.flatten() {
return Some(HintMatch { bounds, action: hint.action.clone(), hyperlink: None });
}

Expand Down
2 changes: 1 addition & 1 deletion alacritty_terminal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ log = "0.4"
mio = "0.6.20"
mio-extras = "2"
parking_lot = "0.12.0"
regex-automata = "0.1.9"
regex-automata = "0.3.6"
serde = { version = "1", features = ["derive", "rc"] }
serde_yaml = "0.8"
toml = "0.7.1"
Expand Down
Loading

0 comments on commit 73276b6

Please sign in to comment.