transcode_view.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2020 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_PARSER_TRANSCODE_VIEW_HPP
  7. #define BOOST_PARSER_TRANSCODE_VIEW_HPP
  8. #include <boost/parser/detail/text/transcode_view.hpp>
  9. namespace boost::parser {
  10. using format = detail::text::format;
  11. /** A view that produces UTF-8 from an given sequence of UTF.
  12. \tparam V Constrained by `std::ranges::view<V>`. Additionally, the
  13. value type of `V` must be `char`, `wchar_t`, `char8_t`, `char16_t`, or
  14. `char32_t`. */
  15. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS || defined(BOOST_PARSER_DOXYGEN)
  16. template<detail::text::utf_range V>
  17. requires std::ranges::view<V>
  18. #else
  19. template<typename V>
  20. #endif
  21. class utf8_view : public detail::text::utf_view<format::utf8, V>
  22. {
  23. public:
  24. constexpr utf8_view()
  25. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS || defined(BOOST_PARSER_DOXYGEN)
  26. requires std::default_initializable<V>
  27. #endif
  28. = default;
  29. constexpr utf8_view(V base) :
  30. detail::text::utf_view<format::utf8, V>{std::move(base)}
  31. {}
  32. };
  33. /** A view that produces UTF-16 from an given sequence of UTF.
  34. \tparam V Constrained by `std::ranges::view<V>`. Additionally, the
  35. value type of `V` must be `char`, `wchar_t`, `char8_t`, `char16_t`, or
  36. `char32_t`. */
  37. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS || defined(BOOST_PARSER_DOXYGEN)
  38. template<detail::text::utf_range V>
  39. requires std::ranges::view<V>
  40. #else
  41. template<typename V>
  42. #endif
  43. class utf16_view : public detail::text::utf_view<format::utf16, V>
  44. {
  45. public:
  46. constexpr utf16_view()
  47. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS || defined(BOOST_PARSER_DOXYGEN)
  48. requires std::default_initializable<V>
  49. #endif
  50. = default;
  51. constexpr utf16_view(V base) :
  52. detail::text::utf_view<format::utf16, V>{std::move(base)}
  53. {}
  54. };
  55. /** A view that produces UTF-32 from an given sequence of UTF.
  56. \tparam V Constrained by `std::ranges::view<V>`. Additionally, the
  57. value type of `V` must be `char`, `wchar_t`, `char8_t`, `char16_t`, or
  58. `char32_t`. */
  59. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS || defined(BOOST_PARSER_DOXYGEN)
  60. template<detail::text::utf_range V>
  61. requires std::ranges::view<V>
  62. #else
  63. template<typename V>
  64. #endif
  65. class utf32_view : public detail::text::utf_view<format::utf32, V>
  66. {
  67. public:
  68. constexpr utf32_view()
  69. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS || defined(BOOST_PARSER_DOXYGEN)
  70. requires std::default_initializable<V>
  71. #endif
  72. = default;
  73. constexpr utf32_view(V base) :
  74. detail::text::utf_view<format::utf32, V>{std::move(base)}
  75. {}
  76. };
  77. #if BOOST_PARSER_DETAIL_TEXT_USE_CONCEPTS
  78. template<class R>
  79. utf8_view(R &&) -> utf8_view<std::views::all_t<R>>;
  80. template<class R>
  81. utf16_view(R &&) -> utf16_view<std::views::all_t<R>>;
  82. template<class R>
  83. utf32_view(R &&) -> utf32_view<std::views::all_t<R>>;
  84. #endif
  85. /** A view adaptor that produces a `utf8_view` of the given view. */
  86. inline constexpr auto as_utf8 = detail::text::as_utf8;
  87. /** A view adaptor that produces a `utf16_view` of the given view. */
  88. inline constexpr auto as_utf16 = detail::text::as_utf16;
  89. /** A view adaptor that produces a `utf32_view` of the given view. */
  90. inline constexpr auto as_utf32 = detail::text::as_utf32;
  91. }
  92. #endif