string.hpp 101 KB

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