aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/base64
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2023-02-19 23:19:20 -0800
committerGopher Robot <gobot@golang.org>2023-08-17 16:23:42 +0000
commit469d9e26eec76341da8ebc4ef9cedb5bdb32ce73 (patch)
treeb98d8ce8561a7b497b5a3ea8a434dc07f07834ec /src/encoding/base64
parent7af3107632bbc761aef3ae453f174341f7a9184b (diff)
downloadgo-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/base64')
-rw-r--r--src/encoding/base64/base64.go20
-rw-r--r--src/encoding/base64/base64_test.go9
2 files changed, 27 insertions, 2 deletions
diff --git a/src/encoding/base64/base64.go b/src/encoding/base64/base64.go
index 87f6897062..5db72b91e2 100644
--- a/src/encoding/base64/base64.go
+++ b/src/encoding/base64/base64.go
@@ -8,6 +8,7 @@ package base64
import (
"encoding/binary"
"io"
+ "slices"
"strconv"
)
@@ -191,6 +192,15 @@ func (enc *Encoding) Encode(dst, src []byte) {
}
}
+// AppendEncode appends the base64 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 base64 encoding of src.
func (enc *Encoding) EncodeToString(src []byte) string {
buf := make([]byte, enc.EncodedLen(len(src)))
@@ -395,6 +405,16 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err
return si, dlen - 1, err
}
+// AppendDecode appends the base64 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 base64 string s.
func (enc *Encoding) DecodeString(s string) ([]byte, error) {
dbuf := make([]byte, enc.DecodedLen(len(s)))
diff --git a/src/encoding/base64/base64_test.go b/src/encoding/base64/base64_test.go
index 97aea845ae..4d7437b919 100644
--- a/src/encoding/base64/base64_test.go
+++ b/src/encoding/base64/base64_test.go
@@ -113,8 +113,9 @@ func TestEncode(t *testing.T) {
for _, p := range pairs {
for _, tt := range encodingTests {
got := tt.enc.EncodeToString([]byte(p.decoded))
- testEqual(t, "Encode(%q) = %q, want %q", p.decoded,
- got, tt.conv(p.encoded))
+ testEqual(t, "Encode(%q) = %q, want %q", p.decoded, got, tt.conv(p.encoded))
+ dst := tt.enc.AppendEncode([]byte("lead"), []byte(p.decoded))
+ testEqual(t, `AppendEncode("lead", %q) = %q, want %q`, p.decoded, string(dst), "lead"+tt.conv(p.encoded))
}
}
}
@@ -162,6 +163,10 @@ func TestDecode(t *testing.T) {
dbuf, err = tt.enc.DecodeString(encoded)
testEqual(t, "DecodeString(%q) = error %v, want %v", encoded, err, error(nil))
testEqual(t, "DecodeString(%q) = %q, want %q", encoded, string(dbuf), p.decoded)
+
+ dst, err := tt.enc.AppendDecode([]byte("lead"), []byte(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)
}
}
}