string.hpp 132 KB

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