Playground
gnomod.toml
module = "gno.land/p/nym-moultest123/x/daily/hexdump/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186405 max_deposit = "5000000ugnot"
hexdump.gno
// Package hexdump renders bytes in the classic xxd/hexdump -C layout, as a
// pure, reusable package.
//
// The format is the familiar one: an 8-digit hex offset, then 16 bytes as hex
// in two 8-byte groups separated by an extra space, then the same bytes as
// ASCII between pipes with non-printables shown as ".". Short final lines are
// padded so the ASCII column stays aligned — the whole point of the layout.
//
// Useful on chain for exactly the reason it is useful off it: when a value is
// not what you expected, the bytes tell you why. Encoding bugs, stray NULs,
// UTF-8 that is not what it claims, trailing whitespace — all invisible in a
// rendered string and obvious in a dump.
//
// A live demo of this package is at
// [r/moul/x/daily/hexdumpdemo](/r/nym-moultest123/x/daily/hexdumpdemo/v0).
package hexdump
import "strings"
// MaxBytes bounds a dump so gas stays predictable. Input beyond this is
// truncated and reported by Dump's second return value.
const MaxBytes = 4096
// BytesPerLine is the classic 16.
const BytesPerLine = 16
const hexDigits = "0123456789abcdef"
// Dump renders b in the xxd -C layout. It returns the dump and the number of
// bytes rendered, which is less than len(b) when the input exceeds MaxBytes.
func Dump(b []byte) (string, int) {
n := len(b)
if n > MaxBytes {
n = MaxBytes
}
if n == 0 {
return "", 0
}
var sb strings.Builder
for off := 0; off < n; off += BytesPerLine {
end := off + BytesPerLine
if end > n {
end = n
}
sb.WriteString(Line(off, b[off:end]))
sb.WriteString("\n")
}
return sb.String(), n
}
// DumpString is Dump over a string's bytes.
func DumpString(s string) (string, int) { return Dump([]byte(s)) }
// Line renders one line: offset, hex columns, ASCII gutter. chunk must hold at
// most BytesPerLine bytes; a shorter chunk is padded so columns stay aligned.
func Line(offset int, chunk []byte) string {
var sb strings.Builder
sb.WriteString(Offset(offset))
sb.WriteString(" ")
for i := 0; i < BytesPerLine; i++ {
if i == BytesPerLine/2 {
sb.WriteString(" ") // the classic gap between the two 8-byte groups
}
if i < len(chunk) {
sb.WriteString(Hex(chunk[i]))
} else {
sb.WriteString(" ") // pad so the ASCII gutter never shifts
}
sb.WriteString(" ")
}
sb.WriteString(" |")
sb.WriteString(ASCII(chunk))
sb.WriteString("|")
return sb.String()
}
// Offset formats an 8-digit lowercase hex offset.
func Offset(n int) string {
if n < 0 {
n = 0
}
out := make([]byte, 8)
for i := 7; i >= 0; i-- {
out[i] = hexDigits[n&0xf]
n >>= 4
}
return string(out)
}
// Hex formats one byte as two lowercase hex digits.
func Hex(b byte) string {
return string([]byte{hexDigits[b>>4], hexDigits[b&0xf]})
}
// ASCII renders the printable-ASCII view of chunk: bytes outside 0x20..0x7e
// become ".". It is NOT padded — Line handles alignment.
func ASCII(chunk []byte) string {
out := make([]byte, len(chunk))
for i, c := range chunk {
if c >= 0x20 && c <= 0x7e {
out[i] = c
} else {
out[i] = '.'
}
}
return string(out)
}
// Printable reports whether c renders as itself rather than as ".".
func Printable(c byte) bool { return c >= 0x20 && c <= 0x7e }
hexdump_test.gno
package hexdump
import (
"strings"
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestOffset(t *testing.T) {
cases := []struct {
in int
want string
}{
{0, "00000000"},
{1, "00000001"},
{15, "0000000f"},
{16, "00000010"},
{255, "000000ff"},
{4096, "00001000"},
{-5, "00000000"}, // clamped
}
for _, c := range cases {
uassert.Equal(t, c.want, Offset(c.in))
}
}
func TestHex(t *testing.T) {
uassert.Equal(t, "00", Hex(0))
uassert.Equal(t, "0f", Hex(15))
uassert.Equal(t, "10", Hex(16))
uassert.Equal(t, "ff", Hex(255))
uassert.Equal(t, "41", Hex('A'))
}
func TestPrintable(t *testing.T) {
uassert.False(t, Printable(0x00), "NUL is not printable")
uassert.False(t, Printable(0x1f), "0x1f is below the printable range")
uassert.True(t, Printable(0x20), "space is printable")
uassert.True(t, Printable('A'))
uassert.True(t, Printable(0x7e), "~ is the last printable")
uassert.False(t, Printable(0x7f), "DEL is not printable")
uassert.False(t, Printable(0xff))
}
func TestASCII(t *testing.T) {
uassert.Equal(t, "ABC", ASCII([]byte("ABC")))
uassert.Equal(t, "...", ASCII([]byte{0x00, 0x01, 0x7f}))
uassert.Equal(t, "a.b", ASCII([]byte{'a', 0x00, 'b'}))
uassert.Equal(t, "", ASCII(nil))
}
// TestLineFullWidth pins the exact classic layout, including the extra space
// between the two 8-byte groups.
func TestLineFullWidth(t *testing.T) {
uassert.Equal(t,
"00000000 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|",
Line(0, []byte("0123456789abcdef")))
}
// TestLineShortChunkStaysAligned is the reason the padding exists: a partial
// final line must not shift the ASCII gutter.
func TestLineShortChunkStaysAligned(t *testing.T) {
full := Line(0, []byte("0123456789abcdef"))
short := Line(16, []byte("hi"))
uassert.Equal(t,
"00000010 68 69 |hi|",
short)
uassert.Equal(t, strings.Index(full, "|"), strings.Index(short, "|"),
"the ASCII gutter must start at the same column on a short line")
}
func TestLineEmptyChunk(t *testing.T) {
// All padding, empty gutter — still aligned.
l := Line(0, nil)
uassert.True(t, strings.HasSuffix(l, "||"), "empty chunk renders an empty gutter")
uassert.Equal(t, strings.Index(Line(0, []byte("x")), "|"), strings.Index(l, "|"))
}
func TestDumpMultiLine(t *testing.T) {
out, n := DumpString("0123456789abcdefhi")
uassert.Equal(t, 18, n)
uassert.Equal(t,
"00000000 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|\n"+
"00000010 68 69 |hi|\n",
out)
}
func TestDumpEmpty(t *testing.T) {
out, n := Dump(nil)
uassert.Equal(t, "", out)
uassert.Equal(t, 0, n)
}
func TestDumpNonPrintable(t *testing.T) {
out, n := Dump([]byte{0x00, 0x0a, 0x41})
uassert.Equal(t, 3, n)
uassert.Equal(t, "00000000 00 0a 41 |..A|\n", out)
}
// TestDumpTruncates pins the bound and the honest byte count.
func TestDumpTruncates(t *testing.T) {
_, n := DumpString(strings.Repeat("x", MaxBytes+100))
uassert.Equal(t, MaxBytes, n, "input beyond MaxBytes is truncated")
_, n = DumpString(strings.Repeat("x", MaxBytes))
uassert.Equal(t, MaxBytes, n, "exactly MaxBytes is not truncated")
}
func TestDumpLineCount(t *testing.T) {
out, _ := DumpString(strings.Repeat("x", 33))
uassert.Equal(t, 3, strings.Count(out, "\n"), "33 bytes is 3 lines (16+16+1)")
}
// TestDumpRevealsInvisibleDifferences is the practical point of the package.
func TestDumpRevealsInvisibleDifferences(t *testing.T) {
a, _ := DumpString("hi")
b, _ := DumpString("hi ")
uassert.False(t, a == b, "a trailing space is invisible rendered, obvious in a dump")
// "é" is two bytes, not one.
out, n := DumpString("é")
uassert.Equal(t, 2, n)
uassert.True(t, strings.Contains(out, "c3 a9"), "UTF-8 shows its real bytes")
}