string.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #ifndef BOOST_JSON_IMPL_STRING_HPP
  10. #define BOOST_JSON_IMPL_STRING_HPP
  11. #include <utility>
  12. BOOST_JSON_NS_BEGIN
  13. string::
  14. string(
  15. detail::key_t const&,
  16. string_view s,
  17. storage_ptr sp)
  18. : sp_(std::move(sp))
  19. , impl_(detail::key_t{},
  20. s, sp_)
  21. {
  22. }
  23. string::
  24. string(
  25. detail::key_t const&,
  26. string_view s1,
  27. string_view s2,
  28. storage_ptr sp)
  29. : sp_(std::move(sp))
  30. , impl_(detail::key_t{},
  31. s1, s2, sp_)
  32. {
  33. }
  34. template<class InputIt, class>
  35. string::
  36. string(
  37. InputIt first,
  38. InputIt last,
  39. storage_ptr sp)
  40. : sp_(std::move(sp))
  41. , impl_(first, last, sp_,
  42. iter_cat<InputIt>{})
  43. {
  44. }
  45. template<class InputIt, class>
  46. string&
  47. string::
  48. assign(
  49. InputIt first,
  50. InputIt last)
  51. {
  52. assign(first, last,
  53. iter_cat<InputIt>{});
  54. return *this;
  55. }
  56. template<class InputIt, class>
  57. string&
  58. string::
  59. append(InputIt first, InputIt last)
  60. {
  61. append(first, last,
  62. iter_cat<InputIt>{});
  63. return *this;
  64. }
  65. // KRYSTIAN TODO: this can be done without copies when
  66. // reallocation is not needed, when the iterator is a
  67. // FowardIterator or better, as we can use std::distance
  68. template<class InputIt, class>
  69. auto
  70. string::
  71. insert(
  72. size_type pos,
  73. InputIt first,
  74. InputIt last) ->
  75. string&
  76. {
  77. struct cleanup
  78. {
  79. detail::string_impl& s;
  80. storage_ptr const& sp;
  81. ~cleanup()
  82. {
  83. s.destroy(sp);
  84. }
  85. };
  86. // We use the default storage because
  87. // the allocation is immediately freed.
  88. storage_ptr dsp;
  89. detail::string_impl tmp(
  90. first, last, dsp,
  91. iter_cat<InputIt>{});
  92. cleanup c{tmp, dsp};
  93. std::memcpy(
  94. impl_.insert_unchecked(pos, tmp.size(), sp_),
  95. tmp.data(),
  96. tmp.size());
  97. return *this;
  98. }
  99. // KRYSTIAN TODO: this can be done without copies when
  100. // reallocation is not needed, when the iterator is a
  101. // FowardIterator or better, as we can use std::distance
  102. template<class InputIt, class>
  103. auto
  104. string::
  105. replace(
  106. const_iterator first,
  107. const_iterator last,
  108. InputIt first2,
  109. InputIt last2) ->
  110. string&
  111. {
  112. struct cleanup
  113. {
  114. detail::string_impl& s;
  115. storage_ptr const& sp;
  116. ~cleanup()
  117. {
  118. s.destroy(sp);
  119. }
  120. };
  121. // We use the default storage because
  122. // the allocation is immediately freed.
  123. storage_ptr dsp;
  124. detail::string_impl tmp(
  125. first2, last2, dsp,
  126. iter_cat<InputIt>{});
  127. cleanup c{tmp, dsp};
  128. std::memcpy(
  129. impl_.replace_unchecked(
  130. first - begin(),
  131. last - first,
  132. tmp.size(),
  133. sp_),
  134. tmp.data(),
  135. tmp.size());
  136. return *this;
  137. }
  138. //----------------------------------------------------------
  139. template<class InputIt>
  140. void
  141. string::
  142. assign(
  143. InputIt first,
  144. InputIt last,
  145. std::random_access_iterator_tag)
  146. {
  147. auto dest = impl_.assign(static_cast<
  148. size_type>(last - first), sp_);
  149. while(first != last)
  150. *dest++ = *first++;
  151. }
  152. template<class InputIt>
  153. void
  154. string::
  155. assign(
  156. InputIt first,
  157. InputIt last,
  158. std::input_iterator_tag)
  159. {
  160. if(first == last)
  161. {
  162. impl_.term(0);
  163. return;
  164. }
  165. detail::string_impl tmp(
  166. first, last, sp_,
  167. std::input_iterator_tag{});
  168. impl_.destroy(sp_);
  169. impl_ = tmp;
  170. }
  171. template<class InputIt>
  172. void
  173. string::
  174. append(
  175. InputIt first,
  176. InputIt last,
  177. std::random_access_iterator_tag)
  178. {
  179. auto const n = static_cast<
  180. size_type>(last - first);
  181. std::copy(first, last,
  182. impl_.append(n, sp_));
  183. }
  184. template<class InputIt>
  185. void
  186. string::
  187. append(
  188. InputIt first,
  189. InputIt last,
  190. std::input_iterator_tag)
  191. {
  192. struct cleanup
  193. {
  194. detail::string_impl& s;
  195. storage_ptr const& sp;
  196. ~cleanup()
  197. {
  198. s.destroy(sp);
  199. }
  200. };
  201. // We use the default storage because
  202. // the allocation is immediately freed.
  203. storage_ptr dsp;
  204. detail::string_impl tmp(
  205. first, last, dsp,
  206. std::input_iterator_tag{});
  207. cleanup c{tmp, dsp};
  208. std::memcpy(
  209. impl_.append(tmp.size(), sp_),
  210. tmp.data(), tmp.size());
  211. }
  212. BOOST_JSON_NS_END
  213. #endif