MCPcopy Create free account
hub / github.com/prometheus/procfs / parseHumanReadableBytes

Function parseHumanReadableBytes

bcachefs/get.go:175–209  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

173}
174
175func parseHumanReadableBytes(s string) (uint64, error) {
176 s = strings.TrimSpace(s)
177 if s == "" {
178 return 0, fmt.Errorf("empty string")
179 }
180
181 multiplier := float64(1)
182 lastChar := s[len(s)-1]
183 switch lastChar {
184 case 'k', 'K':
185 multiplier = 1024
186 s = s[:len(s)-1]
187 case 'm', 'M':
188 multiplier = 1024 * 1024
189 s = s[:len(s)-1]
190 case 'g', 'G':
191 multiplier = 1024 * 1024 * 1024
192 s = s[:len(s)-1]
193 case 't', 'T':
194 multiplier = 1024 * 1024 * 1024 * 1024
195 s = s[:len(s)-1]
196 case 'p', 'P':
197 multiplier = 1024 * 1024 * 1024 * 1024 * 1024
198 s = s[:len(s)-1]
199 case 'e', 'E':
200 multiplier = 1024 * 1024 * 1024 * 1024 * 1024 * 1024
201 s = s[:len(s)-1]
202 }
203
204 value, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
205 if err != nil {
206 return 0, err
207 }
208 return uint64(value * multiplier), nil
209}
210
211func parseCompressionStats(path string) (map[string]CompressionStats, error) {
212 file, err := openIfExists(path)

Callers 7

readHumanBytesMethod · 0.85
parseCompressionStatsFunction · 0.85
parseCounterFileFunction · 0.85
parseBtreeWriteStatsFunction · 0.85
parseDevicesFunction · 0.85

Calls

no outgoing calls

Tested by 2