| 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 |
|
| 5 |
package utils |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"math" |
| 10 |
"reflect" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/gofiber/utils/v2/internal/unsafeconv" |
| 16 |
) |
| 17 |
|
| 18 |
// UnsafeString returns a string pointer without allocation |
| 19 |
func UnsafeString(b []byte) string { |
| 20 |
return unsafeconv.UnsafeString(b) |
| 21 |
} |
| 22 |
|
| 23 |
// UnsafeBytes returns a byte pointer without allocation. |
| 24 |
func UnsafeBytes(s string) []byte { |
| 25 |
return unsafeconv.UnsafeBytes(s) |
| 26 |
} |
| 27 |
|
| 28 |
// CopyString copies a string to make it immutable |
| 29 |
func CopyString(s string) string { |
| 30 |
// #nosec G103 |
| 31 |
return string(UnsafeBytes(s)) |
| 32 |
} |
| 33 |
|
| 34 |
// #nosec G103 |
| 35 |
// CopyBytes copies a slice to make it immutable |
| 36 |
func CopyBytes(b []byte) []byte { |
| 37 |
tmp := make([]byte, len(b)) |
| 38 |
copy(tmp, b) |
| 39 |
return tmp |
| 40 |
} |
| 41 |
|
| 42 |
const ( |
| 43 |
uByte = 1 << (10 * iota) |
| 44 |
uKilobyte |
| 45 |
uMegabyte |
| 46 |
uGigabyte |
| 47 |
uTerabyte |
| 48 |
uPetabyte |
| 49 |
uExabyte |
| 50 |
) |
| 51 |
|
| 52 |
// ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth. |
| 53 |
// The unit that results in the smallest number greater than or equal to 1 is always chosen. |
| 54 |
// Maximum supported input is math.MaxUint64 / 10 (≈ 1844674407370955161). |
| 55 |
func ByteSize(bytes uint64) string { |
| 56 |
const maxSafe = math.MaxUint64 / 10 |
| 57 |
unit := "" |
| 58 |
div := uint64(1) |
| 59 |
switch { |
| 60 |
case bytes >= uExabyte: |
| 61 |
unit = "EB" |
| 62 |
div = uExabyte |
| 63 |
case bytes >= uPetabyte: |
| 64 |
unit = "PB" |
| 65 |
div = uPetabyte |
| 66 |
case bytes >= uTerabyte: |
| 67 |
unit = "TB" |
| 68 |
div = uTerabyte |
| 69 |
case bytes >= uGigabyte: |
| 70 |
unit = "GB" |
| 71 |
div = uGigabyte |
| 72 |
case bytes >= uMegabyte: |
| 73 |
unit = "MB" |
| 74 |
div = uMegabyte |
| 75 |
case bytes >= uKilobyte: |
| 76 |
unit = "KB" |
| 77 |
div = uKilobyte |
| 78 |
case bytes >= uByte: |
| 79 |
unit = "B" |
| 80 |
default: |
| 81 |
return "0B" |
| 82 |
} |
| 83 |
|
| 84 |
buf := make([]byte, 0, 16) |
| 85 |
|
| 86 |
if div == 1 { |
| 87 |
buf = AppendUint(buf, bytes) |
| 88 |
buf = append(buf, unit...) |
| 89 |
return UnsafeString(buf) |
| 90 |
} |
| 91 |
|
| 92 |
// Fix: cap bytes to maxSafe for overflow, but format as fractional |
| 93 |
if bytes > maxSafe { |
| 94 |
bytes = maxSafe |
| 95 |
} |
| 96 |
|
| 97 |
scaled := (bytes/div)*10 + ((bytes%div)*10+div/2)/div |
| 98 |
integer := scaled / 10 |
| 99 |
fractional := scaled % 10 |
| 100 |
|
| 101 |
buf = AppendUint(buf, integer) |
| 102 |
if fractional > 0 { |
| 103 |
buf = append(buf, '.') |
| 104 |
buf = AppendUint(buf, fractional) |
| 105 |
} |
| 106 |
buf = append(buf, unit...) |
| 107 |
return UnsafeString(buf) |
| 108 |
} |
| 109 |
|
| 110 |
// ToString Change arg to string |
| 111 |
func ToString(arg any, timeFormat ...string) string { |
| 112 |
switch v := arg.(type) { |
| 113 |
case int: |
| 114 |
return FormatInt(int64(v)) |
| 115 |
case int8: |
| 116 |
return FormatInt8(v) |
| 117 |
case int16: |
| 118 |
return FormatInt16(v) |
| 119 |
case int32: |
| 120 |
return FormatInt32(v) |
| 121 |
case int64: |
| 122 |
return FormatInt(v) |
| 123 |
case uint: |
| 124 |
return FormatUint(uint64(v)) |
| 125 |
case uint8: |
| 126 |
return FormatUint8(v) |
| 127 |
case uint16: |
| 128 |
return FormatUint16(v) |
| 129 |
case uint32: |
| 130 |
return FormatUint32(v) |
| 131 |
case uint64: |
| 132 |
return FormatUint(v) |
| 133 |
case string: |
| 134 |
return v |
| 135 |
case []byte: |
| 136 |
return string(v) |
| 137 |
case bool: |
| 138 |
return strconv.FormatBool(v) |
| 139 |
case float32: |
| 140 |
return strconv.FormatFloat(float64(v), 'f', -1, 32) |
| 141 |
case float64: |
| 142 |
return strconv.FormatFloat(v, 'f', -1, 64) |
| 143 |
case time.Time: |
| 144 |
if len(timeFormat) > 0 { |
| 145 |
return v.Format(timeFormat[0]) |
| 146 |
} |
| 147 |
return v.Format("2006-01-02 15:04:05") |
| 148 |
case reflect.Value: |
| 149 |
if !v.IsValid() || !v.CanInterface() { |
| 150 |
return "" |
| 151 |
} |
| 152 |
return ToString(v.Interface(), timeFormat...) |
| 153 |
// error before fmt.Stringer to match the fmt package's precedence |
| 154 |
case error: |
| 155 |
return v.Error() |
| 156 |
case fmt.Stringer: |
| 157 |
return v.String() |
| 158 |
// Handle common pointer types directly to avoid reflection |
| 159 |
case *string: |
| 160 |
if v != nil { |
| 161 |
return *v |
| 162 |
} |
| 163 |
return "" |
| 164 |
case *int: |
| 165 |
if v != nil { |
| 166 |
return FormatInt(int64(*v)) |
| 167 |
} |
| 168 |
return "0" |
| 169 |
case *int64: |
| 170 |
if v != nil { |
| 171 |
return FormatInt(*v) |
| 172 |
} |
| 173 |
return "0" |
| 174 |
case *uint64: |
| 175 |
if v != nil { |
| 176 |
return FormatUint(*v) |
| 177 |
} |
| 178 |
return "0" |
| 179 |
case *float64: |
| 180 |
if v != nil { |
| 181 |
return strconv.FormatFloat(*v, 'f', -1, 64) |
| 182 |
} |
| 183 |
return "0" |
| 184 |
case *bool: |
| 185 |
if v != nil { |
| 186 |
return strconv.FormatBool(*v) |
| 187 |
} |
| 188 |
return "false" |
| 189 |
// Handle common slice types directly to avoid reflection |
| 190 |
case []string: |
| 191 |
if len(v) == 0 { |
| 192 |
return "[]" |
| 193 |
} |
| 194 |
// Compute the exact output size for a single allocation: |
| 195 |
// '[' + ']' + one separating space per gap + all element bytes. |
| 196 |
size := len(v) + 1 |
| 197 |
for _, s := range v { |
| 198 |
size += len(s) |
| 199 |
} |
| 200 |
buf := make([]byte, 0, size) |
| 201 |
buf = append(buf, '[') |
| 202 |
for i, s := range v { |
| 203 |
if i > 0 { |
| 204 |
buf = append(buf, ' ') |
| 205 |
} |
| 206 |
buf = append(buf, s...) |
| 207 |
} |
| 208 |
buf = append(buf, ']') |
| 209 |
return UnsafeString(buf) |
| 210 |
case []int: |
| 211 |
if len(v) == 0 { |
| 212 |
return "[]" |
| 213 |
} |
| 214 |
// Compute the exact output size for a single allocation: |
| 215 |
// '[' + ']' + one separating space per gap + all element digits. |
| 216 |
size := len(v) + 1 |
| 217 |
for _, n := range v { |
| 218 |
size += intDigits(int64(n)) |
| 219 |
} |
| 220 |
buf := make([]byte, 0, size) |
| 221 |
buf = append(buf, '[') |
| 222 |
for i, n := range v { |
| 223 |
if i > 0 { |
| 224 |
buf = append(buf, ' ') |
| 225 |
} |
| 226 |
buf = AppendInt(buf, int64(n)) |
| 227 |
} |
| 228 |
buf = append(buf, ']') |
| 229 |
return UnsafeString(buf) |
| 230 |
default: |
| 231 |
// Check if the type is a pointer by using reflection |
| 232 |
rv := reflect.ValueOf(arg) |
| 233 |
if !rv.IsValid() { |
| 234 |
return "" |
| 235 |
} |
| 236 |
kind := rv.Kind() |
| 237 |
if kind == reflect.Pointer && !rv.IsNil() { |
| 238 |
// Dereference the pointer and recursively call ToString |
| 239 |
return ToString(rv.Elem().Interface(), timeFormat...) |
| 240 |
} else if kind == reflect.Slice || kind == reflect.Array { |
| 241 |
// handle slices |
| 242 |
n := rv.Len() |
| 243 |
if n == 0 { |
| 244 |
return "[]" |
| 245 |
} |
| 246 |
var buf strings.Builder |
| 247 |
buf.Grow(n * 8) // Pre-allocate approximate size |
| 248 |
buf.WriteByte('[') |
| 249 |
for i := range n { |
| 250 |
if i > 0 { |
| 251 |
buf.WriteByte(' ') |
| 252 |
} |
| 253 |
buf.WriteString(ToString(rv.Index(i).Interface())) |
| 254 |
} |
| 255 |
buf.WriteByte(']') |
| 256 |
return buf.String() |
| 257 |
} |
| 258 |
|
| 259 |
// For types not explicitly handled, use fmt.Sprint to generate a string representation |
| 260 |
return fmt.Sprint(arg) |
| 261 |
} |
| 262 |
} |