Skip to content

Commit 807e221

Browse files
authored
Merge pull request #290 from BurntSushi/fix-289
Fix a bug in uambiguous prefixes.
2 parents 3cfef1e + 6300490 commit 807e221

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

regex-syntax/src/literals.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,17 @@ impl Literals {
216216
if self.lits.is_empty() {
217217
return self.to_empty();
218218
}
219+
let mut old: Vec<Lit> = self.lits.iter().cloned().collect();
219220
let mut new = self.to_empty();
220221
'OUTER:
221-
for lit1 in &self.lits {
222+
while let Some(mut candidate) = old.pop() {
223+
if candidate.is_empty() {
224+
continue;
225+
}
222226
if new.lits.is_empty() {
223-
new.lits.push(lit1.clone());
227+
new.lits.push(candidate);
224228
continue;
225229
}
226-
let mut candidate = lit1.clone();
227230
for lit2 in &mut new.lits {
228231
if lit2.is_empty() {
229232
continue;
@@ -236,11 +239,14 @@ impl Literals {
236239
lit2.cut = candidate.cut;
237240
continue 'OUTER;
238241
}
239-
if candidate.len() <= lit2.len() {
242+
if candidate.len() < lit2.len() {
240243
if let Some(i) = position(&candidate, &lit2) {
241-
lit2.truncate(i);
242-
lit2.cut();
243244
candidate.cut();
245+
let mut lit3 = lit2.clone();
246+
lit3.truncate(i);
247+
lit3.cut();
248+
old.push(lit3);
249+
lit2.clear();
244250
}
245251
} else {
246252
if let Some(i) = position(&lit2, &candidate) {
@@ -1381,6 +1387,9 @@ mod tests {
13811387
test_unamb!(unambiguous11,
13821388
vec![M("zazb"), M("azb")], vec![C("azb"), C("z")]);
13831389
test_unamb!(unambiguous12, vec![M("foo"), C("foo")], vec![C("foo")]);
1390+
test_unamb!(unambiguous13,
1391+
vec![M("ABCX"), M("CDAX"), M("BCX")],
1392+
vec![C("A"), C("BCX"), C("CD")]);
13841393

13851394
// ************************************************************************
13861395
// Tests for suffix trimming.

tests/regression.rs

+3
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ mat!(ascii_boundary_capture, u!(r"(?-u)(\B)"), "\u{28f3e}", Some((0, 0)));
6868
// See: https://github.com/rust-lang-nursery/regex/issues/280
6969
ismatch!(partial_anchor_alternate_begin, u!(r"^a|z"), "yyyyya", false);
7070
ismatch!(partial_anchor_alternate_end, u!(r"a$|z"), "ayyyyy", false);
71+
72+
// See: https://github.com/rust-lang-nursery/regex/issues/289
73+
mat!(lits_unambiguous, u!(r"(ABC|CDA|BC)X"), "CDAX", Some((0, 4)));

0 commit comments

Comments
 (0)