code wiki / _hdl_build / _utf8_authored.nx

_utf8_authored.nx source

↩ module page · 24 lines · 1479 B

1// AUTHORED BY THE NISHI BUILDER (utf8 template) -- UTF-8 codepoint decode/validate, bits-up 2import "nx_syscalls.nx" 3func utf8_next(s: *u8, n: i64, i: i64, outcp: *i64) -> i64 { 4 if i>=n { return 0-1 } 5 let b0: i64=s[i] as i64 6 if b0<0x80 { outcp[0]=b0; return i+1 } 7 if b0<0xC0 { return 0-1 } 8 if b0<0xE0 { if i+1>=n { return 0-1 } let b1: i64=s[i+1] as i64; if (b1&0xC0)!=0x80 { return 0-1 } outcp[0]=((b0&0x1f)<<6)|(b1&0x3f); return i+2 } 9 if b0<0xF0 { if i+2>=n { return 0-1 } let b1: i64=s[i+1] as i64; let b2: i64=s[i+2] as i64; if (b1&0xC0)!=0x80 { return 0-1 } if (b2&0xC0)!=0x80 { return 0-1 } outcp[0]=((b0&0x0f)<<12)|((b1&0x3f)<<6)|(b2&0x3f); return i+3 } 10 if b0<0xF8 { if i+3>=n { return 0-1 } let b1: i64=s[i+1] as i64; let b2: i64=s[i+2] as i64; let b3: i64=s[i+3] as i64; if (b1&0xC0)!=0x80 { return 0-1 } if (b2&0xC0)!=0x80 { return 0-1 } if (b3&0xC0)!=0x80 { return 0-1 } outcp[0]=((b0&0x07)<<18)|((b1&0x3f)<<12)|((b2&0x3f)<<6)|(b3&0x3f); return i+4 } 11 return 0-1 12} 13func main() -> i64 { 14 let cp: *i64=sys_mmap(8) as *i64 15 let a: *u8=sys_mmap(8); a[0]=0x41 as u8 16 let r1: i64=utf8_next(a,1,0,cp); let c1: i64=cp[0] 17 let e: *u8=sys_mmap(8); e[0]=0xC3 as u8; e[1]=0xA9 as u8 18 let r2: i64=utf8_next(e,2,0,cp); let c2: i64=cp[0] 19 let b: *u8=sys_mmap(8); b[0]=0x80 as u8 20 let r3: i64=utf8_next(b,1,0,cp) 21 if r1==1 { if c1==65 { if r2==2 { if c2==233 { if r3==(0-1) { sys_exit(0) } } } } } 22 sys_exit(1) 23 return 1 24}