Skip to content

Commit

Permalink
Add a naive hexdump() implementation
Browse files Browse the repository at this point in the history
I really like the `hd` format, so this copies that except without the
addressing since we shouldn't really be dealing with large buffers.

Helpful for debugging control characters, because lua's string matching does
not do the ^D => \x04 translation that we do in write().

Signed-off-by: Kyle Evans <[email protected]>
  • Loading branch information
kevans91 committed Jan 27, 2024
1 parent eead76b commit ca692e3
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/orch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,57 @@ function orch.env.fail(func)
return true
end

function orch.env.hexdump(str)
if ctx() == CTX_QUEUE then
error("hexdump may only be called in a non-queue context")
end

local output = ""

local function append(left, right)
if output ~= "" then
output = output .. "\n"
end

left = string.format("%-50s", left)
output = output .. "DEBUG: " .. left .. "\t|" .. right .. "|"
end

local lcol, rcol = "", ""
for c = 1, #str do
if (c - 1) % 16 == 0 then
-- Flush output every 16th character
if c ~= 1 then
append(lcol, rcol)
lcol = ""
rcol = ""
end
else
if (c - 1) % 8 == 0 then
lcol = lcol .. " "
else
lcol = lcol .. " "
end
end

local ch = str:sub(c, c)
local byte = string.byte(ch)
lcol = lcol .. string.format("%.02x", byte)
if byte >= 0x20 and byte < 0x7f then
rcol = rcol .. ch
else
rcol = rcol .. "."
end
end

if lcol ~= "" then
append(lcol, rcol)
end

io.stderr:write(output .. "\n")
return true
end

function orch.env.match(pattern)
local match_action = MatchAction:new("match")
match_action.pattern = pattern
Expand Down

0 comments on commit ca692e3

Please sign in to comment.