unicode.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include <cstdint>
  3. #include <string>
  4. #include <vector>
  5. struct unicode_cpt_flags {
  6. enum {
  7. UNDEFINED = 0x0001,
  8. NUMBER = 0x0002, // regex: \p{N}
  9. LETTER = 0x0004, // regex: \p{L}
  10. SEPARATOR = 0x0008, // regex: \p{Z}
  11. ACCENT_MARK = 0x0010, // regex: \p{M}
  12. PUNCTUATION = 0x0020, // regex: \p{P}
  13. SYMBOL = 0x0040, // regex: \p{S}
  14. CONTROL = 0x0080, // regex: \p{C}
  15. MASK_CATEGORIES = 0x00FF,
  16. };
  17. // codepoint type
  18. uint16_t is_undefined : 1;
  19. uint16_t is_number : 1; // regex: \p{N}
  20. uint16_t is_letter : 1; // regex: \p{L}
  21. uint16_t is_separator : 1; // regex: \p{Z}
  22. uint16_t is_accent_mark : 1; // regex: \p{M}
  23. uint16_t is_punctuation : 1; // regex: \p{P}
  24. uint16_t is_symbol : 1; // regex: \p{S}
  25. uint16_t is_control : 1; // regex: \p{C}
  26. // helper flags
  27. uint16_t is_whitespace : 1; // regex: \s
  28. uint16_t is_lowercase : 1;
  29. uint16_t is_uppercase : 1;
  30. uint16_t is_nfd : 1;
  31. // decode from uint16
  32. inline unicode_cpt_flags(const uint16_t flags = 0) {
  33. *reinterpret_cast<uint16_t*>(this) = flags;
  34. }
  35. inline uint16_t as_uint() const {
  36. return *reinterpret_cast<const uint16_t*>(this);
  37. }
  38. inline uint16_t category_flag() const {
  39. return this->as_uint() & MASK_CATEGORIES;
  40. }
  41. };
  42. size_t unicode_len_utf8(char src);
  43. std::string unicode_cpt_to_utf8 (uint32_t cpt);
  44. uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset);
  45. std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8);
  46. std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts);
  47. unicode_cpt_flags unicode_cpt_flags_from_cpt (uint32_t cpt);
  48. unicode_cpt_flags unicode_cpt_flags_from_utf8(const std::string & utf8);
  49. std::string unicode_byte_to_utf8(uint8_t byte);
  50. uint8_t unicode_utf8_to_byte(const std::string & utf8);
  51. uint32_t unicode_tolower(uint32_t cpt);
  52. std::vector<std::string> unicode_regex_split(const std::string & text, const std::vector<std::string> & regex_exprs);