unicode.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - 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. struct unicode_cpt_flags {
  31. enum {
  32. UNDEFINED = 0x0001,
  33. NUMBER = 0x0002, // regex: \p{N}
  34. LETTER = 0x0004, // regex: \p{L}
  35. SEPARATOR = 0x0008, // regex: \p{Z}
  36. ACCENT_MARK = 0x0010, // regex: \p{M}
  37. PUNCTUATION = 0x0020, // regex: \p{P}
  38. SYMBOL = 0x0040, // regex: \p{S}
  39. CONTROL = 0x0080, // regex: \p{C}
  40. MASK_CATEGORIES = 0x00FF,
  41. };
  42. // codepoint type
  43. uint16_t is_undefined : 1;
  44. uint16_t is_number : 1; // regex: \p{N}
  45. uint16_t is_letter : 1; // regex: \p{L}
  46. uint16_t is_separator : 1; // regex: \p{Z}
  47. uint16_t is_accent_mark : 1; // regex: \p{M}
  48. uint16_t is_punctuation : 1; // regex: \p{P}
  49. uint16_t is_symbol : 1; // regex: \p{S}
  50. uint16_t is_control : 1; // regex: \p{C}
  51. // helper flags
  52. uint16_t is_whitespace : 1; // regex: \s
  53. uint16_t is_lowercase : 1;
  54. uint16_t is_uppercase : 1;
  55. uint16_t is_nfd : 1;
  56. // decode from uint16
  57. inline unicode_cpt_flags(const uint16_t flags = 0) {
  58. *reinterpret_cast<uint16_t*>(this) = flags;
  59. }
  60. inline uint16_t as_uint() const {
  61. return *reinterpret_cast<const uint16_t*>(this);
  62. }
  63. inline uint16_t category_flag() const {
  64. return this->as_uint() & MASK_CATEGORIES;
  65. }
  66. };
  67. size_t unicode_len_utf8(char src);
  68. std::string unicode_cpt_to_utf8 (uint32_t cpt);
  69. uint32_t unicode_cpt_from_utf8(const std::string & utf8, size_t & offset);
  70. std::vector<uint32_t> unicode_cpts_from_utf8(const std::string & utf8);
  71. std::vector<uint32_t> unicode_cpts_normalize_nfd(const std::vector<uint32_t> & cpts);
  72. unicode_cpt_flags unicode_cpt_flags_from_cpt (uint32_t cpt);
  73. unicode_cpt_flags unicode_cpt_flags_from_utf8(const std::string & utf8);
  74. std::string unicode_byte_to_utf8(uint8_t byte);
  75. uint8_t unicode_utf8_to_byte(const std::string & utf8);
  76. uint32_t unicode_tolower(uint32_t cpt);
  77. std::vector<std::string> unicode_regex_split(const std::string & text, const std::vector<std::string> & regex_exprs);