From 6dfb3140ba3535db1e89e75ad2dfad0080fc18c1 Mon Sep 17 00:00:00 2001 From: Twiink Date: Sat, 21 Nov 2026 16:53:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(bytes):=20=E7=B2=BE=E7=A1=AE=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=AD=97=E8=8A=82=E6=95=B0=E9=87=8F=E5=B9=B6=E6=8B=92?= =?UTF-8?q?=E7=BB=9D=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bytes/bytes.go | 55 ++++++++++++++++++++--------------- bytes/parse_precision_test.go | 43 +++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 bytes/parse_precision_test.go diff --git a/bytes/bytes.go b/bytes/bytes.go index c8bb03b..2811b19 100644 --- a/bytes/bytes.go +++ b/bytes/bytes.go @@ -2,6 +2,7 @@ package bytes import ( "fmt" + "math/big" "regexp" "strconv" "strings" @@ -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. @@ -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. diff --git a/bytes/parse_precision_test.go b/bytes/parse_precision_test.go new file mode 100644 index 0000000..e13d395 --- /dev/null +++ b/bytes/parse_precision_test.go @@ -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) + } + }) + } +}