const std = @import("std"); const value = @import("../value.zig"); const Value = value.Value; // Zig API pub fn check(v: Value) bool { return v.isSstr(); } pub fn assert(v: Value) void { if (!check(v)) { v.dump(); @panic("not sstr"); } } pub fn isValidSstr(s: []const u8) bool { if (s.len > 6) return false; for (s) |c| if (c == 0) return false; return true; } fn assertValidSstr(s: []const u8) void { if (!isValidSstr(s)) { std.debug.print("invalid sstr: {s}\n", .{s}); @panic("invalid sstr"); } } const Sstr = @FieldType(Value, "sstr"); const ofs = @offsetOf(Sstr, "bytes"); pub fn pack(s: []const u8) Value { assertValidSstr(s); var v = Value{ .sstr = .{ .bytes = 0 } }; @memcpy(v.bufRW()[ofs .. ofs + s.len], s); return v; } pub fn unpack(v: *const Value) []const u8 { assert(v.*); const len = value.sstrLen(v.bits); return v.bufRO()[ofs .. ofs + len]; } // No Zisp API for sstr specifically, since it's a string. See string.zig.