authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-20 01:05:59-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-20 01:06:29-07:00
log397c9174cb8683ae894beafc97eac23e8aa5a760
tree3edc22dff603dedf121780e70e6382a298758fb0
parenteb4028bf30bca7036c6bdc65feb321b163e84ecd

fix std.fmt.hex


1 files changed, 3 insertions(+), 9 deletions(-)

lib/std/fmt.zig+3-9
...@@ -2722,7 +2722,7 @@ test "recursive format function" {...@@ -2722,7 +2722,7 @@ test "recursive format function" {
2722pub const hex_charset = "0123456789abcdef";2722pub const hex_charset = "0123456789abcdef";
27232723
2724/// Converts an unsigned integer of any multiple of u8 to an array of lowercase2724/// Converts an unsigned integer of any multiple of u8 to an array of lowercase
2725/// hex bytes.2725/// hex bytes, little endian.
2726pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 {2726pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 {
2727 comptime assert(@typeInfo(@TypeOf(x)).Int.signedness == .unsigned);2727 comptime assert(@typeInfo(@TypeOf(x)).Int.signedness == .unsigned);
2728 var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined;2728 var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined;
...@@ -2739,17 +2739,11 @@ test hex {...@@ -2739,17 +2739,11 @@ test hex {
2739 {2739 {
2740 const x = hex(@as(u32, 0xdeadbeef));2740 const x = hex(@as(u32, 0xdeadbeef));
2741 try std.testing.expect(x.len == 8);2741 try std.testing.expect(x.len == 8);
2742 switch (builtin.cpu.arch.endian()) {2742 try std.testing.expectEqualStrings("efbeadde", &x);
2743 .little => try std.testing.expectEqualStrings("efbeadde", &x),
2744 .big => try std.testing.expectEqualStrings("deadbeef", &x),
2745 }
2746 }2743 }
2747 {2744 {
2748 const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]";2745 const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]";
2749 try std.testing.expect(s.len == 18);2746 try std.testing.expect(s.len == 18);
2750 switch (builtin.cpu.arch.endian()) {2747 try std.testing.expectEqualStrings("[00efcdab78563412]", s);
2751 .little => try std.testing.expectEqualStrings("[00efcdab78563412]", s),
2752 .big => try std.testing.expectEqualStrings("[12345678abcdef00]", s),
2753 }
2754 }2748 }
2755}2749}