Line data Source code
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_GRAMMAR_DELIM_RULE_HPP
11 : #define BOOST_URL_GRAMMAR_DELIM_RULE_HPP
12 :
13 : #include <boost/url/detail/config.hpp>
14 : #include <boost/core/detail/string_view.hpp>
15 : #include <boost/url/grammar/charset.hpp>
16 : #include <boost/url/grammar/error.hpp>
17 : #include <boost/url/grammar/type_traits.hpp>
18 : #include <type_traits>
19 :
20 : namespace boost {
21 : namespace urls {
22 : namespace grammar {
23 :
24 : namespace implementation_defined {
25 : struct ch_delim_rule
26 : {
27 : using value_type = core::string_view;
28 :
29 : constexpr
30 11603 : ch_delim_rule(char ch) noexcept
31 11603 : : ch_(ch)
32 : {
33 11603 : }
34 :
35 : BOOST_URL_DECL
36 : system::result<value_type>
37 : parse(
38 : char const*& it,
39 : char const* end) const noexcept;
40 :
41 : private:
42 : char ch_;
43 : };
44 : } // implementation_defined
45 :
46 : /** Match a character literal
47 :
48 : This matches the specified character.
49 : The value is a reference to the character
50 : in the underlying buffer, expressed as a
51 : `core::string_view`. The function @ref squelch
52 : may be used to turn this into `void` instead.
53 : If there is no more input, the error code
54 : @ref error::need_more is returned.
55 :
56 : @par Value Type
57 : @code
58 : using value_type = core::string_view;
59 : @endcode
60 :
61 : @par Example
62 : Rules are used with the function @ref parse.
63 : @code
64 : system::result< core::string_view > rv = parse( ".", delim_rule('.') );
65 : @endcode
66 :
67 : @par BNF
68 : @code
69 : char = %00-FF
70 : @endcode
71 :
72 : @param ch The character to match
73 :
74 : @see
75 : @ref parse,
76 : @ref squelch.
77 : */
78 : constexpr
79 : implementation_defined::ch_delim_rule
80 11603 : delim_rule( char ch ) noexcept
81 : {
82 11603 : return {ch};
83 : }
84 :
85 : //------------------------------------------------
86 :
87 : namespace implementation_defined {
88 : template<class CharSet>
89 : struct cs_delim_rule
90 : {
91 : using value_type = core::string_view;
92 :
93 : constexpr
94 3 : cs_delim_rule(
95 : CharSet const& cs) noexcept
96 : : cs_(cs)
97 : {
98 3 : }
99 :
100 : system::result<value_type>
101 877 : parse(
102 : char const*& it,
103 : char const* end) const noexcept
104 : {
105 877 : if(it == end)
106 : {
107 : // end
108 1 : BOOST_URL_RETURN_EC(
109 : error::need_more);
110 : }
111 876 : if(! cs_(*it))
112 : {
113 : // wrong character
114 350 : BOOST_URL_RETURN_EC(
115 : error::mismatch);
116 : }
117 1052 : return core::string_view{
118 526 : it++, 1 };
119 : }
120 :
121 : private:
122 : CharSet cs_;
123 : };
124 : } // implementation_defined
125 :
126 : /** Match a single character from a character set
127 :
128 : This matches exactly one character which
129 : belongs to the specified character set.
130 : The value is a reference to the character
131 : in the underlying buffer, expressed as a
132 : `core::string_view`. The function @ref squelch
133 : may be used to turn this into `void` instead.
134 : If there is no more input, the error code
135 : @ref error::need_more is returned.
136 :
137 : @par Value Type
138 : @code
139 : using value_type = core::string_view;
140 : @endcode
141 :
142 : @par Example
143 : Rules are used with the function @ref parse.
144 : @code
145 : system::result< core::string_view > rv = parse( "X", delim_rule( alpha_chars ) );
146 : @endcode
147 :
148 : @param cs The character set to use.
149 :
150 : @see
151 : @ref alpha_chars,
152 : @ref parse,
153 : @ref squelch.
154 : */
155 : template<BOOST_URL_CONSTRAINT(CharSet) CS>
156 : constexpr
157 : typename std::enable_if<
158 : ! std::is_convertible<
159 : CS, char>::value,
160 : implementation_defined::cs_delim_rule<CS>>::type
161 3 : delim_rule(
162 : CS const& cs) noexcept
163 : {
164 : // If you get a compile error here it
165 : // means that your type does not meet
166 : // the requirements for a CharSet.
167 : // Please consult the documentation.
168 : static_assert(
169 : is_charset<CS>::value,
170 : "CharSet requirements not met");
171 :
172 3 : return implementation_defined::cs_delim_rule<CS>(cs);
173 : }
174 :
175 : } // grammar
176 : } // urls
177 : } // boost
178 :
179 : #endif
|