pct_encoded_rule.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. namespace implementation_defined {
  18. template<class CharSet>
  19. struct pct_encoded_rule_t
  20. {
  21. using value_type = pct_string_view;
  22. system::result<value_type>
  23. parse(
  24. char const*& it,
  25. char const* end) const noexcept;
  26. constexpr
  27. pct_encoded_rule_t(
  28. CharSet const& cs) noexcept
  29. : cs_(cs)
  30. {
  31. }
  32. private:
  33. CharSet cs_;
  34. };
  35. } // implementation_defined
  36. /** Rule for a string with percent-encoded escapes
  37. This function returns a rule which matches
  38. a percent-encoded string, permitting characters
  39. in the string which are also in the specified
  40. character set to be used unescaped.
  41. @par Value Type
  42. @code
  43. using value_type = pct_string_view;
  44. @endcode
  45. @par Example
  46. Rules are used with the function @ref grammar::parse.
  47. @code
  48. // pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  49. system::result< pct_string_view > rv = grammar::parse( "Program%20Files", pct_encoded_rule( pchars ) );
  50. @endcode
  51. @par BNF
  52. @code
  53. pct-encoded = "%" HEXDIG HEXDIG
  54. @endcode
  55. @param cs The character set indicating
  56. which characters are allowed without escapes.
  57. Any character which is not in this set must be
  58. escaped, or else parsing returns an error.
  59. @return A rule object.
  60. @par Specification
  61. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1">
  62. 2.1. Percent-Encoding (rfc3986)</a>
  63. @see
  64. @ref grammar::parse,
  65. @ref pchars,
  66. @ref pct_string_view.
  67. */
  68. template<BOOST_URL_CONSTRAINT(grammar::CharSet) CS>
  69. constexpr
  70. auto
  71. pct_encoded_rule(CS const& cs) noexcept ->
  72. implementation_defined::pct_encoded_rule_t<CS>
  73. {
  74. // If an error occurs here it means that
  75. // the value of your type does not meet
  76. // the requirements. Please check the
  77. // documentation!
  78. static_assert(
  79. grammar::is_charset<CS>::value,
  80. "CharSet requirements not met");
  81. return implementation_defined::pct_encoded_rule_t<CS>(cs);
  82. }
  83. } // urls
  84. } // boost
  85. #include <boost/url/rfc/impl/pct_encoded_rule.hpp>
  86. #endif