package humanizeimport ()// IEC Sizes.// kibis of bitsconst (Byte = 1 << (iota * 10)KiByteMiByteGiByteTiBytePiByteEiByte)// SI Sizes.const (IByte = 1KByte = IByte * 1000MByte = KByte * 1000GByte = MByte * 1000TByte = GByte * 1000PByte = TByte * 1000EByte = PByte * 1000)var bytesSizeTable = map[string]uint64{"b": Byte,"kib": KiByte,"kb": KByte,"mib": MiByte,"mb": MByte,"gib": GiByte,"gb": GByte,"tib": TiByte,"tb": TByte,"pib": PiByte,"pb": PByte,"eib": EiByte,"eb": EByte,// Without suffix"": Byte,"ki": KiByte,"k": KByte,"mi": MiByte,"m": MByte,"gi": GiByte,"g": GByte,"ti": TiByte,"t": TByte,"pi": PiByte,"p": PByte,"ei": EiByte,"e": EByte,}func logn(, float64) float64 {returnmath.Log() / math.Log()}func countDigits( int64) int { := 0for != 0 { /= 10 += 1 }return}func humanateBytes( uint64, float64, int, []string) string {if < 10 {returnfmt.Sprintf("%d B", ) } := math.Floor(logn(float64(), )) := [int()] := float64() / math.Pow(, )// Determine how many decimal places to keep based on the unrounded // value's integer-digit count, then round exactly once to that // precision. Rounding first to a fixed precision and then again when // formatting (double rounding) can flip the result at a boundary due // to floating point error (see #103). := countDigits(int64()) := max(-, 0) := math.Pow10() = math.Floor(*+0.5) / // Rounding may have carried into an additional integer digit (e.g. // 9.996 -> 10.0), which means fewer decimal places should be shown // than originally computed.if := countDigits(int64()); > { = max(-(-), 0) } := "%%.%df %%s" := fmt.Sprintf(, )returnfmt.Sprintf(, , )}// Bytes produces a human-readable representation of an SI size.//// See also: ParseBytes.//// Bytes(82854982) -> 83 MBfunc ( uint64) string { := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}returnhumanateBytes(, 1000, 2, )}// BytesN produces a human-readable representation of an SI size.// n specifies the total number of digits to output, including the decimal part.// If n is less than or equal to the number of digits in the integer part, the decimal part will be omitted.//// See also: ParseBytes.//// BytesN(82854982, 3) -> 82.9 MB// BytesN(82854982, 4) -> 82.85 MBfunc ( uint64, int) string { := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}returnhumanateBytes(, 1000, , )}// IBytes produces a human-readable representation of an IEC size.//// See also: ParseBytes.//// IBytes(82854982) -> 79 MiBfunc ( uint64) string { := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}returnhumanateBytes(, 1024, 2, )}// IBytesN produces a human-readable representation of an IEC size.// n specifies the total number of digits to output, including the decimal part.// If n is less than or equal to the number of digits in the integer part, the decimal part will be omitted.//// See also: ParseBytes.//// IBytesN(82854982, 4) -> 79.02 MiB// IBytesN(123456789, 3) -> 118 MiB// IBytesN(123456789, 6) -> 117.738 MiBfunc ( uint64, int) string { := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}returnhumanateBytes(, 1024, , )}// ParseBytes parses a string representation of bytes into the number// of bytes it represents.//// See Also: Bytes, IBytes.//// ParseBytes("42 MB") -> 42000000, nil// ParseBytes("42 mib") -> 44040192, nilfunc ( string) (uint64, error) { := 0 := falsefor , := range {if !(unicode.IsDigit() || == '.' || == ',') {break }if == ',' { = true } ++ } := [:]if { = strings.Replace(, ",", "", -1) } , := strconv.ParseFloat(, 64)if != nil {return0, } := strings.ToLower(strings.TrimSpace([:]))if , := bytesSizeTable[]; {// Parse whole-number inputs exactly. A float64 cannot represent // integers above 2^53, and the multiplication below would also reject // otherwise-valid values near math.MaxUint64, so route integers through // uint64 arithmetic with an overflow check.if !strings.ContainsRune(, '.') {if , := strconv.ParseUint(, 10, 64); == nil {if , := bits.Mul64(, ); == 0 {return , nil }return0, fmt.Errorf("too large: %v", ) } } *= float64()if >= math.MaxUint64 {return0, fmt.Errorf("too large: %v", ) }returnuint64(), nil }return0, fmt.Errorf("unhandled size name: %v", )}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.