interplane.Get Omega
Read in your language

hop

Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.

Brought by drakestapleton

branch main Branches Tags History

hop / Omega / tests / mutant / hopcore-addr.om

5040 lines · 97566 bytes Raw Blame History
1note hopcore: Hop's matching core, an Omega program built by Omega NG (ng front-ir, opt, a64 macho). It holds
2note hopfuzz, the name matcher of the one box, and a main that serves it to Hop over stdin and stdout.
3note Contract: Omega/README.md.
4note One call scores typed text against one name: a whole match beats a prefix, which beats a later
5note word start, initials, a plain "contains", one wrong letter, and letters in order with few gaps.
6note The host lowercases both texts and sends one word per character: bits 0..31 a character id
7note (equal characters get equal ids), bit 32 "letter or number", bit 33 "trimmable white space".
8note Any malformed input fails closed as verdict 2 INVALID with the first faulting word; a text
9note over the limits is INVALID, never cut short. Verdict 0 is "no match", 1 is "match" (score at 3).
10
11fn isword w ulong to bool
12ret ne and w 4294967296 0
13end
14
15fn istrim w ulong to bool
16ret ne and w 8589934592 0
17end
18
19fn minu a ulong b ulong to ulong
20if lt a b
21ret a
22end
23ret b
24end
25
26note true when inp[a .. a+n) equals inp[b .. b+n); both runs must lie inside the buffer
27fn same inp view ulong 256 a ulong b ulong n ulong to bool
28for k ulong n
29if ne get inp add a k get inp add b k
30ret false
31end
32end
33ret true
34end
35
36note x is one longer than y (ls characters): true when dropping one character of x gives y
37fn skip1 inp view ulong 256 x ulong y ulong ls ulong to bool
38k = ulong 0
39while lt k ls bound 256
40if ne get inp add x k get inp add y k
41break
42end
43k = add k 1
44end
45ret same inp add x add k 1 add y k sub ls k
46end
47
48note edit distance at most 1, where swapping two neighbours counts as one
49fn near inp view ulong 256 a ulong la ulong b ulong lb ulong to bool
50if eq la lb
51d = ulong 0
52p = ulong 0
53for k ulong la
54if ne get inp add a k get inp add b k
55if eq d 0
56p = k
57end
58d = add d 1
59end
60end
61if le d 1
62ret true
63end
64if eq d 2
65if lt add p 1 la
66if eq get inp add a p get inp add b add p 1
67if eq get inp add a add p 1 get inp add b p
68ret true
69end
70end
71end
72end
73ret false
74end
75if eq la add lb 1
76ret skip1 inp a b lb
77end
78if eq lb add la 1
79ret skip1 inp b a la
80end
81ret false
82end
83
84note score of q = inp[qa .. qa+ql) in the name inp[72 .. 72+nl); 0 means no match. ql and nl >= 1
85fn one inp view ulong 256 qa ulong ql ulong nl ulong to ulong
86if le ql nl
87if same inp qa 72 ql
88if eq ql nl
89ret 200
90end
91ret sub 160 minu 30 sub nl ql
92end
93end
94qw = true
95for k ulong ql
96if not isword get inp add qa k
97qw = false
98end
99end
100wc = ulong 0
101later = false
102inits = true
103inw = false
104for i ulong nl
105c = get inp add 72 i
106if isword c
107if not inw
108if lt wc ql
109if ne c get inp add qa wc
110inits = false
111end
112end
113if gt wc 0
114if qw
115if le add i ql nl
116if same inp qa add 72 i ql
117later = true
118end
119end
120end
121end
122wc = add wc 1
123end
124inw = true
125else
126inw = false
127end
128end
129if later
130ret sub 120 minu 20 div nl 3
131end
132if ge ql 2
133if gt wc 1
134if ge wc ql
135if inits
136ret 110
137end
138end
139end
140end
141if le ql nl
142for i ulong add sub nl ql 1
143if same inp qa add 72 i ql
144ret sub 80 minu 20 div nl 3
145end
146end
147end
148if ge ql 4
149if le ql nl
150if near inp qa ql 72 ql
151ret 70
152end
153end
154if le sub ql 1 nl
155if near inp qa ql 72 sub ql 1
156ret 70
157end
158end
159if le add ql 1 nl
160if near inp qa ql 72 add ql 1
161ret 70
162end
163end
164end
165if lt ql 3
166ret 0
167end
168qi = ulong 0
169gaps = ulong 0
170last = ulong 0
171first = ulong 0
172for i ulong nl
173if lt qi ql
174if eq get inp add 72 i get inp add qa qi
175if gt qi 0
176gaps = add gaps sub sub i last 1
177else
178first = i
179end
180last = i
181qi = add qi 1
182end
183end
184end
185if ne qi ql
186ret 0
187end
188if gt gaps ql
189ret 0
190end
191t = add mul gaps 6 mul first 3
192if ge t 45
193ret 15
194end
195ret sub 60 t
196end
197
198note first faulting word plus 1, or 0 when the input is well formed
199fn check inp view ulong 256 to ulong
200if ne get inp 0 1212568922
201ret 1
202end
203if ne get inp 1 1
204ret 2
205end
206ql = get inp 2
207nl = get inp 3
208if gt ql 64
209ret 3
210end
211if gt nl 184
212ret 4
213end
214for i ulong 4
215if ne get inp add 4 i 0
216ret add 5 i
217end
218end
219for i ulong 64
220w = get inp add 8 i
221if lt i ql
222if ge w 17179869184
223ret add 9 i
224end
225if eq and w 4294967295 0
226ret add 9 i
227end
228else
229if ne w 0
230ret add 9 i
231end
232end
233end
234for i ulong 184
235w = get inp add 72 i
236if lt i nl
237if ge w 17179869184
238ret add 73 i
239end
240if eq and w 4294967295 0
241ret add 73 i
242end
243else
244if ne w 0
245ret add 73 i
246end
247end
248end
249ret 0
250end
251
252fn hopfuzz inp view ulong 256 res edit ulong 256 to ulong
253for i ulong 256
254put res i 0
255end
256put res 0 1212568922
257put res 1 1
258e = check inp
259if ne e 0
260put res 2 2
261put res 4 sub e 1
262ret 2
263end
264nl = get inp 3
265qa = ulong 8
266qe = add 8 get inp 2
267while lt qa qe bound 256
268if not istrim get inp qa
269break
270end
271qa = add qa 1
272end
273while gt qe qa bound 256
274if not istrim get inp sub qe 1
275break
276end
277qe = sub qe 1
278end
279ql = sub qe qa
280if eq ql 0
281ret 0
282end
283if eq nl 0
284ret 0
285end
286s = one inp qa ql nl
287if eq s 0
288terms = ulong 0
289sum = ulong 0
290p = qa
291while lt p qe bound 256
292if eq and get inp p 4294967295 32
293p = add p 1
294continue
295end
296b = p
297while lt p qe bound 256
298if eq and get inp p 4294967295 32
299break
300end
301p = add p 1
302end
303ts = one inp b sub p b nl
304if eq ts 0
305ret 0
306end
307terms = add terms 1
308sum = add sum ts
309end
310if le terms 1
311ret 0
312end
313s = sub div sum terms 10
314end
315put res 2 1
316put res 3 s
317ret 1
318end
319
320note ---- file-name scoring (kind 2): how well a path found on another machine matches the typed terms ----
321note The host sends bytes: the terms, and for each path its lowercased bytes, the lowercased bytes of its last
322note component (its name), and the byte length of the path as written. A term found in the name scores 60 when the name
323note starts with it and 30 otherwise; found only elsewhere in the path it scores 5; a term found nowhere, or an
324note empty term, means no match. One point less for every 20 bytes of path; 40 more when the name is exactly as long as the terms.
325
326fn rd16 b view uchar 1048576 o ulong to ulong
327ret or ulong get b o shl ulong get b add o 1 8
328end
329
330fn rd32 b view uchar 1048576 o ulong to ulong
331ret or rd16 b o shl rd16 b add o 2 16
332end
333
334note true when b[x .. x+n) equals b[y .. y+n)
335fn beq b view uchar 1048576 x ulong y ulong n ulong to bool
336for k ulong n
337if ne get b add x k get b add y k
338ret false
339end
340end
341ret true
342end
343
344note true when the run b[t .. t+tl) occurs in b[h .. h+hl)
345fn bhas b view uchar 1048576 h ulong hl ulong t ulong tl ulong to bool
346if gt tl hl
347ret false
348end
349for i ulong add sub hl tl 1
350if beq b add h i t tl
351ret true
352end
353end
354ret false
355end
356
357note score of one path plus 1000000, or 0 for no match. pm: 0 the number of terms, 1 where the path's bytes
358note start, 2 how many, 3 where its name's bytes start, 4 how many, 5 the path's length as written
359fn fscore b view uchar 1048576 toff view ulong 64 tlen view ulong 64 pm view ulong 8 to ulong
360nt = get pm 0
361p = get pm 1
362pl = get pm 2
363ns = get pm 3
364nl = get pm 4
365ol = get pm 5
366s = ulong 1000000
367sum = ulong 0
368for k ulong nt
369t = get toff k
370tl = get tlen k
371sum = add sum tl
372if eq tl 0
373ret 0
374end
375if bhas b ns nl t tl
376if beq b ns t tl
377s = add s 60
378else
379s = add s 30
380end
381else
382if bhas b p pl t tl
383s = add s 5
384else
385ret 0
386end
387end
388end
389s = sub s div ol 20
390if eq nl sum
391s = add s 40
392end
393ret s
394end
395
396note ---- text decisions (kind 3): is it a question, which words to search for, is it a web address ----
397note The host lowercases the text and sends one 8-byte word per character: bits 0..31 an id (equal characters get
398note equal ids; a character of one Unicode scalar has that scalar's number), then a flag byte: 1 letter or number,
399note 2 trimmable white space, 4 white space, 8 white space or line end.
400note lists: three lists of plain words, separated by "|": 0 the words a question starts with, 1 the words a search
401note leaves out, 2 the endings of a bare web address.
402text lists "what what's whats why how how's who who's whom whose when where which explain define translate summarize summarise describe|the and for with that this what when where does did how why can you are was were have has from into about please make need want just our my its not|com org net io dev ai co edu gov xyz me us uk ca de tv gg"
403
404fn cid b view uchar 1048576 i ulong to ulong
405ret rd32 b add 16 mul i 8
406end
407
408fn cfl b view uchar 1048576 i ulong to ulong
409ret ulong get b add 20 mul i 8
410end
411
412note true when the n characters from s are exactly one word of list sec
413fn inlist b view uchar 1048576 s ulong n ulong sec ulong to bool
414cur = ulong 0
415p = ulong 0
416ln = len lists
417while lt p ln bound 999999
418e = p
419while lt e ln bound 999999
420c = ulong get lists e
421if or eq c 32 eq c 124
422break
423end
424e = add e 1
425end
426if eq cur sec
427if eq sub e p n
428ok = true
429for k ulong n
430if ne cid b add s k ulong get lists add p k
431ok = false
432end
433end
434if ok
435ret true
436end
437end
438end
439if lt e ln
440if eq ulong get lists e 124
441cur = add cur 1
442end
443end
444p = add e 1
445end
446ret false
447end
448
449note op 1: 1 when the text reads as a question: it ends with "?" and is longer than that, or it has three words
450note or more and starts with a question word
451fn qlike b view uchar 1048576 n ulong to ulong
452a = ulong 0
453e = n
454while lt a e bound 999999
455if eq and cfl b a 8 0
456break
457end
458a = add a 1
459end
460while gt e a bound 999999
461if eq and cfl b sub e 1 8 0
462break
463end
464e = sub e 1
465end
466if gt sub e a 1
467if eq cid b sub e 1 63
468ret 1
469end
470end
471w = ulong 0
472fs = ulong 0
473fl = ulong 0
474i = a
475while lt i e bound 999999
476if ne and cfl b i 4 0
477i = add i 1
478continue
479end
480st = i
481while lt i e bound 999999
482if ne and cfl b i 4 0
483break
484end
485i = add i 1
486end
487if eq w 0
488fs = st
489fl = sub i st
490end
491w = add w 1
492end
493if lt w 3
494ret 0
495end
496if inlist b fs fl 0
497ret 1
498end
499ret 0
500end
501
502note a character of a search word: a letter, a number, "-", "_" or "."
503fn iswc b view uchar 1048576 i ulong to bool
504if ne and cfl b i 1 0
505ret true
506end
507c = cid b i
508if eq c 45
509ret true
510end
511if eq c 95
512ret true
513end
514ret eq c 46
515end
516
517fn samec b view uchar 1048576 x ulong y ulong n ulong to bool
518for k ulong n
519if ne cid b add x k cid b add y k
520ret false
521end
522end
523ret true
524end
525
526note true when the word of wl characters at st also stands earlier in the text
527fn seen b view uchar 1048576 st ulong wl ulong to bool
528i = ulong 0
529while lt i st bound 999999
530if not iswc b i
531i = add i 1
532continue
533end
534s0 = i
535while lt i st bound 999999
536if not iswc b i
537break
538end
539i = add i 1
540end
541if eq sub i s0 wl
542if samec b s0 st wl
543ret true
544end
545end
546end
547ret false
548end
549
550note op 2: the words to search for: three characters or more, not a left-out word, each once, the longest four,
551note longest first and in the order typed among equals. Writes the count, then where each starts and its length
552fn keyw b view uchar 1048576 n ulong ob edit uchar 65544 to ulong
553ts = new ulong 4 0
554tl = new ulong 4 0
555k = ulong 0
556i = ulong 0
557while lt i n bound 999999
558if not iswc b i
559i = add i 1
560continue
561end
562st = i
563while lt i n bound 999999
564if not iswc b i
565break
566end
567i = add i 1
568end
569wl = sub i st
570if lt wl 3
571continue
572end
573if inlist b st wl 1
574continue
575end
576if seen b st wl
577continue
578end
579pos = k
580for j ulong k
581if lt get tl j wl
582if eq pos k
583pos = j
584end
585end
586end
587if lt pos 4
588for d ulong sub 3 pos
589u = sub 3 d
590put tl u get tl sub u 1
591put ts u get ts sub u 1
592end
593put tl pos wl
594put ts pos st
595if lt k 4
596k = add k 1
597end
598end
599end
600put ob 8 uchar k
601for j ulong k
602s = get ts j
603l = get tl j
604put ob add 9 mul j 4 uchar and s 255
605put ob add 10 mul j 4 uchar shr s 8
606put ob add 11 mul j 4 uchar and l 255
607put ob add 12 mul j 4 uchar shr l 8
608end
609ret add 1 mul k 4
610end
611
612fn hasscheme b view uchar 1048576 n ulong to bool
613if lt n 7
614ret false
615end
616if ne cid b 0 104
617ret false
618end
619if ne cid b 1 116
620ret false
621end
622if ne cid b 2 116
623ret false
624end
625if ne cid b 3 112
626ret false
627end
628o = ulong 4
629if eq cid b 4 115
630o = ulong 5
631end
632if lt n add o 3
633ret false
634end
635if ne cid b o 58
636ret false
637end
638if ne cid b add o 1 47
639ret false
640end
641ret eq cid b add o 2 47
642end
643
644note op 3: 0 not a web address; 1 it starts with http:// or https:// (the host then checks that it names a
645note machine); 2 a bare address such as example.com/page (the host then checks that it parses). aux: the text's
646note length in bytes as written
647fn weba b view uchar 1048576 n ulong aux ulong to ulong
648if eq n 0
649ret 0
650end
651if ge aux 2000
652ret 0
653end
654for i ulong n
655if ne and cfl b i 4 0
656ret 0
657end
658end
659if hasscheme b n
660ret 1
661end
662h0 = ulong 0
663while lt h0 n bound 999999
664if ne cid b h0 47
665break
666end
667h0 = add h0 1
668end
669h1 = h0
670while lt h1 n bound 999999
671if eq cid b h1 47
672break
673end
674h1 = add h1 1
675end
676parts = ulong 0
677ps = h0
678i = h0
679while le i h1 bound 999999
680atend = eq i h1
681dot = false
682if not atend
683dot = eq cid b i 46
684end
685if or atend dot
686if eq i ps
687ret 0
688end
689parts = add parts 1
690if atend
691if lt parts 2
692ret 0
693end
694if inlist b ps sub i ps 2
695ret 2
696end
697ret 0
698end
699ps = add i 1
700else
701if eq and cfl b i 1 0
702if ne cid b i 45
703ret 0
704end
705end
706end
707i = add i 1
708end
709ret 0
710end
711
712note the single-character edits that turn one spelling into another, on letters and numbers only. r: 0 and 1 the
713note first spelling's characters [from, to), 2 and 3 the second's. Writes 4 the edits and 5 the longer length.
714note Returns 1, and writes nothing, when a spelling has more than 4096 letters and numbers
715fn edist b view uchar 1048576 r edit ulong 8 to ulong
716xa = new ulong 4096 0
717ya = new ulong 4096 0
718row = new ulong 4097 0
719xc = ulong 0
720yc = ulong 0
721i = get r 0
722x1 = get r 1
723while lt i x1 bound 999999
724if ne and cfl b i 1 0
725if ge xc 4096
726ret 1
727end
728put xa xc cid b i
729xc = add xc 1
730end
731i = add i 1
732end
733i = get r 2
734y1 = get r 3
735while lt i y1 bound 999999
736if ne and cfl b i 1 0
737if ge yc 4096
738ret 1
739end
740put ya yc cid b i
741yc = add yc 1
742end
743i = add i 1
744end
745for j ulong add yc 1
746put row j j
747end
748for k ulong xc
749prev = get row 0
750put row 0 add k 1
751xi = get xa k
752for j ulong yc
753cur = get row add j 1
754v = add cur 1
755w = add get row j 1
756if lt w v
757v = w
758end
759w = prev
760if ne xi get ya j
761w = add prev 1
762end
763if lt w v
764v = w
765end
766put row add j 1 v
767prev = cur
768end
769end
770m = xc
771if gt yc m
772m = yc
773end
774put r 4 get row yc
775put r 5 m
776ret 0
777end
778
779note op 4: how alike two spellings are. The text is the first spelling (aux characters), then the second. Writes
780note the edits and the longer length, each as a 16-bit number; the host divides
781fn alike b view uchar 1048576 n ulong aux ulong ob edit uchar 65544 to ulong
782r = new ulong 8 0
783bad = ulong 18446744073709551615
784if gt aux n
785ret bad
786end
787put r 0 0
788put r 1 aux
789put r 2 aux
790put r 3 n
791if ne edist b r 0
792ret bad
793end
794d = get r 4
795m = get r 5
796put ob 8 uchar and d 255
797put ob 9 uchar shr d 8
798put ob 10 uchar and m 255
799put ob 11 uchar shr m 8
800ret 4
801end
802
803note op 5: which run of typed words is the fix for what was heard. The text is what was heard (aux characters),
804note then up to four runs: the first typed word, the first two, the first three, the first four; flag 16 marks the
805note last character of each run. Each run is compared with what was heard (fn edist); the most alike wins, the
806note shorter run among equals. Returns which run (1 to 4), or 0 for no fix: the winner is less than half alike, or
807note is the very text that was heard (the host says which runs are: bit k-1 of sm). 255: a spelling is too long
808fn pick b view uchar 1048576 n ulong aux ulong sm ulong to ulong
809r = new ulong 8 0
810if gt aux n
811ret 255
812end
813bk = ulong 0
814bn = ulong 0
815bd = ulong 1
816k = ulong 0
817i = aux
818while lt i n bound 999999
819e = i
820while lt e n bound 999999
821f = cfl b e
822e = add e 1
823if ne and f 16 0
824break
825end
826end
827k = add k 1
828if gt k 4
829break
830end
831put r 0 0
832put r 1 aux
833put r 2 i
834put r 3 e
835if ne edist b r 0
836ret 255
837end
838m = get r 5
839nu = ulong 1
840de = ulong 1
841if gt m 0
842nu = sub m get r 4
843de = m
844end
845if eq bk 0
846bk = k
847bn = nu
848bd = de
849else
850if gt mul nu bd mul bn de
851bk = k
852bn = nu
853bd = de
854end
855end
856i = e
857end
858if eq bk 0
859ret 0
860end
861if lt mul bn 2 bd
862ret 0
863end
864if ne and shr sm sub bk 1 1 0
865ret 0
866end
867ret bk
868end
869
870note kind 3: op, the number of characters n (at most 16384), aux and 0 as four 32-bit numbers, then n character
871note words. Returns the reply length, or 18446744073709551615 for a malformed body or an unknown op
872fn texts ib view uchar 1048576 nb ulong ob edit uchar 65544 to ulong
873bad = ulong 18446744073709551615
874if lt nb 16
875ret bad
876end
877op = rd32 ib 0
878n = rd32 ib 4
879aux = rd32 ib 8
880if gt n 16384
881ret bad
882end
883if ne nb add 16 mul n 8
884ret bad
885end
886if eq op 1
887put ob 8 uchar qlike ib n
888ret 1
889end
890if eq op 2
891ret keyw ib n ob
892end
893if eq op 3
894put ob 8 uchar weba ib n aux
895ret 1
896end
897if eq op 4
898ret alike ib n aux ob
899end
900if eq op 5
901put ob 8 uchar pick ib n aux rd32 ib 12
902ret 1
903end
904ret bad
905end
906
907note ---- the helper process ----
908note hopcore main: Hop's core as a helper process. A request is 8 bytes (kind, then body length n, both 32-bit
909note little-endian) and n body bytes, n at most 1048576. The reply is 8 bytes (status, then length m) and m bytes.
910note Status 0 is an answer; 1 an unknown kind; 2 a malformed body. End of input ends it with status 0; a body
911note over the limit or a short read ends it with status 3 (the host starts a fresh one).
912note Kind 1, names: the body is k cases of 2048 bytes (256 words, the hopfuzz input); 3 bytes back per case:
913note verdict, score low byte, score high byte. At most 512 cases.
914note Kind 2, paths: 16-bit little-endian fields: number of terms (at most 64), number of paths (at most 16000);
915note each term as length then bytes; each path as three lengths (its lowercased bytes, its name's lowercased
916note bytes, the path as written), then the path bytes, then the name bytes. 4 bytes back per path: 0 for no
917note match, else the score plus 1000000.
918note Kind 3, text: see fn texts. Op 1 and op 3 answer one byte; op 2 a count, then two 16-bit numbers a word;
919note op 4 two 16-bit numbers; op 5 one byte.
920note Kind 4, corrected words: see fn wdiff. Kind 5, a board line or a time: see fn board. Kind 6, a whole board: see fn asm. Kind 7, the members' state and the graph: see fn status.
921
922note fill b[0 .. n) from stdin; false at end of input or on a short read
923fn rdn b edit uchar 1048576 n ulong to bool
924got = ulong 0
925while lt got n bound 999999
926k = read 0 b got sub n got
927if le k 0
928ret false
929end
930got = add got ulong k
931end
932ret true
933end
934
935fn wrn b view uchar 65544 n ulong to bool
936p = ulong 0
937while lt p n bound 999999
938k = write 1 b p sub n p
939if le k 0
940ret false
941end
942p = add p ulong k
943end
944ret true
945end
946
947fn put32 b edit uchar 65544 o ulong v ulong to void
948put b o uchar and v 255
949put b add o 1 uchar and shr v 8 255
950put b add o 2 uchar and shr v 16 255
951put b add o 3 uchar and shr v 24 255
952end
953
954note kind 1: returns the reply length, or 18446744073709551615 for a malformed body
955note ---- one line of a plain-text board (kind 5) ----
956note A swarm's board is a text file, one post a line:
957note <time> <sender> (<detail>) to <recipient> + <recipient> (<detail>): <text>
958note Also read: several senders joined by " + ", no "to" part, recipients written "@name", a leading "- ", and
959note words between the sender's bracket and the colon (they stay in the text). A line that does not start with a
960note time is not a post. A header that does not fit the form is read plainly: the first word is the sender, the
961note rest is the text. The answer names every part by where it starts in the line and how long it is.
962note Limits: 65536 bytes of line, 600 of header, 4 senders, 16 recipients, 64 bytes and 3 words a name, 32768 bytes
963note of text; over a limit the part is cut at a whole character and the answer says so.
964
965note byte i of the line (the line follows the 8-byte op header of the request)
966fn lc b view uchar 1048576 i ulong to ulong
967ret ulong get b add 8 i
968end
969
970fn lsp c ulong to bool
971ret or eq c 32 eq c 9
972end
973
974note the two digits at byte i of the n-byte line as a number, or 100
975fn l2d b view uchar 1048576 i ulong n ulong to ulong
976if ge add i 1 n
977ret 100
978end
979x = lc b i
980y = lc b add i 1
981if or lt x 48 gt x 57
982ret 100
983end
984if or lt y 48 gt y 57
985ret 100
986end
987ret add mul sub x 48 10 sub y 48
988end
989
990note out[k], out[k+1]: the bytes lo..hi without spaces at either end (equal when nothing is left)
991fn ltrim b view uchar 1048576 lo ulong hi ulong outv edit ulong 8 k ulong to void
992a = lo
993e = hi
994while lt a e bound 999999
995if not lsp lc b a
996break
997end
998a = add a 1
999end
1000while gt e a bound 999999
1001if not lsp lc b sub e 1
1002break
1003end
1004e = sub e 1
1005end
1006put outv k a
1007put outv add k 1 e
1008end
1009
1010note where a text of bytes a..e is cut to lim bytes without splitting a character (e when it is short enough)
1011fn lclip b view uchar 1048576 a ulong e ulong lim ulong to ulong
1012if le sub e a lim
1013ret e
1014end
1015ce = add a lim
1016back = ulong 0
1017while gt ce a bound 8
1018if ge back 3
1019break
1020end
1021if ne and lc b ce 192 128
1022break
1023end
1024ce = sub ce 1
1025back = add back 1
1026end
1027ret ce
1028end
1029
1030note "YYYY-MM-DD", then optionally "T" or one space and "HH:MM" or "HH:MM:SS", a fraction, and "Z" or an offset
1031note such as "+00:00", from byte s of the n-byte line. Returns 1 when it is a time, from 1970 on. tm: 1 the
1032note seconds since 1970 as written (less the offset when one is named) plus 100000, 2 the first byte after the
1033note time, 3 1 when the text names its zone
1034fn ltime b view uchar 1048576 s ulong n ulong tm edit ulong 16 to ulong
1035if gt add s 10 n
1036ret 0
1037end
1038ce = l2d b s n
1039yy = l2d b add s 2 n
1040mo = l2d b add s 5 n
1041dy = l2d b add s 8 n
1042if or eq ce 100 eq yy 100
1043ret 0
1044end
1045if or or lt mo 1 gt mo 12 or lt dy 1 gt dy 31
1046ret 0
1047end
1048if or ne lc b add s 4 45 ne lc b add s 7 45
1049ret 0
1050end
1051yr = add mul ce 100 yy
1052if lt yr 1970
1053ret 0
1054end
1055p = add s 10
1056sec = ulong 0
1057off = ulong 0
1058pls = ulong 0
1059zoned = ulong 0
1060tp = ulong 0
1061if lt add p 5 n
1062tp = lc b p
1063end
1064if or eq tp 84 eq tp 32
1065hr = l2d b add p 1 n
1066mi = l2d b add p 4 n
1067okhm = false
1068if and lt hr 24 lt mi 60
1069if eq lc b add p 3 58
1070okhm = true
1071end
1072end
1073if okhm
1074sec = add mul hr 3600 mul mi 60
1075p = add p 6
1076if lt p n
1077if eq lc b p 58
1078sx = l2d b add p 1 n
1079if lt sx 61
1080sec = add sec sx
1081p = add p 3
1082end
1083end
1084end
1085if lt p n
1086if eq lc b p 46
1087q = add p 1
1088while lt q n bound 32
1089if gt sub q p 9
1090break
1091end
1092c = lc b q
1093if or lt c 48 gt c 57
1094break
1095end
1096q = add q 1
1097end
1098if gt q add p 1
1099p = q
1100end
1101end
1102end
1103if lt p n
1104zc = lc b p
1105if or eq zc 90 eq zc 122
1106zoned = 1
1107p = add p 1
1108else
1109if or eq zc 43 eq zc 45
1110oh = l2d b add p 1 n
1111om = ulong 100
1112used = ulong 5
1113c3 = false
1114if lt add p 3 n
1115if eq lc b add p 3 58
1116c3 = true
1117end
1118end
1119if c3
1120om = l2d b add p 4 n
1121used = 6
1122else
1123om = l2d b add p 3 n
1124end
1125if and lt oh 24 lt om 60
1126o = add mul oh 3600 mul om 60
1127if eq zc 43
1128off = o
1129else
1130pls = o
1131end
1132zoned = 1
1133p = add p used
1134end
1135end
1136end
1137end
1138else
1139if eq tp 84
1140ret 0
1141end
1142end
1143else
1144if lt p n
1145if eq lc b p 84
1146ret 0
1147end
1148end
1149end
1150y = yr
1151if le mo 2
1152y = sub yr 1
1153end
1154era = div y 400
1155yoe = sub y mul era 400
1156mm = add mo 9
1157if gt mo 2
1158mm = sub mo 3
1159end
1160doy = sub add div add mul 153 mm 2 5 dy 1
1161doe = add sub add mul yoe 365 div yoe 4 div yoe 100 doy
1162days = sub add mul era 146097 doe 719468
1163put tm 1 sub add add add mul days 86400 sec 100000 pls off
1164put tm 2 p
1165put tm 3 zoned
1166ret 1
1167end
1168
1169note one name from byte lo, stopping before " + ", before "(" and, on the sender side (side 1), before " to "
1170note and " @". A bracket right after the name is its detail. nr: 0 and 1 the name, 2 and 3 the detail, 4 the first
1171note byte after the name, its bracket and the spaces that follow, 5 1 when there was a bracket
1172fn lname b view uchar 1048576 lo ulong limit ulong side ulong nr edit ulong 8 to void
1173i = lo
1174while lt i limit bound 999999
1175if not lsp lc b i
1176break
1177end
1178i = add i 1
1179end
1180start = i
1181while lt i limit bound 999999
1182c = lc b i
1183if eq c 40
1184break
1185end
1186stop = false
1187if eq c 32
1188if lt add i 1 limit
1189d = lc b add i 1
1190if eq d 43
1191if ge add i 2 limit
1192stop = true
1193else
1194if eq lc b add i 2 32
1195stop = true
1196end
1197end
1198end
1199if eq side 1
1200if eq d 64
1201stop = true
1202end
1203if eq d 116
1204if lt add i 2 limit
1205if eq lc b add i 2 111
1206if ge add i 3 limit
1207stop = true
1208else
1209if eq lc b add i 3 32
1210stop = true
1211end
1212end
1213end
1214end
1215end
1216end
1217end
1218end
1219if stop
1220break
1221end
1222i = add i 1
1223end
1224ltrim b start i nr 0
1225while lt i limit bound 999999
1226if not lsp lc b i
1227break
1228end
1229i = add i 1
1230end
1231put nr 2 0
1232put nr 3 0
1233put nr 5 0
1234if lt i limit
1235if eq lc b i 40
1236depth = ulong 0
1237j = i
1238while lt j limit bound 999999
1239cj = lc b j
1240if eq cj 40
1241depth = add depth 1
1242else
1243if eq cj 41
1244depth = sub depth 1
1245if eq depth 0
1246break
1247end
1248end
1249end
1250j = add j 1
1251end
1252ltrim b add i 1 j nr 2
1253put nr 5 1
1254i = limit
1255if lt j limit
1256i = add j 1
1257end
1258while lt i limit bound 999999
1259if not lsp lc b i
1260break
1261end
1262i = add i 1
1263end
1264end
1265end
1266put nr 4 i
1267end
1268
1269note a name fits when it has 1 to 64 bytes and at most 3 words
1270fn lfits b view uchar 1048576 a ulong e ulong to bool
1271if le e a
1272ret false
1273end
1274if gt sub e a 64
1275ret false
1276end
1277n = ulong 0
1278inw = false
1279i = a
1280while lt i e bound 999999
1281if lsp lc b i
1282inw = false
1283else
1284if not inw
1285inw = true
1286n = add n 1
1287end
1288end
1289i = add i 1
1290end
1291ret le n 3
1292end
1293
1294note "all", "all lanes", "everyone" (any case, a leading "@" dropped): the whole board, not one member.
1295note The name fits (lfits), so its key has at most 64 bytes
1296fn levery b view uchar 1048576 a ulong e ulong to bool
1297k = new uchar 80 0
1298n = ulong 0
1299i = a
1300if lt i e
1301if eq lc b i 64
1302i = add i 1
1303end
1304end
1305gap = false
1306while lt i e bound 999999
1307c = lc b i
1308if lsp c
1309gap = true
1310else
1311if and gap gt n 0
1312put k n uchar 45
1313n = add n 1
1314end
1315gap = false
1316if and ge c 65 le c 90
1317c = add c 32
1318end
1319put k n uchar c
1320n = add n 1
1321end
1322i = add i 1
1323end
1324if lt n 3
1325ret false
1326end
1327al = and and eq ulong get k 0 97 eq ulong get k 1 108 eq ulong get k 2 108
1328if eq n 3
1329ret al
1330end
1331if gt n 4
1332if and al eq ulong get k 3 45
1333ret true
1334end
1335end
1336if ne n 8
1337ret false
1338end
1339ev = and and eq ulong get k 0 101 eq ulong get k 1 118 and eq ulong get k 2 101 eq ulong get k 3 114
1340ev = and ev and and eq ulong get k 4 121 eq ulong get k 5 111 and eq ulong get k 6 110 eq ulong get k 7 101
1341ret ev
1342end
1343
1344note where a part starts and how long it is, at byte o of the answer
1345fn lput ob edit uchar 65544 o ulong s ulong e ulong to void
1346l = ulong 0
1347if gt e s
1348l = sub e s
1349end
1350put32 ob add 8 o s
1351put32 ob add 12 o l
1352end
1353
1354note op 1: one board line of n0 bytes. The answer is 344 bytes: 0 1 when it is a post, 1 1 when it is to all,
1355note 2 1 when something went over a limit, 3 the senders, 4 the recipients; 8 the time (64-bit, seconds since 1970
1356note plus 100000); 16 the text; from 24 sixteen bytes a sender (name, detail), from 88 sixteen a recipient. Each
1357note part is two 32-bit numbers: where it starts in the line and its length
1358fn lline b view uchar 1048576 n0 ulong ob edit uchar 65544 to ulong
1359tm = new ulong 16 0
1360nr = new ulong 8 0
1361for z ulong 344
1362put ob add 8 z uchar 0
1363end
1364n = n0
1365clipped = ulong 0
1366if gt n 65536
1367n = lclip b 0 n 65536
1368clipped = 1
1369end
1370put ob 10 uchar clipped
1371en = n
1372while gt en 0 bound 999999
1373c = lc b sub en 1
1374blank = lsp c
1375if or eq c 13 eq c 10
1376blank = true
1377end
1378if not blank
1379break
1380end
1381en = sub en 1
1382end
1383p = ulong 0
1384while lt p en bound 999999
1385if not lsp lc b p
1386break
1387end
1388p = add p 1
1389end
1390if lt add p 1 en
1391if and eq lc b p 45 eq lc b add p 1 32
1392p = add p 2
1393end
1394end
1395if eq ltime b p n tm 0
1396ret 344
1397end
1398te = get tm 2
1399if ge te en
1400ret 344
1401end
1402if not lsp lc b te
1403ret 344
1404end
1405p = te
1406while lt p en bound 999999
1407if not lsp lc b p
1408break
1409end
1410p = add p 1
1411end
1412if ge p en
1413ret 344
1414end
1415put ob 8 uchar 1
1416tv = get tm 1
1417put32 ob 16 tv
1418put32 ob 20 shr tv 32
1419bst = p
1420colon = ulong 0
1421hasc = false
1422depth = ulong 0
1423i = p
1424while lt i en bound 999999
1425if ge sub i p 600
1426break
1427end
1428c = lc b i
1429if eq c 40
1430depth = add depth 1
1431else
1432if eq c 41
1433if gt depth 0
1434depth = sub depth 1
1435end
1436else
1437if and eq c 58 eq depth 0
1438nx = true
1439if lt add i 1 en
1440if ne lc b add i 1 32
1441nx = false
1442end
1443end
1444if nx
1445colon = i
1446hasc = true
1447break
1448end
1449end
1450end
1451end
1452i = add i 1
1453end
1454fits = hasc
1455ts = add colon 1
1456q = p
1457md = ulong 0
1458ns = ulong 0
1459nrc = ulong 0
1460tall = ulong 0
1461rnd = ulong 0
1462while and and fits eq md 0 lt rnd 64 bound 100
1463rnd = add rnd 1
1464lname b q colon 1 nr
1465if not lfits b get nr 0 get nr 1
1466fits = false
1467break
1468end
1469if lt ns 4
1470lput ob add 24 mul ns 16 get nr 0 get nr 1
1471lput ob add 32 mul ns 16 get nr 2 get nr 3
1472ns = add ns 1
1473else
1474clipped = 1
1475end
1476x = get nr 4
1477if ge x colon
1478md = 3
1479else
1480cx = lc b x
1481did = false
1482if eq cx 43
1483sp = true
1484if lt add x 1 colon
1485if ne lc b add x 1 32
1486sp = false
1487end
1488end
1489if sp
1490q = add x 1
1491did = true
1492end
1493end
1494if not did
1495if eq cx 116
1496if lt add x 2 colon
1497if and eq lc b add x 1 111 eq lc b add x 2 32
1498md = 1
1499q = add x 3
1500did = true
1501end
1502end
1503end
1504end
1505if not did
1506if eq cx 64
1507md = 2
1508q = x
1509did = true
1510end
1511end
1512if not did
1513if eq get nr 5 1
1514md = 3
1515ts = x
1516else
1517fits = false
1518end
1519end
1520end
1521end
1522if and fits eq md 0
1523fits = false
1524end
1525rnd = 0
1526while and and fits eq md 1 lt rnd 64 bound 100
1527rnd = add rnd 1
1528lname b q colon 0 nr
1529a = get nr 0
1530e = get nr 1
1531if gt e a
1532if eq lc b a 64
1533a = add a 1
1534end
1535end
1536if not lfits b a e
1537fits = false
1538break
1539end
1540if levery b a e
1541tall = 1
1542else
1543if lt nrc 16
1544lput ob add 88 mul nrc 16 a e
1545lput ob add 96 mul nrc 16 get nr 2 get nr 3
1546nrc = add nrc 1
1547else
1548clipped = 1
1549end
1550end
1551x = get nr 4
1552if ge x colon
1553md = 3
1554else
1555plus = false
1556if eq lc b x 43
1557plus = true
1558if lt add x 1 colon
1559if ne lc b add x 1 32
1560plus = false
1561end
1562end
1563end
1564if plus
1565q = add x 1
1566else
1567fits = false
1568end
1569end
1570end
1571if and fits eq md 1
1572fits = false
1573end
1574rnd = 0
1575while and and fits eq md 2 lt rnd 64 bound 100
1576rnd = add rnd 1
1577while lt q colon bound 999999
1578c = lc b q
1579if not or lsp c eq c 43
1580break
1581end
1582q = add q 1
1583end
1584if ge q colon
1585md = 3
1586break
1587end
1588if ne lc b q 64
1589fits = false
1590break
1591end
1592j = add q 1
1593while lt j colon bound 999999
1594if lsp lc b j
1595break
1596end
1597j = add j 1
1598end
1599ltrim b add q 1 j nr 0
1600a = get nr 0
1601e = get nr 1
1602if not lfits b a e
1603fits = false
1604break
1605end
1606if levery b a e
1607tall = 1
1608else
1609if lt nrc 16
1610lput ob add 88 mul nrc 16 a e
1611nrc = add nrc 1
1612else
1613clipped = 1
1614end
1615end
1616q = j
1617end
1618if and fits eq md 2
1619fits = false
1620end
1621if not fits
1622for z ulong 320
1623put ob add 32 z uchar 0
1624end
1625ns = 1
1626nrc = 0
1627tall = 0
1628j = bst
1629while lt j en bound 999999
1630if lsp lc b j
1631break
1632end
1633j = add j 1
1634end
1635ltrim b bst j nr 0
1636a = get nr 0
1637e = get nr 1
1638if gt sub e a 1
1639if eq lc b sub e 1 58
1640e = sub e 1
1641end
1642end
1643if gt sub e a 64
1644e = lclip b a e 64
1645clipped = 1
1646end
1647lput ob 24 a e
1648ts = j
1649end
1650ltrim b ts en nr 0
1651a = get nr 0
1652e = get nr 1
1653if gt e a
1654if gt sub e a 32768
1655e = lclip b a e 32768
1656clipped = 1
1657end
1658end
1659lput ob 16 a e
1660put ob 9 uchar tall
1661put ob 10 uchar clipped
1662put ob 11 uchar ns
1663put ob 12 uchar nrc
1664ret 344
1665end
1666
1667note ---- who a post calls and which posts it cites (kind 5 op 3) ----
1668note "@name", "@all", "@#channel" and "#seq" in the text of a post. A mention counts only at a word start: the
1669note character before "@" or "#" must not be a name character, and the name must not run on into more name
1670note characters. Positions in these functions are places in the request: the text is the bytes t0..t1.
1671
1672note the character that starts at byte i, as a code point; 4294967295 at or after t1. A broken sequence gives its
1673note first byte
1674fn rune b view uchar 1048576 i ulong t1 ulong to ulong
1675if ge i t1
1676ret 4294967295
1677end
1678b0 = ulong get b i
1679if or lt b0 128 ge b0 248
1680ret b0
1681end
1682if lt b0 192
1683ret b0
1684end
1685need = ulong 1
1686cp = and b0 31
1687if ge b0 224
1688need = 2
1689cp = and b0 15
1690end
1691if ge b0 240
1692need = 3
1693cp = and b0 7
1694end
1695k = ulong 1
1696while le k need bound 8
1697if ge add i k t1
1698ret b0
1699end
1700c = ulong get b add i k
1701if ne and c 192 128
1702ret b0
1703end
1704cp = or shl cp 6 and c 63
1705k = add k 1
1706end
1707ret cp
1708end
1709
1710note the character that ends just before byte e; 4294967295 at the start of the text
1711fn runeb b view uchar 1048576 e ulong t0 ulong t1 ulong to ulong
1712if or le e t0 gt e t1
1713ret 4294967295
1714end
1715i = sub e 1
1716back = ulong 0
1717while gt i t0 bound 8
1718if ge back 3
1719break
1720end
1721if ne and ulong get b i 192 128
1722break
1723end
1724i = sub i 1
1725back = add back 1
1726end
1727ret rune b i t1
1728end
1729
1730note can this character be part of a name? Letters, digits, "_" and "-"; outside ASCII everything except the
1731note blocks that hold punctuation, symbols and emoji
1732fn namer cp ulong to bool
1733if eq cp 4294967295
1734ret false
1735end
1736if lt cp 128
1737if and ge cp 48 le cp 57
1738ret true
1739end
1740if and ge cp 65 le cp 90
1741ret true
1742end
1743if and ge cp 97 le cp 122
1744ret true
1745end
1746ret or eq cp 95 eq cp 45
1747end
1748if or or lt cp 192 eq cp 215 eq cp 247
1749ret false
1750end
1751if and ge cp 8192 le cp 11263
1752ret false
1753end
1754if and ge cp 12288 le cp 12351
1755ret false
1756end
1757if and ge cp 65072 le cp 65135
1758ret false
1759end
1760if or and ge cp 65280 le cp 65295 and ge cp 65306 le cp 65312
1761ret false
1762end
1763if or and ge cp 65339 le cp 65344 and ge cp 65371 le cp 65381
1764ret false
1765end
1766if and ge cp 126976 le cp 129791
1767ret false
1768end
1769ret true
1770end
1771
1772fn lowc c ulong to ulong
1773if and ge c 65 le c 90
1774ret add c 32
1775end
1776ret c
1777end
1778
1779note true when the ln bytes at na stand at byte pos of the text (ASCII letters without case) and no name
1780note character follows them
1781fn nameat b view uchar 1048576 pos ulong na ulong ln ulong t1 ulong to bool
1782if eq ln 0
1783ret false
1784end
1785if gt add pos ln t1
1786ret false
1787end
1788for k ulong ln
1789if ne lowc ulong get b add pos k lowc ulong get b add na k
1790ret false
1791end
1792end
1793ret not namer rune b add pos ln t1
1794end
1795
1796note "all" at byte pos, and no name character after it
1797fn allat b view uchar 1048576 pos ulong t1 ulong to bool
1798if gt add pos 3 t1
1799ret false
1800end
1801if ne lowc ulong get b pos 97
1802ret false
1803end
1804if ne lowc ulong get b add pos 1 108
1805ret false
1806end
1807if ne lowc ulong get b add pos 2 108
1808ret false
1809end
1810ret not namer rune b add pos 3 t1
1811end
1812
1813note a member called "<prefix>-<digits>" also answers to "@<prefix>": the length of that prefix, or 0
1814fn cprefix b view uchar 1048576 na ulong nl ulong to ulong
1815i = nl
1816while gt i 0 bound 999999
1817c = ulong get b add na sub i 1
1818if or lt c 48 gt c 57
1819break
1820end
1821i = sub i 1
1822end
1823if eq i nl
1824ret 0
1825end
1826if lt i 2
1827ret 0
1828end
1829if ne ulong get b add na sub i 1 45
1830ret 0
1831end
1832ret sub i 1
1833end
1834
1835note the length of a channel name at byte pos (after "@#"): a letter, then letters, digits and "-", 24 at most
1836fn chlen b view uchar 1048576 pos ulong t1 ulong to ulong
1837if ge pos t1
1838ret 0
1839end
1840f = lowc ulong get b pos
1841if or lt f 97 gt f 122
1842ret 0
1843end
1844j = pos
1845while lt j t1 bound 64
1846if ge sub j pos 25
1847break
1848end
1849c = lowc ulong get b j
1850okc = and ge c 97 le c 122
1851if and ge c 48 le c 57
1852okc = true
1853end
1854if eq c 45
1855okc = true
1856end
1857if not okc
1858break
1859end
1860j = add j 1
1861end
1862n = sub j pos
1863if gt n 24
1864ret 0
1865end
1866if namer rune b j t1
1867ret 0
1868end
1869ret n
1870end
1871
1872note part idx of the answer: where it starts in the text, its length, its kind (1 a member, 2 all, 3 a channel,
1873note 4 a cited post) and its value (the member's number, or the post's)
1874fn semit ob edit uchar 65544 idx ulong s ulong l ulong kind ulong v ulong to void
1875o = add 16 mul idx 16
1876put32 ob o s
1877put32 ob add o 4 l
1878put32 ob add o 8 kind
1879put32 ob add o 12 v
1880end
1881
1882note op 3: n bytes after the op header: a 16-bit count of member names (96 at most), each name as a 16-bit length
1883note and its bytes, then the text (900000 bytes at most). head is the highest post number that may be cited; 0
1884note turns citations off. The answer: a 16-bit count of parts, a byte that is 1 when the post has more than 128
1885note parts (the rest is not marked), then from byte 8 sixteen bytes a part (semit), in text order. Parts that
1886note start at one byte are kept or dropped together
1887fn spans b view uchar 1048576 n ulong head ulong ob edit uchar 65544 to ulong
1888noff = new ulong 96 0
1889nlen = new ulong 96 0
1890npre = new ulong 96 0
1891bad = ulong 18446744073709551615
1892if lt n 2
1893ret bad
1894end
1895nn = rd16 b 8
1896if gt nn 96
1897ret bad
1898end
1899lim = add 8 n
1900p = ulong 10
1901for k ulong nn
1902if gt add p 2 lim
1903ret bad
1904end
1905l = rd16 b p
1906p = add p 2
1907if gt add p l lim
1908ret bad
1909end
1910put noff k p
1911put nlen k l
1912put npre k cprefix b p l
1913p = add p l
1914end
1915t0 = p
1916t1 = lim
1917if gt sub t1 t0 900000
1918ret bad
1919end
1920cnt = ulong 0
1921clipped = ulong 0
1922i = t0
1923while lt i t1 bound 999999
1924c = ulong get b i
1925st = ulong 1
1926g = ulong 0
1927mark = or eq c 64 eq c 35
1928if mark
1929if namer runeb b i t0 t1
1930mark = false
1931end
1932end
1933if mark
1934if eq c 64
1935nxt = ulong 0
1936if lt add i 1 t1
1937nxt = ulong get b add i 1
1938end
1939if eq nxt 35
1940cl = chlen b add i 2 t1
1941if gt cl 0
1942semit ob cnt sub i t0 add cl 2 3 0
1943g = 1
1944st = add cl 2
1945end
1946else
1947for k ulong nn
1948if nameat b add i 1 get noff k get nlen k t1
1949semit ob add cnt g sub i t0 add get nlen k 1 1 k
1950g = add g 1
1951end
1952end
1953if eq g 0
1954if allat b add i 1 t1
1955semit ob add cnt g sub i t0 4 2 0
1956g = 1
1957end
1958end
1959k2 = ulong 0
1960while lt k2 nn bound 128
1961if gt g 0
1962break
1963end
1964pk = get npre k2
1965if gt pk 0
1966if nameat b add i 1 get noff k2 pk t1
1967j = k2
1968while lt j nn bound 128
1969if eq get npre j pk
1970if nameat b add i 1 get noff j pk t1
1971semit ob add cnt g sub i t0 add pk 1 1 j
1972g = add g 1
1973end
1974end
1975j = add j 1
1976end
1977end
1978end
1979k2 = add k2 1
1980end
1981end
1982else
1983if gt head 0
1984if ne runeb b i t0 t1 38
1985j = add i 1
1986v = ulong 0
1987while lt j t1 bound 16
1988if gt sub j i 9
1989break
1990end
1991d = ulong get b j
1992if or lt d 48 gt d 57
1993break
1994end
1995v = add mul v 10 sub d 48
1996j = add j 1
1997end
1998if gt j add i 1
1999if and ge v 1 le v head
2000if not namer rune b j t1
2001semit ob cnt sub i t0 sub j i 4 v
2002g = 1
2003st = sub j i
2004end
2005end
2006end
2007end
2008end
2009end
2010end
2011if gt g 0
2012if gt add cnt g 128
2013clipped = 1
2014break
2015end
2016cnt = add cnt g
2017end
2018i = add i st
2019end
2020put ob 8 uchar and cnt 255
2021put ob 9 uchar shr cnt 8
2022put ob 10 uchar clipped
2023for z ulong 5
2024put ob add 11 z uchar 0
2025end
2026ret add 8 mul cnt 16
2027end
2028
2029note ---- who a post addresses (kind 5 op 4) ----
2030note the members its header names, the members its text calls with "@name" or reaches through "@#channel", and
2031note whether it is to everyone. The sender is left out.
2032
2033note two runs of bytes of the request, equal when ASCII letters are compared without case
2034fn eqf b view uchar 1048576 x ulong xl ulong y ulong yl ulong to bool
2035if ne xl yl
2036ret false
2037end
2038for k ulong xl
2039if ne lowc ulong get b add x k lowc ulong get b add y k
2040ret false
2041end
2042end
2043ret true
2044end
2045
2046note "main", "all" and "here" are not channels anyone follows
2047fn chplain b view uchar 1048576 x ulong n ulong to bool
2048if eq n 3
2049ret and and eq lowc ulong get b x 97 eq lowc ulong get b add x 1 108 eq lowc ulong get b add x 2 108
2050end
2051if ne n 4
2052ret false
2053end
2054c0 = lowc ulong get b x
2055c1 = lowc ulong get b add x 1
2056c2 = lowc ulong get b add x 2
2057c3 = lowc ulong get b add x 3
2058if and and eq c0 109 eq c1 97 and eq c2 105 eq c3 110
2059ret true
2060end
2061ret and and eq c0 104 eq c1 101 and eq c2 114 eq c3 101
2062end
2063
2064note does a member follow the channel named by the xl bytes at x? cp is where the member's channel list starts: a
2065note count, then each channel as a 16-bit length and its bytes. An empty list is the role's default: everyone but a
2066note worker (role 0) follows every channel. "*" follows every channel
2067fn follows b view uchar 1048576 role ulong cp ulong x ulong xl ulong to bool
2068cc = ulong get b cp
2069if eq cc 0
2070ret eq role 0
2071end
2072p = add cp 1
2073for i ulong cc
2074l = rd16 b p
2075if eq l 1
2076if eq ulong get b add p 2 42
2077ret true
2078end
2079end
2080if eqf b add p 2 l x xl
2081ret true
2082end
2083p = add add p 2 l
2084end
2085ret false
2086end
2087
2088note op 4: n bytes after the op header: 16-bit counts of members (96 at most), parts (128) and header recipients
2089note (16); a byte that is 1 when the header says all; a byte 0; the sender as a 32-bit member number; a byte a
2090note member (0 for a worker); the header's recipients, 32 bits each; the parts as fn spans gives them; for each
2091note member its channel list (follows); then the text of the post. A number that is not a member is 4294967295.
2092note The answer: a byte that is 1 when the post is to everyone, a count, then that many member numbers
2093fn addr b view uchar 1048576 n ulong ob edit uchar 65544 to ulong
2094flag = new ulong 96 0
2095coff = new ulong 96 0
2096bad = ulong 18446744073709551615
2097if lt n 12
2098ret bad
2099end
2100lim = add 8 n
2101mem = rd16 b 8
2102nsp = rd16 b 10
2103nh = rd16 b 12
2104tall = ulong get b 14
2105sender = rd32 b 16
2106if or gt mem 96 or gt nsp 128 gt nh 16
2107ret bad
2108end
2109roles = ulong 20
2110hto = add roles mem
2111sp = add hto mul nh 4
2112p = add sp mul nsp 16
2113if gt p lim
2114ret bad
2115end
2116for m ulong mem
2117if ge p lim
2118ret bad
2119end
2120put coff m p
2121cc = ulong get b p
2122if gt cc 16
2123ret bad
2124end
2125p = add p 1
2126for c ulong cc
2127if gt add p 2 lim
2128ret bad
2129end
2130p = add add p 2 rd16 b p
2131if gt p lim
2132ret bad
2133end
2134end
2135end
2136t0 = p
2137tn = sub lim t0
2138for i ulong nh
2139v = rd32 b add hto mul i 4
2140if lt v mem
2141put flag v 1
2142end
2143end
2144for s ulong nsp
2145o = add sp mul s 16
2146start = rd32 b o
2147ln = rd32 b add o 4
2148kind = rd32 b add o 8
2149v = rd32 b add o 12
2150if eq kind 1
2151if lt v mem
2152put flag v 1
2153end
2154end
2155if eq kind 2
2156tall = 1
2157end
2158if eq kind 3
2159if and gt ln 2 le add start ln tn
2160cx = add add t0 start 2
2161cl = sub ln 2
2162if not chplain b cx cl
2163for m ulong mem
2164if follows b ulong get b add roles m get coff m cx cl
2165put flag m 1
2166end
2167end
2168end
2169end
2170end
2171end
2172if lt sender mem
2173put flag sender 0
2174end
2175cnt = ulong 0
2176for m ulong mem
2177if eq get flag m 1
2178put ob add 10 cnt uchar m
2179cnt = add cnt 1
2180end
2181end
2182if ne tall 0
2183tall = 1
2184end
2185put ob 8 uchar tall
2186put ob 9 uchar cnt
2187ret add 2 cnt
2188end
2189
2190note kind 5: op and a number as two 32-bit values, then the bytes of the text (at most 70000 for op 1 and 2).
2191note op 1: a board line (fn lline). op 2: a time at byte `number` (fn ltime); the answer is 16 bytes: 0 1 when it
2192note is a time, 1 1 when it names its zone, 4 the first byte after it (32-bit), 8 the seconds since 1970 plus 100000
2193note (64-bit). op 3: who a post calls and what it cites (fn spans; the number is the highest post that may be
2194note cited). op 4: who a post addresses (fn addr). Returns the reply length, or 18446744073709551615 for a malformed body or an unknown op
2195fn board ib view uchar 1048576 nb ulong ob edit uchar 65544 to ulong
2196tm = new ulong 16 0
2197bad = ulong 18446744073709551615
2198if lt nb 8
2199ret bad
2200end
2201op = rd32 ib 0
2202arg = rd32 ib 4
2203n = sub nb 8
2204if eq op 3
2205ret spans ib n arg ob
2206end
2207if eq op 4
2208ret addr ib n ob
2209end
2210if gt n 70000
2211ret bad
2212end
2213if eq op 1
2214ret lline ib n ob
2215end
2216if eq op 2
2217for z ulong 16
2218put ob add 8 z uchar 0
2219end
2220if eq ltime ib arg n tm 1
2221put ob 8 uchar 1
2222put ob 9 uchar get tm 3
2223put32 ob 12 get tm 2
2224put32 ob 16 get tm 1
2225put32 ob 20 shr get tm 1 32
2226end
2227ret 16
2228end
2229ret bad
2230end
2231
2232note ---- a whole plain-text board: its members and who each post is from and to (kind 6) ----
2233note The host reads each line (kind 5 op 1) and sends what the lines name: for each line a byte that is 1 for a
2234note post, a byte that is 1 when it was cut, the number of senders (4 at most) and of recipients (16 at most), then
2235note each sender and each recipient as a 16-bit length and the name, a 16-bit length and the bracket after it.
2236note Senders and recipients become members. Two spellings are one member when they share a key (no leading "@",
2237note ASCII lower case, each run of spaces one "-"); when one stands in the brackets of one agent only and is used
2238note as a sender or recipient elsewhere ("merge-desk (omp-queen-spark)"); or when it is that agent's name without
2239note its lane ("Worker" for "lane/Worker") and exactly one sender posts under a name that ends that way.
2240note Limits: 400 lines, 192 spellings, 96 members, 8 comma-separated items in a bracket.
2241global a_keys uchar 12288
2242global a_kl ulong 192
2243global a_sref ulong 192
2244global a_own ulong 192
2245global a_can ulong 192
2246global a_mem ulong 192
2247global a_lo ulong 400
2248global a_kb uchar 72
2249global a_e ulong 8
2250global a_ds ulong 96
2251
2252note entry k of side (0 senders, 1 recipients) of the line record at p. a_e: 0 and 1 the name, 2 and 3 the bracket
2253fn aent b view uchar 1048576 p ulong side ulong k ulong to void
2254skip = k
2255if eq side 1
2256skip = add k ulong get b add p 2
2257end
2258q = add p 4
2259for i ulong skip
2260q = add add q 2 rd16 b q
2261q = add add q 2 rd16 b q
2262end
2263nl = rd16 b q
2264put a_e 0 add q 2
2265put a_e 1 nl
2266q = add add q 2 nl
2267put a_e 2 add q 2
2268put a_e 3 rd16 b q
2269end
2270
2271note the key of the n bytes at off into a_kb; its length, or 65 when it is over 64 bytes (no member has such a key)
2272fn akey b view uchar 1048576 off ulong n ulong to ulong
2273a = off
2274e = add off n
2275while lt a e bound 999999
2276if not lsp ulong get b a
2277break
2278end
2279a = add a 1
2280end
2281while gt e a bound 999999
2282if not lsp ulong get b sub e 1
2283break
2284end
2285e = sub e 1
2286end
2287if lt a e
2288if eq ulong get b a 64
2289a = add a 1
2290end
2291end
2292kn = ulong 0
2293gap = false
2294while lt a e bound 999999
2295c = ulong get b a
2296if lsp c
2297gap = true
2298else
2299if and gap gt kn 0
2300if ge kn 64
2301ret 65
2302end
2303put a_kb kn uchar 45
2304kn = add kn 1
2305end
2306gap = false
2307if ge kn 64
2308ret 65
2309end
2310put a_kb kn uchar lowc c
2311kn = add kn 1
2312end
2313a = add a 1
2314end
2315ret kn
2316end
2317
2318note where the key in a_kb (kn bytes) stands among the first nk keys, or 1000
2319fn afind kn ulong nk ulong to ulong
2320if gt kn 64
2321ret 1000
2322end
2323i = ulong 0
2324while lt i nk bound 256
2325if eq get a_kl i kn
2326j = ulong 0
2327while lt j kn bound 128
2328if ne ulong get a_keys add mul i 64 j ulong get a_kb j
2329break
2330end
2331j = add j 1
2332end
2333if eq j kn
2334ret i
2335end
2336end
2337i = add i 1
2338end
2339ret 1000
2340end
2341
2342note "L1122" or "L1124/L1125": a ledger number, never another name of the agent
2343fn aledger b view uchar 1048576 off ulong n ulong to bool
2344i = ulong 0
2345parts = ulong 0
2346while lt i n bound 16
2347if ge parts 8
2348break
2349end
2350c = ulong get b add off i
2351if and ne c 76 ne c 108
2352ret false
2353end
2354j = add i 1
2355while lt j n bound 16
2356if gt sub j i 9
2357break
2358end
2359d = ulong get b add off j
2360if or lt d 48 gt d 57
2361break
2362end
2363j = add j 1
2364end
2365if eq j add i 1
2366ret false
2367end
2368parts = add parts 1
2369if eq j n
2370ret true
2371end
2372if ne ulong get b add off j 47
2373ret false
2374end
2375i = add j 1
2376end
2377ret false
2378end
2379
2380note the main spelling of the set that holds spelling s
2381fn aroot s ulong nk ulong to ulong
2382i = s
2383hops = ulong 0
2384while lt i nk bound 256
2385if eq get a_can i i
2386break
2387end
2388if ge hops 192
2389break
2390end
2391i = get a_can i
2392hops = add hops 1
2393end
2394ret i
2395end
2396
2397note joins the sets that hold x0 and y0; the earlier spelling stays the main one
2398fn ajoin x0 ulong y0 ulong nk ulong to void
2399x = aroot x0 nk
2400y = aroot y0 nk
2401if or ge x nk ge y nk
2402ret
2403end
2404if eq x y
2405ret
2406end
2407if lt x y
2408put a_can y x
2409else
2410put a_can x y
2411end
2412end
2413
2414note is key f the key s with a lane in front: "lane/s"?
2415fn alane f ulong s ulong to bool
2416fl = get a_kl f
2417sl = get a_kl s
2418if eq sl 0
2419ret false
2420end
2421if lt fl add sl 2
2422ret false
2423end
2424p = sub fl sl
2425if ne ulong get a_keys add mul f 64 sub p 1 47
2426ret false
2427end
2428i = ulong 0
2429while lt i sl bound 128
2430if ne ulong get a_keys add add mul f 64 p i ulong get a_keys add mul s 64 i
2431ret false
2432end
2433i = add i 1
2434end
2435ret true
2436end
2437
2438note the member that the name of the current entry (a_e) belongs to, or 1000
2439fn amember b view uchar 1048576 nk ulong to ulong
2440f = afind akey b get a_e 0 get a_e 1 nk
2441if ge f 1000
2442ret 1000
2443end
2444ret get a_mem f
2445end
2446
2447note kind 6: a 32-bit count of lines, then the line records. The answer: 16-bit counts of members, of joined
2448note spellings and of posts; from byte 8 four 32-bit counts: lines that are not posts, posts that were cut,
2449note agents beyond 96 members, names met after the 192 spellings were full; from byte 24 eight bytes a member:
2450note the line (16-bit), side and entry of its newest spelling, then the line (65535: none) and entry of the newest
2451note bracket after it as a sender; six bytes a joined spelling: its member, 0, then line, side and entry; then a
2452note post each: its sender (255: none), how many others signed and how many members its header names, then
2453note those members. Returns the reply length, or 18446744073709551615 for a malformed body
2454fn asm b view uchar 1048576 n ulong ob edit uchar 65544 to ulong
2455wl = new ulong 8 0
2456to2 = new ulong 24 0
2457bad = ulong 18446744073709551615
2458if lt n 4
2459ret bad
2460end
2461nl = rd32 b 0
2462if gt nl 400
2463ret bad
2464end
2465p = ulong 4
2466for li ulong nl
2467if gt add p 4 n
2468ret bad
2469end
2470put a_lo li p
2471ns = ulong get b add p 2
2472nr = ulong get b add p 3
2473if or gt ns 4 gt nr 16
2474ret bad
2475end
2476p = add p 4
2477for k ulong mul 2 add ns nr
2478if gt add p 2 n
2479ret bad
2480end
2481p = add add p 2 rd16 b p
2482if gt p n
2483ret bad
2484end
2485end
2486end
2487nk = ulong 0
2488sdrop = ulong 0
2489skeys = ulong 0
2490for pz ulong 2
2491for lr ulong nl
2492lq = sub sub nl 1 lr
2493p = get a_lo lq
2494if eq ulong get b p 1
2495cnt = ulong get b add p 2
2496if eq pz 1
2497cnt = ulong get b add p 3
2498end
2499for k ulong cnt
2500aent b p pz k
2501kn = akey b get a_e 0 get a_e 1
2502if and gt kn 0 le kn 64
2503if ge afind kn nk 1000
2504if lt nk 192
2505for j ulong kn
2506put a_keys add mul nk 64 j get a_kb j
2507end
2508put a_kl nk kn
2509put a_sref nk or or shl lq 16 shl pz 8 k
2510nk = add nk 1
2511else
2512sdrop = add sdrop 1
2513end
2514end
2515end
2516end
2517end
2518end
2519if eq pz 0
2520skeys = nk
2521end
2522end
2523for c ulong nk
2524put a_own c 1000
2525put a_can c c
2526put a_mem c 1000
2527end
2528for li ulong nl
2529p = get a_lo li
2530if eq ulong get b p 1
2531for side ulong 2
2532cnt = ulong get b add p 2
2533if eq side 1
2534cnt = ulong get b add p 3
2535end
2536for k ulong cnt
2537aent b p side k
2538d0 = get a_e 2
2539dn = get a_e 3
2540outer = afind akey b get a_e 0 get a_e 1 nk
2541a = ulong 0
2542items = ulong 0
2543while le a dn bound 1024
2544if ge outer 1000
2545break
2546end
2547if ge items 8
2548break
2549end
2550e = a
2551while lt e dn bound 999999
2552if eq ulong get b add d0 e 44
2553break
2554end
2555e = add e 1
2556end
2557x = a
2558y = e
2559while lt x y bound 999999
2560if not lsp ulong get b add d0 x
2561break
2562end
2563x = add x 1
2564end
2565while gt y x bound 999999
2566if not lsp ulong get b add d0 sub y 1
2567break
2568end
2569y = sub y 1
2570end
2571if lt x y
2572if not aledger b add d0 x sub y x
2573inner = afind akey b add d0 x sub y x nk
2574if lt inner 1000
2575if ne inner outer
2576if eq get a_own inner 1000
2577put a_own inner outer
2578else
2579if ne get a_own inner outer
2580put a_own inner 2000
2581end
2582end
2583end
2584end
2585end
2586end
2587items = add items 1
2588a = add e 1
2589end
2590end
2591end
2592end
2593end
2594for c ulong nk
2595if lt get a_own c 1000
2596ajoin get a_own c c nk
2597end
2598end
2599for c ulong nk
2600slash = false
2601for j ulong get a_kl c
2602if eq ulong get a_keys add mul c 64 j 47
2603slash = true
2604end
2605end
2606if not slash
2607mt1 = ulong 0
2608matches = ulong 0
2609for k ulong skeys
2610if ne k c
2611if alane k c
2612mt1 = k
2613matches = add matches 1
2614end
2615end
2616end
2617if eq matches 1
2618ajoin mt1 c nk
2619end
2620end
2621end
2622nm = ulong 0
2623mdrop = ulong 0
2624for c ulong nk
2625if eq aroot c nk c
2626if lt nm 96
2627put a_mem c nm
2628r = get a_sref c
2629o = add 32 mul nm 8
2630put ob o uchar and shr r 16 255
2631put ob add o 1 uchar shr r 24
2632put ob add o 2 uchar and shr r 8 255
2633put ob add o 3 uchar and r 255
2634put ob add o 4 uchar 255
2635put ob add o 5 uchar 255
2636put ob add o 6 uchar 0
2637put ob add o 7 uchar 0
2638put a_ds nm 0
2639nm = add nm 1
2640else
2641mdrop = add mdrop 1
2642end
2643end
2644end
2645o = add 32 mul nm 8
2646nalso = ulong 0
2647for c ulong nk
2648rt = aroot c nk
2649if ne rt c
2650if lt rt nk
2651if lt get a_mem rt 1000
2652put a_mem c get a_mem rt
2653r = get a_sref c
2654put ob o uchar get a_mem rt
2655put ob add o 1 uchar 0
2656put ob add o 2 uchar and shr r 16 255
2657put ob add o 3 uchar shr r 24
2658put ob add o 4 uchar and shr r 8 255
2659put ob add o 5 uchar and r 255
2660o = add o 6
2661nalso = add nalso 1
2662end
2663end
2664end
2665end
2666skipped = ulong 0
2667clipped = ulong 0
2668nposts = ulong 0
2669for li ulong nl
2670p = get a_lo li
2671if ne ulong get b p 1
2672skipped = add skipped 1
2673else
2674if eq ulong get b add p 1 1
2675clipped = add clipped 1
2676end
2677sender = ulong 1000
2678nw = ulong 0
2679ns = ulong get b add p 2
2680for k ulong ns
2681aent b p 0 k
2682m = amember b nk
2683if eq k 0
2684sender = m
2685else
2686if and lt m 1000 ne m sender
2687dup = false
2688for j ulong nw
2689if eq get wl j m
2690dup = true
2691end
2692end
2693if not dup
2694put wl nw m
2695nw = add nw 1
2696end
2697end
2698end
2699end
2700nt = ulong 0
2701nr = ulong get b add p 3
2702for k ulong nr
2703aent b p 1 k
2704m = amember b nk
2705if and lt m 1000 ne m sender
2706dup = false
2707for j ulong nt
2708if eq get to2 j m
2709dup = true
2710end
2711end
2712if not dup
2713put to2 nt m
2714nt = add nt 1
2715end
2716end
2717end
2718sb = ulong 255
2719if lt sender 1000
2720sb = sender
2721end
2722put ob o uchar sb
2723put ob add o 1 uchar nw
2724put ob add o 2 uchar nt
2725o = add o 3
2726for j ulong nw
2727put ob o uchar get wl j
2728o = add o 1
2729end
2730for j ulong nt
2731put ob o uchar get to2 j
2732o = add o 1
2733end
2734nposts = add nposts 1
2735end
2736end
2737for lr ulong nl
2738lq = sub sub nl 1 lr
2739p = get a_lo lq
2740if eq ulong get b p 1
2741ns = ulong get b add p 2
2742for k ulong ns
2743aent b p 0 k
2744dn = get a_e 3
2745m = amember b nk
2746if and lt m 1000 gt dn 0
2747if eq get a_ds m 0
2748put a_ds m 1
2749mo = add 36 mul m 8
2750put ob mo uchar and lq 255
2751put ob add mo 1 uchar shr lq 8
2752put ob add mo 2 uchar k
2753end
2754end
2755end
2756end
2757end
2758put ob 8 uchar and nm 255
2759put ob 9 uchar shr nm 8
2760put ob 10 uchar and nalso 255
2761put ob 11 uchar shr nalso 8
2762put ob 12 uchar and nposts 255
2763put ob 13 uchar shr nposts 8
2764put ob 14 uchar 0
2765put ob 15 uchar 0
2766put32 ob 16 skipped
2767put32 ob 20 clipped
2768put32 ob 24 mdrop
2769put32 ob 28 sdrop
2770ret sub o 8
2771end
2772
2773note ---- the state of a swarm's members, and the graph (kind 7) ----
2774note A request is an op and a count c as two 32-bit values, then c numbers of 64 bits, then bytes when the op has a
2775note text. Every number, in and out, is the value plus 1099511627776, so that -1 ("not known", "none") travels as
2776note an unsigned number. Codes: role 0 worker, 1 coordinator, 2 helper; turn 0 not known, 1 running, 2 ended; wake
2777note 2 parked for mentions, 3 for any post; state 1 working, 2 waiting, 3 stopped; attention 0 none, 1 stuck,
2778note 2 error, 3 quota; group 1 needs you, 2 working, 3 waiting on someone, 4 parked; step 1 planner, 2 result,
2779note 3 user, 4 error; status 1 done, 2 error.
2780const k_b ulong 1099511627776
2781text q_words "resource_exhausted|quota|rate limit|rate-limit|ratelimit|exhausted your capacity|"
2782text q_roles "coordinatorworkernarratormonitor"
2783global d_role ulong 96
2784global d_kill ulong 96
2785global d_wake ulong 96
2786global d_turn ulong 96
2787global d_lr ulong 96
2788global d_lm ulong 96
2789global d_fail ulong 96
2790global d_own ulong 96
2791global d_no ulong 96
2792global d_nl ulong 96
2793global d_cnt ulong 96
2794global d_last ulong 96
2795global d_state ulong 96
2796global d_att ulong 96
2797global d_won ulong 96
2798global d_grp ulong 96
2799global d_key ulong 96
2800global d_ord ulong 96
2801global d_po ulong 400
2802global d_cell ulong 18432
2803global d_nm ulong 96
2804
2805note number k of the request
2806fn gi b view uchar 1048576 k ulong to ulong
2807o = add 8 mul k 8
2808ret or rd32 b o shl rd32 b add o 4 32
2809end
2810
2811note number k of the request as a count; a negative number gives a value over every limit
2812fn gu b view uchar 1048576 k ulong to ulong
2813v = gi b k
2814if lt v k_b
2815ret 4294967295
2816end
2817ret sub v k_b
2818end
2819
2820note number k of the answer
2821fn po ob edit uchar 65544 k ulong v ulong to void
2822o = add 8 mul k 8
2823put32 ob o v
2824put32 ob add o 4 shr v 32
2825end
2826
2827note a member's state is worked out, never taken from what it says about itself. With a roster: stopped when it
2828note was killed; else waiting when its own run has ended and working when it is mid-turn; with no transcript to
2829note go on, waiting when it is parked. With no roster: working when it posted within 900 seconds (age, -1 never)
2830fn swstate killed ulong turn ulong wake ulong roster ulong age ulong to ulong
2831if eq killed add k_b 1
2832ret 3
2833end
2834if ne roster add k_b 1
2835if and ge age k_b le age add k_b 900
2836ret 1
2837end
2838ret 2
2839end
2840if eq turn add k_b 2
2841ret 2
2842end
2843if eq turn add k_b 1
2844ret 1
2845end
2846if or eq wake add k_b 2 eq wake add k_b 3
2847ret 2
2848end
2849ret 1
2850end
2851
2852note what needs a look: a failed newest step, or a member that should be working and has not moved for more
2853note than 600 seconds (idle -1: not known)
2854fn swatt state ulong failure ulong idle ulong to ulong
2855if eq state add k_b 3
2856ret 0
2857end
2858if eq failure add k_b 2
2859ret 2
2860end
2861if eq failure add k_b 3
2862ret 3
2863end
2864if and eq state add k_b 1 gt idle add k_b 600
2865ret 1
2866end
2867ret 0
2868end
2869
2870note the Overview group of one member (asks and won: 1 when it asks the owner, when it waits on someone)
2871fn swgroup state ulong att ulong asks ulong won ulong to ulong
2872if eq state add k_b 3
2873ret 4
2874end
2875if or ne att k_b ne asks k_b
2876ret 1
2877end
2878if eq state add k_b 1
2879ret 2
2880end
2881if ne won k_b
2882ret 3
2883end
2884ret 4
2885end
2886
2887note is this post the first row of a visual group? Posts by one sender, next to each other and at most 600
2888note seconds apart, are one group
2889fn swstarts ps ulong pt ulong s ulong t ulong to ulong
2890if or lt s k_b ne s ps
2891ret 1
2892end
2893if or le t k_b le pt k_b
2894ret 0
2895end
2896if or gt t add pt 600 lt t pt
2897ret 1
2898end
2899ret 0
2900end
2901
2902note the n bytes at off, ASCII letters without case, against the wn bytes at w0 of q_roles
2903fn eqrole b view uchar 1048576 off ulong n ulong w0 ulong wn ulong to bool
2904if ne n wn
2905ret false
2906end
2907i = ulong 0
2908while lt i n bound 64
2909if ne lowc ulong get b add off i ulong get q_roles add w0 i
2910ret false
2911end
2912i = add i 1
2913end
2914ret true
2915end
2916
2917note the order of members: by key (small first), the coordinator before the others, then by name (ASCII letters
2918note without case, the shorter first). n members; keys in d_key, roles in d_role, names at d_no, d_nl; order to d_ord
2919fn sworder b view uchar 1048576 n ulong to void
2920cntd = ulong 0
2921for i ulong n
2922pos = cntd
2923while gt pos 0 bound 128
2924o = get d_ord sub pos 1
2925after = false
2926if ne get d_key o get d_key i
2927after = gt get d_key o get d_key i
2928else
2929co = eq get d_role o 1
2930ci = eq get d_role i 1
2931dif = false
2932if co
2933if not ci
2934dif = true
2935end
2936else
2937if ci
2938dif = true
2939end
2940end
2941if dif
2942after = ci
2943else
2944x = get d_no o
2945xl = get d_nl o
2946y = get d_no i
2947yl = get d_nl i
2948k = ulong 0
2949cmp = ulong 1
2950while lt k xl bound 999999
2951if ge k yl
2952break
2953end
2954cx = lowc ulong get b add x k
2955cy = lowc ulong get b add y k
2956if ne cx cy
2957cmp = 0
2958if gt cx cy
2959cmp = 2
2960end
2961break
2962end
2963k = add k 1
2964end
2965if eq cmp 1
2966if gt xl yl
2967cmp = 2
2968end
2969end
2970after = eq cmp 2
2971end
2972end
2973if not after
2974break
2975end
2976put d_ord pos o
2977pos = sub pos 1
2978end
2979put d_ord pos i
2980cntd = add cntd 1
2981end
2982end
2983
2984note op 8: has the member's run stopped? Its newest steps, oldest first: four counts (they must be equal), then
2985note the kinds, the statuses, for a planner step how many tools it called, and which of them was the wait for
2986note posts (-1 none). Ended (2) when the newest planner step called no tool and nothing came after it, or when it
2987note called the wait and that call's result is done; anything newer means mid-turn (1). 0: not known
2988fn swturn b view uchar 1048576 c ulong to ulong
2989if lt c 4
2990ret 0
2991end
2992n = gu b 0
2993if gt n 100000
2994ret 0
2995end
2996if or eq n 0 or ne gi b 1 gi b 0 or ne gi b 2 gi b 0 ne gi b 3 gi b 0
2997ret 0
2998end
2999if ne c add 4 mul n 4
3000ret 0
3001end
3002kd = ulong 4
3003sx = add 4 n
3004tl = add 4 mul n 2
3005wt2 = add 4 mul n 3
3006i = n
3007results = ulong 0
3008looked = ulong 0
3009while gt i 0 bound 512
3010if ge looked 256
3011break
3012end
3013if ne gi b add kd sub i 1 add k_b 2
3014break
3015end
3016results = add results 1
3017i = sub i 1
3018looked = add looked 1
3019end
3020if or eq i 0 ge looked 256
3021ret 1
3022end
3023i = sub i 1
3024if ne gi b add kd i add k_b 1
3025ret 1
3026end
3027tools = gi b add tl i
3028if le tools k_b
3029if eq results 0
3030ret 2
3031end
3032ret 1
3033end
3034wait = gi b add wt2 i
3035if or lt wait k_b ge wait tools
3036ret 1
3037end
3038if lt results sub tools k_b
3039ret 1
3040end
3041if eq gi b add sx add add i 1 sub wait k_b add k_b 1
3042ret 2
3043end
3044ret 1
3045end
3046
3047note op 9: the step that says what the member is doing now: the newest planner step of the current turn that has
3048note a label. Three counts (equal), then kinds, waits and labels. A turn starts after user input or after a wait
3049note for posts. The answer is the step's place, or -1
3050fn swact b view uchar 1048576 c ulong to ulong
3051none = sub k_b 1
3052if lt c 3
3053ret none
3054end
3055n = gu b 0
3056if gt n 100000
3057ret none
3058end
3059if or ne gi b 1 gi b 0 ne gi b 2 gi b 0
3060ret none
3061end
3062if ne c add 3 mul n 3
3063ret none
3064end
3065i = n
3066looked = ulong 0
3067while gt i 0 bound 512
3068if ge looked 256
3069break
3070end
3071kk = gi b add 3 sub i 1
3072if eq kk add k_b 3
3073ret none
3074end
3075if eq kk add k_b 1
3076if ge gi b add add 3 n sub i 1 k_b
3077ret none
3078end
3079if eq gi b add add 3 mul n 2 sub i 1 add k_b 1
3080ret add k_b sub i 1
3081end
3082end
3083i = sub i 1
3084looked = add looked 1
3085end
3086ret none
3087end
3088
3089note op 10: does an error text (t0..t1) say the member ran out of quota or was rate limited? One of the words of
3090note q_words in its first 32768 places, or "429" standing alone
3091fn swquota b view uchar 1048576 t0 ulong t1 ulong to ulong
3092tn = sub t1 t0
3093w0 = ulong 0
3094wi = ulong 0
3095while lt wi len q_words bound 256
3096if eq ulong get q_words wi 124
3097wn = sub wi w0
3098if and gt wn 0 ge tn wn
3099i = ulong 0
3100while le add i wn tn bound 40000
3101if ge i 32768
3102break
3103end
3104k = ulong 0
3105while lt k wn bound 64
3106if ne lowc ulong get b add add t0 i k ulong get q_words add w0 k
3107break
3108end
3109k = add k 1
3110end
3111if eq k wn
3112ret 1
3113end
3114i = add i 1
3115end
3116end
3117w0 = add wi 1
3118end
3119wi = add wi 1
3120end
3121i = ulong 0
3122while le add i 3 tn bound 40000
3123if ge i 32768
3124break
3125end
3126p = add t0 i
3127if and and eq ulong get b p 52 eq ulong get b add p 1 50 eq ulong get b add p 2 57
3128if not namer runeb b p t0 t1
3129if not namer rune b add p 3 t1
3130ret 1
3131end
3132end
3133end
3134i = add i 1
3135end
3136ret 0
3137end
3138
3139note op 11: the role code of a roster member from its role (rl bytes at ro) and its name (nl bytes at no). The
3140note coordinator leads; "narrator" and "monitor", also with "-<digits>" after, are helpers by name; so is any role
3141note other than "worker"
3142fn swrole b view uchar 1048576 ro ulong rl ulong no ulong nl ulong to ulong
3143if eqrole b ro rl 0 11
3144ret 1
3145end
3146plain = eq rl 0
3147if eqrole b ro rl 11 6
3148plain = true
3149end
3150if not plain
3151ret 2
3152end
3153bl = nl
3154pf = cprefix b no nl
3155if gt pf 0
3156bl = pf
3157end
3158if eqrole b no bl 17 8
3159ret 2
3160end
3161if eqrole b no bl 25 7
3162ret 2
3163end
3164ret 0
3165end
3166
3167note op 16: everything the views show about the members. Numbers: 0 1 when there is a roster, 1 now (seconds
3168note since 1970), 2 the members n (96 at most), 3 the posts p (400 at most); nine a member: role, killed, wake,
3169note turn, the last post it read (-1 not known), its last own movement (0 not known), failure, 1 when it is the
3170note person using Hop, the length of its name; then a post each: its number, its time (0 not known), its sender
3171note (-1: not a member), how many others signed and who, how many members it addresses by name and who. The
3172note names follow as bytes. The answer: 0 the newest post number, 1 to 3 the members working, waiting and stopped
3173note (helpers are not counted); a number a post: 1 when it starts a visual group; then eleven lists of n: posts
3174note by each member, its newest post (-1 none), state, attention, idle seconds (-1), unread posts (-1), 1 for a
3175note helper, who it waits on (-1), the post that says so, the post that asks the owner (0 none), its group; then
3176note the Overview order and the Members order. Returns the count of numbers, or 0 for a malformed request
3177fn swderive b view uchar 1048576 c ulong nbytes ulong ob edit uchar 65544 to ulong
3178if lt c 4
3179ret 0
3180end
3181roster = gi b 0
3182now = gi b 1
3183n = gu b 2
3184p = gu b 3
3185if or gt n 96 gt p 400
3186ret 0
3187end
3188q = add 4 mul n 9
3189if gt q c
3190ret 0
3191end
3192no = add 8 mul c 8
3193for m ulong n
3194o = add 4 mul m 9
3195put d_role m gu b o
3196put d_kill m gi b add o 1
3197put d_wake m gi b add o 2
3198put d_turn m gi b add o 3
3199put d_lr m gi b add o 4
3200put d_lm m gi b add o 5
3201put d_fail m gi b add o 6
3202put d_own m gi b add o 7
3203l = gu b add o 8
3204if gt l 1048576
3205ret 0
3206end
3207put d_no m no
3208put d_nl m l
3209no = add no l
3210put d_cnt m 0
3211put d_last m 1000
3212end
3213if gt no add add 8 mul c 8 nbytes
3214ret 0
3215end
3216for i ulong p
3217if gt add q 4 c
3218ret 0
3219end
3220put d_po i q
3221nw = gu b add q 3
3222if gt nw 4
3223ret 0
3224end
3225if gt add add q 5 nw c
3226ret 0
3227end
3228nd = gu b add add q 4 nw
3229if gt nd 96
3230ret 0
3231end
3232q = add add add q 5 nw nd
3233if gt q c
3234ret 0
3235end
3236end
3237head = k_b
3238if gt p 0
3239head = gi b get d_po sub p 1
3240end
3241po ob 0 head
3242for i ulong p
3243q = get d_po i
3244s = gi b add q 2
3245sg = ulong 1
3246if gt i 0
3247pq = get d_po sub i 1
3248sg = swstarts gi b add pq 2 gi b add pq 1 s gi b add q 1
3249end
3250po ob add 4 i add k_b sg
3251if and ge s k_b lt s add k_b n
3252sm = sub s k_b
3253put d_cnt sm add get d_cnt sm 1
3254put d_last sm i
3255end
3256nw = gu b add q 3
3257for k ulong nw
3258w = gi b add add q 4 k
3259if ne w s
3260if and ge w k_b lt w add k_b n
3261wm = sub w k_b
3262put d_cnt wm add get d_cnt wm 1
3263put d_last wm i
3264end
3265end
3266end
3267end
3268base = add 4 p
3269for m ulong n
3270last = get d_last m
3271age = sub k_b 1
3272if lt last 1000
3273pt = gi b add get d_po last 1
3274if gt pt k_b
3275age = k_b
3276if gt now pt
3277age = add k_b sub now pt
3278end
3279end
3280end
3281st = swstate get d_kill m get d_turn m get d_wake m roster age
3282idle = sub k_b 1
3283if eq roster add k_b 1
3284lm = get d_lm m
3285if gt lm k_b
3286idle = k_b
3287if gt now lm
3288idle = add k_b sub now lm
3289end
3290end
3291end
3292put d_state m st
3293put d_att m swatt add k_b st get d_fail m idle
3294unr = sub k_b 1
3295if eq roster add k_b 1
3296lr = get d_lr m
3297if ge lr k_b
3298unr = k_b
3299if gt head lr
3300unr = add k_b sub head lr
3301end
3302end
3303end
3304po ob add base m add k_b get d_cnt m
3305lp = sub k_b 1
3306if lt last 1000
3307lp = add k_b last
3308end
3309po ob add add base n m lp
3310po ob add add base mul n 2 m add k_b st
3311po ob add add base mul n 3 m add k_b get d_att m
3312po ob add add base mul n 4 m idle
3313po ob add add base mul n 5 m unr
3314hp = ulong 0
3315if eq get d_role m 2
3316hp = 1
3317end
3318po ob add add base mul n 6 m add k_b hp
3319end
3320working = ulong 0
3321waiting = ulong 0
3322stopped = ulong 0
3323for m ulong n
3324on = ulong 1000
3325seq = k_b
3326you = k_b
3327last = get d_last m
3328if and lt last 1000 ne get d_state m 3
3329q = get d_po last
3330nw = gu b add q 3
3331nd = gu b add add q 4 nw
3332t0 = add add q 5 nw
3333if gt nd 0
3334answered = false
3335j = add last 1
3336while lt j p bound 512
3337if answered
3338break
3339end
3340qj = get d_po j
3341sj = gi b add qj 2
3342nwj = gu b add qj 3
3343for k ulong nd
3344tk = gi b add t0 k
3345if eq tk sj
3346answered = true
3347end
3348for k2 ulong nwj
3349if eq tk gi b add add qj 4 k2
3350answered = true
3351end
3352end
3353end
3354j = add j 1
3355end
3356if not answered
3357for k ulong nd
3358tk = gi b add t0 k
3359if and ge tk k_b lt tk add k_b n
3360tm2 = sub tk k_b
3361if eq get d_own tm2 add k_b 1
3362you = gi b q
3363else
3364if and eq on 1000 ne get d_state tm2 3
3365on = tm2
3366end
3367end
3368end
3369end
3370if lt on 1000
3371seq = gi b q
3372end
3373end
3374end
3375end
3376wo = sub k_b 1
3377won = ulong 0
3378if lt on 1000
3379wo = add k_b on
3380won = 1
3381end
3382asks = ulong 0
3383if gt you k_b
3384asks = 1
3385end
3386g = swgroup add k_b get d_state m add k_b get d_att m add k_b asks add k_b won
3387put d_grp m g
3388po ob add add base mul n 7 m wo
3389po ob add add base mul n 8 m seq
3390po ob add add base mul n 9 m you
3391po ob add add base mul n 10 m add k_b g
3392if ne get d_role m 2
3393if eq get d_state m 1
3394working = add working 1
3395else
3396if eq get d_state m 2
3397waiting = add waiting 1
3398else
3399stopped = add stopped 1
3400end
3401end
3402end
3403end
3404po ob 1 add k_b working
3405po ob 2 add k_b waiting
3406po ob 3 add k_b stopped
3407for pass2 ulong 2
3408for m ulong n
3409v = get d_grp m
3410if eq pass2 1
3411v = get d_state m
3412end
3413if ne roster add k_b 1
3414v = mul v 2
3415if eq get d_cnt m 0
3416v = add v 1
3417end
3418end
3419put d_key m v
3420end
3421sworder b n
3422for m ulong n
3423po ob add add base mul n add 11 pass2 m add k_b get d_ord m
3424end
3425end
3426ret add base mul n 13
3427end
3428
3429note op 12: who addressed whom in the first posts, for the graph. Numbers: the members n, upTo, how many posts
3430note have a sender, how many helper flags, how many posts have their co-signers, their addressed members and
3431note their to-all flag; then the helper flags (n); then a post each: sender, how many co-signers (4 at most) and
3432note who, how many addressed members and who, 1 when it is to all. Helpers are left out. The answer is bytes,
3433note six a pair in order of sender then receiver: from, to, how many posts named the receiver (16-bit), how many
3434note reached it only because the post was for everyone (16-bit). Returns the reply length
3435fn swedges b view uchar 1048576 c ulong ob edit uchar 65544 to ulong
3436named = new ulong 96 0
3437if lt c 7
3438ret 0
3439end
3440nn = gi b 0
3441if or le nn k_b gt nn add k_b 96
3442ret 0
3443end
3444n = sub nn k_b
3445upto = gi b 1
3446posts = gu b 2
3447if gt posts 100000
3448ret 0
3449end
3450if lt upto add k_b posts
3451posts = ulong 0
3452if gt upto k_b
3453posts = sub upto k_b
3454end
3455end
3456if or gt posts 400 ne gi b 3 nn
3457ret 0
3458end
3459if or or lt gi b 4 add k_b posts lt gi b 5 add k_b posts lt gi b 6 add k_b posts
3460ret 0
3461end
3462for z ulong mul n n
3463put d_cell z 0
3464put d_cell add 9216 z 0
3465end
3466hb = ulong 7
3467q = add 7 n
3468if gt q c
3469ret 0
3470end
3471for i ulong posts
3472if gt add q 2 c
3473ret 0
3474end
3475s0 = gi b q
3476nw = gu b add q 1
3477if gt nw 4
3478ret 0
3479end
3480if gt add add q 3 nw c
3481ret 0
3482end
3483nd = gu b add add q 2 nw
3484if gt nd 96
3485ret 0
3486end
3487dq = add add q 3 nw
3488if gt add add dq nd 1 c
3489ret 0
3490end
3491tall = gi b add dq nd
3492for s ulong add nw 1
3493from = s0
3494if gt s 0
3495from = gi b add add q 2 sub s 1
3496end
3497okf = and ge from k_b lt from add k_b n
3498if okf
3499if ne gi b add hb sub from k_b k_b
3500okf = false
3501end
3502end
3503if okf
3504if gt s 0
3505if eq from s0
3506okf = false
3507end
3508end
3509end
3510if okf
3511f = sub from k_b
3512for z ulong n
3513put named z 0
3514end
3515for k ulong nd
3516if lt k n
3517tv = gi b add dq k
3518if and ge tv k_b lt tv add k_b n
3519tt = sub tv k_b
3520if and ne tt f eq gi b add hb tt k_b
3521if eq get named tt 0
3522put named tt 1
3523put d_cell add mul f n tt add get d_cell add mul f n tt 1
3524end
3525end
3526end
3527end
3528end
3529if eq tall add k_b 1
3530for tt ulong n
3531if and ne tt f eq gi b add hb tt k_b
3532if eq get named tt 0
3533put d_cell add 9216 add mul f n tt add get d_cell add 9216 add mul f n tt 1
3534end
3535end
3536end
3537end
3538end
3539end
3540q = add add dq nd 1
3541end
3542o = ulong 8
3543for cell ulong mul n n
3544dv = get d_cell cell
3545av = get d_cell add 9216 cell
3546if gt add dv av 0
3547put ob o uchar div cell n
3548put ob add o 1 uchar rem cell n
3549put ob add o 2 uchar and dv 255
3550put ob add o 3 uchar and shr dv 8 255
3551put ob add o 4 uchar and av 255
3552put ob add o 5 uchar and shr av 8 255
3553o = add o 6
3554end
3555end
3556ret sub o 8
3557end
3558
3559note op 14: which members to draw when there are too many for one picture: the keep members with the most posts
3560note to or from others (the earlier member wins a tie). Numbers: the members n, 1 to count posts for everyone
3561note too, keep, how many edge numbers follow, then the edges, five numbers a row: from, to, count, direct, to all.
3562note The answer is a byte a member, 1 when it is picked; a member with no line is never picked
3563fn swbusy b view uchar 1048576 c ulong ob edit uchar 65544 to ulong
3564weight = new ulong 96 0
3565flag = new ulong 96 0
3566if lt c 4
3567ret 0
3568end
3569nn = gi b 0
3570if or le nn k_b gt nn add k_b 96
3571ret 0
3572end
3573n = sub nn k_b
3574ne2 = gu b 3
3575if ne c add 4 ne2
3576ret 0
3577end
3578e = ulong 0
3579while lt add e 4 ne2 bound 16384
3580if ge e 46080
3581break
3582end
3583from = gi b add 4 e
3584tv = gi b add 5 e
3585cv = gi b add 7 e
3586if eq gi b 1 add k_b 1
3587cv = gi b add 6 e
3588end
3589if and and ge from k_b lt from add k_b n and ge tv k_b lt tv add k_b n
3590if ge cv k_b
3591put weight sub from k_b add get weight sub from k_b sub cv k_b
3592put weight sub tv k_b add get weight sub tv k_b sub cv k_b
3593end
3594end
3595e = add e 5
3596end
3597keep = gi b 2
3598picked = ulong 0
3599while lt picked n bound 128
3600if ge add k_b picked keep
3601break
3602end
3603best = ulong 1000
3604for m ulong n
3605if and eq get flag m 0 gt get weight m 0
3606if eq best 1000
3607best = m
3608else
3609if gt get weight m get weight best
3610best = m
3611end
3612end
3613end
3614end
3615if eq best 1000
3616break
3617end
3618put flag best 1
3619picked = add picked 1
3620end
3621for m ulong n
3622put ob add 8 m uchar get flag m
3623end
3624ret n
3625end
3626
3627note op 13: how many of the first posts each member sent to everyone. Numbers: the members n, upTo, how many posts
3628note have a sender and how many have a to-all flag, then a post each: sender, 1 when it is to all. The answer is n
3629note numbers (none when n is not 1 to 96). Returns the count of numbers
3630fn swbroad b view uchar 1048576 c ulong ob edit uchar 65544 to ulong
3631outv = new ulong 96 0
3632if lt c 4
3633ret 0
3634end
3635nn = gi b 0
3636if or le nn k_b gt nn add k_b 96
3637ret 0
3638end
3639n = sub nn k_b
3640posts = gu b 2
3641if gt posts 100000
3642posts = ulong 100000
3643end
3644upto = gi b 1
3645if lt upto add k_b posts
3646posts = ulong 0
3647if gt upto k_b
3648posts = sub upto k_b
3649end
3650end
3651na = gu b 3
3652i = ulong 0
3653while lt i posts bound 512
3654if or ge i 400 ge i na
3655break
3656end
3657if gt add 6 mul i 2 c
3658break
3659end
3660s = gi b add 4 mul i 2
3661if eq gi b add 5 mul i 2 add k_b 1
3662if and ge s k_b lt s add k_b n
3663put outv sub s k_b add get outv sub s k_b 1
3664end
3665end
3666i = add i 1
3667end
3668for m ulong n
3669po ob m add k_b get outv m
3670end
3671ret n
3672end
3673
3674note ---- the one box: which rows it shows for a typed text, and in which order (kind 7 op 17) ----
3675note Numbers: 0 the characters of the typed text (trimmed, lower case), 1 its bytes, 2 a web address (0 no, 1 typed
3676note with its scheme, 2 bare), 3 1 when this Mac has finished searching its files, 4 1 when a file has exactly that
3677note name, 5 1 for a question, 6 1 when a sentence may go to the agent; 7 to 12 the counts of apps, files,
3678note screens, commands, accounts and copied things; then four an app (how well it matched, how much it is used,
3679note its lift from habit, 1 when it was in front before), three a file (its match, its use, 1 when it is an app
3680note already listed), two an account (1 when its name holds the text, 1 when it starts with it), one a copied
3681note thing (1 when pinned); then for each screen and each command a count of words and their lengths. Bytes: the
3682note text, then those words. The rows: the 8 best apps (match + 40 + use + half the lift + 10 for the one in
3683note front before); the address (150 with a scheme; bare, only after the file search: 95, or 3 when a file has
3684note that name); from 2 characters a screen (230 named, 125 a word starts so), a command (220; 110 from 3
3685note characters when a word starts so) and each matching account (95 when it starts so, else 70); the first 8
3686note copied things (85 less the place, 10 more when pinned); the first 12 files that are not apps (match + use);
3687note Ask (1000 for a question, else 2); the agent (999 when nothing else matched, it is not a question and a
3688note sentence may go to it, else 1). Highest first; equal rows stay in this order.
3689note The answer: the count of rows, then four numbers a row: its kind (1 app, 2 address, 3 screen, 4 command,
3690note 5 account, 6 copied, 7 file, 8 ask, 9 agent), its place in its list, its score, 1 for the promoted agent
3691global r_kind ulong 1024
3692global r_idx ulong 1024
3693global r_score ulong 1024
3694global r_flag ulong 1024
3695global r_as ulong 2048
3696global r_ao ulong 2048
3697
3698fn rkadd n ulong kind ulong idx ulong score ulong flag ulong to ulong
3699if ge n 1024
3700ret n
3701end
3702put r_kind n kind
3703put r_idx n idx
3704put r_score n score
3705put r_flag n flag
3706ret add n 1
3707end
3708
3709note half of a number as it travels, toward zero
3710fn rkhalf v ulong to ulong
3711if ge v k_b
3712ret add k_b div sub v k_b 2
3713end
3714ret sub k_b div sub k_b v 2
3715end
3716
3717note the wl bytes at w against the ll bytes of the typed text at lo: equal (md 0), or starting with it (md 1)
3718fn rkword b view uchar 1048576 w ulong wl ulong lo ulong ll ulong md ulong to bool
3719if eq md 0
3720if ne wl ll
3721ret false
3722end
3723else
3724if lt wl ll
3725ret false
3726end
3727end
3728i = ulong 0
3729while lt i ll bound 999999
3730if ne ulong get b add w i ulong get b add lo i
3731ret false
3732end
3733i = add i 1
3734end
3735ret true
3736end
3737
3738note the n rows, highest score first (equal rows stay in their order), as the answer: the count, then four numbers
3739note a row (kind, place, score, flag); how many numbers that is
3740fn rkout n ulong ob edit uchar 65544 to ulong
3741for i ulong n
3742s = get r_score i
3743pos = i
3744while gt pos 0 bound 2048
3745p = get r_ao sub pos 1
3746if ge get r_score p s
3747break
3748end
3749put r_ao pos p
3750pos = sub pos 1
3751end
3752put r_ao pos i
3753end
3754po ob 0 add k_b n
3755for i ulong n
3756x = get r_ao i
3757o = add 1 mul i 4
3758po ob o add k_b get r_kind x
3759po ob add o 1 add k_b get r_idx x
3760po ob add o 2 get r_score x
3761po ob add o 3 add k_b get r_flag x
3762end
3763ret add 1 mul n 4
3764end
3765
3766fn swrank b view uchar 1048576 c ulong nbytes ulong ob edit uchar 65544 to ulong
3767if lt c 13
3768ret 0
3769end
3770lcn = gu b 0
3771ll = gu b 1
3772na = gu b 7
3773nf = gu b 8
3774ns = gu b 9
3775nc = gu b 10
3776nacc = gu b 11
3777ncl = gu b 12
3778if or or gt na 2048 gt nf 4000 or gt ns 32 gt nc 32
3779ret 0
3780end
3781if or gt nacc 800 or gt ncl 64 gt ll 100000
3782ret 0
3783end
3784pa = ulong 13
3785pf = add pa mul na 4
3786pacc = add pf mul nf 3
3787pcl = add pacc mul nacc 2
3788q = add pcl ncl
3789if gt q c
3790ret 0
3791end
3792lo = add 8 mul c 8
3793tend = add lo nbytes
3794wp = add lo ll
3795if gt wp tend
3796ret 0
3797end
3798n = ulong 0
3799for i ulong na
3800o = add pa mul i 4
3801s = sub add add add gi b o gi b add o 1 rkhalf gi b add o 2 40 mul 2 k_b
3802if eq gi b add o 3 add k_b 1
3803s = add s 10
3804end
3805put r_as i s
3806pos = i
3807while gt pos 0 bound 4096
3808p = get r_ao sub pos 1
3809if ge get r_as p s
3810break
3811end
3812put r_ao pos p
3813pos = sub pos 1
3814end
3815put r_ao pos i
3816end
3817for i ulong na
3818if lt i 8
3819n = rkadd n 1 get r_ao i get r_as get r_ao i 0
3820end
3821end
3822adr = gi b 2
3823if eq adr add k_b 1
3824n = rkadd n 2 0 add k_b 150 0
3825end
3826if and eq adr add k_b 2 eq gi b 3 add k_b 1
3827sc = add k_b 95
3828if eq gi b 4 add k_b 1
3829sc = add k_b 3
3830end
3831n = rkadd n 2 0 sc 0
3832end
3833for pass3 ulong 2
3834cntw = ns
3835if eq pass3 1
3836cntw = nc
3837end
3838for k ulong cntw
3839if ge q c
3840ret 0
3841end
3842nw = gu b q
3843q = add q 1
3844if gt nw 64
3845ret 0
3846end
3847exact = false
3848pre = false
3849for w ulong nw
3850if ge q c
3851ret 0
3852end
3853wl = gu b q
3854q = add q 1
3855if gt add wp wl tend
3856ret 0
3857end
3858if rkword b wp wl lo ll 0
3859exact = true
3860end
3861if rkword b wp wl lo ll 1
3862pre = true
3863end
3864wp = add wp wl
3865end
3866if ge lcn 2
3867if eq pass3 0
3868if exact
3869n = rkadd n 3 k add k_b 230 0
3870else
3871if pre
3872n = rkadd n 3 k add k_b 125 0
3873end
3874end
3875else
3876if exact
3877n = rkadd n 4 k add k_b 220 0
3878else
3879if and pre ge lcn 3
3880n = rkadd n 4 k add k_b 110 0
3881end
3882end
3883end
3884end
3885end
3886end
3887if ge lcn 2
3888for i ulong nacc
3889if eq gi b add pacc mul i 2 add k_b 1
3890sc = add k_b 70
3891if eq gi b add add pacc mul i 2 1 add k_b 1
3892sc = add k_b 95
3893end
3894n = rkadd n 5 i sc 0
3895end
3896end
3897end
3898for i ulong ncl
3899if lt i 8
3900sc = sub add k_b 85 i
3901if eq gi b add pcl i add k_b 1
3902sc = add sc 10
3903end
3904n = rkadd n 6 i sc 0
3905end
3906end
3907shown = ulong 0
3908for i ulong nf
3909o = add pf mul i 3
3910if and ne gi b add o 2 add k_b 1 lt shown 12
3911n = rkadd n 7 i sub add gi b o gi b add o 1 k_b 0
3912shown = add shown 1
3913end
3914end
3915nothing = eq n 0
3916quest = eq gi b 5 add k_b 1
3917sc = add k_b 2
3918if quest
3919sc = add k_b 1000
3920end
3921n = rkadd n 8 0 sc 0
3922prom = ulong 0
3923sc = add k_b 1
3924if and and not quest nothing eq gi b 6 add k_b 1
3925prom = 1
3926sc = add k_b 999
3927end
3928n = rkadd n 9 0 sc prom
3929ret rkout n ob
3930end
3931
3932note ---- the one box before anything is typed: what to offer (kind 7 op 18) ----
3933note Numbers: 0 1 when something was copied, 1 how many seconds ago, 2 1 when it is a web address written with
3934note http, 3 1 when the app in front is a browser, 4 how many other copied things follow (at most 20), 5 1 when
3935note the clipboard history is paused, 6 the count of apps; then six an app: its lift from the app in front, its
3936note lift from the hour, how much it is used, 1 when it is running, 1 when it was in front before, and its
3937note points for being in front lately. The apps come in the order of their ids, which settles equal scores.
3938note The rows: the copied thing (130 under a minute old, 95 under ten, else 70) and, for an address, a row to
3939note open it (5 more, or 5 less when a browser is in front); the six best apps among those with a lift, some
3940note use, running or in front before (40 + both lifts + a quarter of the use + 45 in front before + 8 running +
3941note the points for lately); the other copied things (38 less the place); the clipboard screen at 39 when the
3942note history is paused. Highest first; equal rows stay in this order.
3943note The answer: the count of rows, then four numbers a row: its kind (1 app, 2 address, 3 the clipboard
3944note screen, 6 copied), its place (an app's place in the list; 0 the newest copied thing, then 1 and up), its
3945note score, and why it is offered (0 nothing said, 1 just copied, 2 the last thing copied, 3 a copied address,
3946note 4 often opened from here, 5 just here, 6 often opened around now, 7 among the most used, 8 running now,
3947note 9 the history is paused)
3948global s_ps ulong 8
3949global s_pi ulong 8
3950global s_pw ulong 8
3951
3952fn swsuggest b view uchar 1048576 c ulong ob edit uchar 65544 to ulong
3953if lt c 7
3954ret 0
3955end
3956rest = gu b 4
3957na = gu b 6
3958if or gt na 2048 gt rest 20
3959ret 0
3960end
3961if ne c add 7 mul na 6
3962ret 0
3963end
3964n = ulong 0
3965if eq gi b 0 add k_b 1
3966age = gi b 1
3967s = ulong 70
3968why = ulong 2
3969if lt age add k_b 600
3970s = 95
3971why = 1
3972end
3973if lt age add k_b 60
3974s = 130
3975end
3976n = rkadd n 6 0 add k_b s why
3977if eq gi b 2 add k_b 1
3978if eq gi b 3 add k_b 1
3979n = rkadd n 2 0 add k_b sub s 5 3
3980else
3981n = rkadd n 2 0 add k_b add s 5 3
3982end
3983end
3984end
3985cnt = ulong 0
3986for i ulong na
3987o = add 7 mul i 6
3988lf = gu b o
3989lh = gu b add o 1
3990used = gu b add o 2
3991run = eq gi b add o 3 add k_b 1
3992prev = eq gi b add o 4 add k_b 1
3993lately = gu b add o 5
3994if or or gt add lf lh 0 gt used 0 or run prev
3995sc = add add add 40 lf lh div used 4
3996if prev
3997sc = add sc 45
3998end
3999if run
4000sc = add sc 8
4001end
4002sc = add sc lately
4003why = ulong 8
4004if gt used 0
4005why = 7
4006end
4007if gt lh 0
4008why = 6
4009end
4010if prev
4011why = 5
4012end
4013if gt lf 0
4014why = 4
4015end
4016keep = true
4017pos = cnt
4018if eq cnt 6
4019if le sc get s_ps 5
4020keep = false
4021end
4022pos = 5
4023else
4024cnt = add cnt 1
4025end
4026if keep
4027while gt pos 0 bound 8
4028if ge get s_ps sub pos 1 sc
4029break
4030end
4031put s_ps pos get s_ps sub pos 1
4032put s_pi pos get s_pi sub pos 1
4033put s_pw pos get s_pw sub pos 1
4034pos = sub pos 1
4035end
4036put s_ps pos sc
4037put s_pi pos i
4038put s_pw pos why
4039end
4040end
4041end
4042for j ulong cnt
4043n = rkadd n 1 get s_pi j add k_b get s_ps j get s_pw j
4044end
4045for i ulong rest
4046n = rkadd n 6 add 1 i add k_b sub 38 i 0
4047end
4048if eq gi b 5 add k_b 1
4049n = rkadd n 3 0 add k_b 39 9
4050end
4051ret rkout n ob
4052end
4053
4054note ---- habits: how much an app is lifted by where and when it is usually opened (kind 7 op 19) ----
4055note Numbers: 0 the hour now, 1 1 when an app is in front, 2 the count of apps, 3 the count of remembered
4056note launches; then three a launch: 1 when it was made from the app now in front, its hour, and which app it
4057note opened (its place in the list, or -1 for an app not in it). A habit counts from the second time. An app's
4058note lift from the app in front is 60 times its share of the launches made from there; its lift from the hour
4059note is 25 times its share of the launches within an hour of now (23 and 0 are an hour apart).
4060note The answer: the count of apps, then two numbers an app: the lift from the app in front, the lift from the hour
4061fn swlifts b view uchar 1048576 c ulong ob edit uchar 65544 to ulong
4062if lt c 4
4063ret 0
4064end
4065hour = gi b 0
4066front = eq gi b 1 add k_b 1
4067na = gu b 2
4068nl = gu b 3
4069if or gt na 2048 gt nl 4096
4070ret 0
4071end
4072if ne c add 4 mul nl 3
4073ret 0
4074end
4075for i ulong na
4076put r_as i 0
4077put r_ao i 0
4078end
4079ft = ulong 0
4080nt = ulong 0
4081for j ulong nl
4082o = add 4 mul j 3
4083here = and front eq gi b o add k_b 1
4084h = gi b add o 1
4085d = ulong 0
4086if ge h hour
4087d = sub h hour
4088else
4089d = sub hour h
4090end
4091nearh = or le d 1 ge d 23
4092a = gi b add o 2
4093ix = na
4094if ge a k_b
4095if lt sub a k_b na
4096ix = sub a k_b
4097end
4098end
4099if here
4100ft = add ft 1
4101if lt ix na
4102put r_as ix add get r_as ix 1
4103end
4104end
4105if nearh
4106nt = add nt 1
4107if lt ix na
4108put r_ao ix add get r_ao ix 1
4109end
4110end
4111end
4112po ob 0 add k_b na
4113for i ulong na
4114f = ulong 0
4115if ge get r_as i 2
4116f = div mul 60 get r_as i ft
4117end
4118g = ulong 0
4119if ge get r_ao i 2
4120g = div mul 25 get r_ao i nt
4121end
4122po ob add 1 mul i 2 add k_b f
4123po ob add 2 mul i 2 add k_b g
4124end
4125ret add 1 mul na 2
4126end
4127
4128note ---- AES-256-GCM, for Hop's encrypted clipboard history (kind 7 op 20) ----
4129note The history is sealed as CryptoKit's AES.GCM seals it: a 256-bit key, a 12-byte nonce, no added data, a
4130note 16-byte tag. This is that cipher in Omega: AES by its definition (the S-box is worked out, not listed), the
4131note counter mode, and the tag by GHASH with a table of 16 multiples of the hash key. The work is done on
4132note globals: the key in c_k, the nonce in c_n, the data in c_d (in place, 16 MiB at most), the tag in c_tg. fn gcunlock checks
4133note the tag over the whole data before any of it is unsealed, and compares all 16 bytes whatever they are. It is
4134note not written to run in the same time for every key (the tables are indexed by secret bytes): it is for
4135note files on the person's own Mac, not for a service others can time.
4136note Request: two numbers (1 to seal, 0 to unseal; the count of data bytes, 4096 at most), then the bytes: the
4137note key, the nonce, the data, and to unseal the tag. Answer to unseal: 1 and the data's bytes, or 0 alone when
4138note the tag is not the data's. Answer to seal: 1, the sealed bytes, the 16 bytes of the tag.
4139global c_sb uchar 256
4140global c_m2 uchar 256
4141global c_l4 ulong 16
4142global c_rd ulong 1
4143global c_k uchar 32
4144global c_n uchar 12
4145global c_tg uchar 16
4146global c_rk uchar 240
4147global c_st uchar 16
4148global c_tm uchar 16
4149global c_y uchar 16
4150global c_x uchar 16
4151global c_hh ulong 16
4152global c_hl ulong 16
4153global c_d uchar 16777216
4154
4155note the tables that do not depend on the key: the S-box (the inverse in the field, then the affine step), the
4156note doubling in the field, and the 16 reductions of GHASH
4157fn gcinit to void
4158if eq get c_rd 0 1
4159ret
4160end
4161p = ulong 1
4162q = ulong 1
4163for i ulong 255
4164t = xor p shl p 1
4165if ne and p 128 0
4166t = xor t 27
4167end
4168p = and t 255
4169q = and xor q shl q 1 255
4170q = and xor q shl q 2 255
4171q = and xor q shl q 4 255
4172if ne and q 128 0
4173q = xor q 9
4174end
4175r1 = and or shl q 1 shr q 7 255
4176r2 = and or shl q 2 shr q 6 255
4177r3 = and or shl q 3 shr q 5 255
4178r4 = and or shl q 4 shr q 4 255
4179x = xor xor xor xor q r1 r2 r3 r4
4180put c_sb p uchar xor x 99
4181end
4182put c_sb 0 99
4183for i ulong 256
4184d = shl i 1
4185if ne and i 128 0
4186d = xor d 27
4187end
4188put c_m2 i uchar and d 255
4189end
4190put c_l4 0 0
4191put c_l4 1 7200
4192put c_l4 2 14400
4193put c_l4 3 9312
4194put c_l4 4 28800
4195put c_l4 5 27808
4196put c_l4 6 18624
4197put c_l4 7 21728
4198put c_l4 8 57600
4199put c_l4 9 64800
4200put c_l4 10 55616
4201put c_l4 11 50528
4202put c_l4 12 37248
4203put c_l4 13 36256
4204put c_l4 14 43456
4205put c_l4 15 46560
4206put c_rd 0 1
4207end
4208
4209note the cipher on the 16 bytes of c_st, with the round keys of c_rk
4210fn gcblk to void
4211for i ulong 16
4212put c_st i uchar xor ulong get c_st i ulong get c_rk i
4213end
4214for r ulong 14
4215rk = mul add r 1 16
4216for cc ulong 4
4217for w ulong 4
4218put c_tm add mul cc 4 w get c_sb ulong get c_st add mul and add cc w 3 4 w
4219end
4220end
4221if lt r 13
4222for cc ulong 4
4223o = mul cc 4
4224a0 = ulong get c_tm o
4225a1 = ulong get c_tm add o 1
4226a2 = ulong get c_tm add o 2
4227a3 = ulong get c_tm add o 3
4228d0 = ulong get c_m2 a0
4229d1 = ulong get c_m2 a1
4230d2 = ulong get c_m2 a2
4231d3 = ulong get c_m2 a3
4232put c_st o uchar xor xor xor xor xor d0 d1 a1 a2 a3 ulong get c_rk add rk o
4233put c_st add o 1 uchar xor xor xor xor xor a0 d1 d2 a2 a3 ulong get c_rk add rk add o 1
4234put c_st add o 2 uchar xor xor xor xor xor a0 a1 d2 d3 a3 ulong get c_rk add rk add o 2
4235put c_st add o 3 uchar xor xor xor xor xor d0 a0 a1 a2 d3 ulong get c_rk add rk add o 3
4236end
4237else
4238for i ulong 16
4239put c_st i uchar xor ulong get c_tm i ulong get c_rk add rk i
4240end
4241end
4242end
4243end
4244
4245note the key of c_k: its 15 round keys into c_rk, and the 16 multiples of the hash key (the cipher of 16 zero
4246note bytes) into c_hh and c_hl
4247fn gckey to void
4248gcinit
4249for i ulong 32
4250put c_rk i get c_k i
4251end
4252rc = ulong 1
4253for w ulong 52
4254i = add w 8
4255o = mul i 4
4256t0 = ulong get c_rk sub o 4
4257t1 = ulong get c_rk sub o 3
4258t2 = ulong get c_rk sub o 2
4259t3 = ulong get c_rk sub o 1
4260if eq and i 7 0
4261u0 = xor ulong get c_sb t1 rc
4262u1 = ulong get c_sb t2
4263u2 = ulong get c_sb t3
4264u3 = ulong get c_sb t0
4265rc = ulong get c_m2 rc
4266t0 = u0
4267t1 = u1
4268t2 = u2
4269t3 = u3
4270end
4271if eq and i 7 4
4272t0 = ulong get c_sb t0
4273t1 = ulong get c_sb t1
4274t2 = ulong get c_sb t2
4275t3 = ulong get c_sb t3
4276end
4277put c_rk o uchar xor ulong get c_rk sub o 32 t0
4278put c_rk add o 1 uchar xor ulong get c_rk sub o 31 t1
4279put c_rk add o 2 uchar xor ulong get c_rk sub o 30 t2
4280put c_rk add o 3 uchar xor ulong get c_rk sub o 29 t3
4281end
4282for i ulong 16
4283put c_st i 0
4284end
4285gcblk
4286vh = ulong 0
4287vl = ulong 0
4288for i ulong 8
4289vh = or shl vh 8 ulong get c_st i
4290vl = or shl vl 8 ulong get c_st add 8 i
4291end
4292put c_hh 0 0
4293put c_hl 0 0
4294put c_hh 8 vh
4295put c_hl 8 vl
4296ix = ulong 4
4297for s ulong 3
4298tt = ulong 0
4299if ne and vl 1 0
4300tt = 16212958658533785600
4301end
4302vl = or shl vh 63 shr vl 1
4303vh = xor shr vh 1 tt
4304put c_hl ix vl
4305put c_hh ix vh
4306ix = shr ix 1
4307end
4308ix = 2
4309for s ulong 3
4310for j ulong sub ix 1
4311jj = add j 1
4312put c_hh add ix jj xor get c_hh ix get c_hh jj
4313put c_hl add ix jj xor get c_hl ix get c_hl jj
4314end
4315ix = shl ix 1
4316end
4317end
4318
4319note c_y times the hash key, in the field of GHASH
4320fn gcmul to void
4321nlo = and ulong get c_y 15 15
4322zh = get c_hh nlo
4323zl = get c_hl nlo
4324for s ulong 16
4325bv = ulong get c_y sub 15 s
4326nlo = and bv 15
4327nhi = shr bv 4
4328if ne s 0
4329r4 = and zl 15
4330zl = or shl zh 60 shr zl 4
4331zh = xor shr zh 4 shl get c_l4 r4 48
4332zh = xor zh get c_hh nlo
4333zl = xor zl get c_hl nlo
4334end
4335r4 = and zl 15
4336zl = or shl zh 60 shr zl 4
4337zh = xor shr zh 4 shl get c_l4 r4 48
4338zh = xor zh get c_hh nhi
4339zl = xor zl get c_hl nhi
4340end
4341for i ulong 8
4342sh = mul sub 7 i 8
4343put c_y i uchar and shr zh sh 255
4344put c_y add 8 i uchar and shr zl sh 255
4345end
4346end
4347
4348note the tag of the n sealed bytes of c_d under the key and the nonce, into c_x
4349fn gctag n ulong to void
4350for i ulong 16
4351put c_y i 0
4352end
4353nb = div add n 15 16
4354bk = ulong 0
4355for seg ulong 17
4356while and lt bk nb lt bk mul add seg 1 65536 bound 70000
4357o = mul bk 16
4358m = sub n o
4359if gt m 16
4360m = 16
4361end
4362for j ulong m bound 16
4363put c_y j uchar xor ulong get c_y j ulong get c_d add o j
4364end
4365gcmul
4366bk = add bk 1
4367end
4368end
4369bits = mul n 8
4370for j ulong 8
4371put c_y add 8 j uchar xor ulong get c_y add 8 j and shr bits mul sub 7 j 8 255
4372end
4373gcmul
4374for i ulong 12
4375put c_st i get c_n i
4376end
4377put c_st 12 0
4378put c_st 13 0
4379put c_st 14 0
4380put c_st 15 1
4381gcblk
4382for i ulong 16
4383put c_x i uchar xor ulong get c_st i ulong get c_y i
4384end
4385end
4386
4387note the n bytes of c_d through the counter mode (sealing and unsealing are the same)
4388fn gcctr n ulong to void
4389ctr = ulong 2
4390nb = div add n 15 16
4391bk = ulong 0
4392for seg ulong 17
4393while and lt bk nb lt bk mul add seg 1 65536 bound 70000
4394for i ulong 12
4395put c_st i get c_n i
4396end
4397put c_st 12 uchar and shr ctr 24 255
4398put c_st 13 uchar and shr ctr 16 255
4399put c_st 14 uchar and shr ctr 8 255
4400put c_st 15 uchar and ctr 255
4401gcblk
4402o = mul bk 16
4403m = sub n o
4404if gt m 16
4405m = 16
4406end
4407for j ulong m bound 16
4408put c_d add o j uchar xor ulong get c_d add o j ulong get c_st j
4409end
4410ctr = add ctr 1
4411bk = add bk 1
4412end
4413end
4414end
4415
4416note unseal the n bytes of c_d: 1 and they are the data when c_tg is their tag, else 0 and they are untouched
4417fn gcunlock n ulong to ulong
4418if gt n 16777216
4419ret 0
4420end
4421gckey
4422gctag n
4423df = ulong 0
4424for i ulong 16
4425df = or df xor ulong get c_x i ulong get c_tg i
4426end
4427if ne df 0
4428ret 0
4429end
4430gcctr n
4431ret 1
4432end
4433
4434note seal the n bytes of c_d (16 MiB at most; more is left as it is, with a tag of zeros); their tag into c_tg
4435fn gclock n ulong to void
4436if gt n 16777216
4437for i ulong 16
4438put c_tg i 0
4439end
4440ret
4441end
4442gckey
4443gcctr n
4444gctag n
4445for i ulong 16
4446put c_tg i get c_x i
4447end
4448end
4449
4450note forget the key and everything worked out from it
4451fn gcwipe to void
4452for i ulong 32
4453put c_k i 0
4454end
4455for i ulong 240
4456put c_rk i 0
4457end
4458for i ulong 16
4459put c_hh i 0
4460put c_hl i 0
4461put c_st i 0
4462put c_tm i 0
4463put c_y i 0
4464put c_x i 0
4465end
4466end
4467
4468fn swgcm b view uchar 1048576 c ulong nbytes ulong ob edit uchar 65544 to ulong
4469if ne c 2
4470ret 0
4471end
4472dir = gu b 0
4473n = gu b 1
4474if or gt dir 1 gt n 4096
4475ret 0
4476end
4477want = add 44 n
4478if eq dir 0
4479want = add want 16
4480end
4481if ne nbytes want
4482ret 0
4483end
4484t0 = add 8 mul c 8
4485for i ulong 32
4486put c_k i get b add t0 i
4487end
4488for i ulong 12
4489put c_n i get b add add t0 32 i
4490end
4491for i ulong n bound 4096
4492put c_d i get b add add t0 44 i
4493end
4494if eq dir 0
4495for i ulong 16
4496put c_tg i get b add add add t0 44 n i
4497end
4498good = gcunlock n
4499gcwipe
4500po ob 0 add k_b good
4501if eq good 0
4502ret 1
4503end
4504for i ulong n bound 4096
4505po ob add 1 i add k_b ulong get c_d i
4506end
4507ret add 1 n
4508end
4509gclock n
4510gcwipe
4511po ob 0 add k_b 1
4512for i ulong n bound 4096
4513po ob add 1 i add k_b ulong get c_d i
4514end
4515for i ulong 16
4516po ob add add 1 n i add k_b ulong get c_tg i
4517end
4518ret add 17 n
4519end
4520
4521note kind 7: see the note above. Scalar ops answer one number: 1 state, 2 attention, 3 unread (the newest post
4522note less the last one read; -1 not known), 4 group, 5 starts a group, 6 failure (how a member's newest step
4523note failed: a model error at once, a failed tool step once it stayed newest for 60 seconds; 3 when the text
4524note spoke of quota), 7 the line weight of an edge, 1 to 5, by its count against the heaviest, 8 to 11 as noted.
4525note 13 and 16 answer a list of numbers; 12 and 14 answer bytes. 17: the rows of the one box (fn swrank). 18: what the one box offers before anything is typed (fn swsuggest).
4526note 19: the lift of each app from habit (fn swlifts). 20: AES-256-GCM, to seal and unseal (fn swgcm). Returns the reply length, or
4527note 18446744073709551615 for a malformed request
4528fn status ib view uchar 1048576 nb ulong ob edit uchar 65544 to ulong
4529bad = ulong 18446744073709551615
4530if lt nb 8
4531ret bad
4532end
4533op = rd32 ib 0
4534c = rd32 ib 4
4535if gt add 8 mul c 8 nb
4536ret bad
4537end
4538nbytes = sub nb add 8 mul c 8
4539t0 = add 8 mul c 8
4540r = k_b
4541if eq op 1
4542if ne c 5
4543ret bad
4544end
4545r = add k_b swstate gi ib 0 gi ib 1 gi ib 2 gi ib 3 gi ib 4
4546end
4547if eq op 2
4548if ne c 3
4549ret bad
4550end
4551r = add k_b swatt gi ib 0 gi ib 1 gi ib 2
4552end
4553if eq op 3
4554if ne c 2
4555ret bad
4556end
4557r = sub k_b 1
4558if ge gi ib 1 k_b
4559r = k_b
4560if gt gi ib 0 gi ib 1
4561r = add k_b sub gi ib 0 gi ib 1
4562end
4563end
4564end
4565if eq op 4
4566if ne c 4
4567ret bad
4568end
4569r = add k_b swgroup gi ib 0 gi ib 1 gi ib 2 gi ib 3
4570end
4571if eq op 5
4572if ne c 4
4573ret bad
4574end
4575r = add k_b swstarts gi ib 0 gi ib 1 gi ib 2 gi ib 3
4576end
4577if eq op 6
4578if ne c 4
4579ret bad
4580end
4581failed = eq gi ib 0 add k_b 4
4582if and eq gi ib 1 add k_b 2 ge gi ib 2 add k_b 60
4583failed = true
4584end
4585r = k_b
4586if failed
4587r = add k_b 2
4588if eq gi ib 3 add k_b 1
4589r = add k_b 3
4590end
4591end
4592end
4593if eq op 7
4594if ne c 2
4595ret bad
4596end
4597cn = gi ib 0
4598hv = gi ib 1
4599r = add k_b 1
4600if and gt cn add k_b 1 gt hv add k_b 1
4601if ge cn hv
4602r = add k_b 5
4603else
4604r = add add k_b 1 div mul sub sub cn k_b 1 4 sub sub hv k_b 1
4605end
4606end
4607end
4608if eq op 8
4609r = add k_b swturn ib c
4610end
4611if eq op 9
4612r = swact ib c
4613end
4614if eq op 10
4615r = add k_b swquota ib t0 add t0 nbytes
4616end
4617if eq op 11
4618if ne c 2
4619ret bad
4620end
4621rl = gu ib 0
4622nl = gu ib 1
4623if or gt rl 1048576 gt nl 1048576
4624ret bad
4625end
4626if ne add rl nl nbytes
4627ret bad
4628end
4629r = add k_b swrole ib t0 rl add t0 rl nl
4630end
4631if eq op 12
4632ret swedges ib c ob
4633end
4634if eq op 13
4635ret mul 8 swbroad ib c ob
4636end
4637if eq op 14
4638ret swbusy ib c ob
4639end
4640if eq op 16
4641k = swderive ib c nbytes ob
4642if eq k 0
4643ret bad
4644end
4645ret mul k 8
4646end
4647if eq op 17
4648k = swrank ib c nbytes ob
4649if eq k 0
4650ret bad
4651end
4652ret mul k 8
4653end
4654if eq op 18
4655k = swsuggest ib c ob
4656if eq k 0
4657ret bad
4658end
4659ret mul k 8
4660end
4661if eq op 19
4662k = swlifts ib c ob
4663if eq k 0
4664ret bad
4665end
4666ret mul k 8
4667end
4668if eq op 20
4669k = swgcm ib c nbytes ob
4670if eq k 0
4671ret bad
4672end
4673ret mul k 8
4674end
4675if or gt op 11 eq op 0
4676ret bad
4677end
4678po ob 0 r
4679ret 8
4680end
4681
4682note ---- which words were corrected (kind 4) ----
4683note After Hop types a dictation, the field is read again later. The host splits three texts into words (what was
4684note dictated, the field before, the field after) and gives each distinct word a number. This finds the dictation
4685note in the field (its last whole occurrence), sets aside the words that are the same at both ends, lines up the
4686note rest word by word (longest common subsequence) and reports each run where words of the dictation were
4687note replaced: at most 3 words, wholly inside the dictation. Nothing is reported when the dictation is not in the
4688note field, when the part that changed is too large (words before times words after over 250000), or when more
4689note than half of the dictation changed (a rewrite, not a fix).
4690note wt: the table of the line-up, (words before + 1) rows of (words after + 1)
4691global wt ulong 500008
4692
4693note st: 0 where the removed run starts in the field, 1 its length, 2 where the typed run starts in the later
4694note field, 3 its length, 4 runs written, 5 where the dictation starts, 6 its length, 7 removed words that lie
4695note inside the dictation, 8 1 when there were more runs than the answer holds (4096)
4696fn hclose st edit ulong 16 ob edit uchar 65544 to void
4697rc = get st 1
4698ic = get st 3
4699if and gt rc 0 gt ic 0
4700rs = get st 0
4701s0 = get st 5
4702if ge rs s0
4703if le add rs rc add s0 get st 6
4704if le rc 3
4705h = get st 4
4706if ge h 4096
4707put st 8 1
4708else
4709o = add 10 mul h 14
4710put32 ob o rs
4711put ob add o 4 uchar and rc 255
4712put ob add o 5 uchar shr rc 8
4713put32 ob add o 6 get st 2
4714put32 ob add o 10 ic
4715put st 4 add h 1
4716end
4717end
4718end
4719end
4720end
4721put st 1 0
4722put st 3 0
4723end
4724
4725note kind 4: three 32-bit counts (dictated, before, after), then that many 32-bit word numbers for each. The
4726note answer is a 16-bit count, then for each run: where the removed words start in the field before (32-bit), how
4727note many (16-bit), where the typed words start in the field after (32-bit) and how many (32-bit).
4728note Returns the reply length, or 18446744073709551615 for a malformed body or too many runs
4729fn wdiff ib view uchar 1048576 n ulong ob edit uchar 65544 to ulong
4730st = new ulong 16 0
4731bad = ulong 18446744073709551615
4732if lt n 12
4733ret bad
4734end
4735nd = rd32 ib 0
4736nb = rd32 ib 4
4737na = rd32 ib 8
4738if ne n add 12 mul 4 add add nd nb na
4739ret bad
4740end
4741d0 = ulong 12
4742b0 = add 12 mul 4 nd
4743a0 = add b0 mul 4 nb
4744put ob 8 uchar 0
4745put ob 9 uchar 0
4746if eq nd 0
4747ret 2
4748end
4749if lt nb nd
4750ret 2
4751end
4752found = false
4753start = ulong 0
4754q = sub nb nd
4755while true bound 999999
4756k = ulong 0
4757while lt k nd bound 999999
4758if ne rd32 ib add b0 mul 4 add q k rd32 ib add d0 mul 4 k
4759break
4760end
4761k = add k 1
4762end
4763if eq k nd
4764found = true
4765start = q
4766break
4767end
4768if eq q 0
4769break
4770end
4771q = sub q 1
4772end
4773if not found
4774ret 2
4775end
4776p = ulong 0
4777while lt p nb bound 999999
4778if ge p na
4779break
4780end
4781if ne rd32 ib add b0 mul 4 p rd32 ib add a0 mul 4 p
4782break
4783end
4784p = add p 1
4785end
4786s = ulong 0
4787while lt s sub nb p bound 999999
4788if ge s sub na p
4789break
4790end
4791if ne rd32 ib add b0 mul 4 sub sub nb 1 s rd32 ib add a0 mul 4 sub sub na 1 s
4792break
4793end
4794s = add s 1
4795end
4796bc = sub sub nb p s
4797ac = sub sub na p s
4798if eq bc 0
4799ret 2
4800end
4801am = ac
4802if eq am 0
4803am = ulong 1
4804end
4805if gt mul bc am 250000
4806ret 2
4807end
4808w1 = add ac 1
4809for z ulong add bc 1
4810put wt add mul z w1 ac 0
4811end
4812for z ulong w1
4813put wt add mul bc w1 z 0
4814end
4815for ii ulong bc
4816i = sub sub bc 1 ii
4817bi = rd32 ib add b0 mul 4 add p i
4818for jj ulong ac
4819j = sub sub ac 1 jj
4820if eq bi rd32 ib add a0 mul 4 add p j
4821put wt add mul i w1 j add get wt add mul add i 1 w1 add j 1 1
4822else
4823x = get wt add mul add i 1 w1 j
4824y = get wt add mul i w1 add j 1
4825if lt x y
4826x = y
4827end
4828put wt add mul i w1 j x
4829end
4830end
4831end
4832put st 5 start
4833put st 6 nd
4834ci = ulong 0
4835cj = ulong 0
4836while or lt ci bc lt cj ac bound 999999
4837mt = false
4838if lt ci bc
4839if lt cj ac
4840if eq rd32 ib add b0 mul 4 add p ci rd32 ib add a0 mul 4 add p cj
4841mt = true
4842end
4843end
4844end
4845if mt
4846hclose st ob
4847ci = add ci 1
4848cj = add cj 1
4849continue
4850end
4851rm = false
4852if lt ci bc
4853if eq cj ac
4854rm = true
4855else
4856if ge get wt add mul add ci 1 w1 cj get wt add mul ci w1 add cj 1
4857rm = true
4858end
4859end
4860end
4861if rm
4862if eq get st 1 0
4863put st 0 add p ci
4864end
4865put st 1 add get st 1 1
4866ai = add p ci
4867if ge ai start
4868if lt ai add start nd
4869put st 7 add get st 7 1
4870end
4871end
4872ci = add ci 1
4873else
4874if eq get st 3 0
4875put st 2 add p cj
4876end
4877put st 3 add get st 3 1
4878cj = add cj 1
4879end
4880end
4881hclose st ob
4882if ne get st 8 0
4883ret bad
4884end
4885h = get st 4
4886lim = nd
4887if lt lim 2
4888lim = ulong 2
4889end
4890if gt mul get st 7 2 lim
4891h = ulong 0
4892end
4893put ob 8 uchar and h 255
4894put ob 9 uchar shr h 8
4895ret add 2 mul h 14
4896end
4897
4898fn names ib view uchar 1048576 n ulong ob edit uchar 65544 a edit ulong 256 r edit ulong 256 to ulong
4899if ne rem n 2048 0
4900ret 18446744073709551615
4901end
4902c = div n 2048
4903if gt c 512
4904ret 18446744073709551615
4905end
4906for q ulong c
4907for i ulong 256
4908w = ulong 0
4909for j ulong 8
4910w = or w shl ulong get ib add add mul q 2048 mul i 8 j mul j 8
4911end
4912put a i w
4913end
4914v = hopfuzz a r
4915s = get r 3
4916put ob add 8 mul q 3 uchar v
4917put ob add 9 mul q 3 uchar and s 255
4918put ob add 10 mul q 3 uchar shr s 8
4919end
4920ret mul c 3
4921end
4922
4923note kind 2: returns the reply length, or 18446744073709551615 for a malformed body
4924fn paths ib view uchar 1048576 n ulong ob edit uchar 65544 toff edit ulong 64 tlen edit ulong 64 pm edit ulong 8 to ulong
4925bad = ulong 18446744073709551615
4926if lt n 4
4927ret bad
4928end
4929nt = rd16 ib 0
4930np = rd16 ib 2
4931if gt nt 64
4932ret bad
4933end
4934if gt np 16000
4935ret bad
4936end
4937o = ulong 4
4938for k ulong nt
4939if gt add o 2 n
4940ret bad
4941end
4942tl = rd16 ib o
4943o = add o 2
4944if gt add o tl n
4945ret bad
4946end
4947put toff k o
4948put tlen k tl
4949o = add o tl
4950end
4951for q ulong np
4952if gt add o 6 n
4953ret bad
4954end
4955pl = rd16 ib o
4956nl = rd16 ib add o 2
4957ol = rd16 ib add o 4
4958o = add o 6
4959if gt add o add pl nl n
4960ret bad
4961end
4962put pm 0 nt
4963put pm 1 o
4964put pm 2 pl
4965put pm 3 add o pl
4966put pm 4 nl
4967put pm 5 ol
4968put32 ob add 8 mul q 4 fscore ib toff tlen pm
4969o = add o add pl nl
4970end
4971if ne o n
4972ret bad
4973end
4974ret mul np 4
4975end
4976
4977fn main to int
4978ib = new uchar 1048576 0
4979ob = new uchar 65544 0
4980a = new ulong 256 0
4981r = new ulong 256 0
4982toff = new ulong 64 0
4983tlen = new ulong 64 0
4984pm = new ulong 8 0
4985while true bound 999999
4986while true bound 999999
4987if not rdn ib 8
4988ret 0
4989end
4990kind = rd32 ib 0
4991n = rd32 ib 4
4992if gt n 1048576
4993ret 3
4994end
4995if not rdn ib n
4996ret 3
4997end
4998st = ulong 1
4999m = ulong 0
5000if eq kind 1
5001m = names ib n ob a r
5002st = 0
5003end
5004if eq kind 2
5005m = paths ib n ob toff tlen pm
5006st = 0
5007end
5008if eq kind 3
5009m = texts ib n ob
5010st = 0
5011end
5012if eq kind 4
5013m = wdiff ib n ob
5014st = 0
5015end
5016if eq kind 5
5017m = board ib n ob
5018st = 0
5019end
5020if eq kind 6
5021m = asm ib n ob
5022st = 0
5023end
5024if eq kind 7
5025m = status ib n ob
5026st = 0
5027end
5028if eq m 18446744073709551615
5029st = 2
5030m = 0
5031end
5032put32 ob 0 st
5033put32 ob 4 m
5034if not wrn ob add 8 m
5035ret 3
5036end
5037end
5038end
5039ret 0
5040end