Skip to content

Lightpanda: the headless browser designed for AI and automation

`Regex`: compile through the context

Adrià Arrufat35dbda368670
diff --git a/src/Regex.zig b/src/Regex.zig
index 640cbe69d..6353467c1 100644
--- a/src/Regex.zig
+++ b/src/Regex.zig
@@ -100,6 +100,35 @@ pub const Context = struct {
return self;
}
+ /// A failed compile fills `diag`, when given, with PCRE2's message and the
+ /// offset of the offending character.
+ pub fn compile(self: *const Context, pattern: []const u8, options: Options, diag: ?*Diagnostic) Error!Regex {
+ var flags: u32 = 0;
+ if (options.case_insensitive) flags |= pcre2.PCRE2_CASELESS;
+ if (options.unicode) flags |= pcre2.PCRE2_UTF | pcre2.PCRE2_MATCH_INVALID_UTF;
+
+ var err_code: c_int = 0;
+ var err_offset: usize = 0;
+ const code = pcre2.pcre2_compile_8(
+ pattern.ptr,
+ pattern.len,
+ flags,
+ &err_code,
+ &err_offset,
+ self.compile_context,
+ ) orelse {
+ // A failed allocation is ours, not the pattern's.
+ if (err_code == pcre2.PCRE2_ERROR_HEAP_FAILED) return error.OutOfMemory;
+ if (diag) |d| {
+ const len = pcre2.pcre2_get_error_message_8(err_code, &d.buf, d.buf.len);
+ d.len = if (len < 0) 0 else @intCast(len);
+ d.offset = err_offset;
+ }
+ return error.InvalidRegex;
+ };
+ return .{ .code = code, .context = self };
+ }
+
pub fn deinit(self: *Context) void {
pcre2.pcre2_match_context_free_8(self.match_context);
pcre2.pcre2_compile_context_free_8(self.compile_context);
@@ -129,35 +158,6 @@ pub const Context = struct {
}
};
-/// A failed compile fills `diag`, when given, with PCRE2's message and the
-/// offset of the offending character.
-pub fn compile(context: *const Context, pattern: []const u8, options: Options, diag: ?*Diagnostic) Error!Regex {
- var flags: u32 = 0;
- if (options.case_insensitive) flags |= pcre2.PCRE2_CASELESS;
- if (options.unicode) flags |= pcre2.PCRE2_UTF | pcre2.PCRE2_MATCH_INVALID_UTF;
-
- var err_code: c_int = 0;
- var err_offset: usize = 0;
- const code = pcre2.pcre2_compile_8(
- pattern.ptr,
- pattern.len,
- flags,
- &err_code,
- &err_offset,
- context.compile_context,
- ) orelse {
- // A failed allocation is ours, not the pattern's.
- if (err_code == pcre2.PCRE2_ERROR_HEAP_FAILED) return error.OutOfMemory;
- if (diag) |d| {
- const len = pcre2.pcre2_get_error_message_8(err_code, &d.buf, d.buf.len);
- d.len = if (len < 0) 0 else @intCast(len);
- d.offset = err_offset;
- }
- return error.InvalidRegex;
- };
- return .{ .code = code, .context = context };
-}
-
pub fn deinit(self: Regex) void {
pcre2.pcre2_code_free_8(self.code);
}
@@ -191,7 +191,7 @@ test "Regex: JavaScript escapes and unanchored search" {
const context: *Context = try .init(testing.allocator);
defer context.deinit();
- const regex = try Regex.compile(context, "^https?:\\/\\/[0-9a-z]{5,}\\.com\\/.*", .{ .case_insensitive = true }, null);
+ const regex = try context.compile("^https?:\\/\\/[0-9a-z]{5,}\\.com\\/.*", .{ .case_insensitive = true }, null);
defer regex.deinit();
try testing.expect(regex.matches("https://abcde.com/x"));
@@ -199,12 +199,12 @@ test "Regex: JavaScript escapes and unanchored search" {
try testing.expect(!regex.matches("https://abcd.com/x"));
try testing.expect(!regex.matches("https://abcde.org/x"));
- const invoke = try Regex.compile(context, "\\/[0-9a-f]{32}\\/invoke\\.js", .{ .case_insensitive = true }, null);
+ const invoke = try context.compile("\\/[0-9a-f]{32}\\/invoke\\.js", .{ .case_insensitive = true }, null);
defer invoke.deinit();
try testing.expect(invoke.matches("https://host.com/0123456789abcdef0123456789abcdef/invoke.js"));
try testing.expect(!invoke.matches("https://host.com/0123456789abcdef0123456789abcde/invoke.js"));
- const dash = try Regex.compile(context, "[a-z\\-]+\\?s=", .{ .case_insensitive = true }, null);
+ const dash = try context.compile("[a-z\\-]+\\?s=", .{ .case_insensitive = true }, null);
defer dash.deinit();
try testing.expect(dash.matches("https://x.com/a-b?s=1"));
try testing.expect(!dash.matches("https://x.com/?s=1"));
@@ -214,7 +214,7 @@ test "Regex: case is kept by default" {
const context: *Context = try .init(testing.allocator);
defer context.deinit();
- const exact = try Regex.compile(context, "\\/[a-z0-9]{12}\\/[a-zA-Z0-9]{20,}$", .{}, null);
+ const exact = try context.compile("\\/[a-z0-9]{12}\\/[a-zA-Z0-9]{20,}$", .{}, null);
defer exact.deinit();
try testing.expect(exact.matches("https://x.com/abcdef123456/aBcDeFgHiJkLmNoPqRsTuV"));
try testing.expect(!exact.matches("https://x.com/ABCDEF123456/aBcDeFgHiJkLmNoPqRsTuV"));
@@ -224,17 +224,17 @@ test "Regex: invalid patterns are errors, runaway ones no match" {
const context: *Context = try .init(testing.allocator);
defer context.deinit();
- try testing.expectError(error.InvalidRegex, Regex.compile(context, "(", .{}, null));
- try testing.expectError(error.InvalidRegex, Regex.compile(context, "a{2,1}", .{}, null));
+ try testing.expectError(error.InvalidRegex, context.compile("(", .{}, null));
+ try testing.expectError(error.InvalidRegex, context.compile("a{2,1}", .{}, null));
// An unknown alphanumeric escape is the literal, as in JavaScript.
- const literal = try Regex.compile(context, "\\q", .{ .case_insensitive = true }, null);
+ const literal = try context.compile("\\q", .{ .case_insensitive = true }, null);
defer literal.deinit();
try testing.expect(literal.matches("https://x.com/q"));
// Exponential backtracking stops at the match limit instead of stalling
// the caller.
- const runaway = try Regex.compile(context, "^(a+)+$", .{ .case_insensitive = true }, null);
+ const runaway = try context.compile("^(a+)+$", .{ .case_insensitive = true }, null);
defer runaway.deinit();
const subject = "a" ** 64 ++ "b";
try testing.expect(!runaway.matches(subject));
@@ -246,7 +246,7 @@ test "Regex: a diagnostic names the fault and where it is" {
defer context.deinit();
var diag: Diagnostic = .{};
- try testing.expectError(error.InvalidRegex, Regex.compile(context, "ab(", .{}, &diag));
+ try testing.expectError(error.InvalidRegex, context.compile("ab(", .{}, &diag));
try testing.expectString("missing closing parenthesis", diag.message());
try testing.expectEqual(3, diag.offset);
}
@@ -255,28 +255,28 @@ test "Regex: unicode folds case beyond ASCII and tolerates invalid bytes" {
const context: *Context = try .init(testing.allocator);
defer context.deinit();
- const ascii = try Regex.compile(context, "^реклама$", .{ .case_insensitive = true }, null);
+ const ascii = try context.compile("^реклама$", .{ .case_insensitive = true }, null);
defer ascii.deinit();
try testing.expect(ascii.matches("реклама"));
try testing.expect(!ascii.matches("Реклама"));
- const unicode = try Regex.compile(context, "^реклама$", .{ .case_insensitive = true, .unicode = true }, null);
+ const unicode = try context.compile("^реклама$", .{ .case_insensitive = true, .unicode = true }, null);
defer unicode.deinit();
try testing.expect(unicode.matches("Реклама"));
try testing.expect(unicode.matches("РЕКЛАМА"));
// One code point, not one byte.
- const single = try Regex.compile(context, "^.$", .{ .unicode = true }, null);
+ const single = try context.compile("^.$", .{ .unicode = true }, null);
defer single.deinit();
try testing.expect(single.matches("é"));
try testing.expect(!single.matches("ab"));
// Word boundaries stay ASCII, as in JavaScript.
- const word = try Regex.compile(context, "\\bshare\\b", .{ .unicode = true }, null);
+ const word = try context.compile("\\bshare\\b", .{ .unicode = true }, null);
defer word.deinit();
try testing.expect(word.matches("éshare"));
- const sidebar = try Regex.compile(context, "sidebar", .{ .unicode = true }, null);
+ const sidebar = try context.compile("sidebar", .{ .unicode = true }, null);
defer sidebar.deinit();
try testing.expect(sidebar.matches("sidebar\xFF"));
try testing.expect(!sidebar.matches("\xFF"));
diff --git a/src/browser/interactive.zig b/src/browser/interactive.zig
index 0b1df93d4..d6ceabf04 100644
--- a/src/browser/interactive.zig
+++ b/src/browser/interactive.zig
@@ -507,14 +507,14 @@ test "browser.interactive: a name regex filters the walk" {
const context = testing.test_app.regex_context;
const options: Regex.Options = .{ .case_insensitive = true, .unicode = true };
- const starts_add = try Regex.compile(context, "^add", options, null);
+ const starts_add = try context.compile("^add", options, null);
defer starts_add.deinit();
const found_add = try findInteractiveElements(div.asNode(), frame.call_arena, frame, .{ .name_regex = starts_add });
try testing.expectEqual(2, found_add.len);
try testing.expectEqual("Add to cart", found_add[0].name.?);
try testing.expectEqual("Add item", found_add[1].name.?);
- const only_cart = try Regex.compile(context, "^cart$", options, null);
+ const only_cart = try context.compile("^cart$", options, null);
defer only_cart.deinit();
const found_cart = try findInteractiveElements(div.asNode(), frame.call_arena, frame, .{ .name_regex = only_cart });
try testing.expectEqual(1, found_cart.len);
diff --git a/src/browser/tools.zig b/src/browser/tools.zig
index 777cf1525..35690099c 100644
--- a/src/browser/tools.zig
+++ b/src/browser/tools.zig
@@ -2076,7 +2076,7 @@ fn execFindElement(arena: std.mem.Allocator, session: *lp.Session, registry: *No
const pattern: ?[]const u8 = if (args.name) |name| regexBody(name) else null;
var diag: lp.Regex.Diagnostic = .{};
const name_regex: ?lp.Regex = if (pattern) |p|
- lp.Regex.compile(session.browser.app.regex_context, p, .{ .case_insensitive = true, .unicode = true }, &diag) catch |err| switch (err) {
+ session.browser.app.regex_context.compile(p, .{ .case_insensitive = true, .unicode = true }, &diag) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.InvalidRegex => return .{
.text = try std.fmt.allocPrint(arena, "findElement: invalid name regex '{s}': {s} at offset {d}", .{ p, diag.message(), diag.offset }),
diff --git a/src/network/adblock/AdBlocker.zig b/src/network/adblock/AdBlocker.zig
index 255b4c206..b078cfd2c 100644
--- a/src/network/adblock/AdBlocker.zig
+++ b/src/network/adblock/AdBlocker.zig
@@ -223,7 +223,7 @@ pub fn parse(self: *AdBlocker, reader: *Io.Reader) !void {
if (filter.kind == .regex) {
const body = filter.pattern[1 .. filter.pattern.len - 1];
var diag: Regex.Diagnostic = .{};
- const compiled = Regex.compile(self.regex_context, body, .{ .case_insensitive = !filter.match_case }, &diag) catch |err| switch (err) {
+ const compiled = self.regex_context.compile(body, .{ .case_insensitive = !filter.match_case }, &diag) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.InvalidRegex => {
log.debug(.app, "adblock regex rejected", .{