2021-09-11 15:11:41 +00:00
|
|
|
/*
|
|
|
|
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 {
|
2022-04-28 02:56:26 +00:00
|
|
|
return fmt.Sprintf("%db", b)
|
2021-09-11 15:11:41 +00:00
|
|
|
}
|
|
|
|
div, exp := uint64(unit), 0
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
2022-04-28 02:56:26 +00:00
|
|
|
return fmt.Sprintf("%.1f%cb",
|
2021-09-11 15:11:41 +00:00
|
|
|
float64(b)/float64(div), "KMGTPE"[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
|
|
|
|
}
|