blob: c2b28593151c4d932697959bd66a584143a87be8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
const std = @import("std");
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;
}
pub fn assert(v: Value) void {
if (!check(v)) {
v.dump();
@panic("not sstr");
}
}
// For now, ignore encoding, just treat it as []u8.
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}", .{s});
@panic("invalid sstr");
}
}
// 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.
pub fn pack(s: []const u8) Value {
assertValidSstr(s);
var v = Value{ .sstr = .{ .value = 0 } };
const dest: [*]u8 = @ptrCast(&v.sstr.value);
@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, @truncate(i) };
}
return .{ s, 6 };
}
|