File: humanbytes.go

package info (click to toggle)
pk4 5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 1,016 kB
  • sloc: sh: 146; makefile: 7
file content (68 lines) | stat: -rw-r--r-- 1,308 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package humanbytes

import (
	"fmt"
	"strconv"
	"strings"
)

var (
	suffixes = map[string]int64{
		"B": 0,
		"K": 1024,
		"M": 1024 * 1024,
		"G": 1024 * 1024 * 1024,
		"T": 1024 * 1024 * 1024 * 1024,
		"P": 1024 * 1024 * 1024 * 1024 * 1024,

		"KiB": 1024,
		"MiB": 1024 * 1024,
		"GiB": 1024 * 1024 * 1024,
		"TiB": 1024 * 1024 * 1024 * 1024,
		"PiB": 1024 * 1024 * 1024 * 1024 * 1024,

		"KB": 1000,
		"MB": 1000 * 1000,
		"GB": 1000 * 1000 * 1000,
		"TB": 1000 * 1000 * 1000 * 1000,
		"PB": 1000 * 1000 * 1000 * 1000 * 1000,
	}

	suffixOrder = []string{
		"P", "PiB", "PB",
		"T", "TiB", "TB",
		"G", "GiB", "GB",
		"M", "MiB", "MB",
		"K", "KiB", "KB",
		"B"}
)

func Format(b int64) string {
	for _, suffix := range suffixOrder {
		divisor := suffixes[suffix]
		if b < divisor {
			continue
		}

		if suffix == "B" {
			return fmt.Sprintf("%dB", b)
		}
		return fmt.Sprintf("%.2f%s", float64(b)/float64(divisor), suffix)
	}
	return fmt.Sprintf("%dB", b)
}

func Parse(s string) (int64, error) {
	for suffix, factor := range suffixes {
		if !strings.HasSuffix(s, suffix) {
			continue
		}
		b, err := strconv.ParseInt(strings.TrimSuffix(s, suffix), 0, 64)
		if err != nil {
			return 0, err
		}
		return b * factor, nil
	}
	// No suffix: try parsing as a byte value
	return strconv.ParseInt(s, 0, 64)
}