summaryrefslogtreecommitdiff
path: root/core/regexp.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-22 17:56:44 -0300
committerGabriel Schneider <[email protected]>2026-11-22 18:26:17 -0300
commitf60f51fe89f1d3737389047cb2f3671b1b21e56d (patch)
tree527d26a7532f1d4bf36e9726c4830fa109fef18b /core/regexp.zig
parent9863a63b614d200852853825031189364dcacfdc (diff)
downloadpardes-f60f51fe89f1d3737389047cb2f3671b1b21e56d.tar.gz
pardes-f60f51fe89f1d3737389047cb2f3671b1b21e56d.zip
tests: what mvzr compiles it can walk: core/regexp.zig's test of that name searches `()a` on `b` and `a()b` on `a` (no match) and on `ba` and `xab` (what they match), expects `a|*` `|+` `(|?)` `|??` `^*` `^+?` `a|{2}` `({1))` `a{1b` refused, and searches every pattern up to five characters over `()|*+?a` and four over `()|*+?a^{1}` from every offset of `aa\n\na`: refused, or answered inside the budget (Debug: its test program runs in 50 ms); on this base it aborts at its first case, `()a` on `b` (index out of bounds, index 0 of len 0, mvzr.zig:999 `patternAcceptsEmpty` from `groupAcceptsEmpty` :1040 from `matchPattern` :333 of the build's patched copy: the frames of the abort in .scratch/sketch/reorg/merge57/replicas-2.log, where the soak's random typist, narrowing a selection by a regex (`s`) a key at a time, took its session down); outside the tree (.scratch/sketch/reorg/pardes-regex-2/enum: this core/regexp.zig over the build's patched mvzr in a safe build, a forked child per panic) 18,746 of the 960,799 patterns up to seven characters over `()|*+?a` panic, every one indexing an empty slice: `patternAcceptsEmpty` (an empty group with more after it: `()a`, `a()b`), `thisPattern` and `nextPattern` (a repeat right after `|`: `a|*`, `|*`, `|??`); over wider alphabets two more kinds, a repeat right after `^` (`^*`: reached unreachable code) and a count ending in neither `,` nor `}` (`({1))`: index 1 of len 1): 68,627 of 5,229,042 up to six over `()|*+?a^$.{}1`, 40,135 of 5,399,042 up to five over `()[]{},*+?|\^$.-a1xnbf`; the next change is the fix
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/regexp.zig')
-rw-r--r--core/regexp.zig45
1 files changed, 45 insertions, 0 deletions
diff --git a/core/regexp.zig b/core/regexp.zig
index 2936b491..4a51d0ce 100644
--- a/core/regexp.zig
+++ b/core/regexp.zig
@@ -681,3 +681,48 @@ test "a pattern that stops inside a count or an escape is refused, not read past
}
}
}
+
+test "what mvzr compiles it can walk: an empty group matches the empty string; a repeat of `|` or `^`, and a count without its `}`, are refused" {
+ std.testing.log_level = .err; // mvzr warns of each bad string
+ // An empty group with more after it, searched to a line's end, where
+ // mvzr asks whether the group may match nothing (build/patches.zig).
+ const Case = struct { pat: []const u8, text: []const u8, hit: ?[2]usize };
+ for ([_]Case{
+ .{ .pat = "()a", .text = "b", .hit = null },
+ .{ .pat = "()a", .text = "ba", .hit = .{ 1, 2 } },
+ .{ .pat = "a()b", .text = "a", .hit = null },
+ .{ .pat = "a()b", .text = "xab", .hit = .{ 1, 3 } },
+ }) |c| {
+ var rx = try Regex.compile(c.pat);
+ const m = try rx.find(c.text, 0, c.text.len, c.text.len);
+ try std.testing.expectEqual(c.hit, if (m) |h| [2]usize{ h.start, h.end } else null);
+ }
+ // A repeat right after `|` or `^` took it for what it repeats, and a
+ // count ending in neither `,` nor `}` left a hole in the program: each
+ // walked off an empty slice in the matcher.
+ for ([_][]const u8{ "a|*", "|+", "(|?)", "|??", "^*", "^+?", "a|{2}", "({1))", "a{1b" }) |pat|
+ try std.testing.expectError(error.Bad, Regex.compile(pat));
+ // Every pattern up to five characters over the first alphabet and four
+ // over the second, searched from every offset of a text with an empty
+ // line: refused, or searched to an answer inside the budget.
+ const text = "aa\n\na";
+ var pat: [5]u8 = undefined;
+ for ([_][]const u8{ "()|*+?a", "()|*+?a^{1}" }, [_]usize{ 5, 4 }) |alphabet, longest| {
+ for (1..longest + 1) |len| {
+ var n: usize = 1;
+ for (0..len) |_| n *= alphabet.len;
+ for (0..n) |k| {
+ var rest = k;
+ for (pat[0..len]) |*c| {
+ c.* = alphabet[rest % alphabet.len];
+ rest /= alphabet.len;
+ }
+ var rx = Regex.compile(pat[0..len]) catch continue;
+ for (0..text.len + 1) |from| {
+ const m = try rx.find(text, from, text.len, text.len) orelse continue;
+ try std.testing.expect(from <= m.start and m.start <= m.end and m.end <= text.len);
+ }
+ }
+ }
+ }
+}