string.hpp 125 KB

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