string.hpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/json
  9. //
  10. #ifndef BOOST_JSON_STRING_HPP
  11. #define BOOST_JSON_STRING_HPP
  12. #include <boost/json/detail/config.hpp>
  13. #include <boost/json/pilfer.hpp>
  14. #include <boost/json/storage_ptr.hpp>
  15. #include <boost/json/string_view.hpp>
  16. #include <boost/json/detail/digest.hpp>
  17. #include <boost/json/detail/except.hpp>
  18. #include <boost/json/detail/string_impl.hpp>
  19. #include <boost/json/detail/value.hpp>
  20. #include <boost/system/result.hpp>
  21. #include <cstring>
  22. #include <iosfwd>
  23. #include <iterator>
  24. #include <new>
  25. #include <type_traits>
  26. #include <utility>
  27. namespace boost {
  28. namespace json {
  29. class value;
  30. /** The native type of string values.
  31. Instances of string store and manipulate sequences of `char` using the
  32. UTF-8 encoding. The elements of a string are stored contiguously. A pointer
  33. to any character in a string may be passed to functions that expect
  34. a pointer to the first element of a null-terminated `char` array. The type
  35. uses small buffer optimisation to avoid allocations for small strings.
  36. String iterators are regular `char` pointers.
  37. @attention `string` member functions do not validate any UTF-8 byte sequences
  38. passed to them.
  39. @par Thread Safety
  40. Non-const member functions may not be called concurrently with any other
  41. member functions.
  42. @par Satisfies
  43. [_ContiguousContainer_](https://en.cppreference.com/w/cpp/named_req/ContiguousContainer),
  44. [_ReversibleContainer_](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer),
  45. and {req_SequenceContainer}.
  46. */
  47. class string
  48. {
  49. friend class value;
  50. #ifndef BOOST_JSON_DOCS
  51. // VFALCO doc toolchain shouldn't show this but does
  52. friend struct detail::access;
  53. #endif
  54. using string_impl = detail::string_impl;
  55. inline
  56. string(
  57. detail::key_t const&,
  58. string_view s,
  59. storage_ptr sp);
  60. inline
  61. string(
  62. detail::key_t const&,
  63. string_view s1,
  64. string_view s2,
  65. storage_ptr sp);
  66. public:
  67. /// Associated [Allocator](https://en.cppreference.com/w/cpp/named_req/Allocator)
  68. using allocator_type = container::pmr::polymorphic_allocator<value>;
  69. /// The type of a character
  70. using value_type = char;
  71. /// The type used to represent unsigned integers
  72. using size_type = std::size_t;
  73. /// The type used to represent signed integers
  74. using difference_type = std::ptrdiff_t;
  75. /// A pointer to an element
  76. using pointer = char*;
  77. /// A const pointer to an element
  78. using const_pointer = char const*;
  79. /// A reference to an element
  80. using reference = char&;
  81. /// A const reference to an element
  82. using const_reference = const char&;
  83. /// A random access iterator to an element
  84. using iterator = char*;
  85. /// A random access const iterator to an element
  86. using const_iterator = char const*;
  87. /// A reverse random access iterator to an element
  88. using reverse_iterator =
  89. std::reverse_iterator<iterator>;
  90. /// A reverse random access const iterator to an element
  91. using const_reverse_iterator =
  92. std::reverse_iterator<const_iterator>;
  93. /** A special index
  94. Represents the end of the string.
  95. */
  96. static constexpr std::size_t npos =
  97. string_view::npos;
  98. private:
  99. template<class T>
  100. using is_inputit = typename std::enable_if<
  101. std::is_convertible<typename
  102. std::iterator_traits<T>::reference,
  103. char>::value>::type;
  104. storage_ptr sp_; // must come first
  105. string_impl impl_;
  106. public:
  107. /** Destructor.
  108. Any dynamically allocated internal storage is freed.
  109. @par Complexity
  110. Constant.
  111. @par Exception Safety
  112. No-throw guarantee.
  113. */
  114. ~string() noexcept
  115. {
  116. impl_.destroy(sp_);
  117. }
  118. //------------------------------------------------------
  119. //
  120. // Construction
  121. //
  122. //------------------------------------------------------
  123. /** Constructors.
  124. Construct a string.
  125. @li **(1)**, **(2)** the string is empty with a non-zero,
  126. unspecified capacity.
  127. @li **(3)** the string is filled with `count` copies of character `ch`.
  128. @li **(4)** the string will contain a copy of the characters of `s`.
  129. @li **(5)** the string will contain a copy of the characters of the
  130. null-terminated string `s`.
  131. @li **(6)** the string will contain a copy of the characters in the
  132. range `[s, s + count)`.
  133. @li **(7)** the string will contain a copy of the characters in the
  134. range `[first, last)`.
  135. @li **(8)**, **(9)** the string contains a copy of the characters of
  136. `other`.
  137. @li **(10)** the string acquires ownership of the contents of `other`.
  138. @li **(11)** equivalent to **(10)** if `*sp == *other.storage()`;
  139. otherwise equivalent to **(9)**.
  140. @li **(12)** the string is acquires ownership of the contents of
  141. `other` using pilfer semantics. This is more efficient than move
  142. construction, when it is known that the moved-from object
  143. will be immediately destroyed afterwards.
  144. With **(2)**--**(7)**, **(9)**, **(11)** the constructed string uses
  145. memory resource of `sp`. With **(8)**, **(10)**, and **(12)** it uses
  146. `other`'s memory resource. In either case the string will share the
  147. ownership of the memory resource. With **(1)** it uses the
  148. \<\<default_memory_resource, default memory resource\>\>.
  149. After **(10)** `other` behaves as if newly constructed with its
  150. current storage pointer.
  151. After **(12)** `other` is not in a usable state and may only be
  152. destroyed.
  153. @par Constraints
  154. `InputIt` satisfies {req_InputIterator}.
  155. @par Complexity
  156. @li **(1)**, **(2)**, **(10)**, **(12)** constant.
  157. @li **(3)** linear in `count`.
  158. @li **(4)** linear in `s.size()`.
  159. @li **(5)** linear in `std::strlen(s)`.
  160. @li **(6)** linear in `count`.
  161. @li **(7)** linear in `std::distance(first, last)`.
  162. @li **(8)**, **(9)** linear in `other.size()`.
  163. @li **(11)** constant if `*sp == *other.storage()`; otherwise linear in
  164. `other.size()`.
  165. @par Exception Safety
  166. @li **(1)**, **(2)**, **(10)**, **(12)** no-throw guarantee.
  167. @li **(3)**--**(6)**, **(8)**, **(9)**, **(11)** strong guarantee.
  168. @li **(7)** strong guarantee if `InputIt` satisfies
  169. {req_ForwardIterator}, basic guarantee otherwise.
  170. Calls to `memory_resource::allocate` may throw.
  171. @throw boost::system::system_error The constructed string's size would
  172. have exceeded @ref max_size().
  173. @see @ref pilfer,
  174. [Valueless Variants Considered Harmful](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0308r0.html).
  175. @{
  176. */
  177. string() = default;
  178. /** Overload
  179. @param sp A pointer to the @ref boost::container::pmr::memory_resource
  180. to use. The container will acquire shared ownership of the memory
  181. resource.
  182. */
  183. explicit
  184. string(storage_ptr sp)
  185. : sp_(std::move(sp))
  186. {
  187. }
  188. /** Overload
  189. @param count The size of the resulting string.
  190. @param ch The value to initialize characters of the string with.
  191. @param sp
  192. */
  193. BOOST_JSON_DECL
  194. explicit
  195. string(
  196. std::size_t count,
  197. char ch,
  198. storage_ptr sp = {});
  199. /** Overload
  200. @param s The string to copy from.
  201. @param sp
  202. */
  203. BOOST_JSON_DECL
  204. string(
  205. string_view s,
  206. storage_ptr sp = {});
  207. /// Overload
  208. BOOST_JSON_DECL
  209. string(
  210. char const* s,
  211. storage_ptr sp = {});
  212. /// Overload
  213. BOOST_JSON_DECL
  214. explicit
  215. string(
  216. char const* s,
  217. std::size_t count,
  218. storage_ptr sp = {});
  219. /** Overload
  220. @tparam InputIt The type of the iterators.
  221. @param first An input iterator pointing to the first character to
  222. insert, or pointing to the end of the range.
  223. @param last An input iterator pointing to the end of the range.
  224. @param sp
  225. */
  226. template<class InputIt
  227. #ifndef BOOST_JSON_DOCS
  228. ,class = is_inputit<InputIt>
  229. #endif
  230. >
  231. explicit
  232. string(
  233. InputIt first,
  234. InputIt last,
  235. storage_ptr sp = {});
  236. /** Overload
  237. @param other The source string.
  238. */
  239. BOOST_JSON_DECL
  240. string(string const& other);
  241. /// Overload
  242. BOOST_JSON_DECL
  243. explicit
  244. string(
  245. string const& other,
  246. storage_ptr sp);
  247. /// Overload
  248. string(string&& other) noexcept
  249. : sp_(other.sp_)
  250. , impl_(other.impl_)
  251. {
  252. ::new(&other.impl_) string_impl();
  253. }
  254. /// Overload
  255. BOOST_JSON_DECL
  256. explicit
  257. string(
  258. string&& other,
  259. storage_ptr sp);
  260. /// Overload
  261. string(pilfered<string> other) noexcept
  262. : sp_(std::move(other.get().sp_))
  263. , impl_(other.get().impl_)
  264. {
  265. ::new(&other.get().impl_) string_impl();
  266. }
  267. /// @}
  268. //------------------------------------------------------
  269. //
  270. // Assignment
  271. //
  272. //------------------------------------------------------
  273. /** Assignment operators.
  274. @li **(1)**, **(4)** the contents are replaced with an element-wise
  275. copy of `other`.
  276. @li **(2)** takes ownership of `other`'s element storage if
  277. `*storage() == *other.storage()`; otherwise equivalent to **(1)**.
  278. @li **(3)** the contents are replaced with an element-wise copy of
  279. null-terminated string `s`.
  280. After **(2)**, the moved-from array behaves as if newly constructed
  281. with its current storage pointer.
  282. @par Complexity
  283. @li **(1)**, **(4)** linear in `other.size()`.
  284. @li **(2)** constant if `*storage() == *other.storage()`; otherwise
  285. linear in `other.size()`.
  286. @li **(3)** linear in `std::strlen(s)`.
  287. @par Exception Safety
  288. {sp} **(2)** provides strong guarantee if
  289. `*storage() != *other.storage()` and no-throw guarantee otherwise.
  290. Other overloads provide strong guarantee.
  291. Calls to `memory_resource::allocate` may throw.
  292. @param other The string to copy.
  293. @return `*this`
  294. @{
  295. */
  296. BOOST_JSON_DECL
  297. string&
  298. operator=(string const& other);
  299. BOOST_JSON_DECL
  300. string&
  301. operator=(string&& other);
  302. /** Overload
  303. @param s The null-terminated character string.
  304. @throw boost::system::system_error `std::strlen(s) >` @ref max_size().
  305. */
  306. BOOST_JSON_DECL
  307. string&
  308. operator=(char const* s);
  309. /** Overload
  310. @throw `boost::system::system_error` `other.size() >` @ref max_size().
  311. */
  312. BOOST_JSON_DECL
  313. string&
  314. operator=(string_view other);
  315. /// @}
  316. /** Assign characters to a string.
  317. @li **(1)** replaces the contents with `count` copies of character
  318. `ch`.
  319. @li **(2)** replaces the contents with copies of the characters in the
  320. range `[s, s + count)`. This range can contain null characters.
  321. @li **(3)** replaces the contents with those of the null terminated
  322. string `s`. The length of the string is determined by the first null
  323. character.
  324. @li **(4)** replaces the contents with copies of characters in the
  325. range `[first, last)`.
  326. @li **(5)** Replaces the contents with those of string view `s`. This
  327. view can contain null characters.
  328. @li **(6)** replaces the contents with a copy of the characters of
  329. `other`.
  330. @li **(7)** if `*storage() == *other.storage()` takes ownership of the
  331. element storage of `other`; otherwise equivalent to **(6)**.
  332. Self-assignment using **(7)** does nothing.
  333. After **(7)** `other` is left in valid but unspecified state.
  334. @par Constraints
  335. `InputIt` satisfies {req_InputIterator}.
  336. @par Complexity
  337. @li **(1)**, **(2)** linear in `count`.
  338. @li **(3)** linear in `std::strlen(s)`.
  339. @li **(4)** linear in `std::distance(first, last)`.
  340. @li **(5)** linear in `s.size()`.
  341. @li **(6)** linear in `other.size()`.
  342. @li **(7)** constant if `*storage() == *other.storage()`, otherwise
  343. linear in `other.size()`.
  344. @par Exception Safety
  345. {sp} **(7)** provides strong guarantee if
  346. `*storage() != *other.storage()` and no-throw guarantee otherwise.
  347. Other overloads provide strong guarantee. Calls to
  348. `memory_resource::allocate` may throw.
  349. @return `*this`.
  350. @param count The number of the characters to use.
  351. @param ch The character to fill the string with.
  352. @throw boost::system::system_error The size of the string after the
  353. operation would exceed @ref max_size().
  354. @{
  355. */
  356. BOOST_JSON_DECL
  357. string&
  358. assign(
  359. std::size_t count,
  360. char ch);
  361. /** Overload
  362. @param s A pointer to a character string used to copy from.
  363. @param count
  364. */
  365. BOOST_JSON_DECL
  366. string&
  367. assign(
  368. char const* s,
  369. std::size_t count);
  370. /** Overload
  371. @param s
  372. */
  373. BOOST_JSON_DECL
  374. string&
  375. assign(
  376. char const* s);
  377. /** Overload
  378. @tparam InputIt The type of the iterators.
  379. @param first An input iterator pointing to the first character to
  380. insert, or pointing to the end of the range.
  381. @param last An input iterator pointing to the end of the range.
  382. */
  383. template<class InputIt
  384. #ifndef BOOST_JSON_DOCS
  385. ,class = is_inputit<InputIt>
  386. #endif
  387. >
  388. string&
  389. assign(
  390. InputIt first,
  391. InputIt last);
  392. /** Overload
  393. @param s The string view to copy from.
  394. */
  395. string&
  396. assign(string_view s)
  397. {
  398. return assign(s.data(), s.size());
  399. }
  400. /** Overload
  401. @param other Another string.
  402. */
  403. BOOST_JSON_DECL
  404. string&
  405. assign(
  406. string const& other);
  407. /** Overload
  408. @param other
  409. */
  410. BOOST_JSON_DECL
  411. string&
  412. assign(string&& other);
  413. /// @}
  414. /** Return the associated memory resource.
  415. This function returns a smart pointer to the
  416. @ref boost::container::pmr::memory_resource used by the container.
  417. @par Complexity
  418. Constant.
  419. @par Exception Safety
  420. No-throw guarantee.
  421. */
  422. storage_ptr const&
  423. storage() const noexcept
  424. {
  425. return sp_;
  426. }
  427. /** Return the associated allocator.
  428. This function returns an instance of @ref allocator_type constructed
  429. from the associated @ref boost::container::pmr::memory_resource.
  430. @par Complexity
  431. Constant.
  432. @par Exception Safety
  433. No-throw guarantee.
  434. */
  435. allocator_type
  436. get_allocator() const noexcept
  437. {
  438. return sp_.get();
  439. }
  440. //------------------------------------------------------
  441. //
  442. // Element Access
  443. //
  444. //------------------------------------------------------
  445. /** Return a character with bounds checking.
  446. Returns @ref boost::system::result containing a reference to the
  447. character specified at location `pos`, if `pos` is within the range of
  448. the string. Otherwise the result contains an `error_code`.
  449. @par Exception Safety
  450. Strong guarantee.
  451. @param pos A zero-based index to access.
  452. @par Complexity
  453. Constant.
  454. @{
  455. */
  456. BOOST_JSON_DECL
  457. system::result<char&>
  458. try_at(std::size_t pos) noexcept;
  459. BOOST_JSON_DECL
  460. system::result<char const&>
  461. try_at(std::size_t pos) const noexcept;
  462. /// @}
  463. /** Return a character with bounds checking.
  464. Returns a reference to the character specified at location `pos`.
  465. @par Complexity
  466. Constant.
  467. @par Exception Safety
  468. Strong guarantee.
  469. @param pos A zero-based index to access.
  470. @param loc `source_location` to use in thrown exception; the source
  471. location of the call site by default.
  472. @throw boost::system::system_error `pos >=` @ref size().
  473. @{
  474. */
  475. inline
  476. char&
  477. at(
  478. std::size_t pos,
  479. source_location const& loc = BOOST_CURRENT_LOCATION);
  480. BOOST_JSON_DECL
  481. char const&
  482. at(
  483. std::size_t pos,
  484. source_location const& loc = BOOST_CURRENT_LOCATION) const;
  485. /// @}
  486. /** Return a character without bounds checking.
  487. Returns a reference to the character specified at location `pos`.
  488. @par Complexity
  489. Constant.
  490. @pre
  491. @code
  492. pos < size()
  493. @endcode
  494. @param pos A zero-based index to access.
  495. @{
  496. */
  497. char&
  498. operator[](std::size_t pos)
  499. {
  500. return impl_.data()[pos];
  501. }
  502. const char&
  503. operator[](std::size_t pos) const
  504. {
  505. return impl_.data()[pos];
  506. }
  507. /// @}
  508. /** Return the first character.
  509. Returns a reference to the first character.
  510. @pre
  511. @code
  512. ! empty()
  513. @endcode
  514. @par Complexity
  515. Constant.
  516. @par Exception Safety
  517. No-throw guarantee.
  518. @{
  519. */
  520. char&
  521. front()
  522. {
  523. return impl_.data()[0];
  524. }
  525. char const&
  526. front() const
  527. {
  528. return impl_.data()[0];
  529. }
  530. /// @}
  531. /** Return the last character.
  532. Returns a reference to the last character.
  533. @pre
  534. @code
  535. ! empty()
  536. @endcode
  537. @par Complexity
  538. Constant.
  539. @{
  540. */
  541. char&
  542. back()
  543. {
  544. return impl_.data()[impl_.size() - 1];
  545. }
  546. char const&
  547. back() const
  548. {
  549. return impl_.data()[impl_.size() - 1];
  550. }
  551. /// @}
  552. /** Return the underlying character array directly.
  553. Returns a pointer to the underlying array serving as storage. The value
  554. returned is such that the range `[data(), data() + size())` is always
  555. a valid range, even if the container is empty.
  556. @note The value returned from this function is never equal to
  557. `nullptr`.
  558. @par Complexity
  559. Constant.
  560. @par Exception Safety
  561. No-throw guarantee.
  562. @{
  563. */
  564. char*
  565. data() noexcept
  566. {
  567. return impl_.data();
  568. }
  569. char const*
  570. data() const noexcept
  571. {
  572. return impl_.data();
  573. }
  574. /// @@}
  575. /** Return the underlying character array directly.
  576. Returns a pointer to the underlying array serving as storage. The value
  577. returned is such that the range `[c_str(), c_str() + size())` is always
  578. a valid range, even if the container is empty.
  579. @note The value returned from this function is never equal to
  580. `nullptr`.
  581. @par Complexity
  582. Constant.
  583. */
  584. char const*
  585. c_str() const noexcept
  586. {
  587. return impl_.data();
  588. }
  589. /** Convert to a @ref string_view referring to the string.
  590. Returns a string view to the
  591. underlying character string. The size of the view
  592. does not include the null terminator.
  593. @par Complexity
  594. Constant.
  595. */
  596. operator string_view() const noexcept
  597. {
  598. return {data(), size()};
  599. }
  600. #if ! defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
  601. /** Convert to @ref std::string_view referring to the string.
  602. Returns a string view to the underlying character string. The size of
  603. the view does not include the null terminator.
  604. This overload is not defined when `BOOST_NO_CXX17_HDR_STRING_VIEW` is
  605. defined.
  606. @par Complexity
  607. Constant.
  608. */
  609. operator std::string_view() const noexcept
  610. {
  611. return {data(), size()};
  612. }
  613. #endif
  614. //------------------------------------------------------
  615. //
  616. // Iterators
  617. //
  618. //------------------------------------------------------
  619. /** Return an iterator to the beginning.
  620. If the container is empty, @ref end() is returned.
  621. @par Complexity
  622. Constant.
  623. @par Exception Safety
  624. No-throw guarantee.
  625. @{
  626. */
  627. iterator
  628. begin() noexcept
  629. {
  630. return impl_.data();
  631. }
  632. const_iterator
  633. begin() const noexcept
  634. {
  635. return impl_.data();
  636. }
  637. /// @}
  638. /** Return a const iterator to the first element.
  639. If the container is empty, @ref cend() is returned.
  640. @par Complexity
  641. Constant.
  642. @par Exception Safety
  643. No-throw guarantee.
  644. */
  645. const_iterator
  646. cbegin() const noexcept
  647. {
  648. return impl_.data();
  649. }
  650. /** Return an iterator to the end.
  651. The returned iterator only acts as a sentinel. Dereferencing it results
  652. in undefined behavior.
  653. @par Complexity
  654. Constant.
  655. @par Exception Safety
  656. No-throw guarantee.
  657. @{
  658. */
  659. iterator
  660. end() noexcept
  661. {
  662. return impl_.end();
  663. }
  664. const_iterator
  665. end() const noexcept
  666. {
  667. return impl_.end();
  668. }
  669. /// @}
  670. /** Return a const iterator past the last element.
  671. The returned iterator only acts as a sentinel. Dereferencing it results
  672. in undefined behavior.
  673. @par Complexity
  674. Constant.
  675. @par Exception Safety
  676. No-throw guarantee.
  677. */
  678. const_iterator
  679. cend() const noexcept
  680. {
  681. return impl_.end();
  682. }
  683. /** Return a reverse iterator to the first character of the reversed container.
  684. Returns the pointed-to character that corresponds to the last character
  685. of the non-reversed container. If the container is empty, @ref rend()
  686. is returned.
  687. @par Complexity
  688. Constant.
  689. @{
  690. */
  691. reverse_iterator
  692. rbegin() noexcept
  693. {
  694. return reverse_iterator(impl_.end());
  695. }
  696. const_reverse_iterator
  697. rbegin() const noexcept
  698. {
  699. return const_reverse_iterator(impl_.end());
  700. }
  701. /// @}
  702. /** Return a const reverse iterator to the first element of the reversed container.
  703. Returns the pointed-to character that corresponds to the last character
  704. of the non-reversed container. If the container is empty, @ref crend()
  705. is returned.
  706. @par Complexity
  707. Constant.
  708. @par Exception Safety
  709. No-throw guarantee.
  710. */
  711. const_reverse_iterator
  712. crbegin() const noexcept
  713. {
  714. return const_reverse_iterator(impl_.end());
  715. }
  716. /** Return a reverse iterator to the character following the last character of the reversed container.
  717. The pointed-to element corresponds to the element preceding the first
  718. element of the non-reversed container. The returned iterator only acts
  719. as a sentinel. Dereferencing it results in undefined behavior.
  720. @par Complexity
  721. Constant.
  722. @par Exception Safety
  723. No-throw guarantee.
  724. @{
  725. */
  726. reverse_iterator
  727. rend() noexcept
  728. {
  729. return reverse_iterator(begin());
  730. }
  731. const_reverse_iterator
  732. rend() const noexcept
  733. {
  734. return const_reverse_iterator(begin());
  735. }
  736. /// @}
  737. /** Return a const reverse iterator to the character following the last character of the reversed container.
  738. The pointed-to character corresponds to the character preceding the
  739. first character of the non-reversed container. The returned iterator
  740. only acts as a sentinel. Dereferencing it results in undefined
  741. behavior.
  742. @par Complexity
  743. Constant.
  744. @par Exception Safety
  745. No-throw guarantee.
  746. */
  747. const_reverse_iterator
  748. crend() const noexcept
  749. {
  750. return const_reverse_iterator(begin());
  751. }
  752. //------------------------------------------------------
  753. //
  754. // Capacity
  755. //
  756. //------------------------------------------------------
  757. /** Check if the string has no characters.
  758. Returns `true` if there are no characters in the string, i.e. @ref
  759. size() returns 0.
  760. @par Complexity
  761. Constant.
  762. @par Exception Safety
  763. No-throw guarantee.
  764. */
  765. bool
  766. empty() const noexcept
  767. {
  768. return impl_.size() == 0;
  769. }
  770. /** Return the number of characters in the string.
  771. The value returned does not include the null terminator, which is
  772. always present.
  773. @par Complexity
  774. Constant.
  775. */
  776. std::size_t
  777. size() const noexcept
  778. {
  779. return impl_.size();
  780. }
  781. /** Return the maximum number of characters any string can hold.
  782. The maximum is an implementation-defined number. This value is
  783. a theoretical limit; at runtime, the actual maximum size may be less
  784. due to resource limits.
  785. @par Complexity
  786. Constant.
  787. */
  788. static
  789. constexpr
  790. std::size_t
  791. max_size() noexcept
  792. {
  793. return string_impl::max_size();
  794. }
  795. /** Return the number of characters that can be held in currently allocated memory.
  796. Returns the number of characters that the container has currently
  797. allocated space for. This number is never smaller than the value
  798. returned by @ref size().
  799. @par Complexity
  800. Constant.
  801. @par Exception Safety
  802. No-throw guarantee.
  803. */
  804. std::size_t
  805. capacity() const noexcept
  806. {
  807. return impl_.capacity();
  808. }
  809. /** Increase the capacity to at least a certain amount.
  810. This increases the capacity of the array to a value that is greater
  811. than or equal to `new_capacity`. If `new_capacity > `@ref capacity(),
  812. new memory is allocated. Otherwise, the call has no effect. The number
  813. of elements and therefore the @ref size() of the container is not
  814. changed.
  815. If new memory is allocated, all iterators including any past-the-end
  816. iterators, and all references to the elements are invalidated.
  817. Otherwise, no iterators or references are invalidated.
  818. @par Complexity
  819. At most, linear in @ref size().
  820. @par Exception Safety
  821. Strong guarantee. Calls to `memory_resource::allocate` may throw.
  822. @param new_capacity The new capacity of the array.
  823. @throw boost::system::system_error `new_capacity > `@ref max_size().
  824. */
  825. void
  826. reserve(std::size_t new_capacity)
  827. {
  828. if(new_capacity <= capacity())
  829. return;
  830. reserve_impl(new_capacity);
  831. }
  832. /** Request the removal of unused capacity.
  833. This performs a non-binding request to reduce @ref capacity() to
  834. @ref size(). The request may or may not be fulfilled.
  835. @note If reallocation occurs, all iterators including any past-the-end
  836. iterators, and all references to characters are invalidated. Otherwise,
  837. no iterators or references are invalidated.
  838. @par Complexity
  839. At most, linear in @ref size().
  840. */
  841. BOOST_JSON_DECL
  842. void
  843. shrink_to_fit();
  844. //------------------------------------------------------
  845. //
  846. // Operations
  847. //
  848. //------------------------------------------------------
  849. /** Clear the contents.
  850. Erases all characters from the string. After this call, @ref size()
  851. returns zero but @ref capacity() is unchanged. All references,
  852. pointers, or iterators referring to contained elements are invalidated.
  853. Any past-the-end iterators are also invalidated.
  854. @par Complexity
  855. Linear in @ref size().
  856. @par Exception Safety
  857. No-throw guarantee.
  858. */
  859. BOOST_JSON_DECL
  860. void
  861. clear() noexcept;
  862. /** Insert characters at the specified index.
  863. @li **(1)** inserts `sv`.
  864. @li **(2)** inserts `count` copies of `ch`.
  865. @li **(3)** inserts the character `ch`.
  866. @li **(4)** inserts characters from the range `[first, last)`.
  867. The first character is inserted at the index `pos`. All references,
  868. pointers, or iterators referring to contained elements are invalidated.
  869. Any past-the-end iterators are also invalidated.
  870. @par Constraints
  871. `InputIt` satisfies {req_InputIterator}.
  872. @pre
  873. `[first, last)` is a valid range.
  874. @par Exception Safety
  875. @li **(1)**--*(3)* strong guarantee.
  876. @li **(4)** strong guarantee if `InputIt` satisfies
  877. {req_ForwardIterator}, basic guarantee otherwise.
  878. @return `*this`
  879. @param pos The index to insert at.
  880. @param sv The `string_view` to insert.
  881. @throw boost::system::system_error The size of the string would exceed
  882. @ref max_size().
  883. @throw boost::system::system_error `pos > `@ref size().
  884. @{
  885. */
  886. BOOST_JSON_DECL
  887. string&
  888. insert(
  889. std::size_t pos,
  890. string_view sv);
  891. /** Overload
  892. @param count The number of characters to insert.
  893. @param ch The character to insert.
  894. @param pos
  895. */
  896. BOOST_JSON_DECL
  897. string&
  898. insert(
  899. std::size_t pos,
  900. std::size_t count,
  901. char ch);
  902. /** Overload
  903. @param pos
  904. @param ch
  905. */
  906. string&
  907. insert(
  908. size_type pos,
  909. char ch)
  910. {
  911. return insert(pos, 1, ch);
  912. }
  913. /** Overload
  914. @tparam InputIt The type of the iterators.
  915. @param first The beginning of the character range.
  916. @param last The end of the character range.
  917. @param pos
  918. */
  919. template<class InputIt
  920. #ifndef BOOST_JSON_DOCS
  921. ,class = is_inputit<InputIt>
  922. #endif
  923. >
  924. string&
  925. insert(
  926. size_type pos,
  927. InputIt first,
  928. InputIt last);
  929. /// @}
  930. /** Remove characters from the string.
  931. @li **(1)** removes at most `count` but not more than `size() - pos`
  932. characters starting at `index`.
  933. @li **(2)** removes the character at `pos`.
  934. @li **(3)** removes characters in the range `[first, last)`.
  935. All references, pointers, or iterators referring to contained elements
  936. are invalidated. Any past-the-end iterators are also invalidated.
  937. @pre
  938. `pos`, `first`, and `last` are iterators into this string. `first` and
  939. `last` form a valid range.
  940. @par Complexity
  941. @li **(1)** linear in `count`.
  942. @li **(2)** constant.
  943. @li **(3)** linear in `std::distance(first, last)`.
  944. @par Exception Safety
  945. Strong guarantee.
  946. @return
  947. @li **(1)** `*this`.
  948. @li **(2)** An iterator referring to the character immediately
  949. following the removed character, or @ref end() if one does not exist.
  950. @li **(3)** An iterator referring to the character `last` previously
  951. referred to, or @ref end() if one does not exist.
  952. @param index The index of the first character to remove.
  953. @param count The number of characters to remove. By default remove
  954. until the end of the string.
  955. @throw boost::system::system_error `pos >` @ref size().
  956. @{
  957. */
  958. BOOST_JSON_DECL
  959. string&
  960. erase(
  961. std::size_t index = 0,
  962. std::size_t count = npos);
  963. /** Overload
  964. @param pos An iterator referring to the character to erase.
  965. */
  966. BOOST_JSON_DECL
  967. iterator
  968. erase(const_iterator pos);
  969. /** Overload
  970. @param first An iterator representing the first character to erase.
  971. @param last An iterator one past the last character to erase.
  972. */
  973. BOOST_JSON_DECL
  974. iterator
  975. erase(
  976. const_iterator first,
  977. const_iterator last);
  978. /// @}
  979. //------------------------------------------------------
  980. /** Append a character.
  981. Appends a character to the end of the string.
  982. @par Exception Safety
  983. Strong guarantee.
  984. @param ch The character to append.
  985. @throw boost::system::system_error @ref size() `+ 1 > `@ref max_size().
  986. */
  987. BOOST_JSON_DECL
  988. void
  989. push_back(char ch);
  990. /** Remove the last character.
  991. Removes a character from the end of the string.
  992. @pre
  993. @code
  994. ! empty()
  995. @endcode
  996. */
  997. BOOST_JSON_DECL
  998. void
  999. pop_back();
  1000. //------------------------------------------------------
  1001. /** Append characters to the string.
  1002. @li **(1)** appends `count` copies of `ch`.
  1003. @li **(2)** appends copies of characters of `sv`, preserving order.
  1004. @li **(3)** appends characters from the range `[first, last)`,
  1005. preserving order.
  1006. @pre
  1007. `[first, last)` shall be a valid range.
  1008. @par Constraints
  1009. `InputIt` satisfies {req_InputIterator}.
  1010. @par Exception Safety
  1011. Strong guarantee.
  1012. @return `*this`.
  1013. @param count The number of characters to append.
  1014. @param ch The character to append.
  1015. @throw boost::system::system_error The size of the string after the
  1016. operation would exceed @ref max_size().
  1017. @{
  1018. */
  1019. BOOST_JSON_DECL
  1020. string&
  1021. append(
  1022. std::size_t count,
  1023. char ch);
  1024. /** Overload
  1025. @param sv The `string_view` to append.
  1026. */
  1027. BOOST_JSON_DECL
  1028. string&
  1029. append(string_view sv);
  1030. /** Overload
  1031. @tparam InputIt The type of the iterators.
  1032. @param first An iterator representing the first character to append.
  1033. @param last An iterator one past the last character to append.
  1034. */
  1035. template<class InputIt
  1036. #ifndef BOOST_JSON_DOCS
  1037. ,class = is_inputit<InputIt>
  1038. #endif
  1039. >
  1040. string&
  1041. append(InputIt first, InputIt last);
  1042. /// @}
  1043. /** Append characters to the string.
  1044. @li **(1)** appends `[sv.begin(), sv.end())`.
  1045. @li **(2)** appends `ch`.
  1046. @par Exception Safety
  1047. Strong guarantee.
  1048. @return `*this`
  1049. @param sv The `string_view` to append.
  1050. @throw boost::system::system_error The size of the string after the
  1051. operation would exceed @ref max_size().
  1052. @{
  1053. */
  1054. string&
  1055. operator+=(string_view sv)
  1056. {
  1057. return append(sv);
  1058. }
  1059. /** Overload
  1060. @param ch The character to append.
  1061. */
  1062. string&
  1063. operator+=(char ch)
  1064. {
  1065. push_back(ch);
  1066. return *this;
  1067. }
  1068. /// @}
  1069. //------------------------------------------------------
  1070. /** Compare a string with the string.
  1071. Let `comp` be `std::char_traits<char>::compare(data(), sv.data(),
  1072. std::min(size(), sv.size())`. If `comp != 0`, then the result is
  1073. `comp`. Otherwise, the result is `0` if `size() == sv.size()`, `-1` if
  1074. `size() < sv.size()`, and `1` otherwise.
  1075. @par Complexity
  1076. Linear.
  1077. @return The result of lexicographically comparing the characters of
  1078. `sv` and the string.
  1079. @param sv The `string_view` to compare.
  1080. */
  1081. int
  1082. compare(string_view sv) const noexcept
  1083. {
  1084. return subview().compare(sv);
  1085. }
  1086. //------------------------------------------------------
  1087. /** Return whether the string begins with another string.
  1088. @li **(1)** checks if the string begins with `s`.
  1089. @li **(2)** checks if the string begins with `ch`.
  1090. @par Complexity
  1091. @li **(1)** linear in `s.size()`.
  1092. @li **(2)** constant.
  1093. @param s The string to check for.
  1094. @{
  1095. */
  1096. bool
  1097. starts_with(string_view s) const noexcept
  1098. {
  1099. return subview(0, s.size()) == s;
  1100. }
  1101. /** Overload
  1102. @param ch The character to check for.
  1103. */
  1104. bool
  1105. starts_with(char ch) const noexcept
  1106. {
  1107. return ! empty() && front() == ch;
  1108. }
  1109. /// @}
  1110. /** Check if the string ends with given suffix.
  1111. @li **(1)** returns `true` if the string ends with `s`.
  1112. @li **(2)** returns `true` if the string ends with the character `ch`.
  1113. @par Complexity
  1114. @li **(1)** linear in `s`.
  1115. @li **(2)** constant.
  1116. @par Exception Safety
  1117. No-throw guarantee.
  1118. @param s The string to check for.
  1119. @{
  1120. */
  1121. bool
  1122. ends_with(string_view s) const noexcept
  1123. {
  1124. return size() >= s.size() &&
  1125. subview(size() - s.size()) == s;
  1126. }
  1127. /** Overload
  1128. @param ch The character to check for.
  1129. */
  1130. bool
  1131. ends_with(char ch) const noexcept
  1132. {
  1133. return ! empty() && back() == ch;
  1134. }
  1135. /// @}
  1136. /** Replace a substring with another string.
  1137. @li **(1)** replaces `std::min(count, size() - pos)` characters
  1138. starting at index `pos` with those of `sv`.
  1139. @li **(2)** replaces the characters in the range `[first, last)` with
  1140. those of `sv`.
  1141. @li **(3)** replaces the characters in the range `[first, last)` with
  1142. those of `[first2, last2)`.
  1143. @li **(4)** replaces `std::min(count, size() - pos)` characters
  1144. starting at index `pos` with `count2` copies of `ch`.
  1145. @li **(5)** replaces the characters in the range `[first, last)` with
  1146. `count2` copies of `ch`.
  1147. All references, pointers, or iterators referring to contained elements
  1148. are invalidated. Any past-the-end iterators are also invalidated.
  1149. @pre
  1150. `[first, last)` is a valid range. `[first2, last2)` is a valid range.
  1151. @par Constraints
  1152. `InputIt` satisfies {req_InputIterator}.
  1153. @par Exception Safety
  1154. Strong guarantee.
  1155. @return `*this`
  1156. @param pos The index to replace at.
  1157. @param count The number of characters to replace.
  1158. @param sv The `string_view` to replace with.
  1159. @throw boost::system::system_error The resulting string's size would
  1160. have exceeded @ref max_size().
  1161. @{
  1162. */
  1163. BOOST_JSON_DECL
  1164. string&
  1165. replace(
  1166. std::size_t pos,
  1167. std::size_t count,
  1168. string_view sv);
  1169. /** Overload
  1170. @param first An iterator referring to the first character to replace.
  1171. @param last An iterator one past the end of the last character to
  1172. replace.
  1173. @param sv
  1174. */
  1175. string&
  1176. replace(
  1177. const_iterator first,
  1178. const_iterator last,
  1179. string_view sv)
  1180. {
  1181. return replace(first - begin(), last - first, sv);
  1182. }
  1183. /** Overload
  1184. @tparam InputIt The type of the iterators.
  1185. @param first2 An iterator referring to the first character to replace
  1186. with.
  1187. @param last2 An iterator one past the end of the last character to
  1188. replace with.
  1189. @param first
  1190. @param last
  1191. */
  1192. template<class InputIt
  1193. #ifndef BOOST_JSON_DOCS
  1194. ,class = is_inputit<InputIt>
  1195. #endif
  1196. >
  1197. string&
  1198. replace(
  1199. const_iterator first,
  1200. const_iterator last,
  1201. InputIt first2,
  1202. InputIt last2);
  1203. /** Overload
  1204. @param count2 The number of characters to replace with.
  1205. @param ch The character to replace with.
  1206. @param pos
  1207. @param count
  1208. */
  1209. BOOST_JSON_DECL
  1210. string&
  1211. replace(
  1212. std::size_t pos,
  1213. std::size_t count,
  1214. std::size_t count2,
  1215. char ch);
  1216. /** Overload
  1217. @param first
  1218. @param last
  1219. @param count2
  1220. @param ch
  1221. */
  1222. string&
  1223. replace(
  1224. const_iterator first,
  1225. const_iterator last,
  1226. std::size_t count2,
  1227. char ch)
  1228. {
  1229. return replace(first - begin(), last - first, count2, ch);
  1230. }
  1231. /// @}
  1232. //------------------------------------------------------
  1233. /** Return a view.
  1234. @li **(1)** equivalent to `subview().substr(pos, count)`.
  1235. @li **(2)** equivalent to `string_view(data(), size())`.
  1236. @par Exception Safety
  1237. Strong guarantee.
  1238. @param pos The index of the first character of the substring.
  1239. @param count The length of the substring.
  1240. @throw boost::system::system_error `pos > ` @ref size().
  1241. */
  1242. string_view
  1243. subview(
  1244. std::size_t pos,
  1245. std::size_t count = npos) const
  1246. {
  1247. return subview().substr(pos, count);
  1248. }
  1249. /// Overload
  1250. string_view
  1251. subview() const noexcept
  1252. {
  1253. return string_view( data(), size() );
  1254. }
  1255. //------------------------------------------------------
  1256. /** Copy a substring to another string.
  1257. Copies `std::min(count, size() - pos)` characters starting at index
  1258. `pos` to the string pointed to by `dest`.
  1259. @attention This function doesn't put the null terminator after the
  1260. copied characters.
  1261. @return The number of characters copied.
  1262. @param count The number of characters to copy.
  1263. @param dest The string to copy to.
  1264. @param pos The index to begin copying from.
  1265. @throw boost::system::system_error `pos >` @ref max_size().
  1266. */
  1267. std::size_t
  1268. copy(
  1269. char* dest,
  1270. std::size_t count,
  1271. std::size_t pos = 0) const
  1272. {
  1273. return subview().copy(dest, count, pos);
  1274. }
  1275. //------------------------------------------------------
  1276. /** Change the size of the string.
  1277. Resizes the string to contain `count` characters. If
  1278. `count > `@ref size(), **(2)** appends copies of `ch` and **(1)**
  1279. appends ``'\0'``. Otherwise, `size()` is reduced to `count`.
  1280. @param count The size to resize the string to.
  1281. @throw boost::system::system_error `count > `@ref max_size().
  1282. @{
  1283. */
  1284. void
  1285. resize(std::size_t count)
  1286. {
  1287. resize(count, 0);
  1288. }
  1289. /** Overload
  1290. @param count
  1291. @param ch The characters to append if the size increases.
  1292. */
  1293. BOOST_JSON_DECL
  1294. void
  1295. resize(std::size_t count, char ch);
  1296. /// @}
  1297. /** Increase size without changing capacity.
  1298. This increases the size of the string by `n` characters, adjusting the
  1299. position of the terminating null character for the new size. The new
  1300. characters remain uninitialized. This function may be used to append
  1301. characters directly into the storage between @ref end() and @ref data()
  1302. ` + ` @ref capacity().
  1303. @pre
  1304. @code
  1305. count <= capacity() - size()
  1306. @endcode
  1307. @param n The amount to increase the size by.
  1308. */
  1309. void
  1310. grow(std::size_t n) noexcept
  1311. {
  1312. BOOST_ASSERT(
  1313. n <= impl_.capacity() - impl_.size());
  1314. impl_.term(impl_.size() + n);
  1315. }
  1316. /** Swap the contents.
  1317. Exchanges the contents of this string with another string. Ownership of
  1318. the respective @ref boost::container::pmr::memory_resource objects is
  1319. not transferred.
  1320. @li If `&other == this`, do nothing. Otherwise,
  1321. @li if `*other.storage() == *this->storage()`, ownership of the
  1322. underlying memory is swapped in constant time, with no possibility
  1323. of exceptions. All iterators and references remain valid.
  1324. Otherwise,
  1325. @li the contents are logically swapped by making copies, which can
  1326. throw. In this case all iterators and references are invalidated.
  1327. @par Complexity
  1328. Constant or linear in @ref size() `+ other.size()`.
  1329. @par Exception Safety
  1330. Strong guarantee. Calls to `memory_resource::allocate` may throw.
  1331. */
  1332. BOOST_JSON_DECL
  1333. void
  1334. swap(string& other);
  1335. /** Exchange the given values.
  1336. Exchanges the contents of the string `lhs` with another string `rhs`.
  1337. Ownership of the respective @ref boost::container::pmr::memory_resource
  1338. objects is not transferred.
  1339. @li If `&lhs == &rhs`, do nothing. Otherwise,
  1340. @li if `*lhs.storage() == *rhs.storage()`, ownership of the underlying
  1341. memory is swapped in constant time, with no possibility of
  1342. exceptions. All iterators and references remain valid. Otherwise,
  1343. @li the contents are logically swapped by making a copy, which can
  1344. throw. In this case all iterators and references are invalidated.
  1345. @par Effects
  1346. @code
  1347. lhs.swap( rhs );
  1348. @endcode
  1349. @par Complexity
  1350. Constant or linear in `lhs.size() + rhs.size()`.
  1351. @par Exception Safety
  1352. Strong guarantee.
  1353. Calls to `memory_resource::allocate` may throw.
  1354. @param lhs The string to exchange.
  1355. @param rhs The string to exchange.
  1356. @see @ref string::swap
  1357. */
  1358. friend
  1359. void
  1360. swap(string& lhs, string& rhs)
  1361. {
  1362. lhs.swap(rhs);
  1363. }
  1364. //------------------------------------------------------
  1365. //
  1366. // Search
  1367. //
  1368. //------------------------------------------------------
  1369. /** Find the first occurrence of characters within the string.
  1370. Search from `pos` onward for the first substring that is equal to the
  1371. first argument.
  1372. @li **(1)** searches for the presense of the substring equal to `sv`.
  1373. @li **(2)** searches for the presense of the substring consisting of
  1374. the character `ch`.
  1375. @par Complexity
  1376. Linear in @ref size().
  1377. @par Exception Safety
  1378. No-throw guarantee.
  1379. @return The index of the first character of the found substring, or
  1380. @ref npos if none was found.
  1381. @param sv The `string_view` to search for.
  1382. @param pos The index to start searching at.
  1383. @{
  1384. */
  1385. std::size_t
  1386. find(
  1387. string_view sv,
  1388. std::size_t pos = 0) const noexcept
  1389. {
  1390. return subview().find(sv, pos);
  1391. }
  1392. /** Overload
  1393. @param ch The character to search for.
  1394. @param pos
  1395. */
  1396. std::size_t
  1397. find(
  1398. char ch,
  1399. std::size_t pos = 0) const noexcept
  1400. {
  1401. return subview().find(ch, pos);
  1402. }
  1403. /// @}
  1404. /** Find the last occurrence of a string within the string.
  1405. @li **(1)** searches for the last substring equal to `sv`.
  1406. @li **(2)** searches for the last occurrence of `ch`.
  1407. Both functions search for substrings fully contained within `[begin(),
  1408. begin() + pos)`.
  1409. @par Complexity
  1410. Linear.
  1411. @return Index of the first character of the found substring or
  1412. @ref npos if none was found.
  1413. @param sv The string to search for.
  1414. @param pos The index to start searching at. By default searches from
  1415. the end of the string.
  1416. @{
  1417. */
  1418. std::size_t
  1419. rfind(
  1420. string_view sv,
  1421. std::size_t pos = npos) const noexcept
  1422. {
  1423. return subview().rfind(sv, pos);
  1424. }
  1425. /** Overload
  1426. @param ch The character to search for.
  1427. @param pos
  1428. */
  1429. std::size_t
  1430. rfind(
  1431. char ch,
  1432. std::size_t pos = npos) const noexcept
  1433. {
  1434. return subview().rfind(ch, pos);
  1435. }
  1436. /// @}
  1437. //------------------------------------------------------
  1438. /** Find the first character present in the specified string.
  1439. Search from `pos` onward for the first character in this string that is
  1440. equal to any of the characters of `sv`.
  1441. @par Complexity
  1442. Linear in @ref size() `+ sv.size()`.
  1443. @par Exception Safety
  1444. No-throw guarantee.
  1445. @return The index of the found character, or @ref npos if none exists.
  1446. @param sv The characters to search for.
  1447. @param pos The index to start searching at.
  1448. */
  1449. std::size_t
  1450. find_first_of(
  1451. string_view sv,
  1452. std::size_t pos = 0) const noexcept
  1453. {
  1454. return subview().find_first_of(sv, pos);
  1455. }
  1456. /** Find the first character missing from the specified string.
  1457. Search from `pos` onward for the first character in this string that is
  1458. not equal to any of the characters in the string provided as the first
  1459. argument.
  1460. @li **(1)** compares with the characters in `sv`.
  1461. @li **(2)** compares with the character `ch`.
  1462. @par Complexity
  1463. @li **(1)** linear in @ref size() `+ sv.size()`.
  1464. @li **(2)** linear in @ref size().
  1465. @par Exception Safety
  1466. No-throw guarantee.
  1467. @return The index of the found character, or @ref npos if none exists.
  1468. @param sv The characters to compare with.
  1469. @param pos The index to start searching at.
  1470. @{
  1471. */
  1472. std::size_t
  1473. find_first_not_of(
  1474. string_view sv,
  1475. std::size_t pos = 0) const noexcept
  1476. {
  1477. return subview().find_first_not_of(sv, pos);
  1478. }
  1479. /** Overload
  1480. @param ch The character to compare with.
  1481. @param pos
  1482. */
  1483. std::size_t
  1484. find_first_not_of(
  1485. char ch,
  1486. std::size_t pos = 0) const noexcept
  1487. {
  1488. return subview().find_first_not_of(ch, pos);
  1489. }
  1490. /// @}
  1491. /** Find the last character present in the specified string.
  1492. Search from `pos` backwards for the first character in this string that
  1493. is equal to any of the characters of `sv`. If `pos` is equal to @ref
  1494. npos (the default), search from the last character.
  1495. @par Complexity
  1496. Linear in @ref size() `+ sv.size()`.
  1497. @par Exception Safety
  1498. No-throw guarantee.
  1499. @return The index of the found character, or @ref npos if none exists.
  1500. @param sv The characters to search for.
  1501. @param pos The index to start searching at.
  1502. */
  1503. std::size_t
  1504. find_last_of(
  1505. string_view sv,
  1506. std::size_t pos = npos) const noexcept
  1507. {
  1508. return subview().find_last_of(sv, pos);
  1509. }
  1510. /** Find the last character missing from the specified string.
  1511. Search from `pos` backwards for the first character in this string that
  1512. is not equal to any of the characters in the string provided as the
  1513. first argument. If `pos` is equal to @ref npos (the default), search
  1514. from the last character.
  1515. @li **(1)** compares with the characters in `sv`.
  1516. @li **(2)** compares with the character `ch`.
  1517. @par Complexity
  1518. @li **(1)** linear in @ref size() `+ sv.size()`.
  1519. @li **(2)** linear in @ref size().
  1520. @par Exception Safety
  1521. No-throw guarantee.
  1522. @return The index of the found character, or @ref npos if none exists.
  1523. @param sv The characters to compare with.
  1524. @param pos The index to start searching at.
  1525. @{
  1526. */
  1527. std::size_t
  1528. find_last_not_of(
  1529. string_view sv,
  1530. std::size_t pos = npos) const noexcept
  1531. {
  1532. return subview().find_last_not_of(sv, pos);
  1533. }
  1534. /** Overload
  1535. @param ch The character to compare with.
  1536. @param pos
  1537. */
  1538. std::size_t
  1539. find_last_not_of(
  1540. char ch,
  1541. std::size_t pos = npos) const noexcept
  1542. {
  1543. return subview().find_last_not_of(ch, pos);
  1544. }
  1545. /// @}
  1546. /** Serialize a @ref string to an output stream.
  1547. This function serializes a `string` as JSON into the output stream.
  1548. @return Reference to `os`.
  1549. @par Complexity
  1550. Linear in the `str.size()`.
  1551. @par Exception Safety
  1552. Strong guarantee. Calls to `memory_resource::allocate` may throw.
  1553. @param os The output stream to serialize to.
  1554. @param str The value to serialize.
  1555. */
  1556. BOOST_JSON_DECL
  1557. friend
  1558. std::ostream&
  1559. operator<<(
  1560. std::ostream& os,
  1561. string const& str);
  1562. private:
  1563. class undo;
  1564. template<class It>
  1565. using iter_cat = typename
  1566. std::iterator_traits<It>::iterator_category;
  1567. template<class InputIt>
  1568. void
  1569. assign(InputIt first, InputIt last,
  1570. std::random_access_iterator_tag);
  1571. template<class InputIt>
  1572. void
  1573. assign(InputIt first, InputIt last,
  1574. std::input_iterator_tag);
  1575. template<class InputIt>
  1576. void
  1577. append(InputIt first, InputIt last,
  1578. std::random_access_iterator_tag);
  1579. template<class InputIt>
  1580. void
  1581. append(InputIt first, InputIt last,
  1582. std::input_iterator_tag);
  1583. BOOST_JSON_DECL
  1584. void
  1585. reserve_impl(std::size_t new_capacity);
  1586. };
  1587. //----------------------------------------------------------
  1588. namespace detail
  1589. {
  1590. template <>
  1591. inline
  1592. string_view
  1593. to_string_view<string>(string const& s) noexcept
  1594. {
  1595. return s.subview();
  1596. }
  1597. } // namespace detail
  1598. /** Checks if lhs equals rhs.
  1599. @li **(1)** A lexicographical comparison is used.
  1600. @li **(2)** equivalent to `lhs.get() == rhs.get()`.
  1601. @par Complexity
  1602. @li **(1)** linear in `lhs.size() + rhs.size()`.
  1603. @li **(2)** constant.
  1604. @par Exception Safety
  1605. No-throw guarantee.
  1606. */
  1607. #ifdef BOOST_JSON_DOCS
  1608. bool
  1609. operator==(string const& lhs, string const& rhs) noexcept
  1610. #else
  1611. template<class T, class U>
  1612. detail::string_comp_op_requirement<T, U>
  1613. operator==(T const& lhs, U const& rhs) noexcept
  1614. #endif
  1615. {
  1616. return detail::to_string_view(lhs) == detail::to_string_view(rhs);
  1617. }
  1618. /** Checks if lhs does not equal rhs.
  1619. @li **(1)** A lexicographical comparison is used.
  1620. @li **(2)** equivalent to `lhs.get() != rhs.get()`.
  1621. @par Complexity
  1622. @li **(1)** linear in `lhs.size() + rhs.size()`.
  1623. @li **(2)** constant.
  1624. @par Exception Safety
  1625. No-throw guarantee.
  1626. */
  1627. #ifdef BOOST_JSON_DOCS
  1628. bool
  1629. operator!=(string const& lhs, string const& rhs) noexcept
  1630. #else
  1631. template<class T, class U>
  1632. detail::string_comp_op_requirement<T, U>
  1633. operator!=(T const& lhs, U const& rhs) noexcept
  1634. #endif
  1635. {
  1636. return detail::to_string_view(lhs) != detail::to_string_view(rhs);
  1637. }
  1638. /** Check if lhs is less than rhs.
  1639. A lexicographical comparison is used.
  1640. @par Complexity
  1641. Linear in `lhs.size() + rhs.size()`.
  1642. @par Exception Safety
  1643. No-throw guarantee.
  1644. */
  1645. #ifdef BOOST_JSON_DOCS
  1646. bool
  1647. operator<(string const& lhs, string const& rhs) noexcept
  1648. #else
  1649. template<class T, class U>
  1650. detail::string_comp_op_requirement<T, U>
  1651. operator<(T const& lhs, U const& rhs) noexcept
  1652. #endif
  1653. {
  1654. return detail::to_string_view(lhs) < detail::to_string_view(rhs);
  1655. }
  1656. /** Check if lhs is less than or equal to rhs.
  1657. A lexicographical comparison is used.
  1658. @par Complexity
  1659. Linear in `lhs.size() + rhs.size()`.
  1660. @par Exception Safety
  1661. No-throw guarantee.
  1662. */
  1663. #ifdef BOOST_JSON_DOCS
  1664. bool
  1665. operator<=(string const& lhs, string const& rhs) noexcept
  1666. #else
  1667. template<class T, class U>
  1668. detail::string_comp_op_requirement<T, U>
  1669. operator<=(T const& lhs, U const& rhs) noexcept
  1670. #endif
  1671. {
  1672. return detail::to_string_view(lhs) <= detail::to_string_view(rhs);
  1673. }
  1674. /** Check if lhs is more than or equal to rhs.
  1675. A lexicographical comparison is used.
  1676. @par Complexity
  1677. Linear in `lhs.size() + rhs.size()`.
  1678. @par Exception Safety
  1679. No-throw guarantee.
  1680. */
  1681. #ifdef BOOST_JSON_DOCS
  1682. bool
  1683. operator>=(string const& lhs, string const& rhs) noexcept
  1684. #else
  1685. template<class T, class U>
  1686. detail::string_comp_op_requirement<T, U>
  1687. operator>=(T const& lhs, U const& rhs) noexcept
  1688. #endif
  1689. {
  1690. return detail::to_string_view(lhs) >= detail::to_string_view(rhs);
  1691. }
  1692. /** Check if lhs is greater than rhs.
  1693. A lexicographical comparison is used.
  1694. @par Complexity
  1695. Linear in `lhs.size() + rhs.size()`.
  1696. @par Exception Safety
  1697. No-throw guarantee.
  1698. */
  1699. #ifdef BOOST_JSON_DOCS
  1700. bool
  1701. operator>(string const& lhs, string const& rhs) noexcept
  1702. #else
  1703. template<class T, class U>
  1704. detail::string_comp_op_requirement<T, U>
  1705. operator>(T const& lhs, U const& rhs) noexcept
  1706. #endif
  1707. {
  1708. return detail::to_string_view(lhs) > detail::to_string_view(rhs);
  1709. }
  1710. } // namespace json
  1711. } // namespace boost
  1712. // std::hash specialization
  1713. #ifndef BOOST_JSON_DOCS
  1714. namespace std {
  1715. template<>
  1716. struct hash< ::boost::json::string >
  1717. {
  1718. BOOST_JSON_DECL
  1719. std::size_t
  1720. operator()( ::boost::json::string const& js ) const noexcept;
  1721. };
  1722. } // std
  1723. #endif
  1724. #include <boost/json/impl/string.hpp>
  1725. #endif