Skip to content

Commit

Permalink
Dump non-zero len and cap for applicable types.
Browse files Browse the repository at this point in the history
Closes #16.
  • Loading branch information
jrick authored and davecgh committed May 6, 2014
1 parent 9ed19f9 commit 3fdaf5c
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 126 deletions.
2 changes: 2 additions & 0 deletions spew/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ var (
closeAngleBytes = []byte(">")
openMapBytes = []byte("map[")
closeMapBytes = []byte("]")
lenEqualsBytes = []byte("len=")
capEqualsBytes = []byte("cap=")
)

// hexDigits is used to map a decimal value to a hex digit.
Expand Down
6 changes: 3 additions & 3 deletions spew/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ shown here.
flag: (main.Flag) flagTwo,
data: (uintptr) <nil>
}),
ExportedField: (map[interface {}]interface {}) {
(string) "one": (bool) true
ExportedField: (map[interface {}]interface {}) (len=1) {
(string) (len=3) "one": (bool) true
}
}
Byte (and uint8) arrays and slices are displayed uniquely like the hexdump -C
command as shown.
([]uint8) {
([]uint8) (len=32 cap=32) {
00000000 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 |............... |
00000010 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 |!"#$%&'()*+,-./0|
00000020 31 32 |12|
Expand Down
26 changes: 26 additions & 0 deletions spew/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,32 @@ func (d *dumpState) dump(v reflect.Value) {
}
d.ignoreNextType = false

// Display length and capacity if the built-in len and cap functions
// work with the value's kind and the len/cap itself is non-zero.
valueLen, valueCap := 0, 0
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Chan:
valueLen, valueCap = v.Len(), v.Cap()
case reflect.Map, reflect.String:
valueLen = v.Len()
}
if valueLen != 0 || valueCap != 0 {
d.w.Write(openParenBytes)
if valueLen != 0 {
d.w.Write(lenEqualsBytes)
printInt(d.w, int64(valueLen), 10)
}
if valueCap != 0 {
if valueLen != 0 {
d.w.Write(spaceBytes)
}
d.w.Write(capEqualsBytes)
printInt(d.w, int64(valueCap), 10)
}
d.w.Write(closeParenBytes)
d.w.Write(spaceBytes)
}

// Call Stringer/error interfaces if they exist and the handle methods flag
// is enabled
if !d.cs.DisableMethods {
Expand Down
Loading

0 comments on commit 3fdaf5c

Please sign in to comment.