diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-08-31 23:05:16 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-08-31 23:05:16 +0200 |
| commit | 804204db7cf1e8a28defd359d17fa72949e13eff (patch) | |
| tree | 3536d961838d1a6640d1295d808768736bf2317b /src/str.zig | |
| parent | 956cd84ca545f1d93e2218cbbdbb39c81a46bec8 (diff) | |
Implement with ptrCast to make it more transparent.
Diffstat (limited to 'src/str.zig')
| -rw-r--r-- | src/str.zig | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/str.zig b/src/str.zig new file mode 100644 index 0000000..7dde4f9 --- /dev/null +++ b/src/str.zig @@ -0,0 +1,26 @@ + +const c = @import("c"); +const zpy = @import("root.zig"); + +pub const Str = extern struct { + const Self = @This(); + + data: c.PyObject, + + pub fn fromString(buffer: []const u8) *Self { + return @ptrCast(c.PyUnicode_FromStringAndSize(@ptrCast(buffer), @intCast(buffer.len))); + } + + pub fn asUtf8(self: *Self) ![]const u8 { + var size: c.Py_ssize_t = undefined; + const string: [*]const u8 = c.PyUnicode_AsUTF8AndSize(@ptrCast(self), &size); + + if (size == -1) return error.Exception; + + return string[0..@as(usize, @intCast(size))]; + } + + pub fn asObject(self: *Self) *zpy.Object { + return @ptrCast(self); + } +}; |