size.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package units
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. // See: http://en.wikipedia.org/wiki/Binary_prefix
  9. const (
  10. // Decimal
  11. KB = 1000
  12. MB = 1000 * KB
  13. GB = 1000 * MB
  14. TB = 1000 * GB
  15. PB = 1000 * TB
  16. // Binary
  17. KiB = 1024
  18. MiB = 1024 * KiB
  19. GiB = 1024 * MiB
  20. TiB = 1024 * GiB
  21. PiB = 1024 * TiB
  22. )
  23. type unitMap map[string]int64
  24. var (
  25. decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
  26. binaryMap = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
  27. sizeRegex = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`)
  28. )
  29. var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
  30. var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
  31. // CustomSize returns a human-readable approximation of a size
  32. // using custom format.
  33. func CustomSize(format string, size float64, base float64, _map []string) string {
  34. i := 0
  35. for size >= base {
  36. size = size / base
  37. i++
  38. }
  39. return fmt.Sprintf(format, size, _map[i])
  40. }
  41. // HumanSize returns a human-readable approximation of a size
  42. // capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
  43. func HumanSize(size float64) string {
  44. return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs)
  45. }
  46. // BytesSize returns a human-readable size in bytes, kibibytes,
  47. // mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").
  48. func BytesSize(size float64) string {
  49. return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs)
  50. }
  51. // FromHumanSize returns an integer from a human-readable specification of a
  52. // size using SI standard (eg. "44kB", "17MB").
  53. func FromHumanSize(size string) (int64, error) {
  54. return parseSize(size, decimalMap)
  55. }
  56. // RAMInBytes parses a human-readable string representing an amount of RAM
  57. // in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
  58. // returns the number of bytes, or -1 if the string is unparseable.
  59. // Units are case-insensitive, and the 'b' suffix is optional.
  60. func RAMInBytes(size string) (int64, error) {
  61. return parseSize(size, binaryMap)
  62. }
  63. // Parses the human-readable size string into the amount it represents.
  64. func parseSize(sizeStr string, uMap unitMap) (int64, error) {
  65. matches := sizeRegex.FindStringSubmatch(sizeStr)
  66. if len(matches) != 3 {
  67. return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
  68. }
  69. size, err := strconv.ParseInt(matches[1], 10, 0)
  70. if err != nil {
  71. return -1, err
  72. }
  73. unitPrefix := strings.ToLower(matches[2])
  74. if mul, ok := uMap[unitPrefix]; ok {
  75. size *= mul
  76. }
  77. return size, nil
  78. }