diff options
| author | Ian Lance Taylor <iant@golang.org> | 2024-05-22 13:38:40 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-05-23 01:00:11 +0000 |
| commit | b0b1d42db32a992150dd26681d3bda222e108303 (patch) | |
| tree | f520827bed796e2b0edc4cd91b2ac6dce89c718b /src/encoding | |
| parent | 1849ce6a45640ec4a6e63138211eac4276473437 (diff) | |
| download | go-b0b1d42db32a992150dd26681d3bda222e108303.tar.xz | |
all: change from sort functions to slices functions where feasible
Doing this because the slices functions are slightly faster and
slightly easier to use. It also removes one dependency layer.
This CL does not change packages that are used during bootstrap,
as the bootstrap compiler does not have the required slices functions.
It does not change the go/scanner package because the ErrorList
Len, Swap, and Less methods are part of the Go 1 API.
Change-Id: If52899be791c829198e11d2408727720b91ebe8a
Reviewed-on: https://go-review.googlesource.com/c/go/+/587655
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/encoding')
| -rw-r--r-- | src/encoding/asn1/marshal.go | 18 | ||||
| -rw-r--r-- | src/encoding/gob/encoder_test.go | 13 | ||||
| -rw-r--r-- | src/encoding/pem/pem.go | 4 |
3 files changed, 17 insertions, 18 deletions
diff --git a/src/encoding/asn1/marshal.go b/src/encoding/asn1/marshal.go index 69ab4f6f9e..b9c0b8bce0 100644 --- a/src/encoding/asn1/marshal.go +++ b/src/encoding/asn1/marshal.go @@ -10,7 +10,7 @@ import ( "fmt" "math/big" "reflect" - "sort" + "slices" "time" "unicode/utf8" ) @@ -105,15 +105,13 @@ func (s setEncoder) Encode(dst []byte) { e.Encode(l[i]) } - sort.Slice(l, func(i, j int) bool { - // Since we are using bytes.Compare to compare TLV encodings we - // don't need to right pad s[i] and s[j] to the same length as - // suggested in X690. If len(s[i]) < len(s[j]) the length octet of - // s[i], which is the first determining byte, will inherently be - // smaller than the length octet of s[j]. This lets us skip the - // padding step. - return bytes.Compare(l[i], l[j]) < 0 - }) + // Since we are using bytes.Compare to compare TLV encodings we + // don't need to right pad s[i] and s[j] to the same length as + // suggested in X690. If len(s[i]) < len(s[j]) the length octet of + // s[i], which is the first determining byte, will inherently be + // smaller than the length octet of s[j]. This lets us skip the + // padding step. + slices.SortFunc(l, bytes.Compare) var off int for _, b := range l { diff --git a/src/encoding/gob/encoder_test.go b/src/encoding/gob/encoder_test.go index d99b0715f9..efb13bc83b 100644 --- a/src/encoding/gob/encoder_test.go +++ b/src/encoding/gob/encoder_test.go @@ -6,12 +6,13 @@ package gob import ( "bytes" + "cmp" "encoding/hex" "fmt" "io" "math" "reflect" - "sort" + "slices" "strings" "testing" ) @@ -1186,12 +1187,12 @@ func TestMarshalFloatMap(t *testing.T) { for k, v := range m { entries = append(entries, mapEntry{math.Float64bits(k), v}) } - sort.Slice(entries, func(i, j int) bool { - ei, ej := entries[i], entries[j] - if ei.keyBits != ej.keyBits { - return ei.keyBits < ej.keyBits + slices.SortFunc(entries, func(a, b mapEntry) int { + r := cmp.Compare(a.keyBits, b.keyBits) + if r != 0 { + return r } - return ei.value < ej.value + return cmp.Compare(a.value, b.value) }) return entries } diff --git a/src/encoding/pem/pem.go b/src/encoding/pem/pem.go index 4b4f749021..7a515fd363 100644 --- a/src/encoding/pem/pem.go +++ b/src/encoding/pem/pem.go @@ -12,7 +12,7 @@ import ( "encoding/base64" "errors" "io" - "sort" + "slices" "strings" ) @@ -274,7 +274,7 @@ func Encode(out io.Writer, b *Block) error { } } // For consistency of output, write other headers sorted by key. - sort.Strings(h) + slices.Sort(h) for _, k := range h { if err := writeHeader(out, k, b.Headers[k]); err != nil { return err |
