aboutsummaryrefslogtreecommitdiffstats
path: root/routers
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 /routers
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 'routers')
-rw-r--r--routers/api/packages/maven/maven.go4
-rw-r--r--routers/api/packages/pypi/pypi.go4
2 files changed, 4 insertions, 4 deletions
diff --git a/routers/api/packages/maven/maven.go b/routers/api/packages/maven/maven.go
index 0ca3fa1793..d0c9983cbf 100644
--- a/routers/api/packages/maven/maven.go
+++ b/routers/api/packages/maven/maven.go
@@ -8,9 +8,9 @@ import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
+ "encoding/hex"
"encoding/xml"
"errors"
- "fmt"
"io"
"net/http"
"path/filepath"
@@ -128,7 +128,7 @@ func serveMavenMetadata(ctx *context.Context, params parameters) {
tmp := sha512.Sum512(xmlMetadataWithHeader)
hash = tmp[:]
}
- ctx.PlainText(http.StatusOK, fmt.Sprintf("%x", hash))
+ ctx.PlainText(http.StatusOK, hex.EncodeToString(hash))
return
}
diff --git a/routers/api/packages/pypi/pypi.go b/routers/api/packages/pypi/pypi.go
index 826f58095e..6cf9329bbb 100644
--- a/routers/api/packages/pypi/pypi.go
+++ b/routers/api/packages/pypi/pypi.go
@@ -4,7 +4,7 @@
package pypi
import (
- "fmt"
+ "encoding/hex"
"io"
"net/http"
"regexp"
@@ -118,7 +118,7 @@ func UploadPackageFile(ctx *context.Context) {
_, _, hashSHA256, _ := buf.Sums()
- if !strings.EqualFold(ctx.Req.FormValue("sha256_digest"), fmt.Sprintf("%x", hashSHA256)) {
+ if !strings.EqualFold(ctx.Req.FormValue("sha256_digest"), hex.EncodeToString(hashSHA256)) {
apiError(ctx, http.StatusBadRequest, "hash mismatch")
return
}