@@ -10,11 +10,17 @@ import (
10
10
)
11
11
12
12
// Find position of next character which is not ' ', ',', '}' or ']'
13
- func nextValue (data []byte ) int {
13
+ func nextValue (data []byte , skipComma bool ) int {
14
14
for i , c := range data {
15
15
switch c {
16
- case ' ' , '\n' , '\r' , '\t' , ',' :
16
+ case ' ' , '\n' , '\r' , '\t' :
17
17
continue
18
+ case ',' :
19
+ if ! skipComma {
20
+ continue
21
+ } else {
22
+ return i
23
+ }
18
24
default :
19
25
return i
20
26
}
@@ -82,43 +88,35 @@ func searchKeys(data []byte, keys ...string) int {
82
88
ln := len (data )
83
89
lk := len (keys )
84
90
85
- for true {
86
- if i >= ln {
87
- return - 1
88
- }
89
-
91
+ for i < ln {
90
92
switch data [i ] {
91
93
case '"' :
92
94
i ++
95
+ keyBegin := i
93
96
94
- se := stringEnd (data [i :])
95
- if se == - 1 {
97
+ strEnd := stringEnd (data [i :])
98
+ if strEnd == - 1 {
96
99
return - 1
97
100
}
101
+ i += strEnd
102
+ keyEnd := i - 1
98
103
99
- nO := nextValue (data [i + se :])
100
-
101
- if nO == - 1 {
104
+ valueOffset := nextValue (data [i :], true )
105
+ if valueOffset == - 1 {
102
106
return - 1
103
107
}
108
+ i += valueOffset
104
109
105
- if ln > i + se + nO &&
106
- data [i + se + nO ] == ':' && // if string is a Key, and key level match
110
+ if i < ln &&
111
+ data [i ] == ':' && // if string is a Key, and key level match
107
112
keyLevel == level - 1 && // If key nesting level match current object nested level
108
-
109
- // Checks to speedup key comparsion
110
- len (keys [level - 1 ]) == se - 1 && // if it have same length
111
- data [i ] == keys [level - 1 ][0 ] { // If first character same
112
- if keys [level - 1 ] == unsafeBytesToString (data [i :i + se - 1 ]) {
113
+ keys [level - 1 ] == unsafeBytesToString (data [keyBegin :keyEnd ]) {
113
114
keyLevel ++
114
115
// If we found all keys in path
115
116
if keyLevel == lk {
116
- return i + se + nO + 1
117
+ return i + 1
117
118
}
118
- }
119
119
}
120
-
121
- i += se + nO - 1
122
120
case '{' :
123
121
level ++
124
122
case '}' :
@@ -167,7 +165,7 @@ func Get(data []byte, keys ...string) (value []byte, dataType int, offset int, e
167
165
}
168
166
169
167
// Go to closest value
170
- nO := nextValue (data [offset :])
168
+ nO := nextValue (data [offset :], false )
171
169
172
170
if nO == - 1 {
173
171
return []byte {}, NotExist , - 1 , errors .New ("Malformed JSON error" )
@@ -176,7 +174,6 @@ func Get(data []byte, keys ...string) (value []byte, dataType int, offset int, e
176
174
offset += nO
177
175
178
176
endOffset := offset
179
-
180
177
// if string value
181
178
if data [offset ] == '"' {
182
179
dataType = String
@@ -267,7 +264,7 @@ func ArrayEach(data []byte, cb func(value []byte, dataType int, offset int, err
267
264
}
268
265
269
266
// Go to closest value
270
- nO := nextValue (data [offset :])
267
+ nO := nextValue (data [offset :], false )
271
268
272
269
if nO == - 1 {
273
270
return errors .New ("Malformed JSON" )
0 commit comments