-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_int32.go
104 lines (98 loc) · 1.88 KB
/
iterator_int32.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
package jzon
import (
"io"
"math"
"strconv"
)
const (
maxUint32Div10NumDigits = 9
maxUint32Div10 = uint32(math.MaxUint32) / 10
maxUint32Mod10 = int8(math.MaxUint32 - maxUint32Div10*10)
)
// ReadUint32 reads an uint32 value
func (it *Iterator) ReadUint32() (uint32, error) {
c, err := it.nextToken()
if err != nil {
return 0, err
}
it.head++
return it.readUint32(c)
}
func (it *Iterator) readUint32(c byte) (ret uint32, err error) {
u := intDigits[c]
if u == 0 {
return 0, nil
}
if u == invalidDigit {
return 0, InvalidDigitError{c: c}
}
ret = uint32(u)
numDigit := 1
for {
i := it.head
for ; i < it.tail; i++ {
digit := intDigits[it.buffer[i]]
if digit == invalidDigit {
it.head = i
return
}
if numDigit >= maxUint32Div10NumDigits {
if ret > maxUint32Div10 ||
(ret == maxUint32Div10 && digit > maxUint32Mod10) {
it.head = i
err = IntOverflowError{}
return
}
}
ret = (ret << 3) + (ret << 1) + uint32(digit)
numDigit++
}
it.head = i
if err = it.readMore(); err != nil {
if err == io.EOF {
err = nil
}
return
}
}
}
func (it *Iterator) readInt32(c byte) (int32, error) {
if c == '-' {
c, err := it.nextByte()
if err != nil {
return 0, err
}
it.head++
v, err := it.readUint32(c)
if err != nil {
return 0, err
}
if v > math.MaxInt32+1 {
return 0, IntOverflowError{
typ: "int32",
value: "-" + strconv.FormatUint(uint64(v), 10),
}
}
return -int32(v), nil
}
v, err := it.readUint32(c)
if err != nil {
return 0, err
}
if v > math.MaxInt32 {
return 0, IntOverflowError{
typ: "int32",
value: strconv.FormatUint(uint64(v), 10),
}
}
return int32(v), nil
}
// ReadInt32 reads an int32 value
func (it *Iterator) ReadInt32() (int32, error) {
c, err := it.nextToken()
if err != nil {
return 0, err
}
it.head++
return it.readInt32(c)
}