all: use new AppendEncode methods available in Go 1.22 (#11079)

Updates #cleanup

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
pull/10585/merge
Joe Tsai 3 months ago committed by GitHub
parent 94a4f701c2
commit 2e404b769d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -11,7 +11,6 @@ import (
"io" "io"
"io/fs" "io/fs"
"os" "os"
"slices"
"strings" "strings"
) )
@ -37,10 +36,10 @@ func (cs Checksum) String() string {
return hex.EncodeToString(cs.cs[:]) return hex.EncodeToString(cs.cs[:])
} }
func (cs Checksum) AppendText(b []byte) ([]byte, error) { func (cs Checksum) AppendText(b []byte) ([]byte, error) {
return hexAppendEncode(b, cs.cs[:]), nil return hex.AppendEncode(b, cs.cs[:]), nil
} }
func (cs Checksum) MarshalText() ([]byte, error) { func (cs Checksum) MarshalText() ([]byte, error) {
return hexAppendEncode(nil, cs.cs[:]), nil return hex.AppendEncode(nil, cs.cs[:]), nil
} }
func (cs *Checksum) UnmarshalText(b []byte) error { func (cs *Checksum) UnmarshalText(b []byte) error {
if len(b) != 2*len(cs.cs) { if len(b) != 2*len(cs.cs) {
@ -50,14 +49,6 @@ func (cs *Checksum) UnmarshalText(b []byte) error {
return err return err
} }
// 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]
}
// PartialFiles returns a list of partial files in [Handler.Dir] // PartialFiles returns a list of partial files in [Handler.Dir]
// that were sent (or is actively being sent) by the provided id. // that were sent (or is actively being sent) by the provided id.
func (m *Manager) PartialFiles(id ClientID) (ret []string, err error) { func (m *Manager) PartialFiles(id ClientID) (ret []string, err error) {

@ -9,7 +9,6 @@ import (
"encoding/base32" "encoding/base32"
"errors" "errors"
"fmt" "fmt"
"slices"
"github.com/fxamacker/cbor/v2" "github.com/fxamacker/cbor/v2"
"golang.org/x/crypto/blake2s" "golang.org/x/crypto/blake2s"
@ -39,17 +38,9 @@ func (h *AUMHash) UnmarshalText(text []byte) error {
return nil return nil
} }
// TODO(https://go.dev/issue/53693): Use base32.Encoding.AppendEncode instead.
func base32AppendEncode(enc *base32.Encoding, dst, src []byte) []byte {
n := enc.EncodedLen(len(src))
dst = slices.Grow(dst, n)
enc.Encode(dst[len(dst):][:n], src)
return dst[:len(dst)+n]
}
// AppendText implements encoding.TextAppender. // AppendText implements encoding.TextAppender.
func (h AUMHash) AppendText(b []byte) ([]byte, error) { func (h AUMHash) AppendText(b []byte) ([]byte, error) {
return base32AppendEncode(base32StdNoPad, b, h[:]), nil return base32StdNoPad.AppendEncode(b, h[:]), nil
} }
// MarshalText implements encoding.TextMarshaler. // MarshalText implements encoding.TextMarshaler.

@ -53,18 +53,10 @@ func clamp25519Private(b []byte) {
func appendHexKey(dst []byte, prefix string, key []byte) []byte { func appendHexKey(dst []byte, prefix string, key []byte) []byte {
dst = slices.Grow(dst, len(prefix)+hex.EncodedLen(len(key))) dst = slices.Grow(dst, len(prefix)+hex.EncodedLen(len(key)))
dst = append(dst, prefix...) dst = append(dst, prefix...)
dst = hexAppendEncode(dst, key) dst = hex.AppendEncode(dst, key)
return dst return dst
} }
// 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]
}
// parseHex decodes a key string of the form "<prefix><hex string>" // parseHex decodes a key string of the form "<prefix><hex string>"
// into out. The prefix must match, and the decoded base64 must fit // into out. The prefix must match, and the decoded base64 must fit
// exactly into out. // exactly into out.

@ -39,7 +39,7 @@ func ParsePrivateID(in string) (out PrivateID, err error) {
} }
func (id PrivateID) AppendText(b []byte) ([]byte, error) { func (id PrivateID) AppendText(b []byte) ([]byte, error) {
return hexAppendEncode(b, id[:]), nil return hex.AppendEncode(b, id[:]), nil
} }
func (id PrivateID) MarshalText() ([]byte, error) { func (id PrivateID) MarshalText() ([]byte, error) {
@ -51,7 +51,7 @@ func (id *PrivateID) UnmarshalText(in []byte) error {
} }
func (id PrivateID) String() string { func (id PrivateID) String() string {
return string(hexAppendEncode(nil, id[:])) return string(hex.AppendEncode(nil, id[:]))
} }
func (id PrivateID) IsZero() bool { func (id PrivateID) IsZero() bool {
@ -75,7 +75,7 @@ func ParsePublicID(in string) (out PublicID, err error) {
} }
func (id PublicID) AppendText(b []byte) ([]byte, error) { func (id PublicID) AppendText(b []byte) ([]byte, error) {
return hexAppendEncode(b, id[:]), nil return hex.AppendEncode(b, id[:]), nil
} }
func (id PublicID) MarshalText() ([]byte, error) { func (id PublicID) MarshalText() ([]byte, error) {
@ -87,7 +87,7 @@ func (id *PublicID) UnmarshalText(in []byte) error {
} }
func (id PublicID) String() string { func (id PublicID) String() string {
return string(hexAppendEncode(nil, id[:])) return string(hex.AppendEncode(nil, id[:]))
} }
func (id1 PublicID) Less(id2 PublicID) bool { func (id1 PublicID) Less(id2 PublicID) bool {
@ -106,14 +106,6 @@ func (id PublicID) Prefix64() uint64 {
return binary.BigEndian.Uint64(id[:8]) return binary.BigEndian.Uint64(id[:8])
} }
// 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) { func parseID[Bytes []byte | string](funcName string, out *[32]byte, in Bytes) (err error) {
if len(in) != 2*len(out) { if len(in) != 2*len(out) {
return fmt.Errorf("%s: invalid hex length: %d", funcName, len(in)) return fmt.Errorf("%s: invalid hex length: %d", funcName, len(in))

Loading…
Cancel
Save