Debian Codegraph @

convert.go

raw symbols SCIP
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 utilsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
9 "mathscip-go gomod github.com/golang/go/src go1.25.0 math/"
10 "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/"
11 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
12 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
13 "timescip-go gomod github.com/golang/go/src go1.25.0 time/"
14
15 "github.com/gofiber/utils/v2/internal/unsafeconvscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/internal/unsafeconv`/"
16 )
17
18 // UnsafeString returns a string pointer without allocation
19 func UnsafeStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/UnsafeString().(b []byte) string {
20 return unsafeconvscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/internal/unsafeconv`/.UnsafeStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/internal/unsafeconv`/UnsafeString().(b)
21 }
22
23 // UnsafeBytes returns a byte pointer without allocation.
24 func UnsafeBytesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/UnsafeBytes().(s string) []byte {
25 return unsafeconvscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/internal/unsafeconv`/.UnsafeBytesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/internal/unsafeconv`/UnsafeBytes().(s)
26 }
27
28 // CopyString copies a string to make it immutable
29 func CopyStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/CopyString().(s string) string {
30 // #nosec G103
31 return string(UnsafeBytesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/UnsafeBytes().(s))
32 }
33
34 // #nosec G103
35 // CopyBytes copies a slice to make it immutable
36 func CopyBytesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/CopyBytes().(b []byte) []byte {
37 tmp := make([]byte, len(b))
38 copy(tmp, b)
39 return tmp
40 }
41
42 const (
43 uBytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uByte. = 1 << (10 * iota)
44 uKilobytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uKilobyte.
45 uMegabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uMegabyte.
46 uGigabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uGigabyte.
47 uTerabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uTerabyte.
48 uPetabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uPetabyte.
49 uExabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/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 ByteSizescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/ByteSize().(bytes uint64) string {
56 const maxSafe = mathscip-go gomod github.com/golang/go/src go1.25.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.25.0 math/MaxUint64. / 10
57 unit := ""
58 div := uint64(1)
59 switch {
60 case bytes >= uExabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uExabyte.:
61 unit = "EB"
62 div = uExabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uExabyte.
63 case bytes >= uPetabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uPetabyte.:
64 unit = "PB"
65 div = uPetabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uPetabyte.
66 case bytes >= uTerabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uTerabyte.:
67 unit = "TB"
68 div = uTerabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uTerabyte.
69 case bytes >= uGigabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uGigabyte.:
70 unit = "GB"
71 div = uGigabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uGigabyte.
72 case bytes >= uMegabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uMegabyte.:
73 unit = "MB"
74 div = uMegabytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uMegabyte.
75 case bytes >= uKilobytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uKilobyte.:
76 unit = "KB"
77 div = uKilobytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uKilobyte.
78 case bytes >= uBytescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/uByte.:
79 unit = "B"
80 default:
81 return "0B"
82 }
83
84 buf := make([]byte, 0, 16)
85
86 if div == 1 {
87 buf = AppendUintscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendUint().(buf, bytes)
88 buf = append(buf, unit...)
89 return UnsafeStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/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 = AppendUintscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendUint().(buf, integer)
102 if fractional > 0 {
103 buf = append(buf, '.')
104 buf = AppendUintscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendUint().(buf, fractional)
105 }
106 buf = append(buf, unit...)
107 return UnsafeStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/UnsafeString().(buf)
108 }
109
110 // ToString Change arg to string
111 func ToStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/ToString().(arg any, timeFormat ...string) string {
112 switch v := arg.(type) {
113 case int:
114 return FormatIntscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatInt().(int64(v))
115 case int8:
116 return FormatInt8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatInt8().(v)
117 case int16:
118 return FormatInt16scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatInt16().(v)
119 case int32:
120 return FormatInt32scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatInt32().(v)
121 case int64:
122 return FormatIntscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatInt().(v)
123 case uint:
124 return FormatUintscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatUint().(uint64(v))
125 case uint8:
126 return FormatUint8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatUint8().(v)
127 case uint16:
128 return FormatUint16scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatUint16().(v)
129 case uint32:
130 return FormatUint32scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatUint32().(v)
131 case uint64:
132 return FormatUintscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatUint().(v)
133 case string:
134 return v
135 case []byte:
136 return string(v)
137 case bool:
138 return strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.FormatBoolscip-go gomod github.com/golang/go/src go1.25.0 strconv/FormatBool().(v)
139 case float32:
140 return strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.FormatFloatscip-go gomod github.com/golang/go/src go1.25.0 strconv/FormatFloat().(float64(v), 'f', -1, 32)
141 case float64:
142 return strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.FormatFloatscip-go gomod github.com/golang/go/src go1.25.0 strconv/FormatFloat().(v, 'f', -1, 64)
143 case timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#:
144 if len(timeFormat) > 0 {
145 return v.Formatscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Format().(timeFormat[0])
146 }
147 return v.Formatscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Format().("2006-01-02 15:04:05")
148 case reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#:
149 if !v.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() || !v.CanInterfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#CanInterface().() {
150 return ""
151 }
152 return ToStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/ToString().(v.Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Interface().(), timeFormat...)
153 // error before fmt.Stringer to match the fmt package's precedence
154 case error:
155 return v.Error()
156 case fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Stringerscip-go gomod github.com/golang/go/src go1.25.0 fmt/Stringer#:
157 return v.Stringscip-go gomod github.com/golang/go/src go1.25.0 fmt/Stringer#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 FormatIntscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatInt().(int64(*v))
167 }
168 return "0"
169 case *int64:
170 if v != nil {
171 return FormatIntscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatInt().(*v)
172 }
173 return "0"
174 case *uint64:
175 if v != nil {
176 return FormatUintscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/FormatUint().(*v)
177 }
178 return "0"
179 case *float64:
180 if v != nil {
181 return strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.FormatFloatscip-go gomod github.com/golang/go/src go1.25.0 strconv/FormatFloat().(*v, 'f', -1, 64)
182 }
183 return "0"
184 case *bool:
185 if v != nil {
186 return strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.FormatBoolscip-go gomod github.com/golang/go/src go1.25.0 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 UnsafeStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/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 += intDigitsscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/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 = AppendIntscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/AppendInt().(buf, int64(n))
227 }
228 buf = append(buf, ']')
229 return UnsafeStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/UnsafeString().(buf)
230 default:
231 // Check if the type is a pointer by using reflection
232 rv := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(arg)
233 if !rv.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() {
234 return ""
235 }
236 kind := rv.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().()
237 if kind == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 reflect/Pointer. && !rv.IsNilscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsNil().() {
238 // Dereference the pointer and recursively call ToString
239 return ToStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/ToString().(rv.Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().().Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Interface().(), timeFormat...)
240 } else if kind == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Slicescip-go gomod github.com/golang/go/src go1.25.0 reflect/Slice. || kind == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Arrayscip-go gomod github.com/golang/go/src go1.25.0 reflect/Array. {
241 // handle slices
242 n := rv.Lenscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Len().()
243 if n == 0 {
244 return "[]"
245 }
246 var buf stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#
247 buf.Growscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#Grow().(n * 8) // Pre-allocate approximate size
248 buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteByte().('[')
249 for i := range n {
250 if i > 0 {
251 buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteByte().(' ')
252 }
253 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().(ToStringscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2`/ToString().(rv.Indexscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Index().(i).Interfacescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Interface().()))
254 }
255 buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteByte().(']')
256 return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#String().()
257 }
258
259 // For types not explicitly handled, use fmt.Sprint to generate a string representation
260 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(arg)
261 }
262 }