Skip to content

Commit

Permalink
added writeTrue and writeFalse
Browse files Browse the repository at this point in the history
  • Loading branch information
r4gus committed Aug 4, 2024
1 parent bf11f57 commit 1e90579
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ pub fn writeTag(writer: anytype, tag: u64) !void {
/// Serialize a simple value.
pub fn writeSimple(writer: anytype, simple: u8) !void {
if (24 <= simple and simple <= 31) return error.ReservedValue;
const h: u8 = 0xf0;
const h: u8 = 0xe0;
const v: u64 = @as(u64, @intCast(simple));
try encode(writer, h, v);
}

pub fn writeTrue(writer: anytype) !void {
try writeSimple(writer, 21);
}

pub fn writeFalse(writer: anytype) !void {
try writeSimple(writer, 20);
}

/// Write the header of an array to `writer`.
///
/// You must write exactly `len` data items to `writer` afterwards.
Expand Down Expand Up @@ -430,3 +438,16 @@ test "stringify simple using builder 1" {

try std.testing.expectEqualSlices(u8, "\xf8\xff", x);
}

test "write true false" {
const allocator = std.testing.allocator;

var arr = std.ArrayList(u8).init(allocator);
defer arr.deinit();

try writeTrue(arr.writer());
try writeFalse(arr.writer());

try std.testing.expectEqual(@as(u8, 0xf5), arr.items[0]);
try std.testing.expectEqual(@as(u8, 0xf4), arr.items[1]);
}

0 comments on commit 1e90579

Please sign in to comment.