//! Intermediate-length String //! //! Length up to 255, stored in pointer. Typically interned in an IstrPool. const std = @import("std"); const Alloc = std.mem.Allocator; const gc = @import("../gc.zig"); const value = @import("../value.zig"); const IstrPool = gc.IstrPool; const Value = value.Value; pub const max_len = 255; // Zig API pub fn check(v: Value) bool { return v.isIstr(); } pub fn assert(v: Value) void { if (!check(v)) { v.dump(); @panic("not istr"); } } pub fn isValidIstr(s: []const u8) bool { return s.len <= max_len; } fn assertValidIstr(s: []const u8) void { if (!isValidIstr(s)) { std.debug.print("invalid istr: {s}\n", .{s}); @panic("invalid istr"); } } pub fn new(s: []const u8) !Value { const istr = gc.allocIstr(s.len); @memcpy(istr) } pub fn getOrNew(s: []const u8) !Value { return getOrNewInPool(gc.mainIstrPool(), s); } pub fn getOrNewInPool(pool: *IstrPool, s: []const u8) !Value { assertValidIstr(s); return try pool.getOrNew(s); } pub fn getBytes(v: Value) []const u8 { assert(v); const ptr = gc.istrPtrFromIdx(v.istr.idx); return ptr[0..v.istr.len]; } // Zisp API pub fn pred(v: Value) Value { return value.boole.pack(check(v)); } pub fn getLen(v: Value) Value { assert(v); return value.fixnum.pack(v.istr.len); } // TODO: Zisp representation of IstrPool & ability to intern in given IstrPool pub fn intern(v: Value) Value { const istr = assert(v); const pool = gc.mainIstrPool(); return pool.getOrPut(istr); }