I would like to use POSIX regex routines in Zig, but I can't work out how to allocate a pattern buffer (regex_t). I think the problem is because it is defined as typedef struct re_pattern_buffer regex_t (in my case, on a GNU system), and Zig cannot therefore tell how big it is.
In a standard zig init-exe project (Zig master as of today), I have the following main.zig:
const std = @import("std");
const c = @cImport({@cInclude("regex.h");});
pub fn main() anyerror!void {
var re: c.regex_t = undefined;
std.log.info("address of re {}", .{re});
}
In an otherwise default build.zig, I add a line exe.linkLibC();.
Then, zig build gives the following error:
./src/main.zig:5:5: error: variable of type '.cimport:2:11.struct_re_pattern_buffer' not allowed
var re: c.regex_t = undefined;
^
This is fair enough, but I cannot work out what I should do! I can see a couple of potential workarounds:
- Use
struct re_pattern_bufferdirectly; but this is not portable, as it's not part of the POSIX standard (though at least musl and glibc use it as the underlying struct ofregex_t). - Write a C function that
mallocs and returns aregex_t, and use that. Should be fine, as all the POSIX APIs take aregex_t *, so Zig doesn't need to know how big aregex_tis if it only ever has to handle pointers.
But it would be nice to be able to do without either of these workarounds. Any hints?