1package jsonparser
2
3import (
4 bio "bytes"
5)
6
7// minInt64 '-9223372036854775808' is the smallest representable number in int64
8const minInt64 = `9223372036854775808`
9
10// About 2x faster then strconv.ParseInt because it only supports base 10, which is enough for JSON
11func parseInt(bytes []byte) (v int64, ok bool, overflow bool) {
12 if len(bytes) == 0 {
13 return 0, false, false
14 }
15
16 var neg bool = false
17 if bytes[0] == '-' {
18 neg = true
19 bytes = bytes[1:]
20 }
21
22 var b int64 = 0
23 for _, c := range bytes {
24 if c >= '0' && c <= '9' {
25 b = (10 * v) + int64(c-'0')
26 } else {
27 return 0, false, false
28 }
29 if overflow = (b < v); overflow {
30 break
31 }
32 v = b
33 }
34
35 if overflow {
36 if neg && bio.Equal(bytes, []byte(minInt64)) {
37 return b, true, false
38 }
39 return 0, false, true
40 }
41
42 if neg {
43 return -v, true, false
44 } else {
45 return v, true, false
46 }
47}