string.hpp 131 KB

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