authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-29 00:40:15 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-29 00:40:15 -08:00
log72d70b47cc7314371eb65c106a06b01f70b3abde
tree2a8e33d85ce4c2ec80287a044cd2d57909a01f02
parent4461bc011659155d72810a9a8cd3b33129b0823d

add rawIntBytes


2 files changed, 25 insertions(+), 0 deletions(-)

src/lib.zig+1
......@@ -89,3 +89,4 @@ pub fn fd_realpath(fd: std.os.fd_t) ![std.fs.MAX_PATH_BYTES:0]u8 {
8989
9090pub usingnamespace @import("./rawInt.zig");
9191pub usingnamespace @import("./expectSimilarType.zig");
92pub usingnamespace @import("./rawIntBytes.zig");
src/rawIntBytes.zig created+24
......@@ -0,0 +1,24 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const rawInt = extras.rawInt;
5
6pub 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].*;
8}
9
10test {
11 try std.testing.expect(std.mem.eql(
12 u8,
13 &rawIntBytes(u128, 0x5d41402abc4b2a76b9719d911017c592),
14 &[_]u8{ 0x5d, 0x41, 0x40, 0x2a, 0xbc, 0x4b, 0x2a, 0x76, 0xb9, 0x71, 0x9d, 0x91, 0x10, 0x17, 0xc5, 0x92 },
15 ));
16}
17
18test {
19 try std.testing.expect(std.mem.eql(
20 u8,
21 &rawIntBytes(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d),
22 &[_]u8{ 0xaa, 0xf4, 0xc6, 0x1d, 0xdc, 0xc5, 0xe8, 0xa2, 0xda, 0xbe, 0xde, 0x0f, 0x3b, 0x48, 0x2c, 0xd9, 0xae, 0xa9, 0x43, 0x4d },
23 ));
24}