string.hpp 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_STRING_HPP
  11. #define BOOST_CONTAINER_STRING_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. #include <boost/container/container_fwd.hpp>
  21. // container
  22. #include <boost/container/allocator_traits.hpp>
  23. #include <boost/container/new_allocator.hpp> //new_allocator
  24. #include <boost/container/throw_exception.hpp>
  25. // container/detail
  26. #include <boost/container/detail/alloc_helpers.hpp>
  27. #include <boost/container/detail/allocator_version_traits.hpp>
  28. #include <boost/container/detail/allocation_type.hpp>
  29. #include <boost/container/detail/iterator.hpp>
  30. #include <boost/container/detail/iterators.hpp>
  31. #include <boost/container/detail/min_max.hpp>
  32. #include <boost/container/detail/mpl.hpp>
  33. #include <boost/container/detail/next_capacity.hpp>
  34. #include <boost/move/detail/to_raw_pointer.hpp>
  35. #include <boost/container/detail/version_type.hpp>
  36. #include <boost/container/detail/type_traits.hpp>
  37. #include <boost/container/detail/algorithm.hpp>
  38. #include <boost/container/detail/minimal_char_traits_header.hpp> // for char_traits
  39. //intrusive
  40. #include <boost/intrusive/pointer_traits.hpp>
  41. #include <boost/intrusive/detail/hash_combine.hpp>
  42. #include <boost/move/detail/launder.hpp>
  43. //move
  44. #include <boost/move/utility_core.hpp>
  45. #include <boost/move/adl_move_swap.hpp>
  46. #include <boost/move/traits.hpp>
  47. #include <iosfwd>
  48. #include <istream> //
  49. #include <ostream>
  50. #include <ios>
  51. #include <locale>
  52. #include <cstddef>
  53. #include <climits>
  54. //std
  55. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  56. #include <initializer_list> //for std::initializer_list
  57. #endif
  58. //GCC 12 has a regression for array-bounds warnings
  59. #if defined(BOOST_GCC) && (BOOST_GCC >= 120000) && (BOOST_GCC < 130000)
  60. #pragma GCC diagnostic push
  61. #pragma GCC diagnostic ignored "-Warray-bounds"
  62. #endif
  63. namespace boost {
  64. namespace container {
  65. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  66. namespace dtl {
  67. // ------------------------------------------------------------
  68. // Class basic_string_base.
  69. // basic_string_base is a helper class that makes it it easier to write
  70. // an exception-safe version of basic_string. The constructor allocates,
  71. // but does not initialize, a block of memory. The destructor
  72. // deallocates, but does not destroy elements within, a block of
  73. // memory. The destructor assumes that the memory either is the internal buffer,
  74. // or else points to a block of memory that was allocated using string_base's
  75. // allocator and whose size is this->m_storage.
  76. template <class Allocator>
  77. class basic_string_base
  78. {
  79. basic_string_base & operator=(const basic_string_base &);
  80. basic_string_base(const basic_string_base &);
  81. typedef Allocator allocator_type;
  82. public:
  83. typedef allocator_traits<allocator_type> allocator_traits_type;
  84. typedef allocator_type stored_allocator_type;
  85. typedef typename allocator_traits_type::pointer pointer;
  86. typedef typename allocator_traits_type::value_type value_type;
  87. typedef typename allocator_traits_type::size_type size_type;
  88. typedef typename allocator_traits_type::difference_type difference_type;
  89. typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
  90. inline basic_string_base()
  91. : members_()
  92. {}
  93. inline explicit basic_string_base(const allocator_type& a)
  94. : members_(a)
  95. {}
  96. inline explicit basic_string_base(BOOST_RV_REF(allocator_type) a)
  97. : members_(boost::move(a))
  98. {}
  99. inline basic_string_base(const allocator_type& a, size_type n)
  100. : members_(a)
  101. {
  102. this->allocate_initial_block(n);
  103. }
  104. inline explicit basic_string_base(size_type n)
  105. : members_()
  106. {
  107. this->allocate_initial_block(n);
  108. }
  109. inline ~basic_string_base()
  110. {
  111. if(!this->is_short()){
  112. this->deallocate(this->priv_long_addr(), this->priv_long_storage());
  113. }
  114. }
  115. private:
  116. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  117. #pragma GCC diagnostic push
  118. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  119. #endif
  120. //This is the structure controlling a long string
  121. struct long_t
  122. {
  123. size_type is_short : 1;
  124. size_type length : (sizeof(size_type)*CHAR_BIT - 1);
  125. size_type storage;
  126. pointer start;
  127. inline long_t()
  128. : is_short(0)
  129. {}
  130. inline long_t(size_type len, size_type stor, pointer ptr)
  131. : is_short(0), length(len), storage(stor), start(ptr)
  132. {}
  133. inline long_t(const long_t &other)
  134. {
  135. this->is_short = false;
  136. length = other.length;
  137. storage = other.storage;
  138. start = other.start;
  139. }
  140. inline long_t &operator= (const long_t &other)
  141. {
  142. length = other.length;
  143. storage = other.storage;
  144. start = other.start;
  145. return *this;
  146. }
  147. };
  148. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  149. #pragma GCC diagnostic pop
  150. #endif
  151. //This type is the first part of the structure controlling a short string
  152. //The "data" member stores
  153. struct short_header
  154. {
  155. unsigned char is_short : 1;
  156. unsigned char length : (CHAR_BIT - 1);
  157. };
  158. //This type has the same alignment and size as long_t but it's POD
  159. //so, unlike long_t, it can be placed in a union
  160. typedef typename dtl::aligned_storage
  161. <sizeof(long_t), dtl::alignment_of<long_t>::value>::type long_raw_t;
  162. protected:
  163. BOOST_STATIC_CONSTEXPR size_type MinInternalBufferChars = 8;
  164. BOOST_STATIC_CONSTEXPR size_type AlignmentOfValueType =
  165. alignment_of<value_type>::value;
  166. BOOST_STATIC_CONSTEXPR size_type ShortDataOffset = ((sizeof(short_header)-1)/AlignmentOfValueType+1)*AlignmentOfValueType;
  167. BOOST_STATIC_CONSTEXPR size_type ZeroCostInternalBufferChars =
  168. (sizeof(long_t) - ShortDataOffset)/sizeof(value_type);
  169. BOOST_STATIC_CONSTEXPR size_type UnalignedFinalInternalBufferChars =
  170. (ZeroCostInternalBufferChars > MinInternalBufferChars) ?
  171. ZeroCostInternalBufferChars : MinInternalBufferChars;
  172. struct short_t
  173. {
  174. short_header h;
  175. value_type data[UnalignedFinalInternalBufferChars];
  176. };
  177. union repr_t_size_t
  178. {
  179. long_raw_t r;
  180. short_t s;
  181. };
  182. union repr_t
  183. {
  184. long_raw_t r_aligner;
  185. short_t s_aligner;
  186. unsigned char data[sizeof(repr_t_size_t)];
  187. };
  188. struct members_holder
  189. : public allocator_type
  190. {
  191. inline void init()
  192. {
  193. short_t &s = *::new(&this->m_repr) short_t;
  194. s.h.is_short = 1;
  195. s.h.length = 0;
  196. }
  197. inline members_holder()
  198. : allocator_type()
  199. { this->init(); }
  200. template<class AllocatorConvertible>
  201. inline explicit members_holder(BOOST_FWD_REF(AllocatorConvertible) a)
  202. : allocator_type(boost::forward<AllocatorConvertible>(a))
  203. { this->init(); }
  204. inline const short_t *pshort_repr() const
  205. { return move_detail::launder_cast<const short_t*>(&m_repr); }
  206. inline const long_t *plong_repr() const
  207. { return move_detail::launder_cast<const long_t*>(&m_repr); }
  208. inline short_t *pshort_repr()
  209. { return move_detail::launder_cast<short_t*>(&m_repr); }
  210. inline long_t *plong_repr()
  211. { return move_detail::launder_cast<long_t*>(&m_repr); }
  212. repr_t m_repr;
  213. } members_;
  214. inline const allocator_type &alloc() const
  215. { return members_; }
  216. inline allocator_type &alloc()
  217. { return members_; }
  218. BOOST_STATIC_CONSTEXPR size_type InternalBufferChars = (sizeof(repr_t) - ShortDataOffset)/sizeof(value_type);
  219. private:
  220. BOOST_STATIC_CONSTEXPR size_type MinAllocation = InternalBufferChars*2;
  221. protected:
  222. inline bool is_short() const
  223. {
  224. //Access and copy (to avoid UB) the first byte of the union to know if the
  225. //active representation is short or long
  226. short_header hdr;
  227. BOOST_CONTAINER_STATIC_ASSERT((sizeof(short_header) == 1));
  228. *(unsigned char*)&hdr = *static_cast<const unsigned char*>(static_cast<const void *>(&this->members_.m_repr));
  229. return hdr.is_short != 0;
  230. }
  231. inline short_t *construct_short()
  232. {
  233. short_t *ps = ::new(&this->members_.m_repr) short_t;
  234. ps->h.is_short = 1;
  235. return ps;
  236. }
  237. inline void destroy_short()
  238. {
  239. BOOST_ASSERT(this->is_short());
  240. this->members_.pshort_repr()->~short_t();
  241. }
  242. short_t *assure_short()
  243. {
  244. if (!this->is_short()){
  245. this->destroy_long();
  246. return construct_short();
  247. }
  248. return this->members_.pshort_repr();
  249. }
  250. inline long_t *construct_long()
  251. {
  252. long_t *pl = ::new(&this->members_.m_repr) long_t;
  253. //is_short flag is written in the constructor
  254. return pl;
  255. }
  256. inline void destroy_long()
  257. {
  258. BOOST_ASSERT(!this->is_short());
  259. this->members_.plong_repr()->~long_t();
  260. }
  261. long_t *assure_long()
  262. {
  263. if (this->is_short()){
  264. this->destroy_short();
  265. return this->construct_long();
  266. }
  267. return this->members_.plong_repr();
  268. }
  269. protected:
  270. typedef dtl::integral_constant<unsigned,
  271. boost::container::dtl::version<allocator_type>::value> alloc_version;
  272. pointer allocation_command(allocation_type command,
  273. size_type limit_size,
  274. size_type &prefer_in_recvd_out_size,
  275. pointer &reuse)
  276. {
  277. if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){
  278. reuse = 0;
  279. command &= ~(expand_fwd | expand_bwd);
  280. }
  281. return dtl::allocator_version_traits<allocator_type>::allocation_command
  282. (this->alloc(), command, limit_size, prefer_in_recvd_out_size, reuse);
  283. }
  284. size_type next_capacity(size_type additional_objects) const
  285. {
  286. return growth_factor_100()
  287. ( this->priv_storage(), additional_objects, allocator_traits_type::max_size(this->alloc()));
  288. }
  289. void deallocate(pointer p, size_type n)
  290. {
  291. if (p && (n > InternalBufferChars))
  292. this->alloc().deallocate(p, n);
  293. }
  294. void destroy(pointer p, size_type n)
  295. {
  296. value_type *raw_p = boost::movelib::to_raw_pointer(p);
  297. for(; n--; ++raw_p){
  298. allocator_traits_type::destroy( this->alloc(), raw_p);
  299. }
  300. }
  301. inline void destroy(pointer p)
  302. {
  303. allocator_traits_type::destroy
  304. ( this->alloc()
  305. , boost::movelib::to_raw_pointer(p)
  306. );
  307. }
  308. void allocate_initial_block(size_type n)
  309. {
  310. if (n <= this->max_size()) {
  311. if(n > InternalBufferChars){
  312. size_type new_cap = this->next_capacity(n);
  313. pointer reuse = 0;
  314. pointer p = this->allocation_command(allocate_new, n, new_cap, reuse);
  315. BOOST_ASSERT(this->is_short());
  316. this->construct_long();
  317. this->priv_long_addr(p);
  318. this->priv_long_size(0);
  319. this->priv_storage(new_cap);
  320. }
  321. }
  322. else{
  323. throw_length_error("basic_string::allocate_initial_block max_size() exceeded");
  324. }
  325. }
  326. inline void deallocate_block()
  327. { this->deallocate(this->priv_addr(), this->priv_storage()); }
  328. inline size_type max_size() const
  329. { return allocator_traits_type::max_size(this->alloc()) - 1; }
  330. protected:
  331. inline size_type priv_capacity() const
  332. { return this->priv_storage() - 1; }
  333. inline pointer priv_short_addr() const
  334. { return pointer_traits::pointer_to(const_cast<value_type&>(this->members_.pshort_repr()->data[0])); }
  335. //GCC seems a bit confused about uninitialized accesses
  336. #if defined(BOOST_GCC) && (BOOST_GCC >= 40700)
  337. #pragma GCC diagnostic push
  338. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  339. #endif
  340. inline pointer priv_long_addr() const
  341. { return this->members_.plong_repr()->start; }
  342. inline pointer priv_addr() const
  343. {
  344. return this->is_short()
  345. ? priv_short_addr()
  346. : priv_long_addr()
  347. ;
  348. }
  349. inline pointer priv_end_addr() const
  350. {
  351. return this->is_short()
  352. ? this->priv_short_addr() + difference_type(this->priv_short_size())
  353. : this->priv_long_addr() + difference_type(this->priv_long_size())
  354. ;
  355. }
  356. inline void priv_long_addr(pointer addr)
  357. { this->members_.plong_repr()->start = addr; }
  358. inline size_type priv_storage() const
  359. { return this->is_short() ? priv_short_storage() : priv_long_storage(); }
  360. inline size_type priv_short_storage() const
  361. { return InternalBufferChars; }
  362. inline size_type priv_long_storage() const
  363. { return this->members_.plong_repr()->storage; }
  364. inline void priv_storage(size_type storage)
  365. {
  366. if(!this->is_short())
  367. this->priv_long_storage(storage);
  368. }
  369. inline void priv_long_storage(size_type storage)
  370. {
  371. this->members_.plong_repr()->storage = storage;
  372. }
  373. inline size_type priv_size() const
  374. { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); }
  375. inline size_type priv_short_size() const
  376. { return this->members_.pshort_repr()->h.length; }
  377. inline size_type priv_long_size() const
  378. { return this->members_.plong_repr()->length; }
  379. inline void priv_size(size_type sz)
  380. {
  381. if(this->is_short())
  382. this->priv_short_size(sz);
  383. else
  384. this->priv_long_size(sz);
  385. }
  386. inline void priv_short_size(size_type sz)
  387. {
  388. typedef unsigned char uchar_type;
  389. BOOST_STATIC_CONSTEXPR uchar_type mask = uchar_type(uchar_type(-1) >> 1U);
  390. BOOST_ASSERT( sz <= mask );
  391. //Make -Wconversion happy
  392. this->members_.pshort_repr()->h.length = uchar_type(uchar_type(sz) & mask);
  393. }
  394. inline void priv_long_size(size_type sz)
  395. {
  396. BOOST_STATIC_CONSTEXPR size_type mask = size_type(-1) >> 1U;
  397. BOOST_ASSERT( sz <= mask );
  398. //Make -Wconversion happy
  399. this->members_.plong_repr()->length = sz & mask;
  400. }
  401. #if defined(BOOST_GCC) && (BOOST_GCC >= 40700)
  402. #pragma GCC diagnostic pop
  403. #endif
  404. void swap_data(basic_string_base& other)
  405. {
  406. if(this->is_short()){
  407. if(other.is_short()){
  408. repr_t tmp(this->members_.m_repr);
  409. this->members_.m_repr = other.members_.m_repr;
  410. other.members_.m_repr = tmp;
  411. }
  412. else{
  413. short_t short_backup(*this->members_.pshort_repr());
  414. this->members_.pshort_repr()->~short_t();
  415. ::new(this->members_.plong_repr()) long_t(*other.members_.plong_repr());
  416. other.members_.plong_repr()->~long_t();
  417. ::new(other.members_.pshort_repr()) short_t(short_backup);
  418. }
  419. }
  420. else{
  421. if(other.is_short()){
  422. short_t short_backup(*other.members_.pshort_repr());
  423. other.members_.pshort_repr()->~short_t();
  424. ::new(other.members_.plong_repr()) long_t(*this->members_.plong_repr());
  425. this->members_.plong_repr()->~long_t();
  426. ::new(this->members_.pshort_repr()) short_t(short_backup);
  427. }
  428. else{
  429. boost::adl_move_swap(*this->members_.plong_repr(), *other.members_.plong_repr());
  430. }
  431. }
  432. }
  433. };
  434. } //namespace dtl {
  435. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  436. //! The basic_string class represents a Sequence of characters. It contains all the
  437. //! usual operations of a Sequence, and, additionally, it contains standard string
  438. //! operations such as search and concatenation.
  439. //!
  440. //! The basic_string class is parameterized by character type, and by that type's
  441. //! Character Traits.
  442. //!
  443. //! This class has performance characteristics very much like vector<>, meaning,
  444. //! for example, that it does not perform reference-count or copy-on-write, and that
  445. //! concatenation of two strings is an O(N) operation.
  446. //!
  447. //! Some of basic_string's member functions use an unusual method of specifying positions
  448. //! and ranges. In addition to the conventional method using iterators, many of
  449. //! basic_string's member functions use a single value pos of type size_type to represent a
  450. //! position (in which case the position is begin() + pos, and many of basic_string's
  451. //! member functions use two values, pos and n, to represent a range. In that case pos is
  452. //! the beginning of the range and n is its size. That is, the range is
  453. //! [begin() + pos, begin() + pos + n).
  454. //!
  455. //! Note that the C++ standard does not specify the complexity of basic_string operations.
  456. //! In this implementation, basic_string has performance characteristics very similar to
  457. //! those of vector: access to a single character is O(1), while copy and concatenation
  458. //! are O(N).
  459. //!
  460. //! In this implementation, begin(),
  461. //! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators.
  462. //! In this implementation, iterators are only invalidated by member functions that
  463. //! explicitly change the string's contents.
  464. //!
  465. //! \tparam CharT The type of character it contains.
  466. //! \tparam Traits The Character Traits type, which encapsulates basic character operations
  467. //! \tparam Allocator The allocator, used for internal memory management.
  468. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  469. template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = void >
  470. #else
  471. template <class CharT, class Traits, class Allocator>
  472. #endif
  473. class basic_string
  474. : private dtl::basic_string_base<typename real_allocator<CharT, Allocator>::type>
  475. {
  476. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  477. private:
  478. BOOST_COPYABLE_AND_MOVABLE(basic_string)
  479. typedef dtl::basic_string_base<typename real_allocator<CharT, Allocator>::type> base_t;
  480. typedef typename base_t::allocator_traits_type allocator_traits_type;
  481. BOOST_STATIC_CONSTEXPR typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars;
  482. protected:
  483. // Allocator helper class to use a char_traits as a function object.
  484. template <class Tr>
  485. struct Eq_traits
  486. {
  487. //Compatibility with std::binary_function
  488. typedef typename Tr::char_type first_argument_type;
  489. typedef typename Tr::char_type second_argument_type;
  490. typedef bool result_type;
  491. bool operator()(const first_argument_type& x, const second_argument_type& y) const
  492. { return Tr::eq(x, y); }
  493. };
  494. template <class Tr>
  495. struct Not_within_traits
  496. {
  497. typedef typename Tr::char_type argument_type;
  498. typedef bool result_type;
  499. typedef const typename Tr::char_type* Pointer;
  500. const Pointer m_first;
  501. const Pointer m_last;
  502. Not_within_traits(Pointer f, Pointer l)
  503. : m_first(f), m_last(l) {}
  504. bool operator()(const typename Tr::char_type& x) const
  505. {
  506. return boost::container::find_if(m_first, m_last,
  507. boost::container::bind1st(Eq_traits<Tr>(), x)) == m_last;
  508. }
  509. };
  510. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  511. public:
  512. //////////////////////////////////////////////
  513. //
  514. // types
  515. //
  516. //////////////////////////////////////////////
  517. typedef Traits traits_type;
  518. typedef CharT value_type;
  519. typedef typename real_allocator<CharT, Allocator>::type allocator_type;
  520. typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
  521. typedef typename ::boost::container::allocator_traits<allocator_type>::const_pointer const_pointer;
  522. typedef typename ::boost::container::allocator_traits<allocator_type>::reference reference;
  523. typedef typename ::boost::container::allocator_traits<allocator_type>::const_reference const_reference;
  524. typedef typename ::boost::container::allocator_traits<allocator_type>::size_type size_type;
  525. typedef typename ::boost::container::allocator_traits<allocator_type>::difference_type difference_type;
  526. typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
  527. typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
  528. typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
  529. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
  530. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
  531. BOOST_STATIC_CONSTEXPR size_type npos = size_type(-1);
  532. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  533. private:
  534. //`allocator_type::value_type` must match container's `value type`. If this
  535. //assertion fails, please review your allocator definition.
  536. BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same<value_type, typename allocator_traits<allocator_type>::value_type>::value));
  537. typedef constant_iterator<CharT> cvalue_iterator;
  538. typedef typename base_t::alloc_version alloc_version;
  539. typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
  540. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  541. public: // Constructor, destructor, assignment.
  542. //////////////////////////////////////////////
  543. //
  544. // construct/copy/destroy
  545. //
  546. //////////////////////////////////////////////
  547. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  548. struct reserve_t {};
  549. basic_string(reserve_t, size_type n,
  550. const allocator_type& a = allocator_type())
  551. //Select allocator as in copy constructor as reserve_t-based constructors
  552. //are two step copies optimized for capacity
  553. : base_t( allocator_traits_type::select_on_container_copy_construction(a)
  554. , n + 1)
  555. { this->priv_terminate_string(); }
  556. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  557. //! <b>Effects</b>: Default constructs a basic_string.
  558. //!
  559. //! <b>Throws</b>: If allocator_type's default constructor throws.
  560. basic_string() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  561. : base_t()
  562. { this->priv_terminate_string(); }
  563. //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter.
  564. //!
  565. //! <b>Throws</b>: Nothing
  566. explicit basic_string(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
  567. : base_t(a)
  568. { this->priv_terminate_string(); }
  569. //! <b>Effects</b>: Copy constructs a basic_string.
  570. //!
  571. //! <b>Postcondition</b>: x == *this.
  572. //!
  573. //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
  574. basic_string(const basic_string& s)
  575. : base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc()))
  576. {
  577. this->priv_terminate_string();
  578. this->assign(s.begin(), s.end());
  579. }
  580. //! <b>Effects</b>: Same as basic_string(sv.data(), sv.size(), a).
  581. //!
  582. //! <b>Throws</b>: If allocator_type's default constructor or allocation throws.
  583. template<template <class, class> class BasicStringView>
  584. explicit basic_string(BasicStringView<CharT, Traits> sv, const allocator_type& a = allocator_type())
  585. : base_t(allocator_traits_type::select_on_container_copy_construction(a))
  586. {
  587. this->priv_terminate_string();
  588. this->assign(sv);
  589. }
  590. //! <b>Effects</b>: Move constructor. Moves s's resources to *this.
  591. //!
  592. //! <b>Throws</b>: Nothing.
  593. //!
  594. //! <b>Complexity</b>: Constant.
  595. basic_string(BOOST_RV_REF(basic_string) s) BOOST_NOEXCEPT_OR_NOTHROW
  596. : base_t(boost::move(s.alloc()))
  597. {
  598. if(s.alloc() == this->alloc()){
  599. this->swap_data(s);
  600. }
  601. else{
  602. this->assign(s.begin(), s.end());
  603. }
  604. }
  605. //! <b>Effects</b>: Copy constructs a basic_string using the specified allocator.
  606. //!
  607. //! <b>Postcondition</b>: x == *this.
  608. //!
  609. //! <b>Throws</b>: If allocation throws.
  610. basic_string(const basic_string& s, const allocator_type &a)
  611. : base_t(a)
  612. {
  613. this->priv_terminate_string();
  614. this->assign(s.begin(), s.end());
  615. }
  616. //! <b>Effects</b>: Move constructor using the specified allocator.
  617. //! Moves s's resources to *this.
  618. //!
  619. //! <b>Throws</b>: If allocation throws.
  620. //!
  621. //! <b>Complexity</b>: Constant if a == s.get_allocator(), linear otherwise.
  622. basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a)
  623. : base_t(a)
  624. {
  625. this->priv_terminate_string();
  626. if(s.alloc() == this->alloc()){
  627. this->swap_data(s);
  628. }
  629. else{
  630. this->assign(s.begin(), s.end());
  631. }
  632. }
  633. //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
  634. //! and is initialized by a specific number of characters of the s string.
  635. basic_string(const basic_string& s, size_type pos, size_type n = npos)
  636. : base_t()
  637. {
  638. this->priv_terminate_string();
  639. if (pos > s.size())
  640. throw_out_of_range("basic_string::basic_string out of range position");
  641. else
  642. this->assign
  643. (s.begin() + pos, s.begin() + pos + dtl::min_value(n, s.size() - pos));
  644. }
  645. //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
  646. //! and is initialized by a specific number of characters of the s string.
  647. basic_string(const basic_string& s, size_type pos, size_type n, const allocator_type& a)
  648. : base_t(a)
  649. {
  650. this->priv_terminate_string();
  651. if (pos > s.size())
  652. throw_out_of_range("basic_string::basic_string out of range position");
  653. else
  654. this->assign
  655. (s.begin() + pos, s.begin() + pos + dtl::min_value(n, s.size() - pos));
  656. }
  657. //! <b>Effects</b>: Constructs a basic_string taking a default-constructed allocator,
  658. //! and is initialized by a specific number of characters of the s c-string.
  659. basic_string(const CharT* s, size_type n)
  660. : base_t()
  661. {
  662. this->priv_terminate_string();
  663. this->assign(s, s + difference_type(n));
  664. }
  665. //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
  666. //! and is initialized by a specific number of characters of the s c-string.
  667. basic_string(const CharT* s, size_type n, const allocator_type& a)
  668. : base_t(a)
  669. {
  670. this->priv_terminate_string();
  671. this->assign(s, s + difference_type(n));
  672. }
  673. //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
  674. //! and is initialized by the null-terminated s c-string.
  675. basic_string(const CharT* s)
  676. : base_t()
  677. {
  678. this->priv_terminate_string();
  679. this->assign(s, s + Traits::length(s));
  680. }
  681. //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
  682. //! and is initialized by the null-terminated s c-string.
  683. basic_string(const CharT* s, const allocator_type& a)
  684. : base_t(a)
  685. {
  686. this->priv_terminate_string();
  687. this->assign(s, s + Traits::length(s));
  688. }
  689. //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
  690. //! and is initialized by n copies of c.
  691. basic_string(size_type n, CharT c)
  692. : base_t()
  693. {
  694. this->priv_terminate_string();
  695. this->assign(n, c);
  696. }
  697. //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
  698. //! and is initialized by n copies of c.
  699. basic_string(size_type n, CharT c, const allocator_type& a)
  700. : base_t(a)
  701. {
  702. this->priv_terminate_string();
  703. this->assign(n, c);
  704. }
  705. //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
  706. //! and is initialized by n default-initialized characters.
  707. basic_string(size_type n, default_init_t)
  708. : base_t(n + 1)
  709. {
  710. this->priv_size(n);
  711. this->priv_terminate_string();
  712. }
  713. //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
  714. //! and is initialized by n default-initialized characters.
  715. basic_string(size_type n, default_init_t, const allocator_type& a)
  716. : base_t(a, n + 1)
  717. {
  718. this->priv_size(n);
  719. this->priv_terminate_string();
  720. }
  721. //! <b>Effects</b>: Constructs a basic_string with a default-constructed allocator,
  722. //! and a range of iterators.
  723. template <class InputIterator>
  724. basic_string(InputIterator f, InputIterator l)
  725. : base_t()
  726. {
  727. this->priv_terminate_string();
  728. this->assign(f, l);
  729. }
  730. //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
  731. //! and a range of iterators.
  732. template <class InputIterator>
  733. basic_string(InputIterator f, InputIterator l, const allocator_type& a)
  734. : base_t(a)
  735. {
  736. this->priv_terminate_string();
  737. this->assign(f, l);
  738. }
  739. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  740. //! <b>Effects</b>: Same as basic_string(il.begin(), il.end(), a).
  741. //!
  742. basic_string(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
  743. : base_t(a)
  744. {
  745. this->priv_terminate_string();
  746. this->assign(il.begin(), il.end());
  747. }
  748. #endif
  749. //! <b>Effects</b>: Destroys the basic_string. All used memory is deallocated.
  750. //!
  751. //! <b>Throws</b>: Nothing.
  752. //!
  753. //! <b>Complexity</b>: Constant.
  754. ~basic_string() BOOST_NOEXCEPT_OR_NOTHROW
  755. {}
  756. //! <b>Effects</b>: Copy constructs a string.
  757. //!
  758. //! <b>Postcondition</b>: x == *this.
  759. //!
  760. //! <b>Complexity</b>: Linear to the elements x contains.
  761. basic_string& operator=(BOOST_COPY_ASSIGN_REF(basic_string) x)
  762. {
  763. if (BOOST_LIKELY(this != &x)) {
  764. allocator_type &this_alloc = this->alloc();
  765. const allocator_type &x_alloc = x.alloc();
  766. dtl::bool_<allocator_traits_type::
  767. propagate_on_container_copy_assignment::value> flag;
  768. if(flag && this_alloc != x_alloc){
  769. if(!this->is_short()){
  770. this->deallocate_block();
  771. this->assure_short();
  772. Traits::assign(*this->priv_addr(), CharT(0));
  773. this->priv_short_size(0);
  774. }
  775. }
  776. dtl::assign_alloc(this->alloc(), x.alloc(), flag);
  777. this->assign(x.begin(), x.end());
  778. }
  779. return *this;
  780. }
  781. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
  782. //!
  783. //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
  784. //! is false and allocation throws
  785. //!
  786. //! <b>Complexity</b>: Constant if allocator_traits_type::
  787. //! propagate_on_container_move_assignment is true or
  788. //! this->get>allocator() == x.get_allocator(). Linear otherwise.
  789. basic_string& operator=(BOOST_RV_REF(basic_string) x)
  790. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
  791. || allocator_traits_type::is_always_equal::value)
  792. {
  793. if (BOOST_LIKELY(this != &x)) {
  794. //We know resources can be transferred at comiple time if both allocators are
  795. //always equal or the allocator is going to be propagated
  796. const bool can_steal_resources_alloc
  797. = allocator_traits_type::propagate_on_container_move_assignment::value
  798. || allocator_traits_type::is_always_equal::value;
  799. dtl::bool_<can_steal_resources_alloc> flag;
  800. this->priv_move_assign(boost::move(x), flag);
  801. }
  802. return *this;
  803. }
  804. //! <b>Effects</b>: Assignment from a null-terminated c-string.
  805. //!
  806. basic_string& operator=(const CharT* s)
  807. { return this->assign(s, s + Traits::length(s)); }
  808. //! <b>Effects</b>: Returns *this = basic_string(1, c).
  809. //!
  810. basic_string& operator=(CharT c)
  811. { return this->assign(static_cast<size_type>(1), c); }
  812. //! <b>Effects</b>: Equivalent to return assign(sv).
  813. //!
  814. template<template <class, class> class BasicStringView>
  815. basic_string& operator=(BasicStringView<CharT, Traits> sv)
  816. { return this->assign(sv.data(), sv.size()); }
  817. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  818. //! <b>Effects</b>: Returns *this = basic_string(il);
  819. //!
  820. basic_string& operator=(std::initializer_list<CharT> il)
  821. {
  822. return this->assign(il.begin(), il.end());
  823. }
  824. #endif
  825. //! <b>Effects</b>: Returns a copy of the internal allocator.
  826. //!
  827. //! <b>Throws</b>: If allocator's copy constructor throws.
  828. //!
  829. //! <b>Complexity</b>: Constant.
  830. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  831. allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  832. { return this->alloc(); }
  833. //! <b>Effects</b>: Returns a reference to the internal allocator.
  834. //!
  835. //! <b>Throws</b>: Nothing
  836. //!
  837. //! <b>Complexity</b>: Constant.
  838. //!
  839. //! <b>Note</b>: Non-standard extension.
  840. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  841. stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  842. { return this->alloc(); }
  843. //! <b>Effects</b>: Returns a reference to the internal allocator.
  844. //!
  845. //! <b>Throws</b>: Nothing
  846. //!
  847. //! <b>Complexity</b>: Constant.
  848. //!
  849. //! <b>Note</b>: Non-standard extension.
  850. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  851. const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  852. { return this->alloc(); }
  853. //////////////////////////////////////////////
  854. //
  855. // iterators
  856. //
  857. //////////////////////////////////////////////
  858. //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
  859. //!
  860. //! <b>Throws</b>: Nothing.
  861. //!
  862. //! <b>Complexity</b>: Constant.
  863. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  864. iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
  865. { return this->priv_addr(); }
  866. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
  867. //!
  868. //! <b>Throws</b>: Nothing.
  869. //!
  870. //! <b>Complexity</b>: Constant.
  871. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  872. const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
  873. { return this->priv_addr(); }
  874. //! <b>Effects</b>: Returns an iterator to the end of the vector.
  875. //!
  876. //! <b>Throws</b>: Nothing.
  877. //!
  878. //! <b>Complexity</b>: Constant.
  879. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  880. iterator end() BOOST_NOEXCEPT_OR_NOTHROW
  881. { return this->priv_end_addr(); }
  882. //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
  883. //!
  884. //! <b>Throws</b>: Nothing.
  885. //!
  886. //! <b>Complexity</b>: Constant.
  887. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  888. const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
  889. { return this->priv_end_addr(); }
  890. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  891. //! of the reversed vector.
  892. //!
  893. //! <b>Throws</b>: Nothing.
  894. //!
  895. //! <b>Complexity</b>: Constant.
  896. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  897. reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
  898. { return reverse_iterator(this->priv_end_addr()); }
  899. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  900. //! of the reversed vector.
  901. //!
  902. //! <b>Throws</b>: Nothing.
  903. //!
  904. //! <b>Complexity</b>: Constant.
  905. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  906. const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  907. { return this->crbegin(); }
  908. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  909. //! of the reversed vector.
  910. //!
  911. //! <b>Throws</b>: Nothing.
  912. //!
  913. //! <b>Complexity</b>: Constant.
  914. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  915. reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
  916. { return reverse_iterator(this->priv_addr()); }
  917. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  918. //! of the reversed vector.
  919. //!
  920. //! <b>Throws</b>: Nothing.
  921. //!
  922. //! <b>Complexity</b>: Constant.
  923. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  924. const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
  925. { return this->crend(); }
  926. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
  927. //!
  928. //! <b>Throws</b>: Nothing.
  929. //!
  930. //! <b>Complexity</b>: Constant.
  931. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  932. const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  933. { return this->priv_addr(); }
  934. //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
  935. //!
  936. //! <b>Throws</b>: Nothing.
  937. //!
  938. //! <b>Complexity</b>: Constant.
  939. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  940. const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
  941. { return this->priv_end_addr(); }
  942. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  943. //! of the reversed vector.
  944. //!
  945. //! <b>Throws</b>: Nothing.
  946. //!
  947. //! <b>Complexity</b>: Constant.
  948. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  949. const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  950. { return const_reverse_iterator(this->priv_end_addr()); }
  951. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  952. //! of the reversed vector.
  953. //!
  954. //! <b>Throws</b>: Nothing.
  955. //!
  956. //! <b>Complexity</b>: Constant.
  957. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  958. const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
  959. { return const_reverse_iterator(this->priv_addr()); }
  960. //////////////////////////////////////////////
  961. //
  962. // capacity
  963. //
  964. //////////////////////////////////////////////
  965. //! <b>Effects</b>: Returns true if the vector contains no elements.
  966. //!
  967. //! <b>Throws</b>: Nothing.
  968. //!
  969. //! <b>Complexity</b>: Constant.
  970. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  971. bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
  972. { return !this->priv_size(); }
  973. //! <b>Effects</b>: Returns the number of the elements contained in the vector.
  974. //!
  975. //! <b>Throws</b>: Nothing.
  976. //!
  977. //! <b>Complexity</b>: Constant.
  978. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  979. size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
  980. { return this->priv_size(); }
  981. //! <b>Effects</b>: Returns the number of the elements contained in the vector.
  982. //!
  983. //! <b>Throws</b>: Nothing.
  984. //!
  985. //! <b>Complexity</b>: Constant.
  986. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  987. size_type length() const BOOST_NOEXCEPT_OR_NOTHROW
  988. { return this->size(); }
  989. //! <b>Effects</b>: Returns the largest possible size of the vector.
  990. //!
  991. //! <b>Throws</b>: Nothing.
  992. //!
  993. //! <b>Complexity</b>: Constant
  994. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  995. size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  996. { return base_t::max_size(); }
  997. //! <b>Effects</b>: Inserts or erases elements at the end such that
  998. //! the size becomes n. New elements are copy constructed from x.
  999. //!
  1000. //! <b>Throws</b>: If memory allocation throws
  1001. //!
  1002. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1003. void resize(size_type n, CharT c)
  1004. {
  1005. if (n <= this->size())
  1006. this->erase(this->begin() + difference_type(n), this->end());
  1007. else
  1008. this->append(n - this->size(), c);
  1009. }
  1010. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1011. //! the size becomes n. New elements are value initialized.
  1012. //!
  1013. //! <b>Throws</b>: If memory allocation throws
  1014. //!
  1015. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1016. void resize(size_type n)
  1017. { resize(n, CharT()); }
  1018. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1019. //! the size becomes n. New elements are uninitialized.
  1020. //!
  1021. //! <b>Throws</b>: If memory allocation throws
  1022. //!
  1023. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1024. //!
  1025. //! <b>Note</b>: Non-standard extension
  1026. void resize(size_type n, default_init_t)
  1027. {
  1028. if (n <= this->size())
  1029. this->erase(this->begin() + difference_type(n), this->end());
  1030. else{
  1031. this->priv_reserve_no_null_end(n);
  1032. this->priv_size(n);
  1033. this->priv_terminate_string();
  1034. }
  1035. }
  1036. //! <b>Effects</b>: Number of elements for which memory has been allocated.
  1037. //! capacity() is always greater than or equal to size().
  1038. //!
  1039. //! <b>Throws</b>: Nothing.
  1040. //!
  1041. //! <b>Complexity</b>: Constant.
  1042. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1043. size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
  1044. { return this->priv_capacity(); }
  1045. //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
  1046. //! effect. Otherwise, it is a request for allocation of additional memory.
  1047. //! If the request is successful, then capacity() is greater than or equal to
  1048. //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
  1049. //!
  1050. //! <b>Throws</b>: If memory allocation allocation throws
  1051. void reserve(size_type res_arg)
  1052. { this->priv_reserve(res_arg); }
  1053. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  1054. //! with previous allocations. The size of the string is unchanged
  1055. //!
  1056. //! <b>Throws</b>: Nothing
  1057. //!
  1058. //! <b>Complexity</b>: Linear to size().
  1059. void shrink_to_fit()
  1060. {
  1061. //Check if shrinking is possible
  1062. if(this->priv_storage() > InternalBufferChars){
  1063. //Check if we should pass from dynamically allocated buffer
  1064. //to the internal storage
  1065. if(this->priv_size() < InternalBufferChars){
  1066. //Dynamically allocated buffer attributes
  1067. pointer long_addr = this->priv_long_addr();
  1068. size_type long_storage = this->priv_long_storage();
  1069. size_type long_size = this->priv_long_size();
  1070. //Shrink from allocated buffer to the internal one, including trailing null
  1071. Traits::copy( boost::movelib::to_raw_pointer(this->priv_short_addr())
  1072. , boost::movelib::to_raw_pointer(long_addr)
  1073. , long_size+1);
  1074. BOOST_ASSERT(!this->is_short());
  1075. this->destroy_long();
  1076. this->construct_short();
  1077. this->alloc().deallocate(long_addr, long_storage);
  1078. }
  1079. else{
  1080. //Shrinking in dynamic buffer
  1081. this->priv_shrink_to_fit_dynamic_buffer(alloc_version());
  1082. }
  1083. }
  1084. }
  1085. //////////////////////////////////////////////
  1086. //
  1087. // element access
  1088. //
  1089. //////////////////////////////////////////////
  1090. //! <b>Requires</b>: !empty()
  1091. //!
  1092. //! <b>Effects</b>: Returns a reference to the first
  1093. //! element of the container.
  1094. //!
  1095. //! <b>Throws</b>: Nothing.
  1096. //!
  1097. //! <b>Complexity</b>: Constant.
  1098. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1099. reference front() BOOST_NOEXCEPT_OR_NOTHROW
  1100. {
  1101. BOOST_ASSERT(!this->empty());
  1102. return *this->priv_addr();
  1103. }
  1104. //! <b>Requires</b>: !empty()
  1105. //!
  1106. //! <b>Effects</b>: Returns a const reference to the first
  1107. //! element of the container.
  1108. //!
  1109. //! <b>Throws</b>: Nothing.
  1110. //!
  1111. //! <b>Complexity</b>: Constant.
  1112. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1113. const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
  1114. {
  1115. BOOST_ASSERT(!this->empty());
  1116. return *this->priv_addr();
  1117. }
  1118. //! <b>Requires</b>: !empty()
  1119. //!
  1120. //! <b>Effects</b>: Returns a reference to the last
  1121. //! element of the container.
  1122. //!
  1123. //! <b>Throws</b>: Nothing.
  1124. //!
  1125. //! <b>Complexity</b>: Constant.
  1126. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1127. reference back() BOOST_NOEXCEPT_OR_NOTHROW
  1128. {
  1129. BOOST_ASSERT(!this->empty());
  1130. return *(this->priv_addr() + (this->size() - 1u) );
  1131. }
  1132. //! <b>Requires</b>: !empty()
  1133. //!
  1134. //! <b>Effects</b>: Returns a const reference to the last
  1135. //! element of the container.
  1136. //!
  1137. //! <b>Throws</b>: Nothing.
  1138. //!
  1139. //! <b>Complexity</b>: Constant.
  1140. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1141. const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
  1142. {
  1143. BOOST_ASSERT(!this->empty());
  1144. return *(this->priv_addr() + (this->size() - 1u) );
  1145. }
  1146. //! <b>Requires</b>: size() > n.
  1147. //!
  1148. //! <b>Effects</b>: Returns a reference to the nth element
  1149. //! from the beginning of the container.
  1150. //!
  1151. //! <b>Throws</b>: Nothing.
  1152. //!
  1153. //! <b>Complexity</b>: Constant.
  1154. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1155. reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1156. {
  1157. BOOST_ASSERT(this->size() > n);
  1158. return *(this->priv_addr() + difference_type(n));
  1159. }
  1160. //! <b>Requires</b>: size() > n.
  1161. //!
  1162. //! <b>Effects</b>: Returns a const reference to the nth element
  1163. //! from the beginning of the container.
  1164. //!
  1165. //! <b>Throws</b>: Nothing.
  1166. //!
  1167. //! <b>Complexity</b>: Constant.
  1168. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1169. const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1170. {
  1171. BOOST_ASSERT(this->size() > n);
  1172. return *(this->priv_addr() + difference_type(n));
  1173. }
  1174. //! <b>Requires</b>: size() > n.
  1175. //!
  1176. //! <b>Effects</b>: Returns a reference to the nth element
  1177. //! from the beginning of the container.
  1178. //!
  1179. //! <b>Throws</b>: range_error if n >= size()
  1180. //!
  1181. //! <b>Complexity</b>: Constant.
  1182. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1183. reference at(size_type n)
  1184. {
  1185. if (n >= this->size())
  1186. throw_out_of_range("basic_string::at invalid subscript");
  1187. return *(this->priv_addr() + difference_type(n));
  1188. }
  1189. //! <b>Requires</b>: size() > n.
  1190. //!
  1191. //! <b>Effects</b>: Returns a const reference to the nth element
  1192. //! from the beginning of the container.
  1193. //!
  1194. //! <b>Throws</b>: range_error if n >= size()
  1195. //!
  1196. //! <b>Complexity</b>: Constant.
  1197. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1198. const_reference at(size_type n) const {
  1199. if (n >= this->size())
  1200. throw_out_of_range("basic_string::at invalid subscript");
  1201. return *(this->priv_addr() + difference_type(n));
  1202. }
  1203. //////////////////////////////////////////////
  1204. //
  1205. // modifiers
  1206. //
  1207. //////////////////////////////////////////////
  1208. //! <b>Effects</b>: Calls append(str.data, str.size()).
  1209. //!
  1210. //! <b>Returns</b>: *this
  1211. basic_string& operator+=(const basic_string& s)
  1212. { return this->append(s); }
  1213. //! <b>Effects</b>: Same as `return append(sv)`.
  1214. //!
  1215. template<template<class, class> class BasicStringView>
  1216. basic_string& operator+=(BasicStringView<CharT, Traits> sv)
  1217. {
  1218. return this->append(sv);
  1219. }
  1220. //! <b>Effects</b>: Calls append(s).
  1221. //!
  1222. //! <b>Returns</b>: *this
  1223. basic_string& operator+=(const CharT* s)
  1224. { return this->append(s); }
  1225. //! <b>Effects</b>: Calls append(1, c).
  1226. //!
  1227. //! <b>Returns</b>: *this
  1228. basic_string& operator+=(CharT c)
  1229. { this->push_back(c); return *this; }
  1230. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1231. //! <b>Effects</b>: Returns append(il)
  1232. //!
  1233. basic_string& operator+=(std::initializer_list<CharT> il)
  1234. {
  1235. return this->append(il);
  1236. }
  1237. #endif
  1238. //! <b>Effects</b>: Calls append(str.data(), str.size()).
  1239. //!
  1240. //! <b>Returns</b>: *this
  1241. basic_string& append(const basic_string& s)
  1242. { return this->append(s.begin(), s.end()); }
  1243. //! <b>Effects</b>: Same as return append(sv.data(), sv.size()).
  1244. //!
  1245. template<template<class, class> class BasicStringView>
  1246. basic_string& append(BasicStringView<CharT, Traits> sv)
  1247. { return this->append(sv.data(), sv.size()); }
  1248. //! <b>Requires</b>: pos <= str.size()
  1249. //!
  1250. //! <b>Effects</b>: Determines the effective length rlen of the string to append
  1251. //! as the smaller of n and str.size() - pos and calls append(str.data() + pos, rlen).
  1252. //!
  1253. //! <b>Throws</b>: If memory allocation throws and out_of_range if pos > str.size()
  1254. //!
  1255. //! <b>Returns</b>: *this
  1256. basic_string& append(const basic_string& s, size_type pos, size_type n = npos)
  1257. {
  1258. if (pos > s.size())
  1259. throw_out_of_range("basic_string::append out of range position");
  1260. return this->append(s.begin() + pos,
  1261. s.begin() + pos + dtl::min_value(n, s.size() - pos));
  1262. }
  1263. //! <b>Requires</b>: s points to an array of at least n elements of CharT.
  1264. //!
  1265. //! <b>Effects</b>: The function replaces the string controlled by *this with
  1266. //! a string of length size() + n whose irst size() elements are a copy of the
  1267. //! original string controlled by *this and whose remaining
  1268. //! elements are a copy of the initial n elements of s.
  1269. //!
  1270. //! <b>Throws</b>: If memory allocation throws length_error if size() + n > max_size().
  1271. //!
  1272. //! <b>Returns</b>: *this
  1273. basic_string& append(const CharT* s, size_type n)
  1274. { return this->append(s, s + difference_type(n)); }
  1275. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  1276. //!
  1277. //! <b>Effects</b>: Calls append(s, traits::length(s)).
  1278. //!
  1279. //! <b>Returns</b>: *this
  1280. basic_string& append(const CharT* s)
  1281. { return this->append(s, s + Traits::length(s)); }
  1282. //! <b>Effects</b>: Equivalent to append(basic_string(n, c)).
  1283. //!
  1284. //! <b>Returns</b>: *this
  1285. basic_string& append(size_type n, CharT c)
  1286. { return this->append(cvalue_iterator(c, n), cvalue_iterator()); }
  1287. //! <b>Requires</b>: [first,last) is a valid range.
  1288. //!
  1289. //! <b>Effects</b>: Equivalent to append(basic_string(first, last)).
  1290. //!
  1291. //! <b>Returns</b>: *this
  1292. template <class InputIter>
  1293. basic_string& append(InputIter first, InputIter last)
  1294. { this->insert(this->end(), first, last); return *this; }
  1295. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1296. //! <b>Effects</b>: Returns append(il.begin(), il.size()).
  1297. //!
  1298. basic_string& append(std::initializer_list<CharT> il)
  1299. {
  1300. return this->append(il.begin(), il.size());
  1301. }
  1302. #endif
  1303. //! <b>Effects</b>: Equivalent to append(static_cast<size_type>(1), c).
  1304. //!
  1305. void push_back(CharT c)
  1306. {
  1307. const size_type old_size = this->priv_size();
  1308. if (old_size < this->capacity()){
  1309. const pointer addr = this->priv_addr();
  1310. this->priv_construct_null(addr + difference_type(old_size + 1u));
  1311. Traits::assign(addr[difference_type(old_size)], c);
  1312. this->priv_size(old_size+1u);
  1313. }
  1314. else{
  1315. //No enough memory, insert a new object at the end
  1316. this->append(size_type(1), c);
  1317. }
  1318. }
  1319. //! <b>Effects</b>: Equivalent to assign(str, 0, npos).
  1320. //!
  1321. //! <b>Returns</b>: *this
  1322. basic_string& assign(const basic_string& s)
  1323. { return this->operator=(s); }
  1324. //! <b>Effects</b>: Equivalent to return assign(sv.data(), sv.size()).
  1325. //!
  1326. //! <b>Returns</b>: *this
  1327. template<template <class, class> class BasicStringView>
  1328. basic_string& assign(BasicStringView<CharT, Traits> sv)
  1329. { return this->operator=(sv); }
  1330. //! <b>Effects</b>: The function replaces the string controlled by *this
  1331. //! with a string of length str.size() whose elements are a copy of the string
  1332. //! controlled by str. Leaves str in a valid but unspecified state.
  1333. //!
  1334. //! <b>Throws</b>: Nothing
  1335. //!
  1336. //! <b>Returns</b>: *this
  1337. basic_string& assign(BOOST_RV_REF(basic_string) ms) BOOST_NOEXCEPT_OR_NOTHROW
  1338. { return this->swap_data(ms), *this; }
  1339. //! <b>Requires</b>: pos <= str.size()
  1340. //!
  1341. //! <b>Effects</b>: Determines the effective length rlen of the string to assign as
  1342. //! the smaller of n and str.size() - pos and calls assign(str.data() + pos rlen).
  1343. //!
  1344. //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > str.size().
  1345. //!
  1346. //! <b>Returns</b>: *this
  1347. basic_string& assign(const basic_string& s, size_type pos, size_type n)
  1348. {
  1349. if (pos > s.size())
  1350. throw_out_of_range("basic_string::assign out of range position");
  1351. return this->assign(s.begin() + pos,
  1352. s.begin() + pos + dtl::min_value(n, s.size() - pos));
  1353. }
  1354. //! <b>Requires</b>: s points to an array of at least n elements of CharT.
  1355. //!
  1356. //! <b>Effects</b>: Replaces the string controlled by *this with a string of
  1357. //! length n whose elements are a copy of those pointed to by s.
  1358. //!
  1359. //! <b>Throws</b>: If memory allocation throws or length_error if n > max_size().
  1360. //!
  1361. //! <b>Returns</b>: *this
  1362. basic_string& assign(const CharT* s, size_type n)
  1363. { return this->assign(s, s + difference_type(n)); }
  1364. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  1365. //!
  1366. //! <b>Effects</b>: Calls assign(s, traits::length(s)).
  1367. //!
  1368. //! <b>Returns</b>: *this
  1369. basic_string& assign(const CharT* s)
  1370. { return this->assign(s, s + Traits::length(s)); }
  1371. //! <b>Effects</b>: Equivalent to assign(basic_string(n, c)).
  1372. //!
  1373. //! <b>Returns</b>: *this
  1374. basic_string& assign(size_type n, CharT c)
  1375. { return this->assign(cvalue_iterator(c, n), cvalue_iterator()); }
  1376. //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
  1377. //!
  1378. //! <b>Returns</b>: *this
  1379. basic_string& assign(const CharT* first, const CharT* last)
  1380. {
  1381. size_type n = static_cast<size_type>(last - first);
  1382. this->reserve(n);
  1383. CharT* ptr = boost::movelib::to_raw_pointer(this->priv_addr());
  1384. Traits::copy(ptr, first, n);
  1385. this->priv_construct_null(ptr + difference_type(n));
  1386. this->priv_size(n);
  1387. return *this;
  1388. }
  1389. //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
  1390. //!
  1391. //! <b>Returns</b>: *this
  1392. template <class InputIter>
  1393. basic_string& assign(InputIter first, InputIter last
  1394. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1395. , typename dtl::disable_if_convertible<InputIter, size_type>::type * = 0
  1396. #endif
  1397. )
  1398. {
  1399. size_type cur = 0;
  1400. const pointer addr = this->priv_addr();
  1401. CharT *ptr = boost::movelib::to_raw_pointer(addr);
  1402. const size_type old_size = this->priv_size();
  1403. while (first != last && cur != old_size) {
  1404. Traits::assign(*ptr, *first);
  1405. ++first;
  1406. ++cur;
  1407. ++ptr;
  1408. }
  1409. if (first == last)
  1410. this->erase(addr + difference_type(cur), addr + difference_type(old_size));
  1411. else
  1412. this->append(first, last);
  1413. return *this;
  1414. }
  1415. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1416. //! <b>Effects</b>: Returns assign(il.begin(), il.size()).
  1417. //!
  1418. basic_string& assign(std::initializer_list<CharT> il)
  1419. {
  1420. return this->assign(il.begin(), il.size());
  1421. }
  1422. #endif
  1423. //! <b>Requires</b>: pos <= size().
  1424. //!
  1425. //! <b>Effects</b>: Calls insert(pos, str.data(), str.size()).
  1426. //!
  1427. //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
  1428. //!
  1429. //! <b>Returns</b>: *this
  1430. basic_string& insert(size_type pos, const basic_string& s)
  1431. {
  1432. const size_type sz = this->size();
  1433. if (pos > sz)
  1434. throw_out_of_range("basic_string::insert out of range position");
  1435. if (sz > this->max_size() - s.size())
  1436. throw_length_error("basic_string::insert max_size() exceeded");
  1437. this->insert(this->priv_addr() + pos, s.begin(), s.end());
  1438. return *this;
  1439. }
  1440. //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
  1441. //!
  1442. //! <b>Effects</b>: Determines the effective length rlen of the string to insert as
  1443. //! the smaller of n and str.size() - pos2 and calls insert(pos1, str.data() + pos2, rlen).
  1444. //!
  1445. //! <b>Throws</b>: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size().
  1446. //!
  1447. //! <b>Returns</b>: *this
  1448. basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n = npos)
  1449. {
  1450. const size_type sz = this->size();
  1451. const size_type str_size = s.size();
  1452. if (pos1 > sz || pos2 > str_size)
  1453. throw_out_of_range("basic_string::insert out of range position");
  1454. size_type len = dtl::min_value(n, str_size - pos2);
  1455. if (sz > this->max_size() - len)
  1456. throw_length_error("basic_string::insert max_size() exceeded");
  1457. const CharT *beg_ptr = boost::movelib::to_raw_pointer(s.begin()) + pos2;
  1458. const CharT *end_ptr = beg_ptr + len;
  1459. this->insert(this->priv_addr() + pos1, beg_ptr, end_ptr);
  1460. return *this;
  1461. }
  1462. //! <b>Requires</b>: s points to an array of at least n elements of CharT and pos <= size().
  1463. //!
  1464. //! <b>Effects</b>: Replaces the string controlled by *this with a string of length size() + n
  1465. //! whose first pos elements are a copy of the initial elements of the original string
  1466. //! controlled by *this and whose next n elements are a copy of the elements in s and whose
  1467. //! remaining elements are a copy of the remaining elements of the original string controlled by *this.
  1468. //!
  1469. //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size() or
  1470. //! length_error if size() + n > max_size().
  1471. //!
  1472. //! <b>Returns</b>: *this
  1473. basic_string& insert(size_type pos, const CharT* s, size_type n)
  1474. {
  1475. if (pos > this->size())
  1476. throw_out_of_range("basic_string::insert out of range position");
  1477. if (this->size() > this->max_size() - n)
  1478. throw_length_error("basic_string::insert max_size() exceeded");
  1479. this->insert(this->priv_addr() + pos, s, s + difference_type(n));
  1480. return *this;
  1481. }
  1482. //! <b>Requires</b>: pos <= size() and s points to an array of at least traits::length(s) + 1 elements of CharT
  1483. //!
  1484. //! <b>Effects</b>: Calls insert(pos, s, traits::length(s)).
  1485. //!
  1486. //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
  1487. //! length_error if size() > max_size() - Traits::length(s)
  1488. //!
  1489. //! <b>Returns</b>: *this
  1490. basic_string& insert(size_type pos, const CharT* s)
  1491. {
  1492. if (pos > this->size())
  1493. throw_out_of_range("basic_string::insert out of range position");
  1494. size_type len = Traits::length(s);
  1495. if (this->size() > this->max_size() - len)
  1496. throw_length_error("basic_string::insert max_size() exceeded");
  1497. this->insert(this->priv_addr() + pos, s, s + len);
  1498. return *this;
  1499. }
  1500. //! <b>Effects</b>: Equivalent to insert(pos, basic_string(n, c)).
  1501. //!
  1502. //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
  1503. //! length_error if size() > max_size() - n
  1504. //!
  1505. //! <b>Returns</b>: *this
  1506. basic_string& insert(size_type pos, size_type n, CharT c)
  1507. {
  1508. if (pos > this->size())
  1509. throw_out_of_range("basic_string::insert out of range position");
  1510. if (this->size() > this->max_size() - n)
  1511. throw_length_error("basic_string::insert max_size() exceeded");
  1512. this->insert(const_iterator(this->priv_addr() + pos), n, c);
  1513. return *this;
  1514. }
  1515. //! <b>Effects</b>: Same as `return insert(pos, sv.data(), sv.size())`.
  1516. //!
  1517. template<template<class, class> class BasicStringView>
  1518. basic_string& insert(size_type pos, BasicStringView<CharT, Traits> sv)
  1519. { return this->insert(pos, sv.data(), sv.size()); }
  1520. //! <b>Requires</b>: p is a valid iterator on *this.
  1521. //!
  1522. //! <b>Effects</b>: inserts a copy of c before the character referred to by p.
  1523. //!
  1524. //! <b>Returns</b>: An iterator which refers to the copy of the inserted character.
  1525. iterator insert(const_iterator p, CharT c)
  1526. {
  1527. size_type new_offset = size_type(p - this->priv_addr());
  1528. this->insert(p, cvalue_iterator(c, 1), cvalue_iterator());
  1529. return this->priv_addr() + new_offset;
  1530. }
  1531. //! <b>Requires</b>: p is a valid iterator on *this.
  1532. //!
  1533. //! <b>Effects</b>: Inserts n copies of c before the character referred to by p.
  1534. //!
  1535. //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
  1536. iterator insert(const_iterator p, size_type n, CharT c)
  1537. { return this->insert(p, cvalue_iterator(c, n), cvalue_iterator()); }
  1538. //! <b>Requires</b>: p is a valid iterator on *this. [first,last) is a valid range.
  1539. //!
  1540. //! <b>Effects</b>: Equivalent to insert(p - begin(), basic_string(first, last)).
  1541. //!
  1542. //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
  1543. template <class InputIter>
  1544. iterator insert(const_iterator p, InputIter first, InputIter last
  1545. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1546. , typename dtl::disable_if_or
  1547. < void
  1548. , dtl::is_convertible<InputIter, size_type>
  1549. , dtl::is_not_input_iterator<InputIter>
  1550. >::type * = 0
  1551. #endif
  1552. )
  1553. {
  1554. const size_type n_pos = p - this->cbegin();
  1555. for ( ; first != last; ++first, ++p) {
  1556. p = this->insert(p, *first);
  1557. }
  1558. return this->begin() + difference_type(n_pos);
  1559. }
  1560. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1561. template <class ForwardIter>
  1562. iterator insert(const_iterator p, ForwardIter first, ForwardIter last
  1563. , typename dtl::disable_if_or
  1564. < void
  1565. , dtl::is_convertible<ForwardIter, size_type>
  1566. , dtl::is_input_iterator<ForwardIter>
  1567. >::type * = 0
  1568. )
  1569. {
  1570. const size_type n_pos = size_type(p - this->cbegin());
  1571. if (first != last) {
  1572. const size_type n = boost::container::iterator_udistance(first, last);
  1573. const size_type old_size = this->priv_size();
  1574. const size_type remaining = this->capacity() - old_size;
  1575. const pointer old_start = this->priv_addr();
  1576. bool enough_capacity = false;
  1577. size_type new_cap = 0;
  1578. //Check if we have enough capacity
  1579. pointer hint = pointer();
  1580. pointer allocation_ret = pointer();
  1581. if (remaining >= n){
  1582. enough_capacity = true;
  1583. }
  1584. else {
  1585. //Otherwise expand current buffer or allocate new storage
  1586. new_cap = this->next_capacity(n);
  1587. hint = old_start;
  1588. allocation_ret = this->allocation_command
  1589. (allocate_new | expand_fwd | expand_bwd, old_size + n + 1u, new_cap, hint);
  1590. //Check forward expansion
  1591. if(old_start == allocation_ret){
  1592. enough_capacity = true;
  1593. this->priv_storage(new_cap);
  1594. }
  1595. }
  1596. //Reuse same buffer
  1597. if(enough_capacity){
  1598. const size_type elems_after = old_size - size_type(p - old_start);
  1599. const size_type old_length = old_size;
  1600. size_type new_size = 0;
  1601. if (elems_after >= n) {
  1602. const pointer pointer_past_last = old_start + difference_type(old_size + 1u);
  1603. priv_uninitialized_copy(old_start + difference_type(old_size - n + 1u),
  1604. pointer_past_last, pointer_past_last);
  1605. Traits::move(const_cast<CharT*>(boost::movelib::to_raw_pointer(p + difference_type(n))),
  1606. boost::movelib::to_raw_pointer(p),
  1607. (elems_after - n) + 1u);
  1608. (priv_copy)(first, last, const_cast<CharT*>(boost::movelib::to_raw_pointer(p)));
  1609. new_size = old_size + n;
  1610. }
  1611. else {
  1612. ForwardIter mid = first;
  1613. boost::container::iterator_uadvance(mid, elems_after + 1u);
  1614. priv_uninitialized_copy(mid, last, old_start + difference_type(old_size + 1u));
  1615. const size_type newer_size = old_size + (n - elems_after);
  1616. this->priv_size(newer_size);
  1617. priv_uninitialized_copy
  1618. (p, const_iterator(old_start + difference_type(old_length + 1u)),
  1619. old_start + difference_type(newer_size));
  1620. (priv_copy)(first, mid, const_cast<CharT*>(boost::movelib::to_raw_pointer(p)));
  1621. new_size = newer_size + elems_after;
  1622. }
  1623. this->priv_size(new_size);
  1624. this->priv_construct_null(old_start + difference_type(new_size));
  1625. }
  1626. else{
  1627. pointer new_start = allocation_ret;
  1628. if(!hint){
  1629. //Copy data to new buffer
  1630. size_type new_length = 0;
  1631. //This can't throw, since characters are POD
  1632. new_length += priv_uninitialized_copy
  1633. (const_iterator(old_start), p, new_start);
  1634. new_length += priv_uninitialized_copy
  1635. (first, last, new_start + difference_type(new_length));
  1636. new_length += priv_uninitialized_copy
  1637. (p, const_iterator(old_start + difference_type(old_size)),
  1638. new_start + difference_type(new_length));
  1639. this->priv_construct_null(new_start + difference_type(new_length));
  1640. this->deallocate_block();
  1641. this->assure_long();
  1642. this->priv_long_addr(new_start);
  1643. this->priv_long_size(new_length);
  1644. this->priv_long_storage(new_cap);
  1645. }
  1646. else{
  1647. //value_type is POD, so backwards expansion is much easier
  1648. //than with vector<T>
  1649. value_type * const oldbuf = boost::movelib::to_raw_pointer(old_start);
  1650. value_type * const newbuf = boost::movelib::to_raw_pointer(new_start);
  1651. const value_type *const pos = boost::movelib::to_raw_pointer(p);
  1652. const size_type before = size_type(pos - oldbuf);
  1653. //First move old data
  1654. Traits::move(newbuf, oldbuf, before);
  1655. Traits::move(newbuf + difference_type(before + n), pos, old_size - before);
  1656. //Now initialize the new data
  1657. priv_uninitialized_copy(first, last, new_start + difference_type(before));
  1658. this->priv_construct_null(new_start + difference_type(old_size + n));
  1659. this->assure_long();
  1660. this->priv_long_addr(new_start);
  1661. this->priv_long_size(old_size + n);
  1662. this->priv_long_storage(new_cap);
  1663. }
  1664. }
  1665. }
  1666. return this->begin() + difference_type(n_pos);
  1667. }
  1668. #endif
  1669. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1670. //! <b>Effects</b>: As if by insert(p, il.begin(), il.end()).
  1671. //!
  1672. //! <b>Returns</b>: An iterator which refers to the copy of the first inserted
  1673. //! character, or p if i1 is empty.
  1674. inline iterator insert(const_iterator p, std::initializer_list<CharT> il)
  1675. {
  1676. return this->insert(p, il.begin(), il.end());
  1677. }
  1678. #endif
  1679. //! <b>Effects</b>: Removes the last element from the container.
  1680. //!
  1681. //! <b>Throws</b>: Nothing.
  1682. //!
  1683. //! <b>Complexity</b>: Constant time.
  1684. void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
  1685. {
  1686. BOOST_ASSERT(!this->empty());
  1687. iterator p = this->end();
  1688. this->erase(--p);
  1689. }
  1690. //! <b>Requires</b>: pos <= size()
  1691. //!
  1692. //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos.
  1693. //! The function then replaces the string controlled by *this with a string of length size() - xlen
  1694. //! whose first pos elements are a copy of the initial elements of the original string controlled by *this,
  1695. //! and whose remaining elements are a copy of the elements of the original string controlled by *this
  1696. //! beginning at position pos + xlen.
  1697. //!
  1698. //! <b>Throws</b>: out_of_range if pos > size().
  1699. //!
  1700. //! <b>Returns</b>: *this
  1701. basic_string& erase(size_type pos = 0, size_type n = npos)
  1702. {
  1703. if (pos > this->size())
  1704. throw_out_of_range("basic_string::erase out of range position");
  1705. const pointer addr = this->priv_addr();
  1706. erase(addr + difference_type(pos), addr + difference_type(pos) + dtl::min_value(n, this->size() - pos));
  1707. return *this;
  1708. }
  1709. //! <b>Effects</b>: Removes the character referred to by p.
  1710. //!
  1711. //! <b>Throws</b>: Nothing
  1712. //!
  1713. //! <b>Returns</b>: An iterator which points to the element immediately following p prior to the element being
  1714. //! erased. If no such element exists, end() is returned.
  1715. iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1716. {
  1717. // The move includes the terminating null.
  1718. CharT * const ptr = const_cast<CharT*>(boost::movelib::to_raw_pointer(p));
  1719. const size_type old_size = this->priv_size();
  1720. Traits::move(ptr,
  1721. boost::movelib::to_raw_pointer(p + 1),
  1722. old_size - size_type(p - this->priv_addr()));
  1723. this->priv_size(old_size-1u);
  1724. return iterator(ptr);
  1725. }
  1726. //! <b>Requires</b>: first and last are valid iterators on *this, defining a range [first,last).
  1727. //!
  1728. //! <b>Effects</b>: Removes the characters in the range [first,last).
  1729. //!
  1730. //! <b>Throws</b>: Nothing
  1731. //!
  1732. //! <b>Returns</b>: An iterator which points to the element pointed to by last prior to
  1733. //! the other elements being erased. If no such element exists, end() is returned.
  1734. iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1735. {
  1736. CharT * f = const_cast<CharT*>(boost::movelib::to_raw_pointer(first));
  1737. if (first != last) { // The move includes the terminating null.
  1738. const size_type num_erased = size_type(last - first);
  1739. const size_type old_size = this->priv_size();
  1740. Traits::move(f,
  1741. boost::movelib::to_raw_pointer(last),
  1742. old_size + 1u - size_type(last - this->priv_addr()));
  1743. const size_type new_length = old_size - num_erased;
  1744. this->priv_size(new_length);
  1745. }
  1746. return iterator(f);
  1747. }
  1748. //! <b>Effects</b>: Erases all the elements of the vector.
  1749. //!
  1750. //! <b>Throws</b>: Nothing.
  1751. //!
  1752. //! <b>Complexity</b>: Linear to the number of elements in the vector.
  1753. void clear() BOOST_NOEXCEPT_OR_NOTHROW
  1754. {
  1755. if(this->is_short()) {
  1756. Traits::assign(*this->priv_short_addr(), CharT(0));
  1757. this->priv_short_size(0);
  1758. }
  1759. else {
  1760. Traits::assign(*this->priv_long_addr(), CharT(0));
  1761. this->priv_long_size(0);
  1762. }
  1763. }
  1764. //! <b>Requires</b>: pos1 <= size().
  1765. //!
  1766. //! <b>Effects</b>: Calls replace(pos1, n1, str.data(), str.size()).
  1767. //!
  1768. //! <b>Throws</b>: if memory allocation throws or out_of_range if pos1 > size().
  1769. //!
  1770. //! <b>Returns</b>: *this
  1771. basic_string& replace(size_type pos1, size_type n1, const basic_string& str)
  1772. {
  1773. if (pos1 > this->size())
  1774. throw_out_of_range("basic_string::replace out of range position");
  1775. const size_type len = dtl::min_value(n1, this->size() - pos1);
  1776. if (this->size() - len >= this->max_size() - str.size())
  1777. throw_length_error("basic_string::replace max_size() exceeded");
  1778. const pointer addr = this->priv_addr();
  1779. return this->replace( const_iterator(addr + difference_type(pos1))
  1780. , const_iterator(addr + difference_type(pos1 + len))
  1781. , str.begin(), str.end());
  1782. }
  1783. //! <b>Effects</b>: Calls `return replace(pos1, n1, sv.data(), sv.size());`.
  1784. //!
  1785. template<template<class, class> class BasicStringView>
  1786. inline basic_string& replace(size_type pos1, size_type n1, BasicStringView<CharT, Traits> sv)
  1787. {
  1788. return this->replace(pos1, n1, sv.data(), sv.size());
  1789. }
  1790. //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size().
  1791. //!
  1792. //! <b>Effects</b>: Determines the effective length rlen of the string to be
  1793. //! inserted as the smaller of n2 and str.size() - pos2 and calls
  1794. //! replace(pos1, n1, str.data() + pos2, rlen).
  1795. //!
  1796. //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or pos2 > str.size().
  1797. //!
  1798. //! <b>Returns</b>: *this
  1799. basic_string& replace(size_type pos1, size_type n1,
  1800. const basic_string& str, size_type pos2, size_type n2 = npos)
  1801. {
  1802. if (pos2 > str.size())
  1803. throw_out_of_range("basic_string::replace out of range position");
  1804. return this->replace(pos1, n1, str.data()+pos2, dtl::min_value(n2, str.size() - pos2));
  1805. }
  1806. //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > sv.size().
  1807. //!
  1808. //! <b>Effects</b>: Determines the effective length rlen of the string to be inserted as the
  1809. //! smaller of n2 and sv.size() - pos2 and calls `replace(pos1, n1, sv.data() + pos2, rlen)`.
  1810. //!
  1811. //! <b>Returns</b>: *this.
  1812. template<template<class, class> class BasicStringView>
  1813. basic_string& replace(size_type pos1, size_type n1, BasicStringView<CharT, Traits> sv,
  1814. size_type pos2, size_type n2 = npos)
  1815. {
  1816. if (pos2 > sv.size())
  1817. throw_out_of_range("basic_string::replace out of range position");
  1818. return this->replace(pos1, n1, sv.data()+pos2, dtl::min_value(n2, sv.size() - pos2));
  1819. }
  1820. //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
  1821. //!
  1822. //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the
  1823. //! smaller of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error.
  1824. //! Otherwise, the function replaces the string controlled by *this with a string of
  1825. //! length size() - xlen + n2 whose first pos1 elements are a copy of the initial elements
  1826. //! of the original string controlled by *this, whose next n2 elements are a copy of the
  1827. //! initial n2 elements of s, and whose remaining elements are a copy of the elements of
  1828. //! the original string controlled by *this beginning at position pos + xlen.
  1829. //!
  1830. //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
  1831. //! if the length of the resulting string would exceed max_size()
  1832. //!
  1833. //! <b>Returns</b>: *this
  1834. basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2)
  1835. {
  1836. if (pos1 > this->size())
  1837. throw_out_of_range("basic_string::replace out of range position");
  1838. const size_type len = dtl::min_value(n1, this->size() - pos1);
  1839. const size_type max_sz = this->max_size();
  1840. if (n2 > max_sz || (this->size() - len) >= (max_sz - n2))
  1841. throw_length_error("basic_string::replace max_size() exceeded");
  1842. const pointer addr = this->priv_addr() + pos1;
  1843. return this->replace(addr, addr + difference_type(len), s, s + difference_type(n2));
  1844. }
  1845. //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
  1846. //!
  1847. //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller
  1848. //! of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. Otherwise,
  1849. //! the function replaces the string controlled by *this with a string of length size() - xlen + n2
  1850. //! whose first pos1 elements are a copy of the initial elements of the original string controlled
  1851. //! by *this, whose next n2 elements are a copy of the initial n2 elements of s, and whose
  1852. //! remaining elements are a copy of the elements of the original string controlled by *this
  1853. //! beginning at position pos + xlen.
  1854. //!
  1855. //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
  1856. //! if the length of the resulting string would exceed max_size()
  1857. //!
  1858. //! <b>Returns</b>: *this
  1859. inline basic_string& replace(size_type pos, size_type n1, const CharT* s)
  1860. {
  1861. return this->replace(pos, n1, s, Traits::length(s));
  1862. }
  1863. //! <b>Requires</b>: pos1 <= size().
  1864. //!
  1865. //! <b>Effects</b>: Equivalent to replace(pos1, n1, basic_string(n2, c)).
  1866. //!
  1867. //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
  1868. //! if the length of the resulting string would exceed max_size()
  1869. //!
  1870. //! <b>Returns</b>: *this
  1871. basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c)
  1872. {
  1873. if (pos1 > this->size())
  1874. throw_out_of_range("basic_string::replace out of range position");
  1875. const size_type len = dtl::min_value(n1, this->size() - pos1);
  1876. if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
  1877. throw_length_error("basic_string::replace max_size() exceeded");
  1878. const pointer addr = this->priv_addr();
  1879. return this->replace(addr + difference_type(pos1), addr + difference_type(pos1 + len), n2, c);
  1880. }
  1881. //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
  1882. //!
  1883. //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, str).
  1884. //!
  1885. //! <b>Throws</b>: if memory allocation throws
  1886. //!
  1887. //! <b>Returns</b>: *this
  1888. inline basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str)
  1889. { return this->replace(i1, i2, str.data(), str.data()+str.size()); }
  1890. //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and
  1891. //! s points to an array of at least n elements
  1892. //!
  1893. //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, n).
  1894. //!
  1895. //! <b>Throws</b>: if memory allocation throws
  1896. //!
  1897. //! <b>Returns</b>: *this
  1898. inline basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s, size_type n)
  1899. { return this->replace(i1, i2, s, s + difference_type(n)); }
  1900. //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and s points to an
  1901. //! array of at least traits::length(s) + 1 elements of CharT.
  1902. //!
  1903. //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, traits::length(s)).
  1904. //!
  1905. //! <b>Throws</b>: if memory allocation throws
  1906. //!
  1907. //! <b>Returns</b>: *this
  1908. inline basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s)
  1909. { return this->replace(i1, i2, s, s + Traits::length(s)); }
  1910. //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
  1911. //!
  1912. //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(n, c)).
  1913. //!
  1914. //! <b>Throws</b>: if memory allocation throws
  1915. //!
  1916. //! <b>Returns</b>: *this
  1917. basic_string& replace(const_iterator i1, const_iterator i2, size_type n, CharT c)
  1918. {
  1919. const size_type len = static_cast<size_type>(i2 - i1);
  1920. if (len >= n) {
  1921. Traits::assign(const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), n, c);
  1922. erase(i1 + difference_type(n), i2);
  1923. }
  1924. else {
  1925. Traits::assign(const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), len, c);
  1926. insert(i2, n - len, c);
  1927. }
  1928. return *this;
  1929. }
  1930. //! <b>Requires</b>: [begin(),i1), [i1,i2) and [j1,j2) are valid ranges.
  1931. //!
  1932. //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(j1, j2)).
  1933. //!
  1934. //! <b>Throws</b>: if memory allocation throws
  1935. //!
  1936. //! <b>Returns</b>: *this
  1937. template <class InputIter>
  1938. basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2
  1939. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1940. , typename dtl::disable_if_or
  1941. < void
  1942. , dtl::is_convertible<InputIter, size_type>
  1943. , dtl::is_input_iterator<InputIter>
  1944. >::type * = 0
  1945. #endif
  1946. )
  1947. {
  1948. for ( ; i1 != i2 && j1 != j2; ++i1, ++j1){
  1949. Traits::assign(*const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)), *j1);
  1950. }
  1951. if (j1 == j2)
  1952. this->erase(i1, i2);
  1953. else
  1954. this->insert(i2, j1, j2);
  1955. return *this;
  1956. }
  1957. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1958. template <class ForwardIter>
  1959. basic_string& replace(const_iterator i1, const_iterator i2, ForwardIter j1, ForwardIter j2
  1960. , typename dtl::disable_if_or
  1961. < void
  1962. , dtl::is_convertible<ForwardIter, size_type>
  1963. , dtl::is_not_input_iterator<ForwardIter>
  1964. >::type * = 0
  1965. )
  1966. {
  1967. difference_type n = boost::container::iterator_distance(j1, j2);
  1968. const difference_type len = i2 - i1;
  1969. if (len >= n) {
  1970. this->priv_copy(j1, j2, const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)));
  1971. this->erase(i1 + difference_type(n), i2);
  1972. }
  1973. else {
  1974. ForwardIter m = j1;
  1975. boost::container::iterator_advance(m, len);
  1976. this->priv_copy(j1, m, const_cast<CharT*>(boost::movelib::to_raw_pointer(i1)));
  1977. this->insert(i2, m, j2);
  1978. }
  1979. return *this;
  1980. }
  1981. #endif
  1982. //! <b>Requires</b>: [begin(), i1) and [i1, i2) are valid ranges.
  1983. //!
  1984. //! <b>Effects</b>: Calls `replace(i1 - begin(), i2 - i1, sv).`.
  1985. //!
  1986. //! <b>Returns</b>: *this.
  1987. template<template <class, class> class BasicStringView>
  1988. inline basic_string& replace(const_iterator i1, const_iterator i2, BasicStringView<CharT, Traits> sv)
  1989. {
  1990. return this->replace( static_cast<size_type>(i1 - this->cbegin())
  1991. , static_cast<size_type>(i2 - i1), sv);
  1992. }
  1993. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1994. //! <b>Requires</b>: [begin(), i1) and [i1, i2) are valid ranges.
  1995. //!
  1996. //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, il.begin(), il.size()).
  1997. //!
  1998. //! <b>Returns</b>: *this.
  1999. inline basic_string& replace(const_iterator i1, const_iterator i2, std::initializer_list<CharT> il)
  2000. {
  2001. return this->replace( static_cast<size_type>(i1 - this->cbegin())
  2002. , static_cast<size_type>(i2 - i1)
  2003. , il.begin(), il.size());
  2004. }
  2005. #endif
  2006. //! <b>Requires</b>: pos <= size()
  2007. //!
  2008. //! <b>Effects</b>: Determines the effective length rlen of the string to copy as the
  2009. //! smaller of n and size() - pos. s shall designate an array of at least rlen elements.
  2010. //! The function then replaces the string designated by s with a string of length rlen
  2011. //! whose elements are a copy of the string controlled by *this beginning at position pos.
  2012. //! The function does not append a null object to the string designated by s.
  2013. //!
  2014. //! <b>Throws</b>: if memory allocation throws, out_of_range if pos > size().
  2015. //!
  2016. //! <b>Returns</b>: rlen
  2017. size_type copy(CharT* s, size_type n, size_type pos = 0) const
  2018. {
  2019. if (pos > this->size())
  2020. throw_out_of_range("basic_string::copy out of range position");
  2021. const size_type len = dtl::min_value(n, this->size() - pos);
  2022. Traits::copy(s, boost::movelib::to_raw_pointer(this->priv_addr() + pos), len);
  2023. return len;
  2024. }
  2025. //! <b>Effects</b>: *this contains the same sequence of characters that was in s,
  2026. //! s contains the same sequence of characters that was in *this.
  2027. //!
  2028. //! <b>Throws</b>: Nothing
  2029. void swap(basic_string& x)
  2030. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value
  2031. || allocator_traits_type::is_always_equal::value)
  2032. {
  2033. this->base_t::swap_data(x);
  2034. dtl::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
  2035. dtl::swap_alloc(this->alloc(), x.alloc(), flag);
  2036. }
  2037. //////////////////////////////////////////////
  2038. //
  2039. // data access
  2040. //
  2041. //////////////////////////////////////////////
  2042. //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
  2043. //!
  2044. //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
  2045. //!
  2046. //! <b>Complexity</b>: constant time.
  2047. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2048. const CharT* c_str() const BOOST_NOEXCEPT_OR_NOTHROW
  2049. { return boost::movelib::to_raw_pointer(this->priv_addr()); }
  2050. //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
  2051. //!
  2052. //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
  2053. //!
  2054. //! <b>Complexity</b>: constant time.
  2055. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2056. const CharT* data() const BOOST_NOEXCEPT_OR_NOTHROW
  2057. { return boost::movelib::to_raw_pointer(this->priv_addr()); }
  2058. //! <b>Returns</b>: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
  2059. //!
  2060. //! <b>Complexity</b>: constant time.
  2061. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2062. CharT* data() BOOST_NOEXCEPT_OR_NOTHROW
  2063. { return boost::movelib::to_raw_pointer(this->priv_addr()); }
  2064. #ifndef BOOST_CONTAINER_TEMPLATED_CONVERSION_OPERATOR_BROKEN
  2065. //! <b>Returns</b>: a string_view to the characters in the string.
  2066. //!
  2067. //! <b>Complexity</b>: constant time.
  2068. template<template <class, class> class BasicStringView>
  2069. inline operator BasicStringView<CharT, Traits>() const BOOST_NOEXCEPT_OR_NOTHROW
  2070. { return this->to_view< BasicStringView<CharT, Traits> >(); }
  2071. #endif
  2072. //! <b>Returns</b>: a string_view to the characters in the string.
  2073. //!
  2074. //! <b>Complexity</b>: constant time.
  2075. //!
  2076. //! <b>Note</b>: This function is available to write portable code for compilers
  2077. //! that don't support templated conversion operators.
  2078. template<class BasicStringView>
  2079. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2080. BasicStringView to_view() const BOOST_NOEXCEPT_OR_NOTHROW
  2081. { return BasicStringView(this->data(), this->size()); }
  2082. //////////////////////////////////////////////
  2083. //
  2084. // string operations
  2085. //
  2086. //////////////////////////////////////////////
  2087. //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
  2088. //! of the following conditions hold:
  2089. //! 1) pos <= xpos and xpos + str.size() <= size();
  2090. //! 2) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
  2091. //!
  2092. //! <b>Throws</b>: Nothing
  2093. //!
  2094. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2095. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2096. size_type find(const basic_string& s, size_type pos = 0) const
  2097. { return find(s.c_str(), pos, s.size()); }
  2098. //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
  2099. //! of the following conditions hold:
  2100. //! 1) pos <= xpos and xpos + sv.size() <= size();
  2101. //! 2) traits::eq(at(xpos+I), sv.at(I)) for all elements I of the string controlled by sv.
  2102. //!
  2103. //! <b>Throws</b>: Nothing
  2104. //!
  2105. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2106. template<template <class, class> class BasicStringView>
  2107. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2108. size_type find(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
  2109. { return this->find(sv.data(), pos, sv.size()); }
  2110. //! <b>Requires</b>: s points to an array of at least n elements of CharT.
  2111. //!
  2112. //! <b>Throws</b>: Nothing
  2113. //!
  2114. //! <b>Returns</b>: find(basic_string<CharT,traits,allocator_type>(s,n),pos).
  2115. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2116. size_type find(const CharT* s, size_type pos, size_type n) const
  2117. {
  2118. if (pos + n > this->size())
  2119. return npos;
  2120. else {
  2121. const pointer addr = this->priv_addr();
  2122. pointer finish = addr + difference_type(this->priv_size());
  2123. const const_iterator result =
  2124. boost::container::search(boost::movelib::to_raw_pointer(addr + difference_type(pos)),
  2125. boost::movelib::to_raw_pointer(finish),
  2126. s, s + difference_type(n), Eq_traits<Traits>());
  2127. return result != finish ? size_type(result - begin()) : npos;
  2128. }
  2129. }
  2130. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  2131. //!
  2132. //! <b>Throws</b>: Nothing
  2133. //!
  2134. //! <b>Returns</b>: find(basic_string(s), pos).
  2135. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2136. size_type find(const CharT* s, size_type pos = 0) const
  2137. { return this->find(s, pos, Traits::length(s)); }
  2138. //! <b>Throws</b>: Nothing
  2139. //!
  2140. //! <b>Returns</b>: find(basic_string<CharT,traits,allocator_type>(1,c), pos).
  2141. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2142. size_type find(CharT c, size_type pos = 0) const
  2143. {
  2144. const size_type sz = this->size();
  2145. if (pos >= sz)
  2146. return npos;
  2147. else {
  2148. const pointer addr = this->priv_addr();
  2149. pointer finish = addr + difference_type(sz);
  2150. const const_iterator result =
  2151. boost::container::find_if(addr + difference_type(pos), finish,
  2152. boost::container::bind2nd(Eq_traits<Traits>(), c));
  2153. return result != finish ? size_type(result - begin()) : npos;
  2154. }
  2155. }
  2156. //! <b>Effects</b>: Determines the highest position xpos, if possible, such
  2157. //! that both of the following conditions obtain:
  2158. //! a) xpos <= pos and xpos + str.size() <= size();
  2159. //! b) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
  2160. //!
  2161. //! <b>Throws</b>: Nothing
  2162. //!
  2163. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2164. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2165. size_type rfind(const basic_string& str, size_type pos = npos) const
  2166. { return this->rfind(str.c_str(), pos, str.size()); }
  2167. //! <b>Effects</b>: Determines the highest position xpos, if possible, such
  2168. //! that both of the following conditions obtain:
  2169. //! a) xpos <= pos and xpos + sv.size() <= size();
  2170. //! b) traits::eq(at(xpos+I), sv.at(I)) for all elements I of the string controlled by sv.
  2171. //!
  2172. //! <b>Throws</b>: Nothing
  2173. //!
  2174. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2175. template<template <class, class> class BasicStringView>
  2176. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2177. size_type rfind(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
  2178. { return this->rfind(sv.data(), pos, sv.size()); }
  2179. //! <b>Requires</b>: s points to an array of at least n elements of CharT.
  2180. //!
  2181. //! <b>Throws</b>: Nothing
  2182. //!
  2183. //! <b>Returns</b>: rfind(basic_string(s, n), pos).
  2184. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2185. size_type rfind(const CharT* s, size_type pos, size_type n) const
  2186. {
  2187. const size_type len = this->size();
  2188. if (n > len)
  2189. return npos;
  2190. else if (n == 0)
  2191. return dtl::min_value(len, pos);
  2192. else {
  2193. const const_iterator last = begin() + difference_type(dtl::min_value(len - n, pos) + n);
  2194. const const_iterator result = boost::container::find_end
  2195. (begin(), last, s, s + difference_type(n), Eq_traits<Traits>());
  2196. return result != last ? size_type(result - begin()) : npos;
  2197. }
  2198. }
  2199. //! <b>Requires</b>: pos <= size() and s points to an array of at least
  2200. //! traits::length(s) + 1 elements of CharT.
  2201. //!
  2202. //! <b>Throws</b>: Nothing
  2203. //!
  2204. //! <b>Returns</b>: rfind(basic_string(s), pos).
  2205. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2206. size_type rfind(const CharT* s, size_type pos = npos) const
  2207. { return this->rfind(s, pos, Traits::length(s)); }
  2208. //! <b>Throws</b>: Nothing
  2209. //!
  2210. //! <b>Returns</b>: rfind(basic_string<CharT,traits,allocator_type>(1,c),pos).
  2211. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2212. size_type rfind(CharT c, size_type pos = npos) const
  2213. {
  2214. const size_type len = this->size();
  2215. if (len < 1)
  2216. return npos;
  2217. else {
  2218. const const_iterator last = begin() + dtl::min_value(len - 1, pos) + 1;
  2219. const_reverse_iterator rresult =
  2220. boost::container::find_if(const_reverse_iterator(last), rend(),
  2221. boost::container::bind2nd(Eq_traits<Traits>(), c));
  2222. return rresult != rend() ? size_type((rresult.base() - 1) - begin()) : npos;
  2223. }
  2224. }
  2225. //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
  2226. //! following conditions obtain: a) pos <= xpos and xpos < size();
  2227. //! b) traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
  2228. //!
  2229. //! <b>Throws</b>: Nothing
  2230. //!
  2231. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2232. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2233. size_type find_first_of(const basic_string& str, size_type pos = 0) const
  2234. { return this->find_first_of(str.c_str(), pos, str.size()); }
  2235. //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
  2236. //! following conditions obtain: a) pos <= xpos and xpos < size();
  2237. //! b) traits::eq(at(xpos), sv.at(I)) for some element I of the string controlled by sv.
  2238. //!
  2239. //! <b>Throws</b>: Nothing
  2240. //!
  2241. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2242. template<template <class, class> class BasicStringView>
  2243. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2244. size_type find_first_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
  2245. { return this->find_first_of(sv.data(), pos, sv.size()); }
  2246. //! <b>Requires</b>: s points to an array of at least n elements of CharT.
  2247. //!
  2248. //! <b>Throws</b>: Nothing
  2249. //!
  2250. //! <b>Returns</b>: find_first_of(basic_string(s, n), pos).
  2251. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2252. size_type find_first_of(const CharT* s, size_type pos, size_type n) const
  2253. {
  2254. const size_type sz = this->size();
  2255. if (pos >= sz)
  2256. return npos;
  2257. else {
  2258. const pointer addr = this->priv_addr();
  2259. pointer finish = addr + difference_type(sz);
  2260. const_iterator result = boost::container::find_first_of
  2261. (addr + difference_type(pos), finish, s, s + difference_type(n), Eq_traits<Traits>());
  2262. return result != finish ? size_type(result - this->begin()) : npos;
  2263. }
  2264. }
  2265. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  2266. //!
  2267. //! <b>Throws</b>: Nothing
  2268. //!
  2269. //! <b>Returns</b>: find_first_of(basic_string(s), pos).
  2270. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2271. size_type find_first_of(const CharT* s, size_type pos = 0) const
  2272. { return this->find_first_of(s, pos, Traits::length(s)); }
  2273. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  2274. //!
  2275. //! <b>Throws</b>: Nothing
  2276. //!
  2277. //! <b>Returns</b>: find_first_of(basic_string<CharT,traits,allocator_type>(1,c), pos).
  2278. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2279. size_type find_first_of(CharT c, size_type pos = 0) const
  2280. { return this->find(c, pos); }
  2281. //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
  2282. //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
  2283. //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
  2284. //!
  2285. //! <b>Throws</b>: Nothing
  2286. //!
  2287. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2288. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2289. size_type find_last_of(const basic_string& str, size_type pos = npos) const
  2290. { return this->find_last_of(str.c_str(), pos, str.size()); }
  2291. //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
  2292. //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
  2293. //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
  2294. //!
  2295. //! <b>Throws</b>: Nothing
  2296. //!
  2297. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2298. template<template <class, class> class BasicStringView>
  2299. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2300. size_type find_last_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
  2301. { return this->find_last_of(sv.data(), pos, sv.size()); }
  2302. //! <b>Requires</b>: s points to an array of at least n elements of CharT.
  2303. //!
  2304. //! <b>Throws</b>: Nothing
  2305. //!
  2306. //! <b>Returns</b>: find_last_of(basic_string(s, n), pos).
  2307. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2308. size_type find_last_of(const CharT* s, size_type pos, size_type n) const
  2309. {
  2310. const size_type len = this->size();
  2311. if (len < 1)
  2312. return npos;
  2313. else {
  2314. const pointer addr = this->priv_addr();
  2315. const const_iterator last = addr + difference_type(dtl::min_value(len - 1, pos) + 1);
  2316. const const_reverse_iterator rresult =
  2317. boost::container::find_first_of(const_reverse_iterator(last), rend(),
  2318. s, s + difference_type(n), Eq_traits<Traits>());
  2319. return rresult != rend() ? size_type((rresult.base() - 1) - addr) : npos;
  2320. }
  2321. }
  2322. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  2323. //!
  2324. //! <b>Throws</b>: Nothing
  2325. //!
  2326. //! <b>Returns</b>: find_last_of(basic_string<CharT,traits,allocator_type>(1,c),pos).
  2327. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2328. size_type find_last_of(const CharT* s, size_type pos = npos) const
  2329. { return this->find_last_of(s, pos, Traits::length(s)); }
  2330. //! <b>Throws</b>: Nothing
  2331. //!
  2332. //! <b>Returns</b>: find_last_of(basic_string(s), pos).
  2333. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2334. size_type find_last_of(CharT c, size_type pos = npos) const
  2335. { return this->rfind(c, pos); }
  2336. //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
  2337. //! both of the following conditions obtain:
  2338. //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), str.at(I)) for no
  2339. //! element I of the string controlled by str.
  2340. //!
  2341. //! <b>Throws</b>: Nothing
  2342. //!
  2343. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2344. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2345. size_type find_first_not_of(const basic_string& str, size_type pos = 0) const
  2346. { return this->find_first_not_of(str.c_str(), pos, str.size()); }
  2347. //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
  2348. //! both of the following conditions obtain:
  2349. //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), sv.at(I)) for no
  2350. //! element I of the string controlled by sv.
  2351. //!
  2352. //! <b>Throws</b>: Nothing
  2353. //!
  2354. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2355. template<template <class, class> class BasicStringView>
  2356. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2357. size_type find_first_not_of(BasicStringView<CharT, Traits> sv, size_type pos = 0) const
  2358. { return this->find_first_not_of(sv.data(), pos, sv.size()); }
  2359. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  2360. //!
  2361. //! <b>Throws</b>: Nothing
  2362. //!
  2363. //! <b>Returns</b>: find_first_not_of(basic_string(s, n), pos).
  2364. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2365. size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const
  2366. {
  2367. if (pos > this->size())
  2368. return npos;
  2369. else {
  2370. const pointer addr = this->priv_addr();
  2371. const pointer finish = addr + difference_type(this->priv_size());
  2372. const const_iterator result = boost::container::find_if
  2373. (addr + difference_type(pos), finish, Not_within_traits<Traits>(s, s + difference_type(n)));
  2374. return result != finish ? size_type(result - addr) : npos;
  2375. }
  2376. }
  2377. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  2378. //!
  2379. //! <b>Throws</b>: Nothing
  2380. //!
  2381. //! <b>Returns</b>: find_first_not_of(basic_string(s), pos).
  2382. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2383. size_type find_first_not_of(const CharT* s, size_type pos = 0) const
  2384. { return this->find_first_not_of(s, pos, Traits::length(s)); }
  2385. //! <b>Throws</b>: Nothing
  2386. //!
  2387. //! <b>Returns</b>: find_first_not_of(basic_string(1, c), pos).
  2388. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2389. size_type find_first_not_of(CharT c, size_type pos = 0) const
  2390. {
  2391. if (pos > this->size())
  2392. return npos;
  2393. else {
  2394. const pointer addr = this->priv_addr();
  2395. const pointer finish = addr + difference_type(this->priv_size());
  2396. const const_iterator result
  2397. = boost::container::find_if(addr + difference_type(pos), finish,
  2398. boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
  2399. return result != finish ? size_type(result - begin()) : npos;
  2400. }
  2401. }
  2402. //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
  2403. //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
  2404. //! b) traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str.
  2405. //!
  2406. //! <b>Throws</b>: Nothing
  2407. //!
  2408. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2409. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2410. size_type find_last_not_of(const basic_string& str, size_type pos = npos) const
  2411. { return this->find_last_not_of(str.c_str(), pos, str.size()); }
  2412. //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
  2413. //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
  2414. //! b) traits::eq(at(xpos), sv.at(I)) for no element I of the string controlled by sv.
  2415. //!
  2416. //! <b>Throws</b>: Nothing
  2417. //!
  2418. //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
  2419. template<template <class, class> class BasicStringView>
  2420. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2421. size_type find_last_not_of(BasicStringView<CharT, Traits> sv, size_type pos = npos) const
  2422. { return this->find_last_not_of(sv.data(), pos, sv.size()); }
  2423. //! <b>Requires</b>: s points to an array of at least n elements of CharT.
  2424. //!
  2425. //! <b>Throws</b>: Nothing
  2426. //!
  2427. //! <b>Returns</b>: find_last_not_of(basic_string(s, n), pos).
  2428. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2429. size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const
  2430. {
  2431. const size_type len = this->size();
  2432. if (len < 1)
  2433. return npos;
  2434. else {
  2435. const const_iterator last = begin() + dtl::min_value(len - 1, pos) + 1;
  2436. const const_reverse_iterator rresult =
  2437. boost::container::find_if(const_reverse_iterator(last), rend(),
  2438. Not_within_traits<Traits>(s, s + difference_type(n)));
  2439. return rresult != rend() ? size_type((rresult.base() - 1) - begin()) : npos;
  2440. }
  2441. }
  2442. //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
  2443. //!
  2444. //! <b>Throws</b>: Nothing
  2445. //!
  2446. //! <b>Returns</b>: find_last_not_of(basic_string(s), pos).
  2447. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2448. size_type find_last_not_of(const CharT* s, size_type pos = npos) const
  2449. { return this->find_last_not_of(s, pos, Traits::length(s)); }
  2450. //! <b>Throws</b>: Nothing
  2451. //!
  2452. //! <b>Returns</b>: find_last_not_of(basic_string(1, c), pos).
  2453. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2454. size_type find_last_not_of(CharT c, size_type pos = npos) const
  2455. {
  2456. const size_type len = this->size();
  2457. if (len < 1)
  2458. return npos;
  2459. else {
  2460. const const_iterator last = begin() + dtl::min_value(len - 1, pos) + 1;
  2461. const const_reverse_iterator rresult =
  2462. boost::container::find_if(const_reverse_iterator(last), rend(),
  2463. boost::container::not1(boost::container::bind2nd(Eq_traits<Traits>(), c)));
  2464. return rresult != rend() ? size_type((rresult.base() - 1) - begin()) : npos;
  2465. }
  2466. }
  2467. //! <b>Requires</b>: Requires: pos <= size()
  2468. //!
  2469. //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
  2470. //! the smaller of n and size() - pos.
  2471. //!
  2472. //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
  2473. //!
  2474. //! <b>Returns</b>: basic_string<CharT,traits,allocator_type>(data()+pos,rlen).
  2475. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2476. basic_string substr(size_type pos = 0, size_type n = npos) const
  2477. {
  2478. if (pos > this->size())
  2479. throw_out_of_range("basic_string::substr out of range position");
  2480. const pointer addr = this->priv_addr();
  2481. return basic_string(addr + difference_type(pos),
  2482. addr + difference_type(pos + dtl::min_value(n, size() - pos)), this->alloc());
  2483. }
  2484. //! <b>Effects</b>: Determines the effective length rlen of the string to compare as
  2485. //! the smaller of size() and str.size(). The function then compares the two strings by
  2486. //! calling traits::compare(data(), str.data(), rlen).
  2487. //!
  2488. //! <b>Throws</b>: Nothing
  2489. //!
  2490. //! <b>Returns</b>: The nonzero result if the result of the comparison is nonzero.
  2491. //! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(),
  2492. //! and value > 0 if size() > str.size()
  2493. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2494. int compare(const basic_string& str) const
  2495. {
  2496. const pointer addr = this->priv_addr();
  2497. const pointer str_addr = str.priv_addr();
  2498. return this->s_compare(addr, addr + difference_type(this->priv_size()), str_addr, str_addr + difference_type(str.priv_size()));
  2499. }
  2500. //! <b>Throws</b>: Nothing
  2501. //!
  2502. //! <b>Returns</b>: compare(basic_string(sv)).
  2503. template<template <class, class> class BasicStringView>
  2504. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2505. int compare(BasicStringView<CharT,Traits> sv) const
  2506. {
  2507. const pointer addr = this->priv_addr();
  2508. return this->s_compare(addr, addr + difference_type(this->priv_size()), sv.data(), sv.data() + difference_type(sv.size()));
  2509. }
  2510. //! <b>Requires</b>: pos1 <= size()
  2511. //!
  2512. //! <b>Effects</b>: Determines the effective length rlen of the string to compare as
  2513. //! the smaller of (this->size() - pos1), n1 and str.size(). The function then compares the two strings by
  2514. //! calling traits::compare(data()+pos1, str.data(), rlen).
  2515. //!
  2516. //! <b>Throws</b>: out_of_range if pos1 > size()
  2517. //!
  2518. //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(str).
  2519. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2520. int compare(size_type pos1, size_type n1, const basic_string& str) const
  2521. {
  2522. if (pos1 > this->size())
  2523. throw_out_of_range("basic_string::compare out of range position");
  2524. const pointer addr = this->priv_addr();
  2525. const pointer str_addr = str.priv_addr();
  2526. return this->s_compare(addr + difference_type(pos1),
  2527. addr + difference_type(pos1 + dtl::min_value(n1, this->size() - pos1)),
  2528. str_addr, str_addr + difference_type(str.priv_size()));
  2529. }
  2530. //! <b>Requires</b>: pos1 <= size()
  2531. //!
  2532. //! <b>Throws</b>: out_of_range if pos1 > size()
  2533. //!
  2534. //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(sv).
  2535. template<template <class, class> class BasicStringView>
  2536. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2537. int compare(size_type pos1, size_type n1, BasicStringView<CharT,Traits> sv) const
  2538. {
  2539. if (pos1 > this->size())
  2540. throw_out_of_range("basic_string::compare out of range position");
  2541. const pointer addr = this->priv_addr() + pos1;
  2542. const CharT* str_addr = sv.data();
  2543. return this->s_compare(addr, addr + difference_type(dtl::min_value(n1, this->size() - pos1)),
  2544. str_addr, str_addr + sv.size());
  2545. }
  2546. //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
  2547. //!
  2548. //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
  2549. //! the smaller of
  2550. //!
  2551. //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > str.size()
  2552. //!
  2553. //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)).
  2554. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2555. int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos) const
  2556. {
  2557. if (pos1 > this->size() || pos2 > str.size())
  2558. throw_out_of_range("basic_string::compare out of range position");
  2559. const pointer addr = this->priv_addr() + pos1;
  2560. const pointer str_addr = str.priv_addr() + pos2;
  2561. return this->s_compare(addr, addr + difference_type(dtl::min_value(n1, this->size() - pos1)),
  2562. str_addr, str_addr + difference_type(dtl::min_value(n2, str.size() - pos2)));
  2563. }
  2564. //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
  2565. //!
  2566. //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
  2567. //! the smaller of
  2568. //!
  2569. //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > sv.size()
  2570. //!
  2571. //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(BasicStringView<CharT, Traits>(sv, pos2, n2)).
  2572. template<template <class, class> class BasicStringView>
  2573. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2574. int compare(size_type pos1, size_type n1, BasicStringView<CharT,Traits> sv, size_type pos2, size_type n2) const
  2575. {
  2576. if (pos1 > this->size() || pos2 > sv.size())
  2577. throw_out_of_range("basic_string::compare out of range position");
  2578. const pointer addr = this->priv_addr() + pos1;
  2579. const CharT * str_addr = sv.data() + pos2;
  2580. return this->s_compare(addr, addr + difference_type(dtl::min_value(n1, this->size() - pos1)),
  2581. str_addr, str_addr + difference_type(dtl::min_value(n2, sv.size() - pos2)));
  2582. }
  2583. //! <b>Throws</b>: Nothing
  2584. //!
  2585. //! <b>Returns</b>: compare(basic_string(s)).
  2586. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2587. int compare(const CharT* s) const
  2588. {
  2589. const pointer addr = this->priv_addr();
  2590. return this->s_compare(addr, addr + difference_type(this->priv_size()), s, s + Traits::length(s));
  2591. }
  2592. //! <b>Requires</b>: pos1 > size() and s points to an array of at least n2 elements of CharT.
  2593. //!
  2594. //! <b>Throws</b>: out_of_range if pos1 > size()
  2595. //!
  2596. //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
  2597. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2598. int compare(size_type pos1, size_type n1, const CharT* s, size_type n2) const
  2599. {
  2600. if (pos1 > this->size())
  2601. throw_out_of_range("basic_string::compare out of range position");
  2602. const pointer addr = this->priv_addr();
  2603. return this->s_compare( addr + difference_type(pos1),
  2604. addr + difference_type(pos1 + dtl::min_value(n1, this->size() - pos1)),
  2605. s, s + difference_type(n2));
  2606. }
  2607. //! <b>Requires</b>: pos1 > size() and s points to an array of at least traits::length(s) + 1 elements of CharT.
  2608. //!
  2609. //! <b>Throws</b>: out_of_range if pos1 > size()
  2610. //!
  2611. //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
  2612. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2613. int compare(size_type pos1, size_type n1, const CharT* s) const
  2614. { return this->compare(pos1, n1, s, Traits::length(s)); }
  2615. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2616. private:
  2617. void priv_move_assign(BOOST_RV_REF(basic_string) x, dtl::bool_<true> /*steal_resources*/)
  2618. {
  2619. //Destroy objects but retain memory in case x reuses it in the future
  2620. this->clear();
  2621. //Move allocator if needed
  2622. dtl::bool_<allocator_traits_type::
  2623. propagate_on_container_move_assignment::value> flag;
  2624. dtl::move_alloc(this->alloc(), x.alloc(), flag);
  2625. //Nothrow swap
  2626. this->swap_data(x);
  2627. }
  2628. void priv_move_assign(BOOST_RV_REF(basic_string) x, dtl::bool_<false> /*steal_resources*/)
  2629. {
  2630. //We can't guarantee a compile-time equal allocator or propagation so fallback to runtime
  2631. //Resources can be transferred if both allocators are equal
  2632. if (this->alloc() == x.alloc()) {
  2633. this->priv_move_assign(boost::move(x), dtl::true_());
  2634. }
  2635. else {
  2636. this->assign(x.begin(), x.end());
  2637. }
  2638. }
  2639. bool priv_reserve_no_null_end(size_type res_arg)
  2640. {
  2641. if (res_arg > this->max_size()){
  2642. throw_length_error("basic_string::reserve max_size() exceeded");
  2643. }
  2644. const bool do_alloc = this->capacity() < res_arg;
  2645. if (do_alloc){
  2646. //size_type n = dtl::max_value(res_arg, this->size()) + 1;
  2647. size_type n = res_arg + 1;
  2648. size_type new_cap = this->next_capacity(n);
  2649. pointer reuse = 0;
  2650. pointer new_start = this->allocation_command(allocate_new, n, new_cap, reuse);
  2651. const pointer addr = this->priv_addr();
  2652. size_type new_length = priv_uninitialized_copy
  2653. (addr, addr + difference_type(this->priv_size()), new_start);
  2654. this->deallocate_block();
  2655. this->assure_long();
  2656. this->priv_long_addr(new_start);
  2657. this->priv_long_size(new_length);
  2658. this->priv_storage(new_cap);
  2659. }
  2660. return do_alloc;
  2661. }
  2662. void priv_reserve(size_type res_arg)
  2663. {
  2664. if(this->priv_reserve_no_null_end(res_arg))
  2665. this->priv_terminate_string();
  2666. }
  2667. template<class It1, class It2>
  2668. static int s_compare(It1 f1, It1 l1, It2 f2, It2 l2)
  2669. {
  2670. const std::size_t n1 = std::size_t(l1 - f1);
  2671. const std::size_t n2 = std::size_t(l2 - f2);
  2672. const int cmp = Traits::compare(boost::movelib::to_raw_pointer(f1),
  2673. boost::movelib::to_raw_pointer(f2),
  2674. dtl::min_value(n1, n2));
  2675. return cmp != 0 ? cmp : (n1 < n2 ? -1 : (n1 > n2 ? 1 : 0));
  2676. }
  2677. template<class AllocVersion>
  2678. void priv_shrink_to_fit_dynamic_buffer
  2679. ( AllocVersion
  2680. , typename dtl::enable_if<dtl::is_same<AllocVersion, version_1> >::type* = 0)
  2681. {
  2682. //Allocate a new buffer.
  2683. size_type real_cap = 0;
  2684. const pointer long_addr = this->priv_long_addr();
  2685. const size_type long_size = this->priv_long_size();
  2686. const size_type long_storage = this->priv_long_storage();
  2687. //We can make this nothrow as chars are always NoThrowCopyables
  2688. BOOST_CONTAINER_TRY{
  2689. pointer reuse = 0;
  2690. real_cap = long_size+1;
  2691. const pointer ret = this->allocation_command(allocate_new, long_size+1, real_cap, reuse);
  2692. //Copy and update
  2693. Traits::copy( boost::movelib::to_raw_pointer(ret)
  2694. , boost::movelib::to_raw_pointer(this->priv_long_addr())
  2695. , long_size+1);
  2696. this->priv_long_addr(ret);
  2697. this->priv_storage(real_cap);
  2698. //And release old buffer
  2699. this->alloc().deallocate(long_addr, long_storage);
  2700. }
  2701. BOOST_CONTAINER_CATCH(...){
  2702. return;
  2703. }
  2704. BOOST_CONTAINER_CATCH_END
  2705. }
  2706. template<class AllocVersion>
  2707. void priv_shrink_to_fit_dynamic_buffer
  2708. ( AllocVersion
  2709. , typename dtl::enable_if<dtl::is_same<AllocVersion, version_2> >::type* = 0)
  2710. {
  2711. size_type received_size = this->priv_long_size()+1;
  2712. pointer hint = this->priv_long_addr();
  2713. if(this->alloc().allocation_command
  2714. ( shrink_in_place | nothrow_allocation, this->priv_long_storage(), received_size, hint)){
  2715. this->priv_storage(received_size);
  2716. }
  2717. }
  2718. inline void priv_construct_null(pointer p)
  2719. { traits_type::assign(*p, CharT()); }
  2720. // Helper functions used by constructors. It is a severe error for
  2721. // any of them to be called anywhere except from within constructors.
  2722. inline void priv_terminate_string()
  2723. { this->priv_construct_null(this->priv_end_addr()); }
  2724. template<class InpIt, class FwdIt> inline
  2725. size_type priv_uninitialized_copy(InpIt first, InpIt last, FwdIt dest)
  2726. {
  2727. //Save initial destination position
  2728. size_type constructed = 0;
  2729. for (; first != last; ++dest, ++first, ++constructed){
  2730. traits_type::assign(*dest, *first);
  2731. }
  2732. return constructed;
  2733. }
  2734. template <class InputIterator, class OutIterator>
  2735. static void priv_copy(InputIterator first, InputIterator last, OutIterator result)
  2736. {
  2737. for ( ; first != last; ++first, ++result)
  2738. Traits::assign(*result, *first);
  2739. }
  2740. static inline void priv_copy(const CharT* first, const CharT* last, CharT* result)
  2741. { Traits::copy(result, first, std::size_t(last - first)); }
  2742. template <class Integer>
  2743. inline basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
  2744. Integer n, Integer x,
  2745. dtl::true_)
  2746. { return this->replace(first, last, (size_type) n, (CharT) x); }
  2747. template <class InputIter>
  2748. inline basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
  2749. InputIter f, InputIter l,
  2750. dtl::false_)
  2751. {
  2752. typedef typename boost::container::iterator_traits<InputIter>::iterator_category Category;
  2753. return this->priv_replace(first, last, f, l, Category());
  2754. }
  2755. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2756. };
  2757. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  2758. template <typename InputIterator>
  2759. basic_string(InputIterator, InputIterator) ->
  2760. basic_string<typename iterator_traits<InputIterator>::value_type>;
  2761. template <typename InputIterator, typename Allocator>
  2762. basic_string(InputIterator, InputIterator, Allocator const&) ->
  2763. basic_string<typename iterator_traits<InputIterator>::value_type, Allocator>;
  2764. #endif
  2765. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  2766. //!Typedef for a basic_string of
  2767. //!narrow characters
  2768. typedef basic_string
  2769. <char
  2770. ,std::char_traits<char>
  2771. ,new_allocator<char> >
  2772. string;
  2773. //!Typedef for a basic_string of
  2774. //!narrow characters
  2775. typedef basic_string
  2776. <wchar_t
  2777. ,std::char_traits<wchar_t>
  2778. ,new_allocator<wchar_t> >
  2779. wstring;
  2780. #else
  2781. template<class S>
  2782. struct is_string
  2783. {
  2784. BOOST_STATIC_CONSTEXPR bool value = false;
  2785. };
  2786. template<class C, class T, class A>
  2787. struct is_string< basic_string<C, T, A> >
  2788. {
  2789. BOOST_STATIC_CONSTEXPR bool value = true;
  2790. };
  2791. #endif
  2792. // ------------------------------------------------------------
  2793. // Non-member functions.
  2794. // Operator+
  2795. template <class CharT, class Traits, class Allocator> inline
  2796. basic_string<CharT,Traits,Allocator>
  2797. operator+(const basic_string<CharT,Traits,Allocator>& x
  2798. ,const basic_string<CharT,Traits,Allocator>& y)
  2799. {
  2800. typedef basic_string<CharT,Traits,Allocator> str_t;
  2801. typedef typename str_t::reserve_t reserve_t;
  2802. reserve_t reserve;
  2803. str_t result(reserve, x.size() + y.size(), x.get_stored_allocator());
  2804. result.append(x);
  2805. result.append(y);
  2806. return result;
  2807. }
  2808. template <class CharT, class Traits, class Allocator> inline
  2809. basic_string<CharT, Traits, Allocator> operator+
  2810. ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END x
  2811. , BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END y)
  2812. {
  2813. x += y;
  2814. return boost::move(x);
  2815. }
  2816. template <class CharT, class Traits, class Allocator> inline
  2817. basic_string<CharT, Traits, Allocator> operator+
  2818. ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END x
  2819. , const basic_string<CharT,Traits,Allocator>& y)
  2820. {
  2821. x += y;
  2822. return boost::move(x);
  2823. }
  2824. template <class CharT, class Traits, class Allocator> inline
  2825. basic_string<CharT, Traits, Allocator> operator+
  2826. (const basic_string<CharT,Traits,Allocator>& x
  2827. ,BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END y)
  2828. {
  2829. y.insert(y.begin(), x.begin(), x.end());
  2830. return boost::move(y);
  2831. }
  2832. template <class CharT, class Traits, class Allocator> inline
  2833. basic_string<CharT, Traits, Allocator> operator+
  2834. (const CharT* s, basic_string<CharT, Traits, Allocator> y)
  2835. {
  2836. y.insert(y.begin(), s, s + Traits::length(s));
  2837. return y;
  2838. }
  2839. template <class CharT, class Traits, class Allocator> inline
  2840. basic_string<CharT,Traits,Allocator> operator+
  2841. (basic_string<CharT,Traits,Allocator> x, const CharT* s)
  2842. {
  2843. x += s;
  2844. return x;
  2845. }
  2846. template <class CharT, class Traits, class Allocator> inline
  2847. basic_string<CharT,Traits,Allocator> operator+
  2848. (CharT c, basic_string<CharT,Traits,Allocator> y)
  2849. {
  2850. y.insert(y.begin(), c);
  2851. return y;
  2852. }
  2853. template <class CharT, class Traits, class Allocator> inline
  2854. basic_string<CharT,Traits,Allocator> operator+
  2855. (basic_string<CharT,Traits,Allocator> x, const CharT c)
  2856. {
  2857. x += c;
  2858. return x;
  2859. }
  2860. // Operator== and operator!=
  2861. template <class CharT, class Traits, class Allocator>
  2862. inline bool
  2863. operator==(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
  2864. {
  2865. return x.size() == y.size() &&
  2866. Traits::compare(x.data(), y.data(), x.size()) == 0;
  2867. }
  2868. template <class CharT, class Traits, class Allocator>
  2869. inline bool
  2870. operator==(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
  2871. {
  2872. typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
  2873. return n == y.size() && Traits::compare(s, y.data(), n) == 0;
  2874. }
  2875. template <class CharT, class Traits, class Allocator>
  2876. inline bool
  2877. operator==(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
  2878. {
  2879. typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
  2880. return x.size() == n && Traits::compare(x.data(), s, n) == 0;
  2881. }
  2882. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  2883. inline
  2884. BOOST_CONTAINER_DOC1ST( bool,
  2885. typename dtl::disable_if
  2886. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  2887. operator==( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
  2888. {
  2889. return x.size() == y.size() &&
  2890. Traits::compare(x.data(), y.data(), x.size()) == 0;
  2891. }
  2892. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  2893. inline
  2894. BOOST_CONTAINER_DOC1ST( bool,
  2895. typename dtl::disable_if
  2896. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  2897. operator==( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
  2898. {
  2899. return x.size() == y.size() &&
  2900. Traits::compare(x.data(), y.data(), x.size()) == 0;
  2901. }
  2902. template <class CharT, class Traits, class Allocator>
  2903. inline bool
  2904. operator!=(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
  2905. { return !(x == y); }
  2906. template <class CharT, class Traits, class Allocator>
  2907. inline bool
  2908. operator!=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
  2909. { return !(s == y); }
  2910. template <class CharT, class Traits, class Allocator>
  2911. inline bool
  2912. operator!=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
  2913. { return !(x == s); }
  2914. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  2915. inline
  2916. BOOST_CONTAINER_DOC1ST( bool,
  2917. typename dtl::disable_if
  2918. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  2919. operator!=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
  2920. { return !(x == y); }
  2921. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  2922. inline
  2923. BOOST_CONTAINER_DOC1ST( bool,
  2924. typename dtl::disable_if
  2925. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  2926. operator!=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
  2927. { return !(x == y); }
  2928. // Operator< (and also >, <=, and >=).
  2929. template <class CharT, class Traits, class Allocator>
  2930. inline bool
  2931. operator<(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
  2932. {
  2933. return x.compare(y) < 0;
  2934. }
  2935. template <class CharT, class Traits, class Allocator>
  2936. inline bool
  2937. operator<(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
  2938. {
  2939. return y.compare(s) > 0;
  2940. }
  2941. template <class CharT, class Traits, class Allocator>
  2942. inline bool
  2943. operator<(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
  2944. {
  2945. return x.compare(s) < 0;
  2946. }
  2947. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  2948. inline
  2949. BOOST_CONTAINER_DOC1ST( bool,
  2950. typename dtl::disable_if
  2951. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  2952. operator<( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
  2953. { return y.compare(x) > 0; }
  2954. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  2955. inline
  2956. BOOST_CONTAINER_DOC1ST( bool,
  2957. typename dtl::disable_if
  2958. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  2959. operator<( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
  2960. { return x.compare(y) < 0; }
  2961. template <class CharT, class Traits, class Allocator>
  2962. inline bool
  2963. operator>(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y) {
  2964. return y < x;
  2965. }
  2966. template <class CharT, class Traits, class Allocator>
  2967. inline bool
  2968. operator>(const CharT* s, const basic_string<CharT,Traits,Allocator>& y) {
  2969. return y < s;
  2970. }
  2971. template <class CharT, class Traits, class Allocator>
  2972. inline bool
  2973. operator>(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
  2974. {
  2975. return s < x;
  2976. }
  2977. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  2978. inline
  2979. BOOST_CONTAINER_DOC1ST( bool,
  2980. typename dtl::disable_if
  2981. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  2982. operator>( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
  2983. { return y < x; }
  2984. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  2985. inline
  2986. BOOST_CONTAINER_DOC1ST( bool,
  2987. typename dtl::disable_if
  2988. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  2989. operator>( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
  2990. { return y < x; }
  2991. template <class CharT, class Traits, class Allocator>
  2992. inline bool
  2993. operator<=(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
  2994. {
  2995. return !(y < x);
  2996. }
  2997. template <class CharT, class Traits, class Allocator>
  2998. inline bool
  2999. operator<=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
  3000. { return !(y < s); }
  3001. template <class CharT, class Traits, class Allocator>
  3002. inline bool
  3003. operator<=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
  3004. { return !(s < x); }
  3005. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  3006. inline
  3007. BOOST_CONTAINER_DOC1ST( bool,
  3008. typename dtl::disable_if
  3009. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  3010. operator<=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
  3011. { return !(y < x); }
  3012. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  3013. inline
  3014. BOOST_CONTAINER_DOC1ST( bool,
  3015. typename dtl::disable_if
  3016. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  3017. operator<=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
  3018. { return !(y < x); }
  3019. template <class CharT, class Traits, class Allocator>
  3020. inline bool
  3021. operator>=(const basic_string<CharT,Traits,Allocator>& x,
  3022. const basic_string<CharT,Traits,Allocator>& y)
  3023. { return !(x < y); }
  3024. template <class CharT, class Traits, class Allocator>
  3025. inline bool
  3026. operator>=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
  3027. { return !(s < y); }
  3028. template <class CharT, class Traits, class Allocator>
  3029. inline bool
  3030. operator>=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
  3031. { return !(x < s); }
  3032. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  3033. inline
  3034. BOOST_CONTAINER_DOC1ST( bool,
  3035. typename dtl::disable_if
  3036. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  3037. operator>=( BasicStringView<CharT,Traits> x, const basic_string<CharT,Traits,Allocator>& y)
  3038. { return !(x < y); }
  3039. template <class CharT, class Traits, class Allocator, template <class, class> class BasicStringView>
  3040. inline
  3041. BOOST_CONTAINER_DOC1ST( bool,
  3042. typename dtl::disable_if
  3043. <is_string< BasicStringView<CharT BOOST_MOVE_I Traits> > BOOST_MOVE_I bool >::type)
  3044. operator>=( const basic_string<CharT,Traits,Allocator>& x, BasicStringView<CharT,Traits> y)
  3045. { return !(x < y); }
  3046. // Swap.
  3047. template <class CharT, class Traits, class Allocator>
  3048. inline void swap(basic_string<CharT,Traits,Allocator>& x, basic_string<CharT,Traits,Allocator>& y)
  3049. BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT(x.swap(y)))
  3050. { x.swap(y); }
  3051. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  3052. // I/O.
  3053. namespace dtl {
  3054. template <class CharT, class Traits>
  3055. inline bool
  3056. string_fill(std::basic_ostream<CharT, Traits>& os,
  3057. std::basic_streambuf<CharT, Traits>* buf,
  3058. std::size_t n)
  3059. {
  3060. CharT f = os.fill();
  3061. std::size_t i;
  3062. bool ok = true;
  3063. for (i = 0; i < n; i++)
  3064. ok = ok && !Traits::eq_int_type(buf->sputc(f), Traits::eof());
  3065. return ok;
  3066. }
  3067. } //namespace dtl {
  3068. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  3069. template <class CharT, class Traits, class Allocator>
  3070. std::basic_ostream<CharT, Traits>&
  3071. operator<<(std::basic_ostream<CharT, Traits>& os, const basic_string<CharT,Traits,Allocator>& s)
  3072. {
  3073. typename std::basic_ostream<CharT, Traits>::sentry sentry(os);
  3074. bool ok = false;
  3075. if (sentry) {
  3076. ok = true;
  3077. typename basic_string<CharT,Traits,Allocator>::size_type n = s.size();
  3078. typename basic_string<CharT,Traits,Allocator>::size_type pad_len = 0;
  3079. const bool left = (os.flags() & std::ios::left) != 0;
  3080. const std::size_t w = os.width(0);
  3081. std::basic_streambuf<CharT, Traits>* buf = os.rdbuf();
  3082. if (w != 0 && n < w)
  3083. pad_len = w - n;
  3084. if (!left)
  3085. ok = dtl::string_fill(os, buf, pad_len);
  3086. ok = ok &&
  3087. buf->sputn(s.data(), std::streamsize(n)) == std::streamsize(n);
  3088. if (left)
  3089. ok = ok && dtl::string_fill(os, buf, pad_len);
  3090. }
  3091. if (!ok)
  3092. os.setstate(std::ios_base::failbit);
  3093. return os;
  3094. }
  3095. template <class CharT, class Traits, class Allocator>
  3096. std::basic_istream<CharT, Traits>&
  3097. operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
  3098. {
  3099. typename std::basic_istream<CharT, Traits>::sentry sentry(is);
  3100. if (sentry) {
  3101. std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
  3102. const std::ctype<CharT>& ctype = std::use_facet<std::ctype<CharT> >(is.getloc());
  3103. s.clear();
  3104. std::size_t n = is.width(0);
  3105. if (n == 0)
  3106. n = static_cast<std::size_t>(-1);
  3107. else
  3108. s.reserve(n);
  3109. while (n-- > 0) {
  3110. typename Traits::int_type c1 = buf->sbumpc();
  3111. if (Traits::eq_int_type(c1, Traits::eof())) {
  3112. is.setstate(std::ios_base::eofbit);
  3113. break;
  3114. }
  3115. else {
  3116. CharT c = Traits::to_char_type(c1);
  3117. if (ctype.is(std::ctype<CharT>::space, c)) {
  3118. if (Traits::eq_int_type(buf->sputbackc(c), Traits::eof()))
  3119. is.setstate(std::ios_base::failbit);
  3120. break;
  3121. }
  3122. else
  3123. s.push_back(c);
  3124. }
  3125. }
  3126. // If we have read no characters, then set failbit.
  3127. if (s.size() == 0)
  3128. is.setstate(std::ios_base::failbit);
  3129. }
  3130. else
  3131. is.setstate(std::ios_base::failbit);
  3132. return is;
  3133. }
  3134. template <class CharT, class Traits, class Allocator>
  3135. std::basic_istream<CharT, Traits>&
  3136. getline(std::istream& is, basic_string<CharT,Traits,Allocator>& s,CharT delim)
  3137. {
  3138. typename basic_string<CharT,Traits,Allocator>::size_type nread = 0;
  3139. typename std::basic_istream<CharT, Traits>::sentry sentry(is, true);
  3140. if (sentry) {
  3141. std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
  3142. s.clear();
  3143. while (nread < s.max_size()) {
  3144. int c1 = buf->sbumpc();
  3145. if (Traits::eq_int_type(c1, Traits::eof())) {
  3146. is.setstate(std::ios_base::eofbit);
  3147. break;
  3148. }
  3149. else {
  3150. ++nread;
  3151. CharT c = Traits::to_char_type(c1);
  3152. if (!Traits::eq(c, delim))
  3153. s.push_back(c);
  3154. else
  3155. break; // Character is extracted but not appended.
  3156. }
  3157. }
  3158. }
  3159. if (nread == 0 || nread >= s.max_size())
  3160. is.setstate(std::ios_base::failbit);
  3161. return is;
  3162. }
  3163. template <class CharT, class Traits, class Allocator>
  3164. inline std::basic_istream<CharT, Traits>&
  3165. getline(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
  3166. {
  3167. return getline(is, s, '\n');
  3168. }
  3169. //! <b>Effects</b>: Erases all elements that compare equal to v from the container c.
  3170. //!
  3171. //! <b>Complexity</b>: Linear.
  3172. template <class T, class Tr, class A, class U>
  3173. inline typename basic_string<T, Tr, A>::size_type erase(basic_string<T, Tr, A>& c, const U& v)
  3174. {
  3175. typename basic_string<T, Tr, A>::size_type old_size = c.size();
  3176. c.erase(boost::container::remove(c.begin(), c.end(), v), c.end());
  3177. return old_size - c.size();
  3178. }
  3179. //! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
  3180. //!
  3181. //! <b>Complexity</b>: Linear.
  3182. template <class T, class Tr, class A, class Pred>
  3183. inline typename basic_string<T, Tr, A>::size_type erase_if(basic_string<T, Tr, A>& c, Pred pred)
  3184. {
  3185. typename basic_string<T, Tr, A>::size_type old_size = c.size();
  3186. c.erase(boost::container::remove_if(c.begin(), c.end(), pred), c.end());
  3187. return old_size - c.size();
  3188. }
  3189. }}
  3190. //GCC 12 has a regression for array-bounds warnings
  3191. #if defined(BOOST_GCC) && (BOOST_GCC >= 120000) && (BOOST_GCC < 130000)
  3192. #pragma GCC diagnostic pop
  3193. #endif
  3194. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  3195. namespace boost {
  3196. //!has_trivial_destructor_after_move<> == true_type
  3197. //!specialization for optimizations
  3198. template <class C, class T, class Allocator>
  3199. struct has_trivial_destructor_after_move<boost::container::basic_string<C, T, Allocator> >
  3200. {
  3201. typedef typename boost::container::basic_string<C, T, Allocator>::allocator_type allocator_type;
  3202. typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
  3203. BOOST_STATIC_CONSTEXPR bool value =
  3204. ::boost::has_trivial_destructor_after_move<allocator_type>::value &&
  3205. ::boost::has_trivial_destructor_after_move<pointer>::value;
  3206. };
  3207. }
  3208. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  3209. #include <boost/container/detail/config_end.hpp>
  3210. #endif // BOOST_CONTAINER_STRING_HPP