HumanReadableBytes converts a byte count into a human-readable string, e.g. 2.5 MiB there are about a zillion golang packages for formatting bytes as human-readable values, the top search result of which is https://pkg.go.dev/github.com/dustin/go-humanize. However, this package is large and does too
(b int64)
| 237 | // https://pkg.go.dev/github.com/dustin/go-humanize. However, this package is large and does too much, there's no need to use it |
| 238 | // when we can use this trivial tutorial one instead: https://programming.guide/go/formatting-byte-size-to-human-readable-format.html |
| 239 | func HumanReadableBytes(b int64) string { |
| 240 | const unit = 1024 |
| 241 | if b < unit { |
| 242 | return fmt.Sprintf("%d B", b) |
| 243 | } |
| 244 | div, exp := int64(unit), 0 |
| 245 | for n := b / unit; n >= unit; n /= unit { |
| 246 | div *= unit |
| 247 | exp++ |
| 248 | } |
| 249 | return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp]) |
| 250 | } |
| 251 | |
| 252 | // SplitString splits the input string into components based on delimiter characters. |
| 253 | // we want to pick up empty entries here; so "::5" and ":pterm:5" should both return THREE components, rather than one or two |
no outgoing calls
no test coverage detected