Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions bytes/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bytes

import (
"fmt"
"math/big"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -142,27 +143,23 @@ func (*Bytes) ParseBinary(value string) (i int64, err error) {
}
bytesString := parts[1]
multiple := strings.ToUpper(parts[2])
bytes, err := strconv.ParseFloat(bytesString, 64)
if err != nil {
return
}
unit := int64(1)

switch multiple {
case "KI", "KIB":
return int64(bytes * KiB), nil
unit = KiB
case "MI", "MIB":
return int64(bytes * MiB), nil
unit = MiB
case "GI", "GIB":
return int64(bytes * GiB), nil
unit = GiB
case "TI", "TIB":
return int64(bytes * TiB), nil
unit = TiB
case "PI", "PIB":
return int64(bytes * PiB), nil
unit = PiB
case "EI", "EIB":
return int64(bytes * EiB), nil
default:
return int64(bytes), nil
unit = EiB
}
return parseByteCount(bytesString, unit)
}

// ParseDecimal parses human readable bytes string to bytes integer.
Expand All @@ -175,27 +172,37 @@ func (*Bytes) ParseDecimal(value string) (i int64, err error) {
}
bytesString := parts[1]
multiple := strings.ToUpper(parts[2])
bytes, err := strconv.ParseFloat(bytesString, 64)
if err != nil {
return
}
unit := int64(1)

switch multiple {
case "K", "KB":
return int64(bytes * KB), nil
unit = KB
case "M", "MB":
return int64(bytes * MB), nil
unit = MB
case "G", "GB":
return int64(bytes * GB), nil
unit = GB
case "T", "TB":
return int64(bytes * TB), nil
unit = TB
case "P", "PB":
return int64(bytes * PB), nil
unit = PB
case "E", "EB":
return int64(bytes * EB), nil
default:
return int64(bytes), nil
unit = EB
}
return parseByteCount(bytesString, unit)
}

func parseByteCount(value string, unit int64) (int64, error) {
number, ok := new(big.Rat).SetString(value)
if !ok {
return 0, fmt.Errorf("error parsing value=%s", value)
}
number.Mul(number, big.NewRat(unit, 1))
// Truncate fractional bytes only after applying the unit, without float rounding.
whole := new(big.Int).Quo(number.Num(), number.Denom())
if !whole.IsInt64() {
return 0, fmt.Errorf("error parsing value=%s: %w", value, strconv.ErrRange)
}
return whole.Int64(), nil
}

// Format wraps global Bytes's Format function.
Expand Down
43 changes: 43 additions & 0 deletions bytes/parse_precision_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package bytes

import (
"math"
"testing"
)

func TestParseExactByteCounts(t *testing.T) {
for _, tc := range []struct {
input string
want int64
}{
{"1.001KB", 1001},
{"-1.001KB", -1001},
{"9007199254740993B", 9007199254740993},
{"9223372036854775807B", math.MaxInt64},
{"-9223372036854775807B", -math.MaxInt64},
{"-9223372036854775808B", math.MinInt64},
{"0.999999999999999999KiB", 1023},
{"-0.999999999999999999KiB", -1023},
{"010B", 10},
{"12.25KiB", 12544},
{"1.2345KB", 1234},
{"-1.2345KB", -1234},
} {
t.Run(tc.input, func(t *testing.T) {
got, err := Parse(tc.input)
if err != nil || got != tc.want {
t.Fatalf("Parse(%q) = %d, %v; want %d, nil", tc.input, got, err, tc.want)
}
})
}
}

func TestParseRejectsOutOfRangeByteCounts(t *testing.T) {
for _, input := range []string{"9223372036854775808B", "-9223372036854775809B", "8EiB", "10EB"} {
t.Run(input, func(t *testing.T) {
if got, err := Parse(input); err == nil {
t.Fatalf("Parse(%q) = %d, nil; want an out-of-range error", input, got)
}
})
}
}