fix std.fmt.hex

This commit is contained in:
Andrew Kelley 2024-07-20 01:05:59 -07:00
parent eb4028bf30
commit 397c9174cb

View file

@ -2722,7 +2722,7 @@ test "recursive format function" {
pub const hex_charset = "0123456789abcdef"; pub const hex_charset = "0123456789abcdef";
/// Converts an unsigned integer of any multiple of u8 to an array of lowercase /// Converts an unsigned integer of any multiple of u8 to an array of lowercase
/// hex bytes. /// hex bytes, little endian.
pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 { pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 {
comptime assert(@typeInfo(@TypeOf(x)).Int.signedness == .unsigned); comptime assert(@typeInfo(@TypeOf(x)).Int.signedness == .unsigned);
var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined; var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined;
@ -2739,17 +2739,11 @@ test hex {
{ {
const x = hex(@as(u32, 0xdeadbeef)); const x = hex(@as(u32, 0xdeadbeef));
try std.testing.expect(x.len == 8); try std.testing.expect(x.len == 8);
switch (builtin.cpu.arch.endian()) { try std.testing.expectEqualStrings("efbeadde", &x);
.little => try std.testing.expectEqualStrings("efbeadde", &x),
.big => try std.testing.expectEqualStrings("deadbeef", &x),
}
} }
{ {
const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]"; const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]";
try std.testing.expect(s.len == 18); try std.testing.expect(s.len == 18);
switch (builtin.cpu.arch.endian()) { try std.testing.expectEqualStrings("[00efcdab78563412]", s);
.little => try std.testing.expectEqualStrings("[00efcdab78563412]", s),
.big => try std.testing.expectEqualStrings("[12345678abcdef00]", s),
}
} }
} }