-
Notifications
You must be signed in to change notification settings - Fork 2
/
inter_iter.go
executable file
·250 lines (248 loc) · 4.81 KB
/
inter_iter.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package jin
// IterateArray is a callback function that can iterate any array and return value as byte slice.
// It stripes quotation marks from string values before return.
// Path value can be left blank for access main JSON.
func IterateArray(json []byte, callback func([]byte) (bool, error), path ...string) error {
switch len(json) {
case 0, 1:
return errBadJSON(0)
case 2:
if string(json) == "[]" {
return nil
}
return errBadJSON(0)
}
var start int
var err error
if len(path) == 0 {
for space(json[start]) {
if start > len(json)-2 {
return errBadJSON(start)
}
start++
continue
}
} else {
_, start, _, err = core(json, true, path...)
if err != nil {
return err
}
}
chars := []byte{34, 44, 91, 93, 123, 125}
isJSONChar := make([]bool, 256)
for _, v := range chars {
isJSONChar[v] = true
}
if json[start] != 91 {
return errArrayExpected()
}
start++
inQuote := false
level := 0
for i := start; i < len(json); i++ {
curr := json[i]
if !isJSONChar[curr] {
continue
}
if curr == 34 {
if inQuote {
for k := i - 1; k > 0; k-- {
if json[k] == 92 {
continue
}
if (i-k)%2 != 0 {
inQuote = !inQuote
goto cont
}
break
}
} else {
inQuote = !inQuote
continue
}
cont:
continue
}
if inQuote {
continue
} else {
if curr == 91 || curr == 123 {
level++
continue
}
if curr == 93 || curr == 125 {
if level < 0 {
return nil
}
if curr == 93 {
if level == 0 {
_, innerError := callback(cleanValue(json[start:i]))
return innerError
}
}
level--
continue
}
if level == 0 {
if curr == 44 {
keepGoing, innerError := callback(cleanValue(json[start:i]))
if innerError != nil {
return innerError
}
if !keepGoing {
return nil
}
start = i + 1
continue
}
}
}
}
return errBadJSON(start)
}
// IterateKeyValue is a callback function that can iterate any object and return key-value pair as byte slices.
// It stripes quotation marks from string values befour return.
// Path value can be left blank for access main JSON.
func IterateKeyValue(json []byte, callback func([]byte, []byte) (bool, error), path ...string) error {
if string(json) == "{}" {
return errEmpty()
}
var start int
var err error
var end int
if len(path) == 0 {
for space(json[start]) {
if start > len(json)-1 {
return errBadJSON(start)
}
start++
continue
}
} else {
_, start, _, err = core(json, true, path...)
if err != nil {
return err
}
}
chars := []byte{34, 44, 58, 91, 93, 123, 125}
isJSONChar := make([]bool, 256)
for _, v := range chars {
isJSONChar[v] = true
}
if json[start] == 123 {
keyStart := 0
keyEnd := 0
inQuote := false
level := 0
var key []byte
for i := start; i < len(json); i++ {
curr := json[i]
if !isJSONChar[curr] {
continue
}
if curr == 34 {
if inQuote {
for n := i - 1; n > -1; n-- {
if json[n] != 92 {
if (i-n)%2 != 0 {
inQuote = !inQuote
break
} else {
goto cont
}
}
continue
}
} else {
inQuote = !inQuote
}
if inQuote {
keyStart = i
continue
}
keyEnd = i
cont:
continue
}
if inQuote {
continue
} else {
if curr == 91 || curr == 123 {
level++
continue
}
if curr == 93 || curr == 125 {
if curr == 125 {
if level == 1 {
for j := start; j < i; j++ {
if !space(json[j]) {
start = j
break
}
}
for j := i - 1; j > start-1; j-- {
if !space(json[j]) {
end = j
break
}
}
if json[start] == 34 && json[end] == 34 {
callback(key, json[start+1:end])
return nil
}
callback(key, json[start:end+1])
return nil
}
}
if level == 1 {
return nil
}
level--
continue
}
if level == 1 {
if curr == 44 {
end = i - 1
for j := start; j < i; j++ {
if !space(json[j]) {
start = j
break
}
}
for j := i - 1; j > start; j-- {
if !space(json[j]) {
end = j
break
}
}
if json[start] == 34 && json[end] == 34 {
keepGoing, innerError := callback(key, json[start+1:end])
if innerError != nil {
return innerError
}
if !keepGoing {
return nil
}
} else {
keepGoing, innerError := callback(key, json[start:end+1])
if innerError != nil {
return innerError
}
if !keepGoing {
return nil
}
}
continue
}
if curr == 58 {
key = json[keyStart+1 : keyEnd]
start = i + 1
continue
}
}
}
}
return nil
}
return errObjectExpected()
}