interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / crates / allen-courier / courier_core.om

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