ref: ea3c3bcded6031ba1f72e1af6683e0c6bcade96d
dir: /str.myr/
use "die.use" use "sys.use" use "types.use" pkg std = const Badchar : char = -1 castto(char) const encode : (buf : byte[,], chr : char -> bool) const decode : (str : byte[,] -> char) const striter : (str : byte[,] -> [char, byte[,]]) const strjoin : (lst : byte[,][,], delim:byte[,] -> byte[,]) const strsep : (str : byte[,], delim:byte[,] -> byte[,][,]) const strbjoin : (lst : byte[,][,], delim:byte[,] -> byte[,]) const strbsep : (str : byte[,], delim:byte[,] -> byte[,][,]) ;; const striter = {str var len var mask var chr var i var c var tmp c = str[0] if c & 0x80 == 0 /* 0b0xxx_xxxx */ len = 1 elif c & 0xe0 == 0xc0 /* 0b110x_xxxx */ len = 2 elif c & 0xf0 == 0xe0 /* 0b1110_xxxx */ len = 3 elif c & 0xf8 == 0xf0 /* 0b1111_0xxx */ len = 4 else /* skip one char forward so we can try resyncing the character stream */ -> (Badchar, str[1,str.len]) ;; mask = (1 << (7 - len)) - 1 chr = (c castto(uint32)) & mask for i = 1; i < len; i++ tmp = str[i] castto(uint32) chr = (chr << 6) | (tmp & 0x3f) ;; -> (chr castto(char), str[len, str.len]) }