string.hpp 132 KB

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