bd44b5aa08
Nix / Required Checks: Nix (push) Blocked by required conditions
Nix / check-zig-cache-hash (push) Waiting to run
Test / skip (push) Waiting to run
Test / Required Checks: Test (push) Blocked by required conditions
Test / build-bench (push) Blocked by required conditions
Test / list-examples (push) Blocked by required conditions
Test / Example ${{ matrix.dir }} (Windows) (push) Blocked by required conditions
Test / Example ${{ matrix.dir }} (push) Blocked by required conditions
Test / build-cmake (push) Blocked by required conditions
Test / test-lib-vt-pkgconfig (push) Blocked by required conditions
Test / build-flatpak (push) Blocked by required conditions
Test / build-snap (push) Blocked by required conditions
Test / build-linux (namespace-profile-ghostty-md-arm64) (push) Blocked by required conditions
Test / build-nix (namespace-profile-ghostty-md) (push) Blocked by required conditions
Test / build-libghostty-vt (aarch64-linux) (push) Blocked by required conditions
Test / build-libghostty-vt (aarch64-macos) (push) Blocked by required conditions
Test / build-libghostty-vt (wasm32-freestanding) (push) Blocked by required conditions
Test / build-libghostty-vt (x86_64-linux) (push) Blocked by required conditions
Test / build-libghostty-vt (x86_64-linux-musl) (push) Blocked by required conditions
Test / build-libghostty-vt (x86_64-macos) (push) Blocked by required conditions
Test / build-libghostty-vt (x86_64-windows-gnu) (push) Blocked by required conditions
Test / build-libghostty-vt-macos (aarch64-ios) (push) Blocked by required conditions
Test / build-libghostty-vt-macos (aarch64-macos) (push) Blocked by required conditions
Test / build-libghostty-vt-macos (x86_64-macos) (push) Blocked by required conditions
Test / build-libghostty-vt-android (aarch64-linux-android) (push) Blocked by required conditions
Test / build-libghostty-vt-android (arm-linux-androideabi) (push) Blocked by required conditions
Test / build-libghostty-vt-android (x86_64-linux-android) (push) Blocked by required conditions
Test / build-libghostty-vt-windows (push) Blocked by required conditions
Test / build-libghostty-windows-gnu (push) Blocked by required conditions
Test / build-linux (namespace-profile-ghostty-md) (push) Blocked by required conditions
Test / build-linux-libghostty (push) Blocked by required conditions
Test / build-nix (namespace-profile-ghostty-md-arm64) (push) Blocked by required conditions
Test / build-dist (push) Blocked by required conditions
Test / build-dist-lib-vt (push) Blocked by required conditions
Test / trigger-snap (push) Blocked by required conditions
Test / trigger-flatpak (push) Blocked by required conditions
Test / build-macos (push) Blocked by required conditions
Test / build-macos-freetype (push) Blocked by required conditions
Test / test (push) Blocked by required conditions
Test / test-lib-vt (push) Blocked by required conditions
Test / GTK x11=false wayland=false (push) Blocked by required conditions
Test / GTK x11=true wayland=false (push) Blocked by required conditions
Test / GTK x11=false wayland=true (push) Blocked by required conditions
Test / GTK x11=true wayland=true (push) Blocked by required conditions
Test / Build -Dsimd=false (push) Blocked by required conditions
Test / Build -Dsimd=true (push) Blocked by required conditions
Test / Build -Dsentry=false (push) Blocked by required conditions
Test / Build -Dsentry=true (push) Blocked by required conditions
Test / test-macos (push) Blocked by required conditions
Test / test-windows (push) Blocked by required conditions
Test / Build -Di18n=false (push) Blocked by required conditions
Test / Build -Di18n=true (push) Blocked by required conditions
Test / Build test/fuzz-libghostty (push) Blocked by required conditions
Test / zig-fmt (push) Blocked by required conditions
Test / GitHub Actions Pins (push) Blocked by required conditions
Test / prettier (push) Blocked by required conditions
Test / swiftlint (push) Blocked by required conditions
Test / alejandra (push) Blocked by required conditions
Test / typos (push) Blocked by required conditions
Test / shellcheck (push) Blocked by required conditions
Test / translations (push) Blocked by required conditions
Test / blueprint-compiler (push) Blocked by required conditions
Test / Test pkg/wuffs (push) Blocked by required conditions
Test / Test build on Debian 13 (push) Blocked by required conditions
Test / valgrind (push) Blocked by required conditions
153 lines
4.0 KiB
Zig
153 lines
4.0 KiB
Zig
const std = @import("std");
|
|
const c = @import("c.zig").c;
|
|
const types = @import("types.zig");
|
|
const errors = @import("errors.zig");
|
|
const testEnsureInit = @import("testing.zig").ensureInit;
|
|
const MatchParam = @import("match_param.zig").MatchParam;
|
|
const Region = @import("region.zig").Region;
|
|
const Error = errors.Error;
|
|
const ErrorInfo = errors.ErrorInfo;
|
|
const Encoding = types.Encoding;
|
|
const Option = types.Option;
|
|
const Syntax = types.Syntax;
|
|
|
|
pub const Regex = struct {
|
|
value: c.OnigRegex,
|
|
|
|
pub fn init(
|
|
pattern: []const u8,
|
|
options: Option,
|
|
enc: *Encoding,
|
|
syntax: *Syntax,
|
|
err: ?*ErrorInfo,
|
|
) !Regex {
|
|
var self: Regex = undefined;
|
|
_ = try errors.convertError(c.onig_new(
|
|
&self.value,
|
|
pattern.ptr,
|
|
pattern.ptr + pattern.len,
|
|
options.int(),
|
|
@ptrCast(@alignCast(enc)),
|
|
@ptrCast(@alignCast(syntax)),
|
|
@ptrCast(err),
|
|
));
|
|
return self;
|
|
}
|
|
|
|
pub fn deinit(self: *Regex) void {
|
|
c.onig_free(self.value);
|
|
}
|
|
|
|
/// Search an entire string for matches. This always returns a region
|
|
/// which may heap allocate (C allocator).
|
|
pub fn search(
|
|
self: *Regex,
|
|
str: []const u8,
|
|
options: Option,
|
|
) !Region {
|
|
return self.searchWithParam(str, options, null);
|
|
}
|
|
|
|
/// Search an entire string for matches. This always returns a region
|
|
/// which may heap allocate (C allocator).
|
|
pub fn searchWithParam(
|
|
self: *Regex,
|
|
str: []const u8,
|
|
options: Option,
|
|
match_param: ?*MatchParam,
|
|
) !Region {
|
|
var region: Region = .{};
|
|
|
|
// As part of the searchAdvanced API call below, onig may allocate
|
|
// memory even if we fail to match. We need to deinit if there are
|
|
// any errors to free that memory.
|
|
errdefer region.deinit();
|
|
|
|
_ = try self.searchAdvancedWithParam(
|
|
str,
|
|
0,
|
|
str.len,
|
|
®ion,
|
|
options,
|
|
match_param,
|
|
);
|
|
return region;
|
|
}
|
|
|
|
/// onig_search directly
|
|
pub fn searchAdvanced(
|
|
self: *Regex,
|
|
str: []const u8,
|
|
start: usize,
|
|
end: usize,
|
|
region: *Region,
|
|
options: Option,
|
|
) !usize {
|
|
return self.searchAdvancedWithParam(
|
|
str,
|
|
start,
|
|
end,
|
|
region,
|
|
options,
|
|
null,
|
|
);
|
|
}
|
|
|
|
/// onig_search_with_param directly
|
|
pub fn searchAdvancedWithParam(
|
|
self: *Regex,
|
|
str: []const u8,
|
|
start: usize,
|
|
end: usize,
|
|
region: *Region,
|
|
options: Option,
|
|
match_param: ?*MatchParam,
|
|
) !usize {
|
|
const pos = try errors.convertError(if (match_param) |param|
|
|
c.onig_search_with_param(
|
|
self.value,
|
|
str.ptr,
|
|
str.ptr + str.len,
|
|
str.ptr + start,
|
|
str.ptr + end,
|
|
@ptrCast(region),
|
|
options.int(),
|
|
param.value,
|
|
)
|
|
else
|
|
c.onig_search(
|
|
self.value,
|
|
str.ptr,
|
|
str.ptr + str.len,
|
|
str.ptr + start,
|
|
str.ptr + end,
|
|
@ptrCast(region),
|
|
options.int(),
|
|
));
|
|
|
|
return @intCast(pos);
|
|
}
|
|
};
|
|
|
|
test {
|
|
const testing = std.testing;
|
|
|
|
try testEnsureInit();
|
|
var re = try Regex.init("foo", .{}, Encoding.utf8, Syntax.default, null);
|
|
defer re.deinit();
|
|
|
|
var reg = try re.search("hello foo bar", .{});
|
|
defer reg.deinit();
|
|
try testing.expectEqual(@as(usize, 1), reg.count());
|
|
|
|
try testing.expectError(Error.Mismatch, re.search("hello", .{}));
|
|
|
|
var match_param = try MatchParam.init();
|
|
defer match_param.deinit();
|
|
try match_param.setRetryLimitInSearch(1000);
|
|
|
|
var reg_param = try re.searchWithParam("hello foo bar", .{}, &match_param);
|
|
defer reg_param.deinit();
|
|
try testing.expectEqual(@as(usize, 1), reg_param.count());
|
|
}
|