forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/labstack/gommon/bytes/bytes.go

372 lines
4.8 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package bytes
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type (
2024-02-18 10:42:21 +00:00
// Bytes struct
2024-02-18 10:42:21 +00:00
Bytes struct{}
)
// binary units (IEC 60027)
2024-02-18 10:42:21 +00:00
const (
_ = 1.0 << (10 * iota) // ignore first value by assigning to blank identifier
2024-02-18 10:42:21 +00:00
KiB
2024-02-18 10:42:21 +00:00
MiB
2024-02-18 10:42:21 +00:00
GiB
2024-02-18 10:42:21 +00:00
TiB
2024-02-18 10:42:21 +00:00
PiB
2024-02-18 10:42:21 +00:00
EiB
)
// decimal units (SI international system of units)
2024-02-18 10:42:21 +00:00
const (
KB = 1000
2024-02-18 10:42:21 +00:00
MB = KB * 1000
2024-02-18 10:42:21 +00:00
GB = MB * 1000
2024-02-18 10:42:21 +00:00
TB = GB * 1000
2024-02-18 10:42:21 +00:00
PB = TB * 1000
2024-02-18 10:42:21 +00:00
EB = PB * 1000
)
var (
patternBinary = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]iB?)$`)
2024-02-18 10:42:21 +00:00
patternDecimal = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]B?|B?)$`)
global = New()
2024-02-18 10:42:21 +00:00
)
// New creates a Bytes instance.
2024-02-18 10:42:21 +00:00
func New() *Bytes {
2024-02-18 10:42:21 +00:00
return &Bytes{}
2024-02-18 10:42:21 +00:00
}
// Format formats bytes integer to human readable string according to IEC 60027.
2024-02-18 10:42:21 +00:00
// For example, 31323 bytes will return 30.59KB.
2024-02-18 10:42:21 +00:00
func (b *Bytes) Format(value int64) string {
2024-02-18 10:42:21 +00:00
return b.FormatBinary(value)
2024-02-18 10:42:21 +00:00
}
// FormatBinary formats bytes integer to human readable string according to IEC 60027.
2024-02-18 10:42:21 +00:00
// For example, 31323 bytes will return 30.59KB.
2024-02-18 10:42:21 +00:00
func (*Bytes) FormatBinary(value int64) string {
2024-02-18 10:42:21 +00:00
multiple := ""
2024-02-18 10:42:21 +00:00
val := float64(value)
switch {
2024-02-18 10:42:21 +00:00
case value >= EiB:
2024-02-18 10:42:21 +00:00
val /= EiB
2024-02-18 10:42:21 +00:00
multiple = "EiB"
2024-02-18 10:42:21 +00:00
case value >= PiB:
2024-02-18 10:42:21 +00:00
val /= PiB
2024-02-18 10:42:21 +00:00
multiple = "PiB"
2024-02-18 10:42:21 +00:00
case value >= TiB:
2024-02-18 10:42:21 +00:00
val /= TiB
2024-02-18 10:42:21 +00:00
multiple = "TiB"
2024-02-18 10:42:21 +00:00
case value >= GiB:
2024-02-18 10:42:21 +00:00
val /= GiB
2024-02-18 10:42:21 +00:00
multiple = "GiB"
2024-02-18 10:42:21 +00:00
case value >= MiB:
2024-02-18 10:42:21 +00:00
val /= MiB
2024-02-18 10:42:21 +00:00
multiple = "MiB"
2024-02-18 10:42:21 +00:00
case value >= KiB:
2024-02-18 10:42:21 +00:00
val /= KiB
2024-02-18 10:42:21 +00:00
multiple = "KiB"
2024-02-18 10:42:21 +00:00
case value == 0:
2024-02-18 10:42:21 +00:00
return "0"
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return strconv.FormatInt(value, 10) + "B"
2024-02-18 10:42:21 +00:00
}
return fmt.Sprintf("%.2f%s", val, multiple)
2024-02-18 10:42:21 +00:00
}
// FormatDecimal formats bytes integer to human readable string according to SI international system of units.
2024-02-18 10:42:21 +00:00
// For example, 31323 bytes will return 31.32KB.
2024-02-18 10:42:21 +00:00
func (*Bytes) FormatDecimal(value int64) string {
2024-02-18 10:42:21 +00:00
multiple := ""
2024-02-18 10:42:21 +00:00
val := float64(value)
switch {
2024-02-18 10:42:21 +00:00
case value >= EB:
2024-02-18 10:42:21 +00:00
val /= EB
2024-02-18 10:42:21 +00:00
multiple = "EB"
2024-02-18 10:42:21 +00:00
case value >= PB:
2024-02-18 10:42:21 +00:00
val /= PB
2024-02-18 10:42:21 +00:00
multiple = "PB"
2024-02-18 10:42:21 +00:00
case value >= TB:
2024-02-18 10:42:21 +00:00
val /= TB
2024-02-18 10:42:21 +00:00
multiple = "TB"
2024-02-18 10:42:21 +00:00
case value >= GB:
2024-02-18 10:42:21 +00:00
val /= GB
2024-02-18 10:42:21 +00:00
multiple = "GB"
2024-02-18 10:42:21 +00:00
case value >= MB:
2024-02-18 10:42:21 +00:00
val /= MB
2024-02-18 10:42:21 +00:00
multiple = "MB"
2024-02-18 10:42:21 +00:00
case value >= KB:
2024-02-18 10:42:21 +00:00
val /= KB
2024-02-18 10:42:21 +00:00
multiple = "KB"
2024-02-18 10:42:21 +00:00
case value == 0:
2024-02-18 10:42:21 +00:00
return "0"
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return strconv.FormatInt(value, 10) + "B"
2024-02-18 10:42:21 +00:00
}
return fmt.Sprintf("%.2f%s", val, multiple)
2024-02-18 10:42:21 +00:00
}
// Parse parses human readable bytes string to bytes integer.
2024-02-18 10:42:21 +00:00
// For example, 6GiB (6Gi is also valid) will return 6442450944, and
2024-02-18 10:42:21 +00:00
// 6GB (6G is also valid) will return 6000000000.
2024-02-18 10:42:21 +00:00
func (b *Bytes) Parse(value string) (int64, error) {
i, err := b.ParseBinary(value)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return i, err
2024-02-18 10:42:21 +00:00
}
return b.ParseDecimal(value)
2024-02-18 10:42:21 +00:00
}
// ParseBinary parses human readable bytes string to bytes integer.
2024-02-18 10:42:21 +00:00
// For example, 6GiB (6Gi is also valid) will return 6442450944.
2024-02-18 10:42:21 +00:00
func (*Bytes) ParseBinary(value string) (i int64, err error) {
2024-02-18 10:42:21 +00:00
parts := patternBinary.FindStringSubmatch(value)
2024-02-18 10:42:21 +00:00
if len(parts) < 3 {
2024-02-18 10:42:21 +00:00
return 0, fmt.Errorf("error parsing value=%s", value)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
bytesString := parts[1]
2024-02-18 10:42:21 +00:00
multiple := strings.ToUpper(parts[2])
2024-02-18 10:42:21 +00:00
bytes, err := strconv.ParseFloat(bytesString, 64)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
switch multiple {
2024-02-18 10:42:21 +00:00
case "KI", "KIB":
2024-02-18 10:42:21 +00:00
return int64(bytes * KiB), nil
2024-02-18 10:42:21 +00:00
case "MI", "MIB":
2024-02-18 10:42:21 +00:00
return int64(bytes * MiB), nil
2024-02-18 10:42:21 +00:00
case "GI", "GIB":
2024-02-18 10:42:21 +00:00
return int64(bytes * GiB), nil
2024-02-18 10:42:21 +00:00
case "TI", "TIB":
2024-02-18 10:42:21 +00:00
return int64(bytes * TiB), nil
2024-02-18 10:42:21 +00:00
case "PI", "PIB":
2024-02-18 10:42:21 +00:00
return int64(bytes * PiB), nil
2024-02-18 10:42:21 +00:00
case "EI", "EIB":
2024-02-18 10:42:21 +00:00
return int64(bytes * EiB), nil
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return int64(bytes), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// ParseDecimal parses human readable bytes string to bytes integer.
2024-02-18 10:42:21 +00:00
// For example, 6GB (6G is also valid) will return 6000000000.
2024-02-18 10:42:21 +00:00
func (*Bytes) ParseDecimal(value string) (i int64, err error) {
2024-02-18 10:42:21 +00:00
parts := patternDecimal.FindStringSubmatch(value)
2024-02-18 10:42:21 +00:00
if len(parts) < 3 {
2024-02-18 10:42:21 +00:00
return 0, fmt.Errorf("error parsing value=%s", value)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
bytesString := parts[1]
2024-02-18 10:42:21 +00:00
multiple := strings.ToUpper(parts[2])
2024-02-18 10:42:21 +00:00
bytes, err := strconv.ParseFloat(bytesString, 64)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
switch multiple {
2024-02-18 10:42:21 +00:00
case "K", "KB":
2024-02-18 10:42:21 +00:00
return int64(bytes * KB), nil
2024-02-18 10:42:21 +00:00
case "M", "MB":
2024-02-18 10:42:21 +00:00
return int64(bytes * MB), nil
2024-02-18 10:42:21 +00:00
case "G", "GB":
2024-02-18 10:42:21 +00:00
return int64(bytes * GB), nil
2024-02-18 10:42:21 +00:00
case "T", "TB":
2024-02-18 10:42:21 +00:00
return int64(bytes * TB), nil
2024-02-18 10:42:21 +00:00
case "P", "PB":
2024-02-18 10:42:21 +00:00
return int64(bytes * PB), nil
2024-02-18 10:42:21 +00:00
case "E", "EB":
2024-02-18 10:42:21 +00:00
return int64(bytes * EB), nil
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return int64(bytes), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Format wraps global Bytes's Format function.
2024-02-18 10:42:21 +00:00
func Format(value int64) string {
2024-02-18 10:42:21 +00:00
return global.Format(value)
2024-02-18 10:42:21 +00:00
}
// FormatBinary wraps global Bytes's FormatBinary function.
2024-02-18 10:42:21 +00:00
func FormatBinary(value int64) string {
2024-02-18 10:42:21 +00:00
return global.FormatBinary(value)
2024-02-18 10:42:21 +00:00
}
// FormatDecimal wraps global Bytes's FormatDecimal function.
2024-02-18 10:42:21 +00:00
func FormatDecimal(value int64) string {
2024-02-18 10:42:21 +00:00
return global.FormatDecimal(value)
2024-02-18 10:42:21 +00:00
}
// Parse wraps global Bytes's Parse function.
2024-02-18 10:42:21 +00:00
func Parse(value string) (int64, error) {
2024-02-18 10:42:21 +00:00
return global.Parse(value)
2024-02-18 10:42:21 +00:00
}