zkjson 0.1.21 → 0.1.22

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