summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 15:57:57 -0300
committerGabriel Schneider <[email protected]>2026-10-30 16:29:54 -0300
commitf57897b1d8eca7080ba2482a6ed20e1c9fa7d453 (patch)
tree7ee29918c15d1c463ab9ac078bdb3f060e1b7b25 /src
parentf7a0b6d902f001cff0343e11263868c5356f33c6 (diff)
downloadpardes-f57897b1d8eca7080ba2482a6ed20e1c9fa7d453.tar.gz
pardes-f57897b1d8eca7080ba2482a6ed20e1c9fa7d453.zip
regexp: one step budget in every build mode, 32M: what a search finds is the core's state, and with 4M in Debug a Debug replica of a ReleaseSafe session answered TooSlow where its session found no match (`\s*(\w+)\s*=` over 200 letters, the variant hunt's test, here)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/regexp.zig18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/regexp.zig b/src/regexp.zig
index 6d281859..6c858dea 100644
--- a/src/regexp.zig
+++ b/src/regexp.zig
@@ -2,7 +2,6 @@
//! sam searches (editors/acme/regx.c). `addr` (src/ninep/addr.zig) and
//! normal mode's `s` and `S` (src/normal.zig) both call it.
const std = @import("std");
-const builtin = @import("builtin");
const mvzr = @import("mvzr");
/// A compiled pattern and how to run it.
@@ -57,8 +56,11 @@ pub const Regex = struct {
end_hint: struct { ptr: usize = 0, len: usize = 0, start: usize = 0, hi: usize = 0, end: usize = 0 } = .{},
/// Timed on this machine's ReleaseSafe build: 10M steps of the worst
- /// patterns take 45-80 ms, and a Debug build is ten times slower.
- pub const budget: u64 = if (builtin.mode == .debug) 4_000_000 else 32_000_000;
+ /// patterns take 45-80 ms. One budget in every build mode: what a
+ /// search finds is the core's state, and a Debug replica of a
+ /// ReleaseSafe session must find what the session does (a Debug build
+ /// is ten times slower at it, about two seconds).
+ pub const budget: u64 = 32_000_000;
pub const e_long = std.fmt.comptimePrint("bad regular expression: longer than mvzr's {d} operations (about {d} pattern characters)", .{ max_ops, max_ops });
pub const e_anchor = "bad regular expression: in a pattern with \\n, ^ can only come first and $ only just before a \\n";
@@ -555,6 +557,16 @@ test "a search that would backtrack without end runs out of budget, promptly" {
try std.testing.expectError(error.TooSlow, chain.find(&line, 0, line.len, line.len));
}
+test "a search's answer is every build mode's" {
+ // `\s*(\w+)\s*=` over a line of letters costs more than linear: over
+ // 200 of them a Debug build's old budget (4M steps) ran out, TooSlow,
+ // where a release build's (32M) answered no match, so a Debug replica
+ // of a ReleaseSafe session diverged from it on one search.
+ var line: [200]u8 = @splat('a');
+ var rx = try Regex.compile("\\s*(\\w+)\\s*=");
+ try std.testing.expectEqual(@as(?Regex.Match, null), try rx.find(&line, 0, line.len, line.len));
+}
+
test "the match is the leftmost, however long the line" {
// A window cut into the identifier would find `x... =` from its middle.
const text = "let " ++ &@as([100]u8, @splat('x')) ++ " = 1";