MCPcopy
hub / github.com/fabiolb/fabio / atoi

Function atoi

logger/pattern.go:294–325  ·  view source on GitHub ↗

atoi is a replacement for strconv.Atoi/strconv.FormatInt which does not alloc.

(b *bytes.Buffer, i int64, pad int)

Source from the content-addressed store, hash-verified

292// atoi is a replacement for strconv.Atoi/strconv.FormatInt
293// which does not alloc.
294func atoi(b *bytes.Buffer, i int64, pad int) {
295 var flag bool
296 if i < 0 {
297 flag = true
298 i = -i
299 }
300
301 // format number
302 // 2^63-1 == 9223372036854775807
303 var d [128]byte
304 n, p := len(d), len(d)-1
305 for i >= 0 {
306 d[p] = byte('0') + byte(i%10)
307 i /= 10
308 p--
309 if i == 0 {
310 break
311 }
312 }
313
314 // padding
315 for n-p-1 < pad {
316 d[p] = byte('0')
317 p--
318 }
319
320 if flag {
321 d[p] = '-'
322 p--
323 }
324 b.Write(d[p+1:])
325}
326
327// parse parses a format string into a pattern based on the following rules:
328//

Callers 2

TestAtoiFunction · 0.85
pattern.goFile · 0.85

Calls 1

WriteMethod · 0.45

Tested by 1

TestAtoiFunction · 0.68