diff options
| -rw-r--r-- | core/regexp.zig | 45 |
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); + } + } + } + } +} |
