string.hpp 4.6 KB

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