string.ipp 7.1 KB

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