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