code wiki / (root) / nx_tabrec.nx

nx_tabrec.nx source

↩ module page · 31 lines · 2015 B

1// nx_tabrec.nx -- canonical tab-delimited record-field parser. Consolidates the four near-identical extractors 2// (bf_field in nx_blocklist_audit, blv_field in nx_blocklist_view, rg_field in nx_recovery_guard, and the seg_store 3// record readers) into ONE. A seg_store record value is tab-separated fields (the nx_app_store convention); this reads 4// them. Pure functions -- no syscalls, no imports -- so it composes into any stack. license_tier: ORIGINAL 5 6// field `idx` of row buf[rs,re): out2[0]=absolute offset, out2[1]=length; returns 1 if found (len 0 = empty field). 7func tr_field(buf: *u8, rs: i64, re: i64, idx: i64, out2: *i64) -> i64 { 8 var i: i64=rs; var f: i64=0; var fs: i64=rs 9 while i<=re { var sep: i64=0; if i==re { sep=1 } else { if buf[i]==0x09 as u8 { sep=1 } } if sep==1 { if f==idx { out2[0]=fs; out2[1]=i-fs; return 1 } f=f+1; fs=i+1 } i=i+1 } 10 return 0 11} 12 13// 1 if field `idx` of buf[0,rlen) equals z (NUL-terminated); out2 is scratch. 14func tr_field_eq(buf: *u8, rlen: i64, idx: i64, z: *u8, out2: *i64) -> i64 { 15 if tr_field(buf,0,rlen,idx,out2)==0 { return 0 } 16 var zl: i64=0; while z[zl]!=(0 as u8) { zl=zl+1 } 17 if out2[1]!=zl { return 0 } 18 var c: i64=0; while c<zl { if buf[out2[0]+c]!=z[c] { return 0 } c=c+1 } 19 return 1 20} 21 22// parse a non-negative decimal integer at buf[off..off+len) 23func tr_atoi(buf: *u8, off: i64, len: i64) -> i64 { var v: i64=0; var i: i64=0; while i<len { let d: i64=buf[off+i] as i64; if d>=48 { if d<=57 { v=v*10+(d-48) } } i=i+1 } return v } 24 25// ---- record builders (canonical; retire the ap/aptab/apnl copies) ---- 26// append NUL-terminated string s to buf at off; returns new off 27func tr_cat(buf: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { buf[off+i]=s[i]; i=i+1 } return off+i } 28// append a field separator (tab) 29func tr_tab(buf: *u8, off: i64) -> i64 { buf[off]=0x09 as u8; return off+1 } 30// append a record separator (newline) 31func tr_nl(buf: *u8, off: i64) -> i64 { buf[off]=0x0a as u8; return off+1 }