| 1 | // SYS-REQ-015, SYS-REQ-058, SYS-REQ-059, SYS-REQ-064: integer parsing internals |
| 2 | package jsonparserscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ |
| 3 | |
| 4 | const absMinInt64scip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/absMinInt64. = 1 << 63 |
| 5 | const maxInt64scip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/maxInt64. = 1<<63 - 1 |
| 6 | const maxUint64scip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/maxUint64. = 1<<64 - 1 |
| 7 | |
| 8 | // About 2x faster then strconv.ParseInt because it only supports base 10, which is enough for JSON |
| 9 | func parseIntscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/parseInt().(bytes []byte) (v int64, ok bool, overflow bool) { |
| 10 | if len(bytes) == 0 { |
| 11 | return 0, false, false |
| 12 | } |
| 13 | |
| 14 | var neg bool = false |
| 15 | if bytes[0] == '-' { |
| 16 | neg = true |
| 17 | bytes = bytes[1:] |
| 18 | } |
| 19 | |
| 20 | var n uint64 = 0 |
| 21 | for _, c := range bytes { |
| 22 | if c < '0' || c > '9' { |
| 23 | return 0, false, false |
| 24 | } |
| 25 | if n > maxUint64scip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/maxUint64./10 { |
| 26 | return 0, false, true |
| 27 | } |
| 28 | n *= 10 |
| 29 | n1 := n + uint64(c-'0') |
| 30 | if n1 < n { |
| 31 | return 0, false, true |
| 32 | } |
| 33 | n = n1 |
| 34 | } |
| 35 | |
| 36 | if n > maxInt64scip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/maxInt64. { |
| 37 | if neg && n == absMinInt64scip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/absMinInt64. { |
| 38 | return -absMinInt64scip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/absMinInt64., true, false |
| 39 | } |
| 40 | return 0, false, true |
| 41 | } |
| 42 | |
| 43 | if neg { |
| 44 | return -int64(n), true, false |
| 45 | } else { |
| 46 | return int64(n), true, false |
| 47 | } |
| 48 | } |