-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiterator_skip_number.go
157 lines (152 loc) · 2.68 KB
/
iterator_skip_number.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package jzon
import (
"io"
)
func (it *Iterator) skipExponentPart() (err error) {
c, err := it.nextByte()
if err != nil {
return err
}
it.head++
if c == '+' || c == '-' {
c, err = it.nextByte()
if err != nil {
return err
}
it.head++
}
if intDigits[c] == invalidDigit {
return InvalidDigitError{c: c}
}
for {
i := it.head
for ; i < it.tail; i++ {
c = it.buffer[i]
digit := intDigits[c]
if digit == invalidDigit {
it.head = i
return nil
}
}
// i == it.tail
it.head = i
if err = it.readMore(); err != nil {
if err == io.EOF {
return nil
}
return err
}
}
}
func (it *Iterator) skipFractionPart() (err error) {
c, err := it.nextByte()
if err != nil {
return err
}
if intDigits[c] == invalidDigit {
return InvalidDigitError{c: c}
}
it.head++
for {
i := it.head
for ; i < it.tail; i++ {
c = it.buffer[i]
digit := floatDigits[c]
if digit < 0 {
switch digit {
case expInNumber:
it.head = i + 1
return it.skipExponentPart()
default:
it.head = i
return nil
}
}
}
// i == it.tail
it.head = i
if err = it.readMore(); err != nil {
if err == io.EOF {
return nil
}
return err
}
}
}
// make sure that c is '-' or '0'~'9' before calling this method
// (the call of nextToken before this method return NumberValue)
func skipNumber(it *Iterator, c byte) (err error) {
if c == '-' {
c, err = it.nextByte()
if err != nil {
return
}
if intDigits[c] == invalidDigit {
return InvalidDigitError{c: c}
}
it.head++
}
// positive
// here the c can only be '0'~'9'
if c == '0' {
if it.head == it.tail {
if err = it.readMore(); err != nil {
if err == io.EOF {
return nil
}
return err
}
}
switch floatDigits[it.buffer[it.head]] {
case dotInNumber:
it.head++
return it.skipFractionPart()
case expInNumber:
it.head++
return it.skipExponentPart()
default:
return nil
}
} else {
for {
i := it.head
for ; i < it.tail; i++ {
c = it.buffer[i]
digit := floatDigits[c]
if digit < 0 {
switch digit {
case dotInNumber:
it.head = i + 1
return it.skipFractionPart()
case expInNumber:
it.head = i + 1
return it.skipExponentPart()
default:
it.head = i
return nil
}
}
}
// i == it.tail
it.head = i
if err = it.readMore(); err != nil {
if err == io.EOF {
return nil
}
return err
}
}
}
}
// SkipNumber skips a number
func (it *Iterator) SkipNumber() error {
c, err := it.nextToken()
if err != nil {
return err
}
if valueTypeMap[c] != NumberValue {
return UnexpectedByteError{got: c}
}
it.head++
return skipNumber(it, c)
}