code wiki / (root) / nx_sota_why_lib.nx

nx_sota_why_lib.nx source

↩ module page · 39 lines · 1820 B

1// nx_sota_why_lib.nx -- the two pure helpers behind nx_sota_why, in a lib so the ORGAN AND ITS GATE RUN 2// THE SAME CODE. They are here rather than inline for one reason: a gate cannot import a program (a 3// second main), so a helper that lives in the organ is a helper no tooth can reach, and the only tests 4// left available are end-to-end ones that cannot say WHICH part broke. 5import "nx_syscalls.nx" 6import "nx_string_ops.nx" 7 8// Append a NUL-terminated string and keep the result NUL-terminated. Returns the new offset. 9func sw_cat(d: *u8, o: i64, s: *u8) -> i64 { 10 var i: i64 = 0 11 while s[i] != (0 as u8) { d[o + i] = s[i]; i = i + 1 } 12 d[o + i] = 0 as u8 13 return o + i 14} 15 16// Does the row [lo, hi) carry this token? 17// BOUNDED ON BOTH ENDS ON PURPOSE. The ledger holds every domain in one buffer with rows adjacent, so an 18// unbounded search finds the NEXT row class and answers, confidently, about a neighbouring domain. That 19// failure produces a plausible answer rather than an error, which is the shape nobody catches by reading. 20func sw_row_has(buf: *u8, lo: i64, hi: i64, tok: *u8) -> i64 { 21 if hi <= lo { return 0 } 22 let base: *u8 = ((buf as i64) + lo) as *u8 23 if nx_str_find(base, hi - lo, tok, nx_str_len(tok)) >= 0 { return 1 } 24 return 0 25} 26 27// End offset of the row starting at `from`: the next newline, or n. 28// The stop flag is deliberately NOT the cursor. Writing a loop-exit sentinel into the search cursor 29// erases the answer, and this estate has measured that exact defect four times in a single day. 30func sw_row_end(buf: *u8, from: i64, n: i64) -> i64 { 31 var e: i64 = from 32 var stop: i64 = 0 33 while stop == 0 { 34 if e >= n { stop = 1 } 35 if stop == 0 { if (buf[e] as i64) == 10 { stop = 1 } } 36 if stop == 0 { e = e + 1 } 37 } 38 return e 39}