blob: 8c5c7a5288fd29164dfa965b0083124761745cfd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
const std = @import("std");
const c = @import("c.zig").c;
pub const Region = extern struct {
allocated: c_int = 0,
num_regs: c_int = 0,
beg: ?[*]c_int = null,
end: ?[*]c_int = null,
history_root: ?*c.OnigCaptureTreeNode = null, // TODO: convert to Zig
pub fn deinit(self: *Region) void {
// We never free ourself because allocation of Region in the Zig
// bindings is handled by the Zig program.
c.onig_region_free(@ptrCast(self), 0);
}
/// Count the number of matches
pub fn count(self: *const Region) usize {
return @intCast(self.num_regs);
}
/// Iterate over the matched ranges.
pub fn iterator(self: *const Region) Iterator {
return .{ .region = self };
}
pub fn starts(self: *const Region) []const c_int {
if (self.num_regs == 0) return &.{};
return self.beg.?[0..@intCast(self.num_regs)];
}
pub fn ends(self: *const Region) []const c_int {
if (self.num_regs == 0) return &.{};
return self.end.?[0..@intCast(self.num_regs)];
}
pub const Iterator = struct {
region: *const Region,
i: usize = 0,
/// The next range
pub fn next(self: *Iterator) ?[2]usize {
if (self.i >= self.region.num_regs) return null;
defer self.i += 1;
return .{
@intCast(self.region.beg.?[self.i]),
@intCast(self.region.end.?[self.i]),
};
}
};
};
|