string.ipp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_IPP
  10. #define BOOST_JSON_IMPL_STRING_IPP
  11. #include <boost/json/detail/except.hpp>
  12. #include <algorithm>
  13. #include <new>
  14. #include <ostream>
  15. #include <stdexcept>
  16. #include <string>
  17. #include <utility>
  18. BOOST_JSON_NS_BEGIN
  19. //----------------------------------------------------------
  20. //
  21. // Construction
  22. //
  23. //----------------------------------------------------------
  24. string::
  25. string(
  26. std::size_t count,
  27. char ch,
  28. storage_ptr sp)
  29. : sp_(std::move(sp))
  30. {
  31. assign(count, ch);
  32. }
  33. string::
  34. string(
  35. char const* s,
  36. storage_ptr sp)
  37. : sp_(std::move(sp))
  38. {
  39. assign(s);
  40. }
  41. string::
  42. string(
  43. char const* s,
  44. std::size_t count,
  45. storage_ptr sp)
  46. : sp_(std::move(sp))
  47. {
  48. assign(s, count);
  49. }
  50. string::
  51. string(string const& other)
  52. : sp_(other.sp_)
  53. {
  54. assign(other);
  55. }
  56. string::
  57. string(
  58. string const& other,
  59. storage_ptr sp)
  60. : sp_(std::move(sp))
  61. {
  62. assign(other);
  63. }
  64. string::
  65. string(
  66. string&& other,
  67. storage_ptr sp)
  68. : sp_(std::move(sp))
  69. {
  70. assign(std::move(other));
  71. }
  72. string::
  73. string(
  74. string_view s,
  75. storage_ptr sp)
  76. : sp_(std::move(sp))
  77. {
  78. assign(s);
  79. }
  80. //----------------------------------------------------------
  81. //
  82. // Assignment
  83. //
  84. //----------------------------------------------------------
  85. string&
  86. string::
  87. operator=(string const& other)
  88. {
  89. return assign(other);
  90. }
  91. string&
  92. string::
  93. operator=(string&& other)
  94. {
  95. return assign(std::move(other));
  96. }
  97. string&
  98. string::
  99. operator=(char const* s)
  100. {
  101. return assign(s);
  102. }
  103. string&
  104. string::
  105. operator=(string_view s)
  106. {
  107. return assign(s);
  108. }
  109. string&
  110. string::
  111. assign(
  112. size_type count,
  113. char ch)
  114. {
  115. std::char_traits<char>::assign(
  116. impl_.assign(count, sp_),
  117. count,
  118. ch);
  119. return *this;
  120. }
  121. string&
  122. string::
  123. assign(
  124. string const& other)
  125. {
  126. if(this == &other)
  127. return *this;
  128. return assign(
  129. other.data(),
  130. other.size());
  131. }
  132. string&
  133. string::
  134. assign(string&& other)
  135. {
  136. if(*sp_ == *other.sp_)
  137. {
  138. impl_.destroy(sp_);
  139. impl_ = other.impl_;
  140. ::new(&other.impl_) detail::string_impl();
  141. return *this;
  142. }
  143. // copy
  144. return assign(other);
  145. }
  146. string&
  147. string::
  148. assign(
  149. char const* s,
  150. size_type count)
  151. {
  152. std::char_traits<char>::copy(
  153. impl_.assign(count, sp_),
  154. s, count);
  155. return *this;
  156. }
  157. string&
  158. string::
  159. assign(
  160. char const* s)
  161. {
  162. return assign(s, std::char_traits<
  163. char>::length(s));
  164. }
  165. //----------------------------------------------------------
  166. //
  167. // Capacity
  168. //
  169. //----------------------------------------------------------
  170. void
  171. string::
  172. shrink_to_fit()
  173. {
  174. impl_.shrink_to_fit(sp_);
  175. }
  176. //----------------------------------------------------------
  177. //
  178. // Operations
  179. //
  180. //----------------------------------------------------------
  181. void
  182. string::
  183. clear() noexcept
  184. {
  185. impl_.term(0);
  186. }
  187. //----------------------------------------------------------
  188. void
  189. string::
  190. push_back(char ch)
  191. {
  192. *impl_.append(1, sp_) = ch;
  193. }
  194. void
  195. string::
  196. pop_back()
  197. {
  198. back() = 0;
  199. impl_.size(impl_.size() - 1);
  200. }
  201. //----------------------------------------------------------
  202. string&
  203. string::
  204. append(size_type count, char ch)
  205. {
  206. std::char_traits<char>::assign(
  207. impl_.append(count, sp_),
  208. count, ch);
  209. return *this;
  210. }
  211. string&
  212. string::
  213. append(string_view sv)
  214. {
  215. std::char_traits<char>::copy(
  216. impl_.append(sv.size(), sp_),
  217. sv.data(), sv.size());
  218. return *this;
  219. }
  220. //----------------------------------------------------------
  221. string&
  222. string::
  223. insert(
  224. size_type pos,
  225. string_view sv)
  226. {
  227. impl_.insert(pos, sv.data(), sv.size(), sp_);
  228. return *this;
  229. }
  230. string&
  231. string::
  232. insert(
  233. std::size_t pos,
  234. std::size_t count,
  235. char ch)
  236. {
  237. std::char_traits<char>::assign(
  238. impl_.insert_unchecked(pos, count, sp_),
  239. count, ch);
  240. return *this;
  241. }
  242. //----------------------------------------------------------
  243. string&
  244. string::
  245. replace(
  246. std::size_t pos,
  247. std::size_t count,
  248. string_view sv)
  249. {
  250. impl_.replace(pos, count, sv.data(), sv.size(), sp_);
  251. return *this;
  252. }
  253. string&
  254. string::
  255. replace(
  256. std::size_t pos,
  257. std::size_t count,
  258. std::size_t count2,
  259. char ch)
  260. {
  261. std::char_traits<char>::assign(
  262. impl_.replace_unchecked(pos, count, count2, sp_),
  263. count2, ch);
  264. return *this;
  265. }
  266. //----------------------------------------------------------
  267. string&
  268. string::
  269. erase(
  270. size_type pos,
  271. size_type count)
  272. {
  273. if(pos > impl_.size())
  274. detail::throw_out_of_range(
  275. BOOST_JSON_SOURCE_POS);
  276. if( count > impl_.size() - pos)
  277. count = impl_.size() - pos;
  278. std::char_traits<char>::move(
  279. impl_.data() + pos,
  280. impl_.data() + pos + count,
  281. impl_.size() - pos - count + 1);
  282. impl_.term(impl_.size() - count);
  283. return *this;
  284. }
  285. auto
  286. string::
  287. erase(const_iterator pos) ->
  288. iterator
  289. {
  290. return erase(pos, pos+1);
  291. }
  292. auto
  293. string::
  294. erase(
  295. const_iterator first,
  296. const_iterator last) ->
  297. iterator
  298. {
  299. auto const pos = first - begin();
  300. auto const count = last - first;
  301. erase(pos, count);
  302. return data() + pos;
  303. }
  304. //----------------------------------------------------------
  305. void
  306. string::
  307. resize(size_type count, char ch)
  308. {
  309. if(count <= impl_.size())
  310. {
  311. impl_.term(count);
  312. return;
  313. }
  314. reserve(count);
  315. std::char_traits<char>::assign(
  316. impl_.end(),
  317. count - impl_.size(),
  318. ch);
  319. grow(count - size());
  320. }
  321. //----------------------------------------------------------
  322. void
  323. string::
  324. swap(string& other)
  325. {
  326. BOOST_ASSERT(this != &other);
  327. if(*sp_ == *other.sp_)
  328. {
  329. std::swap(impl_, other.impl_);
  330. return;
  331. }
  332. string temp1(
  333. std::move(*this), other.sp_);
  334. string temp2(
  335. std::move(other), sp_);
  336. this->~string();
  337. ::new(this) string(pilfer(temp2));
  338. other.~string();
  339. ::new(&other) string(pilfer(temp1));
  340. }
  341. //----------------------------------------------------------
  342. void
  343. string::
  344. reserve_impl(size_type new_cap)
  345. {
  346. BOOST_ASSERT(
  347. new_cap >= impl_.capacity());
  348. if(new_cap > impl_.capacity())
  349. {
  350. // grow
  351. new_cap = detail::string_impl::growth(
  352. new_cap, impl_.capacity());
  353. detail::string_impl tmp(new_cap, sp_);
  354. std::char_traits<char>::copy(tmp.data(),
  355. impl_.data(), impl_.size() + 1);
  356. tmp.size(impl_.size());
  357. impl_.destroy(sp_);
  358. impl_ = tmp;
  359. return;
  360. }
  361. }
  362. BOOST_JSON_NS_END
  363. #endif