xs_unicode.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
  2. #ifndef _XS_UNICODE_H
  3. #define _XS_UNICODE_H
  4. int xs_utf8_enc(char buf[4], unsigned int cpoint);
  5. int xs_is_utf8_cont_byte(char c);
  6. unsigned int xs_utf8_dec(const char **str);
  7. int xs_unicode_width(unsigned int cpoint);
  8. int xs_is_surrogate(unsigned int cpoint);
  9. int xs_is_diacritic(unsigned int cpoint);
  10. unsigned int xs_surrogate_dec(unsigned int p1, unsigned int p2);
  11. unsigned int xs_surrogate_enc(unsigned int cpoint);
  12. unsigned int *_xs_unicode_upper_search(unsigned int cpoint);
  13. unsigned int *_xs_unicode_lower_search(unsigned int cpoint);
  14. #define xs_unicode_is_upper(cpoint) (!!_xs_unicode_upper_search(cpoint))
  15. #define xs_unicode_is_lower(cpoint) (!!_xs_unicode_lower_search(cpoint))
  16. unsigned int xs_unicode_to_upper(unsigned int cpoint);
  17. unsigned int xs_unicode_to_lower(unsigned int cpoint);
  18. int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac);
  19. int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint);
  20. int xs_unicode_is_alpha(unsigned int cpoint);
  21. int xs_unicode_is_right_to_left(unsigned int cpoint);
  22. int xs_is_emoji(unsigned int cpoint);
  23. int xs_utf8_len(const char *str);
  24. #ifdef _XS_H
  25. xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset);
  26. xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint);
  27. xs_str *xs_utf8_crop_i(xs_str *str, int begin, int end);
  28. xs_str *xs_utf8_to_upper(const char *str);
  29. xs_str *xs_utf8_to_lower(const char *str);
  30. xs_str *xs_utf8_to_nfd(const char *str);
  31. xs_str *xs_utf8_to_nfc(const char *str);
  32. #endif
  33. #ifdef XS_IMPLEMENTATION
  34. #include <ctype.h>
  35. #ifndef xs_countof
  36. #define xs_countof(a) (sizeof((a)) / sizeof((*a)))
  37. #endif
  38. int xs_utf8_enc(char buf[4], unsigned int cpoint)
  39. /* encodes an Unicode codepoint to utf-8 into buf and returns the size in bytes */
  40. {
  41. char *p = buf;
  42. if (cpoint < 0x80) /* 1 byte char */
  43. *p++ = cpoint & 0xff;
  44. else {
  45. if (cpoint < 0x800) /* 2 byte char */
  46. *p++ = 0xc0 | (cpoint >> 6);
  47. else {
  48. if (cpoint < 0x10000) /* 3 byte char */
  49. *p++ = 0xe0 | (cpoint >> 12);
  50. else { /* 4 byte char */
  51. *p++ = 0xf0 | (cpoint >> 18);
  52. *p++ = 0x80 | ((cpoint >> 12) & 0x3f);
  53. }
  54. *p++ = 0x80 | ((cpoint >> 6) & 0x3f);
  55. }
  56. *p++ = 0x80 | (cpoint & 0x3f);
  57. }
  58. return p - buf;
  59. }
  60. int xs_is_utf8_cont_byte(char c)
  61. /* returns true if c is an utf8 continuation byte */
  62. {
  63. return ((c & 0xc0) == 0x80);
  64. }
  65. unsigned int xs_utf8_dec(const char **str)
  66. /* decodes an utf-8 char inside str and updates the pointer */
  67. {
  68. const char *p = *str;
  69. if (!xs_is_string(p))
  70. return 0;
  71. unsigned int cpoint = 0;
  72. unsigned char c = *p++;
  73. int cb = 0;
  74. if ((c & 0x80) == 0) { /* 1 byte char */
  75. cpoint = c;
  76. }
  77. else
  78. if ((c & 0xe0) == 0xc0) { /* 2 byte char */
  79. cpoint = (c & 0x1f) << 6;
  80. cb = 1;
  81. }
  82. else
  83. if ((c & 0xf0) == 0xe0) { /* 3 byte char */
  84. cpoint = (c & 0x0f) << 12;
  85. cb = 2;
  86. }
  87. else
  88. if ((c & 0xf8) == 0xf0) { /* 4 byte char */
  89. cpoint = (c & 0x07) << 18;
  90. cb = 3;
  91. }
  92. /* process the continuation bytes */
  93. while (cb > 0 && *p && xs_is_utf8_cont_byte(*p))
  94. cpoint |= (*p++ & 0x3f) << (--cb * 6);
  95. /* incomplete or broken? */
  96. if (cb)
  97. cpoint = 0xfffd;
  98. *str = p;
  99. return cpoint;
  100. }
  101. /** Unicode character width: intentionally dead simple **/
  102. static unsigned int xs_unicode_width_table[] = {
  103. 0x300, 0x36f, 0, /* diacritics */
  104. 0x1100, 0x11ff, 2, /* Hangul */
  105. 0x2e80, 0xa4cf, 2, /* CJK */
  106. 0xac00, 0xd7a3, 2, /* more Hangul */
  107. 0xe000, 0xf8ff, 0, /* private use */
  108. 0xf900, 0xfaff, 2, /* CJK compatibility */
  109. 0xff00, 0xff60, 2, /* full width things */
  110. 0xffdf, 0xffe6, 2, /* full width things */
  111. 0x1f200, 0x1ffff, 2, /* emojis */
  112. 0x20000, 0x2fffd, 2 /* more CJK */
  113. };
  114. /* magic number from https://en.wikipedia.org/wiki/Emoji#Unicode_blocks */
  115. int xs_is_emoji(unsigned int cpoint) {
  116. /* returns wether the input is an utf8 emoji */
  117. return cpoint > 0x00A9 && cpoint < 0x1ffff;
  118. }
  119. int xs_unicode_width(unsigned int cpoint)
  120. /* returns the width in columns of a Unicode codepoint (somewhat simplified) */
  121. {
  122. int b = 0;
  123. int t = xs_countof(xs_unicode_width_table) / 3 - 1;
  124. while (t >= b) {
  125. int n = (b + t) / 2;
  126. unsigned int *p = &xs_unicode_width_table[n * 3];
  127. if (cpoint < p[0])
  128. t = n - 1;
  129. else
  130. if (cpoint > p[1])
  131. b = n + 1;
  132. else
  133. return p[2];
  134. }
  135. return 1;
  136. }
  137. int xs_is_diacritic(unsigned int cpoint)
  138. {
  139. return cpoint >= 0x300 && cpoint <= 0x36f;
  140. }
  141. /** surrogate pairs **/
  142. int xs_is_surrogate(unsigned int cpoint)
  143. /* checks if cpoint is the first element of a Unicode surrogate pair */
  144. {
  145. return cpoint >= 0xd800 && cpoint <= 0xdfff;
  146. }
  147. unsigned int xs_surrogate_dec(unsigned int p1, unsigned int p2)
  148. /* "decodes" a surrogate pair into a codepoint */
  149. {
  150. return 0x10000 | ((p1 & 0x3ff) << 10) | (p2 & 0x3ff);
  151. }
  152. unsigned int xs_surrogate_enc(unsigned int cpoint)
  153. /* "encodes" a Unicode into a surrogate pair (p1 in the MSB word) */
  154. {
  155. unsigned int p1 = 0xd7c0 + (cpoint >> 10);
  156. unsigned int p2 = 0xdc00 + (cpoint & 0x3ff);
  157. return (p1 << 16) | p2;
  158. }
  159. int xs_utf8_len(const char *str)
  160. /* counts the number of characters (codepoints) in str */
  161. {
  162. int len = 0;
  163. while (xs_utf8_dec(&str))
  164. len++;
  165. return len;
  166. }
  167. #ifdef _XS_H
  168. xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset)
  169. /* encodes an Unicode codepoint to utf-8 into str */
  170. {
  171. char tmp[4];
  172. int c = xs_utf8_enc(tmp, cpoint);
  173. str = xs_insert_m(str, *offset, tmp, c);
  174. *offset += c;
  175. return str;
  176. }
  177. xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint)
  178. /* encodes an Unicode codepoint to utf-8 into str */
  179. {
  180. int offset = strlen(str);
  181. return xs_utf8_insert(str, cpoint, &offset);
  182. }
  183. xs_str *xs_utf8_crop_i(xs_str *str, int begin, int end)
  184. /* crops str from begin to end by characters (codepoints) */
  185. {
  186. const char *p = str;
  187. int sz = xs_utf8_len(str);
  188. unsigned int cpoint;
  189. if (end <= 0)
  190. end = sz + end;
  191. xs_str *ns = xs_str_new(NULL);
  192. for (int n = 0; n < end && (cpoint = xs_utf8_dec(&p)); n++) {
  193. if (n >= begin)
  194. ns = xs_utf8_cat(ns, cpoint);
  195. }
  196. xs_free(str);
  197. return ns;
  198. }
  199. #endif /* _XS_H */
  200. #ifdef _XS_UNICODE_TBL_H
  201. /* include xs_unicode_tbl.h before this one to use these functions */
  202. unsigned int *_xs_unicode_upper_search(unsigned int cpoint)
  203. /* searches for an uppercase codepoint in the case fold table */
  204. {
  205. int b = 0;
  206. int t = xs_countof(xs_unicode_case_fold_table) / 2 - 1;
  207. while (t >= b) {
  208. int n = (b + t) / 2;
  209. unsigned int *p = &xs_unicode_case_fold_table[n * 2];
  210. if (cpoint < p[0])
  211. t = n - 1;
  212. else
  213. if (cpoint > p[0])
  214. b = n + 1;
  215. else
  216. return p;
  217. }
  218. return NULL;
  219. }
  220. unsigned int *_xs_unicode_lower_search(unsigned int cpoint)
  221. /* searches for a lowercase codepoint in the case fold table */
  222. {
  223. unsigned int *p = xs_unicode_case_fold_table;
  224. unsigned int *e = p + xs_countof(xs_unicode_case_fold_table);
  225. while (p < e) {
  226. if (cpoint == p[1])
  227. return p;
  228. p += 2;
  229. }
  230. return NULL;
  231. }
  232. unsigned int xs_unicode_to_lower(unsigned int cpoint)
  233. /* returns the cpoint to lowercase */
  234. {
  235. if (cpoint < 0x80)
  236. return tolower(cpoint);
  237. unsigned int *p = _xs_unicode_upper_search(cpoint);
  238. return p == NULL ? cpoint : p[1];
  239. }
  240. unsigned int xs_unicode_to_upper(unsigned int cpoint)
  241. /* returns the cpoint to uppercase */
  242. {
  243. if (cpoint < 0x80)
  244. return toupper(cpoint);
  245. unsigned int *p = _xs_unicode_lower_search(cpoint);
  246. return p == NULL ? cpoint : p[0];
  247. }
  248. int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac)
  249. /* applies unicode Normalization Form D */
  250. {
  251. int b = 0;
  252. int t = xs_countof(xs_unicode_nfd_table) / 3 - 1;
  253. while (t >= b) {
  254. int n = (b + t) / 2;
  255. unsigned int *p = &xs_unicode_nfd_table[n * 3];
  256. int c = cpoint - p[0];
  257. if (c < 0)
  258. t = n - 1;
  259. else
  260. if (c > 0)
  261. b = n + 1;
  262. else {
  263. *base = p[1];
  264. *diac = p[2];
  265. return 1;
  266. }
  267. }
  268. return 0;
  269. }
  270. int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint)
  271. /* applies unicode Normalization Form C */
  272. {
  273. unsigned int *p = xs_unicode_nfd_table;
  274. unsigned int *e = p + xs_countof(xs_unicode_nfd_table);
  275. while (p < e) {
  276. if (p[1] == base && p[2] == diac) {
  277. *cpoint = p[0];
  278. return 1;
  279. }
  280. p += 3;
  281. }
  282. return 0;
  283. }
  284. int xs_unicode_is_alpha(unsigned int cpoint)
  285. /* checks if a codepoint is an alpha (i.e. a letter) */
  286. {
  287. int b = 0;
  288. int t = xs_countof(xs_unicode_alpha_table) / 2 - 1;
  289. while (t >= b) {
  290. int n = (b + t) / 2;
  291. unsigned int *p = &xs_unicode_alpha_table[n * 2];
  292. if (cpoint < p[0])
  293. t = n - 1;
  294. else
  295. if (cpoint > p[1])
  296. b = n + 1;
  297. else
  298. return 1;
  299. }
  300. return 0;
  301. }
  302. int xs_unicode_is_right_to_left(unsigned int cpoint)
  303. /* checks if a codepoint is a right-to-left letter */
  304. {
  305. int b = 0;
  306. int t = xs_countof(xs_unicode_right_to_left_table) / 2 - 1;
  307. while (t >= b) {
  308. int n = (b + t) / 2;
  309. unsigned int *p = &xs_unicode_right_to_left_table[n * 2];
  310. if (cpoint < p[0])
  311. t = n - 1;
  312. else
  313. if (cpoint > p[1])
  314. b = n + 1;
  315. else
  316. return 1;
  317. }
  318. return 0;
  319. }
  320. #ifdef _XS_H
  321. xs_str *xs_utf8_to_upper(const char *str)
  322. {
  323. xs_str *s = xs_str_new(NULL);
  324. unsigned int cpoint;
  325. int offset = 0;
  326. while ((cpoint = xs_utf8_dec(&str))) {
  327. cpoint = xs_unicode_to_upper(cpoint);
  328. s = xs_utf8_insert(s, cpoint, &offset);
  329. }
  330. return s;
  331. }
  332. xs_str *xs_utf8_to_lower(const char *str)
  333. {
  334. xs_str *s = xs_str_new(NULL);
  335. unsigned int cpoint;
  336. int offset = 0;
  337. while ((cpoint = xs_utf8_dec(&str))) {
  338. cpoint = xs_unicode_to_lower(cpoint);
  339. s = xs_utf8_insert(s, cpoint, &offset);
  340. }
  341. return s;
  342. }
  343. xs_str *xs_utf8_to_nfd(const char *str)
  344. {
  345. xs_str *s = xs_str_new(NULL);
  346. unsigned int cpoint;
  347. int offset = 0;
  348. while ((cpoint = xs_utf8_dec(&str))) {
  349. unsigned int base;
  350. unsigned int diac;
  351. if (xs_unicode_nfd(cpoint, &base, &diac)) {
  352. s = xs_utf8_insert(s, base, &offset);
  353. s = xs_utf8_insert(s, diac, &offset);
  354. }
  355. else
  356. s = xs_utf8_insert(s, cpoint, &offset);
  357. }
  358. return s;
  359. }
  360. xs_str *xs_utf8_to_nfc(const char *str)
  361. {
  362. xs_str *s = xs_str_new(NULL);
  363. unsigned int cpoint;
  364. unsigned int base = 0;
  365. int offset = 0;
  366. while ((cpoint = xs_utf8_dec(&str))) {
  367. if (xs_is_diacritic(cpoint)) {
  368. if (xs_unicode_nfc(base, cpoint, &base))
  369. continue;
  370. }
  371. if (base)
  372. s = xs_utf8_insert(s, base, &offset);
  373. base = cpoint;
  374. }
  375. if (base)
  376. s = xs_utf8_insert(s, base, &offset);
  377. return s;
  378. }
  379. #endif /* _XS_H */
  380. #endif /* _XS_UNICODE_TBL_H */
  381. #endif /* XS_IMPLEMENTATION */
  382. #endif /* _XS_UNICODE_H */