string.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #if __has_include(<unicode/std_string.h>)
  7. #define JVALIDATE_HAS_ICU
  8. #include <unicode/brkiter.h>
  9. #include <unicode/unistr.h>
  10. #endif
  11. namespace jvalidate::detail {
  12. /**
  13. * @brief Calclates the string-length of the argument, treating multi-byte
  14. * characters an unicode graphemes as single characters (which std::string
  15. * cannot do).
  16. *
  17. * @param arg Any UTF8 compatible string (including a standard ASCII string)
  18. *
  19. * @returns A number no greater than arg.size(), depending on the number of
  20. * graphemes/codepoints in the string.
  21. */
  22. inline size_t length(std::string_view arg) {
  23. #ifdef JVALIDATE_HAS_ICU
  24. icu::UnicodeString ucs = icu::UnicodeString::fromUTF8(icu::StringPiece(arg));
  25. return ucs.countChar32();
  26. #else
  27. return arg.size();
  28. #endif
  29. }
  30. /**
  31. * @brief Ensures that any codepoints/graphemes in the given regular expression
  32. * are wrapped in parenthesis in order to ensure that e.g. <PIRATE-EMOJI>*
  33. * properly matches the entire emoji multiple times, instead of just the last
  34. * byte of the string.
  35. *
  36. * Because we are only performing a regex search, and not matching/capturing
  37. * groups - we don't care that all of these extra parenthesis cause us to
  38. * generate new capture-groups or push some of the groups to a later point.
  39. *
  40. * @param arg A regular expression string, to be sanitized for UTF8 pattern-
  41. * matching.
  42. *
  43. * @returns The regular expression, with some more parenthesis added.
  44. */
  45. inline std::string regex_escape(std::string_view arg) {
  46. #ifdef JVALIDATE_HAS_ICU
  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 - 1);
  72. rval.append(')');
  73. rval.append(ucs.char32At(end));
  74. end = iter->next();
  75. } else {
  76. rval.append(ucs, start, end - 1);
  77. }
  78. start = end;
  79. end = iter->next();
  80. }
  81. std::string out;
  82. return rval.toUTF8String(out);
  83. #else
  84. return std::string(arg);
  85. #endif
  86. }
  87. }