| author | |
| committer | |
| log | 5f26f79d2fff2bb758249bb0bde925e2268a7150 |
| tree | 366a65fa8365f0c23c5a111daa696b3873467c4b |
| parent | 9dbbd77fe92435e810bf1da88ba964c1b6852823 |
6 files changed, 19 insertions(+), 19 deletions(-)
src/BufIndexer.zig+5-5| ... | @@ -2,7 +2,7 @@ const std = @import("std"); | ... | @@ -2,7 +2,7 @@ const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const indexBufferT = extras.indexBufferT; | 4 | const indexBufferT = extras.indexBufferT; |
| 5 | const rawInt = extras.rawInt; | 5 | const rawIntBytes = extras.rawIntBytes; |
| 6 | 6 | ||
| 7 | pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type { | 7 | pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type { |
| 8 | return struct { | 8 | return struct { |
| ... | @@ -26,20 +26,20 @@ pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type { | ... | @@ -26,20 +26,20 @@ pub fn BufIndexer(comptime T: type, comptime endian: std.builtin.Endian) type { |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | test { | 28 | test { |
| 29 | const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); | 29 | const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); |
| 30 | const bindex = BufIndexer(u64, .Big).init(&bytes, 1); | 30 | const bindex = BufIndexer(u64, .Big).init(&bytes, 1); |
| 31 | try std.testing.expect(bindex.at(0) == 0x4e5a7da9f3f1d132); | 31 | try std.testing.expect(bindex.at(0) == 0x4e5a7da9f3f1d132); |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | test { | 34 | test { |
| 35 | const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); | 35 | const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); |
| 36 | const bindex = BufIndexer(u32, .Big).init(&bytes, 2); | 36 | const bindex = BufIndexer(u32, .Big).init(&bytes, 2); |
| 37 | try std.testing.expect(bindex.at(0) == 0x4e5a7da9); | 37 | try std.testing.expect(bindex.at(0) == 0x4e5a7da9); |
| 38 | try std.testing.expect(bindex.at(1) == 0xf3f1d132); | 38 | try std.testing.expect(bindex.at(1) == 0xf3f1d132); |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | test { | 41 | test { |
| 42 | const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); | 42 | const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); |
| 43 | const bindex = BufIndexer(u16, .Big).init(&bytes, 4); | 43 | const bindex = BufIndexer(u16, .Big).init(&bytes, 4); |
| 44 | try std.testing.expect(bindex.at(0) == 0x4e5a); | 44 | try std.testing.expect(bindex.at(0) == 0x4e5a); |
| 45 | try std.testing.expect(bindex.at(1) == 0x7da9); | 45 | try std.testing.expect(bindex.at(1) == 0x7da9); |
| ... | @@ -48,7 +48,7 @@ test { | ... | @@ -48,7 +48,7 @@ test { |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | test { | 50 | test { |
| 51 | const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); | 51 | const bytes = rawIntBytes(u64, 0x4e5a7da9f3f1d132); |
| 52 | const bindex = BufIndexer(u8, .Big).init(&bytes, 8); | 52 | const bindex = BufIndexer(u8, .Big).init(&bytes, 8); |
| 53 | try std.testing.expect(bindex.at(0) == 0x4e); | 53 | try std.testing.expect(bindex.at(0) == 0x4e); |
| 54 | try std.testing.expect(bindex.at(1) == 0x5a); | 54 | try std.testing.expect(bindex.at(1) == 0x5a); |
src/hashBytes.zig+4-4| ... | @@ -1,7 +1,7 @@ | ... | @@ -1,7 +1,7 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const rawInt = extras.rawInt; | 4 | const rawIntBytes = extras.rawIntBytes; |
| 5 | 5 | ||
| 6 | pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 { | 6 | pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 { |
| 7 | var h = Algo.init(.{}); | 7 | var h = Algo.init(.{}); |
| ... | @@ -13,16 +13,16 @@ pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 | ... | @@ -13,16 +13,16 @@ pub fn hashBytes(comptime Algo: type, bytes: []const u8) [Algo.digest_length]u8 |
| 13 | 13 | ||
| 14 | test { | 14 | test { |
| 15 | const A = std.crypto.hash.Md5; | 15 | const A = std.crypto.hash.Md5; |
| 16 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &std.mem.toBytes(rawInt(u128, 0x5d41402abc4b2a76b9719d911017c592)))); | 16 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u128, 0x5d41402abc4b2a76b9719d911017c592))); |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | test { | 19 | test { |
| 20 | const A = std.crypto.hash.Sha1; | 20 | const A = std.crypto.hash.Sha1; |
| 21 | // by default the array len is 24 for u160 | 21 | // by default the array len is 24 for u160 |
| 22 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), std.mem.toBytes(rawInt(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d))[0..20])); | 22 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d))); |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | test { | 25 | test { |
| 26 | const A = std.crypto.hash.sha2.Sha256; | 26 | const A = std.crypto.hash.sha2.Sha256; |
| 27 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &std.mem.toBytes(rawInt(u256, 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824)))); | 27 | try std.testing.expect(std.mem.eql(u8, &hashBytes(A, "hello"), &rawIntBytes(u256, 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824))); |
| 28 | } | 28 | } |
src/indexBufferT.zig+3-3| ... | @@ -2,7 +2,7 @@ const std = @import("std"); | ... | @@ -2,7 +2,7 @@ const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const readType = extras.readType; | 4 | const readType = extras.readType; |
| 5 | const rawInt = extras.rawInt; | 5 | const rawIntBytes = extras.rawIntBytes; |
| 6 | 6 | ||
| 7 | pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.Endian, idx: usize, max_len: usize) T { | 7 | pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.Endian, idx: usize, max_len: usize) T { |
| 8 | std.debug.assert(idx < max_len); | 8 | std.debug.assert(idx < max_len); |
| ... | @@ -13,12 +13,12 @@ pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.En | ... | @@ -13,12 +13,12 @@ pub fn indexBufferT(bytes: [*]const u8, comptime T: type, endian: std.builtin.En |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | test { | 15 | test { |
| 16 | const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); | 16 | const bytes = rawIntBytes(u32, 0x4e5a7da9); |
| 17 | try std.testing.expect(indexBufferT(&bytes, u16, .Big, 0, 2) == 0x4e5a); | 17 | try std.testing.expect(indexBufferT(&bytes, u16, .Big, 0, 2) == 0x4e5a); |
| 18 | try std.testing.expect(indexBufferT(&bytes, u16, .Big, 1, 2) == 0x7da9); | 18 | try std.testing.expect(indexBufferT(&bytes, u16, .Big, 1, 2) == 0x7da9); |
| 19 | } | 19 | } |
| 20 | test { | 20 | test { |
| 21 | const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); | 21 | const bytes = rawIntBytes(u32, 0x4e5a7da9); |
| 22 | try std.testing.expect(indexBufferT(&bytes, u16, .Little, 0, 2) == 0x5a4e); | 22 | try std.testing.expect(indexBufferT(&bytes, u16, .Little, 0, 2) == 0x5a4e); |
| 23 | try std.testing.expect(indexBufferT(&bytes, u16, .Little, 1, 2) == 0xa97d); | 23 | try std.testing.expect(indexBufferT(&bytes, u16, .Little, 1, 2) == 0xa97d); |
| 24 | } | 24 | } |
src/rawIntBytes.zig+1-1| ... | @@ -3,7 +3,7 @@ const string = []const u8; | ... | @@ -3,7 +3,7 @@ const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const rawInt = extras.rawInt; | 4 | const rawInt = extras.rawInt; |
| 5 | 5 | ||
| 6 | pub fn rawIntBytes(comptime T: type, comptime literal: comptime_int) [@bitSizeOf(T) / 8]u8 { | 6 | pub inline fn rawIntBytes(comptime T: type, comptime literal: comptime_int) [@bitSizeOf(T) / 8]u8 { |
| 7 | return comptime std.mem.toBytes(rawInt(T, literal))[0 .. @bitSizeOf(T) / 8].*; | 7 | return comptime std.mem.toBytes(rawInt(T, literal))[0 .. @bitSizeOf(T) / 8].*; |
| 8 | } | 8 | } |
| 9 | 9 |
src/readType.zig+4-4| ... | @@ -3,7 +3,7 @@ const string = []const u8; | ... | @@ -3,7 +3,7 @@ const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const builtin = @import("builtin"); | 4 | const builtin = @import("builtin"); |
| 5 | const native_endian = builtin.target.cpu.arch.endian(); | 5 | const native_endian = builtin.target.cpu.arch.endian(); |
| 6 | const rawInt = extras.rawInt; | 6 | const rawIntBytes = extras.rawIntBytes; |
| 7 | 7 | ||
| 8 | pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) !T { | 8 | pub fn readType(reader: anytype, comptime T: type, endian: std.builtin.Endian) !T { |
| 9 | if (T == u8) return reader.readByte(); // single bytes dont have an endianness | 9 | if (T == u8) return reader.readByte(); // single bytes dont have an endianness |
| ... | @@ -56,7 +56,7 @@ test { | ... | @@ -56,7 +56,7 @@ test { |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | test { | 58 | test { |
| 59 | const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); | 59 | const bytes = rawIntBytes(u32, 0x4e5a7da9); |
| 60 | var fba = std.io.fixedBufferStream(&bytes); | 60 | var fba = std.io.fixedBufferStream(&bytes); |
| 61 | const S = struct { | 61 | const S = struct { |
| 62 | a: u16, | 62 | a: u16, |
| ... | @@ -70,7 +70,7 @@ test { | ... | @@ -70,7 +70,7 @@ test { |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | test { | 72 | test { |
| 73 | const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); | 73 | const bytes = rawIntBytes(u32, 0x4e5a7da9); |
| 74 | var fba = std.io.fixedBufferStream(&bytes); | 74 | var fba = std.io.fixedBufferStream(&bytes); |
| 75 | const A = [2]u16; | 75 | const A = [2]u16; |
| 76 | const a = try readType(fba.reader(), A, .Big); | 76 | const a = try readType(fba.reader(), A, .Big); |
| ... | @@ -79,7 +79,7 @@ test { | ... | @@ -79,7 +79,7 @@ test { |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | test { | 81 | test { |
| 82 | const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); | 82 | const bytes = rawIntBytes(u32, 0x4e5a7da9); |
| 83 | var fba = std.io.fixedBufferStream(&bytes); | 83 | var fba = std.io.fixedBufferStream(&bytes); |
| 84 | const S = packed struct { | 84 | const S = packed struct { |
| 85 | a: u16, | 85 | a: u16, |
src/to_hex.zig+2-2| ... | @@ -1,7 +1,7 @@ | ... | @@ -1,7 +1,7 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const rawInt = extras.rawInt; | 4 | const rawIntBytes = extras.rawIntBytes; |
| 5 | 5 | ||
| 6 | pub fn to_hex(array: anytype) [array.len * 2]u8 { | 6 | pub fn to_hex(array: anytype) [array.len * 2]u8 { |
| 7 | var res: [array.len * 2]u8 = undefined; | 7 | var res: [array.len * 2]u8 = undefined; |
| ... | @@ -11,5 +11,5 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 { | ... | @@ -11,5 +11,5 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 { |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | test { | 13 | test { |
| 14 | try std.testing.expect(std.mem.eql(u8, &to_hex(std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132))), "4e5a7da9f3f1d132")); | 14 | try std.testing.expect(std.mem.eql(u8, &to_hex(rawIntBytes(u64, 0x4e5a7da9f3f1d132)), "4e5a7da9f3f1d132")); |
| 15 | } | 15 | } |