Skip to content

Commit

Permalink
utter: use default bytes width if not set
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Apr 10, 2024
1 parent 94f1dba commit 240ff29
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -191,6 +195,10 @@ func initSpewTests() {
" int( /*%p*/ 5),\n"+
" (*interface{}) /*%[1]p*/ (<already shown>),\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",
Expand Down

0 comments on commit 240ff29

Please sign in to comment.