string.hpp 133 KB

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