Name change, the official usage is prefix

This commit is contained in:
Thomas Roehl 2022-03-10 14:51:27 +01:00
parent c9fb8ca327
commit 230a21513d
3 changed files with 26 additions and 26 deletions

View File

@ -2,28 +2,28 @@ package ccunits
import "regexp"
type Scale float64
type Prefix float64
const (
Base Scale = iota
Peta = 1e15
Tera = 1e12
Giga = 1e9
Mega = 1e6
Kilo = 1e3
Milli = 1e-3
Micro = 1e-6
Nano = 1e-9
Kibi = 1024
Mebi = 1024 * 1024
Gibi = 1024 * 1024 * 1024
Tebi = 1024 * 1024 * 1024 * 1024
Base Prefix = iota
Peta = 1e15
Tera = 1e12
Giga = 1e9
Mega = 1e6
Kilo = 1e3
Milli = 1e-3
Micro = 1e-6
Nano = 1e-9
Kibi = 1024
Mebi = 1024 * 1024
Gibi = 1024 * 1024 * 1024
Tebi = 1024 * 1024 * 1024 * 1024
)
const prefixRegexStr = `^([kKmMgGtTpP]?[i]?)(.*)`
var prefixRegex = regexp.MustCompile(prefixRegexStr)
func (s *Scale) String() string {
func (s *Prefix) String() string {
switch *s {
case Base:
return ""
@ -56,7 +56,7 @@ func (s *Scale) String() string {
}
}
func (s *Scale) Prefix() string {
func (s *Prefix) Prefix() string {
switch *s {
case Base:
return ""
@ -89,7 +89,7 @@ func (s *Scale) Prefix() string {
}
}
func NewScale(prefix string) Scale {
func NewPrefix(prefix string) Prefix {
switch prefix {
case "k":
return Kilo

View File

@ -6,7 +6,7 @@ import (
)
type Unit struct {
scale Scale
scale Prefix
measure Measure
divMeasure Measure
}
@ -31,7 +31,7 @@ func (u *Unit) AddDivisorUnit(div Measure) {
u.divMeasure = div
}
func GetScaleFactor(in Scale, out Scale) float64 {
func GetPrefixFactor(in Prefix, out Prefix) float64 {
var factor = 1.0
var in_scale = 1.0
var out_scale = 1.0
@ -45,11 +45,11 @@ func GetScaleFactor(in Scale, out Scale) float64 {
return factor
}
func GetUnitScaleFactor(in Unit, out Unit) (float64, error) {
func GetUnitPrefixFactor(in Unit, out Unit) (float64, error) {
if in.measure != out.measure || in.divMeasure != out.divMeasure {
return 1.0, fmt.Errorf("invalid measures in in and out Unit")
}
return GetScaleFactor(in.scale, out.scale), nil
return GetPrefixFactor(in.scale, out.scale), nil
}
func NewUnit(unit string) Unit {
@ -60,7 +60,7 @@ func NewUnit(unit string) Unit {
}
matches := prefixRegex.FindStringSubmatch(unit)
if len(matches) > 2 {
u.scale = NewScale(matches[1])
u.scale = NewPrefix(matches[1])
measures := strings.Split(matches[2], "/")
u.measure = NewMeasure(measures[0])
// Special case for 'm' as scale for Bytes as thers is nothing like MilliBytes

View File

@ -55,7 +55,7 @@ func TestUnitsExact(t *testing.T) {
}
}
func TestUnitsDifferentScale(t *testing.T) {
func TestUnitsDifferentPrefix(t *testing.T) {
testCases := []struct {
in string
want Unit
@ -71,9 +71,9 @@ func TestUnitsDifferentScale(t *testing.T) {
{"Mib", NewUnit("MBytes"), (1024 * 1024.0) / (1e6)},
{"mb", NewUnit("MBytes"), 1.0},
}
compareUnitWithScale := func(in, out Unit, factor float64) bool {
compareUnitWithPrefix := func(in, out Unit, factor float64) bool {
if in.measure == out.measure && in.divMeasure == out.divMeasure {
if f := GetScaleFactor(in.scale, out.scale); f == factor {
if f := GetPrefixFactor(in.scale, out.scale); f == factor {
return true
} else {
fmt.Println(f)
@ -83,7 +83,7 @@ func TestUnitsDifferentScale(t *testing.T) {
}
for _, c := range testCases {
u := NewUnit(c.in)
if !compareUnitWithScale(u, c.want, c.scaleFactor) {
if !compareUnitWithPrefix(u, c.want, c.scaleFactor) {
t.Errorf("func NewUnit(%q) == %q, want %q with factor %f", c.in, u.String(), c.want.String(), c.scaleFactor)
}
}