ParseBinary parses human readable bytes string to bytes integer. For example, 6GiB (6Gi is also valid) will return 6442450944.
(value string)
| 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. |