code wiki / (root) / nx_colread_lib.nx

nx_colread_lib.nx source

↩ module page · 64 lines · 2633 B

1// nx_colread_lib.nx -- ONE delimiter-separated column reader (2026-09-17). The estate keeps rows as tab columns (manifests, 2// planes, confs) and as pipe columns (oracle rows, plan and judge files), and organs hand-rolled the same scanner each 3// time: the photo ruler's pm_field, the asset page's apl_col, the plan runner's pr_cols. This is the shared form: the 4// delimiter is a parameter, the line walker stops at NL and the content end drops a CR (a data file may arrive from any 5// host, and a CR left on the last column compares unequal while printing identically). Consumers: nx_mediaflow_lib. 6// Owed: fold pm_field and apl_col onto cr_col when their closures next ship (queued on aesthetictwin.plan). 7// license_tier: ORIGINAL No hw writes (Rule 26). 8import "nx_syscalls.nx" 9 10const CR_NL: i64 = 10 11const CR_CR: i64 = 13 12 13func cr_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 14// the index of the NL that ends the line starting at i, or n when the last line has none 15func cr_eol(buf: *u8, n: i64, i: i64) -> i64 { 16 var le: i64 = i 17 var scanning: i64 = 1 18 while scanning == 1 { 19 if le >= n { scanning = 0 } else { 20 if (buf[le] as i64) == CR_NL { scanning = 0 } else { le = le + 1 } 21 } 22 } 23 return le 24} 25// the end of the line's CONTENT: the eol, minus a CR when the line came from a CRLF host 26func cr_trim(buf: *u8, ls: i64, le: i64) -> i64 { 27 if le > ls { if (buf[le - 1] as i64) == CR_CR { return le - 1 } } 28 return le 29} 30// column k of the line [ls,le) by delimiter into span[0..1] as [start,end); 1 when the line has that column 31func cr_col(buf: *u8, ls: i64, le: i64, delim: i64, k: i64, span: *i64) -> i64 { 32 span[0] = 0 - 1 33 span[1] = 0 - 1 34 var col: i64 = 0 35 var cs: i64 = ls 36 var i: i64 = ls 37 while i <= le { 38 var at_end: i64 = 0 39 if i == le { at_end = 1 } else { if (buf[i] as i64) == delim { at_end = 1 } } 40 if at_end == 1 { 41 if col == k { 42 span[0] = cs 43 span[1] = i 44 return 1 45 } 46 col = col + 1 47 cs = i + 1 48 } 49 i = i + 1 50 } 51 return 0 52} 53// 1 when buf[a..b) equals the first slen bytes of s exactly 54func cr_eq(buf: *u8, a: i64, b: i64, s: *u8, slen: i64) -> i64 { 55 if b - a != slen { return 0 } 56 var i: i64 = 0 57 while i < slen { 58 if buf[a + i] != s[i] { return 0 } 59 i = i + 1 60 } 61 return 1 62} 63// 1 when buf[a..b) equals the NUL-terminated literal exactly 64func cr_is(buf: *u8, a: i64, b: i64, lit: *u8) -> i64 { return cr_eq(buf, a, b, lit, cr_slen(lit)) }