summaryrefslogtreecommitdiff
path: root/src/bytes.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-09-03 19:50:47 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-09-03 19:50:47 +0200
commit2173abbd1a52f4af808c3356b63f554f3d2c5f76 (patch)
treed2783c4f3bdf7d34c5219dea0ba86d464fca195c /src/bytes.zig
parent6c2e06ed20cdea1a67629554d8aa1d5fcda030f9 (diff)
implement bytesHEADmaster
Diffstat (limited to 'src/bytes.zig')
-rw-r--r--src/bytes.zig43
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);
+ }
+};