| 1 | package humanize |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | "unicode" |
| 10 | |
| 11 | "github.com/git-lfs/git-lfs/v3/errors" |
| 12 | "github.com/git-lfs/git-lfs/v3/tr" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | Byte = 1 << (iota * 10) |
| 17 | Kibibyte |
| 18 | Mebibyte |
| 19 | Gibibyte |
| 20 | Tebibyte |
| 21 | Pebibyte |
| 22 | |
| 23 | Kilobyte = 1000 * Byte |
| 24 | Megabyte = 1000 * Kilobyte |
| 25 | Gigabyte = 1000 * Megabyte |
| 26 | Terabyte = 1000 * Gigabyte |
| 27 | Petabyte = 1000 * Terabyte |
| 28 | |
| 29 | // eps is the machine epsilon, or a 64-bit floating point value |
| 30 | // reasonably close to zero. |
| 31 | eps float64 = 7.0/3 - 4.0/3 - 1.0 |
| 32 | ) |
| 33 | |
| 34 | var bytesTable = map[string]uint64{ |
| 35 | "": Byte, |
| 36 | "b": Byte, |
| 37 | |
| 38 | "kib": Kibibyte, |
| 39 | "mib": Mebibyte, |
| 40 | "gib": Gibibyte, |
| 41 | "tib": Tebibyte, |
| 42 | "pib": Pebibyte, |
| 43 | |
| 44 | "kb": Kilobyte, |
| 45 | "mb": Megabyte, |
| 46 | "gb": Gigabyte, |
| 47 | "tb": Terabyte, |
| 48 | "pb": Petabyte, |
| 49 | } |
| 50 | |
| 51 | // ParseBytes parses a given human-readable bytes or ibytes string into a number |
| 52 | // of bytes, or an error if the string was unable to be parsed. |
| 53 | func ParseBytes(str string) (uint64, error) { |
| 54 | var sep int |
| 55 | for _, r := range str { |
| 56 | if !(unicode.IsDigit(r) || r == '.' || r == ',') { |
| 57 | break |
| 58 | } |
| 59 | |
| 60 | sep = sep + 1 |
| 61 | } |
| 62 | |
| 63 | var f float64 |
| 64 | |
| 65 | if s := strings.Replace(str[:sep], ",", "", -1); len(s) > 0 { |
| 66 | var err error |
| 67 | |
| 68 | f, err = strconv.ParseFloat(s, 64) |
| 69 | if err != nil { |
| 70 | return 0, err |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | m, err := ParseByteUnit(str[sep:]) |
| 75 | if err != nil { |
| 76 | return 0, err |
| 77 | } |
| 78 | |
| 79 | f = f * float64(m) |
| 80 | if f >= math.MaxUint64 { |
| 81 | return 0, errors.New(tr.Tr.Get("number of bytes too large")) |
| 82 | } |
| 83 | return uint64(f), nil |
| 84 | } |
| 85 | |
| 86 | // ParseByteUnit returns the number of bytes in a given unit of storage, or an |
| 87 | // error, if that unit is unrecognized. |
| 88 | func ParseByteUnit(str string) (uint64, error) { |
| 89 | str = strings.TrimSpace(str) |
| 90 | str = strings.ToLower(str) |
| 91 | |
| 92 | if u, ok := bytesTable[str]; ok { |
| 93 | return u, nil |
| 94 | } |
| 95 | return 0, errors.New(tr.Tr.Get("unknown unit: %q", str)) |
| 96 | } |
| 97 | |
| 98 | var sizes = []string{"B", "KB", "MB", "GB", "TB", "PB"} |
| 99 | |
| 100 | // FormatBytes outputs the given number of bytes "s" as a human-readable string, |
| 101 | // rounding to the nearest half within .01. |
| 102 | func FormatBytes(s uint64) string { |
| 103 | var e float64 |
| 104 | if s == 0 { |
| 105 | e = 0 |
| 106 | } else { |
| 107 | e = math.Floor(log(float64(s), 1000)) |
| 108 | } |
| 109 | |
| 110 | unit := uint64(math.Pow(1000, e)) |
| 111 | suffix := sizes[int(e)] |
| 112 | |
| 113 | return fmt.Sprintf("%s %s", |
| 114 | FormatBytesUnit(s, unit), suffix) |
| 115 | } |
| 116 | |
| 117 | // FormatBytesUnit outputs the given number of bytes "s" as a quantity of the |
| 118 | // given units "u" to the nearest half within .01. |
| 119 | func FormatBytesUnit(s, u uint64) string { |
| 120 | var rounded float64 |
| 121 | if s < 10 { |
| 122 | rounded = float64(s) |
| 123 | } else { |
| 124 | rounded = math.Floor(float64(s)/float64(u)*10+.5) / 10 |
| 125 | } |
| 126 | |
| 127 | format := "%.0f" |
| 128 | if rounded < 10 && u > 1 { |
| 129 | format = "%.1f" |
| 130 | } |
| 131 | |
| 132 | return fmt.Sprintf(format, rounded) |
| 133 | } |
| 134 | |
| 135 | // FormatByteRate outputs the given rate of transfer "r" as the quotient of "s" |
| 136 | // (the number of bytes transferred) over "d" (the duration of time that those |
| 137 | // bytes were transferred in). |
| 138 | // |
| 139 | // It displays the output as a quantity of a "per-unit-time" unit (i.e., B/s, |
| 140 | // MiB/s) in the most representative fashion possible, as above. |
| 141 | func FormatByteRate(s uint64, d time.Duration) string { |
| 142 | // e is the index of the most representative unit of storage. |
| 143 | var e float64 |
| 144 | |
| 145 | // f is the floating-point equivalent of "s", so as to avoid more |
| 146 | // conversions than necessary. |
| 147 | f := float64(s) |
| 148 | |
| 149 | if f != 0 { |
| 150 | f = f / math.Max(time.Nanosecond.Seconds(), d.Seconds()) |
| 151 | e = math.Floor(log(f, 1000)) |
| 152 | if e <= eps { |
| 153 | // The result of math.Floor(log(r, 1000)) can be |
| 154 | // "close-enough" to zero that it should be effectively |
| 155 | // considered zero. |
| 156 | e = 0 |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | unit := uint64(math.Pow(1000, e)) |
| 161 | suffix := sizes[int(e)] |
| 162 | |
| 163 | return fmt.Sprintf("%s %s/s", |
| 164 | FormatBytesUnit(uint64(math.Ceil(f)), unit), suffix) |
| 165 | } |
| 166 | |
| 167 | // log takes the log base "b" of "n" (\log_b{n}) |
| 168 | func log(n, b float64) float64 { |
| 169 | return math.Log(n) / math.Log(b) |
| 170 | } |