From 240ff2951a1ea76d4aa8d6d079c13321d2cf5428 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sat, 6 Apr 2024 08:18:48 +1030 Subject: [PATCH] utter: use default bytes width if not set --- common.go | 4 ++++ config.go | 3 ++- spew_test.go | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/common.go b/common.go index fac40ad..5ae1d5e 100644 --- a/common.go +++ b/common.go @@ -231,6 +231,10 @@ func printComplex(w io.Writer, c complex128, floatPrecision int) { // hexDump is a modified 'hexdump -C'-like that returns a commented Go syntax // byte slice or array. func hexDump(w io.Writer, data []byte, indent string, width int, comment, addr bool) { + if width <= 0 { + width = 16 // This is the width used by hexdump -C, so it makes a reasonable default. + } + var commentBytes []byte if comment { commentBytes = make([]byte, width) diff --git a/config.go b/config.go index a4982d6..2feee50 100644 --- a/config.go +++ b/config.go @@ -54,7 +54,8 @@ type ConfigState struct { Quoting Quoting // BytesWidth specifies the number of byte columns to use when dumping a - // byte slice or array. + // byte slice or array. If this is not set or negative, a value of 16 + // is used. BytesWidth int // CommentBytes specifies whether byte slice or array dumps have ASCII diff --git a/spew_test.go b/spew_test.go index 2602200..81a0a9b 100644 --- a/spew_test.go +++ b/spew_test.go @@ -78,6 +78,10 @@ func initSpewTests() { comPtrDefault := utter.NewDefaultConfig() comPtrDefault.CommentPointers = true + // Byte slice with 8 columns. + bs0Default := utter.NewDefaultConfig() + bs0Default.BytesWidth = 0 + // Byte slice with 8 columns. bs8Default := utter.NewDefaultConfig() bs8Default.BytesWidth = 8 @@ -191,6 +195,10 @@ func initSpewTests() { " int( /*%p*/ 5),\n"+ " (*interface{}) /*%[1]p*/ (),\n"+ " &int /*%p*/ (5),\n },\n}\n", &c[0], &c[1])}, + {bs0Default, fCSFdump, []byte{1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5}, "[]uint8{\n" + + " 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, // |................|\n" + + " 0x05, /* */ // |.|\n}\n", + }, {bs8Default, fCSFdump, []byte{1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3}, "[]uint8{\n" + " 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x01, 0x02, // |........|\n" + " 0x03, 0x04, 0x05, 0x00, 0x01, 0x02, 0x03, /* */ // |.......|\n}\n",