summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorJason Song <i@wolfogre.com>2022-11-28 19:19:18 +0800
committerGitHub <noreply@github.com>2022-11-28 11:19:18 +0000
commit9607750b5e9001ab379fa8deab0dadbb6219c66e (patch)
treedbdd22fbe52114b852d75f7f108342570bd81eeb /modules
parente81ccc406bf723a5a58d685e7782f281736affd4 (diff)
downloadgitea-9607750b5e9001ab379fa8deab0dadbb6219c66e.tar.gz
gitea-9607750b5e9001ab379fa8deab0dadbb6219c66e.zip
Replace fmt.Sprintf with hex.EncodeToString (#21960)
`hex.EncodeToString` has better performance than `fmt.Sprintf("%x", []byte)`, we should use it as much as possible. I'm not an extreme fan of performance, so I think there are some exceptions: - `fmt.Sprintf("%x", func(...)[N]byte())` - We can't slice the function return value directly, and it's not worth adding lines. ```diff func A()[20]byte { ... } - a := fmt.Sprintf("%x", A()) - a := hex.EncodeToString(A()[:]) // invalid + tmp := A() + a := hex.EncodeToString(tmp[:]) ``` - `fmt.Sprintf("%X", []byte)` - `strings.ToUpper(hex.EncodeToString(bytes))` has even worse performance.
Diffstat (limited to 'modules')
-rw-r--r--modules/packages/hashed_buffer_test.go10
-rw-r--r--modules/packages/multi_hasher_test.go18
2 files changed, 14 insertions, 14 deletions
diff --git a/modules/packages/hashed_buffer_test.go b/modules/packages/hashed_buffer_test.go
index 529262226f..e907aa0605 100644
--- a/modules/packages/hashed_buffer_test.go
+++ b/modules/packages/hashed_buffer_test.go
@@ -4,7 +4,7 @@
package packages
import (
- "fmt"
+ "encoding/hex"
"io"
"strings"
"testing"
@@ -36,10 +36,10 @@ func TestHashedBuffer(t *testing.T) {
assert.Equal(t, c.Data, string(data))
hashMD5, hashSHA1, hashSHA256, hashSHA512 := buf.Sums()
- assert.Equal(t, c.HashMD5, fmt.Sprintf("%x", hashMD5))
- assert.Equal(t, c.HashSHA1, fmt.Sprintf("%x", hashSHA1))
- assert.Equal(t, c.HashSHA256, fmt.Sprintf("%x", hashSHA256))
- assert.Equal(t, c.HashSHA512, fmt.Sprintf("%x", hashSHA512))
+ assert.Equal(t, c.HashMD5, hex.EncodeToString(hashMD5))
+ assert.Equal(t, c.HashSHA1, hex.EncodeToString(hashSHA1))
+ assert.Equal(t, c.HashSHA256, hex.EncodeToString(hashSHA256))
+ assert.Equal(t, c.HashSHA512, hex.EncodeToString(hashSHA512))
assert.NoError(t, buf.Close())
}
diff --git a/modules/packages/multi_hasher_test.go b/modules/packages/multi_hasher_test.go
index 42c1ef416f..a37debbc95 100644
--- a/modules/packages/multi_hasher_test.go
+++ b/modules/packages/multi_hasher_test.go
@@ -4,7 +4,7 @@
package packages
import (
- "fmt"
+ "encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
@@ -24,10 +24,10 @@ func TestMultiHasherSums(t *testing.T) {
hashMD5, hashSHA1, hashSHA256, hashSHA512 := h.Sums()
- assert.Equal(t, expectedMD5, fmt.Sprintf("%x", hashMD5))
- assert.Equal(t, expectedSHA1, fmt.Sprintf("%x", hashSHA1))
- assert.Equal(t, expectedSHA256, fmt.Sprintf("%x", hashSHA256))
- assert.Equal(t, expectedSHA512, fmt.Sprintf("%x", hashSHA512))
+ assert.Equal(t, expectedMD5, hex.EncodeToString(hashMD5))
+ assert.Equal(t, expectedSHA1, hex.EncodeToString(hashSHA1))
+ assert.Equal(t, expectedSHA256, hex.EncodeToString(hashSHA256))
+ assert.Equal(t, expectedSHA512, hex.EncodeToString(hashSHA512))
})
t.Run("State", func(t *testing.T) {
@@ -45,9 +45,9 @@ func TestMultiHasherSums(t *testing.T) {
hashMD5, hashSHA1, hashSHA256, hashSHA512 := h2.Sums()
- assert.Equal(t, expectedMD5, fmt.Sprintf("%x", hashMD5))
- assert.Equal(t, expectedSHA1, fmt.Sprintf("%x", hashSHA1))
- assert.Equal(t, expectedSHA256, fmt.Sprintf("%x", hashSHA256))
- assert.Equal(t, expectedSHA512, fmt.Sprintf("%x", hashSHA512))
+ assert.Equal(t, expectedMD5, hex.EncodeToString(hashMD5))
+ assert.Equal(t, expectedSHA1, hex.EncodeToString(hashSHA1))
+ assert.Equal(t, expectedSHA256, hex.EncodeToString(hashSHA256))
+ assert.Equal(t, expectedSHA512, hex.EncodeToString(hashSHA512))
})
}