Last active
May 23, 2024 15:46
-
-
Save Khitiara/ce35b7ba82304a8d540cda822512454e to your computer and use it in GitHub Desktop.
zig std.debug.dump_hex proof of error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn main() !void { | |
const bytes: []const u8 = &.{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x01, 0x12, 0x13 }; | |
std.debug.print("std.debug on {*}: \n", .{bytes.ptr}); | |
std.debug.dump_hex(bytes); | |
std.debug.print("with fix on {*}: \n", .{bytes.ptr}); | |
try dump_hex_fix(bytes); | |
} | |
pub fn dump_hex_fix(bytes: []const u8) !void { | |
const stderr = std.io.getStdErr(); | |
const ttyconf = std.io.tty.detectConfig(stderr); | |
const writer = stderr.writer(); | |
var chunks = std.mem.window(u8, bytes, 16, 16); | |
while (chunks.next()) |window| { | |
// 1. Print the address. | |
const address = (@intFromPtr(bytes.ptr) + 0x10 * (try std.math.divCeil(usize, chunks.index orelse bytes.len, 16))) - 0x10; | |
try ttyconf.setColor(writer, .dim); | |
// We print the address in lowercase and the bytes in uppercase hexadecimal to distinguish them more. | |
// Also, make sure all lines are aligned by padding the address. | |
try writer.print("{x:0>[1]} ", .{ address, @sizeOf(usize) * 2 }); | |
try ttyconf.setColor(writer, .reset); | |
// 2. Print the bytes. | |
for (window, 0..) |byte, index| { | |
try writer.print("{X:0>2} ", .{byte}); | |
if (index == 7) try writer.writeByte(' '); | |
} | |
try writer.writeByte(' '); | |
if (window.len < 16) { | |
var missing_columns = (16 - window.len) * 3; | |
if (window.len < 8) missing_columns += 1; | |
try writer.writeByteNTimes(' ', missing_columns); | |
} | |
// 3. Print the characters. | |
for (window) |byte| { | |
if (std.ascii.isPrint(byte)) { | |
try writer.writeByte(byte); | |
} else { | |
// Related: https://github.com/ziglang/zig/issues/7600 | |
if (ttyconf == .windows_api) { | |
try writer.writeByte('.'); | |
continue; | |
} | |
// Let's print some common control codes as graphical Unicode symbols. | |
// We don't want to do this for all control codes because most control codes apart from | |
// the ones that Zig has escape sequences for are likely not very useful to print as symbols. | |
switch (byte) { | |
'\n' => try writer.writeAll("␊"), | |
'\r' => try writer.writeAll("␍"), | |
'\t' => try writer.writeAll("␉"), | |
else => try writer.writeByte('.'), | |
} | |
} | |
} | |
try writer.writeByte('\n'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std.debug on u8@d9f04c: | |
0000000000d9f04c 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........ | |
0000000000d9f03c 01 12 13 ... | |
with fix on u8@d9f04c: | |
0000000000d9f04c 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF .."3DUfw........ | |
0000000000d9f05c 01 12 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment