-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_int16.go
104 lines (98 loc) · 1.88 KB
/
iterator_int16.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 (
maxUint16Div10NumDigits = 4
maxUint16Div10 = uint16(math.MaxUint16) / 10
maxUint16Mod10 = int8(math.MaxUint16 - maxUint16Div10*10)
)
// ReadUint16 reads an uint16 value
func (it *Iterator) ReadUint16() (uint16, error) {
c, err := it.nextToken()
if err != nil {
return 0, err
}
it.head++
return it.readUint16(c)
}
func (it *Iterator) readUint16(c byte) (ret uint16, err error) {
u := intDigits[c]
if u == 0 {
return 0, nil
}
if u == invalidDigit {
return 0, InvalidDigitError{c: c}
}
ret = uint16(u)
numDigit := 1
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 >= maxUint16Div10NumDigits {
if ret > maxUint16Div10 ||
(ret == maxUint16Div10 && digit > maxUint16Mod10) {
it.head = i
err = IntOverflowError{}
return
}
}
ret = (ret << 3) + (ret << 1) + uint16(digit)
numDigit++
}
it.head = i
if err = it.readMore(); err != nil {
if err == io.EOF {
err = nil
}
return
}
}
}
func (it *Iterator) readInt16(c byte) (int16, error) {
if c == '-' {
c, err := it.nextByte()
if err != nil {
return 0, err
}
it.head++
v, err := it.readUint16(c)
if err != nil {
return 0, err
}
if v > math.MaxInt16+1 {
return 0, IntOverflowError{
typ: "int16",
value: "-" + strconv.FormatUint(uint64(v), 10),
}
}
return -int16(v), nil
}
v, err := it.readUint16(c)
if err != nil {
return 0, err
}
if v > math.MaxInt16 {
return 0, IntOverflowError{
typ: "int16",
value: strconv.FormatUint(uint64(v), 10),
}
}
return int16(v), nil
}
// ReadInt16 reads an int16 value
func (it *Iterator) ReadInt16() (int16, error) {
c, err := it.nextToken()
if err != nil {
return 0, err
}
it.head++
return it.readInt16(c)
}