code wiki / _hdl_build / _wordwrap_authored.nx
_wordwrap_authored.nx source
↩ module page · 27 lines · 1411 B
1// AUTHORED BY THE NISHI BUILDER (wordwrap template) -- reflowable render core (greedy word wrap)
2import "nx_syscalls.nx"
3func word_wrap(s: *u8, n: i64, width: i64, out: *u8) -> i64 {
4 var op: i64=0; var linelen: i64=0; var i: i64=0
5 while i<n {
6 while i<n { if s[i]==0x20 as u8 { i=i+1 } else { break } }
7 if i<n {
8 let ws: i64=i
9 while i<n { if s[i]!=0x20 as u8 { i=i+1 } else { break } }
10 let wl: i64=i-ws
11 if linelen==0 { var k: i64=0; while k<wl { out[op]=s[ws+k]; op=op+1; k=k+1 } linelen=wl }
12 else { if linelen+1+wl<=width { out[op]=0x20 as u8; op=op+1; var k2: i64=0; while k2<wl { out[op]=s[ws+k2]; op=op+1; k2=k2+1 } linelen=linelen+1+wl }
13 else { out[op]=0x0a as u8; op=op+1; var k3: i64=0; while k3<wl { out[op]=s[ws+k3]; op=op+1; k3=k3+1 } linelen=wl } }
14 }
15 }
16 return op
17}
18func main() -> i64 {
19 let li: *i64=sys_mmap(8) as *i64; let s: *u8=sys_read_file("/tmp/ww.input" as *u8, li)
20 let le: *i64=sys_mmap(8) as *i64; let exp: *u8=sys_read_file("/tmp/ww.expected" as *u8, le)
21 if (s as i64)==0 { sys_exit(2) } if (exp as i64)==0 { sys_exit(2) }
22 let out: *u8=sys_mmap(4096)
23 let n: i64=word_wrap(s, li[0], 10, out)
24 var ok: i64=1; if n!=le[0] { ok=0 } var i: i64=0; while i<le[0] { if out[i]!=exp[i] { ok=0 } i=i+1 }
25 if ok==1 { sys_exit(0) } sys_exit(1)
26 return 1
27}