-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse_core.go
More file actions
executable file
·118 lines (116 loc) · 2.39 KB
/
parse_core.go
File metadata and controls
executable file
·118 lines (116 loc) · 2.39 KB
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
package jin
import "strconv"
func pCore(json []byte, core *node) error {
lenj := len(json)
if lenj < 2 {
return nil
}
inQuote := false
braceList := makeSeq(4)
indexList := makeSeq(4)
indexList.push(0)
var start int
var end int
var key []byte
var valStart int
var last byte
chars := []byte{34, 44, 58, 91, 93, 123, 125}
isJSONChar := make([]bool, 256)
for _, v := range chars {
isJSONChar[v] = true
}
for space(json[start]) {
if start > len(json)-1 {
return nil
}
start++
}
for i := start; i < lenj; 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)&1 != 0 {
inQuote = !inQuote
break
} else {
goto cont
}
}
continue
}
} else {
inQuote = true
start = i + 1
continue
}
if inQuote {
start = i + 1
continue
}
end = i
cont:
continue
}
if inQuote {
continue
} else {
switch curr {
case 91, 123:
switch last {
case 58:
newNode := &node{up: core, label: byteArrayToString(key)}
core.down = append(core.down, newNode)
core = newNode
default:
newNode := &node{up: core, label: strconv.Itoa(indexList.last())}
core.down = append(core.down, newNode)
core = newNode
}
indexList.push(0)
braceList.push(i)
valStart = i + 1
last = curr
continue
case 93, 125:
switch last {
case 58:
newNode := &node{up: core, label: byteArrayToString(key), value: json[valStart:i]}
core.down = append(core.down, newNode)
case 44:
newNode := &node{up: core, label: strconv.Itoa(indexList.last()), value: json[valStart:i]}
core.down = append(core.down, newNode)
valStart = i + 1
}
core.value = json[braceList.pop() : i+1]
indexList.pop()
core = core.up
last = curr
continue
case 58:
key = json[start:end]
valStart = i + 1
last = 58
continue
case 44:
switch last {
case 58:
newNode := &node{up: core, label: byteArrayToString(key), value: json[valStart:i]}
core.down = append(core.down, newNode)
case 44, 91:
newNode := &node{up: core, label: strconv.Itoa(indexList.last()), value: json[valStart:i]}
core.down = append(core.down, newNode)
}
indexList.inc()
valStart = i + 1
last = 44
continue
}
}
}
return nil
}