-
Notifications
You must be signed in to change notification settings - Fork 50
/
delim_rule.hpp
254 lines (214 loc) · 5.73 KB
/
delim_rule.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/url
//
#ifndef BOOST_URL_GRAMMAR_DELIM_RULE_HPP
#define BOOST_URL_GRAMMAR_DELIM_RULE_HPP
#include <boost/url/detail/config.hpp>
#include <boost/core/detail/string_view.hpp>
#include <boost/url/grammar/charset.hpp>
#include <boost/url/grammar/error.hpp>
#include <boost/url/grammar/type_traits.hpp>
#include <type_traits>
namespace boost {
namespace urls {
namespace grammar {
/** Match a character literal
This matches the specified character.
The value is a reference to the character
in the underlying buffer, expressed as a
`core::string_view`. The function @ref squelch
may be used to turn this into `void` instead.
If there is no more input, the error code
@ref error::need_more is returned.
@par Value Type
@code
using value_type = core::string_view;
@endcode
@par Example
Rules are used with the function @ref parse.
@code
system::result< core::string_view > rv = parse( ".", delim_rule('.') );
@endcode
@par BNF
@code
char = %00-FF
@endcode
@param ch The character to match
@see
@ref parse,
@ref squelch.
*/
#ifdef BOOST_URL_DOCS
constexpr
__implementation_defined__
delim_rule( char ch ) noexcept;
#else
namespace implementation_defined {
struct ch_delim_rule
{
using value_type = core::string_view;
constexpr
ch_delim_rule(char ch) noexcept
: ch_(ch)
{
}
BOOST_URL_DECL
system::result<value_type>
parse(
char const*& it,
char const* end) const noexcept;
private:
char ch_;
};
} // implementation_defined
/** Match a character literal
This matches the specified character.
The value is a reference to the character
in the underlying buffer, expressed as a
`core::string_view`. The function @ref squelch
may be used to turn this into `void` instead.
If there is no more input, the error code
@ref error::need_more is returned.
@par Value Type
@code
using value_type = core::string_view;
@endcode
@par Example
Rules are used with the function @ref parse.
@code
system::result< core::string_view > rv = parse( ".", delim_rule('.') );
@endcode
@par BNF
@code
char = %00-FF
@endcode
@param ch The character to match
@see
@ref parse,
@ref squelch.
*/
constexpr
implementation_defined::ch_delim_rule
delim_rule( char ch ) noexcept
{
return {ch};
}
#endif
//------------------------------------------------
/** Match a single character from a character set
This matches exactly one character which
belongs to the specified character set.
The value is a reference to the character
in the underlying buffer, expressed as a
`core::string_view`. The function @ref squelch
may be used to turn this into `void` instead.
If there is no more input, the error code
@ref error::need_more is returned.
@par Value Type
@code
using value_type = core::string_view;
@endcode
@par Example
Rules are used with the function @ref parse.
@code
system::result< core::string_view > rv = parse( "X", delim_rule( alpha_chars ) );
@endcode
@param cs The character set to use.
@see
@ref alpha_chars,
@ref parse,
@ref squelch.
*/
#ifdef BOOST_URL_DOCS
template<class CharSet>
constexpr
__implementation_defined__
delim_rule( CharSet const& cs ) noexcept;
#else
namespace implementation_defined {
template<class CharSet>
struct cs_delim_rule
{
using value_type = core::string_view;
constexpr
cs_delim_rule(
CharSet const& cs) noexcept
: cs_(cs)
{
}
system::result<value_type>
parse(
char const*& it,
char const* end) const noexcept
{
if(it == end)
{
// end
BOOST_URL_RETURN_EC(
error::need_more);
}
if(! cs_(*it))
{
// wrong character
BOOST_URL_RETURN_EC(
error::mismatch);
}
return core::string_view{
it++, 1 };
}
private:
CharSet cs_;
};
} // implementation_defined
/** Match a single character from a character set
This matches exactly one character which
belongs to the specified character set.
The value is a reference to the character
in the underlying buffer, expressed as a
`core::string_view`. The function @ref squelch
may be used to turn this into `void` instead.
If there is no more input, the error code
@ref error::need_more is returned.
@par Value Type
@code
using value_type = core::string_view;
@endcode
@par Example
Rules are used with the function @ref parse.
@code
system::result< core::string_view > rv = parse( "X", delim_rule( alpha_chars ) );
@endcode
@param cs The character set to use.
@see
@ref alpha_chars,
@ref parse,
@ref squelch.
*/
template<class CharSet>
constexpr
typename std::enable_if<
! std::is_convertible<
CharSet, char>::value,
implementation_defined::cs_delim_rule<CharSet>>::type
delim_rule(
CharSet const& cs) noexcept
{
// If you get a compile error here it
// means that your type does not meet
// the requirements for a CharSet.
// Please consult the documentation.
static_assert(
is_charset<CharSet>::value,
"CharSet requirements not met");
return implementation_defined::cs_delim_rule<CharSet>(cs);
}
#endif
} // grammar
} // urls
} // boost
#endif