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