impl/string.ipp

100.0% Lines (167/167) 100.0% List of functions (36/36)
f(x) Functions (36)
Function Calls Lines Branches Blocks
boost::json::string::string(unsigned long, char, boost::json::storage_ptr) :30 78x 100.0% 100.0% boost::json::string::string(char const*, boost::json::storage_ptr) :40 175x 100.0% 100.0% boost::json::string::string(char const*, unsigned long, boost::json::storage_ptr) :49 4x 100.0% 86.0% boost::json::string::string(boost::json::string const&) :59 8x 100.0% 83.0% boost::json::string::string(boost::json::string const&, boost::json::storage_ptr) :66 145x 100.0% 100.0% boost::json::string::string(boost::json::string&&, boost::json::storage_ptr) :75 351x 100.0% 100.0% boost::json::string::string(boost::core::basic_string_view<char>, boost::json::storage_ptr) :84 17884x 100.0% 100.0% boost::json::string::operator=(boost::json::string const&) :100 6x 100.0% 100.0% boost::json::string::operator=(boost::json::string&&) :107 10x 100.0% 100.0% boost::json::string::operator=(char const*) :114 9x 100.0% 100.0% boost::json::string::operator=(boost::core::basic_string_view<char>) :121 8x 100.0% 100.0% boost::json::string::assign(unsigned long, char) :130 83x 100.0% 100.0% boost::json::string::assign(boost::json::string const&) :143 193x 100.0% 100.0% boost::json::string::assign(boost::json::string&&) :155 375x 100.0% 100.0% boost::json::string::assign(char const*, unsigned long) :174 18361x 100.0% 100.0% boost::json::string::assign(char const*) :186 189x 100.0% 100.0% boost::json::string::shrink_to_fit() :201 7x 100.0% 100.0% boost::json::string::try_at(unsigned long) :214 12x 100.0% 100.0% boost::json::string::try_at(unsigned long) const :225 20x 100.0% 100.0% boost::json::string::at(unsigned long, boost::source_location const&) const :236 18x 100.0% 100.0% boost::json::string::clear() :248 2x 100.0% 100.0% boost::json::string::push_back(char) :257 88x 100.0% 100.0% boost::json::string::pop_back() :264 29x 100.0% 100.0% boost::json::string::append(unsigned long, char) :274 4x 100.0% 100.0% boost::json::string::append(boost::core::basic_string_view<char>) :284 45x 100.0% 100.0% boost::json::string::insert(unsigned long, boost::core::basic_string_view<char>) :296 27x 100.0% 100.0% boost::json::string::insert(unsigned long, unsigned long, char) :306 11x 100.0% 100.0% boost::json::string::replace(unsigned long, unsigned long, boost::core::basic_string_view<char>) :321 19x 100.0% 100.0% boost::json::string::replace(unsigned long, unsigned long, unsigned long, char) :332 7x 100.0% 100.0% boost::json::string::erase(unsigned long, unsigned long) :348 10x 100.0% 100.0% boost::json::string::erase(char const*) :369 2x 100.0% 100.0% boost::json::string::erase(char const*, char const*) :377 3x 100.0% 100.0% boost::json::string::resize(unsigned long, char) :392 66x 100.0% 100.0% boost::json::string::swap(boost::json::string&) :412 4x 100.0% 77.0% boost::json::string::reserve_impl(unsigned long) :433 9686x 100.0% 94.0% std::hash<boost::json::string>::operator()(boost::json::string const&) const :459 3x 100.0% 100.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9
10 #ifndef BOOST_JSON_IMPL_STRING_IPP
11 #define BOOST_JSON_IMPL_STRING_IPP
12
13 #include <boost/json/detail/except.hpp>
14 #include <algorithm>
15 #include <new>
16 #include <ostream>
17 #include <stdexcept>
18 #include <string>
19 #include <utility>
20
21 namespace boost {
22 namespace json {
23
24 //----------------------------------------------------------
25 //
26 // Construction
27 //
28 //----------------------------------------------------------
29
30 78x string::
31 string(
32 std::size_t count,
33 char ch,
34 78x storage_ptr sp)
35 78x : sp_(std::move(sp))
36 {
37 78x assign(count, ch);
38 78x }
39
40 175x string::
41 string(
42 char const* s,
43 175x storage_ptr sp)
44 175x : sp_(std::move(sp))
45 {
46 175x assign(s);
47 175x }
48
49 4x string::
50 string(
51 char const* s,
52 std::size_t count,
53 4x storage_ptr sp)
54 4x : sp_(std::move(sp))
55 {
56 4x assign(s, count);
57 4x }
58
59 8x string::
60 8x string(string const& other)
61 8x : sp_(other.sp_)
62 {
63 8x assign(other);
64 8x }
65
66 145x string::
67 string(
68 string const& other,
69 145x storage_ptr sp)
70 145x : sp_(std::move(sp))
71 {
72 145x assign(other);
73 145x }
74
75 351x string::
76 string(
77 string&& other,
78 351x storage_ptr sp)
79 351x : sp_(std::move(sp))
80 {
81 351x assign(std::move(other));
82 351x }
83
84 17884x string::
85 string(
86 string_view s,
87 17884x storage_ptr sp)
88 17884x : sp_(std::move(sp))
89 {
90 17884x assign(s);
91 17884x }
92
93 //----------------------------------------------------------
94 //
95 // Assignment
96 //
97 //----------------------------------------------------------
98
99 string&
100 6x string::
101 operator=(string const& other)
102 {
103 6x return assign(other);
104 }
105
106 string&
107 10x string::
108 operator=(string&& other)
109 {
110 10x return assign(std::move(other));
111 }
112
113 string&
114 9x string::
115 operator=(char const* s)
116 {
117 9x return assign(s);
118 }
119
120 string&
121 8x string::
122 operator=(string_view s)
123 {
124 8x return assign(s);
125 }
126
127
128
129 string&
130 83x string::
131 assign(
132 size_type count,
133 char ch)
134 {
135 64x std::char_traits<char>::assign(
136 83x impl_.assign(count, sp_),
137 count,
138 ch);
139 64x return *this;
140 }
141
142 string&
143 193x string::
144 assign(
145 string const& other)
146 {
147 193x if(this == &other)
148 1x return *this;
149 192x return assign(
150 other.data(),
151 165x other.size());
152 }
153
154 string&
155 375x string::
156 assign(string&& other)
157 {
158 375x if( &other == this )
159 1x return *this;
160
161 374x if(*sp_ == *other.sp_)
162 {
163 340x impl_.destroy(sp_);
164 340x impl_ = other.impl_;
165 340x ::new(&other.impl_) detail::string_impl();
166 340x return *this;
167 }
168
169 // copy
170 34x return assign(other);
171 }
172
173 string&
174 18361x string::
175 assign(
176 char const* s,
177 size_type count)
178 {
179 18240x std::char_traits<char>::copy(
180 18361x impl_.assign(count, sp_),
181 s, count);
182 18240x return *this;
183 }
184
185 string&
186 189x string::
187 assign(
188 char const* s)
189 {
190 189x return assign(s, std::char_traits<
191 186x char>::length(s));
192 }
193
194 //----------------------------------------------------------
195 //
196 // Capacity
197 //
198 //----------------------------------------------------------
199
200 void
201 7x string::
202 shrink_to_fit()
203 {
204 7x impl_.shrink_to_fit(sp_);
205 7x }
206
207 //----------------------------------------------------------
208 //
209 // Access
210 //
211 //----------------------------------------------------------
212
213 system::result<char&>
214 12x string::try_at(std::size_t pos) noexcept
215 {
216 12x if( pos < size() )
217 10x return impl_.data()[pos];
218
219 2x system::error_code ec;
220 2x BOOST_JSON_FAIL(ec, error::out_of_range);
221 2x return ec;
222 }
223
224 system::result<char const&>
225 20x string::try_at(std::size_t pos) const noexcept
226 {
227 20x if( pos < size() )
228 18x return impl_.data()[pos];
229
230 2x system::error_code ec;
231 2x BOOST_JSON_FAIL(ec, error::out_of_range);
232 2x return ec;
233 }
234
235 char const&
236 18x string::at(std::size_t pos, source_location const& loc) const
237 {
238 18x return try_at(pos).value(loc);
239 }
240
241 //----------------------------------------------------------
242 //
243 // Operations
244 //
245 //----------------------------------------------------------
246
247 void
248 2x string::
249 clear() noexcept
250 {
251 2x impl_.term(0);
252 2x }
253
254 //----------------------------------------------------------
255
256 void
257 88x string::
258 push_back(char ch)
259 {
260 88x *impl_.append(1, sp_) = ch;
261 86x }
262
263 void
264 29x string::
265 pop_back()
266 {
267 29x back() = 0;
268 29x impl_.size(impl_.size() - 1);
269 29x }
270
271 //----------------------------------------------------------
272
273 string&
274 4x string::
275 append(size_type count, char ch)
276 {
277 2x std::char_traits<char>::assign(
278 4x impl_.append(count, sp_),
279 count, ch);
280 2x return *this;
281 }
282
283 string&
284 45x string::
285 append(string_view sv)
286 {
287 90x std::char_traits<char>::copy(
288 45x impl_.append(sv.size(), sp_),
289 sv.data(), sv.size());
290 34x return *this;
291 }
292
293 //----------------------------------------------------------
294
295 string&
296 27x string::
297 insert(
298 size_type pos,
299 string_view sv)
300 {
301 27x impl_.insert(pos, sv.data(), sv.size(), sp_);
302 17x return *this;
303 }
304
305 string&
306 11x string::
307 insert(
308 std::size_t pos,
309 std::size_t count,
310 char ch)
311 {
312 6x std::char_traits<char>::assign(
313 11x impl_.insert_unchecked(pos, count, sp_),
314 count, ch);
315 6x return *this;
316 }
317
318 //----------------------------------------------------------
319
320 string&
321 19x string::
322 replace(
323 std::size_t pos,
324 std::size_t count,
325 string_view sv)
326 {
327 19x impl_.replace(pos, count, sv.data(), sv.size(), sp_);
328 15x return *this;
329 }
330
331 string&
332 7x string::
333 replace(
334 std::size_t pos,
335 std::size_t count,
336 std::size_t count2,
337 char ch)
338 {
339 4x std::char_traits<char>::assign(
340 7x impl_.replace_unchecked(pos, count, count2, sp_),
341 count2, ch);
342 4x return *this;
343 }
344
345 //----------------------------------------------------------
346
347 string&
348 10x string::
349 erase(
350 size_type pos,
351 size_type count)
352 {
353 10x if(pos > impl_.size())
354 {
355 BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
356 1x detail::throw_system_error( error::out_of_range, &loc );
357 }
358 9x if( count > impl_.size() - pos)
359 4x count = impl_.size() - pos;
360 9x std::char_traits<char>::move(
361 9x impl_.data() + pos,
362 9x impl_.data() + pos + count,
363 9x impl_.size() - pos - count + 1);
364 9x impl_.term(impl_.size() - count);
365 9x return *this;
366 }
367
368 auto
369 2x string::
370 erase(const_iterator pos) ->
371 iterator
372 {
373 2x return erase(pos, pos+1);
374 }
375
376 auto
377 3x string::
378 erase(
379 const_iterator first,
380 const_iterator last) ->
381 iterator
382 {
383 3x auto const pos = first - begin();
384 3x auto const count = last - first;
385 3x erase(pos, count);
386 3x return data() + pos;
387 }
388
389 //----------------------------------------------------------
390
391 void
392 66x string::
393 resize(size_type count, char ch)
394 {
395 66x if(count <= impl_.size())
396 {
397 25x impl_.term(count);
398 25x return;
399 }
400
401 41x reserve(count);
402 35x std::char_traits<char>::assign(
403 impl_.end(),
404 35x count - impl_.size(),
405 ch);
406 35x grow(count - size());
407 }
408
409 //----------------------------------------------------------
410
411 void
412 4x string::
413 swap(string& other)
414 {
415 4x if(*sp_ == *other.sp_)
416 {
417 3x std::swap(impl_, other.impl_);
418 3x return;
419 }
420 string temp1(
421 1x std::move(*this), other.sp_);
422 string temp2(
423 1x std::move(other), sp_);
424 1x this->~string();
425 1x ::new(this) string(pilfer(temp2));
426 1x other.~string();
427 1x ::new(&other) string(pilfer(temp1));
428 1x }
429
430 //----------------------------------------------------------
431
432 void
433 9686x string::
434 reserve_impl(size_type new_cap)
435 {
436 9686x BOOST_ASSERT(
437 new_cap >= impl_.capacity());
438 9686x if(new_cap > impl_.capacity())
439 {
440 // grow
441 9686x new_cap = detail::string_impl::growth(
442 new_cap, impl_.capacity());
443 9685x detail::string_impl tmp(new_cap, sp_);
444 9675x std::char_traits<char>::copy(tmp.data(),
445 9675x impl_.data(), impl_.size() + 1);
446 9675x tmp.size(impl_.size());
447 9675x impl_.destroy(sp_);
448 9675x impl_ = tmp;
449 9675x return;
450 }
451 }
452
453 } // namespace json
454 } // namespace boost
455
456 //----------------------------------------------------------
457
458 std::size_t
459 3x std::hash< ::boost::json::string >::operator()(
460 ::boost::json::string const& js ) const noexcept
461 {
462 3x return ::boost::hash< ::boost::json::string >()( js );
463 }
464
465 #endif
466