string.hpp 139 KB

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