include/boost/url/detail/impl/any_params_iter.hpp

99.4% Lines (167/168) 100.0% List of functions (24/24)
any_params_iter.hpp
f(x) Functions (24)
Function Calls Lines Blocks
boost::urls::detail::any_params_iter::~any_params_iter() :40 1695x 100.0% 100.0% boost::urls::detail::query_string_iter::query_string_iter(boost::core::basic_string_view<char>, bool) :50 429x 100.0% 100.0% boost::urls::detail::query_string_iter::rewind() :62 598x 100.0% 100.0% boost::urls::detail::query_string_iter::measure(unsigned long&) :89 755x 100.0% 100.0% boost::urls::detail::query_string_iter::copy(char*&, char const*) :110 326x 100.0% 86.0% boost::urls::detail::query_string_iter::increment() :132 652x 90.9% 92.0% boost::urls::detail::single_param_iter::single_param_iter(boost::urls::param_view const&, bool) :157 1176x 100.0% 100.0% boost::urls::detail::single_param_iter::rewind() :172 1176x 100.0% 100.0% boost::urls::detail::single_param_iter::measure(unsigned long&) :180 2352x 100.0% 100.0% boost::urls::detail::single_param_iter::copy(char*&, char const*) :205 1176x 100.0% 88.0% boost::urls::detail::params_iter_base::measure_impl(unsigned long&, boost::urls::param_view const&) :239 70x 100.0% 100.0% boost::urls::detail::params_iter_base::copy_impl(char*&, char const*, boost::urls::param_view const&) :262 70x 100.0% 100.0% boost::urls::detail::param_encoded_iter::param_encoded_iter(boost::urls::param_pct_view const&) :295 12x 100.0% 100.0% boost::urls::detail::param_encoded_iter::rewind() :308 12x 100.0% 100.0% boost::urls::detail::param_encoded_iter::measure(unsigned long&) :316 24x 100.0% 100.0% boost::urls::detail::param_encoded_iter::copy(char*&, char const*) :334 12x 100.0% 100.0% boost::urls::detail::params_encoded_iter_base::measure_impl(unsigned long&, boost::urls::param_view const&) :364 51x 100.0% 100.0% boost::urls::detail::params_encoded_iter_base::copy_impl(char*&, char const*, boost::urls::param_view const&) :380 51x 100.0% 100.0% boost::urls::detail::param_value_iter::rewind() :410 9x 100.0% 100.0% boost::urls::detail::param_value_iter::measure(unsigned long&) :418 18x 100.0% 100.0% boost::urls::detail::param_value_iter::copy(char*&, char const*) :440 9x 100.0% 100.0% boost::urls::detail::param_encoded_value_iter::rewind() :465 8x 100.0% 100.0% boost::urls::detail::param_encoded_value_iter::measure(unsigned long&) :473 16x 100.0% 100.0% boost::urls::detail::param_encoded_value_iter::copy(char*&, char const*) :492 8x 100.0% 100.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2023 Alan de Freitas (alandefreitas@gmail.com)
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/boostorg/url
9 //
10
11 #ifndef BOOST_URL_DETAIL_IMPL_ANY_PARAMS_ITER_HPP
12 #define BOOST_URL_DETAIL_IMPL_ANY_PARAMS_ITER_HPP
13
14 #include <boost/url/encode.hpp>
15 #include <boost/url/rfc/detail/charsets.hpp>
16 #include <boost/assert.hpp>
17
18 namespace boost {
19 namespace urls {
20 namespace detail {
21
22 /*
23 When a string is transformed into a range of
24 params, the empty string becomes ambiguous:
25 it can be an empty range, or a range with
26 one param. The value `not_empty` is used on
27 construction to inform the transformation
28 that the empty string should be treated as
29 a one-element range. This simplifies
30 edit_params().
31 */
32
33 //------------------------------------------------
34 //
35 // any_params_iter
36 //
37 //------------------------------------------------
38
39 inline
40 1695x any_params_iter::
41 ~any_params_iter() noexcept = default;
42
43 //------------------------------------------------
44 //
45 // query_iter
46 //
47 //------------------------------------------------
48
49 inline
50 429x query_string_iter::
51 query_string_iter(
52 core::string_view s,
53 429x bool ne) noexcept
54 : any_params_iter(
55 429x s.empty() && ! ne, s)
56 {
57 429x rewind();
58 429x }
59
60 inline
61 void
62 598x query_string_iter::
63 rewind() noexcept
64 {
65 598x if(empty)
66 {
67 260x at_end_ = true;
68 260x return;
69 }
70 338x p_ = s0.begin();
71 338x if(! s0.empty())
72 {
73 auto pos =
74 330x s0.find_first_of('&');
75 330x if(pos != core::string_view::npos)
76 314x n_ = pos;
77 else
78 16x n_ = s0.size();
79 }
80 else
81 {
82 8x n_ = 0;
83 }
84 338x at_end_ = false;
85 }
86
87 inline
88 bool
89 755x query_string_iter::
90 measure(
91 std::size_t& n) noexcept
92 {
93 755x if(at_end_)
94 429x return false;
95 // When interacting with the query as
96 // an intact string, we do not treat
97 // the plus sign as an encoded space.
98 326x encoding_opts opt;
99 326x opt.space_as_plus = false;
100 326x n += encoded_size(
101 core::string_view(p_, n_),
102 query_chars,
103 opt);
104 326x increment();
105 326x return true;
106 }
107
108 inline
109 void
110 326x query_string_iter::
111 copy(
112 char*& dest,
113 char const* end) noexcept
114 {
115 326x BOOST_ASSERT(! at_end_);
116 // When interacting with the query as
117 // an intact string, we do not treat
118 // the plus sign as an encoded space.
119 326x encoding_opts opt;
120 326x opt.space_as_plus = false;
121 326x dest += encode_unsafe(
122 dest,
123 326x end - dest,
124 core::string_view(p_, n_),
125 query_chars,
126 opt);
127 326x increment();
128 326x }
129
130 inline
131 void
132 652x query_string_iter::
133 increment() noexcept
134 {
135 652x p_ += n_;
136 652x if(p_ == s0.end())
137 {
138 338x at_end_ = true;
139 338x return;
140 }
141 314x ++p_;
142 314x core::string_view s(p_, s0.end() - p_);
143 314x auto pos = s.find_first_of('&');
144 314x if(pos != core::string_view::npos)
145 n_ = pos;
146 else
147 314x n_ = s.size();
148 }
149
150 //------------------------------------------------
151 //
152 // param_iter
153 //
154 //------------------------------------------------
155
156 inline
157 1176x single_param_iter::
158 single_param_iter(
159 param_view const& p,
160 1176x bool space_as_plus) noexcept
161 : any_params_iter(
162 false,
163 p.key,
164 p.value)
165 1176x , has_value_(p.has_value)
166 1176x , space_as_plus_(space_as_plus)
167 {
168 1176x }
169
170 inline
171 void
172 1176x single_param_iter::
173 rewind() noexcept
174 {
175 1176x at_end_ = false;
176 1176x }
177
178 inline
179 bool
180 2352x single_param_iter::
181 measure(std::size_t& n) noexcept
182 {
183 2352x if(at_end_)
184 1176x return false;
185 1176x encoding_opts opt;
186 1176x opt.space_as_plus = space_as_plus_;
187 1176x n += encoded_size(
188 s0,
189 detail::param_key_chars,
190 opt);
191 1176x if(has_value_)
192 {
193 1176x ++n; // '='
194 1176x n += encoded_size(
195 s1,
196 detail::param_value_chars,
197 opt);
198 }
199 1176x at_end_ = true;
200 1176x return true;
201 }
202
203 inline
204 void
205 1176x single_param_iter::
206 copy(
207 char*& dest,
208 char const* end) noexcept
209 {
210 1176x BOOST_ASSERT(! at_end_);
211 1176x encoding_opts opt;
212 1176x opt.space_as_plus = space_as_plus_;
213 2352x dest += encode(
214 dest,
215 1176x end - dest,
216 s0,
217 detail::param_key_chars,
218 opt);
219 1176x if (has_value_)
220 {
221 1176x *dest++ = '=';
222 1176x dest += encode(
223 dest,
224 1176x end - dest,
225 s1,
226 detail::param_value_chars,
227 opt);
228 }
229 1176x }
230
231 //------------------------------------------------
232 //
233 // params_iter_base
234 //
235 //------------------------------------------------
236
237 inline
238 void
239 70x params_iter_base::
240 measure_impl(
241 std::size_t& n,
242 param_view const& p) noexcept
243 {
244 70x encoding_opts opt;
245 70x opt.space_as_plus = space_as_plus_;
246 70x n += encoded_size(
247 p.key,
248 detail::param_key_chars,
249 opt);
250 70x if(p.has_value)
251 {
252 58x ++n; // '='
253 58x n += encoded_size(
254 p.value,
255 detail::param_value_chars,
256 opt);
257 }
258 70x }
259
260 inline
261 void
262 70x params_iter_base::
263 copy_impl(
264 char*& dest,
265 char const* end,
266 param_view const& p) noexcept
267 {
268 70x encoding_opts opt;
269 70x opt.space_as_plus = space_as_plus_;
270 140x dest += encode(
271 dest,
272 70x end - dest,
273 p.key,
274 detail::param_key_chars,
275 opt);
276 70x if(p.has_value)
277 {
278 58x *dest++ = '=';
279 58x dest += encode(
280 dest,
281 58x end - dest,
282 p.value,
283 detail::param_value_chars,
284 opt);
285 }
286 70x }
287
288 //------------------------------------------------
289 //
290 // param_encoded_iter
291 //
292 //------------------------------------------------
293
294 inline
295 12x param_encoded_iter::
296 param_encoded_iter(
297 12x param_pct_view const& p) noexcept
298 : any_params_iter(
299 false,
300 p.key,
301 p.value)
302 12x , has_value_(p.has_value)
303 {
304 12x }
305
306 inline
307 void
308 12x param_encoded_iter::
309 rewind() noexcept
310 {
311 12x at_end_ = false;
312 12x }
313
314 inline
315 bool
316 24x param_encoded_iter::
317 measure(std::size_t& n) noexcept
318 {
319 24x if(at_end_)
320 12x return false;
321 12x n += detail::re_encoded_size_unsafe(
322 s0,
323 detail::param_key_chars);
324 12x if(has_value_)
325 12x n += detail::re_encoded_size_unsafe(
326 s1,
327 12x detail::param_value_chars) + 1; // for '='
328 12x at_end_ = true;
329 12x return true;
330 }
331
332 inline
333 void
334 12x param_encoded_iter::
335 copy(
336 char*& dest,
337 char const* end) noexcept
338 {
339 12x detail::re_encode_unsafe(
340 dest,
341 end,
342 s0,
343 detail::param_key_chars);
344 12x if(has_value_)
345 {
346 12x *dest++ = '=';
347 12x detail::re_encode_unsafe(
348 dest,
349 end,
350 s1,
351 detail::param_value_chars);
352 }
353 12x }
354
355
356 //------------------------------------------------
357 //
358 // params_encoded_iter_base
359 //
360 //------------------------------------------------
361
362 inline
363 void
364 51x params_encoded_iter_base::
365 measure_impl(
366 std::size_t& n,
367 param_view const& p) noexcept
368 {
369 51x n += detail::re_encoded_size_unsafe(
370 p.key,
371 detail::param_key_chars);
372 51x if(p.has_value)
373 42x n += detail::re_encoded_size_unsafe(
374 p.value,
375 42x detail::param_value_chars) + 1; // for '='
376 51x }
377
378 inline
379 void
380 51x params_encoded_iter_base::
381 copy_impl(
382 char*& dest,
383 char const* end,
384 param_view const& p) noexcept
385 {
386 51x detail::re_encode_unsafe(
387 dest,
388 end,
389 p.key,
390 detail::param_key_chars);
391 51x if(p.has_value)
392 {
393 42x *dest++ = '=';
394 42x detail::re_encode_unsafe(
395 dest,
396 end,
397 p.value,
398 detail::param_value_chars);
399 }
400 51x }
401
402 //------------------------------------------------
403 //
404 // param_value_iter
405 //
406 //------------------------------------------------
407
408 inline
409 void
410 9x param_value_iter::
411 rewind() noexcept
412 {
413 9x at_end_ = false;
414 9x }
415
416 inline
417 bool
418 18x param_value_iter::
419 measure(
420 std::size_t& n) noexcept
421 {
422 18x if(at_end_)
423 9x return false;
424 9x n += nk_; // skip key
425 9x if(has_value_)
426 {
427 5x encoding_opts opt;
428 5x opt.space_as_plus = false;
429 5x n += encoded_size(
430 s0,
431 detail::param_value_chars,
432 5x opt) + 1; // for '='
433 }
434 9x at_end_ = true;
435 9x return true;
436 }
437
438 inline
439 void
440 9x param_value_iter::
441 copy(char*& it, char const* end) noexcept
442 {
443 9x it += nk_; // skip key
444 9x if(! has_value_)
445 4x return;
446 5x *it++ = '=';
447 5x encoding_opts opt;
448 5x opt.space_as_plus = false;
449 5x it += encode(
450 it,
451 5x end - it,
452 s0,
453 detail::param_value_chars,
454 opt);
455 }
456
457 //------------------------------------------------
458 //
459 // param_encoded_value_iter
460 //
461 //------------------------------------------------
462
463 inline
464 void
465 8x param_encoded_value_iter::
466 rewind() noexcept
467 {
468 8x at_end_ = false;
469 8x }
470
471 inline
472 bool
473 16x param_encoded_value_iter::
474 measure(
475 std::size_t& n) noexcept
476 {
477 16x if(at_end_)
478 8x return false;
479 8x n += nk_; // skip key
480 8x if(has_value_)
481 {
482 4x n += detail::re_encoded_size_unsafe(
483 s0,
484 4x detail::param_value_chars) + 1; // for '='
485 }
486 8x at_end_ = true;
487 8x return true;
488 }
489
490 inline
491 void
492 8x param_encoded_value_iter::
493 copy(
494 char*& dest,
495 char const* end) noexcept
496 {
497 8x dest += nk_; // skip key
498 8x if(! has_value_)
499 4x return;
500 4x *dest++ = '=';
501 4x detail::re_encode_unsafe(
502 dest,
503 end,
504 s0,
505 detail::param_value_chars);
506 }
507
508 } // detail
509 } // urls
510 } // boost
511
512 #endif
513