diff options
| author | Runxi Yu <me@runxiyu.org> | 2025-08-12 11:01:07 +0800 |
|---|---|---|
| committer | Runxi Yu <me@runxiyu.org> | 2025-09-26 17:25:37 +0800 |
| commit | e238ccb06b05e88ec323ca95ad4600bd79743372 (patch) | |
| tree | 46f82d4d0f4ec43e94dcde5677de1ba857862638 /forged/internal/humanize/bytes.go | |
| parent | Remove forge-specific functions from misc (diff) | |
| download | forge-e238ccb06b05e88ec323ca95ad4600bd79743372.tar.gz forge-e238ccb06b05e88ec323ca95ad4600bd79743372.tar.zst forge-e238ccb06b05e88ec323ca95ad4600bd79743372.zip | |
Refactor
Diffstat (limited to 'forged/internal/humanize/bytes.go')
| -rw-r--r-- | forged/internal/humanize/bytes.go | 35 |
1 files changed, 0 insertions, 35 deletions
diff --git a/forged/internal/humanize/bytes.go b/forged/internal/humanize/bytes.go deleted file mode 100644 index bea504c..0000000 --- a/forged/internal/humanize/bytes.go +++ /dev/null @@ -1,35 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2005-2008 Dustin Sallings <dustin@spy.net> -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -// Package humanize provides functions to convert numbers into human-readable formats. -package humanize - -import ( - "fmt" - "math" -) - -// IBytes produces a human readable representation of an IEC size. -func IBytes(s uint64) string { - sizes := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"} - return humanateBytes(s, 1024, sizes) -} - -func humanateBytes(s uint64, base float64, sizes []string) string { - if s < 10 { - return fmt.Sprintf("%d B", s) - } - e := math.Floor(logn(float64(s), base)) - suffix := sizes[int(e)] - val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10 - f := "%.0f %s" - if val < 10 { - f = "%.1f %s" - } - - return fmt.Sprintf(f, val, suffix) -} - -func logn(n, b float64) float64 { - return math.Log(n) / math.Log(b) -} |
