MCPcopy Create free account
hub / github.com/buger/jsonparser / parseInt

Function parseInt

bytes.go:9–69  ·  view source on GitHub ↗

About 2x faster then strconv.ParseInt because it only supports base 10, which is enough for JSON

(bytes []byte)

Source from the content-addressed store, hash-verified

7
8// About 2x faster then strconv.ParseInt because it only supports base 10, which is enough for JSON
9func parseInt(bytes []byte) (v int64, ok bool, overflow bool) {
10 l := len(bytes)
11 if l == 0 {
12 return 0, false, false
13 }
14
15 var neg bool = false
16 i := 0
17 if bytes[0] == '-' {
18 neg = true
19 i = 1
20 }
21
22 if l-i < 19 {
23 for ; i < l; i++ {
24 d := bytes[i] - '0'
25 if d > 9 {
26 return 0, false, false
27 }
28 v = 10*v + int64(d)
29 }
30
31 if neg {
32 return -v, true, false
33 }
34 return v, true, false
35 }
36
37 if neg {
38 bytes = bytes[1:]
39 }
40
41 var n uint64 = 0
42 for _, c := range bytes {
43 if c < '0' || c > '9' {
44 return 0, false, false
45 }
46 if n > maxUint64/10 {
47 return 0, false, true
48 }
49 n *= 10
50 n1 := n + uint64(c-'0')
51 if n1 < n {
52 return 0, false, true
53 }
54 n = n1
55 }
56
57 if n > maxInt64 {
58 if neg && n == absMinInt64 {
59 return -absMinInt64, true, false
60 }
61 return 0, false, true
62 }
63
64 if neg {
65 return -int64(n), true, false
66 } else {

Callers 3

TestBytesParseIntFunction · 0.85
BenchmarkParseIntFunction · 0.85
ParseIntFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestBytesParseIntFunction · 0.68
BenchmarkParseIntFunction · 0.68