aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes/bytes.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-03-22 14:02:15 +0700
committerShulhan <ms@kilabit.info>2023-03-22 22:37:51 +0700
commitbffd1f6ec0f6b63ed3f6b38c2bfddc6277ba86b8 (patch)
tree146071bbff464b0c47cecf6757fdc7a5f1fc49f3 /lib/bytes/bytes.go
parent3bd5fad4c0a972dd279ecfbf77de31838764f609 (diff)
downloadpakakeh.go-bffd1f6ec0f6b63ed3f6b38c2bfddc6277ba86b8.tar.xz
lib/bytes: add function DumpPrettyTable
The DumpPrettyTable write each byte in slice data as hexadecimal, ASCII character, and integer with 8 columns width.
Diffstat (limited to 'lib/bytes/bytes.go')
-rw-r--r--lib/bytes/bytes.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index a0c23c1c..cd666f65 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -8,6 +8,7 @@ package bytes
import (
"bytes"
"fmt"
+ "io"
"unicode"
)
@@ -583,6 +584,58 @@ func WordIndexes(s []byte, word []byte) (idxs []int) {
return idxs
}
+// DumpPrettyTable write each byte in slice data as hexadecimal, ASCII
+// character, and integer with 8 columns width.
+func DumpPrettyTable(w io.Writer, title string, data []byte) {
+ const ncol = 8
+
+ fmt.Fprintf(w, "%s\n", title)
+ fmt.Fprint(w, " | 0 1 2 3 4 5 6 7| 0 1 2 3 4 5 6 7| 0 1 2 3 4 5 6 7|\n")
+ fmt.Fprint(w, " | 8 9 A B C D E F| 8 9 A B C D E F| 8 9 A B C D E F|\n")
+
+ var (
+ chunks = SplitEach(data, ncol)
+ chunk []byte
+ x int
+ y int
+ c byte
+ )
+ for x, chunk = range chunks {
+ fmt.Fprintf(w, `%#02x|`, x*ncol)
+
+ // Print as hex.
+ for y, c = range chunk {
+ fmt.Fprintf(w, ` %02x`, c)
+ }
+ for y++; y < ncol; y++ {
+ fmt.Fprint(w, ` `)
+ }
+
+ // Print as char.
+ fmt.Fprint(w, `|`)
+ for y, c = range chunk {
+ if c >= 33 && c <= 126 {
+ fmt.Fprintf(w, ` %c`, c)
+ } else {
+ fmt.Fprint(w, ` .`)
+ }
+ }
+ for y++; y < ncol; y++ {
+ fmt.Fprint(w, ` `)
+ }
+
+ // Print as integer.
+ fmt.Fprint(w, `|`)
+ for y, c = range chunk {
+ fmt.Fprintf(w, ` %3d`, c)
+ }
+ for y++; y < ncol; y++ {
+ fmt.Fprint(w, ` `)
+ }
+ fmt.Fprintf(w, "|%02d\n", x*ncol)
+ }
+}
+
// WriteUint16 write uint16 value "v" into "data" start at position "x".
// If x is out range, the data will not change.
func WriteUint16(data []byte, x uint, v uint16) {