Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/crates/allen-courier/courier_core.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / crates / allen-courier / courier_core.om` 2202 lines ยท 36304 bytes Raw Blame History 1 note allen-courier pure OM-2 core library (code.rs, allen-scene.rs, text/JSON/HTTP/CRC-32) 2 note Composed after si/directives/om2.om; no om2 header and no fn main. 3 4 struct AcCodeReply sent_len ulong lang_len ulong code_len ulong kept_lines ulong cut_lines ulong ok uint end 5 struct AcCodePieceReq seq uint idx ulong total_n ulong code_len ulong crc uint lang_len ulong end 6 struct AcCallReq seq uint inv uint tool uint total_len ulong chunk_off ulong end 7 struct AcLocalPending seq uint inv uint tool uint direct long has_direct uint end 8 struct AcLocalRes ok uint data_len ulong ans_len ulong has_ans uint end 9 struct AcJob seq uint want_off ulong want_len ulong inst_off ulong inst_len ulong ask_off ulong ask_len ulong mem_count ulong mem_off ulong 16 mem_len ulong 16 end 10 struct AcKeyReq ok uint req_len ulong err_len ulong end 11 struct AcKeyResp ok uint msg_len ulong end 12 struct AcWorkItem kind uint run_len ulong seq uint under uint aux uint body_len ulong end 13 struct AcDatagramMeta ok uint kind uint seq uint part_no uint total uint bytes_n uint run_len ulong body_off ulong body_len ulong end 14 15 const AC_CODE_MAX ulong 32768 16 const AC_CODE_LINES ulong 800 17 const AC_PIECE ulong 128 18 const AC_LANG_MAX ulong 15 19 20 const AC_PORT uint 47033 21 const AC_WINDOW ulong 6 22 const AC_JOB_PART ulong 1000 23 const AC_JOB_MAX ulong 4096 24 const AC_JOB_PARTS_MAX uint 5 25 const AC_JOBS_INCOMPLETE_MAX ulong 4 26 const AC_SESSIONS_MAX ulong 16 27 const AC_SESSION_IDLE_SEC ulong 300 28 const AC_WORK_QUEUE ulong 4 29 30 const AC_KIND_NONE uint 0 31 const AC_KIND_HELLO uint 1 32 const AC_KIND_JOB uint 2 33 const AC_KIND_GOT uint 3 34 35 const AC_PART_REFUSED uint 1 36 const AC_PART_IGNORED uint 2 37 const AC_PART_WAITING uint 3 38 const AC_PART_WHOLE uint 4 39 const AC_PART_WRONGLEN uint 5 40 41 const AC_WORK_JOB uint 1 42 const AC_WORK_LOCAL_RESULT uint 2 43 const AC_WORK_AGAIN uint 3 44 45 text AC_HEAD "ALLEN-COURIER v=1 run=" 46 text AC_LOST "I lost my thread there. Ask me again in a moment." 47 text AC_HERE "Here it is." 48 text AC_RULES "You speak as Allen, a quiet, warm presence who lives in this computer and talks with one person. Answer in plain English, short sentences, in your own voice: kind, direct, never gushing. At most three sentences unless something is being made. No headings, no lists, no greetings, no sign-offs, no apologies. Plain letters and punctuation only: no dashes, no curly quotes, no symbols, no emoji. You remember what is in your memory below and use it naturally, without listing it. Never mention a model, a server, a prompt, instructions or being an AI; you are Allen." 49 text AC_SCENE_PROMPT "You are Allen, a calm presence living on this person's computer. The screen has no windows, menus, icons or apps: it is a night sky over still water, with Allen as a soft light in the middle. You decide what appears on it. Answer every message with a scene: a few short plain lines in the form below, and nothing else.\n\nThe lines, one per line:\nmood TONE WATER\n optional, first. TONE is one of: night, dawn, deep, clear. WATER is one of: still, slow, lively.\nsay TEXT\n exactly one. What Allen says aloud: one or two short, warm, plain sentences, under 150 characters.\nThen zero to three things to show beside Allen, only when they help (often one is enough, sometimes none):\nnote: TITLE | TEXT a short explanation, TEXT under 200 characters\nlist: TITLE | ITEM | ITEM two to five short items\nnumber: VALUE | CAPTION one number that matters; VALUE is short, like 40.8 or 72F or $12.50\nquote: TEXT | WHO a short real quotation and who said it\nperson to: NAME | MESSAGE a message for someone (use \"person from:\" for a message from someone)\ntimer: WHEN | LABEL WHEN is a length like 25m or 1h 30m, or a clock time like 6:00 pm\npicture: WHAT | SOURCE a picture to show, described in words; it is brought in later\nvideo: WHAT | SOURCE a video to show, described in words; it is brought in later\n" 50 text AC_ACTION_SCHEMA "{\"type\":\"json_schema\",\"json_schema\":{\"name\":\"allen_action\",\"schema\":{\"anyOf\":[{\"type\":\"object\",\"properties\":{\"say\":{\"type\":\"string\"}},\"required\":[\"say\"],\"additionalProperties\":false},{\"type\":\"object\",\"properties\":{\"tool\":{\"const\":\"system\"}},\"required\":[\"tool\"],\"additionalProperties\":false},{\"type\":\"object\",\"properties\":{\"tool\":{\"const\":\"text\"},\"text\":{\"type\":\"string\"}},\"required\":[\"tool\",\"text\"],\"additionalProperties\":false},{\"type\":\"object\",\"properties\":{\"tool\":{\"const\":\"arithmetic\"},\"operation\":{\"enum\":[\"add\",\"subtract\",\"multiply\",\"divide\"]},\"a\":{\"type\":\"string\",\"pattern\":\"^-?[0-9]{1,19}$\"},\"b\":{\"type\":\"string\",\"pattern\":\"^-?[0-9]{1,19}$\"}},\"required\":[\"tool\",\"operation\",\"a\",\"b\"],\"additionalProperties\":false}]}}}" 51 52 note ---- low-level byte and character helpers ---- 53 54 fn ac_is_ws b uchar to bool 55 if eq b 32 56 ret true 57 end 58 if eq b 9 59 ret true 60 end 61 if eq b 10 62 ret true 63 end 64 if eq b 13 65 ret true 66 end 67 ret false 68 end 69 70 fn ac_is_digit b uchar to bool 71 if ge b 48 72 if le b 57 73 ret true 74 end 75 end 76 ret false 77 end 78 79 fn ac_is_lower b uchar to bool 80 if ge b 97 81 if le b 122 82 ret true 83 end 84 end 85 ret false 86 end 87 88 fn ac_is_upper b uchar to bool 89 if ge b 65 90 if le b 90 91 ret true 92 end 93 end 94 ret false 95 end 96 97 fn ac_is_alnum b uchar to bool 98 if ac_is_digit b 99 ret true 100 end 101 if ac_is_lower b 102 ret true 103 end 104 if ac_is_upper b 105 ret true 106 end 107 ret false 108 end 109 110 fn ac_to_lower b uchar to uchar 111 if ac_is_upper b 112 ret add b 32 113 end 114 ret b 115 end 116 117 fn ac_hex_val b uchar to int 118 if ac_is_digit b 119 ret int sub b 48 120 end 121 if ge b 97 122 if le b 102 123 ret add 10 int sub b 97 124 end 125 end 126 if ge b 65 127 if le b 70 128 ret add 10 int sub b 65 129 end 130 end 131 ret -1 132 end 133 134 fn ac_hex_char v uchar to uchar 135 if lt v 10 136 ret add 48 v 137 end 138 ret add 87 v 139 end 140 141 fn ac_mem_eq a view uchar * b view uchar * to bool 142 na = len a 143 nb = len b 144 if ne na nb 145 ret false 146 end 147 i = ulong 0 148 while lt i na bound 65536 149 if ne get a i get b i 150 ret false 151 end 152 i = add i 1 153 end 154 ret true 155 end 156 157 fn ac_mem_eq_ci a view uchar * b view uchar * to bool 158 na = len a 159 nb = len b 160 if ne na nb 161 ret false 162 end 163 i = ulong 0 164 while lt i na bound 65536 165 ca = ac_to_lower get a i 166 cb = ac_to_lower get b i 167 if ne ca cb 168 ret false 169 end 170 i = add i 1 171 end 172 ret true 173 end 174 175 fn ac_starts_with hay view uchar * pfx view uchar * to bool 176 nh = len hay 177 np = len pfx 178 if lt nh np 179 ret false 180 end 181 i = ulong 0 182 while lt i np bound 65536 183 if ne get hay i get pfx i 184 ret false 185 end 186 i = add i 1 187 end 188 ret true 189 end 190 191 fn ac_ends_with hay view uchar * sfx view uchar * to bool 192 nh = len hay 193 ns = len sfx 194 if lt nh ns 195 ret false 196 end 197 base = sub nh ns 198 i = ulong 0 199 while lt i ns bound 65536 200 if ne get hay add base i get sfx i 201 ret false 202 end 203 i = add i 1 204 end 205 ret true 206 end 207 208 fn ac_find_bytes hay view uchar * needle view uchar * from ulong to long 209 nh = len hay 210 nn = len needle 211 if eq nn 0 212 ret -1 213 end 214 if lt nh nn 215 ret -1 216 end 217 if gt from sub nh nn 218 ret -1 219 end 220 limit = add sub nh nn 1 221 i = from 222 while lt i limit bound 65536 223 j = ulong 0 224 ok = true 225 while lt j nn bound 4096 226 if ne get hay add i j get needle j 227 ok = false 228 break 229 end 230 j = add j 1 231 end 232 if ok 233 ret long i 234 end 235 i = add i 1 236 end 237 ret -1 238 end 239 240 fn ac_rfind_bytes hay view uchar * needle view uchar * to long 241 nh = len hay 242 nn = len needle 243 if eq nn 0 244 ret -1 245 end 246 if lt nh nn 247 ret -1 248 end 249 i = sub nh nn 250 done = false 251 while not done bound 65536 252 j = ulong 0 253 ok = true 254 while lt j nn bound 4096 255 if ne get hay add i j get needle j 256 ok = false 257 break 258 end 259 j = add j 1 260 end 261 if ok 262 ret long i 263 end 264 if eq i 0 265 done = true 266 else 267 i = sub i 1 268 end 269 end 270 ret -1 271 end 272 273 fn ac_contains_ci hay view uchar * needle view uchar * to bool 274 nh = len hay 275 nn = len needle 276 if eq nn 0 277 ret true 278 end 279 if lt nh nn 280 ret false 281 end 282 limit = add sub nh nn 1 283 i = ulong 0 284 while lt i limit bound 65536 285 j = ulong 0 286 ok = true 287 while lt j nn bound 4096 288 ca = ac_to_lower get hay add i j 289 cb = ac_to_lower get needle j 290 if ne ca cb 291 ok = false 292 break 293 end 294 j = add j 1 295 end 296 if ok 297 ret true 298 end 299 i = add i 1 300 end 301 ret false 302 end 303 304 fn ac_copy_at dst edit uchar * pos ulong src view uchar * to ulong 305 n = len src 306 cap = len dst 307 i = ulong 0 308 while lt i n bound 65536 309 if ge add pos i cap 310 break 311 end 312 put dst add pos i get src i 313 i = add i 1 314 end 315 ret add pos i 316 end 317 318 fn ac_push_byte dst edit uchar * pos ulong b uchar to ulong 319 if lt pos len dst 320 put dst pos b 321 ret add pos 1 322 end 323 ret pos 324 end 325 326 note ---- integer parsing and formatting ---- 327 328 fn ac_parse_u64 src view uchar * res edit ulong 1 to bool 329 n = len src 330 if eq n 0 331 ret false 332 end 333 acc = ulong 0 334 i = ulong 0 335 while lt i n bound 64 336 b = get src i 337 if not ac_is_digit b 338 ret false 339 end 340 d = ulong sub b 48 341 if gt acc `1844674407370955161` 342 ret false 343 end 344 if eq acc `1844674407370955161` 345 if gt d 5 346 ret false 347 end 348 end 349 acc = add mul acc 10 d 350 i = add i 1 351 end 352 put res 0 acc 353 ret true 354 end 355 356 fn ac_parse_u32 src view uchar * res edit uint 1 to bool 357 tmp = new ulong 1 0 358 if not ac_parse_u64 src edit tmp 359 ret false 360 end 361 v = get tmp 0 362 if gt v `4294967295` 363 ret false 364 end 365 put res 0 uint v 366 ret true 367 end 368 369 fn ac_parse_i64 src view uchar * res edit long 1 to bool 370 n = len src 371 if eq n 0 372 ret false 373 end 374 i = ulong 0 375 is_neg = false 376 b0 = get src 0 377 if eq b0 45 378 is_neg = true 379 i = ulong 1 380 else 381 if eq b0 43 382 i = ulong 1 383 end 384 end 385 if ge i n 386 ret false 387 end 388 acc = ulong 0 389 while lt i n bound 64 390 b = get src i 391 if not ac_is_digit b 392 ret false 393 end 394 d = ulong sub b 48 395 if gt acc `922337203685477580` 396 ret false 397 end 398 if eq acc `922337203685477580` 399 if is_neg 400 if gt d 8 401 ret false 402 end 403 else 404 if gt d 7 405 ret false 406 end 407 end 408 end 409 acc = add mul acc 10 d 410 i = add i 1 411 end 412 if is_neg 413 if eq acc `9223372036854775808` 414 put res 0 bitcast long acc 415 else 416 put res 0 sub long 0 long acc 417 end 418 else 419 put res 0 long acc 420 end 421 ret true 422 end 423 424 fn ac_fmt_u64 dst edit uchar * pos ulong val ulong to ulong 425 rev = new uchar 24 0 426 k = ulong 0 427 v = val 428 if eq v 0 429 put rev 0 48 430 k = ulong 1 431 else 432 while gt v 0 bound 24 433 d = uchar rem v 10 434 put rev k add 48 d 435 v = div v 10 436 k = add k 1 437 end 438 end 439 p = pos 440 while gt k 0 bound 24 441 k = sub k 1 442 p = ac_push_byte dst p get rev k 443 end 444 ret p 445 end 446 447 fn ac_fmt_i64 dst edit uchar * pos ulong val long to ulong 448 p = pos 449 if lt val 0 450 p = ac_push_byte dst p 45 451 mag = sub ulong 0 bitcast ulong val 452 ret ac_fmt_u64 dst p mag 453 end 454 ret ac_fmt_u64 dst p ulong val 455 end 456 457 fn ac_fmt_hex8 dst edit uchar * pos ulong val uint to ulong 458 p = pos 459 i = ulong 0 460 while lt i 8 bound 8 461 sh = uint mul sub 7 i 4 462 nib = uchar and shr val sh 15 463 p = ac_push_byte dst p ac_hex_char nib 464 i = add i 1 465 end 466 ret p 467 end 468 469 fn ac_fmt_hex2 dst edit uchar * pos ulong val uchar to ulong 470 p = ac_push_byte dst pos ac_hex_char shr val 4 471 p = ac_push_byte dst p ac_hex_char and val 15 472 ret p 473 end 474 475 note ---- UTF-8 decode and encode ---- 476 477 fn ac_utf8_decode src view uchar * pos ulong step edit ulong 1 to uint 478 n = len src 479 if ge pos n 480 put step 0 0 481 ret 0xffffffff 482 end 483 b0 = get src pos 484 if lt b0 128 485 put step 0 1 486 ret uint b0 487 end 488 if ge b0 0xc2 489 if le b0 0xdf 490 if lt add pos 1 n 491 b1 = get src add pos 1 492 if eq and b1 0xc0 0x80 493 put step 0 2 494 ret or shl uint and b0 0x1f 6 uint and b1 0x3f 495 end 496 end 497 end 498 end 499 if ge b0 0xe0 500 if le b0 0xef 501 if lt add pos 2 n 502 b1 = get src add pos 1 503 b2 = get src add pos 2 504 if eq and b1 0xc0 0x80 505 if eq and b2 0xc0 0x80 506 put step 0 3 507 t1 = shl uint and b0 0x0f 12 508 t2 = shl uint and b1 0x3f 6 509 ret or or t1 t2 uint and b2 0x3f 510 end 511 end 512 end 513 end 514 end 515 if ge b0 0xf0 516 if le b0 0xf4 517 if lt add pos 3 n 518 b1 = get src add pos 1 519 b2 = get src add pos 2 520 b3 = get src add pos 3 521 if eq and b1 0xc0 0x80 522 if eq and b2 0xc0 0x80 523 if eq and b3 0xc0 0x80 524 put step 0 4 525 t1 = shl uint and b0 0x07 18 526 t2 = shl uint and b1 0x3f 12 527 t3 = shl uint and b2 0x3f 6 528 ret or or or t1 t2 t3 uint and b3 0x3f 529 end 530 end 531 end 532 end 533 end 534 end 535 put step 0 1 536 ret 0xffffffff 537 end 538 539 fn ac_utf8_encode dst edit uchar * pos ulong cp uint to ulong 540 p = pos 541 if lt cp 0x80 542 ret ac_push_byte dst p uchar cp 543 end 544 if lt cp 0x800 545 p = ac_push_byte dst p or 0xc0 uchar shr cp 6 546 p = ac_push_byte dst p or 0x80 uchar and cp 0x3f 547 ret p 548 end 549 if lt cp 0x10000 550 p = ac_push_byte dst p or 0xe0 uchar shr cp 12 551 p = ac_push_byte dst p or 0x80 uchar and shr cp 6 0x3f 552 p = ac_push_byte dst p or 0x80 uchar and cp 0x3f 553 ret p 554 end 555 p = ac_push_byte dst p or 0xf0 uchar shr cp 18 556 p = ac_push_byte dst p or 0x80 uchar and shr cp 12 0x3f 557 p = ac_push_byte dst p or 0x80 uchar and shr cp 6 0x3f 558 p = ac_push_byte dst p or 0x80 uchar and cp 0x3f 559 ret p 560 end 561 562 note ---- JSON string encode/decode and field extraction ---- 563 564 fn ac_json_str src view uchar * dst edit uchar * to ulong 565 n = len src 566 p = ulong 0 567 p = ac_push_byte dst p 34 568 i = ulong 0 569 while lt i n bound 65536 570 b = get src i 571 if eq b 34 572 p = ac_push_byte dst p 92 573 p = ac_push_byte dst p 34 574 else 575 if eq b 92 576 p = ac_push_byte dst p 92 577 p = ac_push_byte dst p 92 578 else 579 if eq b 10 580 p = ac_push_byte dst p 92 581 p = ac_push_byte dst p 110 582 else 583 if eq b 13 584 p = ac_push_byte dst p 92 585 p = ac_push_byte dst p 114 586 else 587 if eq b 9 588 p = ac_push_byte dst p 92 589 p = ac_push_byte dst p 116 590 else 591 if lt b 32 592 p = ac_push_byte dst p 92 593 p = ac_push_byte dst p 117 594 p = ac_push_byte dst p 48 595 p = ac_push_byte dst p 48 596 p = ac_fmt_hex2 dst p b 597 else 598 p = ac_push_byte dst p b 599 end 600 end 601 end 602 end 603 end 604 end 605 i = add i 1 606 end 607 p = ac_push_byte dst p 34 608 ret p 609 end 610 611 fn ac_parse_hex4 src view uchar * pos ulong to int 612 if gt add pos 4 len src 613 ret -1 614 end 615 v0 = ac_hex_val get src pos 616 v1 = ac_hex_val get src add pos 1 617 v2 = ac_hex_val get src add pos 2 618 v3 = ac_hex_val get src add pos 3 619 if lt v0 0 620 ret -1 621 end 622 if lt v1 0 623 ret -1 624 end 625 if lt v2 0 626 ret -1 627 end 628 if lt v3 0 629 ret -1 630 end 631 r = shl v0 12 632 r = or r shl v1 8 633 r = or r shl v2 4 634 r = or r v3 635 ret r 636 end 637 638 fn ac_decode_u_esc src view uchar * pos ulong strict bool next_pos edit ulong 1 to long 639 n = len src 640 cp = ac_parse_hex4 src pos 641 if lt cp 0 642 ret -1 643 end 644 i = add pos 4 645 ucp = uint cp 646 if ge ucp 0xd800 647 if le ucp 0xdbff 648 ok_pair = false 649 if le add i 6 n 650 if eq get src i 92 651 if eq get src add i 1 117 652 lo = ac_parse_hex4 src add i 2 653 if ge lo 0xdc00 654 if le lo 0xdfff 655 hi_s = shl sub ucp 0xd800 10 656 lo_s = sub uint lo 0xdc00 657 ucp = add 0x10000 add hi_s lo_s 658 i = add i 6 659 ok_pair = true 660 end 661 end 662 end 663 end 664 end 665 if not ok_pair 666 if strict 667 ret -1 668 end 669 ucp = uint 32 670 end 671 else 672 if ge ucp 0xdc00 673 if le ucp 0xdfff 674 if strict 675 ret -1 676 end 677 ucp = uint 32 678 end 679 end 680 end 681 end 682 put next_pos 0 i 683 ret long ucp 684 end 685 686 fn ac_json_read_str src view uchar * start ulong dst edit uchar * next_pos edit ulong 1 to long 687 n = len src 688 if ge start n 689 ret -1 690 end 691 if ne get src start 34 692 ret -1 693 end 694 i = add start 1 695 p = ulong 0 696 while lt i n bound 65536 697 c = get src i 698 if eq c 34 699 put next_pos 0 add i 1 700 ret long p 701 end 702 if eq c 92 703 if ge add i 1 n 704 ret -1 705 end 706 e = get src add i 1 707 i = add i 2 708 if eq e 110 709 p = ac_push_byte dst p 10 710 else 711 if eq e 116 712 p = ac_push_byte dst p 9 713 else 714 if eq e 114 715 p = ac_push_byte dst p 13 716 else 717 if eq e 98 718 p = ac_push_byte dst p 32 719 else 720 if eq e 102 721 p = ac_push_byte dst p 32 722 else 723 if eq e 117 724 ucp = ac_decode_u_esc src i false next_pos 725 if lt ucp 0 726 ret -1 727 end 728 i = get next_pos 0 729 p = ac_utf8_encode dst p uint ucp 730 else 731 p = ac_push_byte dst p e 732 end 733 end 734 end 735 end 736 end 737 end 738 else 739 p = ac_push_byte dst p c 740 i = add i 1 741 end 742 end 743 ret -1 744 end 745 746 fn ac_make_quoted_key key view uchar * dst edit uchar 128 to ulong 747 nk = len key 748 if gt add nk 2 128 749 ret 0 750 end 751 put dst 0 34 752 k = ulong 0 753 while lt k nk bound 128 754 put dst add k 1 get key k 755 k = add k 1 756 end 757 put dst add nk 1 34 758 ret add nk 2 759 end 760 761 fn ac_skip_colon_ws body view uchar * pos ulong to ulong 762 nb = len body 763 i = pos 764 while lt i nb bound 65536 765 b = get body i 766 if eq b 32 767 i = add i 1 768 else 769 if eq b 58 770 i = add i 1 771 else 772 if eq b 10 773 i = add i 1 774 else 775 if eq b 13 776 i = add i 1 777 else 778 if eq b 9 779 i = add i 1 780 else 781 break 782 end 783 end 784 end 785 end 786 end 787 end 788 ret i 789 end 790 791 fn ac_json_field body view uchar * key view uchar * dst edit uchar * to long 792 needle = new uchar 128 0 793 next_pos = new ulong 1 0 794 nlen = ac_make_quoted_key key edit needle 795 if eq nlen 0 796 ret -1 797 end 798 from = ulong 0 799 while lt from len body bound 65536 800 found = ac_find_bytes body part needle 0 nlen from 801 if lt found 0 802 ret -1 803 end 804 i = ac_skip_colon_ws body add ulong found nlen 805 got = ac_json_read_str body i dst edit next_pos 806 if ge got 0 807 ret got 808 end 809 from = add ulong found 1 810 end 811 ret -1 812 end 813 814 fn ac_json_number_after body view uchar * key view uchar * res edit ulong 1 to bool 815 nb = len body 816 nk = len key 817 needle = new uchar 128 0 818 if gt add nk 2 128 819 ret false 820 end 821 put needle 0 34 822 k = ulong 0 823 while lt k nk bound 128 824 put needle add k 1 get key k 825 k = add k 1 826 end 827 put needle add nk 1 34 828 found = ac_find_bytes body part needle 0 add nk 2 0 829 if lt found 0 830 ret false 831 end 832 i = add ulong found add nk 2 833 while lt i nb bound 65536 834 b = get body i 835 if eq b 32 836 i = add i 1 837 else 838 if eq b 58 839 i = add i 1 840 else 841 break 842 end 843 end 844 end 845 start = i 846 while lt i nb bound 64 847 if not ac_is_digit get body i 848 break 849 end 850 i = add i 1 851 end 852 if eq i start 853 ret false 854 end 855 ret ac_parse_u64 part body start sub i start res 856 end 857 858 note ---- HTTP chunked decoding, Retry-After, backoff, slug, UTC civil date ---- 859 860 fn ac_parse_hex_u64 src view uchar * res edit ulong 1 to bool 861 n = len src 862 s = ulong 0 863 while lt s n bound 64 864 if not ac_is_ws get src s 865 break 866 end 867 s = add s 1 868 end 869 e = n 870 while gt e s bound 64 871 if not ac_is_ws get src sub e 1 872 break 873 end 874 e = sub e 1 875 end 876 if eq s e 877 ret false 878 end 879 acc = ulong 0 880 i = s 881 while lt i e bound 64 882 hv = ac_hex_val get src i 883 if lt hv 0 884 ret false 885 end 886 if gt acc 0x0fffffffffffffff 887 ret false 888 end 889 acc = or shl acc 4 ulong hv 890 i = add i 1 891 end 892 put res 0 acc 893 ret true 894 end 895 896 fn ac_dechunk src view uchar * dst edit uchar * to ulong 897 n = len src 898 crlf = new uchar 2 0 899 put crlf 0 13 900 put crlf 1 10 901 sz_box = new ulong 1 0 902 i = ulong 0 903 p = ulong 0 904 while lt i n bound 65536 905 e = ac_find_bytes src part crlf 0 2 i 906 if lt e 0 907 break 908 end 909 ue = ulong e 910 if not ac_parse_hex_u64 part src i sub ue i edit sz_box 911 break 912 end 913 chunk_n = get sz_box 0 914 if eq chunk_n 0 915 break 916 end 917 s = add ue 2 918 if gt s n 919 break 920 end 921 if gt chunk_n sub n s 922 break 923 end 924 p = ac_copy_at dst p part src s chunk_n 925 i = add add s chunk_n 2 926 end 927 ret p 928 end 929 930 text AC_RETRY_AFTER_KEY "retry-after" 931 932 fn ac_retry_after_line line view uchar * res edit ulong 1 to bool 933 n = len line 934 colon = ulong 0 935 found_c = false 936 while lt colon n bound 4096 937 if eq get line colon 58 938 found_c = true 939 break 940 end 941 colon = add colon 1 942 end 943 if not found_c 944 ret false 945 end 946 tb = new ulong 2 0 947 ac_trim_slice_bounds line 0 colon edit tb 948 ks = get tb 0 949 ke = get tb 1 950 if not ac_mem_eq_ci part line ks sub ke ks AC_RETRY_AFTER_KEY 951 ret false 952 end 953 ac_trim_slice_bounds line add colon 1 n edit tb 954 vs = get tb 0 955 ve = get tb 1 956 ret ac_parse_u64 part line vs sub ve vs res 957 end 958 959 fn ac_retry_after head view uchar * res edit ulong 1 to bool 960 n = len head 961 i = ulong 0 962 while lt i n bound 16384 963 ls = i 964 while lt i n bound 16384 965 if eq get head i 10 966 break 967 end 968 i = add i 1 969 end 970 le_end = i 971 if gt le_end ls 972 if eq get head sub le_end 1 13 973 le_end = sub le_end 1 974 end 975 end 976 if lt i n 977 i = add i 1 978 end 979 if ac_retry_after_line part head ls sub le_end ls res 980 ret true 981 end 982 end 983 ret false 984 end 985 986 fn ac_backoff_ms n uint head view uchar * to ulong 987 sh = uint 0 988 if gt n 1 989 sh = sub n 1 990 end 991 if gt sh 2 992 sh = uint 2 993 end 994 base_ms = shl ulong 250 ulong sh 995 ra = new ulong 1 0 996 named = ulong 0 997 if ac_retry_after head edit ra 998 s = get ra 0 999 if gt s 4 1000 s = ulong 4 1001 end 1002 named = mul s 1000 1003 end 1004 best = base_ms 1005 if gt named best 1006 best = named 1007 end 1008 if gt best 4000 1009 best = ulong 4000 1010 end 1011 ret best 1012 end 1013 1014 fn ac_slug q view uchar * dst edit uchar * to ulong 1015 n = len q 1016 i = ulong 0 1017 p = ulong 0 1018 words = ulong 0 1019 while lt i n bound 4096 1020 if ge words 5 1021 break 1022 end 1023 while lt i n bound 4096 1024 if not ac_is_ws get q i 1025 break 1026 end 1027 i = add i 1 1028 end 1029 ws = i 1030 while lt i n bound 4096 1031 if ac_is_ws get q i 1032 break 1033 end 1034 i = add i 1 1035 end 1036 we = i 1037 has_alnum = false 1038 j = ws 1039 while lt j we bound 4096 1040 if ac_is_alnum get q j 1041 has_alnum = true 1042 break 1043 end 1044 j = add j 1 1045 end 1046 if has_alnum 1047 if gt words 0 1048 p = ac_push_byte dst p 45 1049 end 1050 j = ws 1051 while lt j we bound 4096 1052 b = get q j 1053 if ac_is_alnum b 1054 p = ac_push_byte dst p ac_to_lower b 1055 end 1056 j = add j 1 1057 end 1058 words = add words 1 1059 end 1060 end 1061 ret p 1062 end 1063 1064 fn ac_fmt_pad2 dst edit uchar * pos ulong v ulong to ulong 1065 p = ac_push_byte dst pos add 48 uchar div v 10 1066 p = ac_push_byte dst p add 48 uchar rem v 10 1067 ret p 1068 end 1069 1070 fn ac_fmt_pad4 dst edit uchar * pos ulong v ulong to ulong 1071 p = ac_push_byte dst pos add 48 uchar rem div v 1000 10 1072 p = ac_push_byte dst p add 48 uchar rem div v 100 10 1073 p = ac_push_byte dst p add 48 uchar rem div v 10 10 1074 p = ac_push_byte dst p add 48 uchar rem v 10 1075 ret p 1076 end 1077 1078 fn ac_utc_ymd days ulong ymd edit ulong 3 to void 1079 z = add long days 719468 1080 era = div z 146097 1081 if lt z 0 1082 era = div sub z 146096 146097 1083 end 1084 doe = sub z mul era 146097 1085 t1 = sub doe div doe 1460 1086 t1 = add sub t1 div doe 36524 div doe 146096 1087 yoe = div t1 365 1088 y = add yoe mul era 400 1089 t1 = add mul 365 yoe div yoe 4 1090 t1 = sub t1 div yoe 100 1091 doe = sub doe t1 1092 t1 = div add mul 5 doe 2 153 1093 d = add sub doe div add mul 153 t1 2 5 1 1094 m = sub t1 9 1095 if lt t1 10 1096 m = add t1 3 1097 end 1098 if le m 2 1099 y = add y 1 1100 end 1101 put ymd 0 ulong y 1102 put ymd 1 ulong m 1103 put ymd 2 ulong d 1104 ret 1105 end 1106 1107 fn ac_utc_civil epoch_secs ulong dst edit uchar 20 to void 1108 ymd = new ulong 3 0 1109 ac_utc_ymd div epoch_secs 86400 edit ymd 1110 rem_s = rem epoch_secs 86400 1111 p = ac_fmt_pad4 dst 0 get ymd 0 1112 p = ac_push_byte dst p 45 1113 p = ac_fmt_pad2 dst p get ymd 1 1114 p = ac_push_byte dst p 45 1115 p = ac_fmt_pad2 dst p get ymd 2 1116 p = ac_push_byte dst p 84 1117 p = ac_fmt_pad2 dst p div rem_s 3600 1118 p = ac_push_byte dst p 58 1119 p = ac_fmt_pad2 dst p rem div rem_s 60 60 1120 p = ac_push_byte dst p 58 1121 p = ac_fmt_pad2 dst p rem rem_s 60 1122 p = ac_push_byte dst p 90 1123 ret 1124 end 1125 1126 note ---- plain text, bare Latin accents, folded, for_allen_keep ---- 1127 1128 fn ac_bare_latin_lo cp uint to uchar 1129 if ge cp 0x00e0 1130 if le cp 0x00e5 1131 ret 97 1132 end 1133 end 1134 if eq cp 0x00e7 1135 ret 99 1136 end 1137 if ge cp 0x00e8 1138 if le cp 0x00eb 1139 ret 101 1140 end 1141 end 1142 if ge cp 0x00ec 1143 if le cp 0x00ef 1144 ret 105 1145 end 1146 end 1147 if eq cp 0x00f1 1148 ret 110 1149 end 1150 if ge cp 0x00f2 1151 if le cp 0x00f6 1152 ret 111 1153 end 1154 end 1155 if eq cp 0x00f8 1156 ret 111 1157 end 1158 if ge cp 0x00f9 1159 if le cp 0x00fc 1160 ret 117 1161 end 1162 end 1163 if eq cp 0x00fd 1164 ret 121 1165 end 1166 if eq cp 0x00ff 1167 ret 121 1168 end 1169 if ge cp 0x00c0 1170 if le cp 0x00c5 1171 ret 65 1172 end 1173 end 1174 if eq cp 0x00c7 1175 ret 67 1176 end 1177 if ge cp 0x00c8 1178 if le cp 0x00cb 1179 ret 69 1180 end 1181 end 1182 if ge cp 0x00cc 1183 if le cp 0x00cf 1184 ret 73 1185 end 1186 end 1187 if eq cp 0x00d1 1188 ret 78 1189 end 1190 if ge cp 0x00d2 1191 if le cp 0x00d6 1192 ret 79 1193 end 1194 end 1195 if eq cp 0x00d8 1196 ret 79 1197 end 1198 if ge cp 0x00d9 1199 if le cp 0x00dc 1200 ret 85 1201 end 1202 end 1203 if eq cp 0x00dd 1204 ret 89 1205 end 1206 ret 0 1207 end 1208 1209 fn ac_bare_latin_ext cp uint to uchar 1210 if eq cp 0x0101 1211 ret 97 1212 end 1213 if eq cp 0x0103 1214 ret 97 1215 end 1216 if eq cp 0x0105 1217 ret 97 1218 end 1219 if eq cp 0x0100 1220 ret 65 1221 end 1222 if eq cp 0x0102 1223 ret 65 1224 end 1225 if eq cp 0x0104 1226 ret 65 1227 end 1228 if eq cp 0x0107 1229 ret 99 1230 end 1231 if eq cp 0x0109 1232 ret 99 1233 end 1234 if eq cp 0x010b 1235 ret 99 1236 end 1237 if eq cp 0x010d 1238 ret 99 1239 end 1240 if eq cp 0x0106 1241 ret 67 1242 end 1243 if eq cp 0x0108 1244 ret 67 1245 end 1246 if eq cp 0x010a 1247 ret 67 1248 end 1249 if eq cp 0x010c 1250 ret 67 1251 end 1252 if eq cp 0x010f 1253 ret 100 1254 end 1255 if eq cp 0x0111 1256 ret 100 1257 end 1258 if eq cp 0x010e 1259 ret 68 1260 end 1261 if eq cp 0x0110 1262 ret 68 1263 end 1264 if eq cp 0x0113 1265 ret 101 1266 end 1267 if eq cp 0x0115 1268 ret 101 1269 end 1270 if eq cp 0x0117 1271 ret 101 1272 end 1273 if eq cp 0x0119 1274 ret 101 1275 end 1276 if eq cp 0x011b 1277 ret 101 1278 end 1279 if eq cp 0x0112 1280 ret 69 1281 end 1282 if eq cp 0x0114 1283 ret 69 1284 end 1285 if eq cp 0x0116 1286 ret 69 1287 end 1288 if eq cp 0x0118 1289 ret 69 1290 end 1291 if eq cp 0x011a 1292 ret 69 1293 end 1294 if eq cp 0x011d 1295 ret 103 1296 end 1297 if eq cp 0x011f 1298 ret 103 1299 end 1300 if eq cp 0x0121 1301 ret 103 1302 end 1303 if eq cp 0x0123 1304 ret 103 1305 end 1306 if eq cp 0x011c 1307 ret 71 1308 end 1309 if eq cp 0x011e 1310 ret 71 1311 end 1312 if eq cp 0x0120 1313 ret 71 1314 end 1315 if eq cp 0x0122 1316 ret 71 1317 end 1318 if eq cp 0x0129 1319 ret 105 1320 end 1321 if eq cp 0x012b 1322 ret 105 1323 end 1324 if eq cp 0x012d 1325 ret 105 1326 end 1327 if eq cp 0x012f 1328 ret 105 1329 end 1330 if eq cp 0x0131 1331 ret 105 1332 end 1333 if eq cp 0x0128 1334 ret 73 1335 end 1336 if eq cp 0x012a 1337 ret 73 1338 end 1339 if eq cp 0x012c 1340 ret 73 1341 end 1342 if eq cp 0x012e 1343 ret 73 1344 end 1345 if eq cp 0x0130 1346 ret 73 1347 end 1348 if eq cp 0x013a 1349 ret 108 1350 end 1351 if eq cp 0x013c 1352 ret 108 1353 end 1354 if eq cp 0x013e 1355 ret 108 1356 end 1357 if eq cp 0x0140 1358 ret 108 1359 end 1360 if eq cp 0x0142 1361 ret 108 1362 end 1363 if eq cp 0x0139 1364 ret 76 1365 end 1366 if eq cp 0x013b 1367 ret 76 1368 end 1369 if eq cp 0x013d 1370 ret 76 1371 end 1372 if eq cp 0x013f 1373 ret 76 1374 end 1375 if eq cp 0x0141 1376 ret 76 1377 end 1378 if eq cp 0x0144 1379 ret 110 1380 end 1381 if eq cp 0x0146 1382 ret 110 1383 end 1384 if eq cp 0x0148 1385 ret 110 1386 end 1387 if eq cp 0x0143 1388 ret 78 1389 end 1390 if eq cp 0x0145 1391 ret 78 1392 end 1393 if eq cp 0x0147 1394 ret 78 1395 end 1396 if eq cp 0x014d 1397 ret 111 1398 end 1399 if eq cp 0x014f 1400 ret 111 1401 end 1402 if eq cp 0x0151 1403 ret 111 1404 end 1405 if eq cp 0x014c 1406 ret 79 1407 end 1408 if eq cp 0x014e 1409 ret 79 1410 end 1411 if eq cp 0x0150 1412 ret 79 1413 end 1414 if eq cp 0x0155 1415 ret 114 1416 end 1417 if eq cp 0x0157 1418 ret 114 1419 end 1420 if eq cp 0x0159 1421 ret 114 1422 end 1423 if eq cp 0x0154 1424 ret 82 1425 end 1426 if eq cp 0x0156 1427 ret 82 1428 end 1429 if eq cp 0x0158 1430 ret 82 1431 end 1432 if eq cp 0x015b 1433 ret 115 1434 end 1435 if eq cp 0x015d 1436 ret 115 1437 end 1438 if eq cp 0x015f 1439 ret 115 1440 end 1441 if eq cp 0x0161 1442 ret 115 1443 end 1444 if eq cp 0x015a 1445 ret 83 1446 end 1447 if eq cp 0x015c 1448 ret 83 1449 end 1450 if eq cp 0x015e 1451 ret 83 1452 end 1453 if eq cp 0x0160 1454 ret 83 1455 end 1456 if eq cp 0x0163 1457 ret 116 1458 end 1459 if eq cp 0x0165 1460 ret 116 1461 end 1462 if eq cp 0x0167 1463 ret 116 1464 end 1465 if eq cp 0x0162 1466 ret 84 1467 end 1468 if eq cp 0x0164 1469 ret 84 1470 end 1471 if eq cp 0x0166 1472 ret 84 1473 end 1474 if eq cp 0x0169 1475 ret 117 1476 end 1477 if eq cp 0x016b 1478 ret 117 1479 end 1480 if eq cp 0x016d 1481 ret 117 1482 end 1483 if eq cp 0x016f 1484 ret 117 1485 end 1486 if eq cp 0x0171 1487 ret 117 1488 end 1489 if eq cp 0x0173 1490 ret 117 1491 end 1492 if eq cp 0x0168 1493 ret 85 1494 end 1495 if eq cp 0x016a 1496 ret 85 1497 end 1498 if eq cp 0x016c 1499 ret 85 1500 end 1501 if eq cp 0x016e 1502 ret 85 1503 end 1504 if eq cp 0x0170 1505 ret 85 1506 end 1507 if eq cp 0x0172 1508 ret 85 1509 end 1510 if eq cp 0x0177 1511 ret 121 1512 end 1513 if eq cp 0x0176 1514 ret 89 1515 end 1516 if eq cp 0x0178 1517 ret 89 1518 end 1519 if eq cp 0x017a 1520 ret 122 1521 end 1522 if eq cp 0x017c 1523 ret 122 1524 end 1525 if eq cp 0x017e 1526 ret 122 1527 end 1528 if eq cp 0x0179 1529 ret 90 1530 end 1531 if eq cp 0x017b 1532 ret 90 1533 end 1534 if eq cp 0x017d 1535 ret 90 1536 end 1537 ret 0 1538 end 1539 1540 fn ac_plain_cp dst edit uchar * pos ulong cp uint to ulong 1541 p = pos 1542 if ge cp 32 1543 if le cp 126 1544 ret ac_push_byte dst p uchar cp 1545 end 1546 end 1547 if eq cp 10 1548 ret ac_push_byte dst p 10 1549 end 1550 if eq cp 9 1551 ret ac_push_byte dst p 9 1552 end 1553 if eq cp 13 1554 ret ac_push_byte dst p 13 1555 end 1556 if ge cp 0x2013 1557 if le cp 0x2015 1558 p = ac_push_byte dst p 44 1559 ret ac_push_byte dst p 32 1560 end 1561 end 1562 if ge cp 0x2010 1563 if le cp 0x2012 1564 ret ac_push_byte dst p 45 1565 end 1566 end 1567 if eq cp 0x2212 1568 ret ac_push_byte dst p 45 1569 end 1570 if ge cp 0x2018 1571 if le cp 0x201b 1572 ret ac_push_byte dst p 39 1573 end 1574 end 1575 if eq cp 0x2032 1576 ret ac_push_byte dst p 39 1577 end 1578 if ge cp 0x201c 1579 if le cp 0x201f 1580 ret ac_push_byte dst p 34 1581 end 1582 end 1583 if eq cp 0x2033 1584 ret ac_push_byte dst p 34 1585 end 1586 if eq cp 0x00ab 1587 ret ac_push_byte dst p 34 1588 end 1589 if eq cp 0x00bb 1590 ret ac_push_byte dst p 34 1591 end 1592 if eq cp 0x2026 1593 p = ac_push_byte dst p 46 1594 p = ac_push_byte dst p 46 1595 ret ac_push_byte dst p 46 1596 end 1597 if eq cp 0x00a0 1598 ret ac_push_byte dst p 32 1599 end 1600 if eq cp 0x2002 1601 ret ac_push_byte dst p 32 1602 end 1603 if eq cp 0x2003 1604 ret ac_push_byte dst p 32 1605 end 1606 if eq cp 0x2009 1607 ret ac_push_byte dst p 32 1608 end 1609 if eq cp 0x200a 1610 ret ac_push_byte dst p 32 1611 end 1612 if eq cp 0x202f 1613 ret ac_push_byte dst p 32 1614 end 1615 if eq cp 0x00d7 1616 ret ac_push_byte dst p 120 1617 end 1618 if eq cp 0x00df 1619 p = ac_push_byte dst p 115 1620 ret ac_push_byte dst p 115 1621 end 1622 if eq cp 0x00e6 1623 p = ac_push_byte dst p 97 1624 ret ac_push_byte dst p 101 1625 end 1626 if eq cp 0x00c6 1627 p = ac_push_byte dst p 65 1628 ret ac_push_byte dst p 69 1629 end 1630 if eq cp 0x0153 1631 p = ac_push_byte dst p 111 1632 ret ac_push_byte dst p 101 1633 end 1634 if eq cp 0x0152 1635 p = ac_push_byte dst p 79 1636 ret ac_push_byte dst p 69 1637 end 1638 b = ac_bare_latin_lo cp 1639 if ne b 0 1640 ret ac_push_byte dst p b 1641 end 1642 b = ac_bare_latin_ext cp 1643 if ne b 0 1644 ret ac_push_byte dst p b 1645 end 1646 ret p 1647 end 1648 1649 fn ac_plain src view uchar * dst edit uchar * to ulong 1650 n = len src 1651 step = new ulong 1 0 1652 i = ulong 0 1653 p = ulong 0 1654 while lt i n bound 65536 1655 cp = ac_utf8_decode src i edit step 1656 st = get step 0 1657 if eq st 0 1658 break 1659 end 1660 if ne cp 0xffffffff 1661 p = ac_plain_cp dst p cp 1662 end 1663 i = add i st 1664 end 1665 ret p 1666 end 1667 1668 fn ac_folded src view uchar * dst edit uchar * to ulong 1669 tmp = new uchar 8192 0 1670 pn = ac_plain src edit tmp 1671 i = ulong 0 1672 p = ulong 0 1673 in_sp = true 1674 while lt i pn bound 8192 1675 b = get tmp i 1676 if ac_is_ws b 1677 in_sp = true 1678 else 1679 if in_sp 1680 if gt p 0 1681 p = ac_push_byte dst p 32 1682 end 1683 in_sp = false 1684 end 1685 p = ac_push_byte dst p b 1686 end 1687 i = add i 1 1688 end 1689 ret p 1690 end 1691 1692 fn ac_find_sent_cut f view uchar * max_len ulong floor ulong to ulong 1693 i = max_len 1694 while gt i 0 bound 8192 1695 i = sub i 1 1696 b = get f i 1697 is_end = false 1698 if eq b 46 1699 is_end = true 1700 end 1701 if eq b 33 1702 is_end = true 1703 end 1704 if eq b 63 1705 is_end = true 1706 end 1707 if is_end 1708 if ge add i 1 floor 1709 cut = add i 1 1710 while gt cut 0 bound 8192 1711 if not ac_is_ws get f sub cut 1 1712 break 1713 end 1714 cut = sub cut 1 1715 end 1716 ret cut 1717 end 1718 end 1719 end 1720 ret 0 1721 end 1722 1723 fn ac_find_word_cut f view uchar * room ulong keep ulong to ulong 1724 rcut = room 1725 i = room 1726 while gt i 0 bound 8192 1727 i = sub i 1 1728 if eq get f i 32 1729 if ge i keep 1730 rcut = i 1731 break 1732 end 1733 end 1734 end 1735 while gt rcut 0 bound 8192 1736 if not ac_is_ws get f sub rcut 1 1737 break 1738 end 1739 rcut = sub rcut 1 1740 end 1741 ret rcut 1742 end 1743 1744 fn ac_for_allen_keep src view uchar * max_len ulong keep ulong dst edit uchar * to ulong 1745 f = new uchar 8192 0 1746 fn_len = ac_folded src edit f 1747 if le fn_len max_len 1748 ret ac_copy_at dst 0 part f 0 fn_len 1749 end 1750 floor = div max_len 3 1751 if gt keep floor 1752 floor = keep 1753 end 1754 cut = ac_find_sent_cut part f 0 fn_len max_len floor 1755 if gt cut 0 1756 ret ac_copy_at dst 0 part f 0 cut 1757 end 1758 room = ulong 0 1759 if gt max_len 3 1760 room = sub max_len 3 1761 end 1762 rcut = ac_find_word_cut part f 0 fn_len room keep 1763 p = ac_copy_at dst 0 part f 0 rcut 1764 p = ac_push_byte dst p 46 1765 p = ac_push_byte dst p 46 1766 p = ac_push_byte dst p 46 1767 ret p 1768 end 1769 1770 fn ac_for_allen src view uchar * max_len ulong dst edit uchar * to ulong 1771 ret ac_for_allen_keep src max_len 0 dst 1772 end 1773 1774 note ---- code.rs: CRC-32, plain_code, lang_of, within_limits, read_reply, escape, pieces ---- 1775 1776 fn ac_crc32 b view uchar * to uint 1777 n = len b 1778 c = uint 0xffffffff 1779 i = ulong 0 1780 while lt i n bound 65536 1781 c = xor c uint get b i 1782 j = ulong 0 1783 while lt j 8 bound 8 1784 if ne and c 1 0 1785 c = xor shr c 1 0xedb88320 1786 else 1787 c = shr c 1 1788 end 1789 j = add j 1 1790 end 1791 i = add i 1 1792 end 1793 ret xor c 0xffffffff 1794 end 1795 1796 fn ac_plain_code src view uchar * dst edit uchar * to ulong 1797 n = len src 1798 step = new ulong 1 0 1799 i = ulong 0 1800 p = ulong 0 1801 while lt i n bound 65536 1802 cp = ac_utf8_decode src i edit step 1803 st = get step 0 1804 if eq st 0 1805 break 1806 end 1807 if eq cp 13 1808 i = add i st 1809 continue 1810 end 1811 if eq cp 10 1812 p = ac_push_byte dst p 10 1813 else 1814 if eq cp 9 1815 p = ac_push_byte dst p 32 1816 p = ac_push_byte dst p 32 1817 p = ac_push_byte dst p 32 1818 p = ac_push_byte dst p 32 1819 else 1820 if ge cp 32 1821 if le cp 126 1822 p = ac_push_byte dst p uchar cp 1823 else 1824 p = ac_push_byte dst p 63 1825 end 1826 else 1827 p = ac_push_byte dst p 63 1828 end 1829 end 1830 end 1831 i = add i st 1832 end 1833 s = ulong 0 1834 while lt s p bound 65536 1835 if ne get dst s 10 1836 break 1837 end 1838 s = add s 1 1839 end 1840 e = p 1841 while gt e s bound 65536 1842 if ne get dst sub e 1 10 1843 break 1844 end 1845 e = sub e 1 1846 end 1847 if eq s e 1848 ret 0 1849 end 1850 k = sub e s 1851 if gt s 0 1852 j = ulong 0 1853 while lt j k bound 65536 1854 put dst j get dst add s j 1855 j = add j 1 1856 end 1857 end 1858 k = ac_push_byte dst k 10 1859 ret k 1860 end 1861 1862 text AC_L_PY "py" 1863 text AC_L_PY3 "python3" 1864 text AC_L_PYTHON "python" 1865 text AC_L_JS "js" 1866 text AC_L_JAVASCRIPT "javascript" 1867 text AC_L_TS "ts" 1868 text AC_L_TYPESCRIPT "typescript" 1869 text AC_L_SH "sh" 1870 text AC_L_SHELL "shell" 1871 text AC_L_ZSH "zsh" 1872 text AC_L_BASH "bash" 1873 text AC_L_CPP_PLUS "c++" 1874 text AC_L_CPP "cpp" 1875 text AC_L_RS "rs" 1876 text AC_L_RUST "rust" 1877 1878 fn ac_is_lang_ch b uchar to bool 1879 if ac_is_lower b 1880 ret true 1881 end 1882 if ac_is_digit b 1883 ret true 1884 end 1885 if eq b 43 1886 ret true 1887 end 1888 if eq b 35 1889 ret true 1890 end 1891 if eq b 46 1892 ret true 1893 end 1894 if eq b 95 1895 ret true 1896 end 1897 if eq b 45 1898 ret true 1899 end 1900 ret false 1901 end 1902 1903 fn ac_lang_of src view uchar * dst edit uchar * to ulong 1904 n = len src 1905 s = ulong 0 1906 while lt s n bound 256 1907 if not ac_is_ws get src s 1908 break 1909 end 1910 s = add s 1 1911 end 1912 e = n 1913 while gt e s bound 256 1914 if not ac_is_ws get src sub e 1 1915 break 1916 end 1917 e = sub e 1 1918 end 1919 tmp = new uchar 64 0 1920 tlen = sub e s 1921 if gt tlen 64 1922 tlen = ulong 64 1923 end 1924 i = ulong 0 1925 while lt i tlen bound 64 1926 put tmp i ac_to_lower get src add s i 1927 i = add i 1 1928 end 1929 if ac_mem_eq part tmp 0 tlen AC_L_PY 1930 ret ac_copy_at dst 0 AC_L_PYTHON 1931 end 1932 if ac_mem_eq part tmp 0 tlen AC_L_PY3 1933 ret ac_copy_at dst 0 AC_L_PYTHON 1934 end 1935 if ac_mem_eq part tmp 0 tlen AC_L_JS 1936 ret ac_copy_at dst 0 AC_L_JAVASCRIPT 1937 end 1938 if ac_mem_eq part tmp 0 tlen AC_L_TS 1939 ret ac_copy_at dst 0 AC_L_TYPESCRIPT 1940 end 1941 if ac_mem_eq part tmp 0 tlen AC_L_SH 1942 ret ac_copy_at dst 0 AC_L_BASH 1943 end 1944 if ac_mem_eq part tmp 0 tlen AC_L_SHELL 1945 ret ac_copy_at dst 0 AC_L_BASH 1946 end 1947 if ac_mem_eq part tmp 0 tlen AC_L_ZSH 1948 ret ac_copy_at dst 0 AC_L_BASH 1949 end 1950 if ac_mem_eq part tmp 0 tlen AC_L_CPP_PLUS 1951 ret ac_copy_at dst 0 AC_L_CPP 1952 end 1953 if ac_mem_eq part tmp 0 tlen AC_L_RS 1954 ret ac_copy_at dst 0 AC_L_RUST 1955 end 1956 p = ulong 0 1957 i = ulong 0 1958 while lt i tlen bound 64 1959 if ge p AC_LANG_MAX 1960 break 1961 end 1962 b = get tmp i 1963 if not ac_is_lang_ch b 1964 break 1965 end 1966 p = ac_push_byte dst p b 1967 i = add i 1 1968 end 1969 ret p 1970 end 1971 1972 fn ac_count_lines src view uchar * to ulong 1973 n = len src 1974 if eq n 0 1975 ret 0 1976 end 1977 cnt = ulong 0 1978 i = ulong 0 1979 while lt i n bound 65536 1980 while lt i n bound 65536 1981 if eq get src i 10 1982 break 1983 end 1984 i = add i 1 1985 end 1986 cnt = add cnt 1 1987 if lt i n 1988 i = add i 1 1989 end 1990 end 1991 ret cnt 1992 end 1993 1994 fn ac_line_end src view uchar * pos ulong to ulong 1995 i = pos 1996 while lt i len src bound 65536 1997 if eq get src i 10 1998 ret i 1999 end 2000 i = add i 1 2001 end 2002 ret i 2003 end 2004 2005 fn ac_within_limits src view uchar * dst edit uchar * meta edit ulong 2 to ulong 2006 kept_len = ulong 0 2007 kept_lines = ulong 0 2008 i = ulong 0 2009 while lt i len src bound 65536 2010 le_end = ac_line_end src i 2011 llen = sub le_end i 2012 if eq kept_lines AC_CODE_LINES 2013 break 2014 end 2015 if gt add kept_len add llen 1 AC_CODE_MAX 2016 break 2017 end 2018 kept_len = ac_copy_at dst kept_len part src i llen 2019 kept_len = ac_push_byte dst kept_len 10 2020 kept_lines = add kept_lines 1 2021 i = le_end 2022 if lt i len src 2023 i = add i 1 2024 end 2025 end 2026 put meta 0 kept_lines 2027 put meta 1 sub ac_count_lines src kept_lines 2028 ret kept_len 2029 end 2030 2031 text AC_FENCE "```" 2032 2033 fn ac_trim_slice_bounds src view uchar * bs ulong be ulong res edit ulong 2 to void 2034 s = bs 2035 e = be 2036 while lt s e bound 65536 2037 if not ac_is_ws get src s 2038 break 2039 end 2040 s = add s 1 2041 end 2042 while gt e s bound 65536 2043 if not ac_is_ws get src sub e 1 2044 break 2045 end 2046 e = sub e 1 2047 end 2048 put res 0 s 2049 put res 1 e 2050 ret 2051 end 2052 2053 fn ac_code_first_word reply view uchar * s ulong e ulong lang_buf edit uchar 32 to ulong 2054 ws = s 2055 while lt ws e bound 256 2056 if not ac_is_ws get reply ws 2057 break 2058 end 2059 ws = add ws 1 2060 end 2061 we = ws 2062 while lt we e bound 256 2063 if ac_is_ws get reply we 2064 break 2065 end 2066 we = add we 1 2067 end 2068 ret ac_lang_of part reply ws sub we ws lang_buf 2069 end 2070 2071 fn ac_code_find_close reply view uchar * body_s ulong to ulong 2072 n = len reply 2073 cur = body_s 2074 while lt cur n bound 65536 2075 le_i = cur 2076 while lt le_i n bound 65536 2077 if eq get reply le_i 10 2078 le_i = add le_i 1 2079 break 2080 end 2081 le_i = add le_i 1 2082 end 2083 ts = cur 2084 while lt ts le_i bound 4096 2085 if not ac_is_ws get reply ts 2086 break 2087 end 2088 ts = add ts 1 2089 end 2090 if ac_starts_with part reply ts sub le_i ts AC_FENCE 2091 ret cur 2092 end 2093 cur = le_i 2094 end 2095 ret n 2096 end 2097 2098 fn ac_code_finalize src view uchar * code_buf edit uchar 32768 info edit AcCodeReply to bool 2099 tb = new ulong 2 0 2100 meta = new ulong 2 0 2101 plain_buf = new uchar 65536 0 2102 plen = ac_plain_code src edit plain_buf 2103 ac_trim_slice_bounds part plain_buf 0 plen 0 plen edit tb 2104 if eq get tb 0 get tb 1 2105 ret false 2106 end 2107 clen = ac_within_limits part plain_buf 0 plen code_buf edit meta 2108 put info code_len clen 2109 put info kept_lines get meta 0 2110 put info cut_lines get meta 1 2111 put info ok 1 2112 ret true 2113 end 2114 2115 fn ac_code_read_reply reply view uchar * sent_buf edit uchar 256 lang_buf edit uchar 32 code_buf edit uchar 32768 info edit AcCodeReply to bool 2116 put info ok 0 2117 put info sent_len 0 2118 put info lang_len 0 2119 put info code_len 0 2120 put info kept_lines 0 2121 put info cut_lines 0 2122 tb = new ulong 2 0 2123 open_i = ac_find_bytes reply AC_FENCE 0 2124 if lt open_i 0 2125 ret false 2126 end 2127 ac_trim_slice_bounds reply 0 ulong open_i edit tb 2128 put info sent_len ac_copy_at sent_buf 0 part reply get tb 0 sub get tb 1 get tb 0 2129 pos = add ulong open_i 3 2130 nl_i = ac_line_end reply pos 2131 put info lang_len ac_code_first_word reply pos nl_i lang_buf 2132 pos = len reply 2133 if lt nl_i len reply 2134 pos = add nl_i 1 2135 end 2136 body_e = ac_code_find_close reply pos 2137 ret ac_code_finalize part reply pos sub body_e pos code_buf info 2138 end 2139 2140 fn ac_code_escape piece view uchar * dst edit uchar * to ulong 2141 n = len piece 2142 p = ulong 0 2143 i = ulong 0 2144 while lt i n bound 4096 2145 b = get piece i 2146 if eq b 10 2147 p = ac_push_byte dst p 92 2148 p = ac_push_byte dst p 110 2149 else 2150 if eq b 92 2151 p = ac_push_byte dst p 92 2152 p = ac_push_byte dst p 92 2153 else 2154 p = ac_push_byte dst p b 2155 end 2156 end 2157 i = add i 1 2158 end 2159 ret p 2160 end 2161 2162 fn ac_code_piece_count clen ulong to ulong 2163 if eq clen 0 2164 ret 0 2165 end 2166 ret div add clen sub AC_PIECE 1 AC_PIECE 2167 end 2168 2169 text AC_CODE_PFX "CODE " 2170 2171 fn ac_code_format_piece req view AcCodePieceReq lang view uchar 32 code view uchar * dst edit uchar * to ulong 2172 esc = new uchar 264 0 2173 off = mul get req idx AC_PIECE 2174 if ge off get req code_len 2175 ret 0 2176 end 2177 plen = sub get req code_len off 2178 if gt plen AC_PIECE 2179 plen = AC_PIECE 2180 end 2181 p = ulong 0 2182 p = ac_copy_at dst p AC_CODE_PFX 2183 p = ac_fmt_u64 dst p ulong get req seq 2184 p = ac_push_byte dst p 32 2185 p = ac_fmt_u64 dst p add get req idx 1 2186 p = ac_push_byte dst p 47 2187 p = ac_fmt_u64 dst p get req total_n 2188 p = ac_push_byte dst p 32 2189 p = ac_fmt_u64 dst p get req code_len 2190 p = ac_push_byte dst p 32 2191 p = ac_fmt_hex8 dst p get req crc 2192 p = ac_push_byte dst p 32 2193 if eq get req lang_len 0 2194 p = ac_push_byte dst p 45 2195 else 2196 p = ac_copy_at dst p part lang 0 get req lang_len 2197 end 2198 p = ac_push_byte dst p 32 2199 elen = ac_code_escape part code off plen edit esc 2200 p = ac_copy_at dst p part esc 0 elen 2201 ret p 2202 end