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.zig25
1 files changed, 13 insertions, 12 deletions
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