authorgravatar for 10470872+fifty-six@users.noreply.github.comYusuf Bham <10470872+fifty-six@users.noreply.github.com> 2022-04-17 06:15:15-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-04-17 06:15:15-04:00
log2aeaa1cfc4c563a24525fb27fd2783bfd5da808a
treec841f26576c3294863a9385bbe35270e375bcc14
parent7b090df6689909ba82ff46be4d21733f84f32c33
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.os.uefi: fix GUID formatting (#11452)


1 files changed, 24 insertions(+), 7 deletions(-)

lib/std/os/uefi.zig+24-7
...@@ -53,13 +53,19 @@ pub const Guid = extern struct {...@@ -53,13 +53,19 @@ pub const Guid = extern struct {
53 ) !void {53 ) !void {
54 _ = options;54 _ = options;
55 if (f.len == 0) {55 if (f.len == 0) {
56 return std.fmt.format(writer, "{x:0>8}-{x:0>4}-{x:0>4}-{x:0>2}{x:0>2}-{x:0>12}", .{56 const fmt = std.fmt.fmtSliceHexLower;
57 self.time_low,57
58 self.time_mid,58 const time_low = @byteSwap(u32, self.time_low);
59 self.time_high_and_version,59 const time_mid = @byteSwap(u16, self.time_mid);
60 self.clock_seq_high_and_reserved,60 const time_high_and_version = @byteSwap(u16, self.time_high_and_version);
61 self.clock_seq_low,61
62 self.node,62 return std.fmt.format(writer, "{:0>8}-{:0>4}-{:0>4}-{:0>2}{:0>2}-{:0>12}", .{
63 fmt(std.mem.asBytes(&time_low)),
64 fmt(std.mem.asBytes(&time_mid)),
65 fmt(std.mem.asBytes(&time_high_and_version)),
66 fmt(std.mem.asBytes(&self.clock_seq_high_and_reserved)),
67 fmt(std.mem.asBytes(&self.clock_seq_low)),
68 fmt(std.mem.asBytes(&self.node)),
63 });69 });
64 } else {70 } else {
65 @compileError("Unknown format character: '" ++ f ++ "'");71 @compileError("Unknown format character: '" ++ f ++ "'");
...@@ -133,3 +139,14 @@ pub const TimeCapabilities = extern struct {...@@ -133,3 +139,14 @@ pub const TimeCapabilities = extern struct {
133139
134/// File Handle as specified in the EFI Shell Spec140/// File Handle as specified in the EFI Shell Spec
135pub const FileHandle = *opaque {};141pub const FileHandle = *opaque {};
142
143test "GUID formatting" {
144 var bytes = [_]u8{ 137, 60, 203, 50, 128, 128, 124, 66, 186, 19, 80, 73, 135, 59, 194, 135 };
145
146 var guid = @bitCast(Guid, bytes);
147
148 var str = try std.fmt.allocPrint(std.testing.allocator, "{}", .{guid});
149 defer std.testing.allocator.free(str);
150
151 try std.testing.expect(std.mem.eql(u8, str, "32cb3c89-8080-427c-ba13-5049873bc287"));
152}