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