unicode.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include <cstdint>
  3. #include <string>
  4. #include <vector>
  5. // TODO: reimplement this structure in endian-independent way
  6. struct unicode_cpt_flags {
  7. enum {
  8. UNDEFINED = 0x0001,
  9. NUMBER = 0x0002, // regex: \p{N}
  10. LETTER = 0x0004, // regex: \p{L}
  11. SEPARATOR = 0x0008, // regex: \p{Z}
  12. ACCENT_MARK = 0x0010, // regex: \p{M}
  13. PUNCTUATION = 0x0020, // regex: \p{P}
  14. SYMBOL = 0x0040, // regex: \p{S}
  15. CONTROL = 0x0080, // regex: \p{C}
  16. MASK_CATEGORIES = 0x00FF,
  17. WHITESPACE = 0x0100,
  18. LOWERCASE = 0x0200,
  19. UPPERCASE = 0x0400,
  20. NFD = 0x0800,
  21. };
  22. // codepoint type
  23. uint16_t is_undefined : 1;
  24. uint16_t is_number : 1; // regex: \p{N}
  25. uint16_t is_letter : 1; // regex: \p{L}
  26. uint16_t is_separator : 1; // regex: \p{Z}
  27. uint16_t is_accent_mark : 1; // regex: \p{M}
  28. uint16_t is_punctuation : 1; // regex: \p{P}
  29. uint16_t is_symbol : 1; // regex: \p{S}
  30. uint16_t is_control : 1; // regex: \p{C}
  31. // helper flags
  32. uint16_t is_whitespace : 1; // regex: \s
  33. uint16_t is_lowercase : 1;
  34. uint16_t is_uppercase : 1;
  35. uint16_t is_nfd : 1;
  36. // decode from uint16
  37. inline unicode_cpt_flags(const uint16_t flags = 0) {
  38. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  39. *reinterpret_cast<uint16_t*>(this) = flags;
  40. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  41. is_undefined = (flags & UNDEFINED) ? 1 : 0;
  42. is_number = (flags & NUMBER) ? 1 : 0;
  43. is_letter = (flags & LETTER) ? 1 : 0;
  44. is_separator = (flags & SEPARATOR) ? 1 : 0;
  45. is_accent_mark = (flags & ACCENT_MARK) ? 1 : 0;
  46. is_punctuation = (flags & PUNCTUATION) ? 1 : 0;
  47. is_symbol = (flags & SYMBOL) ? 1 : 0;
  48. is_control = (flags & CONTROL) ? 1 : 0;
  49. is_whitespace = (flags & WHITESPACE) ? 1 : 0;
  50. is_lowercase = (flags & LOWERCASE) ? 1 : 0;
  51. is_uppercase = (flags & UPPERCASE) ? 1 : 0;
  52. is_nfd = (flags & NFD) ? 1 : 0;
  53. #else
  54. #error Unexpected or undefined __BYTE_ORDER__
  55. #endif
  56. }
  57. inline uint16_t as_uint() const {
  58. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  59. return *reinterpret_cast<const uint16_t*>(this);
  60. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  61. uint16_t result =
  62. is_undefined * UNDEFINED
  63. + is_number * NUMBER
  64. + is_letter * LETTER
  65. + is_separator * SEPARATOR
  66. + is_accent_mark * ACCENT_MARK
  67. + is_punctuation * PUNCTUATION
  68. + is_symbol * SYMBOL
  69. + is_control * CONTROL
  70. + is_whitespace * WHITESPACE
  71. + is_lowercase * LOWERCASE
  72. + is_uppercase * UPPERCASE
  73. + is_nfd * NFD
  74. ;
  75. return result;
  76. #else
  77. #error Unexpected or undefined __BYTE_ORDER__
  78. #endif
  79. }
  80. inline uint16_t category_flag() const {
  81. return this->as_uint() & MASK_CATEGORIES;
  82. }
  83. };
  84. size_t unicode_len_utf8(char src);
  85. std::string unicode_cpt_to_utf8 (uint32_t cpt);
  86. uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset);
  87. std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8);
  88. std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts);
  89. unicode_cpt_flags unicode_cpt_flags_from_cpt (uint32_t cpt);
  90. unicode_cpt_flags unicode_cpt_flags_from_utf8(const std::string & utf8);
  91. std::string unicode_byte_to_utf8(uint8_t byte);
  92. uint8_t unicode_utf8_to_byte(const std::string & utf8);
  93. uint32_t unicode_tolower(uint32_t cpt);
  94. bool unicode_cpt_is_han(uint32_t cpt);
  95. std::vector<std::string> unicode_regex_split(const std::string & text, const std::vector<std::string> & regex_exprs);