unicode.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * llama.cpp - commit 3f1ae2e32cde00c39b96be6d01c2997c29bae555 - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include <cstdint>
  28. #include <string>
  29. #include <vector>
  30. // TODO: prefix all symbols with "llama_"
  31. struct codepoint_flags {
  32. enum {
  33. UNDEFINED = 0x0001,
  34. NUMBER = 0x0002, // regex: \p{N}
  35. LETTER = 0x0004, // regex: \p{L}
  36. SEPARATOR = 0x0008, // regex: \p{Z}
  37. ACCENT_MARK = 0x0010, // regex: \p{M}
  38. PUNCTUATION = 0x0020, // regex: \p{P}
  39. SYMBOL = 0x0040, // regex: \p{S}
  40. CONTROL = 0x0080, // regex: \p{C}
  41. MASK_CATEGORIES = 0x00FF,
  42. };
  43. // codepoint type
  44. uint16_t is_undefined : 1;
  45. uint16_t is_number : 1; // regex: \p{N}
  46. uint16_t is_letter : 1; // regex: \p{L}
  47. uint16_t is_separator : 1; // regex: \p{Z}
  48. uint16_t is_accent_mark : 1; // regex: \p{M}
  49. uint16_t is_punctuation : 1; // regex: \p{P}
  50. uint16_t is_symbol : 1; // regex: \p{S}
  51. uint16_t is_control : 1; // regex: \p{C}
  52. // helper flags
  53. uint16_t is_whitespace : 1; // regex: \s
  54. uint16_t is_lowercase : 1;
  55. uint16_t is_uppercase : 1;
  56. uint16_t is_nfd : 1;
  57. // decode from uint16
  58. inline codepoint_flags(const uint16_t flags=0) {
  59. *reinterpret_cast<uint16_t*>(this) = flags;
  60. }
  61. inline uint16_t as_uint() const {
  62. return *reinterpret_cast<const uint16_t*>(this);
  63. }
  64. inline uint16_t category_flag() const {
  65. return this->as_uint() & MASK_CATEGORIES;
  66. }
  67. };
  68. size_t unicode_len_utf8(char src);
  69. std::string unicode_cpt_to_utf8(uint32_t cp);
  70. uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset);
  71. std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8);
  72. std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts);
  73. codepoint_flags unicode_cpt_flags(const uint32_t cp);
  74. codepoint_flags unicode_cpt_flags(const std::string & utf8);
  75. std::string unicode_byte_to_utf8(uint8_t byte);
  76. uint8_t unicode_utf8_to_byte(const std::string & utf8);
  77. uint32_t unicode_tolower(uint32_t cp);
  78. std::vector<std::string> unicode_regex_split(const std::string & text, const std::vector<std::string> & regex_exprs);