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