1  
//
1  
//
2  
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2  
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/url
7  
// Official repository: https://github.com/boostorg/url
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
10  
#ifndef BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
11  
#define BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
11  
#define BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
12  

12  

13  
#include <boost/url/detail/config.hpp>
13  
#include <boost/url/detail/config.hpp>
14  
#include <boost/url/error_types.hpp>
14  
#include <boost/url/error_types.hpp>
15  
#include <boost/url/pct_string_view.hpp>
15  
#include <boost/url/pct_string_view.hpp>
16  
#include <boost/url/grammar/charset.hpp>
16  
#include <boost/url/grammar/charset.hpp>
17  

17  

18  
namespace boost {
18  
namespace boost {
19  
namespace urls {
19  
namespace urls {
20  
namespace implementation_defined {
20  
namespace implementation_defined {
21  
template<class CharSet>
21  
template<class CharSet>
22  
struct pct_encoded_rule_t
22  
struct pct_encoded_rule_t
23  
{
23  
{
24  
    using value_type = pct_string_view;
24  
    using value_type = pct_string_view;
25  

25  

26  
    system::result<value_type>
26  
    system::result<value_type>
27  
    parse(
27  
    parse(
28  
        char const*& it,
28  
        char const*& it,
29  
        char const* end) const noexcept;
29  
        char const* end) const noexcept;
30  

30  

31  
    constexpr
31  
    constexpr
32  
    pct_encoded_rule_t(
32  
    pct_encoded_rule_t(
33  
        CharSet const& cs) noexcept
33  
        CharSet const& cs) noexcept
34  
        : cs_(cs)
34  
        : cs_(cs)
35  
    {
35  
    {
36  
    }
36  
    }
37  

37  

38  
private:
38  
private:
39  
    CharSet cs_;
39  
    CharSet cs_;
40  
};
40  
};
41  
} // implementation_defined
41  
} // implementation_defined
42  

42  

43  
/** Rule for a string with percent-encoded escapes
43  
/** Rule for a string with percent-encoded escapes
44  

44  

45  
    This function returns a rule which matches
45  
    This function returns a rule which matches
46  
    a percent-encoded string, permitting characters
46  
    a percent-encoded string, permitting characters
47  
    in the string which are also in the specified
47  
    in the string which are also in the specified
48  
    character set to be used unescaped.
48  
    character set to be used unescaped.
49  

49  

50  
    @par Value Type
50  
    @par Value Type
51  
    @code
51  
    @code
52  
    using value_type = pct_string_view;
52  
    using value_type = pct_string_view;
53  
    @endcode
53  
    @endcode
54  

54  

55  
    @par Example
55  
    @par Example
56  
    Rules are used with the function @ref grammar::parse.
56  
    Rules are used with the function @ref grammar::parse.
57  
    @code
57  
    @code
58  
    //  pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
58  
    //  pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
59  

59  

60  
    system::result< pct_string_view > rv = grammar::parse( "Program%20Files", pct_encoded_rule( pchars ) );
60  
    system::result< pct_string_view > rv = grammar::parse( "Program%20Files", pct_encoded_rule( pchars ) );
61  
    @endcode
61  
    @endcode
62  

62  

63  
    @par BNF
63  
    @par BNF
64  
    @code
64  
    @code
65  
    pct-encoded   = "%" HEXDIG HEXDIG
65  
    pct-encoded   = "%" HEXDIG HEXDIG
66  
    @endcode
66  
    @endcode
67  

67  

68  
    @param cs The character set indicating
68  
    @param cs The character set indicating
69  
    which characters are allowed without escapes.
69  
    which characters are allowed without escapes.
70  
    Any character which is not in this set must be
70  
    Any character which is not in this set must be
71  
    escaped, or else parsing returns an error.
71  
    escaped, or else parsing returns an error.
72  

72  

73  
    @return A rule object.
73  
    @return A rule object.
74  

74  

75  
    @par Specification
75  
    @par Specification
76  
    @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1">
76  
    @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-2.1">
77  
        2.1. Percent-Encoding (rfc3986)</a>
77  
        2.1. Percent-Encoding (rfc3986)</a>
78  

78  

79  
    @see
79  
    @see
80  
        @ref grammar::parse,
80  
        @ref grammar::parse,
81  
        @ref pchars,
81  
        @ref pchars,
82  
        @ref pct_string_view.
82  
        @ref pct_string_view.
83  
*/
83  
*/
84  
template<BOOST_URL_CONSTRAINT(grammar::CharSet) CS>
84  
template<BOOST_URL_CONSTRAINT(grammar::CharSet) CS>
85  
constexpr
85  
constexpr
86  
auto
86  
auto
87  
pct_encoded_rule(CS const& cs) noexcept ->
87  
pct_encoded_rule(CS const& cs) noexcept ->
88  
    implementation_defined::pct_encoded_rule_t<CS>
88  
    implementation_defined::pct_encoded_rule_t<CS>
89  
{
89  
{
90  
    // If an error occurs here it means that
90  
    // If an error occurs here it means that
91  
    // the value of your type does not meet
91  
    // the value of your type does not meet
92  
    // the requirements. Please check the
92  
    // the requirements. Please check the
93  
    // documentation!
93  
    // documentation!
94  
    static_assert(
94  
    static_assert(
95  
        grammar::is_charset<CS>::value,
95  
        grammar::is_charset<CS>::value,
96  
        "CharSet requirements not met");
96  
        "CharSet requirements not met");
97  

97  

98  
    return implementation_defined::pct_encoded_rule_t<CS>(cs);
98  
    return implementation_defined::pct_encoded_rule_t<CS>(cs);
99  
}
99  
}
100  

100  

101  
} // urls
101  
} // urls
102  
} // boost
102  
} // boost
103  

103  

104  
#include <boost/url/rfc/impl/pct_encoded_rule.hpp>
104  
#include <boost/url/rfc/impl/pct_encoded_rule.hpp>
105  

105  

106  
#endif
106  
#endif