Skip to content

Instantly share code, notes, and snippets.

@travisstaloch
Created June 13, 2022 04:00
Show Gist options
  • Select an option

  • Save travisstaloch/1dca55ced45f463d389a9e61aab9ea01 to your computer and use it in GitHub Desktop.

Select an option

Save travisstaloch/1dca55ced45f463d389a9e61aab9ea01 to your computer and use it in GitHub Desktop.
translates garbled @compilelog output into a readable form
//! this script translates garbled @compileLog output into a readable form.
//! reads from stdin so you may need to redirect stderr to stdin like this:
//! $ zig run file.zig |& zig run print_compile_log.zig
//!
//! works by splitting lines on "u8{" delimiter and then skipping `delim_prefixes`
//! in `skipDelimPrefix()`. this makes things work when the type of the garbled string
//! is either `[]const u8{...}` or `*[N:0]u8{...}`
//!
const std = @import("std");
fn skipDelimPrefix(split: *[]const u8) void {
// demim_prefixes: start and end. end can be empty string
// these are modifiers found before "u8{" and will be removed from output
const delim_prefixes = .{
.{ "[]const ", "" }, // []const u8{97...}
.{ "*[", ":0]" }, // *[N:0]u8{97...}
};
inline for (delim_prefixes) |prefix_start_end| {
const start = prefix_start_end[0];
const end = prefix_start_end[1];
if (end.len == 0) {
if (std.mem.indexOf(u8, split.*, start)) |startidx| {
split.* = split.*[0..startidx];
}
} else {
if (std.mem.indexOf(u8, split.*, start)) |startidx| {
const endidx = std.mem.indexOf(u8, split.*[startidx..], end) orelse 0;
split.* = split.*[0..startidx][endidx..];
}
}
}
}
pub fn main() !void {
const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
const allr = std.heap.ArenaAllocator.init(std.heap.page_allocator).allocator();
const delim = "u8{";
while (try stdin.readUntilDelimiterOrEofAlloc(allr, '\n', std.math.maxInt(u16))) |_line| {
if (_line.len == 0) continue;
var delimit = std.mem.split(u8, _line, delim);
var firstsplit = delimit.next() orelse continue;
skipDelimPrefix(&firstsplit);
try stdout.print("{s}", .{firstsplit});
while (delimit.next()) |split| {
const end = std.mem.indexOfScalar(u8, split, '}') orelse {
try stdout.print("{s}\n", .{split});
continue;
};
const content = split[0..end];
switch (content[0]) {
'0'...'9' => {
var iter = std.mem.split(u8, content, ",");
while (iter.next()) |it2| {
if (it2.len == 0) continue;
const c = std.fmt.parseInt(u8, it2, 10) catch {
try stdout.print("{s}", .{it2});
continue;
};
try stdout.print("{c}", .{c});
}
},
else => {
try stdout.print("{s}\n", .{split});
},
}
var splitend = split[end + 1 ..];
skipDelimPrefix(&splitend);
try stdout.print(", {s}", .{splitend});
}
_ = try stdout.write("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment