diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2023-07-08 12:40:36 +0200 |
---|---|---|
committer | Erik Dubbelboer <erik@dubbelboer.com> | 2023-07-08 12:40:36 +0200 |
commit | 1c85d43dfea23d49285c8248aff906c965062031 (patch) | |
tree | 38d827e13610680cf36ed01c4354490d7734c492 /http.go | |
parent | Avoid nolint:errcheck in header tests (#1589) (diff) | |
download | fasthttp-1c85d43dfea23d49285c8248aff906c965062031.tar.gz fasthttp-1c85d43dfea23d49285c8248aff906c965062031.tar.bz2 fasthttp-1c85d43dfea23d49285c8248aff906c965062031.zip |
Fix round2
- don't limit it to 32 bits
- give it a proper name
- don't over-allocate too much
Diffstat (limited to 'http.go')
-rw-r--r-- | http.go | 25 |
1 files changed, 2 insertions, 23 deletions
@@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "math" "mime/multipart" "net" "os" @@ -2210,7 +2209,7 @@ func readBodyIdentity(r *bufio.Reader, maxBodySize int, dst []byte) ([]byte, err return dst[:offset], ErrBodyTooLarge } if len(dst) == offset { - n := round2(2 * offset) + n := roundUpForSliceCap(2 * offset) if maxBodySize > 0 && n > maxBodySize { n = maxBodySize + 1 } @@ -2229,7 +2228,7 @@ func appendBodyFixedSize(r *bufio.Reader, dst []byte, n int) ([]byte, error) { offset := len(dst) dstLen := offset + n if cap(dst) < dstLen { - b := make([]byte, round2(dstLen)) + b := make([]byte, roundUpForSliceCap(dstLen)) copy(b, dst) dst = b } @@ -2339,26 +2338,6 @@ func readCrLf(r *bufio.Reader) error { return nil } -func round2(n int) int { - if n <= 0 { - return 0 - } - - x := uint32(n - 1) - x |= x >> 1 - x |= x >> 2 - x |= x >> 4 - x |= x >> 8 - x |= x >> 16 - - // Make sure we don't return 0 due to overflow, even on 32 bit systems - if x >= uint32(math.MaxInt32) { - return math.MaxInt32 - } - - return int(x + 1) -} - // SetTimeout sets timeout for the request. // // req.SetTimeout(t); c.Do(&req, &resp) is equivalent to |