-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathparams_base.hpp
More file actions
423 lines (372 loc) · 7.24 KB
/
Copy pathparams_base.hpp
File metadata and controls
423 lines (372 loc) · 7.24 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.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_IMPL_PARAMS_BASE_HPP
#define BOOST_URL_IMPL_PARAMS_BASE_HPP
#include <boost/url/detail/params_iter_impl.hpp>
#include <boost/url/decode_view.hpp>
#include <boost/url/grammar/ci_string.hpp>
#include <iterator>
#include <ostream>
#include <string>
namespace boost {
namespace urls {
//------------------------------------------------
class BOOST_SYMBOL_VISIBLE params_base::iterator
{
detail::params_iter_impl it_;
bool space_as_plus_ = true;
friend class params_base;
friend class params_ref;
iterator(
detail::query_ref const& ref,
encoding_opts opt) noexcept;
iterator(
detail::query_ref const& impl,
encoding_opts opt,
int) noexcept;
iterator(
detail::params_iter_impl const& it,
encoding_opts opt) noexcept
: it_(it)
, space_as_plus_(opt.space_as_plus)
{
}
public:
using value_type = params_base::value_type;
using reference = params_base::reference;
using pointer = reference;
using difference_type =
params_base::difference_type;
using iterator_category =
std::bidirectional_iterator_tag;
iterator() = default;
iterator(iterator const&) = default;
iterator& operator=(
iterator const&) noexcept = default;
iterator&
operator++() noexcept
{
it_.increment();
return *this;
}
iterator
operator++(int) noexcept
{
auto tmp = *this;
++*this;
return tmp;
}
iterator&
operator--() noexcept
{
it_.decrement();
return *this;
}
iterator
operator--(int) noexcept
{
auto tmp = *this;
--*this;
return tmp;
}
reference
operator*() const;
// the return value is too expensive
pointer operator->() const = delete;
bool
operator==(
iterator const& other) const noexcept
{
return it_.equal(other.it_);
}
bool
operator!=(
iterator const& other) const noexcept
{
return ! it_.equal(other.it_);
}
};
//------------------------------------------------
inline
params_base::
iterator::
iterator(
detail::query_ref const& ref,
encoding_opts opt) noexcept
: it_(ref)
, space_as_plus_(opt.space_as_plus)
{
}
inline
params_base::
iterator::
iterator(
detail::query_ref const& ref,
encoding_opts opt,
int) noexcept
: it_(ref, 0)
, space_as_plus_(opt.space_as_plus)
{
}
inline
auto
params_base::
iterator::
operator*() const ->
reference
{
encoding_opts opt;
opt.space_as_plus =
space_as_plus_;
param_pct_view p =
it_.dereference();
return reference(
p.key.decode(opt),
p.value.decode(opt),
p.has_value);
}
//------------------------------------------------
//
// params_base
//
//------------------------------------------------
inline
params_base::
params_base() noexcept
// space_as_plus = true
: opt_(true, false, false)
{
}
inline
bool
params_base::
contains(
core::string_view key,
ignore_case_param ic) const noexcept
{
return find(
begin(),key, ic) != end();
}
inline
auto
params_base::
find(
core::string_view key,
ignore_case_param ic) const noexcept ->
iterator
{
return iterator(
find_impl(
begin().it_, key, ic),
opt_);
}
inline
auto
params_base::
find(
iterator it,
core::string_view key,
ignore_case_param ic) const noexcept ->
iterator
{
return iterator(
find_impl(
it.it_, key, ic),
opt_);
}
inline
auto
params_base::
find_last(
core::string_view key,
ignore_case_param ic) const noexcept ->
iterator
{
return iterator(
find_last_impl(
end().it_, key, ic),
opt_);
}
inline
auto
params_base::
find_last(
iterator it,
core::string_view key,
ignore_case_param ic) const noexcept ->
iterator
{
return iterator(
find_last_impl(
it.it_, key, ic),
opt_);
}
inline
params_base::
params_base(
detail::query_ref const& ref,
encoding_opts opt) noexcept
: ref_(ref)
, opt_(opt)
{
}
inline
pct_string_view
params_base::
buffer() const noexcept
{
return ref_.buffer();
}
inline
bool
params_base::
empty() const noexcept
{
return ref_.nparam() == 0;
}
inline
std::size_t
params_base::
size() const noexcept
{
return ref_.nparam();
}
inline
auto
params_base::
begin() const noexcept ->
iterator
{
return iterator(ref_, opt_);
}
inline
auto
params_base::
end() const noexcept ->
iterator
{
return {ref_, opt_, 0};
}
//------------------------------------------------
inline
std::size_t
params_base::
count(
core::string_view key,
ignore_case_param ic) const noexcept
{
std::size_t n = 0;
auto it = find(key, ic);
auto const end_ = end();
while(it != end_)
{
++n;
++it;
it = find(it, key, ic);
}
return n;
}
inline
std::string
params_base::
get_or(
core::string_view key,
core::string_view value,
ignore_case_param ic) const
{
auto it = find_impl(
begin().it_, key, ic);
detail::params_iter_impl end_(ref_, 0);
if(it.equal(end_))
return std::string(value);
param_pct_view const p = it.dereference();
if(! p.has_value)
return std::string();
auto opt = opt_;
return p.value.decode(opt);
}
//------------------------------------------------
//
// (implementation)
//
//------------------------------------------------
inline
detail::params_iter_impl
params_base::
find_impl(
detail::params_iter_impl it,
core::string_view key,
ignore_case_param ic) const noexcept
{
detail::params_iter_impl end_(ref_, 0);
if(! ic)
{
for(;;)
{
if(it.equal(end_))
return it;
if(*it.key() == key)
return it;
it.increment();
}
}
for(;;)
{
if(it.equal(end_))
return it;
if( grammar::ci_is_equal(
*it.key(), key))
return it;
it.increment();
}
}
inline
detail::params_iter_impl
params_base::
find_last_impl(
detail::params_iter_impl it,
core::string_view key,
ignore_case_param ic) const noexcept
{
detail::params_iter_impl begin_(ref_);
if(! ic)
{
for(;;)
{
if(it.equal(begin_))
return { ref_, 0 };
it.decrement();
if(*it.key() == key)
return it;
}
}
for(;;)
{
if(it.equal(begin_))
return { ref_, 0 };
it.decrement();
if(grammar::ci_is_equal(
*it.key(), key))
return it;
}
}
//------------------------------------------------
inline
std::ostream&
operator<<(
std::ostream& os,
params_base const& qp)
{
os << qp.buffer();
return os;
}
} // urls
} // boost
#endif