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",