diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-05-29 23:24:31 +0200 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-05-29 23:24:31 +0200 |
| commit | 237d2e48af8dc5203dbf4c6b2414af6792a56243 (patch) | |
| tree | 45e87d378fbb389e23c8390949a4ac80408d87ae /src/zisp/value/istr.zig | |
| parent | b80fd02194693700697921bfe37b30b52f78559f (diff) | |
Write a concrete IstrSet specification.
Diffstat (limited to 'src/zisp/value/istr.zig')
| -rw-r--r-- | src/zisp/value/istr.zig | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig index f34dbd0..2ca2dad 100644 --- a/src/zisp/value/istr.zig +++ b/src/zisp/value/istr.zig @@ -1,25 +1,33 @@ const std = @import("std"); -const value = @import("../value.zig"); const gc = @import("../gc.zig"); +const value = @import("../value.zig"); const Value = value.Value; // Zig API -/// Pointer to beginning of an interned string with a 1-byte length prefix. -/// Aligned to 8 bytes like all Zisp heap pointers. +/// Pointer to beginning of an interned string with a 1-byte length prefix and +/// 8-byte hash cache suffix. Aligned to 8 bytes, like all Zisp heap pointers. pub const Istr = *align(8) struct { - pub fn buf(self: *@This()) [*]u8 { + fn buf(self: *@This()) [*]u8 { return @ptrCast(self); } pub fn len(self: *@This()) u8 { - return buf()[0]; + return self.buf()[0]; } pub fn str(self: *@This()) []u8 { - return buf()[1 .. 1 + len()]; + const end = 1 + self.len(); + return self.buf()[1..end]; + } + + pub fn getHash(self: *@This()) u64 { + // Hash stored in the next naturally aligned u64 + const hash_idx = ((self.len() + 8) & -8) / 8; + const buf: [*]u64 = @ptrCast(self); + return buf[hash_idx]; } }; |
