diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2023-02-19 23:19:20 -0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-08-17 16:23:42 +0000 |
| commit | 469d9e26eec76341da8ebc4ef9cedb5bdb32ce73 (patch) | |
| tree | b98d8ce8561a7b497b5a3ea8a434dc07f07834ec /src/encoding/hex | |
| parent | 7af3107632bbc761aef3ae453f174341f7a9184b (diff) | |
| download | go-469d9e26eec76341da8ebc4ef9cedb5bdb32ce73.tar.xz | |
encoding: add AppendEncode and AppendDecode
Implement append-like equivalent of Encode and Decode functions.
Fixes #53693
Change-Id: I79d8d834e3c8f77fad32be2fd391e33d4d1527ea
Reviewed-on: https://go-review.googlesource.com/c/go/+/504884
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Diffstat (limited to 'src/encoding/hex')
| -rw-r--r-- | src/encoding/hex/hex.go | 20 | ||||
| -rw-r--r-- | src/encoding/hex/hex_test.go | 12 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/encoding/hex/hex.go b/src/encoding/hex/hex.go index 375f583170..ccc395e2f7 100644 --- a/src/encoding/hex/hex.go +++ b/src/encoding/hex/hex.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "io" + "slices" "strings" ) @@ -51,6 +52,15 @@ func Encode(dst, src []byte) int { return len(src) * 2 } +// AppendEncode appends the hexadecimally encoded src to dst +// and returns the extended buffer. +func AppendEncode(dst, src []byte) []byte { + n := EncodedLen(len(src)) + dst = slices.Grow(dst, n) + Encode(dst[len(dst):][:n], src) + return dst[:len(dst)+n] +} + // ErrLength reports an attempt to decode an odd-length input // using Decode or DecodeString. // The stream-based Decoder returns io.ErrUnexpectedEOF instead of ErrLength. @@ -102,6 +112,16 @@ func Decode(dst, src []byte) (int, error) { return i, nil } +// AppendDecode appends the hexadecimally decoded src to dst +// and returns the extended buffer. +// If the input is malformed, it returns the partially decoded src and an error. +func AppendDecode(dst, src []byte) ([]byte, error) { + n := DecodedLen(len(src)) + dst = slices.Grow(dst, n) + n, err := Decode(dst[len(dst):][:n], src) + return dst[:len(dst)+n], err +} + // EncodeToString returns the hexadecimal encoding of src. func EncodeToString(src []byte) string { dst := make([]byte, EncodedLen(len(src))) diff --git a/src/encoding/hex/hex_test.go b/src/encoding/hex/hex_test.go index a820fe7a15..03331eaae5 100644 --- a/src/encoding/hex/hex_test.go +++ b/src/encoding/hex/hex_test.go @@ -37,6 +37,11 @@ func TestEncode(t *testing.T) { if string(dst) != test.enc { t.Errorf("#%d: got: %#v want: %#v", i, dst, test.enc) } + dst = []byte("lead") + dst = AppendEncode(dst, test.dec) + if string(dst) != "lead"+test.enc { + t.Errorf("#%d: got: %#v want: %#v", i, dst, "lead"+test.enc) + } } } @@ -52,6 +57,13 @@ func TestDecode(t *testing.T) { } else if !bytes.Equal(dst, test.dec) { t.Errorf("#%d: got: %#v want: %#v", i, dst, test.dec) } + dst = []byte("lead") + dst, err = AppendDecode(dst, []byte(test.enc)) + if err != nil { + t.Errorf("#%d: AppendDecode error: %v", i, err) + } else if string(dst) != "lead"+string(test.dec) { + t.Errorf("#%d: got: %#v want: %#v", i, dst, "lead"+string(test.dec)) + } } } |
