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  
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/boostorg/url
8  
// Official repository: https://github.com/boostorg/url
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
11  
#ifndef BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
12  
#define BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
12  
#define BOOST_URL_RFC_PCT_ENCODED_RULE_HPP
13  

13  

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

18  

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

26  

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

32  

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

39  

40  
private:
40  
private:
41  
    CharSet cs_;
41  
    CharSet cs_;
42  
};
42  
};
43  
} // implementation_defined
43  
} // implementation_defined
44  

44  

45  
/** Rule for a string with percent-encoded escapes
45  
/** Rule for a string with percent-encoded escapes
46  

46  

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

51  

52  
    @par Value Type
52  
    @par Value Type
53  
    @code
53  
    @code
54  
    using value_type = pct_string_view;
54  
    using value_type = pct_string_view;
55  
    @endcode
55  
    @endcode
56  

56  

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

61  

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

64  

65  
    @par BNF
65  
    @par BNF
66  
    @code
66  
    @code
67  
    pct-encoded   = "%" HEXDIG HEXDIG
67  
    pct-encoded   = "%" HEXDIG HEXDIG
68  
    @endcode
68  
    @endcode
69  

69  

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

74  

75  
    @return A rule object.
75  
    @return A rule object.
76  

76  

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

80  

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

99  

100  
    return implementation_defined::pct_encoded_rule_t<CS>(cs);
100  
    return implementation_defined::pct_encoded_rule_t<CS>(cs);
101  
}
101  
}
102  

102  

103  
} // urls
103  
} // urls
104  
} // boost
104  
} // boost
105  

105  

106  
#include <boost/url/rfc/impl/pct_encoded_rule.hpp>
106  
#include <boost/url/rfc/impl/pct_encoded_rule.hpp>
107  

107  

108  
#endif
108  
#endif