also looking at this
Migrate to zig 0.16
#5
13 files
+222
-239
| @@ -1,11 +1,7 @@ | ||
| 1 | 1 | const std = ; |
| 2 | 2 | const Regex = .Regex; |
| 3 | 3 | |
| 4 | pub fn main() !void { | |
| 5 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 6 | defer _ = gpa.deinit(); | |
| 7 | const allocator = gpa.allocator(); | |
| 8 | ||
| 4 | pub fn main(init: std.process.Init) !void { | |
| 9 | 5 | std.debug.print("=== Literal Prefix Optimization Benchmarks ===\n\n", .{}); |
| 10 | 6 | |
| 11 | 7 | // Create a long text where the pattern appears at the end |
| @@ -14,7 +10,7 @@ pub fn main() !void { | ||
| 14 | 10 | // Test 1: Pattern with literal prefix (should use optimization) |
| 15 | 11 | { |
| 16 | 12 | std.debug.print("Test 1: Literal prefix optimization (FOUND_IT!)...\n", .{}); |
| 17 | var regex = try Regex.compile(allocator, "FOUND_IT!"); | |
| 13 | var regex = try Regex.compile(init.gpa, "FOUND_IT!"); | |
| 18 | 14 | defer regex.deinit(); |
| 19 | 15 | |
| 20 | 16 | // Print optimization info |
| @@ -24,31 +20,32 @@ pub fn main() !void { | ||
| 24 | 20 | std.debug.print(" ✗ No literal prefix found\n", .{}); |
| 25 | 21 | } |
| 26 | 22 | |
| 27 | var timer = try std.time.Timer.start(); | |
| 28 | const start = timer.read(); | |
| 23 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 29 | 24 | |
| 30 | 25 | const iterations: usize = 10000; |
| 31 | 26 | var i: usize = 0; |
| 32 | 27 | while (i < iterations) : (i += 1) { |
| 33 | 28 | if (try regex.find(long_text)) |match| { |
| 34 | 29 | var mut_match = match; |
| 35 | mut_match.deinit(allocator); | |
| 30 | mut_match.deinit(init.gpa); | |
| 36 | 31 | } |
| 37 | 32 | } |
| 38 | 33 | |
| 39 | const elapsed = timer.read() - start; | |
| 40 | const avg_ns = elapsed / iterations; | |
| 34 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 35 | const elapsed = start.durationTo(end); | |
| 36 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 37 | ||
| 41 | 38 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 42 | 39 | iterations, |
| 43 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 44 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 40 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 41 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 45 | 42 | }); |
| 46 | 43 | } |
| 47 | 44 | |
| 48 | 45 | // Test 2: Pattern without useful prefix (no optimization) - using fewer iterations |
| 49 | 46 | { |
| 50 | 47 | std.debug.print("Test 2: No prefix optimization (.*FOUND)...\n", .{}); |
| 51 | var regex = try Regex.compile(allocator, ".*FOUND"); | |
| 48 | var regex = try Regex.compile(init.gpa, ".*FOUND"); | |
| 52 | 49 | defer regex.deinit(); |
| 53 | 50 | |
| 54 | 51 | if (regex.opt_info.literal_prefix) |prefix| { |
| @@ -57,8 +54,7 @@ pub fn main() !void { | ||
| 57 | 54 | std.debug.print(" ✗ No literal prefix found (slower performance expected)\n", .{}); |
| 58 | 55 | } |
| 59 | 56 | |
| 60 | var timer = try std.time.Timer.start(); | |
| 61 | const start = timer.read(); | |
| 57 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 62 | 58 | |
| 63 | 59 | // Use fewer iterations since this is much slower without optimization |
| 64 | 60 | const iterations: usize = 100; |
| @@ -66,16 +62,18 @@ pub fn main() !void { | ||
| 66 | 62 | while (i < iterations) : (i += 1) { |
| 67 | 63 | if (try regex.find(long_text)) |match| { |
| 68 | 64 | var mut_match = match; |
| 69 | mut_match.deinit(allocator); | |
| 65 | mut_match.deinit(init.gpa); | |
| 70 | 66 | } |
| 71 | 67 | } |
| 72 | 68 | |
| 73 | const elapsed = timer.read() - start; | |
| 74 | const avg_ns = elapsed / iterations; | |
| 69 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 70 | const elapsed = start.durationTo(end); | |
| 71 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 72 | ||
| 75 | 73 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 76 | 74 | iterations, |
| 77 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 78 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 75 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 76 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 79 | 77 | }); |
| 80 | 78 | } |
| 81 | 79 | |
| @@ -83,7 +81,7 @@ pub fn main() !void { | ||
| 83 | 81 | { |
| 84 | 82 | std.debug.print("Test 3: Prefix with complex suffix (hello.*world)...\n", .{}); |
| 85 | 83 | const text_with_hello = "blah blah " ** 50 ++ "hello there world"; |
| 86 | var regex = try Regex.compile(allocator, "hello.*world"); | |
| 84 | var regex = try Regex.compile(init.gpa, "hello.*world"); | |
| 87 | 85 | defer regex.deinit(); |
| 88 | 86 | |
| 89 | 87 | if (regex.opt_info.literal_prefix) |prefix| { |
| @@ -92,31 +90,31 @@ pub fn main() !void { | ||
| 92 | 90 | std.debug.print(" ✗ No literal prefix found\n", .{}); |
| 93 | 91 | } |
| 94 | 92 | |
| 95 | var timer = try std.time.Timer.start(); | |
| 96 | const start = timer.read(); | |
| 93 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 97 | 94 | |
| 98 | 95 | const iterations: usize = 10000; |
| 99 | 96 | var i: usize = 0; |
| 100 | 97 | while (i < iterations) : (i += 1) { |
| 101 | 98 | if (try regex.find(text_with_hello)) |match| { |
| 102 | 99 | var mut_match = match; |
| 103 | mut_match.deinit(allocator); | |
| 100 | mut_match.deinit(init.gpa); | |
| 104 | 101 | } |
| 105 | 102 | } |
| 106 | 103 | |
| 107 | const elapsed = timer.read() - start; | |
| 108 | const avg_ns = elapsed / iterations; | |
| 104 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 105 | const elapsed = start.durationTo(end); | |
| 106 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 109 | 107 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 110 | 108 | iterations, |
| 111 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 112 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 109 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 110 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 113 | 111 | }); |
| 114 | 112 | } |
| 115 | 113 | |
| 116 | 114 | // Test 4: Anchored patterns |
| 117 | 115 | { |
| 118 | 116 | std.debug.print("Test 4: Start-anchored pattern (^hello)...\n", .{}); |
| 119 | var regex = try Regex.compile(allocator, "^hello"); | |
| 117 | var regex = try Regex.compile(init.gpa, "^hello"); | |
| 120 | 118 | defer regex.deinit(); |
| 121 | 119 | |
| 122 | 120 | if (regex.opt_info.anchored_start) { |
| @@ -127,8 +125,7 @@ pub fn main() !void { | ||
| 127 | 125 | } |
| 128 | 126 | |
| 129 | 127 | const text = "hello world"; |
| 130 | var timer = try std.time.Timer.start(); | |
| 131 | const start = timer.read(); | |
| 128 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 132 | 129 | |
| 133 | 130 | const iterations: usize = 10000; |
| 134 | 131 | var i: usize = 0; |
| @@ -136,12 +133,13 @@ pub fn main() !void { | ||
| 136 | 133 | _ = try regex.isMatch(text); |
| 137 | 134 | } |
| 138 | 135 | |
| 139 | const elapsed = timer.read() - start; | |
| 140 | const avg_ns = elapsed / iterations; | |
| 136 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 137 | const elapsed = start.durationTo(end); | |
| 138 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 141 | 139 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 142 | 140 | iterations, |
| 143 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 144 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 141 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 142 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 145 | 143 | }); |
| 146 | 144 | } |
| 147 | 145 | |
| @@ -149,31 +147,31 @@ pub fn main() !void { | ||
| 149 | 147 | { |
| 150 | 148 | std.debug.print("Test 5: Email pattern (user@example.com)...\n", .{}); |
| 151 | 149 | const email_text = "Contact us at " ++ ("some filler text here. " ** 20) ++ "user@example.com for support"; |
| 152 | var regex = try Regex.compile(allocator, "user@example.com"); | |
| 150 | var regex = try Regex.compile(init.gpa, "user@example.com"); | |
| 153 | 151 | defer regex.deinit(); |
| 154 | 152 | |
| 155 | 153 | if (regex.opt_info.literal_prefix) |prefix| { |
| 156 | std.debug.print(" ✓ Using literal prefix: \"{s}\" (min_len={d})\n", .{prefix, regex.opt_info.min_length}); | |
| 154 | std.debug.print(" ✓ Using literal prefix: \"{s}\" (min_len={d})\n", .{ prefix, regex.opt_info.min_length }); | |
| 157 | 155 | } |
| 158 | 156 | |
| 159 | var timer = try std.time.Timer.start(); | |
| 160 | const start = timer.read(); | |
| 157 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 161 | 158 | |
| 162 | 159 | const iterations: usize = 10000; |
| 163 | 160 | var i: usize = 0; |
| 164 | 161 | while (i < iterations) : (i += 1) { |
| 165 | 162 | if (try regex.find(email_text)) |match| { |
| 166 | 163 | var mut_match = match; |
| 167 | mut_match.deinit(allocator); | |
| 164 | mut_match.deinit(init.gpa); | |
| 168 | 165 | } |
| 169 | 166 | } |
| 170 | 167 | |
| 171 | const elapsed = timer.read() - start; | |
| 172 | const avg_ns = elapsed / iterations; | |
| 168 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 169 | const elapsed = start.durationTo(end); | |
| 170 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 173 | 171 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 174 | 172 | iterations, |
| 175 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 176 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 173 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 174 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 177 | 175 | }); |
| 178 | 176 | } |
| 179 | 177 | |
| @@ -1,45 +1,41 @@ | ||
| 1 | 1 | const std = ; |
| 2 | 2 | const Regex = .Regex; |
| 3 | 3 | |
| 4 | pub fn main() !void { | |
| 5 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 6 | defer _ = gpa.deinit(); | |
| 7 | const allocator = gpa.allocator(); | |
| 8 | ||
| 4 | pub fn main(init: std.process.Init) !void { | |
| 9 | 5 | std.debug.print("=== Simple Regex Benchmarks ===\n\n", .{}); |
| 10 | 6 | |
| 11 | 7 | // Test 1: Literal matching |
| 12 | 8 | { |
| 13 | 9 | std.debug.print("Test 1: Literal matching...\n", .{}); |
| 14 | var regex = try Regex.compile(allocator, "hello"); | |
| 10 | var regex = try Regex.compile(init.gpa, "hello"); | |
| 15 | 11 | defer regex.deinit(); |
| 16 | 12 | |
| 17 | var timer = try std.time.Timer.start(); | |
| 18 | const start = timer.read(); | |
| 19 | ||
| 13 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 20 | 14 | const iterations: usize = 10000; |
| 21 | 15 | var i: usize = 0; |
| 22 | 16 | while (i < iterations) : (i += 1) { |
| 23 | 17 | _ = try regex.isMatch("hello world"); |
| 24 | 18 | } |
| 25 | 19 | |
| 26 | const elapsed = timer.read() - start; | |
| 27 | const avg_ns = elapsed / iterations; | |
| 20 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 21 | const elapsed = start.durationTo(end); | |
| 22 | ||
| 23 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 24 | ||
| 28 | 25 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 29 | 26 | iterations, |
| 30 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 31 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 27 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 28 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 32 | 29 | }); |
| 33 | 30 | } |
| 34 | 31 | |
| 35 | 32 | // Test 2: Quantifiers |
| 36 | 33 | { |
| 37 | 34 | std.debug.print("Test 2: Quantifiers (a+)...\n", .{}); |
| 38 | var regex = try Regex.compile(allocator, "a+"); | |
| 35 | var regex = try Regex.compile(init.gpa, "a+"); | |
| 39 | 36 | defer regex.deinit(); |
| 40 | 37 | |
| 41 | var timer = try std.time.Timer.start(); | |
| 42 | const start = timer.read(); | |
| 38 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 43 | 39 | |
| 44 | 40 | const iterations: usize = 10000; |
| 45 | 41 | var i: usize = 0; |
| @@ -47,23 +43,24 @@ pub fn main() !void { | ||
| 47 | 43 | _ = try regex.isMatch("aaaa"); |
| 48 | 44 | } |
| 49 | 45 | |
| 50 | const elapsed = timer.read() - start; | |
| 51 | const avg_ns = elapsed / iterations; | |
| 46 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 47 | const elapsed = start.durationTo(end); | |
| 48 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 49 | ||
| 52 | 50 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 53 | 51 | iterations, |
| 54 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 55 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 52 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 53 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 56 | 54 | }); |
| 57 | 55 | } |
| 58 | 56 | |
| 59 | 57 | // Test 3: Character classes |
| 60 | 58 | { |
| 61 | 59 | std.debug.print("Test 3: Digit matching (\\d+)...\n", .{}); |
| 62 | var regex = try Regex.compile(allocator, "\\d+"); | |
| 60 | var regex = try Regex.compile(init.gpa, "\\d+"); | |
| 63 | 61 | defer regex.deinit(); |
| 64 | 62 | |
| 65 | var timer = try std.time.Timer.start(); | |
| 66 | const start = timer.read(); | |
| 63 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 67 | 64 | |
| 68 | 65 | const iterations: usize = 10000; |
| 69 | 66 | var i: usize = 0; |
| @@ -71,23 +68,24 @@ pub fn main() !void { | ||
| 71 | 68 | _ = try regex.isMatch("12345"); |
| 72 | 69 | } |
| 73 | 70 | |
| 74 | const elapsed = timer.read() - start; | |
| 75 | const avg_ns = elapsed / iterations; | |
| 71 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 72 | const elapsed = start.durationTo(end); | |
| 73 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 74 | ||
| 76 | 75 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 77 | 76 | iterations, |
| 78 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 79 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 77 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 78 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 80 | 79 | }); |
| 81 | 80 | } |
| 82 | 81 | |
| 83 | 82 | // Test 4: Case-insensitive |
| 84 | 83 | { |
| 85 | 84 | std.debug.print("Test 4: Case-insensitive matching...\n", .{}); |
| 86 | var regex = try Regex.compileWithFlags(allocator, "hello", .{ .case_insensitive = true }); | |
| 85 | var regex = try Regex.compileWithFlags(init.gpa, "hello", .{ .case_insensitive = true }); | |
| 87 | 86 | defer regex.deinit(); |
| 88 | 87 | |
| 89 | var timer = try std.time.Timer.start(); | |
| 90 | const start = timer.read(); | |
| 88 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 91 | 89 | |
| 92 | 90 | const iterations: usize = 10000; |
| 93 | 91 | var i: usize = 0; |
| @@ -95,12 +93,14 @@ pub fn main() !void { | ||
| 95 | 93 | _ = try regex.isMatch("HELLO"); |
| 96 | 94 | } |
| 97 | 95 | |
| 98 | const elapsed = timer.read() - start; | |
| 99 | const avg_ns = elapsed / iterations; | |
| 96 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 97 | const elapsed = start.durationTo(end); | |
| 98 | const avg_ns = @divTrunc(elapsed.nanoseconds, iterations); | |
| 99 | ||
| 100 | 100 | std.debug.print(" {d} iterations in {d:.2}ms ({d:.2}µs/op)\n\n", .{ |
| 101 | 101 | iterations, |
| 102 | @as(f64, @floatFromInt(elapsed)) / 1_000_000.0, | |
| 103 | @as(f64, @floatFromInt(avg_ns)) / 1000.0, | |
| 102 | @as(f64, @floatFromInt(elapsed.toMilliseconds())), | |
| 103 | @as(f64, @floatFromInt(avg_ns)) / std.time.us_per_ms, | |
| 104 | 104 | }); |
| 105 | 105 | } |
| 106 | 106 | |
| @@ -39,6 +39,7 @@ pub fn build(b: *std.Build) void { | ||
| 39 | 39 | // Later on we'll use this module as the root module of a test executable |
| 40 | 40 | // which requires us to specify a target. |
| 41 | 41 | .target = target, |
| 42 | .link_libc = true, | |
| 42 | 43 | }); |
| 43 | 44 | |
| 44 | 45 | // Here we define an executable. An executable needs to have a root module |
| @@ -6,11 +6,7 @@ const Composer = .Composer; | ||
| 6 | 6 | const Lint = .Lint; |
| 7 | 7 | const ComplexityAnalyzer = .ComplexityAnalyzer; |
| 8 | 8 | |
| 9 | pub fn main() !void { | |
| 10 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 11 | defer _ = gpa.deinit(); | |
| 12 | const allocator = gpa.allocator(); | |
| 13 | ||
| 9 | pub fn main(init: std.process.Init) !void { | |
| 14 | 10 | std.debug.print("\n=== Advanced Regex Features Examples ===\n\n", .{}); |
| 15 | 11 | |
| 16 | 12 | // Example 1: Builder API - Type-safe pattern construction |
| @@ -19,7 +15,7 @@ pub fn main() !void { | ||
| 19 | 15 | std.debug.print("Example 1: Builder API for Type-Safe Construction\n", .{}); |
| 20 | 16 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 21 | 17 | |
| 22 | var builder = Builder.init(allocator); | |
| 18 | var builder = Builder.init(init.gpa); | |
| 23 | 19 | defer builder.deinit(); |
| 24 | 20 | |
| 25 | 21 | // Build a pattern for email validation |
renamed throughout · 13 hidden lines
| @@ -35,11 +31,11 @@ pub fn main() !void { renamed throughout | ||
| 35 | 31 | _ = try builder.endOfLine(); |
| 36 | 32 | |
| 37 | 33 | const pattern = try builder.build(); |
| 38 | defer allocator.free(pattern); | |
| 34 | defer init.gpa.free(pattern); | |
| 39 | 35 | |
| 40 | 36 | std.debug.print("Built pattern: {s}\n", .{pattern}); |
| 41 | 37 | |
| 42 | var regex = try Regex.compile(allocator, pattern); | |
| 38 | var regex = try Regex.compile(init.gpa, pattern); | |
| 43 | 39 | defer regex.deinit(); |
| 44 | 40 | |
| 45 | 41 | const test_emails = [_][]const u8{ |
renamed throughout · 28 hidden lines
| @@ -64,21 +60,21 @@ pub fn main() !void { renamed throughout | ||
| 64 | 60 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 65 | 61 | |
| 66 | 62 | // Use predefined patterns |
| 67 | const email_pattern = try Patterns.email(allocator); | |
| 68 | defer allocator.free(email_pattern); | |
| 63 | const email_pattern = try Patterns.email(init.gpa); | |
| 64 | defer init.gpa.free(email_pattern); | |
| 69 | 65 | |
| 70 | const url_pattern = try Patterns.url(allocator); | |
| 71 | defer allocator.free(url_pattern); | |
| 66 | const url_pattern = try Patterns.url(init.gpa); | |
| 67 | defer init.gpa.free(url_pattern); | |
| 72 | 68 | |
| 73 | const phone_pattern = try Patterns.phoneUS(allocator); | |
| 74 | defer allocator.free(phone_pattern); | |
| 69 | const phone_pattern = try Patterns.phoneUS(init.gpa); | |
| 70 | defer init.gpa.free(phone_pattern); | |
| 75 | 71 | |
| 76 | 72 | std.debug.print("Email pattern: {s}\n", .{email_pattern}); |
| 77 | 73 | std.debug.print("URL pattern: {s}\n", .{url_pattern}); |
| 78 | 74 | std.debug.print("Phone pattern: {s}\n\n", .{phone_pattern}); |
| 79 | 75 | |
| 80 | 76 | // Test phone number pattern |
| 81 | var phone_regex = try Regex.compile(allocator, phone_pattern); | |
| 77 | var phone_regex = try Regex.compile(init.gpa, phone_pattern); | |
| 82 | 78 | defer phone_regex.deinit(); |
| 83 | 79 | |
| 84 | 80 | const test_phones = [_][]const u8{ |
| @@ -103,7 +99,7 @@ pub fn main() !void { | ||
| 103 | 99 | std.debug.print("Example 3: Pattern Composition\n", .{}); |
| 104 | 100 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 105 | 101 | |
| 106 | var composer = Composer.init(allocator); | |
| 102 | var composer = Composer.init(init.gpa); | |
| 107 | 103 | defer composer.deinit(); |
| 108 | 104 | |
| 109 | 105 | // Compose multiple patterns with OR |
renamed throughout · 13 hidden lines
| @@ -112,11 +108,11 @@ pub fn main() !void { renamed throughout | ||
| 112 | 108 | _ = try composer.add("bird"); |
| 113 | 109 | |
| 114 | 110 | const alternatives = try composer.alternatives(); |
| 115 | defer allocator.free(alternatives); | |
| 111 | defer init.gpa.free(alternatives); | |
| 116 | 112 | |
| 117 | 113 | std.debug.print("Composed pattern (OR): {s}\n", .{alternatives}); |
| 118 | 114 | |
| 119 | var regex = try Regex.compile(allocator, alternatives); | |
| 115 | var regex = try Regex.compile(init.gpa, alternatives); | |
| 120 | 116 | defer regex.deinit(); |
| 121 | 117 | |
| 122 | 118 | const test_strings = [_][]const u8{ |
| @@ -145,7 +141,7 @@ pub fn main() !void { | ||
| 145 | 141 | const problematic_pattern = "(a+)+b"; |
| 146 | 142 | std.debug.print("Analyzing pattern: \"{s}\"\n\n", .{problematic_pattern}); |
| 147 | 143 | |
| 148 | var lint = Lint.init(allocator, problematic_pattern); | |
| 144 | var lint = Lint.init(init.gpa, problematic_pattern); | |
| 149 | 145 | defer lint.deinit(); |
| 150 | 146 | |
| 151 | 147 | try lint.analyze(); |
| @@ -157,7 +153,7 @@ pub fn main() !void { | ||
| 157 | 153 | const good_pattern = "^[a-z]+$"; |
| 158 | 154 | std.debug.print("Analyzing pattern: \"{s}\"\n\n", .{good_pattern}); |
| 159 | 155 | |
| 160 | var lint2 = Lint.init(allocator, good_pattern); | |
| 156 | var lint2 = Lint.init(init.gpa, good_pattern); | |
| 161 | 157 | defer lint2.deinit(); |
| 162 | 158 | |
| 163 | 159 | try lint2.analyze(); |
| @@ -198,7 +194,7 @@ pub fn main() !void { | ||
| 198 | 194 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 199 | 195 | |
| 200 | 196 | // Build a complex pattern using the builder |
| 201 | var builder = Builder.init(allocator); | |
| 197 | var builder = Builder.init(init.gpa); | |
| 202 | 198 | defer builder.deinit(); |
| 203 | 199 | |
| 204 | 200 | std.debug.print("Building a URL validator:\n", .{}); |
renamed throughout · 15 hidden lines
| @@ -215,13 +211,13 @@ pub fn main() !void { renamed throughout | ||
| 215 | 211 | _ = try builder.endOfLine(); |
| 216 | 212 | |
| 217 | 213 | const url_pattern_built = try builder.build(); |
| 218 | defer allocator.free(url_pattern_built); | |
| 214 | defer init.gpa.free(url_pattern_built); | |
| 219 | 215 | |
| 220 | 216 | std.debug.print("Built pattern: {s}\n\n", .{url_pattern_built}); |
| 221 | 217 | |
| 222 | 218 | // Lint the pattern |
| 223 | 219 | std.debug.print("Linting the pattern:\n", .{}); |
| 224 | var lint = Lint.init(allocator, url_pattern_built); | |
| 220 | var lint = Lint.init(init.gpa, url_pattern_built); | |
| 225 | 221 | defer lint.deinit(); |
| 226 | 222 | |
| 227 | 223 | try lint.analyze(); |
| @@ -232,7 +228,7 @@ pub fn main() !void { | ||
| 232 | 228 | std.debug.print("Complexity: {s} (score: {d})\n\n", .{ @tagName(complexity.getLevel()), complexity.complexity_score }); |
| 233 | 229 | |
| 234 | 230 | // Test the pattern |
| 235 | var regex = try Regex.compile(allocator, url_pattern_built); | |
| 231 | var regex = try Regex.compile(init.gpa, url_pattern_built); | |
| 236 | 232 | defer regex.deinit(); |
| 237 | 233 | |
| 238 | 234 | const test_urls = [_][]const u8{ |
| @@ -257,7 +253,7 @@ pub fn main() !void { | ||
| 257 | 253 | std.debug.print("Example 7: Complex Pattern with Builder\n", .{}); |
| 258 | 254 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 259 | 255 | |
| 260 | var builder = Builder.init(allocator); | |
| 256 | var builder = Builder.init(init.gpa); | |
| 261 | 257 | defer builder.deinit(); |
| 262 | 258 | |
| 263 | 259 | // Build a pattern for matching IPv4 addresses |
renamed throughout · 13 hidden lines
| @@ -301,11 +297,11 @@ pub fn main() !void { renamed throughout | ||
| 301 | 297 | _ = try builder.endOfLine(); |
| 302 | 298 | |
| 303 | 299 | const ipv4_pattern = try builder.build(); |
| 304 | defer allocator.free(ipv4_pattern); | |
| 300 | defer init.gpa.free(ipv4_pattern); | |
| 305 | 301 | |
| 306 | 302 | std.debug.print("Built pattern: {s}\n\n", .{ipv4_pattern}); |
| 307 | 303 | |
| 308 | var regex = try Regex.compile(allocator, ipv4_pattern); | |
| 304 | var regex = try Regex.compile(init.gpa, ipv4_pattern); | |
| 309 | 305 | defer regex.deinit(); |
| 310 | 306 | |
| 311 | 307 | const test_ips = [_][]const u8{ |
| @@ -1,17 +1,13 @@ | ||
| 1 | 1 | const std = ; |
| 2 | 2 | const Regex = .Regex; |
| 3 | 3 | |
| 4 | pub fn main() !void { | |
| 5 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 6 | defer _ = gpa.deinit(); | |
| 7 | const allocator = gpa.allocator(); | |
| 8 | ||
| 4 | pub fn main(init: std.process.Init) !void { | |
| 9 | 5 | std.debug.print("\n=== Zig Regex Examples ===\n\n", .{}); |
| 10 | 6 | |
| 11 | 7 | // Example 1: Simple literal matching |
| 12 | 8 | { |
| 13 | 9 | std.debug.print("Example 1: Literal matching\n", .{}); |
| 14 | var regex = try Regex.compile(allocator, "hello"); | |
| 10 | var regex = try Regex.compile(init.gpa, "hello"); | |
| 15 | 11 | defer regex.deinit(); |
| 16 | 12 | |
| 17 | 13 | const matches = try regex.isMatch("hello"); |
| @@ -22,7 +18,7 @@ pub fn main() !void { | ||
| 22 | 18 | // Example 2: Alternation |
| 23 | 19 | { |
| 24 | 20 | std.debug.print("Example 2: Alternation (cat|dog)\n", .{}); |
| 25 | var regex = try Regex.compile(allocator, "cat|dog"); | |
| 21 | var regex = try Regex.compile(init.gpa, "cat|dog"); | |
| 26 | 22 | defer regex.deinit(); |
| 27 | 23 | |
| 28 | 24 | std.debug.print(" 'cat' matches: {}\n", .{try regex.isMatch("cat")}); |
| @@ -34,7 +30,7 @@ pub fn main() !void { | ||
| 34 | 30 | // Example 3: Star quantifier |
| 35 | 31 | { |
| 36 | 32 | std.debug.print("Example 3: Star quantifier (a*)\n", .{}); |
| 37 | var regex = try Regex.compile(allocator, "a*"); | |
| 33 | var regex = try Regex.compile(init.gpa, "a*"); | |
| 38 | 34 | defer regex.deinit(); |
| 39 | 35 | |
| 40 | 36 | std.debug.print(" '' matches: {}\n", .{try regex.isMatch("")}); |
renamed throughout · 14 hidden lines
| @@ -46,12 +42,12 @@ pub fn main() !void { renamed throughout | ||
| 46 | 42 | // Example 4: Finding matches |
| 47 | 43 | { |
| 48 | 44 | std.debug.print("Example 4: Finding matches\n", .{}); |
| 49 | var regex = try Regex.compile(allocator, "world"); | |
| 45 | var regex = try Regex.compile(init.gpa, "world"); | |
| 50 | 46 | defer regex.deinit(); |
| 51 | 47 | |
| 52 | 48 | if (try regex.find("hello world")) |match_result| { |
| 53 | 49 | var mut_match = match_result; |
| 54 | defer mut_match.deinit(allocator); | |
| 50 | defer mut_match.deinit(init.gpa); | |
| 55 | 51 | std.debug.print(" Found '{s}' at position {d}-{d}\n", .{ match_result.slice, match_result.start, match_result.end }); |
| 56 | 52 | } |
| 57 | 53 | std.debug.print("\n", .{}); |
renamed throughout · 14 hidden lines
| @@ -60,11 +56,11 @@ pub fn main() !void { renamed throughout | ||
| 60 | 56 | // Example 5: Replace |
| 61 | 57 | { |
| 62 | 58 | std.debug.print("Example 5: Replace\n", .{}); |
| 63 | var regex = try Regex.compile(allocator, "world"); | |
| 59 | var regex = try Regex.compile(init.gpa, "world"); | |
| 64 | 60 | defer regex.deinit(); |
| 65 | 61 | |
| 66 | const result = try regex.replace(allocator, "hello world", "Zig"); | |
| 67 | defer allocator.free(result); | |
| 62 | const result = try regex.replace(init.gpa, "hello world", "Zig"); | |
| 63 | defer init.gpa.free(result); | |
| 68 | 64 | |
| 69 | 65 | std.debug.print(" Replaced: '{s}'\n", .{result}); |
| 70 | 66 | std.debug.print("\n", .{}); |
| @@ -4,11 +4,7 @@ const debug = .debug; | ||
| 4 | 4 | const Parser = .parser.Parser; |
| 5 | 5 | const Optimizer = .optimizer.Optimizer; |
| 6 | 6 | |
| 7 | pub fn main() !void { | |
| 8 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 9 | defer _ = gpa.deinit(); | |
| 10 | const allocator = gpa.allocator(); | |
| 11 | ||
| 7 | pub fn main(init: std.process.Init) !void { | |
| 12 | 8 | std.debug.print("\n=== Regex Debugging and Analysis Examples ===\n\n", .{}); |
| 13 | 9 | |
| 14 | 10 | const patterns = [_][]const u8{ |
| @@ -25,7 +21,7 @@ pub fn main() !void { | ||
| 25 | 21 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 26 | 22 | |
| 27 | 23 | // Example 1: Show optimization info |
| 28 | var regex = Regex.compile(allocator, pattern) catch |err| { | |
| 24 | var regex = Regex.compile(init.gpa, pattern) catch |err| { | |
| 29 | 25 | std.debug.print(" ❌ Compilation error: {}\n\n", .{err}); |
| 30 | 26 | continue; |
| 31 | 27 | }; |
renamed throughout · 19 hidden lines
| @@ -54,15 +50,15 @@ pub fn main() !void { renamed throughout | ||
| 54 | 50 | |
| 55 | 51 | // Example 2: Pattern analysis |
| 56 | 52 | { |
| 57 | var parser = Parser.init(allocator, pattern) catch continue; | |
| 53 | var parser = Parser.init(init.gpa, pattern) catch continue; | |
| 58 | 54 | var tree = parser.parse() catch continue; |
| 59 | 55 | defer tree.deinit(); |
| 60 | 56 | |
| 61 | var opt = Optimizer.init(allocator); | |
| 57 | var opt = Optimizer.init(init.gpa); | |
| 62 | 58 | var opt_info = try opt.analyze(tree.root); |
| 63 | defer opt_info.deinit(allocator); | |
| 59 | defer opt_info.deinit(init.gpa); | |
| 64 | 60 | |
| 65 | var analyzer = debug.PatternAnalyzer.init(allocator); | |
| 61 | var analyzer = debug.PatternAnalyzer.init(init.gpa); | |
| 66 | 62 | var analysis = try analyzer.analyze(pattern, &tree, &opt_info); |
| 67 | 63 | defer analysis.deinit(); |
| 68 | 64 | |
| @@ -88,7 +84,7 @@ pub fn main() !void { | ||
| 88 | 84 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 89 | 85 | |
| 90 | 86 | const test_pattern = "hello.*world"; |
| 91 | var test_regex = try Regex.compile(allocator, test_pattern); | |
| 87 | var test_regex = try Regex.compile(init.gpa, test_pattern); | |
| 92 | 88 | defer test_regex.deinit(); |
| 93 | 89 | |
| 94 | 90 | const test_inputs = [_][]const u8{ |
| @@ -4,20 +4,16 @@ const ErrorContext = .ErrorContext; | ||
| 4 | 4 | const ErrorHelper = .ErrorHelper; |
| 5 | 5 | const RegexError = .RegexError; |
| 6 | 6 | |
| 7 | pub fn main() !void { | |
| 8 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 9 | defer _ = gpa.deinit(); | |
| 10 | const allocator = gpa.allocator(); | |
| 11 | ||
| 7 | pub fn main(init: std.process.Init) !void { | |
| 12 | 8 | std.debug.print("\n=== Regex Error Handling Examples ===\n\n", .{}); |
| 13 | 9 | |
| 14 | 10 | // Example 1: Invalid patterns with helpful error messages |
| 15 | 11 | const invalid_patterns = [_][]const u8{ |
| 16 | "abc(def", // Unmatched parenthesis | |
| 17 | "abc[def", // Unmatched bracket | |
| 18 | "abc+(", // Invalid quantifier usage | |
| 19 | "abc\\q", // Invalid escape sequence | |
| 20 | "[z-a]", // Invalid character range | |
| 12 | "abc(def", // Unmatched parenthesis | |
| 13 | "abc[def", // Unmatched bracket | |
| 14 | "abc+(", // Invalid quantifier usage | |
| 15 | "abc\\q", // Invalid escape sequence | |
| 16 | "[z-a]", // Invalid character range | |
| 21 | 17 | "(a(b(c(d(e(f(g(h(i(j(k(l(m(n(o(p(q", // Too many nested groups |
| 22 | 18 | }; |
| 23 | 19 | |
| @@ -26,7 +22,7 @@ pub fn main() !void { | ||
| 26 | 22 | std.debug.print("Testing pattern: \"{s}\"\n", .{pattern}); |
| 27 | 23 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 28 | 24 | |
| 29 | if (Regex.compile(allocator, pattern)) |regex_value| { | |
| 25 | if (Regex.compile(init.gpa, pattern)) |regex_value| { | |
| 30 | 26 | var regex = regex_value; |
| 31 | 27 | defer regex.deinit(); |
| 32 | 28 | std.debug.print("✓ Pattern compiled successfully (unexpected)\n\n", .{}); |
| @@ -53,7 +49,7 @@ pub fn main() !void { | ||
| 53 | 49 | const user_pattern = "hello|world"; |
| 54 | 50 | std.debug.print("User provided pattern: \"{s}\"\n", .{user_pattern}); |
| 55 | 51 | |
| 56 | if (Regex.compile(allocator, user_pattern)) |regex_value| { | |
| 52 | if (Regex.compile(init.gpa, user_pattern)) |regex_value| { | |
| 57 | 53 | var regex = regex_value; |
| 58 | 54 | defer regex.deinit(); |
| 59 | 55 | std.debug.print("✓ Pattern compiled successfully\n", .{}); |
| @@ -84,7 +80,7 @@ pub fn main() !void { | ||
| 84 | 80 | const problematic_pattern = "abc[def"; |
| 85 | 81 | std.debug.print("Problematic pattern: \"{s}\"\n", .{problematic_pattern}); |
| 86 | 82 | |
| 87 | if (Regex.compile(allocator, problematic_pattern)) |regex_value| { | |
| 83 | if (Regex.compile(init.gpa, problematic_pattern)) |regex_value| { | |
| 88 | 84 | var regex = regex_value; |
| 89 | 85 | regex.deinit(); |
| 90 | 86 | } else |err| { |
| @@ -95,7 +91,7 @@ pub fn main() !void { | ||
| 95 | 91 | const fixed_pattern = "abc[def]"; |
| 96 | 92 | std.debug.print("Fixed pattern: \"{s}\"\n", .{fixed_pattern}); |
| 97 | 93 | |
| 98 | if (Regex.compile(allocator, fixed_pattern)) |regex_value| { | |
| 94 | if (Regex.compile(init.gpa, fixed_pattern)) |regex_value| { | |
| 99 | 95 | var regex = regex_value; |
| 100 | 96 | defer regex.deinit(); |
| 101 | 97 | std.debug.print("✓ Fixed pattern compiles successfully!\n", .{}); |
| @@ -119,7 +115,7 @@ pub fn main() !void { | ||
| 119 | 115 | |
| 120 | 116 | for (patterns_to_validate) |pattern| { |
| 121 | 117 | std.debug.print("Validating: \"{s}\" ... ", .{pattern}); |
| 122 | if (Regex.compile(allocator, pattern)) |regex_value| { | |
| 118 | if (Regex.compile(init.gpa, pattern)) |regex_value| { | |
| 123 | 119 | var regex = regex_value; |
| 124 | 120 | regex.deinit(); |
| 125 | 121 | std.debug.print("✓ Valid\n", .{}); |
| @@ -3,11 +3,7 @@ const Regex = .Regex; | ||
| 3 | 3 | const Profiler = .Profiler; |
| 4 | 4 | const ScopedTimer = .ScopedTimer; |
| 5 | 5 | |
| 6 | pub fn main() !void { | |
| 7 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | |
| 8 | defer _ = gpa.deinit(); | |
| 9 | const allocator = gpa.allocator(); | |
| 10 | ||
| 6 | pub fn main(init: std.process.Init) !void { | |
| 11 | 7 | std.debug.print("\n=== Regex Profiling Examples ===\n\n", .{}); |
| 12 | 8 | |
| 13 | 9 | // Example 1: Basic profiling |
| @@ -16,12 +12,12 @@ pub fn main() !void { | ||
| 16 | 12 | std.debug.print("Example 1: Basic profiling\n", .{}); |
| 17 | 13 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 18 | 14 | |
| 19 | var profiler = Profiler.init(allocator, true); | |
| 15 | var profiler = Profiler.init(init.gpa, init.io, true); | |
| 20 | 16 | |
| 21 | 17 | // Profile compilation |
| 22 | profiler.startCompilation(); | |
| 23 | var regex = try Regex.compile(allocator, "hello.*world"); | |
| 24 | profiler.endCompilation(); | |
| 18 | profiler.startCompilation(init.io); | |
| 19 | var regex = try Regex.compile(init.gpa, "hello.*world"); | |
| 20 | profiler.endCompilation(init.io); | |
| 25 | 21 | defer regex.deinit(); |
| 26 | 22 | |
| 27 | 23 | // Profile multiple matches |
| @@ -33,9 +29,9 @@ pub fn main() !void { | ||
| 33 | 29 | }; |
| 34 | 30 | |
| 35 | 31 | for (test_inputs) |input| { |
| 36 | profiler.startMatch(); | |
| 32 | profiler.startMatch(init.io); | |
| 37 | 33 | _ = try regex.isMatch(input); |
| 38 | profiler.endMatch(); | |
| 34 | profiler.endMatch(init.io); | |
| 39 | 35 | } |
| 40 | 36 | |
| 41 | 37 | std.debug.print("\n", .{}); |
| @@ -48,13 +44,13 @@ pub fn main() !void { | ||
| 48 | 44 | std.debug.print("Example 2: Scoped timers\n", .{}); |
| 49 | 45 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 50 | 46 | |
| 51 | var profiler = Profiler.init(allocator, true); | |
| 47 | var profiler = Profiler.init(init.gpa, init.io, true); | |
| 52 | 48 | |
| 53 | 49 | { |
| 54 | var timer = ScopedTimer.init(&profiler, .compilation); | |
| 55 | defer timer.deinit(); | |
| 50 | var timer = ScopedTimer.init(&profiler, init.io, .compilation); | |
| 51 | defer timer.deinit(init.io); | |
| 56 | 52 | |
| 57 | var regex = try Regex.compile(allocator, "[a-z]+@[a-z]+\\.[a-z]+"); | |
| 53 | var regex = try Regex.compile(init.gpa, "[a-z]+@[a-z]+\\.[a-z]+"); | |
| 58 | 54 | regex.deinit(); |
| 59 | 55 | } |
| 60 | 56 | |
| @@ -69,33 +65,34 @@ pub fn main() !void { | ||
| 69 | 65 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 70 | 66 | |
| 71 | 67 | const patterns = [_][]const u8{ |
| 72 | "hello", // Literal - should have prefix optimization | |
| 73 | ".*hello", // No prefix - slower | |
| 74 | "hello.*world", // Prefix optimization | |
| 68 | "hello", // Literal - should have prefix optimization | |
| 69 | ".*hello", // No prefix - slower | |
| 70 | "hello.*world", // Prefix optimization | |
| 75 | 71 | }; |
| 76 | 72 | |
| 77 | 73 | const test_text = ("some random text " ** 10) ++ "hello world"; |
| 78 | 74 | |
| 79 | 75 | for (patterns) |pattern| { |
| 80 | var profiler = Profiler.init(allocator, true); | |
| 76 | var profiler = Profiler.init(init.gpa, init.io, true); | |
| 81 | 77 | |
| 82 | 78 | std.debug.print("\nPattern: \"{s}\"\n", .{pattern}); |
| 83 | 79 | |
| 84 | var regex = try Regex.compile(allocator, pattern); | |
| 80 | var regex = try Regex.compile(init.gpa, pattern); | |
| 85 | 81 | defer regex.deinit(); |
| 86 | 82 | |
| 87 | 83 | // Run multiple matches |
| 88 | 84 | const iterations = 1000; |
| 89 | 85 | var i: usize = 0; |
| 90 | 86 | while (i < iterations) : (i += 1) { |
| 91 | profiler.startMatch(); | |
| 87 | profiler.startMatch(init.io); | |
| 92 | 88 | _ = try regex.isMatch(test_text); |
| 93 | profiler.endMatch(); | |
| 89 | profiler.endMatch(init.io); | |
| 94 | 90 | } |
| 95 | 91 | |
| 96 | 92 | const metrics = profiler.getMetrics(); |
| 97 | const avg_ns = metrics.match_time_ns / iterations; | |
| 98 | std.debug.print("Average match time: {d}μs ({d} iterations)\n", .{ avg_ns / 1000, iterations }); | |
| 93 | // TODO: figure out if this is correct | |
| 94 | const avg_ns = @divTrunc(metrics.match_time_ns, iterations); | |
| 95 | std.debug.print("Average match time: {d}μs ({d} iterations)\n", .{ @divTrunc(avg_ns, 1000), iterations }); | |
| 99 | 96 | } |
| 100 | 97 | } |
| 101 | 98 | |
| @@ -105,12 +102,12 @@ pub fn main() !void { | ||
| 105 | 102 | std.debug.print("Example 4: Memory tracking\n", .{}); |
| 106 | 103 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 107 | 104 | |
| 108 | var profiler = Profiler.init(allocator, true); | |
| 105 | var profiler = Profiler.init(init.gpa, init.io, true); | |
| 109 | 106 | |
| 110 | 107 | const complex_pattern = "(a|b)+c(d|e)*f"; |
| 111 | 108 | std.debug.print("Pattern: \"{s}\"\n", .{complex_pattern}); |
| 112 | 109 | |
| 113 | var regex = try Regex.compile(allocator, complex_pattern); | |
| 110 | var regex = try Regex.compile(init.gpa, complex_pattern); | |
| 114 | 111 | defer regex.deinit(); |
| 115 | 112 | |
| 116 | 113 | // Simulate memory tracking (in real implementation, this would be integrated) |
| @@ -131,30 +128,30 @@ pub fn main() !void { | ||
| 131 | 128 | const test_pattern = "hello"; |
| 132 | 129 | |
| 133 | 130 | // Baseline |
| 134 | var baseline_profiler = Profiler.init(allocator, true); | |
| 131 | var baseline_profiler = Profiler.init(init.gpa, init.io, true); | |
| 135 | 132 | { |
| 136 | var regex = try Regex.compile(allocator, baseline_pattern); | |
| 133 | var regex = try Regex.compile(init.gpa, baseline_pattern); | |
| 137 | 134 | defer regex.deinit(); |
| 138 | 135 | |
| 139 | 136 | var i: usize = 0; |
| 140 | 137 | while (i < 100) : (i += 1) { |
| 141 | baseline_profiler.startMatch(); | |
| 138 | baseline_profiler.startMatch(init.io); | |
| 142 | 139 | _ = try regex.isMatch("hello world"); |
| 143 | baseline_profiler.endMatch(); | |
| 140 | baseline_profiler.endMatch(init.io); | |
| 144 | 141 | } |
| 145 | 142 | } |
| 146 | 143 | |
| 147 | 144 | // Test |
| 148 | var test_profiler = Profiler.init(allocator, true); | |
| 145 | var test_profiler = Profiler.init(init.gpa, init.io, true); | |
| 149 | 146 | { |
| 150 | var regex = try Regex.compile(allocator, test_pattern); | |
| 147 | var regex = try Regex.compile(init.gpa, test_pattern); | |
| 151 | 148 | defer regex.deinit(); |
| 152 | 149 | |
| 153 | 150 | var i: usize = 0; |
| 154 | 151 | while (i < 100) : (i += 1) { |
| 155 | test_profiler.startMatch(); | |
| 152 | test_profiler.startMatch(init.io); | |
| 156 | 153 | _ = try regex.isMatch("hello world"); |
| 157 | test_profiler.endMatch(); | |
| 154 | test_profiler.endMatch(init.io); | |
| 158 | 155 | } |
| 159 | 156 | } |
| 160 | 157 | |
| @@ -162,8 +159,8 @@ pub fn main() !void { | ||
| 162 | 159 | const test_time = test_profiler.getMetrics().match_time_ns; |
| 163 | 160 | const diff_percent = @as(f64, @floatFromInt(test_time)) / @as(f64, @floatFromInt(baseline_time)) * 100.0 - 100.0; |
| 164 | 161 | |
| 165 | std.debug.print("\nBaseline time: {d}μs\n", .{baseline_time / 1000}); | |
| 166 | std.debug.print("Test time: {d}μs\n", .{test_time / 1000}); | |
| 162 | std.debug.print("\nBaseline time: {d}μs\n", .{@divTrunc(baseline_time, 1000)}); | |
| 163 | std.debug.print("Test time: {d}μs\n", .{@divTrunc(test_time, 1000)}); | |
| 167 | 164 | std.debug.print("Difference: {d:.1}%\n", .{diff_percent}); |
| 168 | 165 | |
| 169 | 166 | if (diff_percent > 10.0) { |
| @@ -3,11 +3,7 @@ const Regex = .Regex; | ||
| 3 | 3 | const SharedRegex = .SharedRegex; |
| 4 | 4 | const RegexCache = .RegexCache; |
| 5 | 5 | |
| 6 | pub fn main() !void { | |
| 7 | var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){}; | |
| 8 | defer _ = gpa.deinit(); | |
| 9 | const allocator = gpa.allocator(); | |
| 10 | ||
| 6 | pub fn main(init: std.process.Init) !void { | |
| 11 | 7 | std.debug.print("\n=== Regex Thread Safety Examples ===\n\n", .{}); |
| 12 | 8 | |
| 13 | 9 | // Example 1: Basic concurrent matching |
| @@ -17,7 +13,7 @@ pub fn main() !void { | ||
| 17 | 13 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 18 | 14 | |
| 19 | 15 | // Compile once |
| 20 | var regex = try Regex.compile(allocator, "\\d+"); | |
| 16 | var regex = try Regex.compile(init.gpa, "\\d+"); | |
| 21 | 17 | defer regex.deinit(); |
| 22 | 18 | |
| 23 | 19 | const Worker = struct { |
| @@ -63,7 +59,7 @@ pub fn main() !void { | ||
| 63 | 59 | std.debug.print("Example 2: SharedRegex with reference counting\n", .{}); |
| 64 | 60 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 65 | 61 | |
| 66 | var shared = try SharedRegex.init(allocator, "test"); | |
| 62 | var shared = try SharedRegex.init(init.gpa, "test"); | |
| 67 | 63 | defer shared.deinit(); |
| 68 | 64 | |
| 69 | 65 | const Worker = struct { |
| @@ -128,7 +124,7 @@ pub fn main() !void { | ||
| 128 | 124 | var threads: [thread_count]std.Thread = undefined; |
| 129 | 125 | |
| 130 | 126 | for (&threads, 0..) |*thread, i| { |
| 131 | thread.* = try std.Thread.spawn(.{}, Worker.run, .{ allocator, i }); | |
| 127 | thread.* = try std.Thread.spawn(.{}, Worker.run, .{ init.gpa, i }); | |
| 132 | 128 | } |
| 133 | 129 | |
| 134 | 130 | for (threads) |thread| { |
| @@ -144,12 +140,12 @@ pub fn main() !void { | ||
| 144 | 140 | std.debug.print("Example 4: Performance with concurrent matching\n", .{}); |
| 145 | 141 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 146 | 142 | |
| 147 | var regex = try Regex.compile(allocator, "test"); | |
| 143 | var regex = try Regex.compile(init.gpa, "test"); | |
| 148 | 144 | defer regex.deinit(); |
| 149 | 145 | |
| 150 | 146 | const Worker = struct { |
| 151 | fn run(r: *const Regex) usize { | |
| 152 | var timer = std.time.Timer.start() catch return 0; | |
| 147 | fn run(r: *const Regex, io: std.Io) i64 { | |
| 148 | const start = std.Io.Timestamp.now(io, .real); | |
| 153 | 149 | |
| 154 | 150 | var count: usize = 0; |
| 155 | 151 | var i: usize = 0; |
| @@ -159,37 +155,40 @@ pub fn main() !void { | ||
| 159 | 155 | } else |_| {} |
| 160 | 156 | } |
| 161 | 157 | |
| 162 | const elapsed_ms = timer.read() / 1_000_000; | |
| 163 | return elapsed_ms; | |
| 158 | const end = std.Io.Timestamp.now(io, .real); | |
| 159 | const elapsed = start.durationTo(end); | |
| 160 | return elapsed.toMilliseconds(); | |
| 164 | 161 | } |
| 165 | 162 | }; |
| 166 | 163 | |
| 167 | 164 | // Sequential baseline |
| 168 | const sequential_time = Worker.run(®ex); | |
| 165 | const sequential_time = Worker.run(®ex, init.io); | |
| 169 | 166 | std.debug.print("Sequential: 10,000 matches in {d}ms\n", .{sequential_time}); |
| 170 | 167 | |
| 171 | 168 | // Parallel execution |
| 172 | 169 | const thread_count = 4; |
| 173 | 170 | var threads: [thread_count]std.Thread = undefined; |
| 174 | var times: [thread_count]usize = undefined; | |
| 171 | var times: [thread_count]i64 = undefined; | |
| 175 | 172 | |
| 176 | 173 | const ParallelWorker = struct { |
| 177 | fn run(r: *const Regex, time_ptr: *usize) void { | |
| 178 | time_ptr.* = Worker.run(r); | |
| 174 | fn run(r: *const Regex, io: std.Io, time_ptr: *i64) void { | |
| 175 | time_ptr.* = Worker.run(r, io); | |
| 179 | 176 | } |
| 180 | 177 | }; |
| 181 | 178 | |
| 182 | var parallel_timer = try std.time.Timer.start(); | |
| 179 | // TODO: there is probably a better/correct-er way to do this with io | |
| 180 | const start = std.Io.Timestamp.now(init.io, .real); | |
| 183 | 181 | |
| 184 | 182 | for (&threads, 0..) |*thread, i| { |
| 185 | thread.* = try std.Thread.spawn(.{}, ParallelWorker.run, .{ ®ex, ×[i] }); | |
| 183 | thread.* = try std.Thread.spawn(.{}, ParallelWorker.run, .{ ®ex, init.io, ×[i] }); | |
| 186 | 184 | } |
| 187 | 185 | |
| 188 | 186 | for (threads) |thread| { |
| 189 | 187 | thread.join(); |
| 190 | 188 | } |
| 191 | 189 | |
| 192 | const total_parallel = parallel_timer.read() / 1_000_000; | |
| 190 | const end = std.Io.Timestamp.now(init.io, .real); | |
| 191 | const total_parallel = start.durationTo(end).toMilliseconds(); | |
| 193 | 192 | |
| 194 | 193 | std.debug.print("Parallel ({d} threads): 40,000 matches in {d}ms\n", .{ thread_count, total_parallel }); |
| 195 | 194 | std.debug.print("Individual thread times: ", .{}); |
renamed throughout · 16 hidden lines
| @@ -206,13 +205,13 @@ pub fn main() !void { renamed throughout | ||
| 206 | 205 | std.debug.print("Example 5: Multiple patterns with concurrent access\n", .{}); |
| 207 | 206 | std.debug.print("───────────────────────────────────────────────\n", .{}); |
| 208 | 207 | |
| 209 | var email_regex = try Regex.compile(allocator, "[a-z]+@[a-z]+\\.[a-z]+"); | |
| 208 | var email_regex = try Regex.compile(init.gpa, "[a-z]+@[a-z]+\\.[a-z]+"); | |
| 210 | 209 | defer email_regex.deinit(); |
| 211 | 210 | |
| 212 | var phone_regex = try Regex.compile(allocator, "\\d{3}-\\d{4}"); | |
| 211 | var phone_regex = try Regex.compile(init.gpa, "\\d{3}-\\d{4}"); | |
| 213 | 212 | defer phone_regex.deinit(); |
| 214 | 213 | |
| 215 | var url_regex = try Regex.compile(allocator, "https?://"); | |
| 214 | var url_regex = try Regex.compile(init.gpa, "https?://"); | |
| 216 | 215 | defer url_regex.deinit(); |
| 217 | 216 | |
| 218 | 217 | const Worker = struct { |
| @@ -29,7 +29,7 @@ pub const Builder = struct { | ||
| 29 | 29 | pub fn init(allocator: std.mem.Allocator) Builder { |
| 30 | 30 | return .{ |
| 31 | 31 | .allocator = allocator, |
| 32 | .parts = std.ArrayList([]const u8){}, | |
| 32 | .parts = .empty, | |
| 33 | 33 | }; |
| 34 | 34 | } |
| 35 | 35 | |
| @@ -304,7 +304,7 @@ pub const Composer = struct { | ||
| 304 | 304 | pub fn init(allocator: std.mem.Allocator) Composer { |
| 305 | 305 | return .{ |
| 306 | 306 | .allocator = allocator, |
| 307 | .patterns = std.ArrayList([]const u8){}, | |
| 307 | .patterns = .empty, | |
| 308 | 308 | }; |
| 309 | 309 | } |
| 310 | 310 | |
| @@ -5,11 +5,11 @@ pub const Profiler = struct { | ||
| 5 | 5 | allocator: std.mem.Allocator, |
| 6 | 6 | enabled: bool, |
| 7 | 7 | metrics: Metrics, |
| 8 | start_time: ?std.time.Instant, | |
| 8 | start_time: ?std.Io.Timestamp, | |
| 9 | 9 | |
| 10 | 10 | pub const Metrics = struct { |
| 11 | compilation_time_ns: u64 = 0, | |
| 12 | match_time_ns: u64 = 0, | |
| 11 | compilation_time_ns: i96 = 0, | |
| 12 | match_time_ns: i96 = 0, | |
| 13 | 13 | match_count: usize = 0, |
| 14 | 14 | nfa_states_created: usize = 0, |
| 15 | 15 | backtrack_count: usize = 0, |
| @@ -49,41 +49,42 @@ pub const Profiler = struct { | ||
| 49 | 49 | } |
| 50 | 50 | }; |
| 51 | 51 | |
| 52 | pub fn init(allocator: std.mem.Allocator, enabled: bool) Profiler { | |
| 52 | pub fn init(allocator: std.mem.Allocator, io: std.Io, enabled: bool) Profiler { | |
| 53 | 53 | return .{ |
| 54 | 54 | .allocator = allocator, |
| 55 | 55 | .enabled = enabled, |
| 56 | 56 | .metrics = Metrics{}, |
| 57 | .start_time = if (enabled) std.time.Instant.now() catch null else null, | |
| 57 | // TODO: No clue what to actually pass as a clock | |
| 58 | .start_time = if (enabled) std.Io.Timestamp.now(io, .real) else null, | |
| 58 | 59 | }; |
| 59 | 60 | } |
| 60 | 61 | |
| 61 | 62 | /// Start timing a compilation phase |
| 62 | pub fn startCompilation(self: *Profiler) void { | |
| 63 | pub fn startCompilation(self: *Profiler, io: std.Io) void { | |
| 63 | 64 | if (!self.enabled) return; |
| 64 | self.start_time = std.time.Instant.now() catch null; | |
| 65 | self.start_time = std.Io.Timestamp.now(io, .real); | |
| 65 | 66 | } |
| 66 | 67 | |
| 67 | 68 | /// End timing a compilation phase |
| 68 | pub fn endCompilation(self: *Profiler) void { | |
| 69 | pub fn endCompilation(self: *Profiler, io: std.Io) void { | |
| 69 | 70 | if (!self.enabled) return; |
| 70 | 71 | const start = self.start_time orelse return; |
| 71 | const now = std.time.Instant.now() catch return; | |
| 72 | self.metrics.compilation_time_ns += now.since(start); | |
| 72 | const now = std.Io.Timestamp.now(io, .real); | |
| 73 | self.metrics.compilation_time_ns += start.durationTo(now).nanoseconds; | |
| 73 | 74 | } |
| 74 | 75 | |
| 75 | 76 | /// Start timing a match operation |
| 76 | pub fn startMatch(self: *Profiler) void { | |
| 77 | pub fn startMatch(self: *Profiler, io: std.Io) void { | |
| 77 | 78 | if (!self.enabled) return; |
| 78 | self.start_time = std.time.Instant.now() catch null; | |
| 79 | self.start_time = std.Io.Timestamp.now(io, .real); | |
| 79 | 80 | } |
| 80 | 81 | |
| 81 | 82 | /// End timing a match operation |
| 82 | pub fn endMatch(self: *Profiler) void { | |
| 83 | pub fn endMatch(self: *Profiler, io: std.Io) void { | |
| 83 | 84 | if (!self.enabled) return; |
| 84 | 85 | const start = self.start_time orelse return; |
| 85 | const now = std.time.Instant.now() catch return; | |
| 86 | self.metrics.match_time_ns += now.since(start); | |
| 86 | const now = std.Io.Timestamp.now(io, .real); | |
| 87 | self.metrics.match_time_ns += start.durationTo(now).nanoseconds; | |
| 87 | 88 | self.metrics.match_count += 1; |
| 88 | 89 | } |
| 89 | 90 | |
| @@ -140,9 +141,9 @@ pub const Profiler = struct { | ||
| 140 | 141 | } |
| 141 | 142 | |
| 142 | 143 | /// Reset all metrics |
| 143 | pub fn reset(self: *Profiler) void { | |
| 144 | pub fn reset(self: *Profiler, io: std.Io) void { | |
| 144 | 145 | self.metrics.reset(); |
| 145 | self.start_time = if (self.enabled) std.time.Instant.now() catch null else null; | |
| 146 | self.start_time = if (self.enabled) std.Io.Timestamp.now(io, .real); | |
| 146 | 147 | } |
| 147 | 148 | |
| 148 | 149 | /// Print metrics to stderr |
| @@ -156,26 +157,26 @@ pub const Profiler = struct { | ||
| 156 | 157 | pub const ScopedTimer = struct { |
| 157 | 158 | profiler: *Profiler, |
| 158 | 159 | timer_type: TimerType, |
| 159 | start: ?std.time.Instant, | |
| 160 | start: ?std.Io.Timestamp, | |
| 160 | 161 | |
| 161 | 162 | pub const TimerType = enum { |
| 162 | 163 | compilation, |
| 163 | 164 | match, |
| 164 | 165 | }; |
| 165 | 166 | |
| 166 | pub fn init(profiler: *Profiler, timer_type: TimerType) ScopedTimer { | |
| 167 | pub fn init(profiler: *Profiler, io: std.Io, timer_type: TimerType) ScopedTimer { | |
| 167 | 168 | return .{ |
| 168 | 169 | .profiler = profiler, |
| 169 | 170 | .timer_type = timer_type, |
| 170 | .start = if (profiler.enabled) std.time.Instant.now() catch null else null, | |
| 171 | .start = if (profiler.enabled) std.Io.Timestamp.now(io, .real) else null, | |
| 171 | 172 | }; |
| 172 | 173 | } |
| 173 | 174 | |
| 174 | pub fn deinit(self: *ScopedTimer) void { | |
| 175 | pub fn deinit(self: *ScopedTimer, io: std.Io) void { | |
| 175 | 176 | if (!self.profiler.enabled) return; |
| 176 | 177 | const start = self.start orelse return; |
| 177 | const now = std.time.Instant.now() catch return; | |
| 178 | const elapsed = now.since(start); | |
| 178 | const now = std.Io.Timestamp.now(io, .real); | |
| 179 | const elapsed = start.durationTo(now).nanoseconds; | |
| 179 | 180 | switch (self.timer_type) { |
| 180 | 181 | .compilation => self.profiler.metrics.compilation_time_ns += elapsed, |
| 181 | 182 | .match => { |
| @@ -188,15 +189,16 @@ pub const ScopedTimer = struct { | ||
| 188 | 189 | |
| 189 | 190 | test "profiler basic operations" { |
| 190 | 191 | const allocator = std.testing.allocator; |
| 191 | var profiler = Profiler.init(allocator, true); | |
| 192 | const io = std.testing.io; | |
| 193 | var profiler = Profiler.init(allocator, io, true); | |
| 192 | 194 | |
| 193 | profiler.startCompilation(); | |
| 195 | profiler.startCompilation(io); | |
| 194 | 196 | // Do some work (just loop to consume time) |
| 195 | 197 | var i: usize = 0; |
| 196 | 198 | while (i < 1000) : (i += 1) { |
| 197 | 199 | _ = i * i; |
| 198 | 200 | } |
| 199 | profiler.endCompilation(); | |
| 201 | profiler.endCompilation(io); | |
| 200 | 202 | |
| 201 | 203 | profiler.recordStateCreation(5); |
| 202 | 204 | profiler.recordBacktrack(); |
| @@ -210,11 +212,12 @@ test "profiler basic operations" { | ||
| 210 | 212 | |
| 211 | 213 | test "scoped timer" { |
| 212 | 214 | const allocator = std.testing.allocator; |
| 213 | var profiler = Profiler.init(allocator, true); | |
| 215 | const io = std.testing.io; | |
| 216 | var profiler = Profiler.init(allocator, io, true); | |
| 214 | 217 | |
| 215 | 218 | { |
| 216 | var timer = ScopedTimer.init(&profiler, .match); | |
| 217 | defer timer.deinit(); | |
| 219 | var timer = ScopedTimer.init(&profiler, io, .match); | |
| 220 | defer timer.deinit(io); | |
| 218 | 221 | // Do some work |
| 219 | 222 | var i: usize = 0; |
| 220 | 223 | while (i < 1000) : (i += 1) { |
| @@ -191,7 +191,8 @@ test "RegexCache basic operations" { | ||
| 191 | 191 | |
| 192 | 192 | test "thread-safe allocator with concurrent matching" { |
| 193 | 193 | // Use thread-safe allocator for concurrent access |
| 194 | var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){}; | |
| 194 | var gpa = std.heap.DebugAllocator(.{ .thread_safe = true }){}; | |
| 195 | // TODO: can use this for leak detection | |
| 195 | 196 | defer _ = gpa.deinit(); |
| 196 | 197 | const allocator = gpa.allocator(); |
| 197 | 198 | |
| @@ -222,7 +223,7 @@ test "thread-safe allocator with concurrent matching" { | ||
| 222 | 223 | } |
| 223 | 224 | |
| 224 | 225 | test "concurrent matching different patterns" { |
| 225 | var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){}; | |
| 226 | var gpa = std.heap.DebugAllocator(.{ .thread_safe = true }){}; | |
| 226 | 227 | defer _ = gpa.deinit(); |
| 227 | 228 | const allocator = gpa.allocator(); |
| 228 | 229 | |
| @@ -272,7 +273,7 @@ test "concurrent matching different patterns" { | ||
| 272 | 273 | } |
| 273 | 274 | |
| 274 | 275 | test "stress test: many threads many matches" { |
| 275 | var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){}; | |
| 276 | var gpa = std.heap.DebugAllocator(.{ .thread_safe = true }){}; | |
| 276 | 277 | defer _ = gpa.deinit(); |
| 277 | 278 | const allocator = gpa.allocator(); |
| 278 | 279 | |