string.hpp 135 KB

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