include/boost/url/impl/url.hpp

97.4% Lines (76/78) 100.0% List of functions (11/11)
url.hpp
f(x) Functions (11)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2022 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_IMPL_URL_HPP
12 #define BOOST_URL_IMPL_URL_HPP
13
14 #include <boost/url/detail/except.hpp>
15 #include <boost/assert.hpp>
16 #include <cstring>
17
18 namespace boost {
19 namespace urls {
20
21 //------------------------------------------------
22
23 inline
24 7258x url::
25 7258x ~url()
26 {
27 7258x if(s_)
28 {
29 5121x BOOST_ASSERT(
30 cap_ != 0);
31 5121x deallocate(s_);
32 }
33 7258x }
34
35 // construct empty
36 inline
37 1326x url::
38 url() noexcept = default;
39
40 inline
41 2345x url::
42 2345x url(core::string_view s)
43 2842x : url(parse_uri_reference(s
44 2345x ).value(BOOST_URL_POS))
45 {
46 1848x }
47
48 inline
49 1647x url::
50 1647x url(url&& u) noexcept
51 1647x : url_base(u.impl_)
52 {
53 1647x s_ = u.s_;
54 1647x cap_ = u.cap_;
55 1647x u.s_ = nullptr;
56 1647x u.cap_ = 0;
57 1647x u.impl_ = {from::url};
58 1647x }
59
60 inline
61 url&
62 659x url::
63 operator=(url&& u) noexcept
64 {
65 659x if(this == &u)
66 1x return *this;
67 658x if(s_)
68 3x deallocate(s_);
69 658x impl_ = u.impl_;
70 658x s_ = u.s_;
71 658x cap_ = u.cap_;
72 658x u.s_ = nullptr;
73 658x u.cap_ = 0;
74 658x u.impl_ = {from::url};
75 658x return *this;
76 }
77
78 //------------------------------------------------
79
80 inline
81 char*
82 7685x url::
83 allocate(std::size_t n)
84 {
85 7685x auto s = new char[n + 1];
86 7685x cap_ = n;
87 7685x return s;
88 }
89
90 inline
91 void
92 7685x url::
93 deallocate(char* s)
94 {
95 7685x delete[] s;
96 7685x }
97
98 inline
99 void
100 272x url::
101 clear_impl() noexcept
102 {
103 272x if(s_)
104 {
105 // preserve capacity
106 139x impl_ = {from::url};
107 139x s_[0] = '\0';
108 139x impl_.cs_ = s_;
109 }
110 else
111 {
112 133x BOOST_ASSERT(impl_.cs_[0] == 0);
113 }
114 272x }
115
116 inline
117 void
118 12797x url::
119 reserve_impl(
120 std::size_t n,
121 op_t& op)
122 {
123 12797x if(n > max_size())
124 detail::throw_length_error();
125 12797x if(n <= cap_)
126 5112x return;
127 char* s;
128 7685x if(s_ != nullptr)
129 {
130 // 50% growth policy
131 2561x auto const h = cap_ / 2;
132 std::size_t new_cap;
133 2561x if(cap_ <= max_size() - h)
134 2561x new_cap = cap_ + h;
135 else
136 new_cap = max_size();
137 2561x if( new_cap < n)
138 1171x new_cap = n;
139 2561x s = allocate(new_cap);
140 2561x std::memcpy(s, s_, size() + 1);
141 2561x BOOST_ASSERT(! op.old);
142 2561x op.old = s_;
143 2561x s_ = s;
144 }
145 else
146 {
147 5124x s_ = allocate(n);
148 5124x s_[0] = '\0';
149 }
150 7685x impl_.cs_ = s_;
151 }
152
153 inline
154 void
155 2561x url::
156 cleanup(
157 op_t& op)
158 {
159 2561x if(op.old)
160 2561x deallocate(op.old);
161 2561x }
162
163 //------------------------------------------------
164
165 inline
166 void
167 2x url::
168 swap(url& other) noexcept
169 {
170 2x if (this == &other)
171 1x return;
172 1x std::swap(s_, other.s_);
173 1x std::swap(cap_, other.cap_);
174 1x std::swap(impl_, other.impl_);
175 1x std::swap(external_impl_, other.external_impl_);
176 }
177
178 } // urls
179 } // boost
180
181 #endif
182