pct_encoded_rule.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/url
  8. //
  9. #ifndef BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
  10. #define BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
  11. #include <boost/url/detail/config.hpp>
  12. #include <boost/url/error_types.hpp>
  13. #include <boost/url/pct_string_view.hpp>
  14. #include <boost/url/grammar/charset.hpp>
  15. namespace boost {
  16. namespace urls {
  17. /** Rule for a string with percent-encoded escapes
  18. This function returns a rule which matches
  19. a percent-encoded string, permitting characters
  20. in the string which are also in the specified
  21. character set to be used unescaped.
  22. @par Value Type
  23. @code
  24. using value_type = pct_string_view;
  25. @endcode
  26. @par Example
  27. Rules are used with the function @ref grammar::parse.
  28. @code
  29. // pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  30. system::result< pct_string_view > rv = grammar::parse( "Program%20Files", pct_encoded_rule( pchars ) );
  31. @endcode
  32. @par BNF
  33. @code
  34. pct-encoded = "%" HEXDIG HEXDIG
  35. @endcode
  36. @param cs The character set indicating
  37. which characters are allowed without escapes.
  38. Any character which is not in this set must be
  39. escaped, or else parsing returns an error.
  40. @par Specification
  41. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1">
  42. 2.1. Percent-Encoding (rfc3986)</a>
  43. @see
  44. @ref grammar::parse,
  45. @ref pchars,
  46. @ref pct_string_view.
  47. */
  48. #ifdef BOOST_URL_DOCS
  49. /**@{*/
  50. template<class CharSet>
  51. constexpr
  52. __implementation_defined__
  53. pct_encoded_rule( CharSet const& cs ) noexcept;
  54. /**@}*/
  55. #else
  56. template<class CharSet>
  57. struct pct_encoded_rule_t
  58. {
  59. using value_type = pct_string_view;
  60. template<class CharSet_>
  61. friend
  62. constexpr
  63. auto
  64. pct_encoded_rule(
  65. CharSet_ const& cs) noexcept ->
  66. pct_encoded_rule_t<CharSet_>;
  67. system::result<value_type>
  68. parse(
  69. char const*& it,
  70. char const* end) const noexcept;
  71. private:
  72. constexpr
  73. pct_encoded_rule_t(
  74. CharSet const& cs) noexcept
  75. : cs_(cs)
  76. {
  77. }
  78. CharSet cs_;
  79. };
  80. template<class CharSet>
  81. constexpr
  82. auto
  83. pct_encoded_rule(
  84. CharSet const& cs) noexcept ->
  85. pct_encoded_rule_t<CharSet>
  86. {
  87. // If an error occurs here it means that
  88. // the value of your type does not meet
  89. // the requirements. Please check the
  90. // documentation!
  91. static_assert(
  92. grammar::is_charset<CharSet>::value,
  93. "CharSet requirements not met");
  94. return pct_encoded_rule_t<CharSet>(cs);
  95. }
  96. #endif
  97. } // urls
  98. } // boost
  99. #include <boost/url/rfc/impl/pct_encoded_rule.hpp>
  100. #endif