string.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Utility functions for managing strings, specifically because C++'s
  3. * std::string/std::regex is not well suited for UTF8 comprehensions.
  4. */
  5. #pragma once
  6. #include <ostream>
  7. #include <string>
  8. #include <string_view>
  9. #if __has_include(<unicode/std_string.h>)
  10. #define JVALIDATE_HAS_ICU
  11. #include <unicode/brkiter.h>
  12. #include <unicode/unistr.h>
  13. #endif
  14. #include <jvalidate/detail/expect.h>
  15. #ifdef JVALIDATE_HAS_ICU
  16. namespace jvalidate::detail {
  17. /**
  18. * @brief Calclates the string-length of the argument, treating multi-byte
  19. * characters an unicode graphemes as single characters (which std::string
  20. * cannot do).
  21. *
  22. * @param arg Any UTF8 compatible string (including a standard ASCII string)
  23. *
  24. * @returns A number no greater than arg.size(), depending on the number of
  25. * graphemes/codepoints in the string.
  26. */
  27. inline size_t length(std::string_view arg) {
  28. icu::UnicodeString ucs = icu::UnicodeString::fromUTF8(icu::StringPiece(arg));
  29. return ucs.countChar32();
  30. }
  31. /**
  32. * @brief Ensures that any codepoints/graphemes in the given regular expression
  33. * are wrapped in parenthesis in order to ensure that e.g. <PIRATE-EMOJI>*
  34. * properly matches the entire emoji multiple times, instead of just the last
  35. * byte of the string.
  36. *
  37. * Because we are only performing a regex search, and not matching/capturing
  38. * groups - we don't care that all of these extra parenthesis cause us to
  39. * generate new capture-groups or push some of the groups to a later point.
  40. *
  41. * @param arg A regular expression string, to be sanitized for UTF8 pattern-
  42. * matching.
  43. *
  44. * @returns The regular expression, with some more parenthesis added.
  45. */
  46. inline std::string regex_escape(std::string_view arg) {
  47. icu::UnicodeString const ucs = icu::UnicodeString::fromUTF8(icu::StringPiece(arg));
  48. // Short-circuit if there are no multi-byte codepoints or graphemes, since
  49. // C++ regexes don't have any problems with those.
  50. if (ucs.countChar32() == arg.size()) {
  51. return std::string(arg);
  52. }
  53. UErrorCode status = U_ZERO_ERROR;
  54. // createCharacterInstance directly uses new - without any special allocation
  55. // rules or cleanup, since the first argument is NULL.
  56. std::unique_ptr<icu::BreakIterator> iter(
  57. icu::BreakIterator::createCharacterInstance(NULL, status));
  58. // This should never occur - unless there's like an alloc error
  59. if (U_FAILURE(status)) {
  60. return std::string(arg);
  61. }
  62. icu::UnicodeString rval;
  63. iter->setText(ucs);
  64. int32_t start = iter->first();
  65. int32_t end = iter->next();
  66. while (end != icu::BreakIterator::DONE) {
  67. // 0-or-1, 1-or-more, 0-or-more markings
  68. // This could be optimized to only operate when on a multibyte character
  69. if (std::strchr("?*+", ucs.charAt(end))) {
  70. rval.append('(');
  71. rval.append(ucs, start, end - start);
  72. rval.append(')');
  73. rval.append(ucs.char32At(end));
  74. end = iter->next();
  75. } else {
  76. rval.append(ucs, start, end - start);
  77. }
  78. start = end;
  79. end = iter->next();
  80. }
  81. std::string out;
  82. return rval.toUTF8String(out);
  83. }
  84. inline std::string_view to_u8(std::string_view arg) { return arg; }
  85. inline std::string to_u8(std::u32string_view arg) {
  86. icu::UnicodeString const ucs =
  87. icu::UnicodeString::fromUTF32(reinterpret_cast<int const *>(arg.data()), arg.size());
  88. std::string out;
  89. return ucs.toUTF8String(out);
  90. }
  91. inline std::u32string_view to_u32(std::u32string_view arg) { return arg; }
  92. inline std::u32string to_u32(std::string_view arg) {
  93. icu::UnicodeString const ucs = icu::UnicodeString::fromUTF8(icu::StringPiece(arg));
  94. std::u32string rval;
  95. size_t const capacity = ucs.countChar32();
  96. rval.resize(capacity);
  97. UErrorCode status = U_ZERO_ERROR;
  98. ucs.toUTF32(reinterpret_cast<int *>(rval.data()), capacity, status);
  99. // This should never occur - unless there's like an alloc error
  100. if (U_FAILURE(status)) {
  101. JVALIDATE_THROW(std::runtime_error, "UTF-32 Translation Error");
  102. }
  103. return rval;
  104. }
  105. }
  106. namespace std {
  107. inline std::ostream & operator<<(std::ostream & os, std::u32string_view str) {
  108. return os << jvalidate::detail::to_u8(str);
  109. }
  110. }
  111. #else
  112. #endif