Skip to content

Commit 3b2847f

Browse files
committed
Handle optionals with zero-length haystack
Match was bailing on haystack of zero length, without giving it a chance to match against patterns which can, in fact, match nothing.
1 parent c130207 commit 3b2847f

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The provided Regex type allows 64 'operations' and 8 unique ASCII character sets
1111
Drop the file into your project, or use the Zig build system:
1212

1313
```zig
14-
zig fetch --save "https://github.com/mnemnion/mvzr/archive/refs/tags/v0.3.1.tar.gz"
14+
zig fetch --save "https://github.com/mnemnion/mvzr/archive/refs/tags/v0.3.2.tar.gz"
1515
```
1616

1717
I'll do my best to keep that URL fresh, but it pays to check over here: ➔

build.zig.zon

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// This is a [Semantic Version](https://semver.org/).
99
// In a future version of Zig it will be used for package deduplication.
10-
.version = "0.3.1",
10+
.version = "0.3.2",
1111

1212
// This field is optional.
1313
// This is currently advisory only; Zig does not yet do anything

src/mvzr.zig

+17-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ pub fn SizedRegex(ops: comptime_int, char_sets: comptime_int) type {
112112

113113
/// Match a regex pattern in `haystack`, if found, this returns a `Match`.
114114
pub fn match(regex: *const SizedRegexT, haystack: []const u8) ?Match {
115-
if (haystack.len == 0) return null;
116115
const maybe_matched = regex.matchInternal(haystack);
117116
if (maybe_matched) |m| {
118117
const m1 = m[0];
@@ -2425,3 +2424,20 @@ test "Uppercase Greek" {
24252424
test "M of N multibyte" {
24262425
try testMatchEnd("abλ{3,5}", "abλλλλ");
24272426
}
2427+
2428+
test "zero length match on zero length haystack" {
2429+
const mvzr = @import("mvzr.zig");
2430+
const regex = mvzr.compile(".*");
2431+
const the_match = regex.?.match("");
2432+
try std.testing.expect(the_match != null);
2433+
try std.testing.expectEqual(0, the_match.?.start);
2434+
try std.testing.expectEqual(0, the_match.?.end);
2435+
}
2436+
test "zero length optional match on zero length haystack" {
2437+
const mvzr = @import("mvzr.zig");
2438+
const regex = mvzr.compile(".?");
2439+
const the_match = regex.?.match("");
2440+
try std.testing.expect(the_match != null);
2441+
try std.testing.expectEqual(0, the_match.?.start);
2442+
try std.testing.expectEqual(0, the_match.?.end);
2443+
}

0 commit comments

Comments
 (0)