all: implement AppendText alongside MarshalText (#9207)

This eventually allows encoding packages that may respect
the proposed encoding.TextAppender interface.
The performance gains from this is between 10-30%.

Updates tailscale/corp#14379

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai
2023-09-01 18:15:19 -07:00
committed by GitHub
parent 9a3bc9049c
commit c6fadd6d71
12 changed files with 108 additions and 69 deletions

View File

@@ -38,8 +38,12 @@ func ParsePrivateID(in string) (out PrivateID, err error) {
return out, err
}
func (id PrivateID) AppendText(b []byte) ([]byte, error) {
return hexAppendEncode(b, id[:]), nil
}
func (id PrivateID) MarshalText() ([]byte, error) {
return formatID(id), nil
return id.AppendText(nil)
}
func (id *PrivateID) UnmarshalText(in []byte) error {
@@ -47,7 +51,7 @@ func (id *PrivateID) UnmarshalText(in []byte) error {
}
func (id PrivateID) String() string {
return string(formatID(id))
return string(hexAppendEncode(nil, id[:]))
}
func (id PrivateID) IsZero() bool {
@@ -70,8 +74,12 @@ func ParsePublicID(in string) (out PublicID, err error) {
return out, err
}
func (id PublicID) AppendText(b []byte) ([]byte, error) {
return hexAppendEncode(b, id[:]), nil
}
func (id PublicID) MarshalText() ([]byte, error) {
return formatID(id), nil
return id.AppendText(nil)
}
func (id *PublicID) UnmarshalText(in []byte) error {
@@ -79,7 +87,7 @@ func (id *PublicID) UnmarshalText(in []byte) error {
}
func (id PublicID) String() string {
return string(formatID(id))
return string(hexAppendEncode(nil, id[:]))
}
func (id1 PublicID) Less(id2 PublicID) bool {
@@ -98,10 +106,12 @@ func (id PublicID) Prefix64() uint64 {
return binary.BigEndian.Uint64(id[:8])
}
func formatID(in [32]byte) []byte {
var hexArr [2 * len(in)]byte
hex.Encode(hexArr[:], in[:])
return hexArr[:]
// TODO(https://go.dev/issue/53693): Use hex.AppendEncode instead.
func hexAppendEncode(dst, src []byte) []byte {
n := hex.EncodedLen(len(src))
dst = slices.Grow(dst, n)
hex.Encode(dst[len(dst):][:n], src)
return dst[:len(dst)+n]
}
func parseID[Bytes []byte | string](funcName string, out *[32]byte, in Bytes) (err error) {