Skip to content

Commit 8a6c252

Browse files
committed
move typed wrappers to separated files
1 parent 0f6a6a9 commit 8a6c252

File tree

11 files changed

+493
-427
lines changed

11 files changed

+493
-427
lines changed

src/root.zig

Lines changed: 12 additions & 424 deletions
Large diffs are not rendered by default.

src/values/AnyArray.zig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const std = @import("std");
2+
const Prefix = @import("../root.zig").Prefix;
3+
const maxInt = std.math.maxInt;
4+
5+
pub fn prefix(len: u32) Prefix {
6+
var result: Prefix = .{};
7+
switch (len) {
8+
0...0b00001111 => {
9+
result.appendAssumeCapacity(0b10010000 | (0b00001111 & @as(u8, @truncate(len))));
10+
},
11+
(0b00001111 + 1)...maxInt(u16) => {
12+
result.appendAssumeCapacity(0xdc);
13+
result.writer().writeInt(u16, @truncate(len), .big) catch unreachable;
14+
},
15+
maxInt(u16) + 1...maxInt(u32) => {
16+
result.appendAssumeCapacity(0xdd);
17+
result.writer().writeInt(u32, len, .big) catch unreachable;
18+
},
19+
}
20+
return result;
21+
}
22+
23+
pub fn count(len: u32) usize {
24+
return @call(.always_inline, prefix, .{len}).len;
25+
}
26+
27+
pub fn write(dst: []u8, len: u32) usize {
28+
const p = prefix(len);
29+
@memcpy(dst, p.constSlice());
30+
return p.len;
31+
}
32+
33+
pub fn pipe(writer: anytype, len: u32) !usize {
34+
const p = prefix(len);
35+
return try writer.write(p.constSlice());
36+
}

src/values/AnyBin.zig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const std = @import("std");
2+
const Prefix = @import("../root.zig").Prefix;
3+
const ContainerType = @import("../root.zig").ContainerType;
4+
const maxInt = std.math.maxInt;
5+
6+
pub fn prefix(len: u32) Prefix {
7+
var result: Prefix = .{};
8+
switch (len) {
9+
0...maxInt(u8) => {
10+
result.appendSliceAssumeCapacity(&.{
11+
@intFromEnum(ContainerType.bin8),
12+
@as(u8, @truncate(len)),
13+
});
14+
},
15+
maxInt(u8) + 1...maxInt(u16) => {
16+
result.appendAssumeCapacity(@intFromEnum(ContainerType.bin16));
17+
result.writer().writeInt(u16, @truncate(len), .big) catch unreachable;
18+
},
19+
maxInt(u16) + 1...maxInt(u32) => {
20+
result.appendAssumeCapacity(@intFromEnum(ContainerType.bin32));
21+
result.writer().writeInt(u32, len, .big) catch unreachable;
22+
},
23+
}
24+
return result;
25+
}
26+
27+
pub fn count(len: u32) usize {
28+
return @call(.always_inline, prefix, .{len}).len;
29+
}
30+
31+
pub fn write(dst: []u8, len: u32) usize {
32+
const p = prefix(len);
33+
@memcpy(dst, p.constSlice());
34+
return p.len;
35+
}
36+
37+
pub fn pipe(writer: anytype, len: u32) !usize {
38+
const p = prefix(len);
39+
return try writer.write(p.constSlice());
40+
}
41+
42+
pub fn pipeVal(writer: anytype, value: []const u8) !usize {
43+
const sz0 = try pipe(writer, @intCast(value.len));
44+
const sz1 = try writer.write(value);
45+
return sz0 + sz1;
46+
}

src/values/AnyExt.zig

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const std = @import("std");
2+
const Prefix = @import("../root.zig").Prefix;
3+
const maxInt = std.math.maxInt;
4+
const log2 = std.math.log2;
5+
6+
/// Generate a ext prefix.
7+
pub fn prefix(len: u32, extype: i8) Prefix {
8+
var result: Prefix = .{};
9+
switch (len) {
10+
1, 2, 4, 8, 16 => |b| {
11+
result.appendAssumeCapacity(0xd4 + log2(b));
12+
result.writer().writeInt(i8, extype, .big) catch unreachable;
13+
},
14+
0...maxInt(u8) => {
15+
result.appendSliceAssumeCapacity(&.{ 0xc7, @truncate(len) });
16+
result.writer().writeInt(i8, extype, .big) catch unreachable;
17+
},
18+
maxInt(u8) + 1...maxInt(u16) => {
19+
result.appendAssumeCapacity(0xc8);
20+
result.writer().writeInt(u16, @truncate(len), .big) catch unreachable;
21+
result.writer().writeInt(i8, extype, .big) catch unreachable;
22+
},
23+
maxInt(u16) + 1...maxInt(u32) => {
24+
result.appendAssumeCapacity(0xc9);
25+
result.writer().writeInt(u32, @truncate(len), .big) catch unreachable;
26+
result.writer().writeInt(i8, extype, .big) catch unreachable;
27+
},
28+
}
29+
return result;
30+
}
31+
32+
pub fn count(len: u32, extype: i8) usize {
33+
return @call(.always_inline, prefix, .{ len, extype }).len;
34+
}
35+
36+
pub fn write(dst: []u8, len: u32, extype: i8) usize {
37+
const p = prefix(len, extype);
38+
@memcpy(dst, p.constSlice());
39+
return p.len;
40+
}
41+
42+
pub fn pipe(writer: anytype, len: u32, extype: i8) !usize {
43+
const p = prefix(len, extype);
44+
return try writer.write(p.constSlice());
45+
}
46+
47+
pub fn pipeVal(writer: anytype, extype: i8, value: []const u8) !usize {
48+
const sz0 = try pipe(writer, @intCast(value.len), extype);
49+
const sz1 = try writer.write(value);
50+
return sz0 + sz1;
51+
}

