mirror of
https://github.com/go-gitea/gitea.git
synced 2025-10-27 00:23:41 +09:00
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.
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user