xs_unicode.h 11 KB

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