string.hpp 124 KB

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