xs_unicode.h 10 KB

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