internal/testutil: Add functions to dump printable chars

This commit is contained in:
Sasha Koshka 2025-10-15 17:18:00 -04:00
parent 70fb106b48
commit 7bebc8c5eb

View File

@ -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 { }