@@ -12,9 +12,7 @@ const unpackIntValue = @import("int.zig").unpackIntValue;
1212const unpackShortIntValue = @import ("int.zig" ).unpackShortIntValue ;
1313
1414pub fn sizeOfPackedBinaryHeader (len : usize ) ! usize {
15- if (len <= hdrs .FIXSTR_MAX - hdrs .FIXSTR_MIN ) {
16- return 1 ;
17- } else if (len <= std .math .maxInt (u8 )) {
15+ if (len <= std .math .maxInt (u8 )) {
1816 return 1 + @sizeOf (u8 );
1917 } else if (len <= std .math .maxInt (u16 )) {
2018 return 1 + @sizeOf (u16 );
@@ -30,16 +28,14 @@ pub fn sizeOfPackedBinary(len: usize) !usize {
3028}
3129
3230pub fn packBinaryHeader (writer : * std.Io.Writer , len : usize ) ! void {
33- if (len <= hdrs .FIXSTR_MAX - hdrs .FIXSTR_MIN ) {
34- try writer .writeByte (hdrs .FIXSTR_MIN + @as (u8 , @intCast (len )));
35- } else if (len <= std .math .maxInt (u8 )) {
36- try writer .writeByte (hdrs .STR8 );
31+ if (len <= std .math .maxInt (u8 )) {
32+ try writer .writeByte (hdrs .BIN8 );
3733 try packIntValue (writer , u8 , @intCast (len ));
3834 } else if (len <= std .math .maxInt (u16 )) {
39- try writer .writeByte (hdrs .STR16 );
35+ try writer .writeByte (hdrs .BIN16 );
4036 try packIntValue (writer , u16 , @intCast (len ));
4137 } else if (len <= std .math .maxInt (u32 )) {
42- try writer .writeByte (hdrs .STR32 );
38+ try writer .writeByte (hdrs .BIN32 );
4339 try packIntValue (writer , u32 , @intCast (len ));
4440 } else {
4541 return error .BinaryTooLong ;
@@ -50,6 +46,9 @@ pub fn unpackBinaryHeader(reader: *std.Io.Reader, comptime MaybeOptionalType: ty
5046 const Type = NonOptional (MaybeOptionalType );
5147 const header = try reader .takeByte ();
5248 switch (header ) {
49+ hdrs .BIN8 = > return try unpackIntValue (reader , u8 , Type ),
50+ hdrs .BIN16 = > return try unpackIntValue (reader , u16 , Type ),
51+ hdrs .BIN32 = > return try unpackIntValue (reader , u32 , Type ),
5352 hdrs .FIXSTR_MIN ... hdrs .FIXSTR_MAX = > return try unpackShortIntValue (header , hdrs .FIXSTR_MIN , hdrs .FIXSTR_MAX , Type ),
5453 hdrs .STR8 = > return try unpackIntValue (reader , u8 , Type ),
5554 hdrs .STR16 = > return try unpackIntValue (reader , u16 , Type ),
@@ -89,7 +88,7 @@ pub fn unpackBinaryInto(reader: *std.Io.Reader, buf: []u8) ![]u8 {
8988pub const Binary = struct {
9089 data : []const u8 ,
9190
92- pub fn msgpackWrite (self : * Binary , packer : anytype ) ! void {
91+ pub fn msgpackWrite (self : Binary , packer : anytype ) ! void {
9392 try packer .writeBinary (self .data );
9493 }
9594
@@ -101,21 +100,29 @@ pub const Binary = struct {
101100
102101const packed_null = [_ ]u8 {0xc0 };
103102const packed_abc = [_ ]u8 { 0xa3 , 0x61 , 0x62 , 0x63 };
103+ const packed_bin_abc = [_ ]u8 { 0xc4 , 0x03 , 0x61 , 0x62 , 0x63 };
104104
105105test "packBinary: abc" {
106106 var buffer : [16 ]u8 = undefined ;
107107 var writer = std .Io .Writer .fixed (& buffer );
108108 try packBinary (& writer , []const u8 , "abc" );
109- try std .testing .expectEqualSlices (u8 , & packed_abc , writer .buffered ());
109+ try std .testing .expectEqualSlices (u8 , & packed_bin_abc , writer .buffered ());
110110}
111111
112- test "unpackBinary: abc" {
112+ test "unpackBinary: abc from string header " {
113113 var reader = std .Io .Reader .fixed (& packed_abc );
114114 const data = try unpackBinary (& reader , std .testing .allocator );
115115 defer std .testing .allocator .free (data );
116116 try std .testing .expectEqualSlices (u8 , "abc" , data );
117117}
118118
119+ test "unpackBinary: abc bin8" {
120+ var reader = std .Io .Reader .fixed (& packed_bin_abc );
121+ const data = try unpackBinary (& reader , std .testing .allocator );
122+ defer std .testing .allocator .free (data );
123+ try std .testing .expectEqualSlices (u8 , "abc" , data );
124+ }
125+
119126test "packBinary: null" {
120127 var buffer : [16 ]u8 = undefined ;
121128 var writer = std .Io .Writer .fixed (& buffer );
@@ -124,5 +131,5 @@ test "packBinary: null" {
124131}
125132
126133test "sizeOfPackedBinary" {
127- try std .testing .expectEqual (1 , sizeOfPackedBinary (0 ));
134+ try std .testing .expectEqual (2 , sizeOfPackedBinary (0 ));
128135}
0 commit comments