summaryrefslogtreecommitdiff
path: root/src/libzisp/value/sstr.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2025-02-18 22:48:57 +0100
committerTaylan Kammer <taylan.kammer@gmail.com>2025-02-18 22:48:57 +0100
commit4d0db1a1065f18d879b3ff90da6ecb14e9e1ae31 (patch)
tree7c5c275e7f3dae7bf96377560269b5a1bfa1fb99 /src/libzisp/value/sstr.zig
parent2384a31c42f480c961785bcf0520bb0688b8e028 (diff)
update
Diffstat (limited to 'src/libzisp/value/sstr.zig')
-rw-r--r--src/libzisp/value/sstr.zig24
1 files changed, 9 insertions, 15 deletions
diff --git a/src/libzisp/value/sstr.zig b/src/libzisp/value/sstr.zig
index 55b3f8b..896b8d7 100644
--- a/src/libzisp/value/sstr.zig
+++ b/src/libzisp/value/sstr.zig
@@ -5,10 +5,7 @@ const Value = @import("../value.zig").Value;
// Zig API
pub fn check(v: Value) bool {
- return v.isPacked() and
- !v.sstr.fixnum and
- !v.sstr.ptr and
- v.sstr.tag == .sstr;
+ return v.isOther(.sstr);
}
pub fn assert(v: Value) void {
@@ -41,25 +38,22 @@ fn assertValidSstr(s: []const u8) void {
// Different ways of doing the following have been tested, including manual
// shifting and bit masking, but memcpy always wins easily according to our
-// micro-benchmarks, both under ReleaseSafe and under ReleaseFast.
+// micro-benchmarks, under both ReleaseSafe and ReleaseFast.
pub fn pack(s: []const u8) Value {
assertValidSstr(s);
- var v = Value{ .sstr = .{ .value = 0 } };
- const dest: [*]u8 = @ptrCast(&v.sstr.value);
+ var v = Value{ .sstr = .{ .string = 0 } };
+ const dest: [*]u8 = @ptrCast(&v.sstr.string);
@memcpy(dest, s);
return v;
}
-// It's tempting to inline for here to eliminate the if statement or prevent
-// need of @truncate but all alternatives were a little slower.
-
pub fn unpack(v: Value) struct { [6]u8, u3 } {
- var s: [6]u8 = undefined;
- const src: *const [6]u8 = @ptrCast(&v.sstr.value);
- @memcpy(&s, src);
- for (0..6) |i| {
- if (s[i] == 0) return .{ s, @intCast(i) };
+ const s: [6]u8 = @bitCast(v.sstr.string);
+ inline for (0..6) |i| {
+ if (s[i] == 0) return .{ s, i };
}
return .{ s, 6 };
}
+
+// No Zisp API for sstr specifically, since it's a string. See string.zig.