interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / text / text.om

519 lines · 9964 bytes Raw Blame History
1note seed/text/text.om: bytes, search, fields, integer text and UTF-8 in seeded Omega (ROADMAP-1.0 RM5).
2note A composable unit like seed/json: no om2 line, no global, no text, no host word. The caller puts
3note `om2` first (unsized views) and concatenates this file before its own code. Functions are tx_*,
4note constants tx*. Arrays are the caller's: OM-2 unsized views and edits (`view uchar *`), so a slice is
5note passed with `part A o k` or as an explicit offset and count where the function takes one.
6note Strict rules, stated once: decimal is [0-9]+ (leading zeros allowed, no sign, no space, no +);
7note signed decimal adds one optional leading '-'; hex is [0-9a-fA-F]+ with no 0x prefix. A value outside
8note the type is txerange, never wrapped. A slice outside its array traps 3, like the host words.
9note Loops walk at most 65536 elements per inner pass, so inputs far above the 1000000-pass loop guard
10note are handled (outer bound 999999 passes, about 65 GB).
11
12const txnone ulong 18446744073709551615
13const txeempty long -1
14const txedigit long -2
15const txerange long -3
16const txespace long -4
17
18note tx_slice: trap 3 unless o .. o+n lies inside an array of length l.
19fn tx_slice l ulong o ulong n ulong to void
20if gt o l
21trap 3
22end
23if gt n sub l o
24trap 3
25end
26end
27
28note ======== bytes ========
29note tx_same: a[ao .. ao+n) equals b[0 .. n) (both ranges already checked by the caller).
30fn tx_same a view uchar * ao ulong b view uchar * n ulong to bool
31i = ulong 0
32while lt i n bound 999999
33e = add i 65536
34if gt e n
35e = n
36end
37while lt i e bound 65536
38if ne get a add ao i get b i
39ret false
40end
41i = add i 1
42end
43end
44ret true
45end
46
47fn tx_eq a view uchar * b view uchar * to bool
48if ne len a len b
49ret false
50end
51ret tx_same a 0 b len a
52end
53
54note tx_cmp: unsigned byte order; a proper prefix sorts first. -1, 0 or 1.
55fn tx_cmp a view uchar * b view uchar * to int
56n = len a
57if lt len b n
58n = len b
59end
60i = ulong 0
61while lt i n bound 999999
62e = add i 65536
63if gt e n
64e = n
65end
66while lt i e bound 65536
67x = get a i
68y = get b i
69if ne x y
70if lt x y
71ret int -1
72end
73ret int 1
74end
75i = add i 1
76end
77end
78if lt len a len b
79ret int -1
80end
81if gt len a len b
82ret int 1
83end
84ret int 0
85end
86
87note ======== search and fields ========
88note tx_find: the first index at or after from where nd occurs in h, or txnone. An empty needle is found
89note at from when from <= len h. Plain left-to-right comparison: time up to len h * len nd.
90fn tx_find h view uchar * nd view uchar * from ulong to ulong
91hn = len h
92m = len nd
93if gt from hn
94ret txnone
95end
96if gt m sub hn from
97ret txnone
98end
99stop = add sub hn m 1
100i = from
101while lt i stop bound 999999
102e = add i 65536
103if gt e stop
104e = stop
105end
106while lt i e bound 65536
107if tx_same h i nd m
108ret i
109end
110i = add i 1
111end
112end
113ret txnone
114end
115
116note tx_field: the end of the field that starts at p: the first index >= p holding sep, or len s.
117note p > len s is txnone. Split: p = 0; e = tx_field s sep p; field [p, e); stop when e = len s, else p = e + 1.
118fn tx_field s view uchar * sep uchar p ulong to ulong
119n = len s
120if gt p n
121ret txnone
122end
123i = p
124while lt i n bound 999999
125e = add i 65536
126if gt e n
127e = n
128end
129while lt i e bound 65536
130if eq get s i sep
131ret i
132end
133i = add i 1
134end
135end
136ret n
137end
138
139note ======== integer text ========
140note tx_hexv: value of one hex digit, or 16 for any other byte.
141fn tx_hexv c uchar to ulong
142if and ge c 48 le c 57
143ret ulong sub c 48
144end
145if and ge c 97 le c 102
146ret ulong sub c 87
147end
148if and ge c 65 le c 70
149ret ulong sub c 55
150end
151ret 16
152end
153
154note tx_pacc: one digit d into the accumulator w: w[0] base (10 or 16), w[1] value, w[2] 1 once the value
155note passed 2^64 - 1 (later digits are still checked, so a bad byte is reported before range).
156fn tx_pacc w edit ulong * d ulong to void
157if ne get w 2 0
158ret
159end
160x = get w 1
161base = get w 0
162lim = ulong 1844674407370955161
163if eq base 16
164lim = ulong 1152921504606846975
165end
166if gt x lim
167put w 2 1
168ret
169end
170if and eq base 10 and eq x lim gt d 5
171put w 2 1
172ret
173end
174put w 1 add mul x base d
175end
176
177note tx_pnum: s[o .. o+n) in base w[0] into w[1] (w[1] and w[2] start at 0). 0, txeempty, txedigit (any
178note byte that is not a digit, reported before range) or txerange (above 2^64 - 1).
179fn tx_pnum s view uchar * o ulong n ulong w edit ulong * to long
180tx_slice len s o n
181if eq n 0
182ret txeempty
183end
184i = o
185stop = add o n
186while lt i stop bound 999999
187e = add i 65536
188if gt e stop
189e = stop
190end
191while lt i e bound 65536
192d = tx_hexv get s i
193if ge d get w 0
194ret txedigit
195end
196tx_pacc w d
197i = add i 1
198end
199end
200if ne get w 2 0
201ret txerange
202end
203ret 0
204end
205
206note tx_pdec / tx_phex: decimal or hex s[o .. o+n) into v[0]; v is unchanged on refusal.
207fn tx_pdec s view uchar * o ulong n ulong v edit ulong * to long
208w = new ulong 3 0
209put w 0 10
210r = tx_pnum s o n w
211if eq r 0
212put v 0 get w 1
213end
214ret r
215end
216
217fn tx_phex s view uchar * o ulong n ulong v edit ulong * to long
218w = new ulong 3 0
219put w 0 16
220r = tx_pnum s o n w
221if eq r 0
222put v 0 get w 1
223end
224ret r
225end
226
227note tx_negm: -x as a long for x in 1 .. 2^63 (0 gives 0), without passing through an overflow.
228fn tx_negm x ulong to long
229if eq x 0
230ret long 0
231end
232ret sub neg long sub x 1 1
233end
234
235note tx_pdeci: optional '-' then decimal, -2^63 .. 2^63 - 1, into v[0]. A lone '-' is txedigit.
236fn tx_pdeci s view uchar * o ulong n ulong v edit long * to long
237m = new ulong 1 0
238tx_slice len s o n
239if eq n 0
240ret txeempty
241end
242minus = eq get s o 45
243if minus
244o = add o 1
245n = sub n 1
246if eq n 0
247ret txedigit
248end
249end
250r = tx_pdec s o n m
251if lt r 0
252ret r
253end
254x = get m 0
255if minus
256if gt x 9223372036854775808
257ret txerange
258end
259put v 0 tx_negm x
260ret 0
261end
262if gt x 9223372036854775807
263ret txerange
264end
265put v 0 long x
266ret 0
267end
268
269note tx_ndig: the number of base-10 or base-16 digits of v (1 for zero).
270fn tx_ndig v ulong base ulong to ulong
271k = ulong 1
272x = div v base
273while gt x 0 bound 20
274k = add k 1
275x = div x base
276end
277ret k
278end
279
280note tx_fput: v as exactly k digits (k >= its digit count) at b[o ..], lowercase hex. The end offset.
281fn tx_fput v ulong base ulong k ulong b edit uchar * o ulong to long
282x = v
283j = k
284while gt j 0 bound 64
285j = sub j 1
286d = rem x base
287c = uchar add d 48
288if gt d 9
289c = uchar add d 87
290end
291put b add o j c
292x = div x base
293end
294ret long add o k
295end
296
297note tx_fdec: v in decimal at b[o ..]; the end offset, or txespace (b unchanged) when it does not fit.
298fn tx_fdec v ulong b edit uchar * o ulong to long
299k = tx_ndig v 10
300tx_slice len b o 0
301if gt k sub len b o
302ret txespace
303end
304ret tx_fput v 10 k b o
305end
306
307fn tx_fdeci v long b edit uchar * o ulong to long
308if ge v 0
309ret tx_fdec ulong v b o
310end
311m = add ulong neg add v 1 1
312k = tx_ndig m 10
313tx_slice len b o 0
314if gt add k 1 sub len b o
315ret txespace
316end
317put b o 45
318ret tx_fput m 10 k b add o 1
319end
320
321note tx_fhex: v in lowercase hex, zero-padded to at least w digits (w 0..16, more traps 3).
322fn tx_fhex v ulong w ulong b edit uchar * o ulong to long
323if gt w 16
324trap 3
325end
326k = tx_ndig v 16
327if lt k w
328k = w
329end
330tx_slice len b o 0
331if gt k sub len b o
332ret txespace
333end
334ret tx_fput v 16 k b o
335end
336
337note ======== UTF-8 ========
338note tx_u8st: RFC 3629 section 4 well-formed UTF-8, the state machine of js_u8st in seed/json/json.om.
339note State 0 expects a lead byte; 1, 2, 3 expect that many continuation bytes 80..BF; 4 after E0 expects
340note A0..BF; 5 after ED expects 80..9F; 6 after F0 expects 90..BF; 7 after F4 expects 80..8F. 9 is
341note refusal (overlong, surrogate, above U+10FFFF, stray continuation, C0, C1, F5..FF).
342fn tx_u8st s ulong c uchar to ulong
343if eq s 0
344if lt c 128
345ret 0
346end
347if lt c 194
348ret 9
349end
350if lt c 224
351ret 1
352end
353if eq c 224
354ret 4
355end
356if eq c 237
357ret 5
358end
359if lt c 240
360ret 2
361end
362if eq c 240
363ret 6
364end
365if lt c 244
366ret 3
367end
368if eq c 244
369ret 7
370end
371ret 9
372end
373lo = uchar 128
374hi = uchar 191
375if eq s 4
376lo = uchar 160
377end
378if eq s 5
379hi = uchar 159
380end
381if eq s 6
382lo = uchar 144
383end
384if eq s 7
385hi = uchar 143
386end
387if or lt c lo gt c hi
388ret 9
389end
390if eq s 1
391ret 0
392end
393if or eq s 2 or eq s 4 eq s 5
394ret 1
395end
396ret 2
397end
398
399note tx_u8ok: o + n when s[o .. o+n) is well-formed UTF-8, otherwise the index of the first byte of the
400note first ill-formed or truncated sequence (the same index a strict decoder reports as the error start).
401fn tx_u8ok s view uchar * o ulong n ulong to ulong
402tx_slice len s o n
403st = ulong 0
404lead = o
405i = o
406stop = add o n
407while lt i stop bound 999999
408e = add i 65536
409if gt e stop
410e = stop
411end
412while lt i e bound 65536
413if eq st 0
414lead = i
415end
416st = tx_u8st st get s i
417if eq st 9
418ret lead
419end
420i = add i 1
421end
422end
423if ne st 0
424ret lead
425end
426ret stop
427end
428
429note tx_u8dec: the code point that starts at s[o] within n bytes, as (cp << 3) | its byte length, or txnone
430note when that sequence is ill-formed or truncated (the same rules as tx_u8ok). n = 0 is txnone.
431fn tx_u8dec s view uchar * o ulong n ulong to ulong
432tx_slice len s o n
433if eq n 0
434ret txnone
435end
436c = get s o
437st = tx_u8st 0 c
438if eq st 9
439ret txnone
440end
441if eq st 0
442ret or shl ulong c 3 1
443end
444k = ulong 4
445cp = ulong and c 7
446if lt c 240
447k = 3
448cp = ulong and c 15
449end
450if lt c 224
451k = 2
452cp = ulong and c 31
453end
454if lt n k
455ret txnone
456end
457for i ulong sub k 1
458b = get s add add o i 1
459st = tx_u8st st b
460if eq st 9
461ret txnone
462end
463cp = or shl cp 6 ulong and b 63
464end
465ret or shl cp 3 k
466end
467
468note tx_u8len: the UTF-8 byte length of cp, or 0 for a surrogate D800..DFFF or a value above 10FFFF.
469fn tx_u8len cp ulong to ulong
470if gt cp 1114111
471ret 0
472end
473if and ge cp 55296 le cp 57343
474ret 0
475end
476if lt cp 128
477ret 1
478end
479if lt cp 2048
480ret 2
481end
482if lt cp 65536
483ret 3
484end
485ret 4
486end
487
488note tx_u8enc: cp as UTF-8 at b[o ..]: the end offset, txerange for a surrogate or a value above 10FFFF,
489note or txespace (b unchanged) when it does not fit.
490fn tx_u8enc cp ulong b edit uchar * o ulong to long
491k = tx_u8len cp
492if eq k 0
493ret txerange
494end
495tx_slice len b o 0
496if gt k sub len b o
497ret txespace
498end
499if eq k 1
500put b o uchar cp
501ret long add o 1
502end
503j = sub k 1
504x = cp
505while gt j 0 bound 3
506put b add o j uchar or 128 and x 63
507x = shr x 6
508j = sub j 1
509end
510lead = ulong 192
511if eq k 3
512lead = 224
513end
514if eq k 4
515lead = 240
516end
517put b o uchar or lead x
518ret long add o k
519end