aboutsummaryrefslogtreecommitdiff
path: root/src/utf8.c
blob: e3f8dd3c2c278bb8b937808185459df229f63334 (plain)
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <unistd.h>
  4. #include "stmd.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. ssize_t utf8proc_charlen(const uint8_t *str, ssize_t str_len)
  23. {
  24. ssize_t length, i;
  25. if (!str_len)
  26. return 0;
  27. length = utf8proc_utf8class[str[0]];
  28. if (!length)
  29. return -1;
  30. if (str_len >= 0 && length > str_len)
  31. return -1;
  32. for (i = 1; i < length; i++) {
  33. if ((str[i] & 0xC0) != 0x80)
  34. return -1;
  35. }
  36. return length;
  37. }
  38. ssize_t utf8proc_iterate(const uint8_t *str, ssize_t str_len, int32_t *dst)
  39. {
  40. ssize_t length;
  41. int32_t uc = -1;
  42. *dst = -1;
  43. length = utf8proc_charlen(str, str_len);
  44. if (length < 0)
  45. return -1;
  46. switch (length) {
  47. case 1:
  48. uc = str[0];
  49. break;
  50. case 2:
  51. uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
  52. if (uc < 0x80) uc = -1;
  53. break;
  54. case 3:
  55. uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
  56. + (str[2] & 0x3F);
  57. if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
  58. (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
  59. break;
  60. case 4:
  61. uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
  62. + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
  63. if (uc < 0x10000 || uc >= 0x110000) uc = -1;
  64. break;
  65. }
  66. if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE))
  67. return -1;
  68. *dst = uc;
  69. return length;
  70. }
  71. void utf8_encode_char(int32_t uc, gh_buf *buf)
  72. {
  73. char dst[4];
  74. int len = 0;
  75. if (uc < 0x00) {
  76. assert(false);
  77. } else if (uc < 0x80) {
  78. dst[0] = uc;
  79. len = 1;
  80. } else if (uc < 0x800) {
  81. dst[0] = 0xC0 + (uc >> 6);
  82. dst[1] = 0x80 + (uc & 0x3F);
  83. len = 2;
  84. } else if (uc == 0xFFFF) {
  85. dst[0] = 0xFF;
  86. return 1;
  87. } else if (uc == 0xFFFE) {
  88. dst[0] = 0xFE;
  89. len = 1;
  90. } else if (uc < 0x10000) {
  91. dst[0] = 0xE0 + (uc >> 12);
  92. dst[1] = 0x80 + ((uc >> 6) & 0x3F);
  93. dst[2] = 0x80 + (uc & 0x3F);
  94. len = 3;
  95. } else if (uc < 0x110000) {
  96. dst[0] = 0xF0 + (uc >> 18);
  97. dst[1] = 0x80 + ((uc >> 12) & 0x3F);
  98. dst[2] = 0x80 + ((uc >> 6) & 0x3F);
  99. dst[3] = 0x80 + (uc & 0x3F);
  100. len = 4;
  101. } else {
  102. assert(false);
  103. }
  104. gh_buf_put(buf, dst, len);
  105. }
  106. void utf8proc_case_fold(gh_buf *dest, const unsigned char *str, int len)
  107. {
  108. int32_t c;
  109. #define bufpush(x) \
  110. utf8proc_encode_char(x, dest)
  111. while (len > 0) {
  112. ssize_t char_len = utf8proc_iterate(str, len, &c);
  113. if (char_len < 0) {
  114. bufpush(0xFFFD);
  115. continue;
  116. }
  117. #include "case_fold_switch.inc"
  118. str += char_len;
  119. len -= char_len;
  120. }
  121. }