2018-10-12 22:25:34 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import "sort"
|
2018-10-12 06:18:26 +00:00
|
|
|
|
2018-10-13 18:43:06 +00:00
|
|
|
const (
|
|
|
|
// Binary SI Byte Units
|
|
|
|
UnitKiB = "KiB"
|
|
|
|
UnitMiB = "MiB"
|
|
|
|
UnitGiB = "GiB"
|
|
|
|
UnitTiB = "TiB"
|
|
|
|
UnitPiB = "PiB"
|
|
|
|
UnitEiB = "EiB"
|
|
|
|
|
|
|
|
// Decimal SI Byte Units
|
|
|
|
UnitkB = "kB"
|
|
|
|
UnitKB = "KB"
|
|
|
|
UnitMB = "MB"
|
|
|
|
UnitGB = "GB"
|
|
|
|
UnitTB = "TB"
|
|
|
|
UnitPB = "PB"
|
|
|
|
UnitEB = "EB"
|
|
|
|
|
|
|
|
// Binary SI Byte Rates
|
|
|
|
UnitKiBPerS = "KiB/s"
|
|
|
|
UnitMiBPerS = "MiB/s"
|
|
|
|
UnitGiBPerS = "GiB/s"
|
|
|
|
UnitTiBPerS = "TiB/s"
|
|
|
|
UnitPiBPerS = "PiB/s"
|
|
|
|
UnitEiBPerS = "EiB/s"
|
|
|
|
|
|
|
|
// Decimal SI Byte Rates
|
|
|
|
UnitkBPerS = "kB/s"
|
|
|
|
UnitKBPerS = "KB/s"
|
|
|
|
UnitMBPerS = "MB/s"
|
|
|
|
UnitGBPerS = "GB/s"
|
|
|
|
UnitTBPerS = "TB/s"
|
|
|
|
UnitPBPerS = "PB/s"
|
|
|
|
UnitEBPerS = "EB/s"
|
|
|
|
|
|
|
|
// Hertz units
|
|
|
|
UnitMHz = "MHz"
|
|
|
|
UnitGHz = "GHz"
|
|
|
|
|
|
|
|
// Watts units
|
|
|
|
UnitmW = "mW"
|
|
|
|
UnitW = "W"
|
|
|
|
UnitkW = "kW"
|
|
|
|
UnitMW = "MW"
|
|
|
|
UnitGW = "GW"
|
|
|
|
)
|
|
|
|
|
2018-10-12 06:18:26 +00:00
|
|
|
var (
|
2018-10-12 22:25:34 +00:00
|
|
|
// numUnits is the number of known units
|
|
|
|
numUnits = len(binarySIBytes) + len(decimalSIBytes) + len(binarySIByteRates) + len(decimalSIByteRates) + len(watts) + len(hertz)
|
|
|
|
|
|
|
|
// UnitIndex is a map of unit name to unit
|
|
|
|
UnitIndex = make(map[string]*Unit, numUnits)
|
|
|
|
|
|
|
|
// lengthSortedUnits is a list of unit names sorted by length with longest
|
|
|
|
// first
|
|
|
|
lengthSortedUnits = make([]string, 0, numUnits)
|
2018-10-12 06:18:26 +00:00
|
|
|
|
|
|
|
binarySIBytes = []*Unit{
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitKiB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: 1 << 10,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitMiB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: 1 << 20,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitGiB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: 1 << 30,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitTiB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: 1 << 40,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitPiB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: 1 << 50,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitEiB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: 1 << 60,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
decimalSIBytes = []*Unit{
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitkB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: Pow(1000, 1),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitKB, // Alternative name for kB
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: Pow(1000, 1),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitMB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: Pow(1000, 2),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitGB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: Pow(1000, 3),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitTB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: Pow(1000, 4),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitPB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: Pow(1000, 5),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitEB,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByte,
|
|
|
|
Multiplier: Pow(1000, 6),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
binarySIByteRates = []*Unit{
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitKiBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: 1 << 10,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitMiBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: 1 << 20,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitGiBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: 1 << 30,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitTiBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: 1 << 40,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitPiBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: 1 << 50,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitEiBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: 1 << 60,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
decimalSIByteRates = []*Unit{
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitkBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: Pow(1000, 1),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitKBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: Pow(1000, 1),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitMBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: Pow(1000, 2),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitGBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: Pow(1000, 3),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitTBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: Pow(1000, 4),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitPBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: Pow(1000, 5),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitEBPerS,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitByteRate,
|
|
|
|
Multiplier: Pow(1000, 6),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
hertz = []*Unit{
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitMHz,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitHertz,
|
2018-10-15 22:15:46 +00:00
|
|
|
Multiplier: Pow(1000, 2),
|
2018-10-12 06:18:26 +00:00
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitGHz,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitHertz,
|
|
|
|
Multiplier: Pow(1000, 3),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
watts = []*Unit{
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitmW,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitWatt,
|
|
|
|
Multiplier: Pow(10, 3),
|
|
|
|
InverseMultiplier: true,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitW,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitWatt,
|
|
|
|
Multiplier: 1,
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitkW,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitWatt,
|
|
|
|
Multiplier: Pow(10, 3),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitMW,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitWatt,
|
|
|
|
Multiplier: Pow(10, 6),
|
|
|
|
},
|
2018-10-12 22:34:21 +00:00
|
|
|
{
|
2018-10-13 18:43:06 +00:00
|
|
|
Name: UnitGW,
|
2018-10-12 06:18:26 +00:00
|
|
|
Base: UnitWatt,
|
|
|
|
Multiplier: Pow(10, 9),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Build the index
|
|
|
|
for _, units := range [][]*Unit{binarySIBytes, decimalSIBytes, binarySIByteRates, decimalSIByteRates, watts, hertz} {
|
|
|
|
for _, unit := range units {
|
|
|
|
UnitIndex[unit.Name] = unit
|
2018-10-12 22:25:34 +00:00
|
|
|
lengthSortedUnits = append(lengthSortedUnits, unit.Name)
|
2018-10-12 06:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 22:25:34 +00:00
|
|
|
|
|
|
|
sort.Slice(lengthSortedUnits, func(i, j int) bool {
|
|
|
|
return len(lengthSortedUnits[i]) >= len(lengthSortedUnits[j])
|
|
|
|
})
|
2018-10-12 06:18:26 +00:00
|
|
|
}
|