url.ipp
2.75 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
//
// 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_URL_IPP
#define BOOST_URL_IMPL_URL_IPP
#include <boost/url/url.hpp>
#include <boost/assert.hpp>
namespace boost {
namespace urls {
//------------------------------------------------
url::
~url()
{
if(s_)
{
BOOST_ASSERT(
cap_ != 0);
deallocate(s_);
}
}
// construct empty
url::
url() noexcept = default;
url::
url(string_view s)
: url(parse_uri_reference(s
).value(BOOST_URL_POS))
{
}
url::
url(url&& u) noexcept
: url_base(u.impl_)
{
s_ = u.s_;
cap_ = u.cap_;
u.s_ = nullptr;
u.cap_ = 0;
u.impl_ = {from::url};
}
url&
url::
operator=(url&& u) noexcept
{
if(s_)
deallocate(s_);
impl_ = u.impl_;
s_ = u.s_;
cap_ = u.cap_;
u.s_ = nullptr;
u.cap_ = 0;
u.impl_ = {from::url};
return *this;
}
//------------------------------------------------
char*
url::
allocate(std::size_t n)
{
auto s = new char[n + 1];
cap_ = n;
return s;
}
void
url::
deallocate(char* s)
{
delete[] s;
}
void
url::
clear_impl() noexcept
{
if(s_)
{
// preserve capacity
impl_ = {from::url};
s_[0] = '\0';
impl_.cs_ = s_;
}
else
{
BOOST_ASSERT(impl_.cs_ ==
detail::empty_c_str_);
}
}
void
url::
reserve_impl(
std::size_t n,
op_t& op)
{
if(n > max_size())
detail::throw_length_error();
if(n <= cap_)
return;
char* s;
if(s_ != nullptr)
{
// 50% growth policy
auto const h = cap_ / 2;
std::size_t new_cap;
if(cap_ <= max_size() - h)
new_cap = cap_ + h;
else
new_cap = max_size();
if( new_cap < n)
new_cap = n;
s = allocate(new_cap);
std::memcpy(s, s_, size() + 1);
BOOST_ASSERT(! op.old);
op.old = s_;
s_ = s;
}
else
{
s_ = allocate(n);
s_[0] = '\0';
}
impl_.cs_ = s_;
}
void
url::
cleanup(
op_t& op)
{
if(op.old)
deallocate(op.old);
}
//------------------------------------------------
void
url::
swap(url& other) noexcept
{
if (this == &other)
return;
std::swap(s_, other.s_);
std::swap(cap_, other.cap_);
std::swap(impl_, other.impl_);
std::swap(pi_, other.pi_);
if (pi_ == &other.impl_)
pi_ = &impl_;
if (other.pi_ == &impl_)
other.pi_ = &other.impl_;
}
} // urls
} // boost
#endif