string.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <jvalidate/_config.h>
  7. #include <memory>
  8. #include <string>
  9. #include <string_view>
  10. #ifdef JVALIDATE_HAS_IDNA
  11. #include <ada/idna/unicode_transcoding.h>
  12. #endif
  13. #include <jvalidate/detail/expect.h>
  14. namespace jvalidate::detail {
  15. inline size_t length_u8(std::string_view arg) { return arg.length(); }
  16. inline size_t length_u32(std::u32string_view arg) { return arg.length(); }
  17. inline std::string_view to_u8(std::string_view arg) { return arg; }
  18. inline std::u32string_view to_u32(std::u32string_view arg) { return arg; }
  19. }
  20. #ifdef JVALIDATE_HAS_IDNA
  21. namespace jvalidate::detail {
  22. /**
  23. * @brief Calclates the string-length of the argument, treating multi-byte
  24. * characters as their individual bytes (as if the string was a std::string).
  25. *
  26. * @param arg A string encoded in UTF32
  27. *
  28. * @returns A number no greater than 4 * arg.length(), depending on the number
  29. * of graphemes/codepoints in the string.
  30. */
  31. inline size_t length_u8(std::u32string_view arg) {
  32. return ada::idna::utf8_length_from_utf32(arg.data(), arg.length());
  33. }
  34. /**
  35. * @brief Calclates the string-length of the argument, treating multi-byte
  36. * characters and unicode graphemes as single characters (which std::string
  37. * cannot do).
  38. *
  39. * @param arg Any UTF8 compatible string (including a standard ASCII string)
  40. *
  41. * @returns A number no greater than arg.length(), depending on the number of
  42. * graphemes/codepoints in the string.
  43. */
  44. inline size_t length_u32(std::string_view arg) {
  45. return ada::idna::utf32_length_from_utf8(arg.data(), arg.length());
  46. }
  47. inline std::string to_u8(std::u32string_view str) {
  48. auto data = std::make_unique_for_overwrite<char[]>(4 * str.length());
  49. size_t bytes = ada::idna::utf32_to_utf8(str.data(), str.length(), data.get());
  50. return std::string(data.get(), data.get() + bytes);
  51. }
  52. inline std::u32string to_u32(std::string_view str) {
  53. auto data = std::make_unique_for_overwrite<char32_t[]>(str.length());
  54. size_t bytes = ada::idna::utf8_to_utf32(str.data(), str.length(), data.get());
  55. return std::u32string(data.get(), data.get() + bytes);
  56. }
  57. }
  58. #endif