zkjson 0.1.21 → 0.1.23
Sign up to get free protection for your applications and to get access to all the features.
- package/encoder.js +74 -75
- package/package.json +1 -1
- package/parse.js +133 -92
- package/uint.js +246 -203
package/parse.js
CHANGED
@@ -1,94 +1,108 @@
|
|
1
|
-
const {
|
1
|
+
const {
|
2
|
+
toArray,
|
3
|
+
concat,
|
4
|
+
sum,
|
5
|
+
get,
|
6
|
+
popArray,
|
7
|
+
pushArray,
|
8
|
+
length,
|
9
|
+
next,
|
10
|
+
arr: _arr,
|
11
|
+
push,
|
12
|
+
pop,
|
13
|
+
last,
|
14
|
+
bn,
|
15
|
+
} = require("./uint")
|
2
16
|
const _null_ = [110, 117, 108, 108]
|
3
17
|
const _true_ = [116, 114, 117, 101]
|
4
18
|
const _false_ = [102, 97, 108, 115, 101]
|
5
19
|
|
6
|
-
|
20
|
+
function eql(a, b, blen, size) {
|
7
21
|
var alen = length(a, size)
|
8
22
|
if (alen != blen) return 0
|
9
23
|
var c = [0, size, 0, 0, 0, 0, 0, 0, 0]
|
10
|
-
for (
|
24
|
+
for (var i = 0; i < blen; i++) {
|
11
25
|
c = next(a, c)
|
12
26
|
if (c[0] != b[i]) return 0
|
13
27
|
}
|
14
28
|
return 1
|
15
29
|
}
|
16
30
|
|
17
|
-
|
18
|
-
const constVal = (_v, type, size) => {
|
31
|
+
const constVal = (_v, type, size, digit = 9) => {
|
19
32
|
var val = _arr(size)
|
20
|
-
val = push(val, size,
|
33
|
+
val = push(val, size, digit, type)
|
21
34
|
if (type == 1) {
|
22
35
|
if (eql(_v, _true_, 4, size) == 1) {
|
23
|
-
val = push(val, size,
|
36
|
+
val = push(val, size, digit, 1)
|
24
37
|
} else {
|
25
|
-
val = push(val, size,
|
38
|
+
val = push(val, size, digit, 0)
|
26
39
|
}
|
27
40
|
} else if (type == 3) {
|
28
|
-
val = push(val, size,
|
29
|
-
var c = [0, size, 0, 0, 0, 0, 0, 0, 0]
|
41
|
+
val = push(val, size, digit, length(_v, size))
|
42
|
+
var c = bn([0, size, 0, 0, 0, 0, 0, 0, 0])
|
30
43
|
while (c[5] == 0) {
|
31
44
|
c = next(_v, c)
|
32
|
-
val = push(val, size,
|
45
|
+
val = push(val, size, digit, c[0])
|
33
46
|
}
|
34
47
|
} else if (type == 2) {
|
35
48
|
if (get(_v, size, 0) == 45) {
|
36
|
-
val = push(val, size,
|
49
|
+
val = push(val, size, digit, 0)
|
37
50
|
} else {
|
38
|
-
val = push(val, size,
|
51
|
+
val = push(val, size, digit, 1)
|
39
52
|
}
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
var c = [0, size, 0, 0, 0, 0, 0, 0, 0]
|
53
|
+
var after = 0n
|
54
|
+
var right = 0n
|
55
|
+
var digits = 0n
|
56
|
+
var c = bn([0, size, 0, 0, 0, 0, 0, 0, 0])
|
44
57
|
while (c[5] == 0) {
|
45
58
|
c = next(_v, c)
|
46
59
|
if (c[0] == 46) {
|
47
60
|
after = 1
|
48
61
|
} else if (c[0] != 45) {
|
49
|
-
if (after == 1) right +=
|
50
|
-
digits = digits *
|
62
|
+
if (after == 1) right += 1n
|
63
|
+
digits = digits * 10n + (c[0] - 48n)
|
51
64
|
}
|
52
65
|
}
|
53
|
-
val = push(val, size,
|
54
|
-
val = push(val, size,
|
66
|
+
val = push(val, size, digit, right)
|
67
|
+
val = push(val, size, digit, digits)
|
55
68
|
}
|
56
69
|
return val
|
57
70
|
}
|
58
71
|
|
59
|
-
const constPath = (p, size) => {
|
72
|
+
const constPath = (p, size, digit = 9) => {
|
60
73
|
var pth2 = _arr(size)
|
61
|
-
var len = get(p,size,0)
|
62
|
-
pth2 =
|
63
|
-
for(var i =
|
64
|
-
var len2 = get(p,size,i +
|
65
|
-
var _sum = sum(p, size,
|
66
|
-
var first = get(p, size,
|
67
|
-
if(first == 0){
|
68
|
-
pth2 = push(pth2,size,
|
69
|
-
}else{
|
70
|
-
pth2 = push(pth2,size,
|
74
|
+
var len = get(p, size, 0)
|
75
|
+
pth2 = push(pth2, size, digit, len)
|
76
|
+
for (var i = 0n; i < len; i++) {
|
77
|
+
var len2 = get(p, size, i + 1n)
|
78
|
+
var _sum = sum(p, size, digit, 1, 1n + i)
|
79
|
+
var first = get(p, size, 1n + len + _sum)
|
80
|
+
if (first == 0) {
|
81
|
+
pth2 = push(pth2, size, digit, 0)
|
82
|
+
} else {
|
83
|
+
pth2 = push(pth2, size, digit, len2)
|
71
84
|
}
|
72
|
-
for(var i2 =
|
73
|
-
var v = get(p, size,
|
74
|
-
pth2 = push(pth2,size,
|
85
|
+
for (var i2 = 0n; i2 < len2; i2++) {
|
86
|
+
var v = get(p, size, 1n + len + _sum + i2)
|
87
|
+
pth2 = push(pth2, size, digit, v)
|
75
88
|
}
|
76
89
|
}
|
77
90
|
return pth2
|
78
91
|
}
|
79
92
|
|
80
|
-
const isNumber = (val,size, digit) =>{
|
93
|
+
const isNumber = (val, size, digit) => {
|
81
94
|
var len = length(val, size)
|
82
95
|
var c = [0, size, 0, 0, 0, 0, 0, 0, 0]
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
return
|
96
|
+
while (c[5] == 0) {
|
97
|
+
c = next(val, c)
|
98
|
+
if (c[0] == 47 || c[0] < 45 || c[0] > 57) return 0
|
99
|
+
}
|
100
|
+
|
101
|
+
return 1
|
89
102
|
}
|
90
103
|
|
91
|
-
const parse = (str, size = 100) => {
|
104
|
+
const parse = (str, size = 100, digit = 9) => {
|
105
|
+
str = bn(str)
|
92
106
|
var val = _arr(size)
|
93
107
|
var inVal = 0
|
94
108
|
var isNum = 0
|
@@ -101,7 +115,8 @@ const parse = (str, size = 100) => {
|
|
101
115
|
var ind = _arr(5)
|
102
116
|
var err = 0
|
103
117
|
var json = _arr(size)
|
104
|
-
|
118
|
+
var c = bn([0, size, 0, 0, 0, 0, 0, 0, 0])
|
119
|
+
var bare = 1
|
105
120
|
while (c[5] == 0) {
|
106
121
|
c = next(str, c)
|
107
122
|
var s = c[0]
|
@@ -110,31 +125,40 @@ const parse = (str, size = 100) => {
|
|
110
125
|
esc = 1
|
111
126
|
} else if (s == 34) {
|
112
127
|
if (esc == 1) {
|
113
|
-
val = push(val, size,
|
128
|
+
val = push(val, size, digit, s)
|
114
129
|
} else {
|
115
130
|
inVal = 0
|
116
131
|
if (nextKey == 1 && last(ao, 5) == 1) {
|
117
|
-
path = pushArray(path,size,
|
132
|
+
path = pushArray(path, size, digit, val, size)
|
118
133
|
} else {
|
119
134
|
if (last(ao, 5) == 2) {
|
120
135
|
var _ind = last(ind, 5)
|
121
136
|
var __ind = _arr(size)
|
122
|
-
__ind = push(__ind, size,
|
123
|
-
__ind = push(__ind, size,
|
124
|
-
path = pushArray(path, size,
|
137
|
+
__ind = push(__ind, size, digit, 0)
|
138
|
+
__ind = push(__ind, size, digit, _ind)
|
139
|
+
path = pushArray(path, size, digit, __ind, size)
|
125
140
|
ind = pop(ind, 5)
|
126
|
-
ind = push(ind, 5,
|
141
|
+
ind = push(ind, 5, digit, _ind + 1n)
|
127
142
|
}
|
128
|
-
json = concat(
|
129
|
-
|
143
|
+
json = concat(
|
144
|
+
json,
|
145
|
+
concat(
|
146
|
+
constPath(path, size, digit),
|
147
|
+
constVal(val, 3, size, digit),
|
148
|
+
size,
|
149
|
+
digit,
|
150
|
+
),
|
151
|
+
size,
|
152
|
+
digit,
|
153
|
+
)
|
154
|
+
if (bare == 0) path = popArray(path, size, digit)
|
130
155
|
}
|
131
|
-
|
132
156
|
val = _arr(size)
|
133
157
|
nextKey = 0
|
134
158
|
}
|
135
159
|
esc = 0
|
136
160
|
} else {
|
137
|
-
push(val, size,
|
161
|
+
val = push(val, size, digit, s)
|
138
162
|
esc = 0
|
139
163
|
}
|
140
164
|
} else if (isNum == 1) {
|
@@ -142,18 +166,17 @@ const parse = (str, size = 100) => {
|
|
142
166
|
if (last(ao, 5) == 2) {
|
143
167
|
var _ind = last(ind, 5)
|
144
168
|
var __ind = _arr(size)
|
145
|
-
__ind = push(__ind, size,
|
146
|
-
__ind = push(__ind, size,
|
147
|
-
path = pushArray(path, size,
|
169
|
+
__ind = push(__ind, size, digit, 0)
|
170
|
+
__ind = push(__ind, size, digit, _ind)
|
171
|
+
path = pushArray(path, size, digit, __ind, size)
|
148
172
|
ind = pop(ind, 5)
|
149
|
-
ind = push(ind, 5,
|
150
|
-
|
173
|
+
ind = push(ind, 5, digit, _ind + 1n)
|
151
174
|
}
|
152
175
|
if (
|
153
176
|
eql(val, _true_, 4, size) == 0 &&
|
154
177
|
eql(val, _false_, 5, size) == 0 &&
|
155
|
-
|
156
|
-
|
178
|
+
eql(val, _null_, 4, size) == 0 &&
|
179
|
+
isNumber(val, size, digit) == 0
|
157
180
|
) {
|
158
181
|
err = 1
|
159
182
|
}
|
@@ -166,26 +189,35 @@ const parse = (str, size = 100) => {
|
|
166
189
|
) {
|
167
190
|
type = 1
|
168
191
|
}
|
169
|
-
json = concat(
|
170
|
-
|
192
|
+
json = concat(
|
193
|
+
json,
|
194
|
+
concat(
|
195
|
+
constPath(path, size, digit),
|
196
|
+
constVal(val, type, size, digit),
|
197
|
+
size,
|
198
|
+
digit,
|
199
|
+
),
|
200
|
+
size,
|
201
|
+
digit,
|
202
|
+
)
|
203
|
+
path = popArray(path, size, digit)
|
171
204
|
if (s == 93) {
|
172
205
|
if (last(ao, 5) != 2) err = 1
|
173
|
-
ao
|
174
|
-
popArray(path, size,
|
206
|
+
ao = pop(ao, 5)
|
207
|
+
if (bare == 0) path = popArray(path, size, digit)
|
175
208
|
arr--
|
176
|
-
pop(ind, 5)
|
209
|
+
ind = pop(ind, 5)
|
177
210
|
}
|
178
211
|
if (s == 125) {
|
179
212
|
if (last(ao, 5) != 1) err = 1
|
180
|
-
pop(ao, 5)
|
181
|
-
popArray(path, size, 9)
|
213
|
+
ao = pop(ao, 5)
|
182
214
|
obj--
|
183
215
|
}
|
184
216
|
isNum = 0
|
185
217
|
val = _arr(size)
|
186
218
|
if (s == 44) nextKey = 1
|
187
219
|
} else {
|
188
|
-
val = push(val, size,
|
220
|
+
val = push(val, size, digit, s)
|
189
221
|
}
|
190
222
|
} else if (s == 34) {
|
191
223
|
inVal = 1
|
@@ -199,48 +231,50 @@ const parse = (str, size = 100) => {
|
|
199
231
|
s != 125
|
200
232
|
) {
|
201
233
|
isNum = 1
|
202
|
-
val = push(val, size,
|
234
|
+
val = push(val, size, digit, s)
|
203
235
|
} else {
|
204
236
|
if (s != 32) {
|
205
|
-
if (s == 123 || s == 44)
|
237
|
+
if (s == 123 || s == 44) {
|
238
|
+
bare = 0
|
239
|
+
nextKey = 1
|
240
|
+
}
|
206
241
|
if (s == 123) {
|
207
242
|
if (last(ao, 5) == 2) {
|
208
243
|
var _ind = last(ind, 5)
|
209
244
|
var __ind = _arr(size)
|
210
|
-
__ind = push(__ind, size,
|
211
|
-
__ind = push(__ind, size,
|
212
|
-
path = pushArray(path, size,
|
245
|
+
__ind = push(__ind, size, digit, 0)
|
246
|
+
__ind = push(__ind, size, digit, _ind)
|
247
|
+
path = pushArray(path, size, digit, __ind, size)
|
213
248
|
ind = pop(ind, 5)
|
214
|
-
ind = push(ind, 5,
|
249
|
+
ind = push(ind, 5, digit, _ind + 1n)
|
215
250
|
}
|
216
|
-
ao = push(ao, 5,
|
251
|
+
ao = push(ao, 5, digit, 1)
|
217
252
|
obj++
|
218
253
|
}
|
219
254
|
if (s == 125) {
|
220
255
|
if (last(ao, 5) != 1) err = 1
|
221
|
-
pop(ao, 5)
|
222
|
-
popArray(path, size, 9)
|
256
|
+
ao = pop(ao, 5)
|
223
257
|
obj--
|
224
258
|
}
|
225
259
|
if (s == 91) {
|
226
260
|
if (last(ao, 5) == 2) {
|
227
261
|
var _ind = last(ind, 5)
|
228
262
|
var __ind = _arr(size)
|
229
|
-
__ind = push(__ind, size,
|
230
|
-
__ind = push(__ind, size,
|
231
|
-
path = pushArray(path, size,
|
263
|
+
__ind = push(__ind, size, digit, 0)
|
264
|
+
__ind = push(__ind, size, digit, _ind)
|
265
|
+
path = pushArray(path, size, digit, __ind, size)
|
232
266
|
ind = pop(ind, 5)
|
233
|
-
ind = push(ind, 5,
|
267
|
+
ind = push(ind, 5, digit, _ind + 1n)
|
234
268
|
}
|
235
|
-
ind = push(ind, 5,
|
236
|
-
ao = push(ao, 5,
|
269
|
+
ind = push(ind, 5, digit, 0)
|
270
|
+
ao = push(ao, 5, digit, 2)
|
237
271
|
arr++
|
238
272
|
}
|
239
273
|
if (s == 93) {
|
240
274
|
if (last(ao, 5) != 2) err = 1
|
241
275
|
ao = pop(ao, 5)
|
242
276
|
ind = pop(ind, 5)
|
243
|
-
path = popArray(path, size,
|
277
|
+
path = popArray(path, size, digit)
|
244
278
|
arr--
|
245
279
|
}
|
246
280
|
}
|
@@ -248,20 +282,27 @@ const parse = (str, size = 100) => {
|
|
248
282
|
}
|
249
283
|
if (length(val, size) != 0) {
|
250
284
|
var type = 4
|
251
|
-
if(eql(val,_null_,4,size) == 1){
|
285
|
+
if (eql(val, _null_, 4, size) == 1) {
|
252
286
|
type = 0
|
253
287
|
isNum = 0
|
254
|
-
}else if(
|
255
|
-
eql(val, _true_, 4, size) == 1 ||
|
256
|
-
eql(val, _false_, 5, size)
|
257
|
-
){
|
288
|
+
} else if (eql(val, _true_, 4, size) == 1 || eql(val, _false_, 5, size)) {
|
258
289
|
type = 1
|
259
290
|
isNum = 0
|
260
|
-
} else if (isNumber(val, size,
|
291
|
+
} else if (isNumber(val, size, digit) == 1) {
|
261
292
|
type = 2
|
262
293
|
isNum = 0
|
263
294
|
}
|
264
|
-
json = concat(
|
295
|
+
json = concat(
|
296
|
+
json,
|
297
|
+
concat(
|
298
|
+
constPath(path, size, digit),
|
299
|
+
constVal(val, type, size, digit),
|
300
|
+
size,
|
301
|
+
digit,
|
302
|
+
),
|
303
|
+
size,
|
304
|
+
digit,
|
305
|
+
)
|
265
306
|
}
|
266
307
|
if (ao.length > 0) err = 1
|
267
308
|
if (ind.length > 0) err = 1
|