Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove more uses of fmt package. #454

Merged
merged 2 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 25 additions & 7 deletions rawpointer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package capnp

import (
"fmt"
"capnproto.org/go/capnp/v3/internal/str"
)

// pointerOffset is an address offset in multiples of word size.
Expand Down Expand Up @@ -201,7 +201,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() {
Expand All @@ -222,16 +226,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 "rawPointer(" + str.ZeroPad(16, str.UToHex(p)) + ")"
}
return fmt.Sprintf("rawInterfacePointer(%d)", p.capabilityIndex())
return "rawInterfacePointer(" + str.Utod(p.capabilityIndex()) + ")"
}
}
4 changes: 1 addition & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package server // import "capnproto.org/go/capnp/v3/server"

import (
"context"
"fmt"
"sort"
"sync"

Expand Down Expand Up @@ -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
}
Expand Down