From 0c2fc6fbcf40777a22819b20007275c53f456ca0 Mon Sep 17 00:00:00 2001 From: Ian Denhardt Date: Fri, 17 Feb 2023 20:07:50 -0500 Subject: [PATCH 1/2] Remove more uses of fmt. Only a few left. --- rawpointer.go | 30 +++++++++++++++++++++++++----- server/server.go | 4 +--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/rawpointer.go b/rawpointer.go index 7f555479..bd74e784 100644 --- a/rawpointer.go +++ b/rawpointer.go @@ -2,6 +2,8 @@ package capnp import ( "fmt" + + "capnproto.org/go/capnp/v3/internal/str" ) // pointerOffset is an address offset in multiples of word size. @@ -201,7 +203,11 @@ func (p rawPointer) GoString() string { } switch p.pointerType() { case structPointer: - return fmt.Sprintf("rawStructPointer(%d, %#v)", p.offset(), p.structSize()) + return "rawStructPointer(" + + str.Itod(p.offset()) + + " , " + + p.structSize().String() + + ")" case listPointer: var lt string switch p.listType() { @@ -222,16 +228,30 @@ func (p rawPointer) GoString() string { case compositeList: lt = "compositeList" } - return fmt.Sprintf("rawListPointer(%d, %s, %d)", p.offset(), lt, p.numListElements()) + return "rawListPointer(" + + str.Itod(p.offset()) + + ", " + + lt + + ", " + + str.Itod(p.numListElements()) + + ")" case farPointer: - return fmt.Sprintf("rawFarPointer(%d, %v)", p.farSegment(), p.farAddress()) + return "rawFarPointer(" + + str.Utod(p.farSegment()) + + ", " + + p.farAddress().String() + + ")" case doubleFarPointer: - return fmt.Sprintf("rawDoubleFarPointer(%d, %v)", p.farSegment(), p.farAddress()) + return "rawDoubleFarPointer(" + + str.Utod(p.farSegment()) + + ", " + + p.farAddress().String() + + ")" default: // other pointer if p.otherPointerType() != 0 { return fmt.Sprintf("rawPointer(%#016x)", uint64(p)) } - return fmt.Sprintf("rawInterfacePointer(%d)", p.capabilityIndex()) + return "rawInterfacePointer(" + str.Utod(p.capabilityIndex()) + ")" } } diff --git a/server/server.go b/server/server.go index 053e4636..1434ee2c 100644 --- a/server/server.go +++ b/server/server.go @@ -4,7 +4,6 @@ package server // import "capnproto.org/go/capnp/v3/server" import ( "context" - "fmt" "sort" "sync" @@ -275,8 +274,7 @@ func sendArgsToStruct(s capnp.Send) (capnp.Struct, error) { } if err := s.PlaceArgs(st); err != nil { st.Message().Reset(nil) - // Using fmt.Errorf to ensure sendArgsToStruct returns a generic error. - return capnp.Struct{}, fmt.Errorf("place args: %w", err) + return capnp.Struct{}, exc.WrapError("place args", err) } return st, nil } From fd4ff32e3c2f2252cea56a502d7f0555a0235f04 Mon Sep 17 00:00:00 2001 From: Ian Denhardt Date: Fri, 17 Feb 2023 20:15:41 -0500 Subject: [PATCH 2/2] Remove another use of fmt. --- internal/str/str.go | 20 ++++++++++++++++++++ rawpointer.go | 4 +--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/internal/str/str.go b/internal/str/str.go index 34d97d3d..84b4c21f 100644 --- a/internal/str/str.go +++ b/internal/str/str.go @@ -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 } diff --git a/rawpointer.go b/rawpointer.go index bd74e784..2a5aeb61 100644 --- a/rawpointer.go +++ b/rawpointer.go @@ -1,8 +1,6 @@ package capnp import ( - "fmt" - "capnproto.org/go/capnp/v3/internal/str" ) @@ -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()) + ")" }