From 5e461592bfda4b42e6c11beaae1fa4b04f0aec95 Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Sun, 31 May 2026 14:35:49 +0200 Subject: [WORKING STATE] Fixes and cleanup. --- src/zisp/value/istr.zig | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/zisp/value/istr.zig') diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig index 8f70725..a525c7e 100644 --- a/src/zisp/value/istr.zig +++ b/src/zisp/value/istr.zig @@ -12,8 +12,10 @@ const Value = value.Value; /// Pointer to an interned string. /// /// First 64 bits are a cached hash, of which the lowest 8 bits are actually the -/// length of the string: u64hash = (real_u64hash << 1) | u8length -pub const Istr = *align(8) packed union { +/// length of the string: u64hash = (real_u64hash << 8) | u8length +pub const IstrPtr = *align(8) IstrHeader; + +const IstrHeader = packed union { hash: u64, meta: packed struct(u64) { len: u8, @@ -26,7 +28,8 @@ pub const Istr = *align(8) packed union { pub fn putStr(self: *@This(), s: []const u8) void { std.debug.assert(s.len <= 255); - @memcpy(self.bufU8()[8 .. 8 + s.len], s); + const buf = self.bufU8(); + @memcpy(buf[8 .. 8 + s.len], s); } pub fn str(self: *@This()) []const u8 { @@ -35,23 +38,23 @@ pub const Istr = *align(8) packed union { } }; -pub fn new(alloc: Alloc, hash: u64, s: []const u8) !Istr { - const aln = std.mem.Alignment.of(Istr); +pub fn new(alloc: Alloc, hash: u64, s: []const u8) !IstrPtr { + const aln = std.mem.Alignment.of(IstrPtr); const ptr = try alloc.alignedAlloc(u8, aln, 8 + s.len); - const istr: Istr = @ptrCast(ptr); + const istr: IstrPtr = @ptrCast(ptr); istr.hash = hash; istr.putStr(s); return istr; } -pub fn check(v: Value) ?Istr { +pub fn check(v: Value) ?IstrPtr { if (v.getPtr(.istr)) |p| { return @ptrCast(p); } return null; } -pub fn assert(v: Value) Istr { +pub fn assert(v: Value) IstrPtr { return check(v) orelse { v.dump(); @panic("not istr"); -- cgit v1.2.3