src/values/AnyMap.zig

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const std = @import("std");
2+
const Prefix = @import("../root.zig").Prefix;
3+
const maxInt = std.math.maxInt;
4+
5+
/// Generate a map prefix.
6+
///
7+
/// The `len` here is the number of the k-v pairs.
8+
/// The elements of the map must be placed as
9+
/// KEY VALUE KEY VALUE ... so on.
10+
pub fn prefix(len: u32) Prefix {
11+
var result: Prefix = .{};
12+
switch (len) {
13+
0...0b00001111 => {
14+
result.appendAssumeCapacity(0b10000000 | (0b00001111 & @as(u8, @truncate(len))));
15+
},
16+
(0b00001111 + 1)...maxInt(u16) => {
17+
result.appendAssumeCapacity(0xde);
18+
result.writer().writeInt(u16, @truncate(len), .big) catch unreachable;
19+
},
20+
maxInt(u16) + 1...maxInt(u32) => {
21+
result.appendAssumeCapacity(0xdf);
22+
result.writer().writeInt(u32, len, .big) catch unreachable;
23+
},
24+
}
25+
return result;
26+
}
27+
28+
pub fn count(len: u32) usize {
29+
return @call(.always_inline, prefix, .{len}).len;
30+
}
31+
32+
pub fn write(dst: []u8, len: u32) usize {
33+
const p = prefix(len);
34+
@memcpy(dst, p.constSlice());
35+
return p.len;
36+
}
37+
38+
pub fn pipe(writer: anytype, len: u32) !usize {
39+
const p = prefix(len);
40+
return try writer.write(p.constSlice());
41+
}

src/values/AnyStr.zig

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const std = @import("std");
2+
const Prefix = @import("../root.zig").Prefix;
3+
const maxInt = std.math.maxInt;
4+
5+
pub fn prefix(len: u32) Prefix {
6+
var result: Prefix = .{};
7+
switch (len) {
8+
0...0b00011111 => {
9+
result.appendAssumeCapacity(0b10100000 | (0b00011111 & @as(u8, @intCast(len))));
10+
},
11+
0b00011111 + 1...maxInt(u8) => {
12+
result.appendSliceAssumeCapacity(&.{
13+
0xd9,
14+
@truncate(len),
15+
});
16+
},
17+
maxInt(u8) + 1...maxInt(u16) => {
18+
result.appendAssumeCapacity(0xda);
19+
result.writer().writeInt(u16, @truncate(len), .big) catch unreachable;
20+
},
21+
maxInt(u16) + 1...maxInt(u32) => {
22+
result.appendAssumeCapacity(0xdb);
23+
result.writer().writeInt(u32, len, .big) catch unreachable;
24+
},
25+
}
26+
return result;
27+
}
28+
29+
pub fn count(len: u32) usize {
30+
return @call(.always_inline, prefix, .{len}).len;
31+
}
32+
33+
pub fn write(dst: []u8, len: u32) usize {
34+
const p = prefix(len);
35+
@memcpy(dst, p.constSlice());
36+
return p.len;
37+
}
38+
39+
pub fn pipe(writer: anytype, len: u32) !usize {
40+
const p = prefix(len);
41+
return try writer.write(p.constSlice());
42+
}
43+
44+
pub fn pipeVal(writer: anytype, value: []const u8) !usize {
45+
const sz0 = try pipe(writer, @intCast(value.len));
46+
const sz1 = try writer.write(value);
47+
return sz0 + sz1;
48+
}

src/values/Bool.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
pub fn count(_: bool) usize {
2+
return 1;
3+
}
4+
5+
inline fn convert(value: bool) u8 {
6+
return switch (value) {
7+
true => 0xc3,
8+
false => 0xc2,
9+
};
10+
}
11+
12+
pub fn pipe(writer: anytype, value: bool) !usize {
13+
_ = try writer.writeByte(convert(value));
14+
return 1;
15+
}
16+
17+
pub fn write(dst: []u8, value: bool) usize {
18+
dst[0] = convert(value);
19+
return 1;
20+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const std = @import("std");
2-
const Float = @import("./root.zig").Float;
2+
const Float = @import("../root.zig").Float;
33

44
pub fn count(value: comptime_int) usize {
55
return pipe(std.io.null_writer, value) catch unreachable;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
2-
const compatstd = @import("./compatstd.zig");
3-
const toolkit = @import("./toolkit.zig");
2+
const compatstd = @import("../compatstd.zig");
3+
const toolkit = @import("../toolkit.zig");
44
const countIntByteRounded = toolkit.countIntByteRounded;
55
const makeFixIntNeg = toolkit.makeFixIntNeg;
66
const makeFixIntPos = toolkit.makeFixIntPos;

src/values/Nil.zig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const std = @import("std");
2+
3+
const Nil = @This();
4+
5+
pub fn count() usize {
6+
return 1;
7+
}
8+
9+
pub fn pipe(writer: anytype) !usize {
10+
_ = try writer.writeByte(0xc0);
11+
return 1;
12+
}
13+
14+
pub fn write(dst: []u8) usize {
15+
dst[0] = 0xc0;
16+
return 1;
17+
}
18+
19+
test write {
20+
const t = std.testing;
21+
var buf: [1]u8 = .{0};
22+
const size = Nil.write(&buf);
23+
try t.expectEqual(@as(usize, 1), size);
24+
try t.expectEqual(@as(u8, 0xc0), buf[0]);
25+
}

0 commit comments

Comments
 (0)