|
| 1 | +package bytes |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +type ( |
| 11 | + // Bytes struct |
| 12 | + Bytes struct{} |
| 13 | +) |
| 14 | + |
| 15 | +// binary units (IEC 60027) |
| 16 | +const ( |
| 17 | + _ = 1.0 << (10 * iota) // ignore first value by assigning to blank identifier |
| 18 | + KiB |
| 19 | + MiB |
| 20 | + GiB |
| 21 | + TiB |
| 22 | + PiB |
| 23 | + EiB |
| 24 | +) |
| 25 | + |
| 26 | +// decimal units (SI international system of units) |
| 27 | +const ( |
| 28 | + KB = 1000 |
| 29 | + MB = KB * 1000 |
| 30 | + GB = MB * 1000 |
| 31 | + TB = GB * 1000 |
| 32 | + PB = TB * 1000 |
| 33 | + EB = PB * 1000 |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + patternBinary = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]iB?)$`) |
| 38 | + patternDecimal = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]B?|B?)$`) |
| 39 | + global = New() |
| 40 | +) |
| 41 | + |
| 42 | +// New creates a Bytes instance. |
| 43 | +func New() *Bytes { |
| 44 | + return &Bytes{} |
| 45 | +} |
| 46 | + |
| 47 | +// Format formats bytes integer to human readable string according to IEC 60027. |
| 48 | +// For example, 31323 bytes will return 30.59KB. |
| 49 | +func (b *Bytes) Format(value int64) string { |
| 50 | + return b.FormatBinary(value) |
| 51 | +} |
| 52 | + |
| 53 | +// FormatBinary formats bytes integer to human readable string according to IEC 60027. |
| 54 | +// For example, 31323 bytes will return 30.59KB. |
| 55 | +func (*Bytes) FormatBinary(value int64) string { |
| 56 | + multiple := "" |
| 57 | + val := float64(value) |
| 58 | + |
| 59 | + switch { |
| 60 | + case value >= EiB: |
| 61 | + val /= EiB |
| 62 | + multiple = "EiB" |
| 63 | + case value >= PiB: |
| 64 | + val /= PiB |
| 65 | + multiple = "PiB" |
| 66 | + case value >= TiB: |
| 67 | + val /= TiB |
| 68 | + multiple = "TiB" |
| 69 | + case value >= GiB: |
| 70 | + val /= GiB |
| 71 | + multiple = "GiB" |
| 72 | + case value >= MiB: |
| 73 | + val /= MiB |
| 74 | + multiple = "MiB" |
| 75 | + case value >= KiB: |
| 76 | + val /= KiB |
| 77 | + multiple = "KiB" |
| 78 | + case value == 0: |
| 79 | + return "0" |
| 80 | + default: |
| 81 | + return strconv.FormatInt(value, 10) + "B" |
| 82 | + } |
| 83 | + |
| 84 | + return fmt.Sprintf("%.2f%s", val, multiple) |
| 85 | +} |
| 86 | + |
| 87 | +// FormatDecimal formats bytes integer to human readable string according to SI international system of units. |
| 88 | +// For example, 31323 bytes will return 31.32KB. |
| 89 | +func (*Bytes) FormatDecimal(value int64) string { |
| 90 | + multiple := "" |
| 91 | + val := float64(value) |
| 92 | + |
| 93 | + switch { |
| 94 | + case value >= EB: |
| 95 | + val /= EB |
| 96 | + multiple = "EB" |
| 97 | + case value >= PB: |
| 98 | + val /= PB |
| 99 | + multiple = "PB" |
| 100 | + case value >= TB: |
| 101 | + val /= TB |
| 102 | + multiple = "TB" |
| 103 | + case value >= GB: |
| 104 | + val /= GB |
| 105 | + multiple = "GB" |
| 106 | + case value >= MB: |
| 107 | + val /= MB |
| 108 | + multiple = "MB" |
| 109 | + case value >= KB: |
| 110 | + val /= KB |
| 111 | + multiple = "KB" |
| 112 | + case value == 0: |
| 113 | + return "0" |
| 114 | + default: |
| 115 | + return strconv.FormatInt(value, 10) + "B" |
| 116 | + } |
| 117 | + |
| 118 | + return fmt.Sprintf("%.2f%s", val, multiple) |
| 119 | +} |
| 120 | + |
| 121 | +// Parse parses human readable bytes string to bytes integer. |
| 122 | +// For example, 6GiB (6Gi is also valid) will return 6442450944, and |
| 123 | +// 6GB (6G is also valid) will return 6000000000. |
| 124 | +func (b *Bytes) Parse(value string) (int64, error) { |
| 125 | + |
| 126 | + i, err := b.ParseBinary(value) |
| 127 | + if err == nil { |
| 128 | + return i, err |
| 129 | + } |
| 130 | + |
| 131 | + return b.ParseDecimal(value) |
| 132 | +} |
| 133 | + |
| 134 | +// ParseBinary parses human readable bytes string to bytes integer. |
| 135 | +// For example, 6GiB (6Gi is also valid) will return 6442450944. |
| 136 | +func (*Bytes) ParseBinary(value string) (i int64, err error) { |
| 137 | + parts := patternBinary.FindStringSubmatch(value) |
| 138 | + if len(parts) < 3 { |
| 139 | + return 0, fmt.Errorf("error parsing value=%s", value) |
| 140 | + } |
| 141 | + bytesString := parts[1] |
| 142 | + multiple := strings.ToUpper(parts[2]) |
| 143 | + bytes, err := strconv.ParseFloat(bytesString, 64) |
| 144 | + if err != nil { |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + switch multiple { |
| 149 | + case "KI", "KIB": |
| 150 | + return int64(bytes * KiB), nil |
| 151 | + case "MI", "MIB": |
| 152 | + return int64(bytes * MiB), nil |
| 153 | + case "GI", "GIB": |
| 154 | + return int64(bytes * GiB), nil |
| 155 | + case "TI", "TIB": |
| 156 | + return int64(bytes * TiB), nil |
| 157 | + case "PI", "PIB": |
| 158 | + return int64(bytes * PiB), nil |
| 159 | + case "EI", "EIB": |
| 160 | + return int64(bytes * EiB), nil |
| 161 | + default: |
| 162 | + return int64(bytes), nil |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// ParseDecimal parses human readable bytes string to bytes integer. |
| 167 | +// For example, 6GB (6G is also valid) will return 6000000000. |
| 168 | +func (*Bytes) ParseDecimal(value string) (i int64, err error) { |
| 169 | + parts := patternDecimal.FindStringSubmatch(value) |
| 170 | + if len(parts) < 3 { |
| 171 | + return 0, fmt.Errorf("error parsing value=%s", value) |
| 172 | + } |
| 173 | + bytesString := parts[1] |
| 174 | + multiple := strings.ToUpper(parts[2]) |
| 175 | + bytes, err := strconv.ParseFloat(bytesString, 64) |
| 176 | + if err != nil { |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + switch multiple { |
| 181 | + case "K", "KB": |
| 182 | + return int64(bytes * KB), nil |
| 183 | + case "M", "MB": |
| 184 | + return int64(bytes * MB), nil |
| 185 | + case "G", "GB": |
| 186 | + return int64(bytes * GB), nil |
| 187 | + case "T", "TB": |
| 188 | + return int64(bytes * TB), nil |
| 189 | + case "P", "PB": |
| 190 | + return int64(bytes * PB), nil |
| 191 | + case "E", "EB": |
| 192 | + return int64(bytes * EB), nil |
| 193 | + default: |
| 194 | + return int64(bytes), nil |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +// Format wraps global Bytes's Format function. |
| 199 | +func Format(value int64) string { |
| 200 | + return global.Format(value) |
| 201 | +} |
| 202 | + |
| 203 | +// FormatBinary wraps global Bytes's FormatBinary function. |
| 204 | +func FormatBinary(value int64) string { |
| 205 | + return global.FormatBinary(value) |
| 206 | +} |
| 207 | + |
| 208 | +// FormatDecimal wraps global Bytes's FormatDecimal function. |
| 209 | +func FormatDecimal(value int64) string { |
| 210 | + return global.FormatDecimal(value) |
| 211 | +} |
| 212 | + |
| 213 | +// Parse wraps global Bytes's Parse function. |
| 214 | +func Parse(value string) (int64, error) { |
| 215 | + return global.Parse(value) |
| 216 | +} |
0 commit comments