summaryrefslogtreecommitdiff
path: root/src/Str.zig
blob: eaca378d5e6b2d539fe4edcdd0b4e160a1f6d79c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const c = @import("c");
const zpy = @import("root.zig");

const Self = @This();

object: zpy.Object,


pub fn asUtf8(self: *Self) zpy.Error![]const u8 {
    var size: c.Py_ssize_t = undefined;
    const string: [*]const u8 = c.PyUnicode_AsUTF8AndSize(self.object.ptr, &size);

    if (size == -1) return error.Exception;

    return string[0..@as(usize, @intCast(size))];
}

pub fn deinit(self: *Self) void {
    self.object.decref();
    self.* = undefined;
}