-
Notifications
You must be signed in to change notification settings - Fork 50
/
charset.hpp
237 lines (197 loc) · 5.56 KB
/
charset.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
//
// Copyright (c) 2021 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_CHARSET_HPP
#define BOOST_URL_GRAMMAR_CHARSET_HPP
#include <boost/url/detail/config.hpp>
#include <boost/url/grammar/detail/charset.hpp>
#include <boost/static_assert.hpp>
#include <cstdint>
#include <type_traits>
#include <utility>
namespace boost {
namespace urls {
namespace grammar {
namespace see_below
{
template<class T, class = void>
struct is_charset : std::false_type {};
template<class T>
struct is_charset<T, void_t<
decltype(
std::declval<bool&>() =
std::declval<T const&>().operator()(
std::declval<char>())
) > > : std::true_type
{
};
}
/** Alias for `std::true_type` if T satisfies <em>CharSet</em>.
This metafunction determines if the
type `T` meets these requirements of
<em>CharSet</em>:
@li An instance of `T` is invocable
with this equivalent function signature:
@code
bool T::operator()( char ) const noexcept;
@endcode
@par Example
Use with `enable_if` on the return value:
@code
template< class CharSet >
typename std::enable_if< is_charset<T>::value >::type
func( CharSet const& cs );
@endcode
@tparam T the type to check.
*/
template<class T>
using is_charset = BOOST_URL_SEE_BELOW(see_below::is_charset<T>);
//------------------------------------------------
/** Find the first character in the string that is in the set.
@par Exception Safety
Throws nothing.
@return A pointer to the found character,
otherwise the value `last`.
@param first A pointer to the first character
in the string to search.
@param last A pointer to one past the last
character in the string to search.
@param cs The character set to use.
@see
@ref find_if_not.
*/
template<class CharSet>
char const*
find_if(
char const* const first,
char const* const last,
CharSet const& cs) noexcept
{
// If you get a compile error here
// it means your type does not meet
// the requirements. Please check the
// documentation.
static_assert(
is_charset<CharSet>::value,
"CharSet requirements not met");
return detail::find_if(first, last, cs,
detail::has_find_if<CharSet>{});
}
/** Find the first character in the string that is not in CharSet
@par Exception Safety
Throws nothing.
@return A pointer to the found character,
otherwise the value `last`.
@param first A pointer to the first character
in the string to search.
@param last A pointer to one past the last
character in the string to search.
@param cs The character set to use.
@see
@ref find_if_not.
*/
template<class CharSet>
char const*
find_if_not(
char const* const first,
char const* const last,
CharSet const& cs) noexcept
{
// If you get a compile error here
// it means your type does not meet
// the requirements. Please check the
// documentation.
static_assert(
is_charset<CharSet>::value,
"CharSet requirements not met");
return detail::find_if_not(first, last, cs,
detail::has_find_if_not<CharSet>{});
}
//------------------------------------------------
#ifndef BOOST_URL_DOCS
namespace implementation_defined {
template<class CharSet>
struct charset_ref
{
CharSet const& cs_;
constexpr
bool
operator()(char ch) const noexcept
{
return cs_(ch);
}
char const*
find_if(
char const* first,
char const* last) const noexcept
{
return grammar::find_if(
first, last, cs_);
}
char const*
find_if_not(
char const* first,
char const* last) const noexcept
{
return grammar::find_if_not(
first, last, cs_ );
}
};
} // implementation_defined
#endif
/** Return a reference to a character set
This function returns a character set which
references the specified object. This is
used to reduce the number of bytes of
storage (`sizeof`) required by a combinator
when it stores a copy of the object.
<br>
Ownership of the object is not transferred;
the caller is responsible for ensuring the
lifetime of the object is extended until it
is no longer referenced. For best results,
`ref` should only be used with compile-time
constants.
*/
#ifdef BOOST_URL_DOCS
template<class CharSet>
constexpr
__implementation_defined__
ref(CharSet const& cs) noexcept;
#else
/** Return a reference to a character set
This function returns a character set which
references the specified object. This is
used to reduce the number of bytes of
storage (`sizeof`) required by a combinator
when it stores a copy of the object.
<br>
Ownership of the object is not transferred;
the caller is responsible for ensuring the
lifetime of the object is extended until it
is no longer referenced. For best results,
`ref` should only be used with compile-time
constants.
*/
template<class CharSet>
constexpr
typename std::enable_if<
is_charset<CharSet>::value &&
! std::is_same<CharSet,
implementation_defined::charset_ref<CharSet> >::value,
implementation_defined::charset_ref<CharSet> >::type
ref(CharSet const& cs) noexcept
{
return implementation_defined::charset_ref<
CharSet>{cs};
}
#endif
} // grammar
} // urls
} // boost
#endif