2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
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)
7 // Official repository: https://github.com/boostorg/json
10 #ifndef BOOST_JSON_IMPL_STRING_IPP
11 #define BOOST_JSON_IMPL_STRING_IPP
13 #include <boost/json/detail/except.hpp>
23 //----------------------------------------------------------
27 //----------------------------------------------------------
59 string(string const& other)
80 assign(std::move(other));
92 //----------------------------------------------------------
96 //----------------------------------------------------------
100 operator=(string const& other)
102 return assign(other);
107 operator=(string&& other)
109 return assign(std::move(other));
114 operator=(char const* s)
121 operator=(string_view s)
134 std::char_traits<char>::assign(
135 impl_.assign(count, sp_),
155 assign(string&& other)
157 if(*sp_ == *other.sp_)
161 ::new(&other.impl_) detail::string_impl();
166 return assign(other);
175 std::char_traits<char>::copy(
176 impl_.assign(count, sp_),
186 return assign(s, std::char_traits<
190 //----------------------------------------------------------
194 //----------------------------------------------------------
200 impl_.shrink_to_fit(sp_);
203 //----------------------------------------------------------
207 //----------------------------------------------------------
216 //----------------------------------------------------------
222 *impl_.append(1, sp_) = ch;
230 impl_.size(impl_.size() - 1);
233 //----------------------------------------------------------
237 append(size_type count, char ch)
239 std::char_traits<char>::assign(
240 impl_.append(count, sp_),
247 append(string_view sv)
249 std::char_traits<char>::copy(
250 impl_.append(sv.size(), sp_),
251 sv.data(), sv.size());
255 //----------------------------------------------------------
263 impl_.insert(pos, sv.data(), sv.size(), sp_);
274 std::char_traits<char>::assign(
275 impl_.insert_unchecked(pos, count, sp_),
280 //----------------------------------------------------------
289 impl_.replace(pos, count, sv.data(), sv.size(), sp_);
301 std::char_traits<char>::assign(
302 impl_.replace_unchecked(pos, count, count2, sp_),
307 //----------------------------------------------------------
315 if(pos > impl_.size())
316 detail::throw_out_of_range(
317 BOOST_CURRENT_LOCATION);
318 if( count > impl_.size() - pos)
319 count = impl_.size() - pos;
320 std::char_traits<char>::move(
322 impl_.data() + pos + count,
323 impl_.size() - pos - count + 1);
324 impl_.term(impl_.size() - count);
330 erase(const_iterator pos) ->
333 return erase(pos, pos+1);
339 const_iterator first,
340 const_iterator last) ->
343 auto const pos = first - begin();
344 auto const count = last - first;
349 //----------------------------------------------------------
353 resize(size_type count, char ch)
355 if(count <= impl_.size())
362 std::char_traits<char>::assign(
364 count - impl_.size(),
366 grow(count - size());
369 //----------------------------------------------------------
375 BOOST_ASSERT(this != &other);
376 if(*sp_ == *other.sp_)
378 std::swap(impl_, other.impl_);
382 std::move(*this), other.sp_);
384 std::move(other), sp_);
386 ::new(this) string(pilfer(temp2));
388 ::new(&other) string(pilfer(temp1));
391 //----------------------------------------------------------
395 reserve_impl(size_type new_cap)
398 new_cap >= impl_.capacity());
399 if(new_cap > impl_.capacity())
402 new_cap = detail::string_impl::growth(
403 new_cap, impl_.capacity());
404 detail::string_impl tmp(new_cap, sp_);
405 std::char_traits<char>::copy(tmp.data(),
406 impl_.data(), impl_.size() + 1);
407 tmp.size(impl_.size());