summaryrefslogtreecommitdiff
path: root/src/str.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/str.zig')
-rw-r--r--src/str.zig26
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);
+ }
+};