aboutsummaryrefslogtreecommitdiff
path: root/src/utf8.c
blob: d642ef402ebd6479463ff08a7b36c27eddff5783 (plain)
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <assert.h>
  4. #include "utf8.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. static void encode_unknown(strbuf *buf)
  23. {
  24. static const uint8_t repl[] = {239, 191, 189};
  25. strbuf_put(buf, repl, 3);
  26. }
  27. int utf8proc_charlen(const uint8_t *str, int str_len)
  28. {
  29. int length, i;
  30. if (!str_len)
  31. return 0;
  32. length = utf8proc_utf8class[str[0]];
  33. if (!length)
  34. return -1;
  35. if (str_len >= 0 && length > str_len)
  36. return -str_len;
  37. for (i = 1; i < length; i++) {
  38. if ((str[i] & 0xC0) != 0x80)
  39. return -i;
  40. }
  41. return length;
  42. }
  43. void utf8proc_detab(strbuf *ob, const uint8_t *line, size_t size)
  44. {
  45. static const uint8_t whitespace[] = " ";
  46. size_t i = 0, tab = 0;
  47. while (i < size) {
  48. size_t org = i;
  49. while (i < size && line[i] != '\t' && line[i] <= 0x80) {
  50. i++; tab++;
  51. }
  52. if (i > org)
  53. strbuf_put(ob, line + org, i - org);
  54. if (i >= size)
  55. break;
  56. if (line[i] == '\t') {
  57. int numspaces = 4 - (tab % 4);
  58. strbuf_put(ob, whitespace, numspaces);
  59. i += 1;
  60. tab += numspaces;
  61. } else {
  62. int charlen = utf8proc_charlen(line + i, size - i);
  63. if (charlen >= 0) {
  64. strbuf_put(ob, line + i, charlen);
  65. } else {
  66. encode_unknown(ob);
  67. charlen = -charlen;
  68. }
  69. i += charlen;
  70. tab += 1;
  71. }
  72. }
  73. }
  74. int utf8proc_iterate(const uint8_t *str, int str_len, int32_t *dst)
  75. {
  76. int length;
  77. int32_t uc = -1;
  78. *dst = -1;
  79. length = utf8proc_charlen(str, str_len);
  80. if (length < 0)
  81. return -1;
  82. switch (length) {
  83. case 1:
  84. uc = str[0];
  85. break;
  86. case 2:
  87. uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
  88. if (uc < 0x80) uc = -1;
  89. break;
  90. case 3:
  91. uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
  92. + (str[2] & 0x3F);
  93. if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
  94. (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
  95. break;
  96. case 4:
  97. uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
  98. + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
  99. if (uc < 0x10000 || uc >= 0x110000) uc = -1;
  100. break;
  101. }
  102. if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE))
  103. return -1;
  104. *dst = uc;
  105. return length;
  106. }
  107. void utf8proc_encode_char(int32_t uc, strbuf *buf)
  108. {
  109. uint8_t dst[4];
  110. int len = 0;
  111. assert(uc >= 0);
  112. if (uc < 0x80) {
  113. dst[0] = uc;
  114. len = 1;
  115. } else if (uc < 0x800) {
  116. dst[0] = 0xC0 + (uc >> 6);
  117. dst[1] = 0x80 + (uc & 0x3F);
  118. len = 2;
  119. } else if (uc == 0xFFFF) {
  120. dst[0] = 0xFF;
  121. len = 1;
  122. } else if (uc == 0xFFFE) {
  123. dst[0] = 0xFE;
  124. len = 1;
  125. } else if (uc < 0x10000) {
  126. dst[0] = 0xE0 + (uc >> 12);
  127. dst[1] = 0x80 + ((uc >> 6) & 0x3F);
  128. dst[2] = 0x80 + (uc & 0x3F);
  129. len = 3;
  130. } else if (uc < 0x110000) {
  131. dst[0] = 0xF0 + (uc >> 18);
  132. dst[1] = 0x80 + ((uc >> 12) & 0x3F);
  133. dst[2] = 0x80 + ((uc >> 6) & 0x3F);
  134. dst[3] = 0x80 + (uc & 0x3F);
  135. len = 4;
  136. } else {
  137. encode_unknown(buf);
  138. return;
  139. }
  140. strbuf_put(buf, dst, len);
  141. }
  142. void utf8proc_case_fold(strbuf *dest, const uint8_t *str, int len)
  143. {
  144. int32_t c;
  145. #define bufpush(x) \
  146. utf8proc_encode_char(x, dest)
  147. while (len > 0) {
  148. int char_len = utf8proc_iterate(str, len, &c);
  149. if (char_len >= 0) {
  150. #include "case_fold_switch.inc"
  151. } else {
  152. encode_unknown(dest);
  153. char_len = -char_len;
  154. }
  155. str += char_len;
  156. len -= char_len;
  157. }
  158. }