diff options
Diffstat (limited to 'src/zisp/value')
| -rw-r--r-- | src/zisp/value/fixnum.zig | 2 | ||||
| -rw-r--r-- | src/zisp/value/istr.zig | 25 |
2 files changed, 13 insertions, 14 deletions
diff --git a/src/zisp/value/fixnum.zig b/src/zisp/value/fixnum.zig index 1929ba2..3bd6112 100644 --- a/src/zisp/value/fixnum.zig +++ b/src/zisp/value/fixnum.zig @@ -54,8 +54,6 @@ fn unpackPositive(v: Value) i64 { return @bitCast(uint ^ positive_mask); } -// Although we use if, these should compile to branchless code using cmov. - pub fn pack(int: i64) Value { assertValidRange(int); if (int < 0) { diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig index dfe4614..a581289 100644 --- a/src/zisp/value/istr.zig +++ b/src/zisp/value/istr.zig @@ -12,6 +12,8 @@ const value = @import("../value.zig"); const IstrSet = gc.IstrSet; const Value = value.Value; +pub const max_len = 255; + // Zig API /// Pointer to an interned string. First byte is length. @@ -49,7 +51,7 @@ pub fn assert(v: Value) void { } pub fn isValidIstr(s: []const u8) bool { - return s.len <= 255; + return s.len <= max_len; } fn assertValidIstr(s: []const u8) void { @@ -67,6 +69,15 @@ pub fn new(alloc: Alloc, s: []const u8) !IstrPtr { return istr; } +pub fn getOrNew(s: []const u8) !IstrPtr { + return getOrNewInSet(gc.mainIstrSet(), s); +} + +pub fn getOrNewInSet(set: *IstrSet, s: []const u8) !IstrPtr { + assertValidIstr(s); + return try set.getOrNew(s); +} + pub fn pack(istr: IstrPtr) Value { const ptr = @intFromPtr(istr); return .{ .istr = .{ .ptr = @intCast(ptr) } }; @@ -76,19 +87,9 @@ pub fn unpack(v: Value) IstrPtr { return @ptrFromInt(v.istr.ptr); } -pub fn getOrNew(s: []const u8) !Value { - return getOrNewInSet(gc.mainIstrSet(), s); -} - -pub fn getOrNewInSet(set: *IstrSet, s: []const u8) !Value { - assertValidIstr(s); - return pack(try set.getOrNew(s)); -} - pub fn getBytes(v: Value) []const u8 { assert(v); - const istr: IstrPtr = @ptrFromInt(v.istr.ptr); - return istr.bytes(); + return unpack(v).bytes(); } // Zisp API |
