summaryrefslogtreecommitdiff
path: root/abi
diff options
context:
space:
mode:
Diffstat (limited to 'abi')
-rw-r--r--abi/addon.zig24
1 files changed, 15 insertions, 9 deletions
diff --git a/abi/addon.zig b/abi/addon.zig
index 1fa2279a..059e016d 100644
--- a/abi/addon.zig
+++ b/abi/addon.zig
@@ -248,8 +248,14 @@ pub fn loadBytes(s: anytype, bytes: []const u8, version: u32) bool {
pub const body_max = 4096;
var body_cut = false;
-fn span(p: [*c]const u8, n: u64) []const u8 {
- return if (p == null) "" else p[0..n];
+/// A C pointer and its count as a slice; NULL is none, whatever the count:
+/// C says none with a NULL (a page's search without a hit, a selection
+/// over no text, an outline of no entries), and a NULL C pointer sliced
+/// is a safety panic even at length 0 (a release build's abort). Every
+/// pointer a C struct or a C caller hands with its count is read through
+/// it: pdf.zig's test holds the bridge's to it.
+pub fn cSlice(ptr: anytype, n: usize) []const std.meta.Child(@TypeOf(ptr)) {
+ return if (ptr == null) &.{} else ptr[0..n];
}
/// `pardes_addon` exported, when built as a library alone: the kind is its
@@ -282,7 +288,7 @@ pub fn ops(comptime T: type) c.pa_ops {
fn open(path: [*c]const u8, bytes: [*c]const u8, len: u64) callconv(.c) ?*anyopaque {
const s = arena.create(T) catch return null;
s.* = std.mem.zeroInit(T, .{});
- if (@hasDecl(T, "open")) T.open(s, if (path == null) null else std.mem.span(path), if (bytes == null) null else bytes[0..len]);
+ if (@hasDecl(T, "open")) T.open(s, if (path == null) null else std.mem.span(path), if (bytes == null) null else cSlice(bytes, len));
return s;
}
fn close(st: ?*anyopaque) callconv(.c) void {
@@ -329,7 +335,7 @@ pub fn ops(comptime T: type) c.pa_ops {
return @intCast(from.len);
}
fn address(st: ?*anyopaque, t: [*c]const u8, n: u32) callconv(.c) c_int {
- return @intFromBool(T.address(self(st) orelse return 0, span(t, n)));
+ return @intFromBool(T.address(self(st) orelse return 0, cSlice(t, n)));
}
fn where(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 {
return write(T.where, self(st) orelse return 0, buf, n);
@@ -337,26 +343,26 @@ pub fn ops(comptime T: type) c.pa_ops {
fn ctl(st: ?*anyopaque, line: [*c]const u8, n: u32, reply: [*c]u8, cap: u32) callconv(.c) c_int {
var w: std.Io.Writer = .fixed(reply[0..cap -| 1]);
defer reply[w.end] = 0;
- return if (T.ctl(self(st) orelse return 1, span(line, n), &w)) c.PA_OK else 1;
+ return if (T.ctl(self(st) orelse return 1, cSlice(line, n), &w)) c.PA_OK else 1;
}
fn ctl_read(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 {
return write(T.ctlRead, self(st) orelse return 0, buf, n);
}
fn job_run(in: [*c]const u8, n: u32, out: [*c]c.pa_out) callconv(.c) u32 {
const gpa: Allocator = .{ .ptr = out, .vtable = &.{ .alloc = jobAlloc, .resize = Allocator.noResize, .remap = Allocator.noRemap, .free = Allocator.noFree } };
- return @intCast((T.jobRun(span(in, n), gpa) orelse return 0).len);
+ return @intCast((T.jobRun(cSlice(in, n), gpa) orelse return 0).len);
}
fn job_done(st: ?*anyopaque, token: u32, out: [*c]const u8, n: u32) callconv(.c) void {
- T.jobDone(self(st) orelse return, token, span(out, n));
+ T.jobDone(self(st) orelse return, token, cSlice(out, n));
}
fn dump(st: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 {
return @intCast(T.dump(self(st) orelse return 0, buf[0..n]));
}
fn load(st: ?*anyopaque, buf: [*c]const u8, n: u32, version: u32) callconv(.c) c_int {
- return @intFromBool(!T.load(self(st) orelse return 1, span(buf, n), version));
+ return @intFromBool(!T.load(self(st) orelse return 1, cSlice(buf, n), version));
}
fn search(st: ?*anyopaque, pat: [*c]const u8, n: u32) callconv(.c) void {
- T.search(self(st) orelse return, span(pat, n));
+ T.search(self(st) orelse return, cSlice(pat, n));
}
};
if (T.head.name.len > 15) @compileError(@typeName(T) ++ "'s name " ++ T.head.name ++ " is over 15 bytes");