Skip to content

Commit

Permalink
Merge pull request #11 from nektro/main
Browse files Browse the repository at this point in the history
update to Zig 0.11
  • Loading branch information
kivikakk authored Aug 28, 2023
2 parents 14efa82 + 791ea0f commit 43df7fb
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 23 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/zig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
submodules: recursive
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
version: 0.11.0
- run: zig build
- run: zig build test
test-macos:
Expand All @@ -24,10 +24,10 @@ jobs:
submodules: recursive
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
- run: brew install pkg-config pcre
- run: env ZIG_SYSTEM_LINKER_HACK=1 zig build
- run: env ZIG_SYSTEM_LINKER_HACK=1 zig build test
version: 0.11.0
- run: brew install pcre
- run: zig build
- run: zig build test
test-windows:
runs-on: windows-latest
steps:
Expand All @@ -36,7 +36,7 @@ jobs:
submodules: recursive
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
version: 0.11.0
- run: c:; cd \vcpkg; git pull; .\bootstrap-vcpkg.bat
- run: vcpkg integrate install
- run: vcpkg install pcre --triplet x64-windows-static
Expand All @@ -50,5 +50,5 @@ jobs:
submodules: recursive
- uses: goto-bus-stop/setup-zig@v1
with:
version: master
version: 0.11.0
- run: zig fmt --check build.zig src/*.zig
31 changes: 24 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,50 @@ pub fn build(b: *std.build.Builder) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});

_ = b.addModule("libpcre", .{
.source_file = .{ .path = "src/main.zig" },
});

const lib = b.addStaticLibrary(.{
.name = "libpcre.zig",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
try linkPcre(lib);
try linkPcre(b, lib);
b.installArtifact(lib);

var main_tests = b.addTest(.{
const main_tests = b.addTest(.{
.name = "main_tests",
.root_source_file = .{ .path = "src/main.zig" },
.optimize = optimize,
.target = target,
});
try linkPcre(main_tests);
try linkPcre(b, main_tests);

const main_tests_run = b.addRunArtifact(main_tests);
main_tests_run.step.dependOn(&main_tests.step);

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
test_step.dependOn(&main_tests_run.step);
}

pub fn linkPcre(exe: *std.build.LibExeObjStep) !void {
pub fn linkPcre(b: *std.build.Builder, exe: *std.build.LibExeObjStep) !void {
exe.linkLibC();
if (builtin.os.tag == .windows) {
try exe.addVcpkgPaths(.static);
exe.linkSystemLibrary("pcre");
}
if (builtin.os.tag == .macos) {
// If `pkg-config libpcre` doesn't error, linkSystemLibrary("libpcre") will succeed.
// If it errors, try "pcre", as either it will hit a .pc by that name, or the fallthru
// `-lpcre` and standard includes will work. (Or it's not installed.)
var code: u8 = undefined;
if (b.execAllowFail(&[_][]const u8{ "pkg-config", "libpcre" }, &code, .Ignore)) |_| {
exe.linkSystemLibrary("libpcre");
} else |_| {
exe.linkSystemLibrary("pcre");
}
} else {
exe.linkSystemLibrary("libpcre");
exe.linkSystemLibrary("pcre");
}
}
58 changes: 58 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
outputs = {
self,
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib;
in rec {
formatter = pkgs.alejandra;

devShells.default = pkgs.mkShell {
nativeBuildInputs = [pkgs.zig pkgs.pcre];
};
});
}
18 changes: 9 additions & 9 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ pub const Regex = struct {
var err_offset: c_int = undefined;

const pcre = c.pcre_compile(pattern.ptr, options.compile(), &err, &err_offset, 0) orelse {
std.log.warn("pcre_compile (at {}): {s}\n", .{ err_offset, @ptrCast([*:0]const u8, err) });
std.log.warn("pcre_compile (at {}): {s}\n", .{ err_offset, @as([*:0]const u8, @ptrCast(err)) });
return error.CompileError;
};
errdefer c.pcre_free.?(pcre);

const pcre_extra = c.pcre_study(pcre, 0, &err);
if (err != 0) {
std.log.warn("pcre_study: {s}\n", .{@ptrCast([*:0]const u8, err)});
std.log.warn("pcre_study: {s}\n", .{@as([*:0]const u8, @ptrCast(err))});
return error.CompileError;
}
errdefer c.pcre_free_study(pcre_extra);
Expand All @@ -111,7 +111,7 @@ pub const Regex = struct {
return Regex{
.pcre = pcre,
.pcre_extra = pcre_extra,
.capture_count = @intCast(usize, capture_count),
.capture_count = @as(usize, @intCast(capture_count)),
};
}

Expand All @@ -123,7 +123,7 @@ pub const Regex = struct {
/// Returns the start and end index of the match if any, otherwise null.
pub fn matches(self: Regex, s: []const u8, options: Options) ExecError!?Capture {
var ovector: [3]c_int = undefined;
var result = c.pcre_exec(self.pcre, self.pcre_extra, s.ptr, @intCast(c_int, s.len), 0, options.compile(), &ovector, 3);
var result = c.pcre_exec(self.pcre, self.pcre_extra, s.ptr, @as(c_int, @intCast(s.len)), 0, options.compile(), &ovector, 3);
switch (result) {
c.PCRE_ERROR_NOMATCH => return null,
c.PCRE_ERROR_NOMEMORY => return error.OutOfMemory,
Expand All @@ -135,7 +135,7 @@ pub const Regex = struct {
std.log.warn("pcre_exec: {}\n", .{result});
return error.ExecError; // TODO: should clarify
}
return Capture{ .start = @intCast(usize, ovector[0]), .end = @intCast(usize, ovector[1]) };
return Capture{ .start = @as(usize, @intCast(ovector[0])), .end = @as(usize, @intCast(ovector[1])) };
}

/// Searches for capture groups in s. The 0th Capture of the result is the entire match.
Expand All @@ -144,7 +144,7 @@ pub const Regex = struct {
var ovector: []c_int = try allocator.alloc(c_int, ovecsize);
defer allocator.free(ovector);

var result = c.pcre_exec(self.pcre, self.pcre_extra, s.ptr, @intCast(c_int, s.len), 0, options.compile(), &ovector[0], @intCast(c_int, ovecsize));
var result = c.pcre_exec(self.pcre, self.pcre_extra, s.ptr, @as(c_int, @intCast(s.len)), 0, options.compile(), &ovector[0], @as(c_int, @intCast(ovecsize)));

switch (result) {
c.PCRE_ERROR_NOMATCH => return null,
Expand All @@ -158,7 +158,7 @@ pub const Regex = struct {
return error.ExecError; // TODO: should clarify
}

var caps: []?Capture = try allocator.alloc(?Capture, @intCast(usize, self.capture_count + 1));
var caps: []?Capture = try allocator.alloc(?Capture, @as(usize, @intCast(self.capture_count + 1)));
errdefer allocator.free(caps);
for (caps, 0..) |*cap, i| {
if (i >= result) {
Expand All @@ -168,8 +168,8 @@ pub const Regex = struct {
cap.* = null;
} else {
cap.* = .{
.start = @intCast(usize, ovector[i * 2]),
.end = @intCast(usize, ovector[i * 2 + 1]),
.start = @as(usize, @intCast(ovector[i * 2])),
.end = @as(usize, @intCast(ovector[i * 2 + 1])),
};
}
}
Expand Down

0 comments on commit 43df7fb

Please sign in to comment.