-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinter_core.go
executable file
·490 lines (487 loc) · 12.1 KB
/
inter_core.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package jin
import (
"strconv"
)
// Only this function commented, other Get() and Set() functions based on same logic.
// Do not use with zero length path! no control for that
// Not for public usage
func core(json []byte, justStart bool, path ...string) (int, int, int, error) {
// null json control.
if len(json) < 2 {
return -1, -1, -1, errBadJSON(0)
}
// main offset track of this search.
offset := 0
currentPath := path[0]
// important chars for json interpretation.
// 34 = "
// 44 = ,
// 58 = :
// 91 = [
// 93 = ]
// 123 = {
// 125 = }
chars := []byte{34, 44, 58, 91, 93, 123, 125}
// creating a bool array fill with false
isJSONChar := make([]bool, 256)
// only interested chars is true
for _, v := range chars {
isJSONChar[v] = true
}
// trim spaces of start
for space(json[offset]) {
// json length overflow control
if offset > len(json)-1 {
return -1, -1, -1, errBadJSON(offset)
}
offset++
continue
}
// braceType determine whether or not search will be a key search or index search
braceType := json[offset]
// if last path pointing at a key, that placeholder stores key start point.
keyStart := -1
// main iteration off all bytes.
for k := 0; k < len(path); k++ {
// 91 = [, beginning of an array search
if braceType == 91 {
// ARRAY SEARCH SCOPE
// path value cast to integer for determine index.
arrayIndex, err := strconv.Atoi(currentPath)
if err != nil {
// braceType and current path type is conflicts.
return -1, -1, -1, errIndexExpected()
}
// zeroth index search.
if arrayIndex == 0 {
// Increment offset for not catch current brace.
offset++
// Inner iteration for brace type search.
for i := offset; i < len(json); i++ {
// curr is current byte of reading.
curr := json[i]
if !space(curr) {
// Open brace
if curr == 123 || curr == 91 {
// change brace type of next search.
braceType = curr
if k != len(path)-1 {
// If its not last path than change currentPath to next path.
currentPath = path[k+1]
}
// Assign offset to brace index.
offset = i
break
} else {
if k != len(path)-1 {
return -1, -1, -1, errIndexOutOfRange()
}
break
}
}
}
} else {
// Brace level every brace increments the level
level := 0
// main in quote flag for determine what is in quote and what is not
inQuote := false
// index found flag.
found := false
// Index count of current element.
indexCount := 0
// Not interested with column char in this search
isJSONChar[58] = false
for i := offset; i < len(json); i++ {
// curr is current byte of reading.
curr := json[i]
// Just interested with json chars. Other wise continue.
if !isJSONChar[curr] {
continue
}
// If current byte is quote
if curr == 34 {
for n := i - 1; n > -1; n-- {
if json[n] != 92 {
if (i-n)&1 != 0 {
inQuote = !inQuote
break
} else {
break
}
}
continue
}
continue
}
if inQuote {
continue
} else {
// Not found before
if !found {
// same level with path
if level == 1 {
// current byte is comma
if curr == 44 {
// Inc index
indexCount++
if indexCount == arrayIndex {
found = true
for j := i + 1; j < len(json); j++ {
curr := json[j]
if !space(curr) {
if curr != 91 && curr != 123 {
if k != len(path)-1 {
return -1, -1, -1, errIndexOutOfRange()
}
break
}
break
}
}
if k == len(path)-1 {
// last path found, break
offset = i + 1
break
} else {
continue
}
// keep going for find next brace Type.
}
continue
}
}
}
// Open braces
if curr == 91 || curr == 123 {
// if found before done with this search
// break array search scope
if found {
offset = i
braceType = curr
currentPath = path[k+1]
break
} else {
level++
continue
}
}
if curr == 93 || curr == 125 {
// if level is less than 1 it mean index not in this array.
if level < 2 {
return -1, -1, -1, errIndexOutOfRange()
}
level--
continue
}
}
}
if !found {
return -1, -1, -1, errIndexOutOfRange()
}
// Check true for column char again for keep same with first declaration.
isJSONChar[58] = true
}
} else {
// KEY SEARCH SCOPE
// main in quote flag for determine what is in quote and what is not.
inQuote := false
// Key found flag.
found := false
// Key start index.
start := 0
// Key end index.
end := 0
// Current level.
level := k
// Not interested with comma in this search
isJSONChar[44] = false
for i := offset; i < len(json); i++ {
// curr is current byte of reading.
curr := json[i]
// Just interested with json chars. Other wise continue.
if !isJSONChar[curr] {
continue
}
// If current byte is quote
if curr == 34 {
// If key found no need to determine start and end points.
if found {
continue
}
// If level not same as path level no need to determine start and end points.
if level != k+1 {
continue
}
// escape char control algorithm
for n := i - 1; n > -1; n-- {
if json[n] != 92 {
if (i-n)&1 != 0 {
inQuote = !inQuote
break
} else {
goto cont
}
}
continue
}
// If starting new quote that means key starts here
if inQuote {
start = i + 1
continue
}
// if quote ends that means key ends here
end = i
cont:
continue
}
if inQuote {
continue
} else {
// same level with path
if level == k+1 {
// column
if curr == 58 {
// comp between current path and key
// length comp between current path and key
if len(currentPath) == end-start {
same := true
// compare all elements
for j := 0; j < len(currentPath); j++ {
if currentPath[j] != json[start+j] {
same = false
break
} else {
continue
}
}
if same {
offset = i + 1
found = true
// if it is the last path element break
// and include comma character to json chars.
if k == len(path)-1 {
keyStart = start
break
} else {
continue
}
}
}
// Include comma character to json chars for jump function
isJSONChar[44] = true
// exclude column character to json chars for jump function
isJSONChar[58] = false
// exclude space character to json chars for jump function
// jump function start :{} -> ,
// it is fast travel from column to comma
// first we need keys
// for this purpose skipping values.
// Only need value if key is correct
innerLevel := level
for j := i; j < len(json); j++ {
// curr is current byte of reading.
curr := json[j]
// Just interested with json chars. Other wise continue.
if !isJSONChar[curr] {
continue
}
// Quote
if curr == 34 {
// check before char it might be escape char.
// escape char control algorithm
for n := j - 1; n > -1; n-- {
if json[n] != 92 {
if (j-1-n)%2 == 0 {
inQuote = !inQuote
break
} else {
break
}
}
continue
}
continue
}
if inQuote {
continue
} else {
// This brace conditions for level trace
// it is necessary to keep level value correct
if curr == 91 || curr == 123 {
innerLevel++
continue
}
if curr == 93 || curr == 125 {
if innerLevel < k+1 {
return -1, -1, -1, errKeyNotFound(currentPath)
}
innerLevel--
continue
}
// comma
if curr == 44 {
// level same with path
if innerLevel == k+1 {
// jump i to j
i = j
break
} else {
continue
}
}
continue
}
}
// exclude comma character to json chars, jump func is ending.
isJSONChar[44] = false
// Include column character to json chars, jump func is ending.
isJSONChar[58] = true
// Include space character to json chars, jump func is ending.
continue
}
}
// open square brace
if curr == 91 {
// if found and new brace is square brace than
// next search is array search break loop and
// update the current path
if found {
offset = i
braceType = curr
currentPath = path[k+1]
break
} else {
level++
continue
}
}
// open curly brace
if curr == 123 {
// if found and new brace is curly brace than
// next search is key search continue with this loop and
// update the current path
// close found flag for next search.
if found {
k++
currentPath = path[k]
found = false
}
level++
continue
}
// Close brace
if curr == 93 || curr == 125 {
if level < k+1 {
return -1, -1, -1, errKeyNotFound(currentPath)
}
level--
continue
}
}
}
// key not found return error
if !found {
return -1, -1, -1, errKeyNotFound(currentPath)
}
// Include comma character to json chars to restore original.
isJSONChar[44] = true
// Include space character to json chars to restore original.
}
}
// this means not search operation has take place
// it must be some kinda error or bad format
if offset == 0 {
return -1, -1, -1, errBadJSON(0)
}
// skip spaces from top.
for space(json[offset]) {
// json length overflow control
if offset > len(json)-1 {
return -1, -1, -1, errBadJSON(offset)
}
offset++
continue
}
if justStart {
return keyStart, offset, 0, nil
}
// If value starts with open braces
if json[offset] == 91 || json[offset] == 123 {
// main level indicator.
level := 0
// Quote check flag
inQuote := false
for i := offset; i < len(json); i++ {
// curr is current byte of reading.
curr := json[i]
// Just interested with json chars. Other wise continue.
if !isJSONChar[curr] {
continue
}
if curr == 34 {
// check before char it might be escape char.
// escape char control algorithm
for n := i - 1; n > -1; n-- {
if json[n] != 92 {
if (i-n)%2 != 0 {
inQuote = !inQuote
break
} else {
break
}
}
continue
}
continue
}
if inQuote {
continue
} else {
if curr == 91 || curr == 123 {
level++
continue
}
if curr == 93 || curr == 125 {
if level == 1 {
return keyStart, offset, i + 1, nil
}
level--
continue
}
continue
}
}
} else {
// If value starts with quote
if json[offset] == 34 {
for i := offset + 1; i < len(json); i++ {
curr := json[i]
if curr == 92 {
i++
continue
} else {
// find ending quote
if curr == 34 {
// just interested with json chars. Other wise continue.
return keyStart, offset + 1, i, nil
}
}
}
} else {
for i := offset; i < len(json); i++ {
curr := json[i]
// if current byte is space or one of these ',' ']' '}' this means end of the value is i
if space(curr) || curr == 44 || curr == 93 || curr == 125 {
if offset == i {
return -1, -1, -1, errEmptyArray()
}
return keyStart, offset, i, nil
}
}
}
}
// This means not search operation has take place
// not any formatting operation has take place
// it must be some kinda bad JSON format
return -1, -1, -1, errBadJSON(offset)
}