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

10  

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

13  

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

18  

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

26  

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

32  

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

39  

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

44  

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

46  

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

51  

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

56  

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

61  

60  
    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 ) );
61  
    @endcode
63  
    @endcode
62  

64  

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

69  

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

74  

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

76  

75  
    @par Specification
77  
    @par Specification
76  
    @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">
77  
        2.1. Percent-Encoding (rfc3986)</a>
79  
        2.1. Percent-Encoding (rfc3986)</a>
78  

80  

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

99  

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

102  

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

105  

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

107  

106  
#endif
108  
#endif