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

syntax: fix interval set optimizations #1104

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file not shown.
2 changes: 1 addition & 1 deletion regex-syntax/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "regex-syntax"
version = "0.8.0" #:version
version = "0.8.1" #:version
authors = ["The Rust Project Developers", "Andrew Gallant <jamslam@gmail.com>"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/regex/tree/master/regex-syntax"
Expand Down
44 changes: 21 additions & 23 deletions regex-syntax/src/hir/interval.rs
Expand Up @@ -94,32 +94,30 @@ impl<I: Interval> IntervalSet<I> {
// Find the first range that is not greater than the new interval.
// This is the first range that could possibly be unioned with the
// new interval.
let mut drain_end = self.ranges.len();
while drain_end > 0
&& self.ranges[drain_end - 1].lower() > interval.upper()
&& !self.ranges[drain_end - 1].is_contiguous(&interval)
{
drain_end -= 1;
}

// Try to union the new interval with old intervals backwards.
if drain_end > 0 && self.ranges[drain_end - 1].is_contiguous(&interval)
{
self.ranges[drain_end - 1] =
self.ranges[drain_end - 1].union(&interval).unwrap();
for i in (0..drain_end - 1).rev() {
if let Some(union) =
self.ranges[drain_end - 1].union(&self.ranges[i])
{
self.ranges[drain_end - 1] = union;
} else {
self.ranges.drain(i + 1..drain_end - 1);
break;
for i in 0..self.ranges.len() {
if self.ranges[i].is_contiguous(&interval) {
self.ranges[i] = self.ranges[i].union(&interval).unwrap();
// Try to union the new interval with all subsequent ranges.
// When it's no longer possible to union, remove the remaining
// ranges and return.
for j in i + 1..self.ranges.len() {
if let Some(union) = self.ranges[i].union(&self.ranges[j])
{
self.ranges[i] = union;
} else {
self.ranges.drain(i + 1..j);
return;
}
}
self.ranges.drain(i + 1..);
return;
} else if self.ranges[i].lower() > interval.upper() {
self.ranges.insert(i, interval);
return;
}
} else {
self.ranges.insert(drain_end, interval);
}

self.ranges.push(interval);
}

/// Return an iterator over all intervals in this set.
Expand Down
13 changes: 13 additions & 0 deletions testdata/regression.toml
Expand Up @@ -800,3 +800,16 @@ name = "non-prefix-literal-quit-state"
regex = '.+\b\n'
haystack = "β77\n"
matches = [[0, 5]]

# This is a regression test for some errant HIR interval set operations that
# were made in the regex-syntax 0.8.0 release and then reverted in 0.8.1. The
# issue here is that the HIR produced from the regex had out-of-order ranges.
#
# See: https://github.com/rust-lang/regex/issues/1103
# Ref: https://github.com/rust-lang/regex/pull/1051
# Ref: https://github.com/rust-lang/regex/pull/1102
[[test]]
name = "hir-optimization-out-of-order-class"
regex = '^[[:alnum:]./-]+$'
haystack = "a-b"
matches = [[0, 3]]