[PATCH hare] regex: fix anchor bug in whole-expression alternations
Export this patch
The regex pattern `a|b` doesn't match `xb` because it contains an
implicit start of line anchor (`^`) before the `b`. The pattern behaves
like `a|^b`.
This commit fixes this by adding an `inst_skip` at every
whole-expression alternation.
Signed-off-by: Max Schillinger <max@mxsr.de>
---
regex/+test.ha | 4 ++++
regex/regex.ha | 4 ++++
2 files changed, 8 insertions(+)
diff --git a/regex/+test.ha b/regex/+test.ha
index 19a10e81..bd925d2f 100644
--- a/regex/+test.ha
+++ b/regex/+test.ha
@@ -581,8 +581,12 @@ fn run_rawreplace_case(
(`ab|cd`, "cd", matchres::MATCH, 0, 2),
(`ab|cd`, "abc", matchres::MATCH, 0, 2),
(`ab|cd`, "abcd", matchres::MATCH, 0, 2),
+ (`ab|cd`, "bcd", matchres::MATCH, 1, 3),
+ (`^ab|cd`, "bcd", matchres::MATCH, 1, 3),
+ (`^ab|cd`, "zab", matchres::NOMATCH, 0, 0),
// multiple alternation
(`a|b|c|d|e`, "e", matchres::MATCH, 0, -1),
+ (`a|b|c|d|e`, "xe", matchres::MATCH, 1, -1),
(`(a|b|c|d|e)f`, "ef", matchres::MATCH, 0, -1),
// TODO: nested capture groups
(`((a))`, "abc", matchres::ERROR, 0, -1),
diff --git a/regex/regex.ha b/regex/regex.ha
index 34bf50d3..097c524b 100644
--- a/regex/regex.ha
+++ b/regex/regex.ha
@@ -311,6 +311,10 @@ export fn compile(expr: str) (regex | error) = {
jump_idxs[len(jump_idxs) - 1] + 1 else origin;
insert(insts[split_idx], newinst);
append(jump_idxs, len(insts) - 1);
+ // add skip if it's a whole-expression alternation
+ if (origin == 0) {
+ append(insts, inst_skip);
+ };
case '{' =>
let origin = len(insts) - 1;
if (insts[origin] is inst_groupend) {
--
2.45.2
hare/patches: SUCCESS in 1m8s
[regex: fix anchor bug in whole-expression alternations][0] from [Max Schillinger][1]
[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/53948
[1]: mailto:max@mxsr.de
✓ #1278344 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/1278344
✓ #1278343 SUCCESS hare/patches/alpine.yml https://builds.sr.ht/~sircmpwn/job/1278343
✓ #1278346 SUCCESS hare/patches/openbsd.yml https://builds.sr.ht/~sircmpwn/job/1278346
✓ #1278345 SUCCESS hare/patches/netbsd.yml https://builds.sr.ht/~sircmpwn/job/1278345
Hadn't thought about this, thank you for catching this! :)
to git@git.sr.ht:~sircmpwn/hare
f07c16a2..2e40a947 master -> master