convert.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 Github Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. package utils
  5. import (
  6. "fmt"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "unsafe"
  12. )
  13. const MaxStringLen = 0x7fff0000 // Maximum string length for UnsafeBytes. (decimal: 2147418112)
  14. // UnsafeString returns a string pointer without allocation
  15. //
  16. //nolint:gosec // unsafe is used for better performance here
  17. func UnsafeString(b []byte) string {
  18. return *(*string)(unsafe.Pointer(&b))
  19. }
  20. // UnsafeBytes returns a byte pointer without allocation.
  21. // String length shouldn't be more than 2147418112.
  22. //
  23. //nolint:gosec // unsafe is used for better performance here
  24. func UnsafeBytes(s string) []byte {
  25. if s == "" {
  26. return nil
  27. }
  28. return (*[MaxStringLen]byte)(unsafe.Pointer(
  29. (*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
  30. )[:len(s):len(s)]
  31. }
  32. // CopyString copies a string to make it immutable
  33. func CopyString(s string) string {
  34. return string(UnsafeBytes(s))
  35. }
  36. // CopyBytes copies a slice to make it immutable
  37. func CopyBytes(b []byte) []byte {
  38. tmp := make([]byte, len(b))
  39. copy(tmp, b)
  40. return tmp
  41. }
  42. const (
  43. uByte = 1 << (10 * iota) // 1 << 10 == 1024
  44. uKilobyte
  45. uMegabyte
  46. uGigabyte
  47. uTerabyte
  48. uPetabyte
  49. uExabyte
  50. )
  51. // ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth.
  52. // The unit that results in the smallest number greater than or equal to 1 is always chosen.
  53. func ByteSize(bytes uint64) string {
  54. unit := ""
  55. value := float64(bytes)
  56. switch {
  57. case bytes >= uExabyte:
  58. unit = "EB"
  59. value /= uExabyte
  60. case bytes >= uPetabyte:
  61. unit = "PB"
  62. value /= uPetabyte
  63. case bytes >= uTerabyte:
  64. unit = "TB"
  65. value /= uTerabyte
  66. case bytes >= uGigabyte:
  67. unit = "GB"
  68. value /= uGigabyte
  69. case bytes >= uMegabyte:
  70. unit = "MB"
  71. value /= uMegabyte
  72. case bytes >= uKilobyte:
  73. unit = "KB"
  74. value /= uKilobyte
  75. case bytes >= uByte:
  76. unit = "B"
  77. default:
  78. return "0B"
  79. }
  80. result := strconv.FormatFloat(value, 'f', 1, 64)
  81. result = strings.TrimSuffix(result, ".0")
  82. return result + unit
  83. }
  84. // ToString Change arg to string
  85. func ToString(arg interface{}, timeFormat ...string) string {
  86. tmp := reflect.Indirect(reflect.ValueOf(arg)).Interface()
  87. switch v := tmp.(type) {
  88. case int:
  89. return strconv.Itoa(v)
  90. case int8:
  91. return strconv.FormatInt(int64(v), 10)
  92. case int16:
  93. return strconv.FormatInt(int64(v), 10)
  94. case int32:
  95. return strconv.FormatInt(int64(v), 10)
  96. case int64:
  97. return strconv.FormatInt(v, 10)
  98. case uint:
  99. return strconv.Itoa(int(v))
  100. case uint8:
  101. return strconv.FormatInt(int64(v), 10)
  102. case uint16:
  103. return strconv.FormatInt(int64(v), 10)
  104. case uint32:
  105. return strconv.FormatInt(int64(v), 10)
  106. case uint64:
  107. return strconv.FormatInt(int64(v), 10)
  108. case string:
  109. return v
  110. case []byte:
  111. return string(v)
  112. case bool:
  113. return strconv.FormatBool(v)
  114. case float32:
  115. return strconv.FormatFloat(float64(v), 'f', -1, 32)
  116. case float64:
  117. return strconv.FormatFloat(v, 'f', -1, 64)
  118. case time.Time:
  119. if len(timeFormat) > 0 {
  120. return v.Format(timeFormat[0])
  121. }
  122. return v.Format("2006-01-02 15:04:05")
  123. case reflect.Value:
  124. return ToString(v.Interface(), timeFormat...)
  125. case fmt.Stringer:
  126. return v.String()
  127. default:
  128. return ""
  129. }
  130. }