From 72d70b47cc7314371eb65c106a06b01f70b3abde Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 29 Jan 2024 00:40:15 -0800 Subject: [PATCH] add rawIntBytes --- src/lib.zig | 1 + src/rawIntBytes.zig | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/rawIntBytes.zig diff --git a/src/lib.zig b/src/lib.zig index a268bc277ba4242f35008b74382ab647097c5ed9..1e5712ec37d19e7016d8e8b563d3fd6b236ccddd 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -89,3 +89,4 @@ pub fn fd_realpath(fd: std.os.fd_t) ![std.fs.MAX_PATH_BYTES:0]u8 { pub usingnamespace @import("./rawInt.zig"); pub usingnamespace @import("./expectSimilarType.zig"); +pub usingnamespace @import("./rawIntBytes.zig"); diff --git a/src/rawIntBytes.zig b/src/rawIntBytes.zig new file mode 100644 index 0000000000000000000000000000000000000000..2ec75ae4fb2cfe14603fadc61018a094a7be471f --- /dev/null +++ b/src/rawIntBytes.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const string = []const u8; +const extras = @import("./lib.zig"); +const rawInt = extras.rawInt; + +pub fn rawIntBytes(comptime T: type, comptime literal: comptime_int) [@bitSizeOf(T) / 8]u8 { + return comptime std.mem.toBytes(rawInt(T, literal))[0 .. @bitSizeOf(T) / 8].*; +} + +test { + try std.testing.expect(std.mem.eql( + u8, + &rawIntBytes(u128, 0x5d41402abc4b2a76b9719d911017c592), + &[_]u8{ 0x5d, 0x41, 0x40, 0x2a, 0xbc, 0x4b, 0x2a, 0x76, 0xb9, 0x71, 0x9d, 0x91, 0x10, 0x17, 0xc5, 0x92 }, + )); +} + +test { + try std.testing.expect(std.mem.eql( + u8, + &rawIntBytes(u160, 0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d), + &[_]u8{ 0xaa, 0xf4, 0xc6, 0x1d, 0xdc, 0xc5, 0xe8, 0xa2, 0xda, 0xbe, 0xde, 0x0f, 0x3b, 0x48, 0x2c, 0xd9, 0xae, 0xa9, 0x43, 0x4d }, + )); +} -- 2.54.0