[PATCH hare] regex: allow $ at end of every whole-expression alternation
Export this patch
Signed-off-by: Max Schillinger <max@mxsr.de>
---
regex/+test.ha | 7 +++++++
regex/regex.ha | 18 +++++++++++++-----
2 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/regex/+test.ha b/regex/+test.ha
index 999b88e3..f5db579b 100644
--- a/regex/+test.ha
+++ b/regex/+test.ha
@@ -582,10 +582,17 @@ fn run_rawreplace_case(
(`ab|cd`, "bcd", matchres::MATCH, 1, 3),
(`^ab|cd`, "bcd", matchres::MATCH, 1, 3),
(`^ab|cd`, "zab", matchres::NOMATCH, 0, 0),
+ (`ab$|cd`, "ab", matchres::MATCH, 0, 2),
+ (`ab$|cd`, "abc", matchres::NOMATCH, 0, 0),
+ (`ab|cd$`, "cde", 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),
+ (`a|b$|c$|d$|e`, "cd", matchres::MATCH, 1, -1),
+ (`a|b$|c$|d$|e`, "ax", matchres::MATCH, 0, 1),
+ (`a|b$|c$|d$|e`, "cx", matchres::NOMATCH, 0, 0),
+ (`a|b$|c$|d$|e`, "ex", matchres::MATCH, 0, 1),
// TODO: nested capture groups
(`((a))`, "abc", matchres::ERROR, 0, -1),
// (`((a))`, "abc", matchres::MATCH, 0, -1),
diff --git a/regex/regex.ha b/regex/regex.ha
index 097c524b..aabc5cbc 100644
--- a/regex/regex.ha
+++ b/regex/regex.ha
@@ -222,7 +222,6 @@ export fn compile(expr: str) (regex | error) = {
let charsets: []charset = [];
let iter = strings::iter(expr);
let r_idx = 0z;
- let anchored = false;
let jump_idxs: []size = [];
let in_bracket = false;
let skip_charclass_rest = false;
@@ -272,10 +271,17 @@ export fn compile(expr: str) (regex | error) = {
return `Anchor '^' not at start`: error;
};
case '$' =>
- if (r_idx != len(expr) - 1) {
- return `Anchor '$' not at end`: error;
+ if (n_groupstarts > 0) {
+ return `Anchor '$' in capture groups is unsupported`: error;
};
- anchored = true;
+ const peek1 = strings::next(&iter);
+ if (peek1 is rune) {
+ if (peek1 as rune != '|') {
+ return `Anchor '$' not at end of whole pattern or alternation`: error;
+ };
+ strings::prev(&iter);
+ };
+ append(insts, true: inst_match);
case '[' =>
in_bracket = true;
case ']' =>
@@ -412,7 +418,9 @@ export fn compile(expr: str) (regex | error) = {
};
jump_idxs = [];
- append(insts, anchored: inst_match);
+ if (len(insts) == 0 || !(insts[len(insts) - 1] is inst_match)) {
+ append(insts, false: inst_match);
+ };
return regex {
insts = insts,
--
2.45.2
hare/patches: SUCCESS in 1m7s
[regex: allow $ at end of every whole-expression alternation][0] from [Max Schillinger][1]
[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/53968
[1]: mailto:max@mxsr.de
✓ #1279291 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/1279291
✓ #1279292 SUCCESS hare/patches/netbsd.yml https://builds.sr.ht/~sircmpwn/job/1279292
✓ #1279290 SUCCESS hare/patches/alpine.yml https://builds.sr.ht/~sircmpwn/job/1279290
✓ #1279293 SUCCESS hare/patches/openbsd.yml https://builds.sr.ht/~sircmpwn/job/1279293
Thanks!
to git@git.sr.ht:~sircmpwn/hare
2e40a947..dc65431e master -> master