Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegexSet: fix literal optimization bug #370

Merged
merged 1 commit into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ impl ExecBuilder {
let mut prefixes = Some(Literals::empty());
let mut suffixes = Some(Literals::empty());
let mut bytes = false;
let is_set = self.options.pats.len() > 1;
// If we're compiling a regex set and that set has any anchored
// expressions, then disable all literal optimizations.
for pat in &self.options.pats {
let parser =
ExprBuilder::new()
Expand All @@ -227,6 +230,10 @@ impl ExecBuilder {
// Partial anchors unfortunately make it hard to use prefixes,
// so disable them.
prefixes = None;
} else if is_set && expr.is_anchored_start() {
// Regex sets with anchors do not go well with literal
// optimizations.
prefixes = None;
}
prefixes = prefixes.and_then(|mut prefixes| {
if !prefixes.union_prefixes(&expr) {
Expand All @@ -240,6 +247,10 @@ impl ExecBuilder {
// Partial anchors unfortunately make it hard to use suffixes,
// so disable them.
suffixes = None;
} else if is_set && expr.is_anchored_end() {
// Regex sets with anchors do not go well with literal
// optimizations.
prefixes = None;
}
suffixes = suffixes.and_then(|mut suffixes| {
if !suffixes.union_suffixes(&expr) {
Expand Down
1 change: 1 addition & 0 deletions tests/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ matset!(set17, &[".*a"], "a", 0);
nomatset!(nset1, &["a", "a"], "b");
nomatset!(nset2, &["^foo", "bar$"], "bar foo");
nomatset!(nset3, { let xs: &[&str] = &[]; xs }, "a");
nomatset!(nset4, &[r"^rooted$", r"\.log$"], "notrooted");

// See: https://github.com/rust-lang/regex/issues/187
#[test]
Expand Down