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

97.4% Lines (223/229) 100.0% List of functions (35/35)
url_impl.hpp
f(x) Functions (35)
Function Calls Lines Blocks
boost::urls::detail::url_impl::apply_scheme(boost::core::basic_string_view<char>) :34 7235x 100.0% 100.0% boost::urls::detail::url_impl::apply_userinfo(boost::urls::pct_string_view const&, boost::urls::pct_string_view const*) :44 423x 100.0% 93.0% boost::urls::detail::url_impl::apply_host(boost::urls::host_type, boost::urls::pct_string_view, unsigned char const*) :75 6613x 100.0% 90.0% boost::urls::detail::url_impl::apply_port(boost::core::basic_string_view<char>, unsigned short) :99 378x 100.0% 80.0% boost::urls::detail::url_impl::apply_authority(boost::urls::detail::url_impl const&) :114 6541x 100.0% 88.0% boost::urls::detail::url_impl::apply_path(boost::urls::pct_string_view, unsigned long) :142 19825x 100.0% 100.0% boost::urls::detail::url_impl::apply_query(boost::urls::pct_string_view, unsigned long) :157 4568x 100.0% 100.0% boost::urls::detail::url_impl::apply_frag(boost::urls::pct_string_view) :171 1242x 100.0% 100.0% boost::urls::detail::url_impl::len(int, int) const :184 52168x 100.0% 75.0% boost::urls::detail::url_impl::len(int) const :197 790574x 100.0% 86.0% boost::urls::detail::url_impl::offset(int) const :209 2085195x 100.0% 100.0% boost::urls::detail::url_impl::get(int) const :221 157059x 100.0% 100.0% boost::urls::detail::url_impl::get(int, int) const :231 1186x 100.0% 100.0% boost::urls::detail::url_impl::pct_get(int) const :242 3404x 100.0% 100.0% boost::urls::detail::url_impl::pct_get(int, int) const :255 130x 100.0% 100.0% boost::urls::detail::url_impl::set_size(int, unsigned long) :275 73255x 100.0% 100.0% boost::urls::detail::url_impl::split(int, unsigned long) :299 1914x 100.0% 80.0% boost::urls::detail::url_impl::adjust_right(int, int, unsigned long) :313 2944x 100.0% 100.0% boost::urls::detail::url_impl::adjust_left(int, int, unsigned long) :327 2408x 100.0% 100.0% boost::urls::detail::url_impl::collapse(int, int, unsigned long) :341 5333x 100.0% 100.0% boost::urls::detail::path_ref::path_ref(boost::urls::detail::url_impl const&) :360 10616x 100.0% 100.0% boost::urls::detail::path_ref::path_ref(boost::core::basic_string_view<char>, unsigned long, unsigned long) :379 222x 100.0% 100.0% boost::urls::detail::path_ref::buffer() const :393 12780x 100.0% 100.0% boost::urls::detail::path_ref::size() const :408 35915x 100.0% 100.0% boost::urls::detail::path_ref::data() const :418 39066x 100.0% 100.0% boost::urls::detail::path_ref::end() const :429 16785x 100.0% 100.0% boost::urls::detail::path_ref::nseg() const :440 44197x 100.0% 100.0% boost::urls::detail::path_ref::decoded_size() const :450 17364x 100.0% 100.0% boost::urls::detail::query_ref::query_ref(boost::core::basic_string_view<char>, unsigned long, unsigned long) :465 3333x 100.0% 100.0% boost::urls::detail::query_ref::query_ref(boost::urls::detail::url_impl const&) :478 8962x 100.0% 100.0% boost::urls::detail::query_ref::buffer() const :503 24x 66.7% 56.0% boost::urls::detail::query_ref::size() const :532 32286x 100.0% 100.0% boost::urls::detail::query_ref::begin() const :545 19117x 87.5% 83.0% boost::urls::detail::query_ref::end() const :564 10123x 100.0% 100.0% boost::urls::detail::query_ref::nparam() const :575 48601x 100.0% 100.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2022 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_URL_IMPL_HPP
12 #define BOOST_URL_DETAIL_IMPL_URL_IMPL_HPP
13
14 #include <boost/url/detail/config.hpp>
15 #include <boost/url/detail/memcpy.hpp>
16 #include <boost/url/detail/path.hpp>
17 #include <boost/url/detail/url_impl.hpp>
18 #include <boost/url/scheme.hpp>
19 #include <boost/assert.hpp>
20 #include <cstring>
21
22 namespace boost {
23 namespace urls {
24 namespace detail {
25
26 //------------------------------------------------
27 //
28 // url_impl
29 //
30 //------------------------------------------------
31
32 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
33 void
34 7235x url_impl::
35 apply_scheme(
36 core::string_view s) noexcept
37 {
38 7235x scheme_ = string_to_scheme(s);
39 7235x set_size(id_scheme, s.size() + 1);
40 7235x }
41
42 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
43 void
44 423x url_impl::
45 apply_userinfo(
46 pct_string_view const& user,
47 pct_string_view const* pass) noexcept
48 {
49 // this function is for
50 // authority_view_rule only
51 423x BOOST_ASSERT(from_ == from::authority);
52
53 // userinfo
54 423x set_size(id_user, user.size());
55 423x decoded_[id_user] =
56 423x detail::to_size_type(
57 user.decoded_size());
58 423x if(pass)
59 {
60 286x set_size(id_pass,
61 286x pass->size() + 2);
62 286x decoded_[id_pass] =
63 286x detail::to_size_type(
64 pass->decoded_size());
65 }
66 else
67 {
68 // trailing '@'
69 137x set_size(id_pass, 1 );
70 }
71 423x }
72
73 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
74 void
75 6613x url_impl::
76 apply_host(
77 host_type ht,
78 pct_string_view s,
79 unsigned char const* addr) noexcept
80 {
81 // this function is for
82 // authority_view_rule only
83 6613x BOOST_ASSERT(from_ == from::authority);
84
85 // host, port
86 6613x host_type_ = ht;
87 6613x set_size(id_host, s.size());
88 6613x decoded_[id_host] =
89 6613x detail::to_size_type(
90 s.decoded_size());
91 detail::memcpy(
92 6613x ip_addr_,
93 addr,
94 sizeof(ip_addr_));
95 6613x }
96
97 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
98 void
99 378x url_impl::
100 apply_port(
101 core::string_view s,
102 unsigned short pn) noexcept
103 {
104 // this function is for
105 // authority_view_rule only
106 378x BOOST_ASSERT(from_ == from::authority);
107
108 378x port_number_ = pn;
109 378x set_size(id_port, 1 + s.size());
110 378x }
111
112 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
113 void
114 6541x url_impl::
115 apply_authority(
116 url_impl const& a) noexcept
117 {
118 6541x BOOST_ASSERT(from_ != from::authority);
119
120 // userinfo
121 6541x set_size(id_user,
122 6541x a.len(id_user) +
123 6541x (from_ == from::authority ? 0 : 2));
124 6541x set_size(id_pass, a.len(id_pass));
125 6541x decoded_[id_user] = a.decoded_[id_user];
126 6541x decoded_[id_pass] = a.decoded_[id_pass];
127
128 // host, port
129 6541x host_type_ = a.host_type_;
130 6541x port_number_ = a.port_number_;
131 6541x set_size(id_host, a.len(id_host));
132 6541x set_size(id_port, a.len(id_port));
133 detail::memcpy(
134 6541x ip_addr_,
135 6541x a.ip_addr_,
136 sizeof(ip_addr_));
137 6541x decoded_[id_host] = a.decoded_[id_host];
138 6541x }
139
140 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
141 void
142 19825x url_impl::
143 apply_path(
144 pct_string_view s,
145 std::size_t nseg) noexcept
146 {
147 19825x set_size(id_path, s.size());
148 19825x decoded_[id_path] =
149 19825x detail::to_size_type(
150 s.decoded_size());
151 19825x nseg_ = detail::to_size_type(
152 detail::path_segments(s, nseg));
153 19825x }
154
155 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
156 void
157 4568x url_impl::
158 apply_query(
159 pct_string_view s,
160 std::size_t n) noexcept
161 {
162 4568x nparam_ = detail::to_size_type(n);
163 4568x set_size(id_query, 1 + s.size());
164 4568x decoded_[id_query] =
165 4568x detail::to_size_type(
166 s.decoded_size());
167 4568x }
168
169 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
170 void
171 1242x url_impl::
172 apply_frag(
173 pct_string_view s) noexcept
174 {
175 1242x set_size(id_frag, s.size() + 1);
176 1242x decoded_[id_frag] =
177 1242x detail::to_size_type(
178 s.decoded_size());
179 1242x }
180
181 // return length of [first, last)
182 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
183 std::size_t
184 52168x url_impl::
185 len(
186 int first,
187 int last) const noexcept
188 {
189 52168x BOOST_ASSERT(first <= last);
190 52168x BOOST_ASSERT(last <= id_end);
191 52168x return offset(last) - offset(first);
192 }
193
194 // return length of part
195 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
196 std::size_t
197 790574x url_impl::
198 len(int id) const noexcept
199 {
200 return id == id_end
201 1581148x ? zero_
202 790574x : ( offset(id + 1) -
203 1581148x offset(id) );
204 }
205
206 // return offset of id
207 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
208 std::size_t
209 2085195x url_impl::
210 offset(int id) const noexcept
211 {
212 return
213 id == id_scheme
214 2085195x ? zero_
215 2085195x : offset_[id];
216 }
217
218 // return id as string
219 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
220 core::string_view
221 157059x url_impl::
222 get(int id) const noexcept
223 {
224 return {
225 157059x cs_ + offset(id), len(id) };
226 }
227
228 // return [first, last) as string
229 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
230 core::string_view
231 1186x url_impl::
232 get(int first,
233 int last) const noexcept
234 {
235 1186x return { cs_ + offset(first),
236 1186x offset(last) - offset(first) };
237 }
238
239 // return id as pct-string
240 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
241 pct_string_view
242 3404x url_impl::
243 pct_get(
244 int id) const noexcept
245 {
246 3404x return make_pct_string_view_unsafe(
247 3404x cs_ + offset(id),
248 len(id),
249 6808x decoded_[id]);
250 }
251
252 // return [first, last) as pct-string
253 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
254 pct_string_view
255 130x url_impl::
256 pct_get(
257 int first,
258 int last) const noexcept
259 {
260 130x auto const pos = offset(first);
261 130x std::size_t n = 0;
262 420x for(auto i = first; i < last;)
263 290x n += decoded_[i++];
264 130x return make_pct_string_view_unsafe(
265 130x cs_ + pos,
266 130x offset(last) - pos,
267 130x n);
268 }
269
270 //------------------------------------------------
271
272 // change id to size n
273 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
274 void
275 73255x url_impl::
276 set_size(
277 int id,
278 std::size_t n) noexcept
279 {
280 73255x auto const cur = len(id);
281 73255x if(n >= cur)
282 {
283 71867x auto const d = n - cur;
284 71867x for(auto i = id + 1;
285 402796x i <= id_end; ++i)
286 330929x offset_[i] += detail::to_size_type(d);
287 71867x return;
288 }
289 1388x auto const d = cur - n;
290 1388x for(auto i = id + 1;
291 5580x i <= id_end; ++i)
292 4192x offset_[i] -= detail::to_size_type(d);
293 }
294
295 // trim id to size n,
296 // moving excess into id+1
297 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
298 void
299 1914x url_impl::
300 split(
301 int id,
302 std::size_t n) noexcept
303 {
304 1914x BOOST_ASSERT(id < id_end - 1);
305 //BOOST_ASSERT(n <= len(id));
306 1914x offset_[id + 1] = detail::to_size_type(
307 1914x offset(id) + n);
308 1914x }
309
310 // add n to [first, last]
311 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
312 void
313 2944x url_impl::
314 adjust_right(
315 int first,
316 int last,
317 std::size_t n) noexcept
318 {
319 2944x for(int i = first;
320 17615x i <= last; ++i)
321 14671x offset_[i] += detail::to_size_type(n);
322 2944x }
323
324 // remove n from [first, last]
325 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
326 void
327 2408x url_impl::
328 adjust_left(
329 int first,
330 int last,
331 std::size_t n) noexcept
332 {
333 2408x for(int i = first;
334 12133x i <= last; ++i)
335 9725x offset_[i] -= detail::to_size_type(n);
336 2408x }
337
338 // set [first, last) offset
339 BOOST_URL_CXX20_CONSTEXPR_OR_INLINE
340 void
341 5333x url_impl::
342 collapse(
343 int first,
344 int last,
345 std::size_t n) noexcept
346 {
347 5333x for(int i = first + 1;
348 6674x i < last; ++i)
349 1341x offset_[i] = detail::to_size_type(n);
350 5333x }
351
352
353 //------------------------------------------------
354 //
355 // path_ref
356 //
357 //------------------------------------------------
358
359 inline
360 10616x path_ref::
361 path_ref(
362 10616x url_impl const& impl) noexcept
363 {
364 10616x if(impl.from_ == url_impl::from::url)
365 {
366 10054x impl_ = &impl;
367 }
368 else
369 {
370 562x core::string_view s = impl.get(id_path);
371 562x data_ = s.data();
372 562x size_ = s.size();
373 562x nseg_ = impl.nseg_;
374 562x dn_ = impl.decoded_[id_path];
375 }
376 10616x }
377
378 inline
379 222x path_ref::
380 path_ref(
381 core::string_view s,
382 std::size_t dn,
383 222x std::size_t nseg) noexcept
384 444x : data_(s.data())
385 222x , size_(s.size())
386 222x , nseg_(nseg)
387 222x , dn_(dn)
388 {
389 222x }
390
391 inline
392 pct_string_view
393 12780x path_ref::
394 buffer() const noexcept
395 {
396 12780x if(impl_)
397 10336x return make_pct_string_view_unsafe(
398 10336x impl_->cs_ +
399 10336x impl_->offset(id_path),
400 10336x impl_->len(id_path),
401 20672x impl_->decoded_[id_path]);
402 2444x return make_pct_string_view_unsafe(
403 2444x data_, size_, dn_);
404 }
405
406 inline
407 std::size_t
408 35915x path_ref::
409 size() const noexcept
410 {
411 35915x if(impl_)
412 34354x return impl_->len(id_path);
413 1561x return size_;
414 }
415
416 inline
417 char const*
418 39066x path_ref::
419 data() const noexcept
420 {
421 39066x if(impl_)
422 33342x return impl_->cs_ +
423 33342x impl_->offset(id_path);
424 5724x return data_;
425 }
426
427 inline
428 char const*
429 16785x path_ref::
430 end() const noexcept
431 {
432 16785x if(impl_)
433 15072x return impl_->cs_ +
434 15072x impl_->offset(id_query);
435 1713x return data_ + size_;
436 }
437
438 inline
439 std::size_t
440 44197x path_ref::
441 nseg() const noexcept
442 {
443 44197x if(impl_)
444 40398x return impl_->nseg_;
445 3799x return nseg_;
446 }
447
448 inline
449 std::size_t
450 17364x path_ref::
451 decoded_size() const noexcept
452 {
453 17364x if(impl_)
454 16588x return impl_->decoded_[id_path];
455 776x return dn_;
456 }
457
458 //------------------------------------------------
459 //
460 // query_ref
461 //
462 //------------------------------------------------
463
464 inline
465 3333x query_ref::
466 query_ref(
467 core::string_view s,
468 std::size_t dn,
469 3333x std::size_t nparam) noexcept
470 6666x : data_(s.data())
471 3333x , size_(s.size())
472 3333x , nparam_(nparam)
473 3333x , dn_(dn)
474 {
475 3333x }
476
477 inline
478 8962x query_ref::
479 query_ref(
480 8962x url_impl const& impl) noexcept
481 {
482 8962x if(impl.from_ == url_impl::from::url)
483 {
484 8831x impl_ = &impl;
485 }
486 else
487 {
488 131x core::string_view s = impl.get(id_query);
489 131x if (!s.empty())
490 {
491 125x s.remove_prefix(1);
492 125x question_mark_ = true;
493 }
494 131x data_ = s.data();
495 131x size_ = s.size();
496 131x nparam_ = impl.nparam_;
497 131x dn_ = impl.decoded_[id_query];
498 }
499 8962x }
500
501 inline
502 pct_string_view
503 24x query_ref::
504 buffer() const noexcept
505 {
506 24x if(impl_)
507 {
508 2x auto pos = impl_->offset_[id_query];
509 2x auto pos1 = impl_->offset_[id_frag];
510 2x if(pos < pos1)
511 {
512 ++pos; // no '?'
513 return make_pct_string_view_unsafe(
514 impl_->cs_ + pos,
515 pos1 - pos,
516 impl_->decoded_[id_query]);
517 }
518 // empty
519 2x return make_pct_string_view_unsafe(
520 2x impl_->cs_ + pos,
521 0,
522 2x 0);
523 }
524 // no '?'
525 22x return make_pct_string_view_unsafe(
526 22x data_, size_, dn_);
527 }
528
529 // with '?'
530 inline
531 std::size_t
532 32286x query_ref::
533 size() const noexcept
534 {
535 32286x if(impl_)
536 28517x return impl_->len(id_query);
537 3769x if(size_ > 0)
538 3727x return size_ + 1;
539 42x return question_mark_;
540 }
541
542 // no '?'
543 inline
544 char const*
545 19117x query_ref::
546 begin() const noexcept
547 {
548 19117x if(impl_)
549 {
550 // using the offset array here
551 15173x auto pos = impl_->offset_[id_query];
552 15173x auto pos1 = impl_->offset_[id_frag];
553 15173x if(pos < pos1)
554 15173x return impl_->cs_ + pos + 1; // no '?'
555 // empty
556 return impl_->cs_ + pos;
557 }
558 3944x return data_;
559
560 }
561
562 inline
563 char const*
564 10123x query_ref::
565 end() const noexcept
566 {
567 10123x if(impl_)
568 8590x return impl_->cs_ +
569 8590x impl_->offset(id_frag);
570 1533x return data_ + size_;
571 }
572
573 inline
574 std::size_t
575 48601x query_ref::
576 nparam() const noexcept
577 {
578 48601x if(impl_)
579 42461x return impl_->nparam_;
580 6140x return nparam_;
581 }
582
583 } // detail
584 } // urls
585 } // boost
586
587 #endif
588