string.h 3.5 KB

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