-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_int64.go
105 lines (99 loc) · 1.91 KB
/
iterator_int64.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package jzon
import (
"io"
"math"
"strconv"
)
const (
maxUint64Div10NumDigits = 19
maxUint64Div10 = uint64(math.MaxUint64) / 10
maxUint64Mod10 = int8(math.MaxUint64 - maxUint64Div10*10)
)
// ReadUint64 reads an uint64 value
func (it *Iterator) ReadUint64() (uint64, error) {
c, err := it.nextToken()
if err != nil {
return 0, err
}
it.head++
return it.readUint64(c)
}
func (it *Iterator) readUint64(c byte) (ret uint64, err error) {
u := intDigits[c]
if u == 0 {
return 0, nil
}
if u == invalidDigit {
return 0, InvalidDigitError{c: c}
}
ret = uint64(u)
numDigit := 1
// TODO: inline expansion
for {
i := it.head
for ; i < it.tail; i++ {
digit := intDigits[it.buffer[i]]
if digit == invalidDigit {
it.head = i
return ret, nil
}
if numDigit >= maxUint64Div10NumDigits {
if ret > maxUint64Div10 ||
(ret == maxUint64Div10 && digit > maxUint64Mod10) {
it.head = i
err = IntOverflowError{}
return
}
}
ret = (ret << 3) + (ret << 1) + uint64(digit)
numDigit++
}
it.head = i
if err = it.readMore(); err != nil {
if err == io.EOF {
err = nil
}
return
}
}
}
func (it *Iterator) readInt64(c byte) (int64, error) {
if c == '-' {
c, err := it.nextByte()
if err != nil {
return 0, err
}
it.head++
v, err := it.readUint64(c)
if err != nil {
return 0, err
}
if v > math.MaxInt64+1 {
return 0, IntOverflowError{
typ: "int64",
value: "-" + strconv.FormatUint(uint64(v), 10),
}
}
return -int64(v), nil
}
v, err := it.readUint64(c)
if err != nil {
return 0, err
}
if v > math.MaxInt64 {
return 0, IntOverflowError{
typ: "int64",
value: strconv.FormatUint(uint64(v), 10),
}
}
return int64(v), nil
}
// ReadInt64 reads an int64 value
func (it *Iterator) ReadInt64() (int64, error) {
c, err := it.nextToken()
if err != nil {
return 0, err
}
it.head++
return it.readInt64(c)
}