Adds HumanBytes(bytes uint64) string, which writes a byte count the way a
person reads it: "0 B", "1023 B", "1.0 KiB", "1.5 MiB", "16.0 EiB".
This belongs here because every program that touches files, downloads or memory
eventually prints a size, and it sits naturally beside the existing TimeDiffHuman, which does the same job for durations.
Things to know:
The units are powers of 1024 with the "KiB" style names, not powers of 1000
with "kB". That is the honest labelling for the arithmetic being done. A
caller that wants disk-vendor units would need a separate function.
There is a second, easily missed step after the unit is chosen: the value is
checked against the threshold once more after rounding, because 1048575 bytes
divides to 1023.999 KiB, which would otherwise print as "1024.0 KiB" instead
of "1.0 MiB". There is a test for exactly that value.
The largest uint64 is "16.0 EiB", so the loop cannot run past the last unit.
The code is in a new file, humanbytes.go, so that the ten proposal branches
do not all conflict in the same place.
make test on this branch reports one failure, TestNowUnixMicro. That test
already fails on master and is unrelated to this change.
Model: opus-5
Adds `HumanBytes(bytes uint64) string`, which writes a byte count the way a
person reads it: "0 B", "1023 B", "1.0 KiB", "1.5 MiB", "16.0 EiB".
This belongs here because every program that touches files, downloads or memory
eventually prints a size, and it sits naturally beside the existing
`TimeDiffHuman`, which does the same job for durations.
Things to know:
- The units are powers of 1024 with the "KiB" style names, not powers of 1000
with "kB". That is the honest labelling for the arithmetic being done. A
caller that wants disk-vendor units would need a separate function.
- There is a second, easily missed step after the unit is chosen: the value is
checked against the threshold once more after rounding, because 1048575 bytes
divides to 1023.999 KiB, which would otherwise print as "1024.0 KiB" instead
of "1.0 MiB". There is a test for exactly that value.
- The largest `uint64` is "16.0 EiB", so the loop cannot run past the last unit.
- The code is in a new file, `humanbytes.go`, so that the ten proposal branches
do not all conflict in the same place.
- `make test` on this branch reports one failure, `TestNowUnixMicro`. That test
already fails on `master` and is unrelated to this change.
Model: opus-5
clawbot
self-assigned this 2026-09-05 05:27:55 +02:00
HumanBytes turns a byte count into a short string using powers of 1024, with
whole bytes below the first threshold and one decimal place above it. Comes
with a doc comment and table-driven tests. (closes#11)
Model: opus-5
sneak
merged commit 64965b11ed into master2026-09-05 05:44:38 +02:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Adds
HumanBytes(bytes uint64) string, which writes a byte count the way aperson reads it: "0 B", "1023 B", "1.0 KiB", "1.5 MiB", "16.0 EiB".
This belongs here because every program that touches files, downloads or memory
eventually prints a size, and it sits naturally beside the existing
TimeDiffHuman, which does the same job for durations.Things to know:
with "kB". That is the honest labelling for the arithmetic being done. A
caller that wants disk-vendor units would need a separate function.
checked against the threshold once more after rounding, because 1048575 bytes
divides to 1023.999 KiB, which would otherwise print as "1024.0 KiB" instead
of "1.0 MiB". There is a test for exactly that value.
uint64is "16.0 EiB", so the loop cannot run past the last unit.humanbytes.go, so that the ten proposal branchesdo not all conflict in the same place.
make teston this branch reports one failure,TestNowUnixMicro. That testalready fails on
masterand is unrelated to this change.Model: opus-5