MCPcopy
hub / github.com/tdewolff/minify / Number

Function Number

common.go:211–512  ·  view source on GitHub ↗

Number minifies a given byte slice containing a number and removes superfluous characters.

(num []byte, prec int)

Source from the content-addressed store, hash-verified

209
210// Number minifies a given byte slice containing a number and removes superfluous characters.
211func Number(num []byte, prec int) []byte {
212 if len(num) <= 1 {
213 return num
214 }
215
216 // omit first + and register mantissa start and end, whether it's negative and the exponent
217 neg := false
218 start := 0
219 dot := -1
220 end := len(num)
221 origExp := 0
222 if num[0] == '+' || num[0] == '-' {
223 if num[0] == '-' {
224 neg = true
225 }
226 start++
227 }
228 for i, c := range num[start:] {
229 if c == '.' {
230 dot = start + i
231 } else if c == 'e' || c == 'E' {
232 end = start + i
233 i += start + 1
234 if i < len(num) && num[i] == '+' {
235 i++
236 }
237 if tmpOrigExp, n := strconv.ParseInt(num[i:]); 0 < n && int64(MinInt) <= tmpOrigExp && tmpOrigExp <= int64(MaxInt) {
238 // range checks for when int is 32 bit
239 origExp = int(tmpOrigExp)
240 } else {
241 return num
242 }
243 break
244 }
245 }
246 if dot == -1 {
247 dot = end
248 }
249
250 // trim leading zeros but leave at least one digit
251 for start < end-1 && num[start] == '0' {
252 start++
253 }
254 // trim trailing zeros
255 i := end - 1
256 for ; dot < i; i-- {
257 if num[i] != '0' {
258 end = i + 1
259 break
260 }
261 }
262 if i == dot {
263 end = dot
264 if start == end {
265 num[start] = '0'
266 return num[start : start+1]
267 }
268 } else if start == end-1 && num[start] == '0' {

Callers 15

TestNumberFunction · 0.85
TestNumberTruncateFunction · 0.85
TestNumberRandomFunction · 0.85
BenchmarkNumberFunction · 0.85
BenchmarkNumber2Function · 0.85
setCustomLevelFunction · 0.85
createLanguageServiceFunction · 0.85
ScrollNumberFunction · 0.85
SingleNumberFunction · 0.85
sample_antd.jsFile · 0.85
shouldDelayFunction · 0.85

Calls 2

lenFunction · 0.85
copyFunction · 0.50

Tested by 5

TestNumberFunction · 0.68
TestNumberTruncateFunction · 0.68
TestNumberRandomFunction · 0.68
BenchmarkNumberFunction · 0.68
BenchmarkNumber2Function · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…