From 28b9cb4c40632aa087947c642c34dc714b6ad18a Mon Sep 17 00:00:00 2001 From: Peterino2 Date: Wed, 11 Nov 2026 19:17:12 -0700 Subject: [PATCH] xxd testing updates and colors --- engine/lib/lz4/src/test/xxd.zig | 111 +++++++++++++++++++-- engine/lib/p2/src/structures/xxd.zig | 139 +++++++++++++++++++-------- engine/lib/slugger/build.zig | 2 +- engine/lib/slugger/src/slugger.zig | 17 ++++ 4 files changed, 220 insertions(+), 49 deletions(-) diff --git a/engine/lib/lz4/src/test/xxd.zig b/engine/lib/lz4/src/test/xxd.zig index 41649f4..ddbe88e 100644 --- a/engine/lib/lz4/src/test/xxd.zig +++ b/engine/lib/lz4/src/test/xxd.zig @@ -15,22 +15,87 @@ pub const XxdOptions = struct { groupSplit: u32 = 4, // extra space every N groups; 0 disables it showAddress: bool = true, showAscii: bool = false, + color: bool = true, // if true uses an ascii color code }; -pub const Error = std.Io.Writer.Error || error{InvalidOptions}; +const Color = enum { + default, + black, + red, + green, + yellow, + blue, +}; + +const ColorOp = struct { + color: Color = .default, + count: u32 = 0, + + pub fn colorAscii(self: @This()) []const u8 { + switch (self.color) { + .default => return "\x1b[0m", + .black => return "\x1b[0;30m", + .red => return "\x1b[0;31m", + .green => return "\x1b[0;32m", + .yellow => return "\x1b[0;33m", + .blue => return "\x1b[0;34m", + } + } +}; + +pub fn getColorFromByte(byte: u8) Color { + return switch (byte) { + 0x0 => return .red, + 0x20...0x7E => return .blue, + else => .default, + }; +} + +pub fn cursorProceed(opCursor: *usize, opCount: *usize, ops: []const ColorOp) void { + opCount.* -= 1; + if (opCount.* == 0) { + opCursor.* += 1; + opCount.* = ops[opCursor.*].count; + } +} /// Writes a hex dump. Does not allocate or flush the caller's writer. -pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) Error!void { +pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) !void { if (options.wordSize == 0 or options.wordSize % 8 != 0 or options.wordsPerGroup == 0 or options.groupsPerLine == 0) return error.InvalidOptions; const bytes_per_word: usize = options.wordSize / 8; const words_per_line = std.math.mul(usize, options.wordsPerGroup, options.groupsPerLine) catch return error.InvalidOptions; const bytes_per_line = std.math.mul(usize, bytes_per_word, words_per_line) catch return error.InvalidOptions; + var ops: [256]ColorOp = undefined; + var offset: usize = 0; while (offset < bytes.len) { const row = bytes[offset..][0..@min(bytes_per_line, bytes.len - offset)]; + // read ahead for the rest of the length + var cra: usize = 0; // color read-ahead + var opscount: usize = 0; + var active = &ops[opscount]; + active.* = .{}; + while (cra < row.len) : (cra += 1) { + const color = getColorFromByte(row[cra]); + if (color == active.color) { + active.count += 1; + } else { + if (active.count != 0) + opscount += 1; + active = &ops[opscount]; + active.color = color; + active.count = 1; + } + } + opscount += 1; + if (options.showAddress) try writer.print("{x:0>8}: ", .{offset}); const word_count = if (options.showAscii) words_per_line else (row.len - 1) / bytes_per_word + 1; + + var opCursor: usize = 0; + var opCount: usize = ops[opCursor].count; + for (0..word_count) |word_index| { if (word_index > 0 and word_index % options.wordsPerGroup == 0) { try writer.writeByte(' '); @@ -43,31 +108,61 @@ pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) const word = row[start..][0..@min(bytes_per_word, row.len - start)]; for (0..word.len) |i| { const index = if (options.endian == .little) i else word.len - 1 - i; - try writeByteAsHex(word[index], writer); + if (opCursor >= opscount) return { + return error.WhatTheFucl; + }; + try writeByteAsHex(word[index], writer, options, ops[opCursor]); + cursorProceed(&opCursor, &opCount, &ops); } if (options.showAscii) { // Pad missing bytes so every row's ASCII starts in the same column. for (word.len..bytes_per_word) |_| try writer.writeAll(" "); } } + if (options.showAscii) { + opCursor = 0; + opCount = ops[opCursor].count; + + try writer.writeAll("\x1b[0m"); try writer.writeAll(" | "); - for (row) |byte| try writer.writeByte(if (byte >= 0x20 and byte <= 0x7e) byte else '.'); + for (row) |byte| { + try writer.writeAll(ops[opCursor].colorAscii()); + try writer.writeByte(if (byte >= 0x20 and byte <= 0x7e) byte else getUnprintableByte(byte)); + cursorProceed(&opCursor, &opCount, &ops); + } + try writer.writeAll("\x1b[0m"); + try writer.writeAll(" | "); + + // for (ops[0..opscount]) |op| { + // try writer.print("{s}{d}", .{ op.colorAscii(), op.count }); + // try writer.writeAll("\x1b[0m"); + // } } + try writer.writeByte('\n'); offset += row.len; } } -/// Hex plus printable ASCII, with '.' for control and non-ASCII bytes. -/// For separate byte columns, set wordsPerGroup = 1 and groupsPerLine = 16. -pub fn xxdWriteAscii(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) Error!void { +fn getUnprintableByte(byte: u8) u8 { + return switch (byte) { + 0 => return '~', + else => return '.', + }; +} + +pub fn xxdWriteAscii(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) !void { var ascii_options = options; ascii_options.showAscii = true; try xxdWrite(writer, bytes, ascii_options); } -inline fn writeByteAsHex(byte: u8, writer: *std.Io.Writer) std.Io.Writer.Error!void { +inline fn writeByteAsHex(byte: u8, writer: *std.Io.Writer, options: XxdOptions, op: ColorOp) std.Io.Writer.Error!void { const digits = "0123456789ABCDEF"; + + if (options.color) + try writer.writeAll(op.colorAscii()); + try writer.writeAll(&.{ digits[byte >> 4], digits[byte & 0xf] }); } diff --git a/engine/lib/p2/src/structures/xxd.zig b/engine/lib/p2/src/structures/xxd.zig index f128403..de79ca1 100644 --- a/engine/lib/p2/src/structures/xxd.zig +++ b/engine/lib/p2/src/structures/xxd.zig @@ -15,22 +15,88 @@ pub const XxdOptions = struct { groupSplit: u32 = 4, // extra space every N groups; 0 disables it showAddress: bool = true, showAscii: bool = false, + color: bool = true, // if true uses an ascii color code }; -pub const Error = std.Io.Writer.Error || error{InvalidOptions}; +const Color = enum { + default, + black, + red, + green, + yellow, + blue, +}; + +const ColorOp = struct { + color: Color = .default, + count: u32 = 0, + + pub fn colorAscii(self: @This()) []const u8 { + switch (self.color) { + .default => return "\x1b[0m", + .black => return "\x1b[0;30m", + .red => return "\x1b[0;31m", + .green => return "\x1b[0;32m", + .yellow => return "\x1b[0;33m", + .blue => return "\x1b[0;34m", + } + } +}; + +pub fn getColorFromByte(byte: u8) Color { + return switch (byte) { + 0x0 => return .red, + 0x20...0x7E => return .blue, + else => .default, + }; +} + +pub fn cursorProceed(opCursor: *usize, opCount: *usize, ops: []const ColorOp) void { + opCount.* -= 1; + if (opCount.* == 0) { + opCursor.* += 1; + opCount.* = ops[opCursor.*].count; + } +} /// Writes a hex dump. Does not allocate or flush the caller's writer. -pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) Error!void { +pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) !void { if (options.wordSize == 0 or options.wordSize % 8 != 0 or options.wordsPerGroup == 0 or options.groupsPerLine == 0) return error.InvalidOptions; const bytes_per_word: usize = options.wordSize / 8; const words_per_line = std.math.mul(usize, options.wordsPerGroup, options.groupsPerLine) catch return error.InvalidOptions; const bytes_per_line = std.math.mul(usize, bytes_per_word, words_per_line) catch return error.InvalidOptions; + var ops: [256]ColorOp = undefined; + var offset: usize = 0; while (offset < bytes.len) { const row = bytes[offset..][0..@min(bytes_per_line, bytes.len - offset)]; + + // read ahead for the rest of the length + var cra: usize = 0; // color read-ahead + var opscount: usize = 0; + var active = &ops[opscount]; + active.* = .{}; + while (cra < row.len) : (cra += 1) { + const color = getColorFromByte(row[cra]); + if (color == active.color) { + active.count += 1; + } else { + if (active.count != 0) + opscount += 1; + active = &ops[opscount]; + active.color = color; + active.count = 1; + } + } + opscount += 1; + if (options.showAddress) try writer.print("{x:0>8}: ", .{offset}); const word_count = if (options.showAscii) words_per_line else (row.len - 1) / bytes_per_word + 1; + + var opCursor: usize = 0; + var opCount: usize = ops[opCursor].count; + for (0..word_count) |word_index| { if (word_index > 0 and word_index % options.wordsPerGroup == 0) { try writer.writeByte(' '); @@ -43,63 +109,56 @@ pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) const word = row[start..][0..@min(bytes_per_word, row.len - start)]; for (0..word.len) |i| { const index = if (options.endian == .little) i else word.len - 1 - i; - try writeByteAsHex(word[index], writer); + if (opCursor >= opscount) return { + return error.WhatTheFucl; + }; + try writeByteAsHex(word[index], writer, options, ops[opCursor]); + cursorProceed(&opCursor, &opCount, &ops); } if (options.showAscii) { // Pad missing bytes so every row's ASCII starts in the same column. for (word.len..bytes_per_word) |_| try writer.writeAll(" "); } } + if (options.showAscii) { + opCursor = 0; + opCount = ops[opCursor].count; + + try writer.writeAll("\x1b[0m"); + try writer.writeAll(" | "); + for (row) |byte| { + try writer.writeAll(ops[opCursor].colorAscii()); + try writer.writeByte(if (byte >= 0x20 and byte <= 0x7e) byte else getUnprintableByte(byte)); + cursorProceed(&opCursor, &opCount, &ops); + } + try writer.writeAll("\x1b[0m"); try writer.writeAll(" | "); - for (row) |byte| try writer.writeByte(if (byte >= 0x20 and byte <= 0x7e) byte else '.'); } + try writer.writeByte('\n'); offset += row.len; } } -/// Hex plus printable ASCII, with '.' for control and non-ASCII bytes. -/// For separate byte columns, set wordsPerGroup = 1 and groupsPerLine = 16. -pub fn xxdWriteAscii(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) Error!void { +fn getUnprintableByte(byte: u8) u8 { + return switch (byte) { + 0 => return '~', + else => return '.', + }; +} + +pub fn xxdWriteAscii(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions) !void { var ascii_options = options; ascii_options.showAscii = true; try xxdWrite(writer, bytes, ascii_options); } -inline fn writeByteAsHex(byte: u8, writer: *std.Io.Writer) std.Io.Writer.Error!void { +inline fn writeByteAsHex(byte: u8, writer: *std.Io.Writer, options: XxdOptions, op: ColorOp) std.Io.Writer.Error!void { const digits = "0123456789ABCDEF"; + + if (options.color) + try writer.writeAll(op.colorAscii()); + try writer.writeAll(&.{ digits[byte >> 4], digits[byte & 0xf] }); } - -test "hex and ASCII dump layout" { - var output: std.Io.Writer.Allocating = .init(std.testing.allocator); - defer output.deinit(); - const options: XxdOptions = .{ .wordsPerGroup = 1, .groupsPerLine = 4, .groupSplit = 2 }; - try xxdWriteAscii(&output.writer, "AB\x00\t~\x7f\x80!x", options); - try std.testing.expectEqualStrings( - "00000000: 41 42 00 09 | AB..\n" ++ - "00000004: 7E 7F 80 21 | ~..!\n" ++ - "00000008: 78" ++ " " ++ " | x\n", - output.written(), - ); - output.writer.end = 0; - try xxdWriteAscii(&output.writer, "ABC", .{ .wordSize = 16, .wordsPerGroup = 1, .groupsPerLine = 2, .groupSplit = 0, .endian = .big, .showAddress = false }); - try std.testing.expectEqualStrings("4241 43 | ABC\n", output.written()); - output.writer.end = 0; - try xxdWrite(&output.writer, "ABCDE", .{ .wordSize = 16, .wordsPerGroup = 1, .groupsPerLine = 2, .groupSplit = 0, .showAddress = false }); - try std.testing.expectEqualStrings("4142 4344\n45\n", output.written()); -} - -test "hex dump empty input options and writer errors" { - var output: std.Io.Writer.Allocating = .init(std.testing.allocator); - defer output.deinit(); - try xxdWriteAscii(&output.writer, "", .{}); - try std.testing.expectEqual(@as(usize, 0), output.written().len); - for ([_]XxdOptions{ .{ .wordSize = 0 }, .{ .wordSize = 7 }, .{ .wordsPerGroup = 0 }, .{ .groupsPerLine = 0 } }) |options| { - try std.testing.expectError(error.InvalidOptions, xxdWrite(&output.writer, "x", options)); - } - var buffer: [1]u8 = undefined; - var fixed: std.Io.Writer = .fixed(&buffer); - try std.testing.expectError(error.WriteFailed, xxdWriteAscii(&fixed, "x", .{})); -} diff --git a/engine/lib/slugger/build.zig b/engine/lib/slugger/build.zig index 5d6f20d..fd92e64 100644 --- a/engine/lib/slugger/build.zig +++ b/engine/lib/slugger/build.zig @@ -6,7 +6,6 @@ pub fn build(b: *std.Build) void { const p2 = b.dependency("p2", .{ .target = target, .optimize = optimize }); const lz4 = b.dependency("lz4", .{ .target = target, .optimize = optimize }); - _ = lz4; const mod = b.addModule("slugger", .{ .root_source_file = b.path("src/slugger.zig"), @@ -14,6 +13,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .imports = &.{.{ .name = "p2", .module = p2.module("p2") }}, }); + mod.addImport("lz4", lz4.module("lz4_zig")); const tests = b.addTest(.{ .root_module = mod, diff --git a/engine/lib/slugger/src/slugger.zig b/engine/lib/slugger/src/slugger.zig index c844992..a2b16f2 100644 --- a/engine/lib/slugger/src/slugger.zig +++ b/engine/lib/slugger/src/slugger.zig @@ -1130,9 +1130,26 @@ test "testing" { try std.testing.expectEqualStrings(expected.written(), actual.written()); } } + + { + const io = std.testing.io; + const file = try std.Io.Dir.cwd().createFile(io, "output.bin", .{}); + defer file.close(io); + + var buffer: [4096]u8 = undefined; + var file_writer = file.writer(io, &buffer); + + const w = &file_writer.interface; + var compressedBuffer: [4096]u8 = undefined; + var compressedWriter = try lz4.Lz4Writer.init(std.testing.allocator, w, .{ .buffer = &compressedBuffer }); + defer compressedWriter.deinit(); + try sluggerformat.write(std.testing.allocator, &gLoggerSystem.mem, &compressedWriter.writer); + try w.flush(); + } } pub const sluggerformat = @import("sluggerformat.zig"); const p2 = @import("p2"); const std = @import("std"); const builtin = @import("builtin"); +const lz4 = @import("lz4");