]> Cypherpunks repositories - gostls13.git/commitdiff
encoding: show the alphabet for base32 and base64
authorJoe Tsai <joetsai@digital-static.net>
Sun, 3 Sep 2023 03:38:49 +0000 (20:38 -0700)
committerGopher Robot <gobot@golang.org>
Sun, 3 Sep 2023 18:57:29 +0000 (18:57 +0000)
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>

src/encoding/base32/base32.go
src/encoding/base64/base64.go
src/encoding/base64/base64_test.go

index e92188728592a1383c88b9c69c72dff645af4f17..d26cb5c685bec5a8ae2be9ed588d4f10f1b9e7ee 100644 (file)
@@ -26,9 +26,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" +
@@ -48,9 +51,6 @@ const (
        invalidIndex = '\xff'
 )
 
-const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
-const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
-
 // NewEncoding returns a new padded Encoding defined by the given alphabet,
 // which must be a 32-byte string that contains unique byte values and
 // does not contain the padding character or CR / LF ('\r', '\n').
@@ -83,13 +83,12 @@ func NewEncoding(encoder string) *Encoding {
        return e
 }
 
-// StdEncoding is the standard base32 encoding, as defined in
-// RFC 4648.
-var StdEncoding = NewEncoding(encodeStd)
+// StdEncoding is the standard base32 encoding, as defined in RFC 4648.
+var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")
 
 // HexEncoding is the “Extended Hex Alphabet” defined in RFC 4648.
 // It is typically used in DNS.
-var HexEncoding = NewEncoding(encodeHex)
+var HexEncoding = NewEncoding("0123456789ABCDEFGHIJKLMNOPQRSTUV")
 
 // WithPadding creates a new encoding identical to enc except
 // with a specified padding character, or NoPadding to disable padding.
index 9445cbd4efb8913d240091f64664bc1d43c33e8e..992f5c243fd619c0758ffb6a990327d0598d4177 100644 (file)
@@ -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.
index 6dfdaef1f1a11b61f853c61576d08b2f96605dfe..7f5ebd8085de56c9fe442d6a3c03d246ab1c5407 100644 (file)
@@ -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('@'))