Skip to content

Commit

Permalink
Remove another use of fmt.
Browse files Browse the repository at this point in the history
  • Loading branch information
zenhack committed Feb 18, 2023
1 parent 0c2fc6f commit fd4ff32
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 20 additions & 0 deletions internal/str/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ func Itod[T Int](n T) string {
return strconv.FormatInt(int64(n), 10)
}

// UToHex returns n formatted in hexidecimal.
func UToHex[T Uint](n T) string {
return strconv.FormatUint(uint64(n), 16)
}

// ZeroPad pads value to the left with zeros, making the resulting string
// count bytes long.
func ZeroPad(count int, value string) string {
pad := count - len(value)
if pad < 0 {
panic("ZeroPad: count is less than len(value)")
}
buf := make([]byte, count)
for i := 0; i < pad; i++ {
buf[i] = '0'
}
copy(buf[:pad], value[:])
return string(buf)
}

type Uint interface {
~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint
}
Expand Down
4 changes: 1 addition & 3 deletions rawpointer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package capnp

import (
"fmt"

"capnproto.org/go/capnp/v3/internal/str"
)

Expand Down Expand Up @@ -250,7 +248,7 @@ func (p rawPointer) GoString() string {
default:
// other pointer
if p.otherPointerType() != 0 {
return fmt.Sprintf("rawPointer(%#016x)", uint64(p))
return "rawPointer(" + str.ZeroPad(16, str.UToHex(p)) + ")"
}
return "rawInterfacePointer(" + str.Utod(p.capabilityIndex()) + ")"
}
Expand Down

0 comments on commit fd4ff32

Please sign in to comment.