string.hpp 70 KB

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