aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / site / verify-core.om
| 1 | note verify-core.om: the Omega site verifier's arena, file tree, byte search, Unicode classes |
| 2 | note and failure report. Port of crates/site-builder `verify` (Rust oracle kept independent). |
| 3 | note Composition (one OM-2 program unit, leading om2 line supplied by the composer): |
| 4 | note om2, runtime.om, json.om, verify-unicode.om, verify-core.om, verify-data.om, |
| 5 | note verify-text.om, verify-seo.om, verify-site.om, verify.om (the only fn main). |
| 6 | note The site generator may compose verify-unicode.om, verify-core.om and verify-data.om alone |
| 7 | note and call sv_vinit then sv_vdata before writing output (pre-generation B1-B4 gate). |
| 8 | note Names: functions sv_*, globals/texts/consts v*/V*, locals w*. No global is passed as an |
| 9 | note argument (OM-1/OM-2 error 19); arena helpers name the arena global directly. |
| 10 | note Files are read whole into the arena `va` as numbered slots (stack discipline: sv_drop pops |
| 11 | note a slot and every later one). Every text read is strict UTF-8, the same refusal as Rust |
| 12 | note fs::read_to_string. Failures are counted and reported to stderr as |
| 13 | note verify: FAIL <RULE> <path>: <reason> [excerpt] |
| 14 | note and the verifier exits 1 when any rule failed. Capacity or I/O refusals exit 70 (runtime). |
| 15 | note Linux AArch64 constants: O_DIRECTORY 040000 = 16384, O_CLOEXEC 02000000 = 524288 |
| 16 | note (/usr/include/aarch64-linux-gnu/asm/fcntl.h); st_mode is the u32 at byte 16 of the |
| 17 | note 128-byte stat; S_IFMT 61440, S_IFDIR 16384, S_IFREG 32768; linux_dirent64 d_reclen u16 at |
| 18 | note 16, name at 19 (NUL-terminated). |
| 19 | const VNONE ulong 18446744073709551615 |
| 20 | const VACAP ulong 16777216 |
| 21 | const VSLOTS ulong 4096 |
| 22 | const VFILES ulong 65536 |
| 23 | const VPOOL ulong 4194304 |
| 24 | global va uchar 16777216 |
| 25 | global vtop ulong 1 |
| 26 | global vso ulong 4096 |
| 27 | global vsn ulong 4096 |
| 28 | global vns ulong 1 |
| 29 | global vpath uchar 4352 |
| 30 | global vpn ulong 1 |
| 31 | global vroot uchar 4096 |
| 32 | global vrn ulong 1 |
| 33 | global vctx uchar 4352 |
| 34 | global vcn ulong 1 |
| 35 | global vfail ulong 1 |
| 36 | global vst uchar 128 |
| 37 | global vdb uchar 32768 |
| 38 | global vfp uchar 4194304 |
| 39 | global vfpn ulong 1 |
| 40 | global vfo ulong 65536 |
| 41 | global vfl ulong 65536 |
| 42 | global vfk uchar 65536 |
| 43 | global vfc ulong 1 |
| 44 | text vtfail "verify: FAIL " |
| 45 | text vtsp " " |
| 46 | text vtcol ": " |
| 47 | text vtnl "\n" |
| 48 | text vtxo " [" |
| 49 | text vtxc "]" |
| 50 | text vtcap "ARENA_CAPACITY" |
| 51 | text vtslot "SLOT_CAPACITY" |
| 52 | text vtpth "PATH_CAPACITY" |
| 53 | text vtfil "TREE_CAPACITY" |
| 54 | text vtdir "DIRECTORY_READ" |
| 55 | text vtrd "FILE_READ" |
| 56 | text vtu8 "U8" |
| 57 | text vtu8w "not valid UTF-8 (Rust read_to_string refuses it)" |
| 58 | text vtopn "unreadable (not a missing file)" |
| 59 | text vtdot "." |
| 60 | text vtdd ".." |
| 61 | text vtsl "/" |
| 62 | |
| 63 | note ---------------------------------------------------------------- report |
| 64 | fn sv_errv off ulong cnt ulong to void |
| 65 | wp = ulong 0 |
| 66 | while lt wp cnt bound 65536 |
| 67 | wk = write 2 va add off wp sub cnt wp |
| 68 | if le wk 0 |
| 69 | exit 70 |
| 70 | end |
| 71 | wp = add wp ulong wk |
| 72 | end |
| 73 | end |
| 74 | fn sv_errc to void |
| 75 | wp = ulong 0 |
| 76 | while lt wp get vcn 0 bound 4352 |
| 77 | wk = write 2 vctx wp sub get vcn 0 wp |
| 78 | if le wk 0 |
| 79 | exit 70 |
| 80 | end |
| 81 | wp = add wp ulong wk |
| 82 | end |
| 83 | end |
| 84 | fn sv_errn n ulong to void |
| 85 | wb = new uchar 20 0 |
| 86 | wp = ulong 20 |
| 87 | while true bound 20 |
| 88 | wp = sub wp 1 |
| 89 | put wb wp uchar add rem n 10 48 |
| 90 | n = div n 10 |
| 91 | if eq n 0 |
| 92 | break |
| 93 | end |
| 94 | end |
| 95 | while lt wp 20 bound 20 |
| 96 | wk = write 2 wb wp sub ulong 20 wp |
| 97 | if le wk 0 |
| 98 | exit 70 |
| 99 | end |
| 100 | wp = add wp ulong wk |
| 101 | end |
| 102 | end |
| 103 | fn sv_head rule view uchar * to void |
| 104 | sr_diag vtfail |
| 105 | sr_diag rule |
| 106 | sr_diag vtsp |
| 107 | sv_errc |
| 108 | sr_diag vtcol |
| 109 | put vfail 0 add get vfail 0 1 |
| 110 | end |
| 111 | fn sv_fail rule view uchar * why view uchar * to void |
| 112 | sv_head rule |
| 113 | sr_diag why |
| 114 | sr_diag vtnl |
| 115 | end |
| 116 | note sv_failx: the failure plus an excerpt of at most 160 arena bytes from off. |
| 117 | fn sv_failx rule view uchar * why view uchar * off ulong cnt ulong to void |
| 118 | sv_head rule |
| 119 | sr_diag why |
| 120 | sr_diag vtxo |
| 121 | wn = cnt |
| 122 | if gt wn 160 |
| 123 | wn = 160 |
| 124 | end |
| 125 | sv_errv off wn |
| 126 | sr_diag vtxc |
| 127 | sr_diag vtnl |
| 128 | end |
| 129 | note sv_failn: the failure plus a decimal count (bookkeeping only). |
| 130 | fn sv_failn rule view uchar * why view uchar * n ulong to void |
| 131 | sv_head rule |
| 132 | sr_diag why |
| 133 | sr_diag vtxo |
| 134 | sv_errn n |
| 135 | sr_diag vtxc |
| 136 | sr_diag vtnl |
| 137 | end |
| 138 | fn sv_ctxt s view uchar * to void |
| 139 | if gt len s len vctx |
| 140 | sr_fail vtpth |
| 141 | end |
| 142 | for wj ulong len s |
| 143 | put vctx wj get s wj |
| 144 | end |
| 145 | put vcn 0 len s |
| 146 | end |
| 147 | fn sv_ctxp to void |
| 148 | for wj ulong get vpn 0 |
| 149 | put vctx wj get vpath wj |
| 150 | end |
| 151 | put vcn 0 get vpn 0 |
| 152 | end |
| 153 | |
| 154 | note ---------------------------------------------------------------- paths |
| 155 | fn sv_pch c uchar to void |
| 156 | wn = get vpn 0 |
| 157 | if ge wn 4095 |
| 158 | sr_fail vtpth |
| 159 | end |
| 160 | put vpath wn c |
| 161 | put vpn 0 add wn 1 |
| 162 | end |
| 163 | fn sv_padd s view uchar * to void |
| 164 | for wj ulong len s |
| 165 | sv_pch get s wj |
| 166 | end |
| 167 | end |
| 168 | fn sv_pva off ulong cnt ulong to void |
| 169 | for wj ulong cnt |
| 170 | sv_pch get va add off wj |
| 171 | end |
| 172 | end |
| 173 | note sv_proot: vpath = the root directory followed by exactly one '/'. |
| 174 | fn sv_proot to void |
| 175 | put vpn 0 0 |
| 176 | for wj ulong get vrn 0 |
| 177 | sv_pch get vroot wj |
| 178 | end |
| 179 | wn = get vpn 0 |
| 180 | if gt wn 0 |
| 181 | if ne get vpath sub wn 1 47 |
| 182 | sv_pch 47 |
| 183 | end |
| 184 | end |
| 185 | end |
| 186 | fn sv_prel rel view uchar * to void |
| 187 | sv_proot |
| 188 | sv_padd rel |
| 189 | sv_ctxp |
| 190 | end |
| 191 | fn sv_setroot s view uchar * to void |
| 192 | if or eq len s 0 gt len s 4000 |
| 193 | sr_fail vtpth |
| 194 | end |
| 195 | for wj ulong len s |
| 196 | put vroot wj get s wj |
| 197 | end |
| 198 | put vrn 0 len s |
| 199 | end |
| 200 | |
| 201 | note ---------------------------------------------------------------- metadata |
| 202 | fn sv_mode to ulong |
| 203 | if eq get vpn 0 0 |
| 204 | ret 0 |
| 205 | end |
| 206 | wk = stat vpath 0 get vpn 0 vst 0 128 |
| 207 | if lt wk 0 |
| 208 | ret 0 |
| 209 | end |
| 210 | wm = or ulong get vst 16 shl ulong get vst 17 8 |
| 211 | ret or wm or shl ulong get vst 18 16 shl ulong get vst 19 24 |
| 212 | end |
| 213 | fn sv_exists to bool |
| 214 | ret ne sv_mode 0 |
| 215 | end |
| 216 | fn sv_isdir to bool |
| 217 | ret eq and sv_mode 61440 16384 |
| 218 | end |
| 219 | fn sv_isreg to bool |
| 220 | ret eq and sv_mode 61440 32768 |
| 221 | end |
| 222 | fn sv_there rel view uchar * to bool |
| 223 | sv_prel rel |
| 224 | ret sv_exists |
| 225 | end |
| 226 | |
| 227 | note ---------------------------------------------------------------- slots |
| 228 | fn sv_snew to ulong |
| 229 | wn = get vns 0 |
| 230 | if ge wn VSLOTS |
| 231 | sr_fail vtslot |
| 232 | end |
| 233 | put vso wn get vtop 0 |
| 234 | put vsn wn 0 |
| 235 | put vns 0 add wn 1 |
| 236 | ret wn |
| 237 | end |
| 238 | fn sv_drop s ulong to void |
| 239 | if or eq s 0 ge s get vns 0 |
| 240 | sr_fail vtslot |
| 241 | end |
| 242 | put vtop 0 get vso s |
| 243 | put vns 0 s |
| 244 | end |
| 245 | fn sv_off s ulong to ulong |
| 246 | ret get vso s |
| 247 | end |
| 248 | fn sv_n s ulong to ulong |
| 249 | ret get vsn s |
| 250 | end |
| 251 | note sv_push: append one byte to the newest slot. |
| 252 | fn sv_push c uchar to void |
| 253 | wt = get vtop 0 |
| 254 | if ge wt VACAP |
| 255 | sr_fail vtcap |
| 256 | end |
| 257 | put va wt c |
| 258 | put vtop 0 add wt 1 |
| 259 | ws = sub get vns 0 1 |
| 260 | put vsn ws add get vsn ws 1 |
| 261 | end |
| 262 | fn sv_pusht s view uchar * to void |
| 263 | for wj ulong len s |
| 264 | sv_push get s wj |
| 265 | end |
| 266 | end |
| 267 | fn sv_pusha off ulong cnt ulong to void |
| 268 | wb = ulong 0 |
| 269 | while lt wb cnt bound 64 |
| 270 | wm = sub cnt wb |
| 271 | if gt wm 524288 |
| 272 | wm = ulong 524288 |
| 273 | end |
| 274 | wo = add off wb |
| 275 | for wj ulong wm |
| 276 | sv_push get va add wo wj |
| 277 | end |
| 278 | wb = add wb wm |
| 279 | end |
| 280 | end |
| 281 | note sv_trunc: the newest slot s keeps its first n bytes (in-place transforms shrink it). |
| 282 | fn sv_trunc s ulong n ulong to void |
| 283 | if ne s sub get vns 0 1 |
| 284 | sr_fail vtslot |
| 285 | end |
| 286 | if gt n get vsn s |
| 287 | sr_fail vtslot |
| 288 | end |
| 289 | put vsn s n |
| 290 | put vtop 0 add get vso s n |
| 291 | end |
| 292 | |
| 293 | note ---------------------------------------------------------------- reading |
| 294 | note sv_load reads vpath whole into a new slot. Missing or unopenable: 0 (callers report the |
| 295 | note rule). Open but unreadable, a directory, or invalid UTF-8: rule U8/FILE_READ and 0. |
| 296 | fn sv_rawload to ulong |
| 297 | wpr = new uchar 1 0 |
| 298 | wfd = fopen vpath 0 get vpn 0 524288 0 |
| 299 | if lt wfd 0 |
| 300 | ret 0 |
| 301 | end |
| 302 | ws = sv_snew |
| 303 | wb = get vtop 0 |
| 304 | wt = wb |
| 305 | wok = true |
| 306 | while true bound 999999 |
| 307 | if ge wt VACAP |
| 308 | break |
| 309 | end |
| 310 | wk = read int wfd va wt sub VACAP wt |
| 311 | if lt wk 0 |
| 312 | wok = false |
| 313 | break |
| 314 | end |
| 315 | if eq wk 0 |
| 316 | break |
| 317 | end |
| 318 | wt = add wt ulong wk |
| 319 | end |
| 320 | if and wok ge wt VACAP |
| 321 | wk = read int wfd wpr 0 1 |
| 322 | if ne wk 0 |
| 323 | wk = fclose int wfd |
| 324 | sr_fail vtcap |
| 325 | end |
| 326 | end |
| 327 | wk = fclose int wfd |
| 328 | if not wok |
| 329 | sv_drop ws |
| 330 | sv_fail vtrd vtopn |
| 331 | ret 0 |
| 332 | end |
| 333 | put vsn ws sub wt wb |
| 334 | put vtop 0 wt |
| 335 | ret ws |
| 336 | end |
| 337 | fn sv_load to ulong |
| 338 | ws = sv_rawload |
| 339 | if eq ws 0 |
| 340 | ret 0 |
| 341 | end |
| 342 | if not sv_utf8 ws |
| 343 | sv_drop ws |
| 344 | sv_fail vtu8 vtu8w |
| 345 | ret 0 |
| 346 | end |
| 347 | ret ws |
| 348 | end |
| 349 | fn sv_loadrel rel view uchar * to ulong |
| 350 | sv_prel rel |
| 351 | ret sv_load |
| 352 | end |
| 353 | |
| 354 | note ---------------------------------------------------------------- bytes |
| 355 | fn sv_b s ulong i ulong to uchar |
| 356 | ret get va add get vso s i |
| 357 | end |
| 358 | fn sv_eqm off ulong pat view uchar * to bool |
| 359 | for wj ulong len pat |
| 360 | if ne get va add off wj get pat wj |
| 361 | ret false |
| 362 | end |
| 363 | end |
| 364 | ret true |
| 365 | end |
| 366 | fn sv_lc c uchar to uchar |
| 367 | if and ge c 65 le c 90 |
| 368 | ret add c 32 |
| 369 | end |
| 370 | ret c |
| 371 | end |
| 372 | note sv_eqmc: ASCII case-insensitive; pat is already lowercase (Rust to_ascii_lowercase + find). |
| 373 | fn sv_eqmc off ulong pat view uchar * to bool |
| 374 | for wj ulong len pat |
| 375 | if ne sv_lc get va add off wj get pat wj |
| 376 | ret false |
| 377 | end |
| 378 | end |
| 379 | ret true |
| 380 | end |
| 381 | fn sv_eqat s ulong i ulong pat view uchar * to bool |
| 382 | if gt len pat get vsn s |
| 383 | ret false |
| 384 | end |
| 385 | if gt i sub get vsn s len pat |
| 386 | ret false |
| 387 | end |
| 388 | ret sv_eqm add get vso s i pat |
| 389 | end |
| 390 | fn sv_eqatc s ulong i ulong pat view uchar * to bool |
| 391 | if gt len pat get vsn s |
| 392 | ret false |
| 393 | end |
| 394 | if gt i sub get vsn s len pat |
| 395 | ret false |
| 396 | end |
| 397 | ret sv_eqmc add get vso s i pat |
| 398 | end |
| 399 | note sv_findn: first i in [from, lim - len pat] where pat starts; VNONE when none. ci selects |
| 400 | note ASCII case-insensitive matching. Two-level loop: 64 x 262144 >= the 16 MiB arena. |
| 401 | fn sv_findx s ulong pat view uchar * from ulong lim ulong ci bool to ulong |
| 402 | if gt lim get vsn s |
| 403 | lim = get vsn s |
| 404 | end |
| 405 | if gt len pat lim |
| 406 | ret VNONE |
| 407 | end |
| 408 | note lim becomes the last start index and from the cursor (no extra names: register budget). |
| 409 | lim = sub lim len pat |
| 410 | wb = get vso s |
| 411 | while le from lim bound 64 |
| 412 | wk = ulong 0 |
| 413 | while and le from lim lt wk 262144 bound 262144 |
| 414 | if sv_eqci add wb from pat ci |
| 415 | ret from |
| 416 | end |
| 417 | from = add from 1 |
| 418 | wk = add wk 1 |
| 419 | end |
| 420 | end |
| 421 | ret VNONE |
| 422 | end |
| 423 | note sv_eqci: sv_eqmc when ci, else sv_eqm (split out of sv_findx for registers). |
| 424 | fn sv_eqci b ulong pat view uchar * ci bool to bool |
| 425 | if ci |
| 426 | ret sv_eqmc b pat |
| 427 | end |
| 428 | ret sv_eqm b pat |
| 429 | end |
| 430 | fn sv_findn s ulong pat view uchar * from ulong lim ulong to ulong |
| 431 | ret sv_findx s pat from lim false |
| 432 | end |
| 433 | fn sv_find s ulong pat view uchar * from ulong to ulong |
| 434 | ret sv_findx s pat from get vsn s false |
| 435 | end |
| 436 | fn sv_findc s ulong pat view uchar * from ulong to ulong |
| 437 | ret sv_findx s pat from get vsn s true |
| 438 | end |
| 439 | fn sv_has s ulong pat view uchar * to bool |
| 440 | ret ne sv_find s pat 0 VNONE |
| 441 | end |
| 442 | fn sv_hasc s ulong pat view uchar * to bool |
| 443 | ret ne sv_findc s pat 0 VNONE |
| 444 | end |
| 445 | fn sv_hasn s ulong pat view uchar * from ulong lim ulong to bool |
| 446 | ret ne sv_findn s pat from lim VNONE |
| 447 | end |
| 448 | note sv_count: non-overlapping occurrences (Rust str::matches().count()). |
| 449 | fn sv_count s ulong pat view uchar * to ulong |
| 450 | wc = ulong 0 |
| 451 | wi = ulong 0 |
| 452 | wstep = len pat |
| 453 | if eq wstep 0 |
| 454 | wstep = 1 |
| 455 | end |
| 456 | while true bound 999999 |
| 457 | wf = sv_find s pat wi |
| 458 | if eq wf VNONE |
| 459 | ret wc |
| 460 | end |
| 461 | wc = add wc 1 |
| 462 | wi = add wf wstep |
| 463 | end |
| 464 | ret wc |
| 465 | end |
| 466 | fn sv_rfind s ulong pat view uchar * to ulong |
| 467 | wl = VNONE |
| 468 | wi = ulong 0 |
| 469 | while true bound 999999 |
| 470 | wf = sv_find s pat wi |
| 471 | if eq wf VNONE |
| 472 | ret wl |
| 473 | end |
| 474 | wl = wf |
| 475 | wi = add wf 1 |
| 476 | end |
| 477 | ret wl |
| 478 | end |
| 479 | note sv_finda: first i in [from, lim - an] where the arena bytes [aoff, aoff + an) occur. |
| 480 | fn sv_finda s ulong from ulong lim ulong aoff ulong an ulong to ulong |
| 481 | if gt lim get vsn s |
| 482 | lim = get vsn s |
| 483 | end |
| 484 | if gt an lim |
| 485 | ret VNONE |
| 486 | end |
| 487 | wl = sub lim an |
| 488 | wb = get vso s |
| 489 | wi = from |
| 490 | while le wi wl bound 64 |
| 491 | wk = ulong 0 |
| 492 | while and le wi wl lt wk 262144 bound 262144 |
| 493 | if sv_amatch add wb wi aoff an |
| 494 | ret wi |
| 495 | end |
| 496 | wi = add wi 1 |
| 497 | wk = add wk 1 |
| 498 | end |
| 499 | end |
| 500 | ret VNONE |
| 501 | end |
| 502 | note sv_amatch: the an arena bytes at bp equal those at aoff (split out of sv_finda for registers). |
| 503 | fn sv_amatch bp ulong aoff ulong an ulong to bool |
| 504 | for wj ulong an |
| 505 | if ne get va add bp wj get va add aoff wj |
| 506 | ret false |
| 507 | end |
| 508 | end |
| 509 | ret true |
| 510 | end |
| 511 | note sv_amv1: one block of sv_amove (n below the 1,000,000-pass for bound). |
| 512 | fn sv_amv1 dst ulong src ulong n ulong to void |
| 513 | for wj ulong n |
| 514 | put va add dst wj get va add src wj |
| 515 | end |
| 516 | end |
| 517 | note sv_amove: forward byte copy of n arena bytes from src to dst (dst <= src, or no overlap). |
| 518 | fn sv_amove dst ulong src ulong n ulong to void |
| 519 | wb = ulong 0 |
| 520 | while lt wb n bound 64 |
| 521 | wm = sub n wb |
| 522 | if gt wm 524288 |
| 523 | wm = ulong 524288 |
| 524 | end |
| 525 | sv_amv1 add dst wb add src wb wm |
| 526 | wb = add wb wm |
| 527 | end |
| 528 | end |
| 529 | note sv_findb: first index of byte c in slot s, VNONE when absent. |
| 530 | fn sv_findb s ulong c uchar to ulong |
| 531 | wn = get vsn s |
| 532 | wb = get vso s |
| 533 | wi = ulong 0 |
| 534 | while lt wi wn bound 64 |
| 535 | wk = ulong 0 |
| 536 | while and lt wi wn lt wk 262144 bound 262144 |
| 537 | if eq get va add wb wi c |
| 538 | ret wi |
| 539 | end |
| 540 | wi = add wi 1 |
| 541 | wk = add wk 1 |
| 542 | end |
| 543 | end |
| 544 | ret VNONE |
| 545 | end |
| 546 | fn sv_starts s ulong pat view uchar * to bool |
| 547 | ret sv_eqat s 0 pat |
| 548 | end |
| 549 | fn sv_ends s ulong pat view uchar * to bool |
| 550 | if gt len pat get vsn s |
| 551 | ret false |
| 552 | end |
| 553 | ret sv_eqat s sub get vsn s len pat pat |
| 554 | end |
| 555 | note sv_dash: U+2014 (E2 80 94) or U+2013 (E2 80 93) anywhere in [from, lim). |
| 556 | fn sv_dashn s ulong from ulong lim ulong to bool |
| 557 | if gt lim get vsn s |
| 558 | lim = get vsn s |
| 559 | end |
| 560 | wb = get vso s |
| 561 | wi = from |
| 562 | while lt add wi 2 lim bound 64 |
| 563 | wk = ulong 0 |
| 564 | while and lt add wi 2 lim lt wk 262144 bound 262144 |
| 565 | if eq get va add wb wi 226 |
| 566 | if eq get va add add wb wi 1 128 |
| 567 | wc = get va add add wb wi 2 |
| 568 | if or eq wc 148 eq wc 147 |
| 569 | ret true |
| 570 | end |
| 571 | end |
| 572 | end |
| 573 | wi = add wi 1 |
| 574 | wk = add wk 1 |
| 575 | end |
| 576 | end |
| 577 | ret false |
| 578 | end |
| 579 | fn sv_dash s ulong to bool |
| 580 | ret sv_dashn s 0 get vsn s |
| 581 | end |
| 582 | |
| 583 | note ---------------------------------------------------------------- UTF-8 |
| 584 | note Strict RFC 3629: no overlongs, no surrogates, nothing above U+10FFFF, no truncation. |
| 585 | fn sv_u8one s ulong i ulong n ulong to ulong |
| 586 | c = sv_b s i |
| 587 | if lt c 128 |
| 588 | ret 1 |
| 589 | end |
| 590 | wl = ulong 0 |
| 591 | wlo = uchar 128 |
| 592 | whi = uchar 191 |
| 593 | if and ge c 194 le c 223 |
| 594 | wl = 2 |
| 595 | end |
| 596 | if and ge c 224 le c 239 |
| 597 | wl = 3 |
| 598 | if eq c 224 |
| 599 | wlo = 160 |
| 600 | end |
| 601 | if eq c 237 |
| 602 | whi = 159 |
| 603 | end |
| 604 | end |
| 605 | if and ge c 240 le c 244 |
| 606 | wl = 4 |
| 607 | if eq c 240 |
| 608 | wlo = 144 |
| 609 | end |
| 610 | if eq c 244 |
| 611 | whi = 143 |
| 612 | end |
| 613 | end |
| 614 | if eq wl 0 |
| 615 | ret 0 |
| 616 | end |
| 617 | if gt add i wl n |
| 618 | ret 0 |
| 619 | end |
| 620 | wc = sv_b s add i 1 |
| 621 | if or lt wc wlo gt wc whi |
| 622 | ret 0 |
| 623 | end |
| 624 | for wj ulong sub wl 2 |
| 625 | wc = sv_b s add add i 2 wj |
| 626 | if or lt wc 128 gt wc 191 |
| 627 | ret 0 |
| 628 | end |
| 629 | end |
| 630 | ret wl |
| 631 | end |
| 632 | fn sv_utf8 s ulong to bool |
| 633 | wn = get vsn s |
| 634 | wi = ulong 0 |
| 635 | while lt wi wn bound 64 |
| 636 | wk = ulong 0 |
| 637 | while and lt wi wn lt wk 262144 bound 262144 |
| 638 | wl = sv_u8one s wi wn |
| 639 | if eq wl 0 |
| 640 | ret false |
| 641 | end |
| 642 | wi = add wi wl |
| 643 | wk = add wk 1 |
| 644 | end |
| 645 | end |
| 646 | ret true |
| 647 | end |
| 648 | note sv_cp: the code point at i (the slot is valid UTF-8; i is a character start). |
| 649 | fn sv_cp s ulong i ulong to ulong |
| 650 | c = ulong sv_b s i |
| 651 | if lt c 128 |
| 652 | ret c |
| 653 | end |
| 654 | if lt c 224 |
| 655 | ret or shl and c 31 6 and ulong sv_b s add i 1 63 |
| 656 | end |
| 657 | if lt c 240 |
| 658 | wv = or shl and c 15 12 shl and ulong sv_b s add i 1 63 6 |
| 659 | ret or wv and ulong sv_b s add i 2 63 |
| 660 | end |
| 661 | wv = or shl and c 7 18 shl and ulong sv_b s add i 1 63 12 |
| 662 | wv = or wv shl and ulong sv_b s add i 2 63 6 |
| 663 | ret or wv and ulong sv_b s add i 3 63 |
| 664 | end |
| 665 | fn sv_cpl s ulong i ulong to ulong |
| 666 | c = sv_b s i |
| 667 | if lt c 128 |
| 668 | ret 1 |
| 669 | end |
| 670 | if lt c 224 |
| 671 | ret 2 |
| 672 | end |
| 673 | if lt c 240 |
| 674 | ret 3 |
| 675 | end |
| 676 | ret 4 |
| 677 | end |
| 678 | note sv_pv: start of the character before i (i > 0). |
| 679 | fn sv_pv s ulong i ulong to ulong |
| 680 | wi = sub i 1 |
| 681 | while gt wi 0 bound 3 |
| 682 | if ne and sv_b s wi 192 128 |
| 683 | ret wi |
| 684 | end |
| 685 | wi = sub wi 1 |
| 686 | end |
| 687 | ret wi |
| 688 | end |
| 689 | |
| 690 | note ---------------------------------------------------------------- Unicode classes |
| 691 | fn sv_upar src view uchar * which uchar to void |
| 692 | wv = ulong 0 |
| 693 | wd = false |
| 694 | wk = ulong 0 |
| 695 | for wj ulong len src |
| 696 | c = get src wj |
| 697 | if and ge c 48 le c 57 |
| 698 | wv = add mul wv 10 ulong sub c 48 |
| 699 | wd = true |
| 700 | else |
| 701 | if wd |
| 702 | if eq which 0 |
| 703 | put vuwr wk wv |
| 704 | end |
| 705 | if eq which 1 |
| 706 | put vudr wk wv |
| 707 | end |
| 708 | if eq which 2 |
| 709 | put vusr wk wv |
| 710 | end |
| 711 | wk = add wk 1 |
| 712 | end |
| 713 | wv = 0 |
| 714 | wd = false |
| 715 | end |
| 716 | end |
| 717 | if wd |
| 718 | if eq which 0 |
| 719 | put vuwr wk wv |
| 720 | end |
| 721 | if eq which 1 |
| 722 | put vudr wk wv |
| 723 | end |
| 724 | if eq which 2 |
| 725 | put vusr wk wv |
| 726 | end |
| 727 | end |
| 728 | end |
| 729 | fn sv_uinit to void |
| 730 | sv_upar vuw 0 |
| 731 | sv_upar vud 1 |
| 732 | sv_upar vus 2 |
| 733 | end |
| 734 | note Binary search of sorted disjoint ranges; one function per table (globals are not passed). |
| 735 | fn sv_inw cp ulong to bool |
| 736 | if lt cp 128 |
| 737 | ret or or and ge cp 48 le cp 57 and ge cp 65 le cp 90 or eq cp 95 and ge cp 97 le cp 122 |
| 738 | end |
| 739 | wlo = ulong 0 |
| 740 | whi = VUWN |
| 741 | while lt wlo whi bound 20 |
| 742 | wm = div add wlo whi 2 |
| 743 | if lt cp get vuwr mul wm 2 |
| 744 | whi = wm |
| 745 | else |
| 746 | if gt cp get vuwr add mul wm 2 1 |
| 747 | wlo = add wm 1 |
| 748 | else |
| 749 | ret true |
| 750 | end |
| 751 | end |
| 752 | end |
| 753 | ret false |
| 754 | end |
| 755 | fn sv_ind cp ulong to bool |
| 756 | if lt cp 128 |
| 757 | ret and ge cp 48 le cp 57 |
| 758 | end |
| 759 | wlo = ulong 0 |
| 760 | whi = VUDN |
| 761 | while lt wlo whi bound 20 |
| 762 | wm = div add wlo whi 2 |
| 763 | if lt cp get vudr mul wm 2 |
| 764 | whi = wm |
| 765 | else |
| 766 | if gt cp get vudr add mul wm 2 1 |
| 767 | wlo = add wm 1 |
| 768 | else |
| 769 | ret true |
| 770 | end |
| 771 | end |
| 772 | end |
| 773 | ret false |
| 774 | end |
| 775 | fn sv_ins cp ulong to bool |
| 776 | for wj ulong VUSN |
| 777 | if and ge cp get vusr mul wj 2 le cp get vusr add mul wj 2 1 |
| 778 | ret true |
| 779 | end |
| 780 | end |
| 781 | ret false |
| 782 | end |
| 783 | note sv_fold: simple case folding onto ASCII as regex (?i) does: A-Z, U+212A KELVIN SIGN -> k, |
| 784 | note U+017F LATIN SMALL LETTER LONG S -> s (regex-syntax case_folding_simple.rs). |
| 785 | fn sv_fold cp ulong to ulong |
| 786 | if and ge cp 65 le cp 90 |
| 787 | ret add cp 32 |
| 788 | end |
| 789 | if eq cp 8490 |
| 790 | ret 107 |
| 791 | end |
| 792 | if eq cp 383 |
| 793 | ret 115 |
| 794 | end |
| 795 | ret cp |
| 796 | end |
| 797 | fn sv_wat s ulong i ulong to bool |
| 798 | if ge i get vsn s |
| 799 | ret false |
| 800 | end |
| 801 | ret sv_inw sv_cp s i |
| 802 | end |
| 803 | fn sv_wbef s ulong i ulong to bool |
| 804 | if eq i 0 |
| 805 | ret false |
| 806 | end |
| 807 | ret sv_inw sv_cp s sv_pv s i |
| 808 | end |
| 809 | note sv_wb: regex \b (Unicode word boundary) at byte index i, a character start. |
| 810 | fn sv_wb s ulong i ulong to bool |
| 811 | ret ne sv_wbef s i sv_wat s i |
| 812 | end |
| 813 | fn sv_sat s ulong i ulong to bool |
| 814 | if ge i get vsn s |
| 815 | ret false |
| 816 | end |
| 817 | ret sv_ins sv_cp s i |
| 818 | end |
| 819 | |
| 820 | note ---------------------------------------------------------------- tree |
| 821 | note sv_walk lists every entry under the root once (directories followed through symlinks, |
| 822 | note as Rust Path::is_dir does). Entry 0 is the root itself. An unreadable directory refuses |
| 823 | note the run (DIRECTORY_READ), never a silently shorter list. |
| 824 | fn sv_fadd rel0 ulong reln ulong name ulong namen ulong to ulong |
| 825 | wq = get vfc 0 |
| 826 | if ge wq VFILES |
| 827 | sr_fail vtfil |
| 828 | end |
| 829 | wo = get vfpn 0 |
| 830 | wneed = add add reln namen 1 |
| 831 | if gt wneed sub VPOOL wo |
| 832 | sr_fail vtfil |
| 833 | end |
| 834 | for wj ulong reln |
| 835 | put vfp add wo wj get vfp add rel0 wj |
| 836 | end |
| 837 | wp = add wo reln |
| 838 | if gt reln 0 |
| 839 | put vfp wp 47 |
| 840 | wp = add wp 1 |
| 841 | end |
| 842 | sv_fname wp name namen |
| 843 | wp = add wp namen |
| 844 | put vfo wq wo |
| 845 | put vfl wq sub wp wo |
| 846 | put vfpn 0 wp |
| 847 | put vfc 0 add wq 1 |
| 848 | ret wq |
| 849 | end |
| 850 | note sv_fname: copy namen dirent-name bytes from vdb at name to vfp at wp (split out of sv_fadd). |
| 851 | fn sv_fname wp ulong name ulong namen ulong to void |
| 852 | for wj ulong namen |
| 853 | put vfp add wp wj get vdb add name wj |
| 854 | end |
| 855 | end |
| 856 | fn sv_pfile q ulong to void |
| 857 | sv_proot |
| 858 | for wj ulong get vfl q |
| 859 | sv_pch get vfp add get vfo q wj |
| 860 | end |
| 861 | sv_ctxp |
| 862 | end |
| 863 | fn sv_kind q ulong to void |
| 864 | sv_pfile q |
| 865 | if sv_isdir |
| 866 | put vfk q 2 |
| 867 | else |
| 868 | put vfk q 1 |
| 869 | end |
| 870 | end |
| 871 | fn sv_dirent q ulong wa ulong to ulong |
| 872 | wr = or ulong get vdb add wa 16 shl ulong get vdb add wa 17 8 |
| 873 | wn = ulong 0 |
| 874 | while lt add add wa 19 wn add wa wr bound 4096 |
| 875 | if eq get vdb add add wa 19 wn 0 |
| 876 | break |
| 877 | end |
| 878 | wn = add wn 1 |
| 879 | end |
| 880 | wdot = and eq wn 1 eq get vdb add wa 19 46 |
| 881 | wdd = and eq wn 2 eq get vdb add wa 19 46 |
| 882 | wdd = and wdd eq get vdb add wa 20 46 |
| 883 | if not or wdot wdd |
| 884 | wc = sv_fadd get vfo q get vfl q add wa 19 wn |
| 885 | sv_kind wc |
| 886 | end |
| 887 | ret wr |
| 888 | end |
| 889 | fn sv_readdir q ulong to void |
| 890 | sv_pfile q |
| 891 | wfd = fopen vpath 0 get vpn 0 540672 0 |
| 892 | if lt wfd 0 |
| 893 | sr_fail vtdir |
| 894 | end |
| 895 | while true bound 65536 |
| 896 | wk = getdnts int wfd vdb 0 len vdb |
| 897 | if lt wk 0 |
| 898 | sr_fail vtdir |
| 899 | end |
| 900 | if eq wk 0 |
| 901 | break |
| 902 | end |
| 903 | wat = ulong 0 |
| 904 | while lt wat ulong wk bound 32768 |
| 905 | wr = sv_dirent q wat |
| 906 | if eq wr 0 |
| 907 | sr_fail vtdir |
| 908 | end |
| 909 | wat = add wat wr |
| 910 | end |
| 911 | end |
| 912 | wk = fclose int wfd |
| 913 | if ne wk 0 |
| 914 | sr_fail vtdir |
| 915 | end |
| 916 | end |
| 917 | fn sv_walk to void |
| 918 | put vfc 0 0 |
| 919 | put vfpn 0 0 |
| 920 | put vfo 0 0 |
| 921 | put vfl 0 0 |
| 922 | put vfc 0 1 |
| 923 | put vfk 0 2 |
| 924 | wq = ulong 0 |
| 925 | while lt wq get vfc 0 bound 65536 |
| 926 | if eq get vfk wq 2 |
| 927 | sv_readdir wq |
| 928 | end |
| 929 | wq = add wq 1 |
| 930 | end |
| 931 | end |
| 932 | fn sv_fext q ulong ext view uchar * to bool |
| 933 | wl = get vfl q |
| 934 | if gt len ext wl |
| 935 | ret false |
| 936 | end |
| 937 | wo = add get vfo q sub wl len ext |
| 938 | for wj ulong len ext |
| 939 | if ne get vfp add wo wj get ext wj |
| 940 | ret false |
| 941 | end |
| 942 | end |
| 943 | ret true |
| 944 | end |
| 945 | fn sv_fis q ulong rel view uchar * to bool |
| 946 | if ne len rel get vfl q |
| 947 | ret false |
| 948 | end |
| 949 | for wj ulong len rel |
| 950 | if ne get vfp add get vfo q wj get rel wj |
| 951 | ret false |
| 952 | end |
| 953 | end |
| 954 | ret true |
| 955 | end |
| 956 | fn sv_fload q ulong to ulong |
| 957 | sv_pfile q |
| 958 | ret sv_load |
| 959 | end |
| 960 | |
| 961 | note ---------------------------------------------------------------- tables |
| 962 | note A table is a text of entries separated by LF; sv_tcount / sv_tent read it. Entries are |
| 963 | note passed on as `part` views of the table (OM-2 part of a text is a view). |
| 964 | fn sv_tcount t view uchar * to ulong |
| 965 | wc = ulong 0 |
| 966 | for wj ulong len t |
| 967 | if eq get t wj 10 |
| 968 | wc = add wc 1 |
| 969 | end |
| 970 | end |
| 971 | ret wc |
| 972 | end |
| 973 | note sv_tat: byte offset of entry k (entries end with LF). |
| 974 | fn sv_tat t view uchar * k ulong to ulong |
| 975 | wc = ulong 0 |
| 976 | if eq k 0 |
| 977 | ret 0 |
| 978 | end |
| 979 | for wj ulong len t |
| 980 | if eq get t wj 10 |
| 981 | wc = add wc 1 |
| 982 | if eq wc k |
| 983 | ret add wj 1 |
| 984 | end |
| 985 | end |
| 986 | end |
| 987 | ret len t |
| 988 | end |
| 989 | fn sv_tlen t view uchar * wa ulong to ulong |
| 990 | wn = ulong 0 |
| 991 | while lt add wa wn len t bound 65536 |
| 992 | if eq get t add wa wn 10 |
| 993 | ret wn |
| 994 | end |
| 995 | wn = add wn 1 |
| 996 | end |
| 997 | ret wn |
| 998 | end |
| 999 | fn sv_vinit to void |
| 1000 | put vtop 0 0 |
| 1001 | put vns 0 1 |
| 1002 | put vso 0 0 |
| 1003 | put vsn 0 0 |
| 1004 | put vfail 0 0 |
| 1005 | put vcn 0 0 |
| 1006 | sv_uinit |
| 1007 | end |