xs_unicode.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* copyright (c) 2022 - 2023 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. xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint);
  6. unsigned int xs_utf8_dec(char **str);
  7. int xs_unicode_width(unsigned int cpoint);
  8. unsigned int *_xs_unicode_upper_search(unsigned int cpoint);
  9. unsigned int *_xs_unicode_lower_search(unsigned int cpoint);
  10. #define xs_unicode_is_upper(cpoint) (!!_xs_unicode_upper_search(cpoint))
  11. #define xs_unicode_is_lower(cpoint) (!!_xs_unicode_lower_search(cpoint))
  12. unsigned int xs_unicode_to_upper(unsigned int cpoint);
  13. unsigned int xs_unicode_to_lower(unsigned int cpoint);
  14. int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac);
  15. int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint);
  16. int xs_unicode_is_alpha(unsigned int cpoint);
  17. #ifdef XS_IMPLEMENTATION
  18. int _xs_utf8_enc(char buf[4], unsigned int cpoint)
  19. /* encodes an Unicode codepoint to utf-8 into buf and returns the size in bytes */
  20. {
  21. unsigned char *p = (unsigned char *)buf;
  22. if (cpoint < 0x80) /* 1 byte char */
  23. *p++ = cpoint & 0xff;
  24. else {
  25. if (cpoint < 0x800) /* 2 byte char */
  26. *p++ = 0xc0 | (cpoint >> 6);
  27. else {
  28. if (cpoint < 0x10000) /* 3 byte char */
  29. *p++ = 0xe0 | (cpoint >> 12);
  30. else { /* 4 byte char */
  31. *p++ = 0xf0 | (cpoint >> 18);
  32. *p++ = 0x80 | ((cpoint >> 12) & 0x3f);
  33. }
  34. *p++ = 0x80 | ((cpoint >> 6) & 0x3f);
  35. }
  36. *p++ = 0x80 | (cpoint & 0x3f);
  37. }
  38. return p - (unsigned char *)buf;
  39. }
  40. xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint)
  41. /* encodes an Unicode codepoint to utf-8 into str */
  42. {
  43. char tmp[4];
  44. int c = _xs_utf8_enc(tmp, cpoint);
  45. return xs_append_m(str, tmp, c);
  46. }
  47. unsigned int xs_utf8_dec(char **str)
  48. /* decodes an utf-8 char inside str and updates the pointer */
  49. {
  50. unsigned char *p = (unsigned char *)*str;
  51. unsigned int cpoint = 0;
  52. int c = *p++;
  53. int cb = 0;
  54. if ((c & 0x80) == 0) { /* 1 byte char */
  55. cpoint = c;
  56. }
  57. else
  58. if ((c & 0xe0) == 0xc0) { /* 2 byte char */
  59. cpoint = (c & 0x1f) << 6;
  60. cb = 1;
  61. }
  62. else
  63. if ((c & 0xf0) == 0xe0) { /* 3 byte char */
  64. cpoint = (c & 0x0f) << 12;
  65. cb = 2;
  66. }
  67. else
  68. if ((c & 0xf8) == 0xf0) { /* 4 byte char */
  69. cpoint = (c & 0x07) << 18;
  70. cb = 3;
  71. }
  72. /* process the continuation bytes */
  73. while (cb--) {
  74. if ((*p & 0xc0) == 0x80)
  75. cpoint |= (*p++ & 0x3f) << (cb * 6);
  76. else {
  77. cpoint = 0xfffd;
  78. break;
  79. }
  80. }
  81. *str = (char *)p;
  82. return cpoint;
  83. }
  84. static int int_range_cmp(const void *p1, const void *p2)
  85. {
  86. const unsigned int *a = p1;
  87. const unsigned int *b = p2;
  88. return *a < b[0] ? -1 : *a > b[1] ? 1 : 0;
  89. }
  90. /* intentionally dead simple */
  91. static unsigned int xs_unicode_width_table[] = {
  92. 0x300, 0x36f, 0, /* diacritics */
  93. 0x1100, 0x11ff, 2, /* Hangul */
  94. 0x2e80, 0xa4cf, 2, /* CJK */
  95. 0xac00, 0xd7a3, 2, /* more Hangul */
  96. 0xe000, 0xf8ff, 0, /* private use */
  97. 0xf900, 0xfaff, 2, /* CJK compatibility */
  98. 0xff00, 0xff60, 2, /* full width things */
  99. 0xffdf, 0xffe6, 2, /* full width things */
  100. 0x1f200, 0x1ffff, 2, /* emojis */
  101. 0x20000, 0x2fffd, 2 /* more CJK */
  102. };
  103. int xs_unicode_width(unsigned int cpoint)
  104. /* returns the width in columns of a Unicode codepoint (somewhat simplified) */
  105. {
  106. unsigned int *r = bsearch(&cpoint, xs_unicode_width_table,
  107. sizeof(xs_unicode_width_table) / (sizeof(unsigned int) * 3),
  108. sizeof(unsigned int) * 3,
  109. int_range_cmp);
  110. return r ? r[2] : 1;
  111. }
  112. #ifdef _XS_UNICODE_TBL_H
  113. /* include xs_unicode_tbl.h before this one to use these functions */
  114. static int int_cmp(const void *p1, const void *p2)
  115. {
  116. const unsigned int *a = p1;
  117. const unsigned int *b = p2;
  118. return *a < *b ? -1 : *a > *b ? 1 : 0;
  119. }
  120. unsigned int *_xs_unicode_upper_search(unsigned int cpoint)
  121. /* searches for an uppercase codepoint in the case fold table */
  122. {
  123. return bsearch(&cpoint, xs_unicode_case_fold_table,
  124. sizeof(xs_unicode_case_fold_table) / (sizeof(unsigned int) * 2),
  125. sizeof(unsigned int) * 2,
  126. int_cmp);
  127. }
  128. unsigned int *_xs_unicode_lower_search(unsigned int cpoint)
  129. /* searches for a lowercase codepoint in the case fold table */
  130. {
  131. unsigned int *p = xs_unicode_case_fold_table + 1;
  132. unsigned int *e = xs_unicode_case_fold_table +
  133. sizeof(xs_unicode_case_fold_table) / sizeof(unsigned int);
  134. while (p < e) {
  135. if (cpoint == *p)
  136. return p;
  137. p += 2;
  138. }
  139. return NULL;
  140. }
  141. unsigned int xs_unicode_to_upper(unsigned int cpoint)
  142. /* returns the cpoint to uppercase */
  143. {
  144. unsigned int *p = _xs_unicode_lower_search(cpoint);
  145. return p == NULL ? cpoint : p[-1];
  146. }
  147. unsigned int xs_unicode_to_lower(unsigned int cpoint)
  148. /* returns the cpoint to lowercase */
  149. {
  150. unsigned int *p = _xs_unicode_upper_search(cpoint);
  151. return p == NULL ? cpoint : p[1];
  152. }
  153. int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac)
  154. /* applies unicode Normalization Form D */
  155. {
  156. unsigned int *r = bsearch(&cpoint, xs_unicode_nfd_table,
  157. sizeof(xs_unicode_nfd_table) / (sizeof(unsigned int) * 3),
  158. sizeof(unsigned int) * 3,
  159. int_cmp);
  160. if (r != NULL) {
  161. *base = r[1];
  162. *diac = r[2];
  163. }
  164. return !!r;
  165. }
  166. int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint)
  167. /* applies unicode Normalization Form C */
  168. {
  169. unsigned int *p = xs_unicode_nfd_table;
  170. unsigned int *e = xs_unicode_nfd_table +
  171. sizeof(xs_unicode_nfd_table) / sizeof(unsigned int);
  172. while (p < e) {
  173. if (p[1] == base && p[2] == diac) {
  174. *cpoint = p[0];
  175. return 1;
  176. }
  177. p += 3;
  178. }
  179. return 0;
  180. }
  181. int xs_unicode_is_alpha(unsigned int cpoint)
  182. /* checks if a codepoint is an alpha (i.e. a letter) */
  183. {
  184. unsigned int *r = bsearch(&cpoint, xs_unicode_alpha_table,
  185. sizeof(xs_unicode_alpha_table) / (sizeof(unsigned int) * 2),
  186. sizeof(unsigned int) * 2,
  187. int_range_cmp);
  188. return !!r;
  189. }
  190. #endif /* _XS_UNICODE_TBL_H */
  191. #endif /* XS_IMPLEMENTATION */
  192. #endif /* _XS_UNICODE_H */