string.hpp 69 KB

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