string.ipp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. 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. // Access
  182. //
  183. //----------------------------------------------------------
  184. system::result<char&>
  185. string::try_at(std::size_t pos) noexcept
  186. {
  187. if( pos < size() )
  188. return impl_.data()[pos];
  189. system::error_code ec;
  190. BOOST_JSON_FAIL(ec, error::out_of_range);
  191. return ec;
  192. }
  193. system::result<char const&>
  194. string::try_at(std::size_t pos) const noexcept
  195. {
  196. if( pos < size() )
  197. return impl_.data()[pos];
  198. system::error_code ec;
  199. BOOST_JSON_FAIL(ec, error::out_of_range);
  200. return ec;
  201. }
  202. char const&
  203. string::at(std::size_t pos, source_location const& loc) const
  204. {
  205. return try_at(pos).value(loc);
  206. }
  207. //----------------------------------------------------------
  208. //
  209. // Operations
  210. //
  211. //----------------------------------------------------------
  212. void
  213. string::
  214. clear() noexcept
  215. {
  216. impl_.term(0);
  217. }
  218. //----------------------------------------------------------
  219. void
  220. string::
  221. push_back(char ch)
  222. {
  223. *impl_.append(1, sp_) = ch;
  224. }
  225. void
  226. string::
  227. pop_back()
  228. {
  229. back() = 0;
  230. impl_.size(impl_.size() - 1);
  231. }
  232. //----------------------------------------------------------
  233. string&
  234. string::
  235. append(size_type count, char ch)
  236. {
  237. std::char_traits<char>::assign(
  238. impl_.append(count, sp_),
  239. count, ch);
  240. return *this;
  241. }
  242. string&
  243. string::
  244. append(string_view sv)
  245. {
  246. std::char_traits<char>::copy(
  247. impl_.append(sv.size(), sp_),
  248. sv.data(), sv.size());
  249. return *this;
  250. }
  251. //----------------------------------------------------------
  252. string&
  253. string::
  254. insert(
  255. size_type pos,
  256. string_view sv)
  257. {
  258. impl_.insert(pos, sv.data(), sv.size(), sp_);
  259. return *this;
  260. }
  261. string&
  262. string::
  263. insert(
  264. std::size_t pos,
  265. std::size_t count,
  266. char ch)
  267. {
  268. std::char_traits<char>::assign(
  269. impl_.insert_unchecked(pos, count, sp_),
  270. count, ch);
  271. return *this;
  272. }
  273. //----------------------------------------------------------
  274. string&
  275. string::
  276. replace(
  277. std::size_t pos,
  278. std::size_t count,
  279. string_view sv)
  280. {
  281. impl_.replace(pos, count, sv.data(), sv.size(), sp_);
  282. return *this;
  283. }
  284. string&
  285. string::
  286. replace(
  287. std::size_t pos,
  288. std::size_t count,
  289. std::size_t count2,
  290. char ch)
  291. {
  292. std::char_traits<char>::assign(
  293. impl_.replace_unchecked(pos, count, count2, sp_),
  294. count2, ch);
  295. return *this;
  296. }
  297. //----------------------------------------------------------
  298. string&
  299. string::
  300. erase(
  301. size_type pos,
  302. size_type count)
  303. {
  304. if(pos > impl_.size())
  305. {
  306. BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
  307. detail::throw_system_error( error::out_of_range, &loc );
  308. }
  309. if( count > impl_.size() - pos)
  310. count = impl_.size() - pos;
  311. std::char_traits<char>::move(
  312. impl_.data() + pos,
  313. impl_.data() + pos + count,
  314. impl_.size() - pos - count + 1);
  315. impl_.term(impl_.size() - count);
  316. return *this;
  317. }
  318. auto
  319. string::
  320. erase(const_iterator pos) ->
  321. iterator
  322. {
  323. return erase(pos, pos+1);
  324. }
  325. auto
  326. string::
  327. erase(
  328. const_iterator first,
  329. const_iterator last) ->
  330. iterator
  331. {
  332. auto const pos = first - begin();
  333. auto const count = last - first;
  334. erase(pos, count);
  335. return data() + pos;
  336. }
  337. //----------------------------------------------------------
  338. void
  339. string::
  340. resize(size_type count, char ch)
  341. {
  342. if(count <= impl_.size())
  343. {
  344. impl_.term(count);
  345. return;
  346. }
  347. reserve(count);
  348. std::char_traits<char>::assign(
  349. impl_.end(),
  350. count - impl_.size(),
  351. ch);
  352. grow(count - size());
  353. }
  354. //----------------------------------------------------------
  355. void
  356. string::
  357. swap(string& other)
  358. {
  359. if(*sp_ == *other.sp_)
  360. {
  361. std::swap(impl_, other.impl_);
  362. return;
  363. }
  364. string temp1(
  365. std::move(*this), other.sp_);
  366. string temp2(
  367. std::move(other), sp_);
  368. this->~string();
  369. ::new(this) string(pilfer(temp2));
  370. other.~string();
  371. ::new(&other) string(pilfer(temp1));
  372. }
  373. //----------------------------------------------------------
  374. void
  375. string::
  376. reserve_impl(size_type new_cap)
  377. {
  378. BOOST_ASSERT(
  379. new_cap >= impl_.capacity());
  380. if(new_cap > impl_.capacity())
  381. {
  382. // grow
  383. new_cap = detail::string_impl::growth(
  384. new_cap, impl_.capacity());
  385. detail::string_impl tmp(new_cap, sp_);
  386. std::char_traits<char>::copy(tmp.data(),
  387. impl_.data(), impl_.size() + 1);
  388. tmp.size(impl_.size());
  389. impl_.destroy(sp_);
  390. impl_ = tmp;
  391. return;
  392. }
  393. }
  394. } // namespace json
  395. } // namespace boost
  396. //----------------------------------------------------------
  397. std::size_t
  398. std::hash< ::boost::json::string >::operator()(
  399. ::boost::json::string const& js ) const noexcept
  400. {
  401. return ::boost::hash< ::boost::json::string >()( js );
  402. }
  403. #endif