erigon-pulse/common/bytes.go

64 lines
1.3 KiB
Go
Raw Normal View History

/*
Copyright 2021 Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"fmt"
)
func ByteCount(b uint64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%dB", b)
}
bGb, exp := MBToGB(b)
return fmt.Sprintf("%.1f%cB", bGb, "KMGTPE"[exp])
}
func MBToGB(b uint64) (float64, int) {
const unit = 1024
if b < unit {
return float64(b), 0
}
div, exp := uint64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return float64(b) / float64(div), exp
}
func Copy(b []byte) []byte {
if b == nil {
return nil
}
c := make([]byte, len(b))
copy(c, b)
return c
}
2021-09-18 13:58:20 +00:00
func EnsureEnoughSize(in []byte, size int) []byte {
if cap(in) < size {
newBuf := make([]byte, size)
copy(newBuf, in)
return newBuf
}
return in[:size] // Reuse the space if it has enough capacity
}