gytes.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package gytes
  2. import (
  3. "fmt"
  4. "math"
  5. "strconv"
  6. )
  7. var (
  8. global = New()
  9. )
  10. type (
  11. Gytes struct {
  12. iec bool
  13. }
  14. )
  15. // New creates a Gytes instance.
  16. func New() *Gytes {
  17. return &Gytes{}
  18. }
  19. // SetBinaryPrefix sets binary prefix format.
  20. func (g *Gytes) SetBinaryPrefix(on bool) {
  21. g.iec = on
  22. }
  23. // Format formats bytes to string. For example, 1323 bytes will return 1.32 KB.
  24. // If binary prefix is set, it will return 1.29 KiB.
  25. func (g *Gytes) Format(b uint64) string {
  26. unit := uint64(1000)
  27. if g.iec {
  28. unit = 1024
  29. }
  30. if b < unit {
  31. return strconv.FormatUint(b, 10) + " B"
  32. } else {
  33. b := float64(b)
  34. unit := float64(unit)
  35. x := math.Floor(math.Log(b) / math.Log(unit))
  36. pre := make([]byte, 1, 2)
  37. pre[0] = "KMGTPE"[uint8(x)-1]
  38. if g.iec {
  39. pre = pre[:2]
  40. pre[1] = 'i'
  41. }
  42. // TODO: Improve performance?
  43. return fmt.Sprintf("%.02f %sB", b/math.Pow(unit, x), pre)
  44. }
  45. }
  46. func BinaryPrefix(on bool) {
  47. global.SetBinaryPrefix(on)
  48. }
  49. // Format wraps default instance's Format function.
  50. func Format(b uint64) string {
  51. return global.Format(b)
  52. }