-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_int8.go
109 lines (103 loc) · 1.9 KB
/
iterator_int8.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
106
107
108
109
package jzon
import (
"io"
"math"
"strconv"
)
const (
maxUint8Div10 = uint8(math.MaxUint8) / 10
maxUint8Mod10 = int8(math.MaxUint8 - maxUint8Div10*10)
)
// ReadUint8 reads an Uint8 value
func (it *Iterator) ReadUint8() (uint8, error) {
c, err := it.nextToken()
if err != nil {
return 0, err
}
it.head++
return it.readUint8(c)
}
func (it *Iterator) readUint8(c byte) (ret uint8, err error) {
u := intDigits[c]
if u == 0 {
return 0, nil
}
if u == invalidDigit {
return 0, InvalidDigitError{c: c}
}
ret = uint8(u)
if it.head == it.tail {
if err = it.readMore(); err != nil {
if err == io.EOF {
err = nil
}
return
}
}
u = intDigits[it.buffer[it.head]] // second digit
if u == invalidDigit {
return
}
ret = (ret << 3) + (ret << 1) + uint8(u)
it.head++
if it.head == it.tail {
if err = it.readMore(); err != nil {
if err == io.EOF {
err = nil
}
return
}
}
u = intDigits[it.buffer[it.head]] // third digit
if u == invalidDigit {
return
}
it.head++
if ret > maxUint8Div10 ||
(ret == maxUint8Div10 && u > maxUint8Mod10) {
err = IntOverflowError{}
return
}
ret = (ret << 3) + (ret << 1) + uint8(u)
return
}
func (it *Iterator) readInt8(c byte) (int8, error) {
if c == '-' {
c, err := it.nextByte()
if err != nil {
return 0, err
}
it.head++
v, err := it.readUint8(c)
if err != nil {
return 0, err
}
if v > math.MaxInt8+1 {
return 0, IntOverflowError{
typ: "int8",
value: "-" + strconv.FormatUint(uint64(v), 10),
}
}
return -int8(v), nil
}
v, err := it.readUint8(c)
if err != nil {
return 0, err
}
if v > math.MaxInt8 {
return 0, IntOverflowError{
typ: "int8",
value: strconv.FormatUint(uint64(v), 10),
}
}
return int8(v), nil
}
// ReadInt8 reads an int8 value
func (it *Iterator) ReadInt8() (int8, error) {
c, err := it.nextToken()
if err != nil {
return 0, err
}
it.head++
return it.readInt8(c)
}