| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 10 | #ifndef BOOST_URL_RFC_PCT_ENCODED_RULE_HPP | ||
| 11 | #define BOOST_URL_RFC_PCT_ENCODED_RULE_HPP | ||
| 12 | |||
| 13 | #include <boost/url/detail/config.hpp> | ||
| 14 | #include <boost/url/error_types.hpp> | ||
| 15 | #include <boost/url/pct_string_view.hpp> | ||
| 16 | #include <boost/url/grammar/charset.hpp> | ||
| 17 | |||
| 18 | namespace boost { | ||
| 19 | namespace urls { | ||
| 20 | |||
| 21 | /** Rule for a string with percent-encoded escapes | ||
| 22 | |||
| 23 | This function returns a rule which matches | ||
| 24 | a percent-encoded string, permitting characters | ||
| 25 | in the string which are also in the specified | ||
| 26 | character set to be used unescaped. | ||
| 27 | |||
| 28 | @par Value Type | ||
| 29 | @code | ||
| 30 | using value_type = pct_string_view; | ||
| 31 | @endcode | ||
| 32 | |||
| 33 | @par Example | ||
| 34 | Rules are used with the function @ref grammar::parse. | ||
| 35 | @code | ||
| 36 | // pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | ||
| 37 | |||
| 38 | system::result< pct_string_view > rv = grammar::parse( "Program%20Files", pct_encoded_rule( pchars ) ); | ||
| 39 | @endcode | ||
| 40 | |||
| 41 | @par BNF | ||
| 42 | @code | ||
| 43 | pct-encoded = "%" HEXDIG HEXDIG | ||
| 44 | @endcode | ||
| 45 | |||
| 46 | @param cs The character set indicating | ||
| 47 | which characters are allowed without escapes. | ||
| 48 | Any character which is not in this set must be | ||
| 49 | escaped, or else parsing returns an error. | ||
| 50 | |||
| 51 | @par Specification | ||
| 52 | @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1"> | ||
| 53 | 2.1. Percent-Encoding (rfc3986)</a> | ||
| 54 | |||
| 55 | @see | ||
| 56 | @ref grammar::parse, | ||
| 57 | @ref pchars, | ||
| 58 | @ref pct_string_view. | ||
| 59 | */ | ||
| 60 | #ifdef BOOST_URL_DOCS | ||
| 61 | /**@{*/ | ||
| 62 | template<class CharSet> | ||
| 63 | constexpr | ||
| 64 | __implementation_defined__ | ||
| 65 | pct_encoded_rule( CharSet const& cs ) noexcept; | ||
| 66 | /**@}*/ | ||
| 67 | #else | ||
| 68 | namespace implementation_defined { | ||
| 69 | template<class CharSet> | ||
| 70 | struct pct_encoded_rule_t | ||
| 71 | { | ||
| 72 | using value_type = pct_string_view; | ||
| 73 | |||
| 74 | system::result<value_type> | ||
| 75 | parse( | ||
| 76 | char const*& it, | ||
| 77 | char const* end) const noexcept; | ||
| 78 | |||
| 79 | constexpr | ||
| 80 | 5431 | pct_encoded_rule_t( | |
| 81 | CharSet const& cs) noexcept | ||
| 82 | 5431 | : cs_(cs) | |
| 83 | { | ||
| 84 | 5431 | } | |
| 85 | |||
| 86 | private: | ||
| 87 | CharSet cs_; | ||
| 88 | }; | ||
| 89 | } // implementation_defined | ||
| 90 | |||
| 91 | /** Rule for a string with percent-encoded escapes | ||
| 92 | |||
| 93 | This function returns a rule which matches | ||
| 94 | a percent-encoded string, permitting characters | ||
| 95 | in the string which are also in the specified | ||
| 96 | character set to be used unescaped. | ||
| 97 | |||
| 98 | @par Value Type | ||
| 99 | @code | ||
| 100 | using value_type = pct_string_view; | ||
| 101 | @endcode | ||
| 102 | |||
| 103 | @par Example | ||
| 104 | Rules are used with the function @ref grammar::parse. | ||
| 105 | @code | ||
| 106 | // pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | ||
| 107 | |||
| 108 | system::result< pct_string_view > rv = grammar::parse( "Program%20Files", pct_encoded_rule( pchars ) ); | ||
| 109 | @endcode | ||
| 110 | |||
| 111 | @par BNF | ||
| 112 | @code | ||
| 113 | pct-encoded = "%" HEXDIG HEXDIG | ||
| 114 | @endcode | ||
| 115 | |||
| 116 | @param cs The character set indicating | ||
| 117 | which characters are allowed without escapes. | ||
| 118 | Any character which is not in this set must be | ||
| 119 | escaped, or else parsing returns an error. | ||
| 120 | |||
| 121 | @par Specification | ||
| 122 | @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1"> | ||
| 123 | 2.1. Percent-Encoding (rfc3986)</a> | ||
| 124 | |||
| 125 | @see | ||
| 126 | @ref grammar::parse, | ||
| 127 | @ref pchars, | ||
| 128 | @ref pct_string_view. | ||
| 129 | */ | ||
| 130 | template<class CharSet> | ||
| 131 | constexpr | ||
| 132 | auto | ||
| 133 | 3205 | pct_encoded_rule( | |
| 134 | CharSet const& cs) noexcept -> | ||
| 135 | implementation_defined::pct_encoded_rule_t<CharSet> | ||
| 136 | { | ||
| 137 | // If an error occurs here it means that | ||
| 138 | // the value of your type does not meet | ||
| 139 | // the requirements. Please check the | ||
| 140 | // documentation! | ||
| 141 | static_assert( | ||
| 142 | grammar::is_charset<CharSet>::value, | ||
| 143 | "CharSet requirements not met"); | ||
| 144 | |||
| 145 | 3205 | return implementation_defined::pct_encoded_rule_t<CharSet>(cs); | |
| 146 | } | ||
| 147 | |||
| 148 | #endif | ||
| 149 | |||
| 150 | } // urls | ||
| 151 | } // boost | ||
| 152 | |||
| 153 | #include <boost/url/rfc/impl/pct_encoded_rule.hpp> | ||
| 154 | |||
| 155 | #endif | ||
| 156 |