1
0

unicode.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "unicode.h"
  2. #include "unicode-data.h"
  3. #include <cassert>
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include <map>
  7. #include <stdexcept>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <utility>
  11. #include <vector>
  12. static std::string unicode_cpts_to_utf8(const std::vector<uint32_t> & cps) {
  13. std::string result;
  14. for (size_t i = 0; i < cps.size(); ++i) {
  15. result.append(unicode_cpt_to_utf8(cps[i]));
  16. }
  17. return result;
  18. }
  19. static uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset) {
  20. assert(offset < utf8.size());
  21. if (!(utf8[offset + 0] & 0x80)) {
  22. auto result = utf8[offset + 0];
  23. offset += 1;
  24. return result;
  25. }
  26. if (!(utf8[offset + 0] & 0x40)) {
  27. throw std::invalid_argument("invalid character");
  28. }
  29. if (!(utf8[offset + 0] & 0x20)) {
  30. if (offset + 1 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80)) {
  31. throw std::invalid_argument("invalid character");
  32. }
  33. auto result = ((utf8[offset + 0] & 0x1f) << 6) | (utf8[offset + 1] & 0x3f);
  34. offset += 2;
  35. return result;
  36. }
  37. if (!(utf8[offset + 0] & 0x10)) {
  38. if (offset + 2 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80)) {
  39. throw std::invalid_argument("invalid character");
  40. }
  41. auto result = ((utf8[offset + 0] & 0x0f) << 12) | ((utf8[offset + 1] & 0x3f) << 6) | (utf8[offset + 2] & 0x3f);
  42. offset += 3;
  43. return result;
  44. }
  45. if (!(utf8[offset + 0] & 0x08)) {
  46. if (offset + 3 >= utf8.size() || ! ((utf8[offset + 1] & 0xc0) == 0x80) || ! ((utf8[offset + 2] & 0xc0) == 0x80) || !((utf8[offset + 3] & 0xc0) == 0x80)) {
  47. throw std::invalid_argument("invalid character");
  48. }
  49. auto result = ((utf8[offset + 0] & 0x07) << 18) | ((utf8[offset + 1] & 0x3f) << 12) | ((utf8[offset + 2] & 0x3f) << 6) | (utf8[offset + 3] & 0x3f);
  50. offset += 4;
  51. return result;
  52. }
  53. throw std::invalid_argument("invalid string");
  54. }
  55. static std::vector<uint16_t> unicode_cpt_to_utf16(uint32_t cp) {
  56. std::vector<uint16_t> result;
  57. if (/* 0x0000 <= cp && */ cp <= 0xffff) {
  58. result.emplace_back(cp);
  59. }
  60. else if (0x10000 <= cp && cp <= 0x10ffff) {
  61. result.emplace_back(0xd800 | ((cp - 0x10000) >> 10));
  62. result.emplace_back(0xdc00 | ((cp - 0x10000) & 0x03ff));
  63. }
  64. else {
  65. throw std::invalid_argument("invalid cpt");
  66. }
  67. return result;
  68. }
  69. //static std::vector<uint16_t> unicode_cpts_to_utf16(const std::vector<uint32_t> & cps) {
  70. // std::vector<uint16_t> result;
  71. // for (size_t i = 0; i < cps.size(); ++i) {
  72. // auto temp = unicode_cpt_to_utf16(cps[i]);
  73. // result.insert(result.end(), temp.begin(), temp.end());
  74. // }
  75. // return result;
  76. //}
  77. static uint32_t cpt_from_utf16(const std::vector<uint16_t> & utf16, size_t & offset) {
  78. assert(offset < utf16.size());
  79. if (((utf16[0] >> 10) << 10) != 0xd800) {
  80. auto result = utf16[offset + 0];
  81. offset += 1;
  82. return result;
  83. }
  84. if (offset + 1 >= utf16.size() || !((utf16[1] & 0xdc00) == 0xdc00)) {
  85. throw std::invalid_argument("invalid character");
  86. }
  87. auto result = 0x10000 + (((utf16[0] & 0x03ff) << 10) | (utf16[1] & 0x03ff));
  88. offset += 2;
  89. return result;
  90. }
  91. //static std::vector<uint32_t> unicode_cpts_from_utf16(const std::vector<uint16_t> & utf16) {
  92. // std::vector<uint32_t> result;
  93. // size_t offset = 0;
  94. // while (offset < utf16.size()) {
  95. // result.push_back(cpt_from_utf16(utf16, offset));
  96. // }
  97. // return result;
  98. //}
  99. static std::unordered_map<uint32_t, int> unicode_cpt_type_map() {
  100. std::unordered_map<uint32_t, int> cpt_types;
  101. for (auto p : unicode_ranges_digit) {
  102. for (auto i = p.first; i <= p.second; ++ i) {
  103. cpt_types[i] = CODEPOINT_TYPE_DIGIT;
  104. }
  105. }
  106. for (auto p : unicode_ranges_letter) {
  107. for (auto i = p.first; i <= p.second; ++ i) {
  108. cpt_types[i] = CODEPOINT_TYPE_LETTER;
  109. }
  110. }
  111. for (auto p : unicode_ranges_whitespace) {
  112. for (auto i = p.first; i <= p.second; ++ i) {
  113. cpt_types[i] = CODEPOINT_TYPE_WHITESPACE;
  114. }
  115. }
  116. for (auto p : unicode_ranges_accent_mark) {
  117. for (auto i = p.first; i <= p.second; ++ i) {
  118. cpt_types[i] = CODEPOINT_TYPE_ACCENT_MARK;
  119. }
  120. }
  121. for (auto p : unicode_ranges_punctuation) {
  122. for (auto i = p.first; i <= p.second; ++ i) {
  123. cpt_types[i] = CODEPOINT_TYPE_PUNCTUATION;
  124. }
  125. }
  126. for (auto p : unicode_ranges_symbol) {
  127. for (auto i = p.first; i <= p.second; ++i) {
  128. cpt_types[i] = CODEPOINT_TYPE_SYMBOL;
  129. }
  130. }
  131. for (auto p : unicode_ranges_control) {
  132. for (auto i = p.first; i <= p.second; ++ i) {
  133. cpt_types[i] = CODEPOINT_TYPE_CONTROL;
  134. }
  135. }
  136. return cpt_types;
  137. }
  138. static std::unordered_map<uint8_t, std::string> unicode_byte_to_utf8_map() {
  139. std::unordered_map<uint8_t, std::string> map;
  140. for (int ch = u'!'; ch <= u'~'; ++ch) {
  141. assert(0 <= ch && ch < 256);
  142. map[ch] = unicode_cpt_to_utf8(ch);
  143. }
  144. for (int ch = u'¡'; ch <= u'¬'; ++ch) {
  145. assert(0 <= ch && ch < 256);
  146. map[ch] = unicode_cpt_to_utf8(ch);
  147. }
  148. for (int ch = u'®'; ch <= u'ÿ'; ++ch) {
  149. assert(0 <= ch && ch < 256);
  150. map[ch] = unicode_cpt_to_utf8(ch);
  151. }
  152. auto n = 0;
  153. for (int ch = 0; ch < 256; ++ch) {
  154. if (map.find(ch) == map.end()) {
  155. map[ch] = unicode_cpt_to_utf8(256 + n);
  156. ++n;
  157. }
  158. }
  159. return map;
  160. }
  161. static std::unordered_map<std::string, uint8_t> unicode_utf8_to_byte_map() {
  162. std::unordered_map<std::string, uint8_t> map;
  163. for (int ch = u'!'; ch <= u'~'; ++ch) {
  164. assert(0 <= ch && ch < 256);
  165. map[unicode_cpt_to_utf8(ch)] = ch;
  166. }
  167. for (int ch = u'¡'; ch <= u'¬'; ++ch) {
  168. assert(0 <= ch && ch < 256);
  169. map[unicode_cpt_to_utf8(ch)] = ch;
  170. }
  171. for (int ch = u'®'; ch <= u'ÿ'; ++ch) {
  172. assert(0 <= ch && ch < 256);
  173. map[unicode_cpt_to_utf8(ch)] = ch;
  174. }
  175. auto n = 0;
  176. for (int ch = 0; ch < 256; ++ch) {
  177. if (map.find(unicode_cpt_to_utf8(ch)) == map.end()) {
  178. map[unicode_cpt_to_utf8(256 + n)] = ch;
  179. ++n;
  180. }
  181. }
  182. return map;
  183. }
  184. //
  185. // interface
  186. //
  187. std::string unicode_cpt_to_utf8(uint32_t cp) {
  188. std::string result;
  189. if (/* 0x00 <= cp && */ cp <= 0x7f) {
  190. result.push_back(cp);
  191. }
  192. else if (0x80 <= cp && cp <= 0x7ff) {
  193. result.push_back(0xc0 | ((cp >> 6) & 0x1f));
  194. result.push_back(0x80 | (cp & 0x3f));
  195. }
  196. else if (0x800 <= cp && cp <= 0xffff) {
  197. result.push_back(0xe0 | ((cp >> 12) & 0x0f));
  198. result.push_back(0x80 | ((cp >> 6) & 0x3f));
  199. result.push_back(0x80 | (cp & 0x3f));
  200. }
  201. else if (0x10000 <= cp && cp <= 0x10ffff) {
  202. result.push_back(0xf0 | ((cp >> 18) & 0x07));
  203. result.push_back(0x80 | ((cp >> 12) & 0x3f));
  204. result.push_back(0x80 | ((cp >> 6) & 0x3f));
  205. result.push_back(0x80 | (cp & 0x3f));
  206. }
  207. else {
  208. throw std::invalid_argument("invalid codepoint");
  209. }
  210. return result;
  211. }
  212. std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts) {
  213. std::vector<uint32_t> result;
  214. result.reserve(cpts.size());
  215. for (size_t i = 0; i < cpts.size(); ++i) {
  216. auto it = unicode_map_nfd.find(cpts[i]);
  217. if (it == unicode_map_nfd.end()) {
  218. result.push_back(cpts[i]);
  219. } else {
  220. result.push_back(it->second);
  221. }
  222. }
  223. return result;
  224. }
  225. std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8) {
  226. std::vector<uint32_t> result;
  227. size_t offset = 0;
  228. while (offset < utf8.size()) {
  229. result.push_back(unicode_cpt_from_utf8(utf8, offset));
  230. }
  231. return result;
  232. }
  233. int unicode_cpt_type(uint32_t cp) {
  234. static std::unordered_map<uint32_t, int> cpt_types = unicode_cpt_type_map();
  235. const auto it = cpt_types.find(cp);
  236. return it == cpt_types.end() ? CODEPOINT_TYPE_UNIDENTIFIED : it->second;
  237. }
  238. int unicode_cpt_type(const std::string & utf8) {
  239. if (utf8.length() == 0) {
  240. return CODEPOINT_TYPE_UNIDENTIFIED;
  241. }
  242. size_t offset = 0;
  243. return unicode_cpt_type(unicode_cpt_from_utf8(utf8, offset));
  244. }
  245. std::string unicode_byte_to_utf8(uint8_t byte) {
  246. static std::unordered_map<uint8_t, std::string> map = unicode_byte_to_utf8_map();
  247. return map.at(byte);
  248. }
  249. uint8_t unicode_utf8_to_byte(const std::string & utf8) {
  250. static std::unordered_map<std::string, uint8_t> map = unicode_utf8_to_byte_map();
  251. return map.at(utf8);
  252. }
  253. char32_t unicode_tolower(char32_t cp) {
  254. auto it = unicode_map_lowercase.find(cp);
  255. return it == unicode_map_lowercase.end() ? cp : it->second;
  256. }