utf8.odin 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package utf8
  2. RUNE_ERROR :: '\ufffd';
  3. RUNE_SELF :: 0x80;
  4. RUNE_BOM :: 0xfeff;
  5. RUNE_EOF :: ~rune(0);
  6. MAX_RUNE :: '\U0010ffff';
  7. UTF_MAX :: 4;
  8. SURROGATE_MIN :: 0xd800;
  9. SURROGATE_MAX :: 0xdfff;
  10. T1 :: 0b0000_0000;
  11. TX :: 0b1000_0000;
  12. T2 :: 0b1100_0000;
  13. T3 :: 0b1110_0000;
  14. T4 :: 0b1111_0000;
  15. T5 :: 0b1111_1000;
  16. MASKX :: 0b0011_1111;
  17. MASK2 :: 0b0001_1111;
  18. MASK3 :: 0b0000_1111;
  19. MASK4 :: 0b0000_0111;
  20. RUNE1_MAX :: 1<<7 - 1;
  21. RUNE2_MAX :: 1<<11 - 1;
  22. RUNE3_MAX :: 1<<16 - 1;
  23. // The default lowest and highest continuation byte.
  24. LOCB :: 0b1000_0000;
  25. HICB :: 0b1011_1111;
  26. Accept_Range :: struct {lo, hi: u8};
  27. accept_ranges := [5]Accept_Range{
  28. {0x80, 0xbf},
  29. {0xa0, 0xbf},
  30. {0x80, 0x9f},
  31. {0x90, 0xbf},
  32. {0x80, 0x8f},
  33. };
  34. accept_sizes := [256]u8{
  35. 0x00..0x7f = 0xf0,
  36. 0x80..0xc1 = 0xf1,
  37. 0xc2..0xdf = 0x02,
  38. 0xe0 = 0x13,
  39. 0xe1..0xec = 0x03,
  40. 0xed = 0x23,
  41. 0xee..0xef = 0x03,
  42. 0xf0 = 0x34,
  43. 0xf1..0xf3 = 0x04,
  44. 0xf4 = 0x44,
  45. 0xf5..0xff = 0xf1,
  46. };
  47. encode_rune :: proc(c: rune) -> ([4]u8, int) {
  48. r := c;
  49. buf: [4]u8;
  50. i := u32(r);
  51. mask :: u8(0x3f);
  52. if i <= 1<<7-1 {
  53. buf[0] = u8(r);
  54. return buf, 1;
  55. }
  56. if i <= 1<<11-1 {
  57. buf[0] = 0xc0 | u8(r>>6);
  58. buf[1] = 0x80 | u8(r) & mask;
  59. return buf, 2;
  60. }
  61. // Invalid or Surrogate range
  62. if i > 0x0010ffff ||
  63. (0xd800 <= i && i <= 0xdfff) {
  64. r = 0xfffd;
  65. }
  66. if i <= 1<<16-1 {
  67. buf[0] = 0xe0 | u8(r>>12);
  68. buf[1] = 0x80 | u8(r>>6) & mask;
  69. buf[2] = 0x80 | u8(r) & mask;
  70. return buf, 3;
  71. }
  72. buf[0] = 0xf0 | u8(r>>18);
  73. buf[1] = 0x80 | u8(r>>12) & mask;
  74. buf[2] = 0x80 | u8(r>>6) & mask;
  75. buf[3] = 0x80 | u8(r) & mask;
  76. return buf, 4;
  77. }
  78. decode_rune_in_string :: inline proc(s: string) -> (rune, int) do return decode_rune(cast([]u8)s);
  79. decode_rune :: proc(s: []u8) -> (rune, int) {
  80. n := len(s);
  81. if n < 1 {
  82. return RUNE_ERROR, 0;
  83. }
  84. s0 := s[0];
  85. x := accept_sizes[s0];
  86. if x >= 0xF0 {
  87. mask := rune(x) << 31 >> 31; // NOTE(bill): Create 0x0000 or 0xffff.
  88. return rune(s[0])&~mask | RUNE_ERROR&mask, 1;
  89. }
  90. sz := x & 7;
  91. accept := accept_ranges[x>>4];
  92. if n < int(sz) {
  93. return RUNE_ERROR, 1;
  94. }
  95. b1 := s[1];
  96. if b1 < accept.lo || accept.hi < b1 {
  97. return RUNE_ERROR, 1;
  98. }
  99. if sz == 2 {
  100. return rune(s0&MASK2)<<6 | rune(b1&MASKX), 2;
  101. }
  102. b2 := s[2];
  103. if b2 < LOCB || HICB < b2 {
  104. return RUNE_ERROR, 1;
  105. }
  106. if sz == 3 {
  107. return rune(s0&MASK3)<<12 | rune(b1&MASKX)<<6 | rune(b2&MASKX), 3;
  108. }
  109. b3 := s[3];
  110. if b3 < LOCB || HICB < b3 {
  111. return RUNE_ERROR, 1;
  112. }
  113. return rune(s0&MASK4)<<18 | rune(b1&MASKX)<<12 | rune(b2&MASKX)<<6 | rune(b3&MASKX), 4;
  114. }
  115. decode_last_rune_in_string :: inline proc(s: string) -> (rune, int) do return decode_last_rune(cast([]u8)s);
  116. decode_last_rune :: proc(s: []u8) -> (rune, int) {
  117. r: rune;
  118. size: int;
  119. start, end, limit: int;
  120. end = len(s);
  121. if end == 0 {
  122. return RUNE_ERROR, 0;
  123. }
  124. start = end-1;
  125. r = rune(s[start]);
  126. if r < RUNE_SELF {
  127. return r, 1;
  128. }
  129. limit = max(end - UTF_MAX, 0);
  130. for start-=1; start >= limit; start-=1 {
  131. if rune_start(s[start]) do break;
  132. }
  133. start = max(start, 0);
  134. r, size = decode_rune(s[start:end]);
  135. if start+size != end {
  136. return RUNE_ERROR, 1;
  137. }
  138. return r, size;
  139. }
  140. rune_at_pos :: proc(s: string, pos: int) -> rune {
  141. if pos < 0 {
  142. return RUNE_ERROR;
  143. }
  144. i := 0;
  145. for r in s {
  146. if i == pos {
  147. return r;
  148. }
  149. i += 1;
  150. }
  151. return RUNE_ERROR;
  152. }
  153. rune_string_at_pos :: proc(s: string, pos: int) -> string {
  154. if pos < 0 {
  155. return "";
  156. }
  157. i := 0;
  158. for c, offset in s {
  159. if i == pos {
  160. w := rune_size(c);
  161. return s[offset:][:w];
  162. }
  163. i += 1;
  164. }
  165. return "";
  166. }
  167. rune_at :: proc(s: string, byte_index: int) -> rune {
  168. r, _ := decode_rune_in_string(s[byte_index:]);
  169. return r;
  170. }
  171. // Returns the byte position of rune at position pos in s with an optional start byte position.
  172. // Returns -1 if it runs out of the string.
  173. rune_offset :: proc(s: string, pos: int, start: int = 0) -> int {
  174. if pos < 0 {
  175. return -1;
  176. }
  177. i := 0;
  178. for _, offset in s[start:] {
  179. if i == pos {
  180. return offset+start;
  181. }
  182. i += 1;
  183. }
  184. return -1;
  185. }
  186. valid_rune :: proc(r: rune) -> bool {
  187. if r < 0 {
  188. return false;
  189. } else if SURROGATE_MIN <= r && r <= SURROGATE_MAX {
  190. return false;
  191. } else if r > MAX_RUNE {
  192. return false;
  193. }
  194. return true;
  195. }
  196. valid_string :: proc(s: string) -> bool {
  197. n := len(s);
  198. for i := 0; i < n; {
  199. si := s[i];
  200. if si < RUNE_SELF { // ascii
  201. i += 1;
  202. continue;
  203. }
  204. x := accept_sizes[si];
  205. if x == 0xf1 {
  206. return false;
  207. }
  208. size := int(x & 7);
  209. if i+size > n {
  210. return false;
  211. }
  212. ar := accept_ranges[x>>4];
  213. if b := s[i+1]; b < ar.lo || ar.hi < b {
  214. return false;
  215. } else if size == 2 {
  216. // Okay
  217. } else if c := s[i+2]; c < 0x80 || 0xbf < c {
  218. return false;
  219. } else if size == 3 {
  220. // Okay
  221. } else if d := s[i+3]; b < 0x80 || 0xbf < d {
  222. return false;
  223. }
  224. i += size;
  225. }
  226. return true;
  227. }
  228. rune_start :: inline proc(b: u8) -> bool do return b&0xc0 != 0x80;
  229. rune_count_in_string :: inline proc(s: string) -> int do return rune_count(cast([]u8)s);
  230. rune_count :: proc(s: []u8) -> int {
  231. count := 0;
  232. n := len(s);
  233. for i := 0; i < n; {
  234. defer count += 1;
  235. si := s[i];
  236. if si < RUNE_SELF { // ascii
  237. i += 1;
  238. continue;
  239. }
  240. x := accept_sizes[si];
  241. if x == 0xf1 {
  242. i += 1;
  243. continue;
  244. }
  245. size := int(x & 7);
  246. if i+size > n {
  247. i += 1;
  248. continue;
  249. }
  250. ar := accept_ranges[x>>4];
  251. if b := s[i+1]; b < ar.lo || ar.hi < b {
  252. size = 1;
  253. } else if size == 2 {
  254. // Okay
  255. } else if c := s[i+2]; c < 0x80 || 0xbf < c {
  256. size = 1;
  257. } else if size == 3 {
  258. // Okay
  259. } else if d := s[i+3]; d < 0x80 || 0xbf < d {
  260. size = 1;
  261. }
  262. i += size;
  263. }
  264. return count;
  265. }
  266. rune_size :: proc(r: rune) -> int {
  267. switch {
  268. case r < 0: return -1;
  269. case r <= 1<<7 - 1: return 1;
  270. case r <= 1<<11 - 1: return 2;
  271. case SURROGATE_MIN <= r && r <= SURROGATE_MAX: return -1;
  272. case r <= 1<<16 - 1: return 3;
  273. case r <= MAX_RUNE: return 4;
  274. }
  275. return -1;
  276. }