aboutsummaryrefslogtreecommitdiff
path: root/src/utf8.c
blob: e144c72150a2869098edbe1c48ea8094d8cfcc79 (plain)
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <assert.h>
  4. #include "utf8.h"
  5. static const int8_t utf8proc_utf8class[256] = {
  6. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  7. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  8. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  9. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  10. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  11. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  12. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  13. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  14. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  15. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  19. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  20. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  21. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
  22. static void encode_unknown(strbuf *buf)
  23. {
  24. static const uint8_t repl[] = {239, 191, 189};
  25. strbuf_put(buf, repl, 3);
  26. }
  27. int utf8proc_charlen(const uint8_t *str, int str_len)
  28. {
  29. int length, i;
  30. if (!str_len)
  31. return 0;
  32. length = utf8proc_utf8class[str[0]];
  33. if (!length)
  34. return -1;
  35. if (str_len >= 0 && length > str_len)
  36. return -str_len;
  37. for (i = 1; i < length; i++) {
  38. if ((str[i] & 0xC0) != 0x80)
  39. return -i;
  40. }
  41. return length;
  42. }
  43. void utf8proc_detab(strbuf *ob, const uint8_t *line, size_t size)
  44. {
  45. static const uint8_t whitespace[] = " ";
  46. size_t i = 0, tab = 0;
  47. while (i < size) {
  48. size_t org = i;
  49. while (i < size && line[i] != '\t' && line[i] <= 0x80) {
  50. i++; tab++;
  51. }
  52. if (i > org)
  53. strbuf_put(ob, line + org, i - org);
  54. if (i >= size)
  55. break;
  56. if (line[i] == '\t') {
  57. int numspaces = 4 - (tab % 4);
  58. strbuf_put(ob, whitespace, numspaces);
  59. i += 1;
  60. tab += numspaces;
  61. } else {
  62. int charlen = utf8proc_charlen(line + i, size - i);
  63. if (charlen >= 0) {
  64. strbuf_put(ob, line + i, charlen);
  65. } else {
  66. encode_unknown(ob);
  67. charlen = -charlen;
  68. }
  69. i += charlen;
  70. tab += 1;
  71. }
  72. }
  73. }
  74. int utf8proc_iterate(const uint8_t *str, int str_len, int32_t *dst)
  75. {
  76. int length;
  77. int32_t uc = -1;
  78. *dst = -1;
  79. length = utf8proc_charlen(str, str_len);
  80. if (length < 0)
  81. return -1;
  82. switch (length) {
  83. case 1:
  84. uc = str[0];
  85. break;
  86. case 2:
  87. uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
  88. if (uc < 0x80) uc = -1;
  89. break;
  90. case 3:
  91. uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
  92. + (str[2] & 0x3F);
  93. if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
  94. (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
  95. break;
  96. case 4:
  97. uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
  98. + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
  99. if (uc < 0x10000 || uc >= 0x110000) uc = -1;
  100. break;
  101. }
  102. if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE))
  103. return -1;
  104. *dst = uc;
  105. return length;
  106. }
  107. void utf8proc_encode_char(int32_t uc, strbuf *buf)
  108. {
  109. uint8_t dst[4];
  110. int len = 0;
  111. assert(uc >= 0);
  112. if (uc < 0x80) {
  113. dst[0] = uc;
  114. len = 1;
  115. } else if (uc < 0x800) {
  116. dst[0] = 0xC0 + (uc >> 6);
  117. dst[1] = 0x80 + (uc & 0x3F);
  118. len = 2;
  119. } else if (uc == 0xFFFF) {
  120. dst[0] = 0xFF;
  121. len = 1;
  122. } else if (uc == 0xFFFE) {
  123. dst[0] = 0xFE;
  124. len = 1;
  125. } else if (uc < 0x10000) {
  126. dst[0] = 0xE0 + (uc >> 12);
  127. dst[1] = 0x80 + ((uc >> 6) & 0x3F);
  128. dst[2] = 0x80 + (uc & 0x3F);
  129. len = 3;
  130. } else if (uc < 0x110000) {
  131. dst[0] = 0xF0 + (uc >> 18);
  132. dst[1] = 0x80 + ((uc >> 12) & 0x3F);
  133. dst[2] = 0x80 + ((uc >> 6) & 0x3F);
  134. dst[3] = 0x80 + (uc & 0x3F);
  135. len = 4;
  136. } else {
  137. encode_unknown(buf);
  138. return;
  139. }
  140. strbuf_put(buf, dst, len);
  141. }
  142. void utf8proc_case_fold(strbuf *dest, const uint8_t *str, int len)
  143. {
  144. int32_t c;
  145. #define bufpush(x) \
  146. utf8proc_encode_char(x, dest)
  147. while (len > 0) {
  148. int char_len = utf8proc_iterate(str, len, &c);
  149. if (char_len >= 0) {
  150. #include "case_fold_switch.inc"
  151. } else {
  152. encode_unknown(dest);
  153. char_len = -char_len;
  154. }
  155. str += char_len;
  156. len -= char_len;
  157. }
  158. }
  159. // matches anything in the Zs class, plus LF, CR, TAB, FF.
  160. int utf8proc_is_space(int32_t uc)
  161. {
  162. return (uc == 9 ||
  163. uc == 10 ||
  164. uc == 12 ||
  165. uc == 13 ||
  166. uc == 32 ||
  167. uc == 160 ||
  168. uc == 5760 ||
  169. (uc >= 8192 && uc <= 8202) ||
  170. uc == 8239 ||
  171. uc == 8287 ||
  172. uc == 12288);
  173. }
  174. // matches anything in the P[cdefios] classes.
  175. int utf8proc_is_punctuation(int32_t uc)
  176. {
  177. return ((uc >= 33 && uc <= 35) ||
  178. (uc >= 37 && uc <= 42) ||
  179. (uc >= 44 && uc <= 47) ||
  180. uc == 58 ||
  181. uc == 59 ||
  182. uc == 63 ||
  183. uc == 64 ||
  184. (uc >= 91 && uc <= 93) ||
  185. uc == 95 ||
  186. uc == 123 ||
  187. uc == 125 ||
  188. uc == 161 ||
  189. uc == 167 ||
  190. uc == 171 ||
  191. uc == 182 ||
  192. uc == 183 ||
  193. uc == 187 ||
  194. uc == 191 ||
  195. uc == 894 ||
  196. uc == 903 ||
  197. (uc >= 1370 && uc <= 1375) ||
  198. uc == 1417 ||
  199. uc == 1418 ||
  200. uc == 1470 ||
  201. uc == 1472 ||
  202. uc == 1475 ||
  203. uc == 1478 ||
  204. uc == 1523 ||
  205. uc == 1524 ||
  206. uc == 1545 ||
  207. uc == 1546 ||
  208. uc == 1548 ||
  209. uc == 1549 ||
  210. uc == 1563 ||
  211. uc == 1566 ||
  212. uc == 1567 ||
  213. (uc >= 1642 && uc <= 1645) ||
  214. uc == 1748 ||
  215. (uc >= 1792 && uc <= 1805) ||
  216. (uc >= 2039 && uc <= 2041) ||
  217. (uc >= 2096 && uc <= 2110) ||
  218. uc == 2142 ||
  219. uc == 2404 ||
  220. uc == 2405 ||
  221. uc == 2416 ||
  222. uc == 2800 ||
  223. uc == 3572 ||
  224. uc == 3663 ||
  225. uc == 3674 ||
  226. uc == 3675 ||
  227. (uc >= 3844 && uc <= 3858) ||
  228. uc == 3860 ||
  229. (uc >= 3898 && uc <= 3901) ||
  230. uc == 3973 ||
  231. (uc >= 4048 && uc <= 4052) ||
  232. uc == 4057 ||
  233. uc == 4058 ||
  234. (uc >= 4170 && uc <= 4175) ||
  235. uc == 4347 ||
  236. (uc >= 4960 && uc <= 4968) ||
  237. uc == 5120 ||
  238. uc == 5741 ||
  239. uc == 5742 ||
  240. uc == 5787 ||
  241. uc == 5788 ||
  242. (uc >= 5867 && uc <= 5869) ||
  243. uc == 5941 ||
  244. uc == 5942 ||
  245. (uc >= 6100 && uc <= 6102) ||
  246. (uc >= 6104 && uc <= 6106) ||
  247. (uc >= 6144 && uc <= 6154) ||
  248. uc == 6468 ||
  249. uc == 6469 ||
  250. uc == 6686 ||
  251. uc == 6687 ||
  252. (uc >= 6816 && uc <= 6822) ||
  253. (uc >= 6824 && uc <= 6829) ||
  254. (uc >= 7002 && uc <= 7008) ||
  255. (uc >= 7164 && uc <= 7167) ||
  256. (uc >= 7227 && uc <= 7231) ||
  257. uc == 7294 ||
  258. uc == 7295 ||
  259. (uc >= 7360 && uc <= 7367) ||
  260. uc == 7379 ||
  261. (uc >= 8208 && uc <= 8231) ||
  262. (uc >= 8240 && uc <= 8259) ||
  263. (uc >= 8261 && uc <= 8273) ||
  264. (uc >= 8275 && uc <= 8286) ||
  265. uc == 8317 ||
  266. uc == 8318 ||
  267. uc == 8333 ||
  268. uc == 8334 ||
  269. (uc >= 8968 && uc <= 8971) ||
  270. uc == 9001 ||
  271. uc == 9002 ||
  272. (uc >= 10088 && uc <= 10101) ||
  273. uc == 10181 ||
  274. uc == 10182 ||
  275. (uc >= 10214 && uc <= 10223) ||
  276. (uc >= 10627 && uc <= 10648) ||
  277. (uc >= 10712 && uc <= 10715) ||
  278. uc == 10748 ||
  279. uc == 10749 ||
  280. (uc >= 11513 && uc <= 11516) ||
  281. uc == 11518 ||
  282. uc == 11519 ||
  283. uc == 11632 ||
  284. (uc >= 11776 && uc <= 11822) ||
  285. (uc >= 11824 && uc <= 11842) ||
  286. (uc >= 12289 && uc <= 12291) ||
  287. (uc >= 12296 && uc <= 12305) ||
  288. (uc >= 12308 && uc <= 12319) ||
  289. uc == 12336 ||
  290. uc == 12349 ||
  291. uc == 12448 ||
  292. uc == 12539 ||
  293. uc == 42238 ||
  294. uc == 42239 ||
  295. (uc >= 42509 && uc <= 42511) ||
  296. uc == 42611 ||
  297. uc == 42622 ||
  298. (uc >= 42738 && uc <= 42743) ||
  299. (uc >= 43124 && uc <= 43127) ||
  300. uc == 43214 ||
  301. uc == 43215 ||
  302. (uc >= 43256 && uc <= 43258) ||
  303. uc == 43310 ||
  304. uc == 43311 ||
  305. uc == 43359 ||
  306. (uc >= 43457 && uc <= 43469) ||
  307. uc == 43486 ||
  308. uc == 43487 ||
  309. (uc >= 43612 && uc <= 43615) ||
  310. uc == 43742 ||
  311. uc == 43743 ||
  312. uc == 43760 ||
  313. uc == 43761 ||
  314. uc == 44011 ||
  315. uc == 64830 ||
  316. uc == 64831 ||
  317. (uc >= 65040 && uc <= 65049) ||
  318. (uc >= 65072 && uc <= 65106) ||
  319. (uc >= 65108 && uc <= 65121) ||
  320. uc == 65123 ||
  321. uc == 65128 ||
  322. uc == 65130 ||
  323. uc == 65131 ||
  324. (uc >= 65281 && uc <= 65283) ||
  325. (uc >= 65285 && uc <= 65290) ||
  326. (uc >= 65292 && uc <= 65295) ||
  327. uc == 65306 ||
  328. uc == 65307 ||
  329. uc == 65311 ||
  330. uc == 65312 ||
  331. (uc >= 65339 && uc <= 65341) ||
  332. uc == 65343 ||
  333. uc == 65371 ||
  334. uc == 65373 ||
  335. (uc >= 65375 && uc <= 65381) ||
  336. (uc >= 65792 && uc <= 65794) ||
  337. uc == 66463 ||
  338. uc == 66512 ||
  339. uc == 66927 ||
  340. uc == 67671 ||
  341. uc == 67871 ||
  342. uc == 67903 ||
  343. (uc >= 68176 && uc <= 68184) ||
  344. uc == 68223 ||
  345. (uc >= 68336 && uc <= 68342) ||
  346. (uc >= 68409 && uc <= 68415) ||
  347. (uc >= 68505 && uc <= 68508) ||
  348. (uc >= 69703 && uc <= 69709) ||
  349. uc == 69819 ||
  350. uc == 69820 ||
  351. (uc >= 69822 && uc <= 69825) ||
  352. (uc >= 69952 && uc <= 69955) ||
  353. uc == 70004 ||
  354. uc == 70005 ||
  355. (uc >= 70085 && uc <= 70088) ||
  356. uc == 70093 ||
  357. (uc >= 70200 && uc <= 70205) ||
  358. uc == 70854 ||
  359. (uc >= 71105 && uc <= 71113) ||
  360. (uc >= 71233 && uc <= 71235) ||
  361. (uc >= 74864 && uc <= 74868) ||
  362. uc == 92782 ||
  363. uc == 92783 ||
  364. uc == 92917 ||
  365. (uc >= 92983 && uc <= 92987) ||
  366. uc == 92996 ||
  367. uc == 113823);
  368. }