summaryrefslogtreecommitdiff
path: root/src/zisp/value/istr.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/value/istr.zig')
-rw-r--r--src/zisp/value/istr.zig23
1 files changed, 13 insertions, 10 deletions
diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig
index dec3b09..8f70725 100644
--- a/src/zisp/value/istr.zig
+++ b/src/zisp/value/istr.zig
@@ -3,6 +3,8 @@ const std = @import("std");
const gc = @import("../gc.zig");
const value = @import("../value.zig");
+const Alloc = std.mem.Allocator;
+
const Value = value.Value;
// Zig API
@@ -22,25 +24,26 @@ pub const Istr = *align(8) packed union {
return @ptrCast(self);
}
- pub fn new(alloc: *Allocator, hash: u64, s: []const u8) Istr {
- const istr: Istr = @ptrCast(alloc.alloc(u8, 8 + s.len));
- istr.hash = hash;
- istr.putStr(s);
- return istr;
- }
-
pub fn putStr(self: *@This(), s: []const u8) void {
std.debug.assert(s.len <= 255);
- @memcpy(istr.bufU8()[8 .. 8 + s.len], s);
+ @memcpy(self.bufU8()[8 .. 8 + s.len], s);
}
pub fn str(self: *@This()) []const u8 {
const buf = self.bufU8();
- const len = self.meta.len;
- return buf[8 .. 8 + len];
+ return buf[8 .. 8 + self.meta.len];
}
};
+pub fn new(alloc: Alloc, hash: u64, s: []const u8) !Istr {
+ const aln = std.mem.Alignment.of(Istr);
+ const ptr = try alloc.alignedAlloc(u8, aln, 8 + s.len);
+ const istr: Istr = @ptrCast(ptr);
+ istr.hash = hash;
+ istr.putStr(s);
+ return istr;
+}
+
pub fn check(v: Value) ?Istr {
if (v.getPtr(.istr)) |p| {
return @ptrCast(p);