From 469d9e26eec76341da8ebc4ef9cedb5bdb32ce73 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Sun, 19 Feb 2023 23:19:20 -0800 Subject: 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 TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Run-TryBot: Joseph Tsai Auto-Submit: Joseph Tsai --- src/encoding/base32/base32.go | 20 ++++++++++++++++++++ src/encoding/base32/base32_test.go | 10 +++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) (limited to 'src/encoding/base32') diff --git a/src/encoding/base32/base32.go b/src/encoding/base32/base32.go index 69ced9ca3c..7cccbd17be 100644 --- a/src/encoding/base32/base32.go +++ b/src/encoding/base32/base32.go @@ -7,6 +7,7 @@ package base32 import ( "io" + "slices" "strconv" ) @@ -176,6 +177,15 @@ func (enc *Encoding) Encode(dst, src []byte) { } } +// AppendEncode appends the base32 encoded src to dst +// and returns the extended buffer. +func (enc *Encoding) AppendEncode(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] +} + // EncodeToString returns the base32 encoding of src. func (enc *Encoding) EncodeToString(src []byte) string { buf := make([]byte, enc.EncodedLen(len(src))) @@ -378,6 +388,16 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) { return } +// AppendDecode appends the base32 decoded src to dst +// and returns the extended buffer. +// If the input is malformed, it returns the partially decoded src and an error. +func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error) { + n := enc.DecodedLen(len(src)) + dst = slices.Grow(dst, n) + n, err := enc.Decode(dst[len(dst):][:n], src) + return dst[:len(dst)+n], err +} + // DecodeString returns the bytes represented by the base32 string s. func (enc *Encoding) DecodeString(s string) ([]byte, error) { buf := []byte(s) diff --git a/src/encoding/base32/base32_test.go b/src/encoding/base32/base32_test.go index bdb9f0e61f..0132744507 100644 --- a/src/encoding/base32/base32_test.go +++ b/src/encoding/base32/base32_test.go @@ -57,6 +57,8 @@ func TestEncode(t *testing.T) { for _, p := range pairs { got := StdEncoding.EncodeToString([]byte(p.decoded)) testEqual(t, "Encode(%q) = %q, want %q", p.decoded, got, p.encoded) + dst := StdEncoding.AppendEncode([]byte("lead"), []byte(p.decoded)) + testEqual(t, `AppendEncode("lead", %q) = %q, want %q`, p.decoded, string(dst), "lead"+p.encoded) } } @@ -99,13 +101,15 @@ func TestDecode(t *testing.T) { if len(p.encoded) > 0 { testEqual(t, "Decode(%q) = end %v, want %v", p.encoded, end, (p.encoded[len(p.encoded)-1] == '=')) } - testEqual(t, "Decode(%q) = %q, want %q", p.encoded, - string(dbuf[0:count]), - p.decoded) + testEqual(t, "Decode(%q) = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded) dbuf, err = StdEncoding.DecodeString(p.encoded) testEqual(t, "DecodeString(%q) = error %v, want %v", p.encoded, err, error(nil)) testEqual(t, "DecodeString(%q) = %q, want %q", p.encoded, string(dbuf), p.decoded) + + dst, err := StdEncoding.AppendDecode([]byte("lead"), []byte(p.encoded)) + testEqual(t, "AppendDecode(%q) = error %v, want %v", p.encoded, err, error(nil)) + testEqual(t, `AppendDecode("lead", %q) = %q, want %q`, p.encoded, string(dst), "lead"+p.decoded) } } -- cgit v1.3