From 7bebc8c5eb201ce514851a04bae457729c9d089d Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 15 Oct 2025 17:18:00 -0400 Subject: [PATCH] internal/testutil: Add functions to dump printable chars --- internal/testutil/testutil.go | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index 0e3cf62..a6670c8 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -97,6 +97,32 @@ func (sn Snake) String() string { return out.String() } +func (sn Snake) CharsString() string { + if len(sn) == 0 || len(sn[0]) == 0 || len(sn[0][0]) == 0 { + return "EMPTY" + } + + out := strings.Builder { } + for index, sector := range sn { + if index > 0 { out.WriteString(" : ") } + out.WriteRune('[') + for index, variation := range sector { + if index > 0 { out.WriteString(" / ") } + for _, byt := range variation { + run := rune(byt) + if unicode.IsPrint(run) && run < 0x7F { + out.WriteRune(run) + } else { + out.WriteRune('.') + } + out.WriteRune(' ') + } + } + out.WriteRune(']') + } + return out.String() +} + // HexBytes formats bytes into a hexadecimal string. func HexBytes(data []byte) string { if len(data) == 0 { return "EMPTY" } @@ -107,6 +133,24 @@ func HexBytes(data []byte) string { return out.String() } +// HexChars returns all printable bytes in the string, with non-printable ones +// replaced with a dot. Each character has an extra space after it for placing +// underneath the result of HexBytes. +func HexChars(data []byte) string { + if len(data) == 0 { return "EMPTY" } + out := strings.Builder { } + for _, byt := range data { + run := rune(byt) + if unicode.IsPrint(run) && run < 0x7F { + out.WriteRune(run) + } else { + out.WriteRune('.') + } + out.WriteRune(' ') + } + return out.String() +} + // Describe returns a string representing the type and data of the given value. func Describe(value any) string { desc := describer { }