string.hpp 72 KB

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