string.hpp 70 KB

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