-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathbytes.go
More file actions
72 lines (63 loc) · 1.19 KB
/
Copy pathbytes.go
File metadata and controls
72 lines (63 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SYS-REQ-015, SYS-REQ-058, SYS-REQ-059, SYS-REQ-064: integer parsing internals
package jsonparser
const absMinInt64 = 1 << 63
const maxInt64 = 1<<63 - 1
const maxUint64 = 1<<64 - 1
// About 2x faster then strconv.ParseInt because it only supports base 10, which is enough for JSON
func parseInt(bytes []byte) (v int64, ok bool, overflow bool) {
l := len(bytes)
if l == 0 {
return 0, false, false
}
var neg bool = false
i := 0
if bytes[0] == '-' {
neg = true
i = 1
}
if i == l {
return 0, false, false
}
if l-i < 19 {
for ; i < l; i++ {
d := bytes[i] - '0'
if d > 9 {
return 0, false, false
}
v = 10*v + int64(d)
}
if neg {
return -v, true, false
}
return v, true, false
}
if neg {
bytes = bytes[1:]
}
var n uint64 = 0
for _, c := range bytes {
if c < '0' || c > '9' {
return 0, false, false
}
if n > maxUint64/10 {
return 0, false, true
}
n *= 10
n1 := n + uint64(c-'0')
if n1 < n {
return 0, false, true
}
n = n1
}
if n > maxInt64 {
if neg && n == absMinInt64 {
return -absMinInt64, true, false
}
return 0, false, true
}
if neg {
return -int64(n), true, false
} else {
return int64(n), true, false
}
}