diff options
Diffstat (limited to 'src/bytes.zig')
| -rw-r--r-- | src/bytes.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/bytes.zig b/src/bytes.zig new file mode 100644 index 0000000..f1eb258 --- /dev/null +++ b/src/bytes.zig @@ -0,0 +1,43 @@ +const c = @import("c"); +const zpy = @import("root.zig"); + +pub const Bytes = extern struct { + const Self = @This(); + + data: c.PyObject, + + pub fn fromString(string: []const u8) !*Self { + return @ptrCast(c.PyBytes_FromStringAndSize(@ptrCast(string), string.len) orelse return error.Exception); + } + + pub fn fromObject(object: *zpy.Object) !*Self { + return @ptrCast(c.PyBytes_FromObject(@ptrCast(object)) orelse return error.Exception); + } + + pub fn size(self: *const Self) usize { + return @intCast(c.PyBytes_Size(@ptrCast(@constCast(self)))); + } + + pub fn toString(self: *Self) []const u8 { + var buffer: [*]const u8 = undefined; + var length: c.Py_ssize_t = undefined; + + if (c.PyBytes_AsStringAndSize(@ptrCast(self), &buffer, &length) == -1) { + return error.Exception; + } + + return buffer[0..length]; + } + + pub fn concat(self: *Self, new: *Self) !void { + c.PyBytes_Concat(@ptrCast(&self), @ptrCast(new)); + } + + pub fn concatAndDelete(self: *Self, new: *Self) !void { + c.PyBytes_ConcatAndDel(@ptrCast(&self), @ptrCast(new)); + } + + pub fn join(self: *Self, object: *zpy.Object) !*zpy.Object { + return @ptrCast(c.PyBytes_Join(@ptrCast(self), @ptrCast(object)) orelse return error.Exception); + } +}; |