Documentation
¶
Overview ¶
Package bytesize provides a type for representing byte sizes with support for both decimal (SI) and binary (IEC) units, as well as parsing from strings and formatting to human-readable strings.
Index ¶
- Variables
- func IsValidUnit(unit string) bool
- type Bytes
- type FormatOption
- type Uint128
- func (u Uint128) Add(v Uint128) Uint128
- func (u Uint128) Add64(v uint64) Uint128
- func (u Uint128) Add64Err(v uint64) (Uint128, error)
- func (u Uint128) AddBytes(v Bytes) Uint128
- func (u Uint128) AddBytesErr(v Bytes) (Uint128, error)
- func (u Uint128) AddErr(v Uint128) (Uint128, error)
- func (u Uint128) AddWrap(v Uint128) Uint128
- func (u Uint128) AddWrap64(v uint64) Uint128
- func (u Uint128) AddWrapBytes(v Bytes) Uint128
- func (u Uint128) And(v Uint128) Uint128
- func (u Uint128) And64(v uint64) Uint128
- func (u Uint128) AndBytes(v Bytes) Uint128
- func (u Uint128) AppendBytes(b []byte) []byte
- func (u Uint128) AppendBytesBE(b []byte) []byte
- func (u Uint128) Big() *big.Int
- func (u Uint128) Cmp(v Uint128) int
- func (u Uint128) Cmp64(v uint64) int
- func (u Uint128) CmpBytes(v Bytes) int
- func (u Uint128) Div(v Uint128) Uint128
- func (u Uint128) Div64(v uint64) Uint128
- func (u Uint128) DivBytes(v Bytes) Uint128
- func (u Uint128) Equals(v Uint128) bool
- func (u Uint128) Equals64(v uint64) bool
- func (u Uint128) EqualsBytes(v Bytes) bool
- func (u Uint128) IsZero() bool
- func (u Uint128) LeadingZeros() int
- func (u Uint128) Len() int
- func (u Uint128) Lsh(n uint) (s Uint128)
- func (u Uint128) MarshalText() ([]byte, error)
- func (u Uint128) Mod(v Uint128) (r Uint128)
- func (u Uint128) Mod64(v uint64) (r uint64)
- func (u Uint128) ModBytes(v Bytes) (r Uint128)
- func (u Uint128) Mul(v Uint128) Uint128
- func (u Uint128) Mul64(v uint64) Uint128
- func (u Uint128) Mul64Err(v uint64) (Uint128, error)
- func (u Uint128) MulBytes(v Bytes) Uint128
- func (u Uint128) MulBytesErr(v Bytes) (Uint128, error)
- func (u Uint128) MulErr(v Uint128) (Uint128, error)
- func (u Uint128) MulWrap(v Uint128) Uint128
- func (u Uint128) MulWrap64(v uint64) Uint128
- func (u Uint128) MulWrapBytes(v Bytes) Uint128
- func (u Uint128) OnesCount() int
- func (u Uint128) Or(v Uint128) Uint128
- func (u Uint128) Or64(v uint64) Uint128
- func (u Uint128) OrBytes(v Bytes) Uint128
- func (u Uint128) PutBytes(b []byte)
- func (u Uint128) PutBytesBE(b []byte)
- func (u Uint128) QuoRem(v Uint128) (q, r Uint128)
- func (u Uint128) QuoRem64(v uint64) (q Uint128, r uint64)
- func (u Uint128) QuoRemBytes(v Bytes) (q, r Uint128)
- func (u Uint128) Reverse() Uint128
- func (u Uint128) ReverseBytes() Uint128
- func (u Uint128) RotateLeft(k int) Uint128
- func (u Uint128) RotateRight(k int) Uint128
- func (u Uint128) Rsh(n uint) (s Uint128)
- func (u *Uint128) Scan(s fmt.ScanState, ch rune) error
- func (u Uint128) String() string
- func (u Uint128) Sub(v Uint128) Uint128
- func (u Uint128) Sub64(v uint64) Uint128
- func (u Uint128) Sub64Err(v uint64) (Uint128, error)
- func (u Uint128) SubBytes(v Bytes) Uint128
- func (u Uint128) SubBytesErr(v Bytes) (Uint128, error)
- func (u Uint128) SubErr(v Uint128) (Uint128, error)
- func (u Uint128) SubWrap(v Uint128) Uint128
- func (u Uint128) SubWrap64(v uint64) Uint128
- func (u Uint128) SubWrapBytes(v Bytes) Uint128
- func (u Uint128) TrailingZeros() int
- func (u *Uint128) UnmarshalText(b []byte) error
- func (u Uint128) Xor(v Uint128) Uint128
- func (u Uint128) Xor64(v uint64) Uint128
- func (u Uint128) XorBytes(v Bytes) Uint128
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( None = Bytes{0, 0} One = Bytes{1, 0} B = One KB = Bytes(Uint128(B).Mul64(1e3)) // 1e3 MB = Bytes(Uint128(KB).Mul64(1e3)) // 1e6 GB = Bytes(Uint128(MB).Mul64(1e3)) // 1e9 TB = Bytes(Uint128(GB).Mul64(1e3)) // 1e12 PB = Bytes(Uint128(TB).Mul64(1e3)) // 1e15 EB = Bytes(Uint128(PB).Mul64(1e3)) // 1e18 ZB = Bytes(Uint128(EB).Mul64(1e3)) // 1e21 YB = Bytes(Uint128(ZB).Mul64(1e3)) // 1e24 RB = Bytes(Uint128(YB).Mul64(1e3)) // 1e27 QB = Bytes(Uint128(RB).Mul64(1e3)) // 1e30 )
Decimal byte size units (powers of 10).
var ( KiB = Bytes{1024, 0} MiB = Bytes{uint64(math.Pow(1024, 2)), 0} GiB = Bytes{uint64(math.Pow(1024, 3)), 0} TiB = Bytes{uint64(math.Pow(1024, 4)), 0} PiB = Bytes{uint64(math.Pow(1024, 5)), 0} EiB = Bytes{uint64(math.Pow(1024, 6)), 0} // ZB (2^70) and YB (2^80) cannot be represented as a single // uint64, so we use the high bits. // 2^70 = 2^(64+6) = 2^64 * 2^6 = (1 << 6) in the high bits. ZiB = Bytes{0, 1 << 6} // 2^80 = 2^(64+16) = 2^64 * 2^16 = (1 << 16) in the high bits. YiB = Bytes{0, 1 << 16} // 2^90 = 2^(64+26) = 2^64 * 2^26 = (1 << 26) in the high bits. RiB = Bytes{0, 1 << 26} // 2^100 = 2^(64+36) = 2^64 * 2^36 = (1 << 36) in the high bits. QiB = Bytes{0, 1 << 36} )
Binary byte size units (powers of 2).
var ( // DefaultFormatStr is the default format string for formatting byte // sizes, which includes two decimal places and the unit. DefaultFormatStr = "%.2f %s" // DefaultForcedUnitType is the default forced unit for formatting byte // sizes, which is nil to indicate automatic unit selection based on the // value. DefaultForcedUnitType *Bytes // DefaultLongUnits indicates whether to use long unit names, such // as "Megabyte" instead of "MB", though the default is to use short unit // names. DefaultLongUnits = false // DefaultDecimalUnits indicates whether to use decimal (SI) units by default DefaultDecimalUnits = true )
These default options can be overridden by users of this package
var LongBinary = map[Bytes]string{ KiB: "Kibibyte", MiB: "Mebibyte", GiB: "Gibibyte", TiB: "Tebibyte", PiB: "Pebibyte", EiB: "Exbibyte", ZiB: "Zebibyte", YiB: "Yobibyte", RiB: "Ronnibyte", QiB: "Quettibyte", }
LongBinary maps binary byte size units to their long names.
var LongDecimal = map[Bytes]string{ KB: "Kilobyte", MB: "Megabyte", GB: "Gigabyte", TB: "Terabyte", PB: "Petabyte", EB: "Exabyte", ZB: "Zettabyte", YB: "Yottabyte", RB: "Ronnabyte", QB: "Quettabyte", }
LongDecimal maps decimal byte size units to their long names.
var Max = NewUint128(math.MaxUint64, math.MaxUint64)
Max is the largest possible uint128 value.
var ShortBinary = map[Bytes]string{ KiB: "KiB", MiB: "MiB", GiB: "GiB", TiB: "TiB", PiB: "PiB", EiB: "EiB", ZiB: "ZiB", YiB: "YiB", RiB: "RiB", QiB: "QiB", }
ShortBinary maps binary byte size units to their short names.
var ShortDecimal = map[Bytes]string{ KB: "KB", MB: "MB", GB: "GB", TB: "TB", PB: "PB", EB: "EB", ZB: "ZB", YB: "YB", RB: "RB", QB: "QB", }
ShortDecimal maps decimal byte size units to their short names.
var UnitStringToBytes = map[string]Bytes{ "b": B, "kb": KB, "mb": MB, "gb": GB, "tb": TB, "pb": PB, "eb": EB, "zb": ZB, "yb": YB, "rb": RB, "qb": QB, "kib": KiB, "mib": MiB, "gib": GiB, "tib": TiB, "pib": PiB, "eib": EiB, "zib": ZiB, "yib": YiB, "rib": RiB, "qib": QiB, "byte": B, "bytes": B, "kilobyte": KB, "kilobytes": KB, "megabyte": MB, "megabytes": MB, "gigabyte": GB, "gigabytes": GB, "terabyte": TB, "terabytes": TB, "petabyte": PB, "petabytes": PB, "exabyte": EB, "exabytes": EB, "zettabyte": ZB, "zettabytes": ZB, "yottabyte": YB, "yottabytes": YB, "ronnabyte": RB, "ronnabytes": RB, "quettabyte": QB, "quettabytes": QB, "kibibyte": KiB, "kibibytes": KiB, "mebibyte": MiB, "mebibytes": MiB, "gibibyte": GiB, "gibibytes": GiB, "tebibyte": TiB, "tebibytes": TiB, "pebibyte": PiB, "pebibytes": PiB, "exbibyte": EiB, "exbibytes": EiB, "zebibyte": ZiB, "zebibytes": ZiB, "yobibyte": YiB, "yobibytes": YiB, "ronnibyte": RiB, "ronnibytes": RiB, "quettibyte": QiB, "quettibytes": QiB, }
UnitStringToBytes maps valid unit strings to their corresponding Bytes multiplier values for parsing.
var ValidUnits = []string{
"b",
"kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb", "rb", "qb",
"kib", "mib", "gib", "tib", "pib", "eib", "zib", "yib", "rib", "qib",
"byte", "bytes",
"kilobyte", "kilobytes", "megabyte", "megabytes", "gigabyte", "gigabytes", "terabyte", "terabytes", "petabyte", "petabytes",
"exabyte", "exabytes", "zettabyte", "zettabytes", "yottabyte", "yottabytes", "ronnabyte", "ronnabytes", "quettabyte", "quettabytes",
"kibibyte", "kibibytes", "mebibyte", "mebibytes", "gibibyte", "gibibytes", "tebibyte", "tebibytes", "pebibyte", "pebibytes",
"exbibyte", "exbibytes", "zebibyte", "zebibytes", "yobibyte", "yobibytes", "ronnibyte", "ronnibytes", "quettibyte", "quettibytes",
}
ValidUnits lists all supported unit strings for parsing.
Functions ¶
func IsValidUnit ¶
IsValidUnit checks if the provided unit string is a valid unit for parsing byte sizes.
Example ¶
package main
import (
"fmt"
"github.com/beauhoyt/bytesize"
)
func main() {
fmt.Printf("Is 'MB' a valid unit? %v\n", bytesize.IsValidUnit("MB"))
fmt.Printf("Is 'invalid' a valid unit? %v\n", bytesize.IsValidUnit("invalid"))
}
Output: Is 'MB' a valid unit? true Is 'invalid' a valid unit? false
Types ¶
type Bytes ¶
type Bytes Uint128
Bytes represents a byte size as a 128-bit unsigned integer, allowing for very large sizes up to 2^128 - 1 bytes.
Example ¶
package main
import (
"fmt"
"github.com/beauhoyt/bytesize"
)
func main() {
bs := bytesize.Bytes{123456789, 0}
fmt.Printf("%s\n", bs)
}
Output: 123.46 MB
func Parse ¶
Parse parses a string representation of a byte size (e.g., "10 MB", "5.5 GiB", "100 kilobytes", "2.34 Tebibytes") returns the corresponding Bytes value.
Example ¶
package main
import (
"fmt"
"github.com/beauhoyt/bytesize"
)
func main() {
bs, err := bytesize.Parse("123.45 MB")
if err != nil {
fmt.Printf("Error parsing byte size: %v", err)
return
}
fmt.Printf("%s\n", bs)
}
Output: 123.45 MB
func (Bytes) Format ¶
func (b Bytes) Format(opts ...FormatOption) (string, error)
Format formats the Bytes value as a human-readable string using the specified options. It returns the formatted string or an error if any of the options are invalid.
Example ¶
package main
import (
"fmt"
"github.com/beauhoyt/bytesize"
)
func main() {
bs := bytesize.Bytes{1234567890, 0}
formatStr, err := bs.Format(bytesize.WithForcedUnit(bytesize.MiB))
if err != nil {
fmt.Printf("Error formatting byte size: %v\n", err)
return
}
fmt.Printf("%s\n", formatStr)
}
Output: 1177.38 MiB
func (*Bytes) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface for Bytes.
type FormatOption ¶
type FormatOption func(*formatOptions) error
FormatOption defines a functional option for configuring the formatting of byte sizes.
func WithDecimalUnits ¶
func WithDecimalUnits(decimalUnits bool) FormatOption
WithDecimalUnits allows you to specify whether to use decimal (SI) units or binary (IEC) units when formatting byte sizes. If true, it will use decimal units (KB, MB, etc.); if false, it will use binary units (KiB, MiB, etc.).
func WithForcedUnit ¶
func WithForcedUnit(unit Bytes) FormatOption
WithForcedUnit allows you to specify a specific unit to use when formatting byte sizes. If not set, the formatting will automatically choose the most appropriate unit based on the value.
func WithFormatString ¶
func WithFormatString(formatStr string) FormatOption
WithFormatString allows you to specify a custom format string for formatting byte sizes. The format string should include two verbs: one for the value (e.g., %.2f) and one for the unit (e.g., %s).
func WithLongUnits ¶
func WithLongUnits(longUnits bool) FormatOption
WithLongUnits allows you to specify whether to use long unit names (e.g., "Megabyte") or short unit names (e.g., "MB") when formatting byte sizes.
type Uint128 ¶
type Uint128 struct {
Lo, Hi uint64
}
A Uint128 is an unsigned 128-bit number.
var Zero Uint128
Zero is a zero-valued uint128.
func FromBig ¶
FromBig converts i to a Uint128 value. It panics if i is negative or overflows 128 bits.
func FromBigErr ¶ added in v0.1.0
FromBigErr converts i to a Uint128 value, returning an error if i is negative or overflows 128 bits.
func FromBytesBE ¶
FromBytesBE converts big-endian b to a Uint128 value.
func FromString ¶
FromString parses s as a Uint128 value.
func NewUint128 ¶
NewUint128 returns the Uint128 value (lo,hi).
func (Uint128) AddBytesErr ¶ added in v0.3.0
AddBytesErr returns u+v, returning an error on overflow.
func (Uint128) AddWrap ¶
AddWrap returns u+v with wraparound semantics; for example, Max.AddWrap(From64(1)) == Zero.
func (Uint128) AddWrap64 ¶
AddWrap64 returns u+v with wraparound semantics; for example, Max.AddWrap64(1) == Zero.
func (Uint128) AddWrapBytes ¶ added in v0.3.0
AddWrapBytes returns u+v with wraparound semantics; for example, Max.AddWrapBytes(Bytes{1,0})) == Zero.
func (Uint128) AppendBytes ¶
AppendBytes appends u to b in little-endian order and returns the extended buffer.
func (Uint128) AppendBytesBE ¶
AppendBytesBE appends u to b in big-endian order and returns the extended buffer.
func (Uint128) CmpBytes ¶ added in v0.3.0
CmpBytes compares u and v and returns:
-1 if u < v 0 if u == v +1 if u > v
func (Uint128) Equals ¶
Equals returns true if u == v.
Uint128 values can be compared directly with ==, but use of the Equals method is preferred for consistency.
func (Uint128) EqualsBytes ¶ added in v0.3.0
EqualsBytes returns true if u == v.
func (Uint128) LeadingZeros ¶
LeadingZeros returns the number of leading zero bits in u; the result is 128 for u == 0.
func (Uint128) Len ¶
Len returns the minimum number of bits required to represent u; the result is 0 for u == 0.
func (Uint128) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (Uint128) MulBytesErr ¶ added in v0.3.0
MulBytesErr returns u*v, returning an error on overflow.
func (Uint128) MulWrap ¶
MulWrap returns u*v with wraparound semantics; for example, Max.MulWrap(Max) == 1.
func (Uint128) MulWrap64 ¶
MulWrap64 returns u*v with wraparound semantics; for example, Max.MulWrap64(2) == Max.Sub64(1).
func (Uint128) MulWrapBytes ¶ added in v0.3.0
MulWrapBytes returns u*v with wraparound semantics; for example, Max.MulWrapBytes(Bytes{1,0}) == Max.
func (Uint128) PutBytesBE ¶
PutBytesBE stores u in b in big-endian order. It panics if len(ip) < 16.
func (Uint128) QuoRemBytes ¶ added in v0.3.0
QuoRemBytes returns q = u/v and r = u%v.
func (Uint128) ReverseBytes ¶
ReverseBytes returns the value of u with its bytes in reversed order.
func (Uint128) RotateLeft ¶
RotateLeft returns the value of u rotated left by (k mod 128) bits.
func (Uint128) RotateRight ¶
RotateRight returns the value of u rotated left by (k mod 128) bits.
func (Uint128) SubBytesErr ¶ added in v0.3.0
SubBytesErr returns u-v, returning an error on underflow.
func (Uint128) SubWrap ¶
SubWrap returns u-v with wraparound semantics; for example, Zero.SubWrap(From64(1)) == Max.
func (Uint128) SubWrap64 ¶
SubWrap64 returns u-v with wraparound semantics; for example, Zero.SubWrap64(1) == Max.
func (Uint128) SubWrapBytes ¶ added in v0.3.0
SubWrapBytes returns u-v with wraparound semantics; for example, Zero.SubWrapBytes(Bytes{1,0}) == Max.
func (Uint128) TrailingZeros ¶
TrailingZeros returns the number of trailing zero bits in u; the result is 128 for u == 0.
func (*Uint128) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.