omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / protocol / omnet / om / json / json.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Bounded JSON syntax, decoded tokens, exact integer conversion and output. |
| 3 | const jsonbad ulong 65537 |
| 4 | |
| 5 | fn jsonbyte o edit uchar 65536 p ulong b uchar to ulong |
| 6 | if ge p 65536 |
| 7 | ret jsonbad |
| 8 | end |
| 9 | put o p b |
| 10 | ret add p 1 |
| 11 | end |
| 12 | |
| 13 | fn jsonraw o edit uchar 65536 p ulong s view uchar 65536 n ulong to ulong |
| 14 | if or gt p 65536 gt n 65536 |
| 15 | ret jsonbad |
| 16 | end |
| 17 | if gt n sub ulong 65536 p |
| 18 | ret jsonbad |
| 19 | end |
| 20 | for i ulong n |
| 21 | put o add p i get s i |
| 22 | end |
| 23 | ret add p n |
| 24 | end |
| 25 | |
| 26 | fn jsonhex c ulong to ulong |
| 27 | if and ge c 48 le c 57 |
| 28 | ret sub c 48 |
| 29 | end |
| 30 | if and ge c 65 le c 70 |
| 31 | ret sub c 55 |
| 32 | end |
| 33 | if and ge c 97 le c 102 |
| 34 | ret sub c 87 |
| 35 | end |
| 36 | ret 16 |
| 37 | end |
| 38 | |
| 39 | fn jsonh4 s view uchar 65536 p ulong n ulong to ulong |
| 40 | if gt add p 4 n |
| 41 | ret 65536 |
| 42 | end |
| 43 | v = ulong 0 |
| 44 | for i ulong 4 |
| 45 | c = jsonhex ulong get s add p i |
| 46 | if eq c 16 |
| 47 | ret 65536 |
| 48 | end |
| 49 | v = add mul v 16 c |
| 50 | end |
| 51 | ret v |
| 52 | end |
| 53 | |
| 54 | note Decode one UTF8 scalar. Bad sequences consume one byte, matching Go. |
| 55 | fn jsonutf s view uchar 65536 p ulong n ulong r edit ulong 2 to void |
| 56 | put r 0 65533 |
| 57 | put r 1 1 |
| 58 | c = ulong get s p |
| 59 | if lt c 128 |
| 60 | put r 0 c |
| 61 | ret |
| 62 | end |
| 63 | w = ulong 0 |
| 64 | v = ulong 0 |
| 65 | if and ge c 194 le c 223 |
| 66 | w = 2 |
| 67 | v = and c 31 |
| 68 | end |
| 69 | if and ge c 224 le c 239 |
| 70 | w = 3 |
| 71 | v = and c 15 |
| 72 | end |
| 73 | if and ge c 240 le c 244 |
| 74 | w = 4 |
| 75 | v = and c 7 |
| 76 | end |
| 77 | if or eq w 0 gt add p w n |
| 78 | ret |
| 79 | end |
| 80 | i = ulong 1 |
| 81 | while lt i w bound 4 |
| 82 | c = ulong get s add p i |
| 83 | if or lt c 128 gt c 191 |
| 84 | ret |
| 85 | end |
| 86 | v = add mul v 64 and c 63 |
| 87 | i = add i 1 |
| 88 | end |
| 89 | if and eq w 3 lt v 2048 |
| 90 | ret |
| 91 | end |
| 92 | if and eq w 4 lt v 65536 |
| 93 | ret |
| 94 | end |
| 95 | if or gt v 1114111 and ge v 55296 le v 57343 |
| 96 | ret |
| 97 | end |
| 98 | put r 0 v |
| 99 | put r 1 w |
| 100 | end |
| 101 | |
| 102 | fn jsonrune o edit uchar 65536 p ulong v ulong to ulong |
| 103 | if lt v 128 |
| 104 | ret jsonbyte o p uchar v |
| 105 | end |
| 106 | if lt v 2048 |
| 107 | p = jsonbyte o p uchar add ulong 192 shr v 6 |
| 108 | else |
| 109 | if lt v 65536 |
| 110 | p = jsonbyte o p uchar add ulong 224 shr v 12 |
| 111 | else |
| 112 | p = jsonbyte o p uchar add ulong 240 shr v 18 |
| 113 | p = jsonbyte o p uchar add ulong 128 and shr v 12 63 |
| 114 | end |
| 115 | p = jsonbyte o p uchar add ulong 128 and shr v 6 63 |
| 116 | end |
| 117 | ret jsonbyte o p uchar add ulong 128 and v 63 |
| 118 | end |
| 119 | |
| 120 | note State: input length, cursor, decoded length, token count, parent, depth. |
| 121 | fn jsonesc s view uchar 65536 st edit ulong 8 to ulong |
| 122 | p = get st 1 |
| 123 | n = get st 0 |
| 124 | if ge p n |
| 125 | ret 1114112 |
| 126 | end |
| 127 | c = ulong get s p |
| 128 | put st 1 add p 1 |
| 129 | if or eq c 34 or eq c 47 eq c 92 |
| 130 | ret c |
| 131 | end |
| 132 | if eq c 98 |
| 133 | ret 8 |
| 134 | end |
| 135 | if eq c 102 |
| 136 | ret 12 |
| 137 | end |
| 138 | if eq c 110 |
| 139 | ret 10 |
| 140 | end |
| 141 | if eq c 114 |
| 142 | ret 13 |
| 143 | end |
| 144 | if eq c 116 |
| 145 | ret 9 |
| 146 | end |
| 147 | if ne c 117 |
| 148 | ret 1114112 |
| 149 | end |
| 150 | p = add p 1 |
| 151 | c = jsonh4 s p n |
| 152 | if eq c 65536 |
| 153 | ret 1114112 |
| 154 | end |
| 155 | p = add p 4 |
| 156 | put st 1 p |
| 157 | if and ge c 55296 le c 56319 |
| 158 | if le add p 6 n |
| 159 | if and eq get s p 92 eq get s add p 1 117 |
| 160 | v = jsonh4 s add p 2 n |
| 161 | if and ge v 56320 le v 57343 |
| 162 | put st 1 add p 6 |
| 163 | ret add ulong 65536 add mul sub c 55296 1024 sub v 56320 |
| 164 | end |
| 165 | end |
| 166 | end |
| 167 | end |
| 168 | if and ge c 55296 le c 57343 |
| 169 | ret 65533 |
| 170 | end |
| 171 | ret c |
| 172 | end |
| 173 | |
| 174 | fn jsonstring s view uchar 65536 st edit ulong 8 d edit uchar 65536 to int |
| 175 | r = new ulong 2 0 |
| 176 | put st 1 add get st 1 1 |
| 177 | while lt get st 1 get st 0 bound 65536 |
| 178 | p = get st 1 |
| 179 | c = ulong get s p |
| 180 | put st 1 add p 1 |
| 181 | if eq c 34 |
| 182 | ret 0 |
| 183 | end |
| 184 | if lt c 32 |
| 185 | ret 1 |
| 186 | end |
| 187 | if eq c 92 |
| 188 | c = jsonesc s st |
| 189 | if gt c 1114111 |
| 190 | ret 1 |
| 191 | end |
| 192 | else |
| 193 | if ge c 128 |
| 194 | jsonutf s p get st 0 r |
| 195 | c = get r 0 |
| 196 | put st 1 add p get r 1 |
| 197 | end |
| 198 | end |
| 199 | p = jsonrune d get st 2 c |
| 200 | if eq p jsonbad |
| 201 | ret 2 |
| 202 | end |
| 203 | put st 2 p |
| 204 | end |
| 205 | ret 1 |
| 206 | end |
| 207 | |
| 208 | fn jsondigit c uchar to bool |
| 209 | ret and ge c 48 le c 57 |
| 210 | end |
| 211 | |
| 212 | fn jsonnumber s view uchar 65536 st edit ulong 8 d edit uchar 65536 to int |
| 213 | p = get st 1 |
| 214 | beg = p |
| 215 | n = get st 0 |
| 216 | if eq get s p 45 |
| 217 | p = add p 1 |
| 218 | end |
| 219 | if ge p n |
| 220 | ret 1 |
| 221 | end |
| 222 | if eq get s p 48 |
| 223 | p = add p 1 |
| 224 | else |
| 225 | if not jsondigit get s p |
| 226 | ret 1 |
| 227 | end |
| 228 | while lt p n bound 65536 |
| 229 | if not jsondigit get s p |
| 230 | break |
| 231 | end |
| 232 | p = add p 1 |
| 233 | end |
| 234 | end |
| 235 | if lt p n |
| 236 | if eq get s p 46 |
| 237 | p = add p 1 |
| 238 | begd = p |
| 239 | while lt p n bound 65536 |
| 240 | if not jsondigit get s p |
| 241 | break |
| 242 | end |
| 243 | p = add p 1 |
| 244 | end |
| 245 | if eq p begd |
| 246 | ret 1 |
| 247 | end |
| 248 | end |
| 249 | end |
| 250 | if lt p n |
| 251 | if or eq get s p 101 eq get s p 69 |
| 252 | p = add p 1 |
| 253 | if lt p n |
| 254 | if or eq get s p 43 eq get s p 45 |
| 255 | p = add p 1 |
| 256 | end |
| 257 | end |
| 258 | begd = p |
| 259 | while lt p n bound 65536 |
| 260 | if not jsondigit get s p |
| 261 | break |
| 262 | end |
| 263 | p = add p 1 |
| 264 | end |
| 265 | if eq p begd |
| 266 | ret 1 |
| 267 | end |
| 268 | end |
| 269 | end |
| 270 | cnt = sub p beg |
| 271 | if gt cnt sub ulong 65536 get st 2 |
| 272 | ret 2 |
| 273 | end |
| 274 | for i ulong cnt |
| 275 | put d add get st 2 i get s add beg i |
| 276 | end |
| 277 | put st 2 add get st 2 cnt |
| 278 | put st 1 p |
| 279 | ret 0 |
| 280 | end |
| 281 | |
| 282 | fn jsonoctet v ulong i ulong to uchar |
| 283 | ret uchar and shr v mul i 8 255 |
| 284 | end |
| 285 | |
| 286 | fn jsonliteral s view uchar 65536 st edit ulong 8 kind ulong to int |
| 287 | p = get st 1 |
| 288 | n = ulong 4 |
| 289 | v = ulong 1702195828 |
| 290 | if eq kind 6 |
| 291 | n = 5 |
| 292 | v = 435728179558 |
| 293 | end |
| 294 | if eq kind 7 |
| 295 | v = 1819047278 |
| 296 | end |
| 297 | if gt add p n get st 0 |
| 298 | ret 1 |
| 299 | end |
| 300 | for i ulong n |
| 301 | if ne get s add p i jsonoctet v i |
| 302 | ret 1 |
| 303 | end |
| 304 | end |
| 305 | put st 1 add p n |
| 306 | ret 0 |
| 307 | end |
| 308 | |
| 309 | fn jsonequal d view uchar 65536 a ulong b ulong n ulong to bool |
| 310 | for i ulong n |
| 311 | if ne get d add a i get d add b i |
| 312 | ret false |
| 313 | end |
| 314 | end |
| 315 | ret true |
| 316 | end |
| 317 | |
| 318 | fn jsonkeyeq t view ulong 32768 d view uchar 65536 a ulong b ulong to bool |
| 319 | if ne get t add a 2 get t add b 2 |
| 320 | ret false |
| 321 | end |
| 322 | ret jsonequal d get t add a 1 get t add b 1 get t add b 2 |
| 323 | end |
| 324 | |
| 325 | fn jsondupe t view ulong 32768 d view uchar 65536 id ulong to bool |
| 326 | b = mul id 8 |
| 327 | par = get t add b 4 |
| 328 | i = add par 1 |
| 329 | while lt i id bound 4096 |
| 330 | a = mul i 8 |
| 331 | if eq get t add a 6 1 |
| 332 | if jsonkeyeq t d a b |
| 333 | ret true |
| 334 | end |
| 335 | end |
| 336 | i = get t add a 3 |
| 337 | end |
| 338 | ret false |
| 339 | end |
| 340 | |
| 341 | fn jsonfinish t edit ulong 32768 st view ulong 8 b ulong to void |
| 342 | put t add b 2 sub get st 2 get t add b 1 |
| 343 | end |
| 344 | |
| 345 | fn jsonvalue s view uchar 65536 st edit ulong 8 t edit ulong 32768 d edit uchar 65536 key ulong to int |
| 346 | if ge get st 3 4095 |
| 347 | ret 3 |
| 348 | end |
| 349 | id = add get st 3 1 |
| 350 | put st 3 id |
| 351 | b = mul id 8 |
| 352 | for i ulong 8 |
| 353 | put t add b i 0 |
| 354 | end |
| 355 | put t b 4 |
| 356 | put t add b 1 get st 2 |
| 357 | put t add b 3 add id 1 |
| 358 | put t add b 4 get st 4 |
| 359 | put t add b 6 key |
| 360 | c = get s get st 1 |
| 361 | if or eq c 123 eq c 91 |
| 362 | if ge get st 5 32 |
| 363 | ret 4 |
| 364 | end |
| 365 | put t b 1 |
| 366 | if eq c 91 |
| 367 | put t b 2 |
| 368 | end |
| 369 | put st 4 id |
| 370 | put st 5 add get st 5 1 |
| 371 | put st 1 add get st 1 1 |
| 372 | ret 0 |
| 373 | end |
| 374 | e = int 0 |
| 375 | if eq c 34 |
| 376 | put t b 3 |
| 377 | e = jsonstring s st d |
| 378 | else |
| 379 | if or eq c 116 or eq c 102 eq c 110 |
| 380 | put t b 5 |
| 381 | if eq c 102 |
| 382 | put t b 6 |
| 383 | end |
| 384 | if eq c 110 |
| 385 | put t b 7 |
| 386 | end |
| 387 | e = jsonliteral s st get t b |
| 388 | else |
| 389 | e = jsonnumber s st d |
| 390 | end |
| 391 | end |
| 392 | if ne e 0 |
| 393 | ret e |
| 394 | end |
| 395 | jsonfinish t st b |
| 396 | if eq key 1 |
| 397 | if jsondupe t d id |
| 398 | ret 5 |
| 399 | end |
| 400 | end |
| 401 | ret 0 |
| 402 | end |
| 403 | |
| 404 | fn jsonspace c uchar to bool |
| 405 | ret or eq c 32 or eq c 9 or eq c 10 eq c 13 |
| 406 | end |
| 407 | |
| 408 | note Container phase 0 initial, 1 separator, 2 after comma, 3 colon, 4 object value. |
| 409 | fn jsonstep s view uchar 65536 st edit ulong 8 t edit ulong 32768 d edit uchar 65536 to int |
| 410 | par = get st 4 |
| 411 | b = mul par 8 |
| 412 | phase = get t add b 7 |
| 413 | c = get s get st 1 |
| 414 | obj = eq get t b 1 |
| 415 | if or eq phase 0 eq phase 1 |
| 416 | if or and obj eq c 125 and not obj eq c 93 |
| 417 | put t add b 3 add get st 3 1 |
| 418 | put st 4 get t add b 4 |
| 419 | put st 5 sub get st 5 1 |
| 420 | put st 1 add get st 1 1 |
| 421 | ret 0 |
| 422 | end |
| 423 | end |
| 424 | if eq phase 1 |
| 425 | if ne c 44 |
| 426 | ret 1 |
| 427 | end |
| 428 | put t add b 7 2 |
| 429 | put st 1 add get st 1 1 |
| 430 | ret 0 |
| 431 | end |
| 432 | if eq phase 3 |
| 433 | if ne c 58 |
| 434 | ret 1 |
| 435 | end |
| 436 | put t add b 7 4 |
| 437 | put st 1 add get st 1 1 |
| 438 | ret 0 |
| 439 | end |
| 440 | key = ulong 0 |
| 441 | put t add b 7 1 |
| 442 | if and obj ne phase 4 |
| 443 | if ne c 34 |
| 444 | ret 1 |
| 445 | end |
| 446 | if ge get t add b 5 512 |
| 447 | ret 6 |
| 448 | end |
| 449 | key = 1 |
| 450 | put t add b 7 3 |
| 451 | end |
| 452 | put t add b 5 add get t add b 5 1 |
| 453 | ret jsonvalue s st t d key |
| 454 | end |
| 455 | |
| 456 | fn jsonparse s view uchar 65536 n ulong t edit ulong 32768 d edit uchar 65536 to int |
| 457 | put t 0 0 |
| 458 | if or eq n 0 gt n 65536 |
| 459 | ret 2 |
| 460 | end |
| 461 | st = new ulong 8 0 |
| 462 | put st 0 n |
| 463 | e = int 0 |
| 464 | while lt get st 1 n bound 65536 |
| 465 | if jsonspace get s get st 1 |
| 466 | put st 1 add get st 1 1 |
| 467 | else |
| 468 | if eq get st 4 0 |
| 469 | if ne get st 3 0 |
| 470 | ret 1 |
| 471 | end |
| 472 | e = jsonvalue s st t d 0 |
| 473 | else |
| 474 | e = jsonstep s st t d |
| 475 | end |
| 476 | if ne e 0 |
| 477 | ret e |
| 478 | end |
| 479 | end |
| 480 | end |
| 481 | if or ne get st 4 0 eq get st 3 0 |
| 482 | ret 1 |
| 483 | end |
| 484 | put t 0 get st 3 |
| 485 | ret 0 |
| 486 | end |
| 487 | |
| 488 | fn jsonuint d view uchar 65536 off ulong n ulong r edit ulong 1 to int |
| 489 | put r 0 0 |
| 490 | if or gt off 65536 or eq n 0 gt n 20 |
| 491 | ret 1 |
| 492 | end |
| 493 | if gt n sub ulong 65536 off |
| 494 | ret 1 |
| 495 | end |
| 496 | if and gt n 1 eq get d off 48 |
| 497 | ret 1 |
| 498 | end |
| 499 | v = ulong 0 |
| 500 | for i ulong n |
| 501 | c = ulong get d add off i |
| 502 | if or lt c 48 gt c 57 |
| 503 | ret 1 |
| 504 | end |
| 505 | c = sub c 48 |
| 506 | if gt v div sub ulong 18446744073709551615 c 10 |
| 507 | ret 1 |
| 508 | end |
| 509 | v = add mul v 10 c |
| 510 | end |
| 511 | put r 0 v |
| 512 | ret 0 |
| 513 | end |
| 514 | |
| 515 | fn jsonint d view uchar 65536 off ulong n ulong r edit long 1 to int |
| 516 | put r 0 0 |
| 517 | if or ge off 65536 eq n 0 |
| 518 | ret 1 |
| 519 | end |
| 520 | negv = eq get d off 45 |
| 521 | if negv |
| 522 | off = add off 1 |
| 523 | n = sub n 1 |
| 524 | end |
| 525 | v = new ulong 1 0 |
| 526 | if ne jsonuint d off n v 0 |
| 527 | ret 1 |
| 528 | end |
| 529 | x = get v 0 |
| 530 | if negv |
| 531 | if gt x 9223372036854775808 |
| 532 | ret 1 |
| 533 | end |
| 534 | if eq x 9223372036854775808 |
| 535 | put r 0 -9223372036854775808 |
| 536 | ret 0 |
| 537 | end |
| 538 | put r 0 neg long x |
| 539 | else |
| 540 | if gt x 9223372036854775807 |
| 541 | ret 1 |
| 542 | end |
| 543 | put r 0 long x |
| 544 | end |
| 545 | ret 0 |
| 546 | end |
| 547 | |
| 548 | fn jsoncopy t view ulong 32768 d view uchar 65536 id ulong o edit uchar 65536 to ulong |
| 549 | if or eq id 0 gt id get t 0 |
| 550 | ret jsonbad |
| 551 | end |
| 552 | b = mul id 8 |
| 553 | n = get t add b 2 |
| 554 | for i ulong n |
| 555 | put o i get d add get t add b 1 i |
| 556 | end |
| 557 | ret n |
| 558 | end |
| 559 | |
| 560 | fn jsonname d view uchar 65536 off ulong key view uchar 65536 n ulong to bool |
| 561 | for j ulong n |
| 562 | if ne get key j get d add off j |
| 563 | ret false |
| 564 | end |
| 565 | end |
| 566 | ret true |
| 567 | end |
| 568 | |
| 569 | fn jsonfind t view ulong 32768 d view uchar 65536 obj ulong key view uchar 65536 n ulong to ulong |
| 570 | if or eq obj 0 gt obj get t 0 |
| 571 | ret 0 |
| 572 | end |
| 573 | if or ne get t mul obj 8 1 gt n 65536 |
| 574 | ret 0 |
| 575 | end |
| 576 | i = add obj 1 |
| 577 | while lt i get t add mul obj 8 3 bound 4096 |
| 578 | b = mul i 8 |
| 579 | if eq n get t add b 2 |
| 580 | if jsonname d get t add b 1 key n |
| 581 | ret add i 1 |
| 582 | end |
| 583 | end |
| 584 | i = get t add mul add i 1 8 3 |
| 585 | end |
| 586 | ret 0 |
| 587 | end |
| 588 | |
| 589 | fn jsonhc v ulong to uchar |
| 590 | if lt v 10 |
| 591 | ret uchar add v 48 |
| 592 | end |
| 593 | ret uchar add v 87 |
| 594 | end |
| 595 | |
| 596 | fn jsonuesc o edit uchar 65536 p ulong v ulong to ulong |
| 597 | p = jsonbyte o p 92 |
| 598 | p = jsonbyte o p 117 |
| 599 | p = jsonbyte o p jsonhc and shr v 12 15 |
| 600 | p = jsonbyte o p jsonhc and shr v 8 15 |
| 601 | p = jsonbyte o p jsonhc and shr v 4 15 |
| 602 | ret jsonbyte o p jsonhc and v 15 |
| 603 | end |
| 604 | |
| 605 | fn jsonneeds v ulong to bool |
| 606 | ret or lt v 32 or eq v 60 or eq v 62 or eq v 38 or eq v 8232 eq v 8233 |
| 607 | end |
| 608 | |
| 609 | fn jsonquote o edit uchar 65536 p ulong s view uchar 65536 off ulong n ulong to ulong |
| 610 | if or gt n 65536 gt off 65536 |
| 611 | ret jsonbad |
| 612 | end |
| 613 | if gt n sub ulong 65536 off |
| 614 | ret jsonbad |
| 615 | end |
| 616 | n = add off n |
| 617 | p = jsonbyte o p 34 |
| 618 | i = off |
| 619 | r = new ulong 2 0 |
| 620 | while lt i n bound 65536 |
| 621 | jsonutf s i n r |
| 622 | v = get r 0 |
| 623 | i = add i get r 1 |
| 624 | if or eq v 34 eq v 92 |
| 625 | p = jsonbyte o p 92 |
| 626 | p = jsonbyte o p uchar v |
| 627 | else |
| 628 | if jsonneeds v |
| 629 | c = ulong 0 |
| 630 | if eq v 8 |
| 631 | c = 98 |
| 632 | end |
| 633 | if eq v 9 |
| 634 | c = 116 |
| 635 | end |
| 636 | if eq v 10 |
| 637 | c = 110 |
| 638 | end |
| 639 | if eq v 12 |
| 640 | c = 102 |
| 641 | end |
| 642 | if eq v 13 |
| 643 | c = 114 |
| 644 | end |
| 645 | if ne c 0 |
| 646 | p = jsonbyte o p 92 |
| 647 | p = jsonbyte o p uchar c |
| 648 | else |
| 649 | p = jsonuesc o p v |
| 650 | end |
| 651 | else |
| 652 | if and eq v 65533 eq get r 1 1 |
| 653 | p = jsonuesc o p v |
| 654 | else |
| 655 | p = jsonrune o p v |
| 656 | end |
| 657 | end |
| 658 | end |
| 659 | if eq p jsonbad |
| 660 | ret jsonbad |
| 661 | end |
| 662 | end |
| 663 | ret jsonbyte o p 34 |
| 664 | end |
| 665 | |
| 666 | fn jsonstr o edit uchar 65536 p ulong s view uchar 65536 n ulong to ulong |
| 667 | ret jsonquote o p s 0 n |
| 668 | end |
| 669 | |
| 670 | fn jsonu64 o edit uchar 65536 p ulong v ulong to ulong |
| 671 | buf = new uchar 20 0 |
| 672 | n = ulong 0 |
| 673 | while ne v 0 bound 20 |
| 674 | put buf n uchar add rem v 10 48 |
| 675 | n = add n 1 |
| 676 | v = div v 10 |
| 677 | end |
| 678 | if eq n 0 |
| 679 | ret jsonbyte o p 48 |
| 680 | end |
| 681 | while gt n 0 bound 20 |
| 682 | n = sub n 1 |
| 683 | p = jsonbyte o p get buf n |
| 684 | end |
| 685 | ret p |
| 686 | end |
| 687 | |
| 688 | fn jsoni64 o edit uchar 65536 p ulong v long to ulong |
| 689 | if ge v 0 |
| 690 | ret jsonu64 o p ulong v |
| 691 | end |
| 692 | p = jsonbyte o p 45 |
| 693 | if eq v -9223372036854775808 |
| 694 | ret jsonu64 o p 9223372036854775808 |
| 695 | end |
| 696 | ret jsonu64 o p ulong neg v |
| 697 | end |
| 698 | |
| 699 | fn jsonspan o edit uchar 65536 p ulong s view uchar 65536 off ulong n ulong to ulong |
| 700 | if or gt off 65536 gt n 65536 |
| 701 | ret jsonbad |
| 702 | end |
| 703 | if gt n sub ulong 65536 off |
| 704 | ret jsonbad |
| 705 | end |
| 706 | if gt p 65536 |
| 707 | ret jsonbad |
| 708 | end |
| 709 | if gt n sub ulong 65536 p |
| 710 | ret jsonbad |
| 711 | end |
| 712 | for i ulong n |
| 713 | put o add p i get s add off i |
| 714 | end |
| 715 | ret add p n |
| 716 | end |
| 717 | |
| 718 | fn jsonword o edit uchar 65536 p ulong v ulong n ulong to ulong |
| 719 | for i ulong n |
| 720 | p = jsonbyte o p uchar and shr v mul i 8 255 |
| 721 | end |
| 722 | ret p |
| 723 | end |
| 724 | |
| 725 | fn jsonatom t view ulong 32768 d view uchar 65536 id ulong o edit uchar 65536 p ulong to ulong |
| 726 | b = mul id 8 |
| 727 | k = get t b |
| 728 | if eq k 1 |
| 729 | ret jsonbyte o p 123 |
| 730 | end |
| 731 | if eq k 2 |
| 732 | ret jsonbyte o p 91 |
| 733 | end |
| 734 | if eq k 3 |
| 735 | ret jsonquote o p d get t add b 1 get t add b 2 |
| 736 | end |
| 737 | if eq k 4 |
| 738 | ret jsonspan o p d get t add b 1 get t add b 2 |
| 739 | end |
| 740 | if eq k 5 |
| 741 | ret jsonword o p 1702195828 4 |
| 742 | end |
| 743 | if eq k 6 |
| 744 | ret jsonword o p 435728179558 5 |
| 745 | end |
| 746 | ret jsonword o p 1819047278 4 |
| 747 | end |
| 748 | |
| 749 | fn jsoncomma t view ulong 32768 b ulong par ulong to bool |
| 750 | ret or eq get t mul par 8 2 eq get t add b 6 1 |
| 751 | end |
| 752 | |
| 753 | note Token table must be from a successful parse and unmodified. |
| 754 | fn jsonencode t view ulong 32768 d view uchar 65536 o edit uchar 65536 to ulong |
| 755 | if or eq get t 0 0 gt get t 0 4095 |
| 756 | ret jsonbad |
| 757 | end |
| 758 | p = ulong 0 |
| 759 | cur = ulong 0 |
| 760 | id = ulong 1 |
| 761 | while le id get t 0 bound 4095 |
| 762 | b = mul id 8 |
| 763 | par = get t add b 4 |
| 764 | while ne cur par bound 32 |
| 765 | c = uchar 125 |
| 766 | if eq get t mul cur 8 2 |
| 767 | c = 93 |
| 768 | end |
| 769 | p = jsonbyte o p c |
| 770 | cur = get t add mul cur 8 4 |
| 771 | end |
| 772 | if ne par 0 |
| 773 | if gt id add par 1 |
| 774 | if jsoncomma t b par |
| 775 | p = jsonbyte o p 44 |
| 776 | end |
| 777 | end |
| 778 | end |
| 779 | p = jsonatom t d id o p |
| 780 | if eq get t add b 6 1 |
| 781 | p = jsonbyte o p 58 |
| 782 | end |
| 783 | if le get t b 2 |
| 784 | cur = id |
| 785 | end |
| 786 | if eq p jsonbad |
| 787 | ret jsonbad |
| 788 | end |
| 789 | id = add id 1 |
| 790 | end |
| 791 | while ne cur 0 bound 32 |
| 792 | c = uchar 125 |
| 793 | if eq get t mul cur 8 2 |
| 794 | c = 93 |
| 795 | end |
| 796 | p = jsonbyte o p c |
| 797 | cur = get t add mul cur 8 4 |
| 798 | end |
| 799 | ret p |
| 800 | end |