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 : /** Match a character literal
25 :
26 : This matches the specified character.
27 : The value is a reference to the character
28 : in the underlying buffer, expressed as a
29 : `core::string_view`. The function @ref squelch
30 : may be used to turn this into `void` instead.
31 : If there is no more input, the error code
32 : @ref error::need_more is returned.
33 :
34 : @par Value Type
35 : @code
36 : using value_type = core::string_view;
37 : @endcode
38 :
39 : @par Example
40 : Rules are used with the function @ref parse.
41 : @code
42 : system::result< core::string_view > rv = parse( ".", delim_rule('.') );
43 : @endcode
44 :
45 : @par BNF
46 : @code
47 : char = %00-FF
48 : @endcode
49 :
50 : @param ch The character to match
51 :
52 : @see
53 : @ref parse,
54 : @ref squelch.
55 : */
56 : #ifdef BOOST_URL_DOCS
57 : constexpr
58 : __implementation_defined__
59 : delim_rule( char ch ) noexcept;
60 : #else
61 :
62 : namespace implementation_defined {
63 : struct ch_delim_rule
64 : {
65 : using value_type = core::string_view;
66 :
67 : constexpr
68 11591 : ch_delim_rule(char ch) noexcept
69 11591 : : ch_(ch)
70 : {
71 11591 : }
72 :
73 : BOOST_URL_DECL
74 : system::result<value_type>
75 : parse(
76 : char const*& it,
77 : char const* end) const noexcept;
78 :
79 : private:
80 : char ch_;
81 : };
82 : } // implementation_defined
83 :
84 : /** Match a character literal
85 :
86 : This matches the specified character.
87 : The value is a reference to the character
88 : in the underlying buffer, expressed as a
89 : `core::string_view`. The function @ref squelch
90 : may be used to turn this into `void` instead.
91 : If there is no more input, the error code
92 : @ref error::need_more is returned.
93 :
94 : @par Value Type
95 : @code
96 : using value_type = core::string_view;
97 : @endcode
98 :
99 : @par Example
100 : Rules are used with the function @ref parse.
101 : @code
102 : system::result< core::string_view > rv = parse( ".", delim_rule('.') );
103 : @endcode
104 :
105 : @par BNF
106 : @code
107 : char = %00-FF
108 : @endcode
109 :
110 : @param ch The character to match
111 :
112 : @see
113 : @ref parse,
114 : @ref squelch.
115 : */
116 : constexpr
117 : implementation_defined::ch_delim_rule
118 11591 : delim_rule( char ch ) noexcept
119 : {
120 11591 : return {ch};
121 : }
122 : #endif
123 :
124 : //------------------------------------------------
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 : #ifdef BOOST_URL_DOCS
156 : template<class CharSet>
157 : constexpr
158 : __implementation_defined__
159 : delim_rule( CharSet const& cs ) noexcept;
160 : #else
161 : namespace implementation_defined {
162 : template<class CharSet>
163 : struct cs_delim_rule
164 : {
165 : using value_type = core::string_view;
166 :
167 : constexpr
168 3 : cs_delim_rule(
169 : CharSet const& cs) noexcept
170 : : cs_(cs)
171 : {
172 3 : }
173 :
174 : system::result<value_type>
175 877 : parse(
176 : char const*& it,
177 : char const* end) const noexcept
178 : {
179 877 : if(it == end)
180 : {
181 : // end
182 1 : BOOST_URL_RETURN_EC(
183 : error::need_more);
184 : }
185 876 : if(! cs_(*it))
186 : {
187 : // wrong character
188 350 : BOOST_URL_RETURN_EC(
189 : error::mismatch);
190 : }
191 1052 : return core::string_view{
192 526 : it++, 1 };
193 : }
194 :
195 : private:
196 : CharSet cs_;
197 : };
198 : } // implementation_defined
199 :
200 : /** Match a single character from a character set
201 :
202 : This matches exactly one character which
203 : belongs to the specified character set.
204 : The value is a reference to the character
205 : in the underlying buffer, expressed as a
206 : `core::string_view`. The function @ref squelch
207 : may be used to turn this into `void` instead.
208 : If there is no more input, the error code
209 : @ref error::need_more is returned.
210 :
211 : @par Value Type
212 : @code
213 : using value_type = core::string_view;
214 : @endcode
215 :
216 : @par Example
217 : Rules are used with the function @ref parse.
218 : @code
219 : system::result< core::string_view > rv = parse( "X", delim_rule( alpha_chars ) );
220 : @endcode
221 :
222 : @param cs The character set to use.
223 :
224 : @see
225 : @ref alpha_chars,
226 : @ref parse,
227 : @ref squelch.
228 : */
229 : template<class CharSet>
230 : constexpr
231 : typename std::enable_if<
232 : ! std::is_convertible<
233 : CharSet, char>::value,
234 : implementation_defined::cs_delim_rule<CharSet>>::type
235 3 : delim_rule(
236 : CharSet const& cs) noexcept
237 : {
238 : // If you get a compile error here it
239 : // means that your type does not meet
240 : // the requirements for a CharSet.
241 : // Please consult the documentation.
242 : static_assert(
243 : is_charset<CharSet>::value,
244 : "CharSet requirements not met");
245 :
246 3 : return implementation_defined::cs_delim_rule<CharSet>(cs);
247 : }
248 : #endif
249 :
250 : } // grammar
251 : } // urls
252 : } // boost
253 :
254 : #endif
|