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

Function parseInt

bytes.go:9–72  ·  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 if i == l {
22 return 0, false, false
23 }
24
25 if l-i < 19 {
26 for ; i < l; i++ {
27 d := bytes[i] - '0'
28 if d > 9 {
29 return 0, false, false
30 }
31 v = 10*v + int64(d)
32 }
33
34 if neg {
35 return -v, true, false
36 }
37 return v, true, false
38 }
39
40 if neg {
41 bytes = bytes[1:]
42 }
43
44 var n uint64 = 0
45 for _, c := range bytes {
46 if c < '0' || c > '9' {
47 return 0, false, false
48 }
49 if n > maxUint64/10 {
50 return 0, false, true
51 }
52 n *= 10
53 n1 := n + uint64(c-'0')
54 if n1 < n {
55 return 0, false, true
56 }
57 n = n1
58 }
59
60 if n > maxInt64 {
61 if neg && n == absMinInt64 {
62 return -absMinInt64, true, false
63 }
64 return 0, false, true
65 }
66

Callers 4

TestBytesParseIntFunction · 0.85
BenchmarkParseIntFunction · 0.85
ParseIntFunction · 0.85
GetUint64Function · 0.85

Calls

no outgoing calls

Tested by 2

TestBytesParseIntFunction · 0.68
BenchmarkParseIntFunction · 0.68