aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/base64
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2023-09-02 20:38:49 -0700
committerGopher Robot <gobot@golang.org>2023-09-03 18:57:29 +0000
commit53739590571e66cbace8a6b388a79901cfb8fc3a (patch)
tree2647e4919e964fbbaf320cb1dd623f5229c20c7d /src/encoding/base64
parent05d4b57c9fd082c8f68f454d1bb4ae26a3c7f5b9 (diff)
downloadgo-53739590571e66cbace8a6b388a79901cfb8fc3a.tar.xz
encoding: show the alphabet for base32 and base64
There is not a great reason to hide the alphabet used for StdEncoding, HexEncoding, and URLEncoding. Although this is specified in RFC 4748, showing it in GoDoc saves an extra click from going to the RFC itself to see the alphabet being used. Also, split exported and unexported constants apart so that GoDoc renders more cleanly. Fixes #55126 Change-Id: I03bfa607fb6c3df7f757e33fc0f4ec2b233de1a1 Reviewed-on: https://go-review.googlesource.com/c/go/+/525296 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> 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.go19
-rw-r--r--src/encoding/base64/base64_test.go2
2 files changed, 11 insertions, 10 deletions
diff --git a/src/encoding/base64/base64.go b/src/encoding/base64/base64.go
index 9445cbd4ef..992f5c243f 100644
--- a/src/encoding/base64/base64.go
+++ b/src/encoding/base64/base64.go
@@ -29,9 +29,12 @@ type Encoding struct {
}
const (
- StdPadding rune = '=' // Standard padding character
- NoPadding rune = -1 // No padding
- decodeMapInitialize = "" +
+ StdPadding rune = '=' // Standard padding character
+ NoPadding rune = -1 // No padding
+)
+
+const (
+ decodeMapInitialize = "" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
@@ -51,9 +54,6 @@ const (
invalidIndex = '\xff'
)
-const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
-const encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
-
// NewEncoding returns a new padded Encoding defined by the given alphabet,
// which must be a 64-byte string that contains unique byte values and
// does not contain the padding character or CR / LF ('\r', '\n').
@@ -115,13 +115,12 @@ func (enc Encoding) Strict() *Encoding {
return &enc
}
-// StdEncoding is the standard base64 encoding, as defined in
-// RFC 4648.
-var StdEncoding = NewEncoding(encodeStd)
+// StdEncoding is the standard base64 encoding, as defined in RFC 4648.
+var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
// URLEncoding is the alternate base64 encoding defined in RFC 4648.
// It is typically used in URLs and file names.
-var URLEncoding = NewEncoding(encodeURL)
+var URLEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
// RawStdEncoding is the standard raw, unpadded base64 encoding,
// as defined in RFC 4648 section 3.2.
diff --git a/src/encoding/base64/base64_test.go b/src/encoding/base64/base64_test.go
index 6dfdaef1f1..7f5ebd8085 100644
--- a/src/encoding/base64/base64_test.go
+++ b/src/encoding/base64/base64_test.go
@@ -70,6 +70,8 @@ func rawURLRef(ref string) string {
return rawRef(urlRef(ref))
}
+const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+
// A nonstandard encoding with a funny padding character, for testing
var funnyEncoding = NewEncoding(encodeStd).WithPadding(rune('@'))