Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1ca90c0

Browse filesBrowse files
committed
Added search() function and tests
1 parent cba0210 commit 1ca90c0
Copy full SHA for 1ca90c0

File tree

Expand file treeCollapse file tree

2 files changed

+47
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+47
-1
lines changed
Open diff view settings
Collapse file

‎ctregex.zig‎

Copy file name to clipboardExpand all lines: ctregex.zig
+26-1Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,10 @@ pub fn MatchResult(comptime regex: []const u8, comptime options: MatchOptions) t
910910
slice: []const CharT,
911911
captures: [capture_len]?[]const CharT = [1]?[]const CharT{null} ** capture_len,
912912

913+
inline fn resetCaptures(self: *Self) void {
914+
self.captures = [1]?[]const CharT{null} ** capture_len;
915+
}
916+
913917
pub usingnamespace if (capture_len != 0)
914918
struct {
915919
pub fn capture(self: Self, comptime name: []const u8) ?[]const CharT {
@@ -946,6 +950,27 @@ pub fn match(comptime regex: []const u8, comptime options: MatchOptions, str: []
946950
return {};
947951
}
948952

949-
// TODO search, findAll, etc.
953+
pub fn search(comptime regex: []const u8, comptime options: MatchOptions, str: []const options.encoding.CharT()) !?MatchResult(regex, options) {
954+
if (comptime RegexParser.parse(regex)) |parsed| {
955+
var result: MatchResult(regex, options) = .{
956+
.slice = undefined,
957+
};
958+
const min_len = comptime parsed.root.minLen(options.encoding);
959+
// TODO Better strategy.
960+
var start_idx: usize = 0;
961+
while (start_idx < (str.len - min_len)) : (start_idx += 1) {
962+
if (try matchExpr(parsed.root, options, str[start_idx..], &result)) |slice| {
963+
result.slice = slice;
964+
return result;
965+
}
966+
result.resetCaptures();
967+
}
968+
return null;
969+
}
970+
971+
return {};
972+
}
973+
974+
// TODO findAll, etc.
950975
// TODO Convert to DFA when we can (otherwise some mix of DFA + DFS?)
951976
// TODO More features, aim for PCRE compatibility
Collapse file

‎tests.zig‎

Copy file name to clipboardExpand all lines: tests.zig
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ fn testMatch(comptime regex: []const u8, comptime encoding: ctregex.Encoding, co
2828
comptime expect((try ctregex.match(regex, .{.encoding = encoding}, encoded_str)) != null);
2929
}
3030

31+
fn testSearchInner(comptime regex: []const u8, comptime encoding: ctregex.Encoding, comptime str: []const encoding.CharT(), comptime found: []const encoding.CharT()) !void {
32+
const result = try ctregex.search(regex, .{.encoding = encoding}, str);
33+
expect(result != null);
34+
expect(std.mem.eql(encoding.CharT(), result.?.slice, found));
35+
}
36+
37+
fn testSearch(comptime regex: []const u8, comptime encoding: ctregex.Encoding, comptime str: []const u8, comptime found: []const u8) !void {
38+
const encoded_str = comptime encodeStr(encoding, str);
39+
const encoded_found = comptime encodeStr(encoding, found);
40+
41+
try testSearchInner(regex, encoding, encoded_str, encoded_found);
42+
comptime try testSearchInner(regex, encoding, encoded_str, encoded_found);
43+
}
44+
3145
fn testCapturesInner(comptime regex: []const u8, comptime encoding: ctregex.Encoding, comptime str: []const encoding.CharT(), comptime captures: []const ?[]const encoding.CharT()) !void {
3246
const result = try ctregex.match(regex, .{.encoding = encoding}, str);
3347
expect(result != null);
@@ -83,3 +97,10 @@ test "regex matching" {
8397
});
8498
}
8599

100+
test "regex searching" {
101+
@setEvalBranchQuota(3800);
102+
try testSearch("foo|bar", .ascii, "some very interesting test string including foobar.", "foo");
103+
try testSearch("(abc|αβγ)+", .utf8, "a lorem ipsum αβγαβγαβγ abcabc", "αβγαβγαβγ");
104+
try testSearch("(abc|αβγ)+", .utf16le, "a lorem ipsum αβγαβγαβγ abcabc", "αβγαβγαβγ");
105+
try testSearch("(abc|αβγ)+", .codepoint, "a lorem ipsum αβγαβγαβγ abcabc", "αβγαβγαβγ");
106+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.