cow_string.h 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657
  1. // Definition of gcc4-compatible Copy-on-Write basic_string -*- C++ -*-
  2. // Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/cow_string.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{string}
  23. *
  24. * Defines the reference-counted COW string implentation.
  25. */
  26. #ifndef _COW_STRING_H
  27. #define _COW_STRING_H 1
  28. #if ! _GLIBCXX_USE_CXX11_ABI
  29. #include <ext/atomicity.h> // _Atomic_word, __is_single_threaded
  30. #ifdef __cpp_lib_is_constant_evaluated
  31. // Support P1032R1 in C++20 (but not P0980R1 for COW strings).
  32. # define __cpp_lib_constexpr_string 201811L
  33. #elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
  34. // Support P0426R1 changes to char_traits in C++17.
  35. # define __cpp_lib_constexpr_string 201611L
  36. #endif
  37. namespace std _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. /**
  41. * @class basic_string basic_string.h <string>
  42. * @brief Managing sequences of characters and character-like objects.
  43. *
  44. * @ingroup strings
  45. * @ingroup sequences
  46. *
  47. * @tparam _CharT Type of character
  48. * @tparam _Traits Traits for character type, defaults to
  49. * char_traits<_CharT>.
  50. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  51. *
  52. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  53. * <a href="tables.html#66">reversible container</a>, and a
  54. * <a href="tables.html#67">sequence</a>. Of the
  55. * <a href="tables.html#68">optional sequence requirements</a>, only
  56. * @c push_back, @c at, and @c %array access are supported.
  57. *
  58. * @doctodo
  59. *
  60. *
  61. * Documentation? What's that?
  62. * Nathan Myers <ncm@cantrip.org>.
  63. *
  64. * A string looks like this:
  65. *
  66. * @code
  67. * [_Rep]
  68. * _M_length
  69. * [basic_string<char_type>] _M_capacity
  70. * _M_dataplus _M_refcount
  71. * _M_p ----------------> unnamed array of char_type
  72. * @endcode
  73. *
  74. * Where the _M_p points to the first character in the string, and
  75. * you cast it to a pointer-to-_Rep and subtract 1 to get a
  76. * pointer to the header.
  77. *
  78. * This approach has the enormous advantage that a string object
  79. * requires only one allocation. All the ugliness is confined
  80. * within a single %pair of inline functions, which each compile to
  81. * a single @a add instruction: _Rep::_M_data(), and
  82. * string::_M_rep(); and the allocation function which gets a
  83. * block of raw bytes and with room enough and constructs a _Rep
  84. * object at the front.
  85. *
  86. * The reason you want _M_data pointing to the character %array and
  87. * not the _Rep is so that the debugger can see the string
  88. * contents. (Probably we should add a non-inline member to get
  89. * the _Rep for the debugger to use, so users can check the actual
  90. * string length.)
  91. *
  92. * Note that the _Rep object is a POD so that you can have a
  93. * static <em>empty string</em> _Rep object already @a constructed before
  94. * static constructors have run. The reference-count encoding is
  95. * chosen so that a 0 indicates one reference, so you never try to
  96. * destroy the empty-string _Rep object.
  97. *
  98. * All but the last paragraph is considered pretty conventional
  99. * for a Copy-On-Write C++ string implementation.
  100. */
  101. // 21.3 Template class basic_string
  102. template<typename _CharT, typename _Traits, typename _Alloc>
  103. class basic_string
  104. {
  105. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  106. rebind<_CharT>::other _CharT_alloc_type;
  107. typedef __gnu_cxx::__alloc_traits<_CharT_alloc_type> _CharT_alloc_traits;
  108. // Types:
  109. public:
  110. typedef _Traits traits_type;
  111. typedef typename _Traits::char_type value_type;
  112. typedef _Alloc allocator_type;
  113. typedef typename _CharT_alloc_traits::size_type size_type;
  114. typedef typename _CharT_alloc_traits::difference_type difference_type;
  115. #if __cplusplus < 201103L
  116. typedef typename _CharT_alloc_type::reference reference;
  117. typedef typename _CharT_alloc_type::const_reference const_reference;
  118. #else
  119. typedef value_type& reference;
  120. typedef const value_type& const_reference;
  121. #endif
  122. typedef typename _CharT_alloc_traits::pointer pointer;
  123. typedef typename _CharT_alloc_traits::const_pointer const_pointer;
  124. typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
  125. typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
  126. const_iterator;
  127. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  128. typedef std::reverse_iterator<iterator> reverse_iterator;
  129. protected:
  130. // type used for positions in insert, erase etc.
  131. typedef iterator __const_iterator;
  132. private:
  133. // _Rep: string representation
  134. // Invariants:
  135. // 1. String really contains _M_length + 1 characters: due to 21.3.4
  136. // must be kept null-terminated.
  137. // 2. _M_capacity >= _M_length
  138. // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
  139. // 3. _M_refcount has three states:
  140. // -1: leaked, one reference, no ref-copies allowed, non-const.
  141. // 0: one reference, non-const.
  142. // n>0: n + 1 references, operations require a lock, const.
  143. // 4. All fields==0 is an empty string, given the extra storage
  144. // beyond-the-end for a null terminator; thus, the shared
  145. // empty string representation needs no constructor.
  146. struct _Rep_base
  147. {
  148. size_type _M_length;
  149. size_type _M_capacity;
  150. _Atomic_word _M_refcount;
  151. };
  152. struct _Rep : _Rep_base
  153. {
  154. // Types:
  155. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  156. rebind<char>::other _Raw_bytes_alloc;
  157. // (Public) Data members:
  158. // The maximum number of individual char_type elements of an
  159. // individual string is determined by _S_max_size. This is the
  160. // value that will be returned by max_size(). (Whereas npos
  161. // is the maximum number of bytes the allocator can allocate.)
  162. // If one was to divvy up the theoretical largest size string,
  163. // with a terminating character and m _CharT elements, it'd
  164. // look like this:
  165. // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
  166. // Solving for m:
  167. // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
  168. // In addition, this implementation quarters this amount.
  169. static const size_type _S_max_size;
  170. static const _CharT _S_terminal;
  171. // The following storage is init'd to 0 by the linker, resulting
  172. // (carefully) in an empty string with one reference.
  173. static size_type _S_empty_rep_storage[];
  174. static _Rep&
  175. _S_empty_rep() _GLIBCXX_NOEXCEPT
  176. {
  177. // NB: Mild hack to avoid strict-aliasing warnings. Note that
  178. // _S_empty_rep_storage is never modified and the punning should
  179. // be reasonably safe in this case.
  180. void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
  181. return *reinterpret_cast<_Rep*>(__p);
  182. }
  183. bool
  184. _M_is_leaked() const _GLIBCXX_NOEXCEPT
  185. {
  186. #if defined(__GTHREADS)
  187. // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
  188. // so we need to use an atomic load. However, _M_is_leaked
  189. // predicate does not change concurrently (i.e. the string is either
  190. // leaked or not), so a relaxed load is enough.
  191. return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
  192. #else
  193. return this->_M_refcount < 0;
  194. #endif
  195. }
  196. bool
  197. _M_is_shared() const _GLIBCXX_NOEXCEPT
  198. {
  199. #if defined(__GTHREADS)
  200. // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
  201. // so we need to use an atomic load. Another thread can drop last
  202. // but one reference concurrently with this check, so we need this
  203. // load to be acquire to synchronize with release fetch_and_add in
  204. // _M_dispose.
  205. if (!__gnu_cxx::__is_single_threaded())
  206. return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
  207. #endif
  208. return this->_M_refcount > 0;
  209. }
  210. void
  211. _M_set_leaked() _GLIBCXX_NOEXCEPT
  212. { this->_M_refcount = -1; }
  213. void
  214. _M_set_sharable() _GLIBCXX_NOEXCEPT
  215. { this->_M_refcount = 0; }
  216. void
  217. _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
  218. {
  219. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  220. if (__builtin_expect(this != &_S_empty_rep(), false))
  221. #endif
  222. {
  223. this->_M_set_sharable(); // One reference.
  224. this->_M_length = __n;
  225. traits_type::assign(this->_M_refdata()[__n], _S_terminal);
  226. // grrr. (per 21.3.4)
  227. // You cannot leave those LWG people alone for a second.
  228. }
  229. }
  230. _CharT*
  231. _M_refdata() throw()
  232. { return reinterpret_cast<_CharT*>(this + 1); }
  233. _CharT*
  234. _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
  235. {
  236. return (!_M_is_leaked() && __alloc1 == __alloc2)
  237. ? _M_refcopy() : _M_clone(__alloc1);
  238. }
  239. // Create & Destroy
  240. static _Rep*
  241. _S_create(size_type, size_type, const _Alloc&);
  242. void
  243. _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
  244. {
  245. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  246. if (__builtin_expect(this != &_S_empty_rep(), false))
  247. #endif
  248. {
  249. // Be race-detector-friendly. For more info see bits/c++config.
  250. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
  251. // Decrement of _M_refcount is acq_rel, because:
  252. // - all but last decrements need to release to synchronize with
  253. // the last decrement that will delete the object.
  254. // - the last decrement needs to acquire to synchronize with
  255. // all the previous decrements.
  256. // - last but one decrement needs to release to synchronize with
  257. // the acquire load in _M_is_shared that will conclude that
  258. // the object is not shared anymore.
  259. if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
  260. -1) <= 0)
  261. {
  262. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
  263. _M_destroy(__a);
  264. }
  265. }
  266. } // XXX MT
  267. void
  268. _M_destroy(const _Alloc&) throw();
  269. _CharT*
  270. _M_refcopy() throw()
  271. {
  272. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  273. if (__builtin_expect(this != &_S_empty_rep(), false))
  274. #endif
  275. __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
  276. return _M_refdata();
  277. } // XXX MT
  278. _CharT*
  279. _M_clone(const _Alloc&, size_type __res = 0);
  280. };
  281. // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
  282. struct _Alloc_hider : _Alloc
  283. {
  284. _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
  285. : _Alloc(__a), _M_p(__dat) { }
  286. _CharT* _M_p; // The actual data.
  287. };
  288. public:
  289. // Data Members (public):
  290. // NB: This is an unsigned type, and thus represents the maximum
  291. // size that the allocator can hold.
  292. /// Value returned by various member functions when they fail.
  293. static const size_type npos = static_cast<size_type>(-1);
  294. private:
  295. // Data Members (private):
  296. mutable _Alloc_hider _M_dataplus;
  297. _CharT*
  298. _M_data() const _GLIBCXX_NOEXCEPT
  299. { return _M_dataplus._M_p; }
  300. _CharT*
  301. _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
  302. { return (_M_dataplus._M_p = __p); }
  303. _Rep*
  304. _M_rep() const _GLIBCXX_NOEXCEPT
  305. { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
  306. // For the internal use we have functions similar to `begin'/`end'
  307. // but they do not call _M_leak.
  308. iterator
  309. _M_ibegin() const _GLIBCXX_NOEXCEPT
  310. { return iterator(_M_data()); }
  311. iterator
  312. _M_iend() const _GLIBCXX_NOEXCEPT
  313. { return iterator(_M_data() + this->size()); }
  314. void
  315. _M_leak() // for use in begin() & non-const op[]
  316. {
  317. if (!_M_rep()->_M_is_leaked())
  318. _M_leak_hard();
  319. }
  320. size_type
  321. _M_check(size_type __pos, const char* __s) const
  322. {
  323. if (__pos > this->size())
  324. __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  325. "this->size() (which is %zu)"),
  326. __s, __pos, this->size());
  327. return __pos;
  328. }
  329. void
  330. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  331. {
  332. if (this->max_size() - (this->size() - __n1) < __n2)
  333. __throw_length_error(__N(__s));
  334. }
  335. // NB: _M_limit doesn't check for a bad __pos value.
  336. size_type
  337. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  338. {
  339. const bool __testoff = __off < this->size() - __pos;
  340. return __testoff ? __off : this->size() - __pos;
  341. }
  342. // True if _Rep and source do not overlap.
  343. bool
  344. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  345. {
  346. return (less<const _CharT*>()(__s, _M_data())
  347. || less<const _CharT*>()(_M_data() + this->size(), __s));
  348. }
  349. // When __n = 1 way faster than the general multichar
  350. // traits_type::copy/move/assign.
  351. static void
  352. _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
  353. {
  354. if (__n == 1)
  355. traits_type::assign(*__d, *__s);
  356. else
  357. traits_type::copy(__d, __s, __n);
  358. }
  359. static void
  360. _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
  361. {
  362. if (__n == 1)
  363. traits_type::assign(*__d, *__s);
  364. else
  365. traits_type::move(__d, __s, __n);
  366. }
  367. static void
  368. _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
  369. {
  370. if (__n == 1)
  371. traits_type::assign(*__d, __c);
  372. else
  373. traits_type::assign(__d, __n, __c);
  374. }
  375. // _S_copy_chars is a separate template to permit specialization
  376. // to optimize for the common case of pointers as iterators.
  377. template<class _Iterator>
  378. static void
  379. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  380. {
  381. for (; __k1 != __k2; ++__k1, (void)++__p)
  382. traits_type::assign(*__p, *__k1); // These types are off.
  383. }
  384. static void
  385. _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
  386. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  387. static void
  388. _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
  389. _GLIBCXX_NOEXCEPT
  390. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  391. static void
  392. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
  393. { _M_copy(__p, __k1, __k2 - __k1); }
  394. static void
  395. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  396. _GLIBCXX_NOEXCEPT
  397. { _M_copy(__p, __k1, __k2 - __k1); }
  398. static int
  399. _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
  400. {
  401. const difference_type __d = difference_type(__n1 - __n2);
  402. if (__d > __gnu_cxx::__numeric_traits<int>::__max)
  403. return __gnu_cxx::__numeric_traits<int>::__max;
  404. else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
  405. return __gnu_cxx::__numeric_traits<int>::__min;
  406. else
  407. return int(__d);
  408. }
  409. void
  410. _M_mutate(size_type __pos, size_type __len1, size_type __len2);
  411. void
  412. _M_leak_hard();
  413. static _Rep&
  414. _S_empty_rep() _GLIBCXX_NOEXCEPT
  415. { return _Rep::_S_empty_rep(); }
  416. #if __cplusplus >= 201703L
  417. // A helper type for avoiding boiler-plate.
  418. typedef basic_string_view<_CharT, _Traits> __sv_type;
  419. template<typename _Tp, typename _Res>
  420. using _If_sv = enable_if_t<
  421. __and_<is_convertible<const _Tp&, __sv_type>,
  422. __not_<is_convertible<const _Tp*, const basic_string*>>,
  423. __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
  424. _Res>;
  425. // Allows an implicit conversion to __sv_type.
  426. static __sv_type
  427. _S_to_string_view(__sv_type __svt) noexcept
  428. { return __svt; }
  429. // Wraps a string_view by explicit conversion and thus
  430. // allows to add an internal constructor that does not
  431. // participate in overload resolution when a string_view
  432. // is provided.
  433. struct __sv_wrapper
  434. {
  435. explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
  436. __sv_type _M_sv;
  437. };
  438. /**
  439. * @brief Only internally used: Construct string from a string view
  440. * wrapper.
  441. * @param __svw string view wrapper.
  442. * @param __a Allocator to use.
  443. */
  444. explicit
  445. basic_string(__sv_wrapper __svw, const _Alloc& __a)
  446. : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
  447. #endif
  448. public:
  449. // Construct/copy/destroy:
  450. // NB: We overload ctors in some cases instead of using default
  451. // arguments, per 17.4.4.4 para. 2 item 2.
  452. /**
  453. * @brief Default constructor creates an empty string.
  454. */
  455. basic_string()
  456. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  457. _GLIBCXX_NOEXCEPT
  458. : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
  459. #else
  460. : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
  461. #endif
  462. { }
  463. /**
  464. * @brief Construct an empty string using allocator @a a.
  465. */
  466. explicit
  467. basic_string(const _Alloc& __a)
  468. : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
  469. { }
  470. // NB: per LWG issue 42, semantics different from IS:
  471. /**
  472. * @brief Construct string with copy of value of @a str.
  473. * @param __str Source string.
  474. */
  475. basic_string(const basic_string& __str)
  476. : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
  477. __str.get_allocator()),
  478. __str.get_allocator())
  479. { }
  480. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  481. // 2583. no way to supply an allocator for basic_string(str, pos)
  482. /**
  483. * @brief Construct string as copy of a substring.
  484. * @param __str Source string.
  485. * @param __pos Index of first character to copy from.
  486. * @param __a Allocator to use.
  487. */
  488. basic_string(const basic_string& __str, size_type __pos,
  489. const _Alloc& __a = _Alloc());
  490. /**
  491. * @brief Construct string as copy of a substring.
  492. * @param __str Source string.
  493. * @param __pos Index of first character to copy from.
  494. * @param __n Number of characters to copy.
  495. */
  496. basic_string(const basic_string& __str, size_type __pos,
  497. size_type __n);
  498. /**
  499. * @brief Construct string as copy of a substring.
  500. * @param __str Source string.
  501. * @param __pos Index of first character to copy from.
  502. * @param __n Number of characters to copy.
  503. * @param __a Allocator to use.
  504. */
  505. basic_string(const basic_string& __str, size_type __pos,
  506. size_type __n, const _Alloc& __a);
  507. /**
  508. * @brief Construct string initialized by a character %array.
  509. * @param __s Source character %array.
  510. * @param __n Number of characters to copy.
  511. * @param __a Allocator to use (default is default allocator).
  512. *
  513. * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
  514. * has no special meaning.
  515. */
  516. basic_string(const _CharT* __s, size_type __n,
  517. const _Alloc& __a = _Alloc())
  518. : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
  519. { }
  520. /**
  521. * @brief Construct string as copy of a C string.
  522. * @param __s Source C string.
  523. * @param __a Allocator to use (default is default allocator).
  524. */
  525. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  526. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  527. // 3076. basic_string CTAD ambiguity
  528. template<typename = _RequireAllocator<_Alloc>>
  529. #endif
  530. basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
  531. : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
  532. __s + npos, __a), __a)
  533. { }
  534. /**
  535. * @brief Construct string as multiple characters.
  536. * @param __n Number of characters.
  537. * @param __c Character to use.
  538. * @param __a Allocator to use (default is default allocator).
  539. */
  540. basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
  541. : _M_dataplus(_S_construct(__n, __c, __a), __a)
  542. { }
  543. #if __cplusplus >= 201103L
  544. /**
  545. * @brief Move construct string.
  546. * @param __str Source string.
  547. *
  548. * The newly-created string contains the exact contents of @a __str.
  549. * @a __str is a valid, but unspecified string.
  550. */
  551. basic_string(basic_string&& __str) noexcept
  552. : _M_dataplus(std::move(__str._M_dataplus))
  553. {
  554. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  555. // Make __str use the shared empty string rep.
  556. __str._M_data(_S_empty_rep()._M_refdata());
  557. #else
  558. // Rather than allocate an empty string for the rvalue string,
  559. // just share ownership with it by incrementing the reference count.
  560. // If the rvalue string was the unique owner then there are exactly
  561. // two owners now.
  562. if (_M_rep()->_M_is_shared())
  563. __gnu_cxx::__atomic_add_dispatch(&_M_rep()->_M_refcount, 1);
  564. else
  565. _M_rep()->_M_refcount = 1;
  566. #endif
  567. }
  568. /**
  569. * @brief Construct string from an initializer %list.
  570. * @param __l std::initializer_list of characters.
  571. * @param __a Allocator to use (default is default allocator).
  572. */
  573. basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
  574. : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
  575. { }
  576. basic_string(const basic_string& __str, const _Alloc& __a)
  577. : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a)
  578. { }
  579. basic_string(basic_string&& __str, const _Alloc& __a)
  580. : _M_dataplus(__str._M_data(), __a)
  581. {
  582. if (__a == __str.get_allocator())
  583. {
  584. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  585. __str._M_data(_S_empty_rep()._M_refdata());
  586. #else
  587. __str._M_data(_S_construct(size_type(), _CharT(), __a));
  588. #endif
  589. }
  590. else
  591. _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
  592. }
  593. #endif // C++11
  594. #if __cplusplus >= 202100L
  595. basic_string(nullptr_t) = delete;
  596. basic_string& operator=(nullptr_t) = delete;
  597. #endif // C++23
  598. /**
  599. * @brief Construct string as copy of a range.
  600. * @param __beg Start of range.
  601. * @param __end End of range.
  602. * @param __a Allocator to use (default is default allocator).
  603. */
  604. template<class _InputIterator>
  605. basic_string(_InputIterator __beg, _InputIterator __end,
  606. const _Alloc& __a = _Alloc())
  607. : _M_dataplus(_S_construct(__beg, __end, __a), __a)
  608. { }
  609. #if __cplusplus >= 201703L
  610. /**
  611. * @brief Construct string from a substring of a string_view.
  612. * @param __t Source object convertible to string view.
  613. * @param __pos The index of the first character to copy from __t.
  614. * @param __n The number of characters to copy from __t.
  615. * @param __a Allocator to use.
  616. */
  617. template<typename _Tp,
  618. typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>>
  619. basic_string(const _Tp& __t, size_type __pos, size_type __n,
  620. const _Alloc& __a = _Alloc())
  621. : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
  622. /**
  623. * @brief Construct string from a string_view.
  624. * @param __t Source object convertible to string view.
  625. * @param __a Allocator to use (default is default allocator).
  626. */
  627. template<typename _Tp, typename = _If_sv<_Tp, void>>
  628. explicit
  629. basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
  630. : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
  631. #endif // C++17
  632. /**
  633. * @brief Destroy the string instance.
  634. */
  635. ~basic_string() _GLIBCXX_NOEXCEPT
  636. { _M_rep()->_M_dispose(this->get_allocator()); }
  637. /**
  638. * @brief Assign the value of @a str to this string.
  639. * @param __str Source string.
  640. */
  641. basic_string&
  642. operator=(const basic_string& __str)
  643. { return this->assign(__str); }
  644. /**
  645. * @brief Copy contents of @a s into this string.
  646. * @param __s Source null-terminated string.
  647. */
  648. basic_string&
  649. operator=(const _CharT* __s)
  650. { return this->assign(__s); }
  651. /**
  652. * @brief Set value to string of length 1.
  653. * @param __c Source character.
  654. *
  655. * Assigning to a character makes this string length 1 and
  656. * (*this)[0] == @a c.
  657. */
  658. basic_string&
  659. operator=(_CharT __c)
  660. {
  661. this->assign(1, __c);
  662. return *this;
  663. }
  664. #if __cplusplus >= 201103L
  665. /**
  666. * @brief Move assign the value of @a str to this string.
  667. * @param __str Source string.
  668. *
  669. * The contents of @a str are moved into this string (without copying).
  670. * @a str is a valid, but unspecified string.
  671. */
  672. basic_string&
  673. operator=(basic_string&& __str)
  674. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  675. {
  676. // NB: DR 1204.
  677. this->swap(__str);
  678. return *this;
  679. }
  680. /**
  681. * @brief Set value to string constructed from initializer %list.
  682. * @param __l std::initializer_list.
  683. */
  684. basic_string&
  685. operator=(initializer_list<_CharT> __l)
  686. {
  687. this->assign(__l.begin(), __l.size());
  688. return *this;
  689. }
  690. #endif // C++11
  691. #if __cplusplus >= 201703L
  692. /**
  693. * @brief Set value to string constructed from a string_view.
  694. * @param __svt An object convertible to string_view.
  695. */
  696. template<typename _Tp>
  697. _If_sv<_Tp, basic_string&>
  698. operator=(const _Tp& __svt)
  699. { return this->assign(__svt); }
  700. /**
  701. * @brief Convert to a string_view.
  702. * @return A string_view.
  703. */
  704. operator __sv_type() const noexcept
  705. { return __sv_type(data(), size()); }
  706. #endif // C++17
  707. // Iterators:
  708. /**
  709. * Returns a read/write iterator that points to the first character in
  710. * the %string. Unshares the string.
  711. */
  712. iterator
  713. begin() // FIXME C++11: should be noexcept.
  714. {
  715. _M_leak();
  716. return iterator(_M_data());
  717. }
  718. /**
  719. * Returns a read-only (constant) iterator that points to the first
  720. * character in the %string.
  721. */
  722. const_iterator
  723. begin() const _GLIBCXX_NOEXCEPT
  724. { return const_iterator(_M_data()); }
  725. /**
  726. * Returns a read/write iterator that points one past the last
  727. * character in the %string. Unshares the string.
  728. */
  729. iterator
  730. end() // FIXME C++11: should be noexcept.
  731. {
  732. _M_leak();
  733. return iterator(_M_data() + this->size());
  734. }
  735. /**
  736. * Returns a read-only (constant) iterator that points one past the
  737. * last character in the %string.
  738. */
  739. const_iterator
  740. end() const _GLIBCXX_NOEXCEPT
  741. { return const_iterator(_M_data() + this->size()); }
  742. /**
  743. * Returns a read/write reverse iterator that points to the last
  744. * character in the %string. Iteration is done in reverse element
  745. * order. Unshares the string.
  746. */
  747. reverse_iterator
  748. rbegin() // FIXME C++11: should be noexcept.
  749. { return reverse_iterator(this->end()); }
  750. /**
  751. * Returns a read-only (constant) reverse iterator that points
  752. * to the last character in the %string. Iteration is done in
  753. * reverse element order.
  754. */
  755. const_reverse_iterator
  756. rbegin() const _GLIBCXX_NOEXCEPT
  757. { return const_reverse_iterator(this->end()); }
  758. /**
  759. * Returns a read/write reverse iterator that points to one before the
  760. * first character in the %string. Iteration is done in reverse
  761. * element order. Unshares the string.
  762. */
  763. reverse_iterator
  764. rend() // FIXME C++11: should be noexcept.
  765. { return reverse_iterator(this->begin()); }
  766. /**
  767. * Returns a read-only (constant) reverse iterator that points
  768. * to one before the first character in the %string. Iteration
  769. * is done in reverse element order.
  770. */
  771. const_reverse_iterator
  772. rend() const _GLIBCXX_NOEXCEPT
  773. { return const_reverse_iterator(this->begin()); }
  774. #if __cplusplus >= 201103L
  775. /**
  776. * Returns a read-only (constant) iterator that points to the first
  777. * character in the %string.
  778. */
  779. const_iterator
  780. cbegin() const noexcept
  781. { return const_iterator(this->_M_data()); }
  782. /**
  783. * Returns a read-only (constant) iterator that points one past the
  784. * last character in the %string.
  785. */
  786. const_iterator
  787. cend() const noexcept
  788. { return const_iterator(this->_M_data() + this->size()); }
  789. /**
  790. * Returns a read-only (constant) reverse iterator that points
  791. * to the last character in the %string. Iteration is done in
  792. * reverse element order.
  793. */
  794. const_reverse_iterator
  795. crbegin() const noexcept
  796. { return const_reverse_iterator(this->end()); }
  797. /**
  798. * Returns a read-only (constant) reverse iterator that points
  799. * to one before the first character in the %string. Iteration
  800. * is done in reverse element order.
  801. */
  802. const_reverse_iterator
  803. crend() const noexcept
  804. { return const_reverse_iterator(this->begin()); }
  805. #endif
  806. public:
  807. // Capacity:
  808. /// Returns the number of characters in the string, not including any
  809. /// null-termination.
  810. size_type
  811. size() const _GLIBCXX_NOEXCEPT
  812. { return _M_rep()->_M_length; }
  813. /// Returns the number of characters in the string, not including any
  814. /// null-termination.
  815. size_type
  816. length() const _GLIBCXX_NOEXCEPT
  817. { return _M_rep()->_M_length; }
  818. /// Returns the size() of the largest possible %string.
  819. size_type
  820. max_size() const _GLIBCXX_NOEXCEPT
  821. { return _Rep::_S_max_size; }
  822. /**
  823. * @brief Resizes the %string to the specified number of characters.
  824. * @param __n Number of characters the %string should contain.
  825. * @param __c Character to fill any new elements.
  826. *
  827. * This function will %resize the %string to the specified
  828. * number of characters. If the number is smaller than the
  829. * %string's current size the %string is truncated, otherwise
  830. * the %string is extended and new elements are %set to @a __c.
  831. */
  832. void
  833. resize(size_type __n, _CharT __c);
  834. /**
  835. * @brief Resizes the %string to the specified number of characters.
  836. * @param __n Number of characters the %string should contain.
  837. *
  838. * This function will resize the %string to the specified length. If
  839. * the new size is smaller than the %string's current size the %string
  840. * is truncated, otherwise the %string is extended and new characters
  841. * are default-constructed. For basic types such as char, this means
  842. * setting them to 0.
  843. */
  844. void
  845. resize(size_type __n)
  846. { this->resize(__n, _CharT()); }
  847. #if __cplusplus >= 201103L
  848. #pragma GCC diagnostic push
  849. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  850. /// A non-binding request to reduce capacity() to size().
  851. void
  852. shrink_to_fit() noexcept
  853. { reserve(); }
  854. #pragma GCC diagnostic pop
  855. #endif
  856. /**
  857. * Returns the total number of characters that the %string can hold
  858. * before needing to allocate more memory.
  859. */
  860. size_type
  861. capacity() const _GLIBCXX_NOEXCEPT
  862. { return _M_rep()->_M_capacity; }
  863. /**
  864. * @brief Attempt to preallocate enough memory for specified number of
  865. * characters.
  866. * @param __res_arg Number of characters required.
  867. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  868. *
  869. * This function attempts to reserve enough memory for the
  870. * %string to hold the specified number of characters. If the
  871. * number requested is more than max_size(), length_error is
  872. * thrown.
  873. *
  874. * The advantage of this function is that if optimal code is a
  875. * necessity and the user can determine the string length that will be
  876. * required, the user can reserve the memory in %advance, and thus
  877. * prevent a possible reallocation of memory and copying of %string
  878. * data.
  879. */
  880. void
  881. reserve(size_type __res_arg);
  882. /// Equivalent to shrink_to_fit().
  883. #if __cplusplus > 201703L
  884. [[deprecated("use shrink_to_fit() instead")]]
  885. #endif
  886. void
  887. reserve();
  888. /**
  889. * Erases the string, making it empty.
  890. */
  891. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  892. void
  893. clear() _GLIBCXX_NOEXCEPT
  894. {
  895. if (_M_rep()->_M_is_shared())
  896. {
  897. _M_rep()->_M_dispose(this->get_allocator());
  898. _M_data(_S_empty_rep()._M_refdata());
  899. }
  900. else
  901. _M_rep()->_M_set_length_and_sharable(0);
  902. }
  903. #else
  904. // PR 56166: this should not throw.
  905. void
  906. clear()
  907. { _M_mutate(0, this->size(), 0); }
  908. #endif
  909. /**
  910. * Returns true if the %string is empty. Equivalent to
  911. * <code>*this == ""</code>.
  912. */
  913. _GLIBCXX_NODISCARD bool
  914. empty() const _GLIBCXX_NOEXCEPT
  915. { return this->size() == 0; }
  916. // Element access:
  917. /**
  918. * @brief Subscript access to the data contained in the %string.
  919. * @param __pos The index of the character to access.
  920. * @return Read-only (constant) reference to the character.
  921. *
  922. * This operator allows for easy, array-style, data access.
  923. * Note that data access with this operator is unchecked and
  924. * out_of_range lookups are not defined. (For checked lookups
  925. * see at().)
  926. */
  927. const_reference
  928. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  929. {
  930. __glibcxx_assert(__pos <= size());
  931. return _M_data()[__pos];
  932. }
  933. /**
  934. * @brief Subscript access to the data contained in the %string.
  935. * @param __pos The index of the character to access.
  936. * @return Read/write reference to the character.
  937. *
  938. * This operator allows for easy, array-style, data access.
  939. * Note that data access with this operator is unchecked and
  940. * out_of_range lookups are not defined. (For checked lookups
  941. * see at().) Unshares the string.
  942. */
  943. reference
  944. operator[](size_type __pos)
  945. {
  946. // Allow pos == size() both in C++98 mode, as v3 extension,
  947. // and in C++11 mode.
  948. __glibcxx_assert(__pos <= size());
  949. // In pedantic mode be strict in C++98 mode.
  950. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
  951. _M_leak();
  952. return _M_data()[__pos];
  953. }
  954. /**
  955. * @brief Provides access to the data contained in the %string.
  956. * @param __n The index of the character to access.
  957. * @return Read-only (const) reference to the character.
  958. * @throw std::out_of_range If @a n is an invalid index.
  959. *
  960. * This function provides for safer data access. The parameter is
  961. * first checked that it is in the range of the string. The function
  962. * throws out_of_range if the check fails.
  963. */
  964. const_reference
  965. at(size_type __n) const
  966. {
  967. if (__n >= this->size())
  968. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  969. "(which is %zu) >= this->size() "
  970. "(which is %zu)"),
  971. __n, this->size());
  972. return _M_data()[__n];
  973. }
  974. /**
  975. * @brief Provides access to the data contained in the %string.
  976. * @param __n The index of the character to access.
  977. * @return Read/write reference to the character.
  978. * @throw std::out_of_range If @a n is an invalid index.
  979. *
  980. * This function provides for safer data access. The parameter is
  981. * first checked that it is in the range of the string. The function
  982. * throws out_of_range if the check fails. Success results in
  983. * unsharing the string.
  984. */
  985. reference
  986. at(size_type __n)
  987. {
  988. if (__n >= size())
  989. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  990. "(which is %zu) >= this->size() "
  991. "(which is %zu)"),
  992. __n, this->size());
  993. _M_leak();
  994. return _M_data()[__n];
  995. }
  996. #if __cplusplus >= 201103L
  997. /**
  998. * Returns a read/write reference to the data at the first
  999. * element of the %string.
  1000. */
  1001. reference
  1002. front()
  1003. {
  1004. __glibcxx_assert(!empty());
  1005. return operator[](0);
  1006. }
  1007. /**
  1008. * Returns a read-only (constant) reference to the data at the first
  1009. * element of the %string.
  1010. */
  1011. const_reference
  1012. front() const noexcept
  1013. {
  1014. __glibcxx_assert(!empty());
  1015. return operator[](0);
  1016. }
  1017. /**
  1018. * Returns a read/write reference to the data at the last
  1019. * element of the %string.
  1020. */
  1021. reference
  1022. back()
  1023. {
  1024. __glibcxx_assert(!empty());
  1025. return operator[](this->size() - 1);
  1026. }
  1027. /**
  1028. * Returns a read-only (constant) reference to the data at the
  1029. * last element of the %string.
  1030. */
  1031. const_reference
  1032. back() const noexcept
  1033. {
  1034. __glibcxx_assert(!empty());
  1035. return operator[](this->size() - 1);
  1036. }
  1037. #endif
  1038. // Modifiers:
  1039. /**
  1040. * @brief Append a string to this string.
  1041. * @param __str The string to append.
  1042. * @return Reference to this string.
  1043. */
  1044. basic_string&
  1045. operator+=(const basic_string& __str)
  1046. { return this->append(__str); }
  1047. /**
  1048. * @brief Append a C string.
  1049. * @param __s The C string to append.
  1050. * @return Reference to this string.
  1051. */
  1052. basic_string&
  1053. operator+=(const _CharT* __s)
  1054. { return this->append(__s); }
  1055. /**
  1056. * @brief Append a character.
  1057. * @param __c The character to append.
  1058. * @return Reference to this string.
  1059. */
  1060. basic_string&
  1061. operator+=(_CharT __c)
  1062. {
  1063. this->push_back(__c);
  1064. return *this;
  1065. }
  1066. #if __cplusplus >= 201103L
  1067. /**
  1068. * @brief Append an initializer_list of characters.
  1069. * @param __l The initializer_list of characters to be appended.
  1070. * @return Reference to this string.
  1071. */
  1072. basic_string&
  1073. operator+=(initializer_list<_CharT> __l)
  1074. { return this->append(__l.begin(), __l.size()); }
  1075. #endif // C++11
  1076. #if __cplusplus >= 201703L
  1077. /**
  1078. * @brief Append a string_view.
  1079. * @param __svt The object convertible to string_view to be appended.
  1080. * @return Reference to this string.
  1081. */
  1082. template<typename _Tp>
  1083. _If_sv<_Tp, basic_string&>
  1084. operator+=(const _Tp& __svt)
  1085. { return this->append(__svt); }
  1086. #endif // C++17
  1087. /**
  1088. * @brief Append a string to this string.
  1089. * @param __str The string to append.
  1090. * @return Reference to this string.
  1091. */
  1092. basic_string&
  1093. append(const basic_string& __str);
  1094. /**
  1095. * @brief Append a substring.
  1096. * @param __str The string to append.
  1097. * @param __pos Index of the first character of str to append.
  1098. * @param __n The number of characters to append.
  1099. * @return Reference to this string.
  1100. * @throw std::out_of_range if @a __pos is not a valid index.
  1101. *
  1102. * This function appends @a __n characters from @a __str
  1103. * starting at @a __pos to this string. If @a __n is is larger
  1104. * than the number of available characters in @a __str, the
  1105. * remainder of @a __str is appended.
  1106. */
  1107. basic_string&
  1108. append(const basic_string& __str, size_type __pos, size_type __n = npos);
  1109. /**
  1110. * @brief Append a C substring.
  1111. * @param __s The C string to append.
  1112. * @param __n The number of characters to append.
  1113. * @return Reference to this string.
  1114. */
  1115. basic_string&
  1116. append(const _CharT* __s, size_type __n);
  1117. /**
  1118. * @brief Append a C string.
  1119. * @param __s The C string to append.
  1120. * @return Reference to this string.
  1121. */
  1122. basic_string&
  1123. append(const _CharT* __s)
  1124. {
  1125. __glibcxx_requires_string(__s);
  1126. return this->append(__s, traits_type::length(__s));
  1127. }
  1128. /**
  1129. * @brief Append multiple characters.
  1130. * @param __n The number of characters to append.
  1131. * @param __c The character to use.
  1132. * @return Reference to this string.
  1133. *
  1134. * Appends __n copies of __c to this string.
  1135. */
  1136. basic_string&
  1137. append(size_type __n, _CharT __c);
  1138. #if __cplusplus >= 201103L
  1139. /**
  1140. * @brief Append an initializer_list of characters.
  1141. * @param __l The initializer_list of characters to append.
  1142. * @return Reference to this string.
  1143. */
  1144. basic_string&
  1145. append(initializer_list<_CharT> __l)
  1146. { return this->append(__l.begin(), __l.size()); }
  1147. #endif // C++11
  1148. /**
  1149. * @brief Append a range of characters.
  1150. * @param __first Iterator referencing the first character to append.
  1151. * @param __last Iterator marking the end of the range.
  1152. * @return Reference to this string.
  1153. *
  1154. * Appends characters in the range [__first,__last) to this string.
  1155. */
  1156. template<class _InputIterator>
  1157. basic_string&
  1158. append(_InputIterator __first, _InputIterator __last)
  1159. { return this->replace(_M_iend(), _M_iend(), __first, __last); }
  1160. #if __cplusplus >= 201703L
  1161. /**
  1162. * @brief Append a string_view.
  1163. * @param __svt The object convertible to string_view to be appended.
  1164. * @return Reference to this string.
  1165. */
  1166. template<typename _Tp>
  1167. _If_sv<_Tp, basic_string&>
  1168. append(const _Tp& __svt)
  1169. {
  1170. __sv_type __sv = __svt;
  1171. return this->append(__sv.data(), __sv.size());
  1172. }
  1173. /**
  1174. * @brief Append a range of characters from a string_view.
  1175. * @param __svt The object convertible to string_view to be appended
  1176. * from.
  1177. * @param __pos The position in the string_view to append from.
  1178. * @param __n The number of characters to append from the string_view.
  1179. * @return Reference to this string.
  1180. */
  1181. template<typename _Tp>
  1182. _If_sv<_Tp, basic_string&>
  1183. append(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1184. {
  1185. __sv_type __sv = __svt;
  1186. return append(__sv.data()
  1187. + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
  1188. std::__sv_limit(__sv.size(), __pos, __n));
  1189. }
  1190. #endif // C++17
  1191. /**
  1192. * @brief Append a single character.
  1193. * @param __c Character to append.
  1194. */
  1195. void
  1196. push_back(_CharT __c)
  1197. {
  1198. const size_type __len = 1 + this->size();
  1199. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  1200. this->reserve(__len);
  1201. traits_type::assign(_M_data()[this->size()], __c);
  1202. _M_rep()->_M_set_length_and_sharable(__len);
  1203. }
  1204. /**
  1205. * @brief Set value to contents of another string.
  1206. * @param __str Source string to use.
  1207. * @return Reference to this string.
  1208. */
  1209. basic_string&
  1210. assign(const basic_string& __str);
  1211. #if __cplusplus >= 201103L
  1212. /**
  1213. * @brief Set value to contents of another string.
  1214. * @param __str Source string to use.
  1215. * @return Reference to this string.
  1216. *
  1217. * This function sets this string to the exact contents of @a __str.
  1218. * @a __str is a valid, but unspecified string.
  1219. */
  1220. basic_string&
  1221. assign(basic_string&& __str)
  1222. noexcept(allocator_traits<_Alloc>::is_always_equal::value)
  1223. {
  1224. this->swap(__str);
  1225. return *this;
  1226. }
  1227. #endif // C++11
  1228. /**
  1229. * @brief Set value to a substring of a string.
  1230. * @param __str The string to use.
  1231. * @param __pos Index of the first character of str.
  1232. * @param __n Number of characters to use.
  1233. * @return Reference to this string.
  1234. * @throw std::out_of_range if @a pos is not a valid index.
  1235. *
  1236. * This function sets this string to the substring of @a __str
  1237. * consisting of @a __n characters at @a __pos. If @a __n is
  1238. * is larger than the number of available characters in @a
  1239. * __str, the remainder of @a __str is used.
  1240. */
  1241. basic_string&
  1242. assign(const basic_string& __str, size_type __pos, size_type __n = npos)
  1243. { return this->assign(__str._M_data()
  1244. + __str._M_check(__pos, "basic_string::assign"),
  1245. __str._M_limit(__pos, __n)); }
  1246. /**
  1247. * @brief Set value to a C substring.
  1248. * @param __s The C string to use.
  1249. * @param __n Number of characters to use.
  1250. * @return Reference to this string.
  1251. *
  1252. * This function sets the value of this string to the first @a __n
  1253. * characters of @a __s. If @a __n is is larger than the number of
  1254. * available characters in @a __s, the remainder of @a __s is used.
  1255. */
  1256. basic_string&
  1257. assign(const _CharT* __s, size_type __n);
  1258. /**
  1259. * @brief Set value to contents of a C string.
  1260. * @param __s The C string to use.
  1261. * @return Reference to this string.
  1262. *
  1263. * This function sets the value of this string to the value of @a __s.
  1264. * The data is copied, so there is no dependence on @a __s once the
  1265. * function returns.
  1266. */
  1267. basic_string&
  1268. assign(const _CharT* __s)
  1269. {
  1270. __glibcxx_requires_string(__s);
  1271. return this->assign(__s, traits_type::length(__s));
  1272. }
  1273. /**
  1274. * @brief Set value to multiple characters.
  1275. * @param __n Length of the resulting string.
  1276. * @param __c The character to use.
  1277. * @return Reference to this string.
  1278. *
  1279. * This function sets the value of this string to @a __n copies of
  1280. * character @a __c.
  1281. */
  1282. basic_string&
  1283. assign(size_type __n, _CharT __c)
  1284. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  1285. /**
  1286. * @brief Set value to a range of characters.
  1287. * @param __first Iterator referencing the first character to append.
  1288. * @param __last Iterator marking the end of the range.
  1289. * @return Reference to this string.
  1290. *
  1291. * Sets value of string to characters in the range [__first,__last).
  1292. */
  1293. template<class _InputIterator>
  1294. basic_string&
  1295. assign(_InputIterator __first, _InputIterator __last)
  1296. { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
  1297. #if __cplusplus >= 201103L
  1298. /**
  1299. * @brief Set value to an initializer_list of characters.
  1300. * @param __l The initializer_list of characters to assign.
  1301. * @return Reference to this string.
  1302. */
  1303. basic_string&
  1304. assign(initializer_list<_CharT> __l)
  1305. { return this->assign(__l.begin(), __l.size()); }
  1306. #endif // C++11
  1307. #if __cplusplus >= 201703L
  1308. /**
  1309. * @brief Set value from a string_view.
  1310. * @param __svt The source object convertible to string_view.
  1311. * @return Reference to this string.
  1312. */
  1313. template<typename _Tp>
  1314. _If_sv<_Tp, basic_string&>
  1315. assign(const _Tp& __svt)
  1316. {
  1317. __sv_type __sv = __svt;
  1318. return this->assign(__sv.data(), __sv.size());
  1319. }
  1320. /**
  1321. * @brief Set value from a range of characters in a string_view.
  1322. * @param __svt The source object convertible to string_view.
  1323. * @param __pos The position in the string_view to assign from.
  1324. * @param __n The number of characters to assign.
  1325. * @return Reference to this string.
  1326. */
  1327. template<typename _Tp>
  1328. _If_sv<_Tp, basic_string&>
  1329. assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1330. {
  1331. __sv_type __sv = __svt;
  1332. return assign(__sv.data()
  1333. + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
  1334. std::__sv_limit(__sv.size(), __pos, __n));
  1335. }
  1336. #endif // C++17
  1337. /**
  1338. * @brief Insert multiple characters.
  1339. * @param __p Iterator referencing location in string to insert at.
  1340. * @param __n Number of characters to insert
  1341. * @param __c The character to insert.
  1342. * @throw std::length_error If new length exceeds @c max_size().
  1343. *
  1344. * Inserts @a __n copies of character @a __c starting at the
  1345. * position referenced by iterator @a __p. If adding
  1346. * characters causes the length to exceed max_size(),
  1347. * length_error is thrown. The value of the string doesn't
  1348. * change if an error is thrown.
  1349. */
  1350. void
  1351. insert(iterator __p, size_type __n, _CharT __c)
  1352. { this->replace(__p, __p, __n, __c); }
  1353. /**
  1354. * @brief Insert a range of characters.
  1355. * @param __p Iterator referencing location in string to insert at.
  1356. * @param __beg Start of range.
  1357. * @param __end End of range.
  1358. * @throw std::length_error If new length exceeds @c max_size().
  1359. *
  1360. * Inserts characters in range [__beg,__end). If adding
  1361. * characters causes the length to exceed max_size(),
  1362. * length_error is thrown. The value of the string doesn't
  1363. * change if an error is thrown.
  1364. */
  1365. template<class _InputIterator>
  1366. void
  1367. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  1368. { this->replace(__p, __p, __beg, __end); }
  1369. #if __cplusplus >= 201103L
  1370. /**
  1371. * @brief Insert an initializer_list of characters.
  1372. * @param __p Iterator referencing location in string to insert at.
  1373. * @param __l The initializer_list of characters to insert.
  1374. * @throw std::length_error If new length exceeds @c max_size().
  1375. */
  1376. void
  1377. insert(iterator __p, initializer_list<_CharT> __l)
  1378. {
  1379. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  1380. this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
  1381. }
  1382. #endif // C++11
  1383. /**
  1384. * @brief Insert value of a string.
  1385. * @param __pos1 Position in string to insert at.
  1386. * @param __str The string to insert.
  1387. * @return Reference to this string.
  1388. * @throw std::length_error If new length exceeds @c max_size().
  1389. *
  1390. * Inserts value of @a __str starting at @a __pos1. If adding
  1391. * characters causes the length to exceed max_size(),
  1392. * length_error is thrown. The value of the string doesn't
  1393. * change if an error is thrown.
  1394. */
  1395. basic_string&
  1396. insert(size_type __pos1, const basic_string& __str)
  1397. { return this->insert(__pos1, __str, size_type(0), __str.size()); }
  1398. /**
  1399. * @brief Insert a substring.
  1400. * @param __pos1 Position in string to insert at.
  1401. * @param __str The string to insert.
  1402. * @param __pos2 Start of characters in str to insert.
  1403. * @param __n Number of characters to insert.
  1404. * @return Reference to this string.
  1405. * @throw std::length_error If new length exceeds @c max_size().
  1406. * @throw std::out_of_range If @a pos1 > size() or
  1407. * @a __pos2 > @a str.size().
  1408. *
  1409. * Starting at @a pos1, insert @a __n character of @a __str
  1410. * beginning with @a __pos2. If adding characters causes the
  1411. * length to exceed max_size(), length_error is thrown. If @a
  1412. * __pos1 is beyond the end of this string or @a __pos2 is
  1413. * beyond the end of @a __str, out_of_range is thrown. The
  1414. * value of the string doesn't change if an error is thrown.
  1415. */
  1416. basic_string&
  1417. insert(size_type __pos1, const basic_string& __str,
  1418. size_type __pos2, size_type __n = npos)
  1419. { return this->insert(__pos1, __str._M_data()
  1420. + __str._M_check(__pos2, "basic_string::insert"),
  1421. __str._M_limit(__pos2, __n)); }
  1422. /**
  1423. * @brief Insert a C substring.
  1424. * @param __pos Position in string to insert at.
  1425. * @param __s The C string to insert.
  1426. * @param __n The number of characters to insert.
  1427. * @return Reference to this string.
  1428. * @throw std::length_error If new length exceeds @c max_size().
  1429. * @throw std::out_of_range If @a __pos is beyond the end of this
  1430. * string.
  1431. *
  1432. * Inserts the first @a __n characters of @a __s starting at @a
  1433. * __pos. If adding characters causes the length to exceed
  1434. * max_size(), length_error is thrown. If @a __pos is beyond
  1435. * end(), out_of_range is thrown. The value of the string
  1436. * doesn't change if an error is thrown.
  1437. */
  1438. basic_string&
  1439. insert(size_type __pos, const _CharT* __s, size_type __n);
  1440. /**
  1441. * @brief Insert a C string.
  1442. * @param __pos Position in string to insert at.
  1443. * @param __s The C string to insert.
  1444. * @return Reference to this string.
  1445. * @throw std::length_error If new length exceeds @c max_size().
  1446. * @throw std::out_of_range If @a pos is beyond the end of this
  1447. * string.
  1448. *
  1449. * Inserts the first @a n characters of @a __s starting at @a __pos. If
  1450. * adding characters causes the length to exceed max_size(),
  1451. * length_error is thrown. If @a __pos is beyond end(), out_of_range is
  1452. * thrown. The value of the string doesn't change if an error is
  1453. * thrown.
  1454. */
  1455. basic_string&
  1456. insert(size_type __pos, const _CharT* __s)
  1457. {
  1458. __glibcxx_requires_string(__s);
  1459. return this->insert(__pos, __s, traits_type::length(__s));
  1460. }
  1461. /**
  1462. * @brief Insert multiple characters.
  1463. * @param __pos Index in string to insert at.
  1464. * @param __n Number of characters to insert
  1465. * @param __c The character to insert.
  1466. * @return Reference to this string.
  1467. * @throw std::length_error If new length exceeds @c max_size().
  1468. * @throw std::out_of_range If @a __pos is beyond the end of this
  1469. * string.
  1470. *
  1471. * Inserts @a __n copies of character @a __c starting at index
  1472. * @a __pos. If adding characters causes the length to exceed
  1473. * max_size(), length_error is thrown. If @a __pos > length(),
  1474. * out_of_range is thrown. The value of the string doesn't
  1475. * change if an error is thrown.
  1476. */
  1477. basic_string&
  1478. insert(size_type __pos, size_type __n, _CharT __c)
  1479. { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
  1480. size_type(0), __n, __c); }
  1481. /**
  1482. * @brief Insert one character.
  1483. * @param __p Iterator referencing position in string to insert at.
  1484. * @param __c The character to insert.
  1485. * @return Iterator referencing newly inserted char.
  1486. * @throw std::length_error If new length exceeds @c max_size().
  1487. *
  1488. * Inserts character @a __c at position referenced by @a __p.
  1489. * If adding character causes the length to exceed max_size(),
  1490. * length_error is thrown. If @a __p is beyond end of string,
  1491. * out_of_range is thrown. The value of the string doesn't
  1492. * change if an error is thrown.
  1493. */
  1494. iterator
  1495. insert(iterator __p, _CharT __c)
  1496. {
  1497. _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
  1498. const size_type __pos = __p - _M_ibegin();
  1499. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  1500. _M_rep()->_M_set_leaked();
  1501. return iterator(_M_data() + __pos);
  1502. }
  1503. #if __cplusplus >= 201703L
  1504. /**
  1505. * @brief Insert a string_view.
  1506. * @param __pos Position in string to insert at.
  1507. * @param __svt The object convertible to string_view to insert.
  1508. * @return Reference to this string.
  1509. */
  1510. template<typename _Tp>
  1511. _If_sv<_Tp, basic_string&>
  1512. insert(size_type __pos, const _Tp& __svt)
  1513. {
  1514. __sv_type __sv = __svt;
  1515. return this->insert(__pos, __sv.data(), __sv.size());
  1516. }
  1517. /**
  1518. * @brief Insert a string_view.
  1519. * @param __pos1 Position in string to insert at.
  1520. * @param __svt The object convertible to string_view to insert from.
  1521. * @param __pos2 Position in string_view to insert from.
  1522. * @param __n The number of characters to insert.
  1523. * @return Reference to this string.
  1524. */
  1525. template<typename _Tp>
  1526. _If_sv<_Tp, basic_string&>
  1527. insert(size_type __pos1, const _Tp& __svt,
  1528. size_type __pos2, size_type __n = npos)
  1529. {
  1530. __sv_type __sv = __svt;
  1531. return this->replace(__pos1, size_type(0), __sv.data()
  1532. + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
  1533. std::__sv_limit(__sv.size(), __pos2, __n));
  1534. }
  1535. #endif // C++17
  1536. /**
  1537. * @brief Remove characters.
  1538. * @param __pos Index of first character to remove (default 0).
  1539. * @param __n Number of characters to remove (default remainder).
  1540. * @return Reference to this string.
  1541. * @throw std::out_of_range If @a pos is beyond the end of this
  1542. * string.
  1543. *
  1544. * Removes @a __n characters from this string starting at @a
  1545. * __pos. The length of the string is reduced by @a __n. If
  1546. * there are < @a __n characters to remove, the remainder of
  1547. * the string is truncated. If @a __p is beyond end of string,
  1548. * out_of_range is thrown. The value of the string doesn't
  1549. * change if an error is thrown.
  1550. */
  1551. basic_string&
  1552. erase(size_type __pos = 0, size_type __n = npos)
  1553. {
  1554. _M_mutate(_M_check(__pos, "basic_string::erase"),
  1555. _M_limit(__pos, __n), size_type(0));
  1556. return *this;
  1557. }
  1558. /**
  1559. * @brief Remove one character.
  1560. * @param __position Iterator referencing the character to remove.
  1561. * @return iterator referencing same location after removal.
  1562. *
  1563. * Removes the character at @a __position from this string. The value
  1564. * of the string doesn't change if an error is thrown.
  1565. */
  1566. iterator
  1567. erase(iterator __position)
  1568. {
  1569. _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
  1570. && __position < _M_iend());
  1571. const size_type __pos = __position - _M_ibegin();
  1572. _M_mutate(__pos, size_type(1), size_type(0));
  1573. _M_rep()->_M_set_leaked();
  1574. return iterator(_M_data() + __pos);
  1575. }
  1576. /**
  1577. * @brief Remove a range of characters.
  1578. * @param __first Iterator referencing the first character to remove.
  1579. * @param __last Iterator referencing the end of the range.
  1580. * @return Iterator referencing location of first after removal.
  1581. *
  1582. * Removes the characters in the range [first,last) from this string.
  1583. * The value of the string doesn't change if an error is thrown.
  1584. */
  1585. iterator
  1586. erase(iterator __first, iterator __last);
  1587. #if __cplusplus >= 201103L
  1588. /**
  1589. * @brief Remove the last character.
  1590. *
  1591. * The string must be non-empty.
  1592. */
  1593. void
  1594. pop_back() // FIXME C++11: should be noexcept.
  1595. {
  1596. __glibcxx_assert(!empty());
  1597. erase(size() - 1, 1);
  1598. }
  1599. #endif // C++11
  1600. /**
  1601. * @brief Replace characters with value from another string.
  1602. * @param __pos Index of first character to replace.
  1603. * @param __n Number of characters to be replaced.
  1604. * @param __str String to insert.
  1605. * @return Reference to this string.
  1606. * @throw std::out_of_range If @a pos is beyond the end of this
  1607. * string.
  1608. * @throw std::length_error If new length exceeds @c max_size().
  1609. *
  1610. * Removes the characters in the range [__pos,__pos+__n) from
  1611. * this string. In place, the value of @a __str is inserted.
  1612. * If @a __pos is beyond end of string, out_of_range is thrown.
  1613. * If the length of the result exceeds max_size(), length_error
  1614. * is thrown. The value of the string doesn't change if an
  1615. * error is thrown.
  1616. */
  1617. basic_string&
  1618. replace(size_type __pos, size_type __n, const basic_string& __str)
  1619. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  1620. /**
  1621. * @brief Replace characters with value from another string.
  1622. * @param __pos1 Index of first character to replace.
  1623. * @param __n1 Number of characters to be replaced.
  1624. * @param __str String to insert.
  1625. * @param __pos2 Index of first character of str to use.
  1626. * @param __n2 Number of characters from str to use.
  1627. * @return Reference to this string.
  1628. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  1629. * __str.size().
  1630. * @throw std::length_error If new length exceeds @c max_size().
  1631. *
  1632. * Removes the characters in the range [__pos1,__pos1 + n) from this
  1633. * string. In place, the value of @a __str is inserted. If @a __pos is
  1634. * beyond end of string, out_of_range is thrown. If the length of the
  1635. * result exceeds max_size(), length_error is thrown. The value of the
  1636. * string doesn't change if an error is thrown.
  1637. */
  1638. basic_string&
  1639. replace(size_type __pos1, size_type __n1, const basic_string& __str,
  1640. size_type __pos2, size_type __n2 = npos)
  1641. { return this->replace(__pos1, __n1, __str._M_data()
  1642. + __str._M_check(__pos2, "basic_string::replace"),
  1643. __str._M_limit(__pos2, __n2)); }
  1644. /**
  1645. * @brief Replace characters with value of a C substring.
  1646. * @param __pos Index of first character to replace.
  1647. * @param __n1 Number of characters to be replaced.
  1648. * @param __s C string to insert.
  1649. * @param __n2 Number of characters from @a s to use.
  1650. * @return Reference to this string.
  1651. * @throw std::out_of_range If @a pos1 > size().
  1652. * @throw std::length_error If new length exceeds @c max_size().
  1653. *
  1654. * Removes the characters in the range [__pos,__pos + __n1)
  1655. * from this string. In place, the first @a __n2 characters of
  1656. * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
  1657. * @a __pos is beyond end of string, out_of_range is thrown. If
  1658. * the length of result exceeds max_size(), length_error is
  1659. * thrown. The value of the string doesn't change if an error
  1660. * is thrown.
  1661. */
  1662. basic_string&
  1663. replace(size_type __pos, size_type __n1, const _CharT* __s,
  1664. size_type __n2);
  1665. /**
  1666. * @brief Replace characters with value of a C string.
  1667. * @param __pos Index of first character to replace.
  1668. * @param __n1 Number of characters to be replaced.
  1669. * @param __s C string to insert.
  1670. * @return Reference to this string.
  1671. * @throw std::out_of_range If @a pos > size().
  1672. * @throw std::length_error If new length exceeds @c max_size().
  1673. *
  1674. * Removes the characters in the range [__pos,__pos + __n1)
  1675. * from this string. In place, the characters of @a __s are
  1676. * inserted. If @a __pos is beyond end of string, out_of_range
  1677. * is thrown. If the length of result exceeds max_size(),
  1678. * length_error is thrown. The value of the string doesn't
  1679. * change if an error is thrown.
  1680. */
  1681. basic_string&
  1682. replace(size_type __pos, size_type __n1, const _CharT* __s)
  1683. {
  1684. __glibcxx_requires_string(__s);
  1685. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  1686. }
  1687. /**
  1688. * @brief Replace characters with multiple characters.
  1689. * @param __pos Index of first character to replace.
  1690. * @param __n1 Number of characters to be replaced.
  1691. * @param __n2 Number of characters to insert.
  1692. * @param __c Character to insert.
  1693. * @return Reference to this string.
  1694. * @throw std::out_of_range If @a __pos > size().
  1695. * @throw std::length_error If new length exceeds @c max_size().
  1696. *
  1697. * Removes the characters in the range [pos,pos + n1) from this
  1698. * string. In place, @a __n2 copies of @a __c are inserted.
  1699. * If @a __pos is beyond end of string, out_of_range is thrown.
  1700. * If the length of result exceeds max_size(), length_error is
  1701. * thrown. The value of the string doesn't change if an error
  1702. * is thrown.
  1703. */
  1704. basic_string&
  1705. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  1706. { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
  1707. _M_limit(__pos, __n1), __n2, __c); }
  1708. /**
  1709. * @brief Replace range of characters with string.
  1710. * @param __i1 Iterator referencing start of range to replace.
  1711. * @param __i2 Iterator referencing end of range to replace.
  1712. * @param __str String value to insert.
  1713. * @return Reference to this string.
  1714. * @throw std::length_error If new length exceeds @c max_size().
  1715. *
  1716. * Removes the characters in the range [__i1,__i2). In place,
  1717. * the value of @a __str is inserted. If the length of result
  1718. * exceeds max_size(), length_error is thrown. The value of
  1719. * the string doesn't change if an error is thrown.
  1720. */
  1721. basic_string&
  1722. replace(iterator __i1, iterator __i2, const basic_string& __str)
  1723. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  1724. /**
  1725. * @brief Replace range of characters with C substring.
  1726. * @param __i1 Iterator referencing start of range to replace.
  1727. * @param __i2 Iterator referencing end of range to replace.
  1728. * @param __s C string value to insert.
  1729. * @param __n Number of characters from s to insert.
  1730. * @return Reference to this string.
  1731. * @throw std::length_error If new length exceeds @c max_size().
  1732. *
  1733. * Removes the characters in the range [__i1,__i2). In place,
  1734. * the first @a __n characters of @a __s are inserted. If the
  1735. * length of result exceeds max_size(), length_error is thrown.
  1736. * The value of the string doesn't change if an error is
  1737. * thrown.
  1738. */
  1739. basic_string&
  1740. replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
  1741. {
  1742. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1743. && __i2 <= _M_iend());
  1744. return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
  1745. }
  1746. /**
  1747. * @brief Replace range of characters with C string.
  1748. * @param __i1 Iterator referencing start of range to replace.
  1749. * @param __i2 Iterator referencing end of range to replace.
  1750. * @param __s C string value to insert.
  1751. * @return Reference to this string.
  1752. * @throw std::length_error If new length exceeds @c max_size().
  1753. *
  1754. * Removes the characters in the range [__i1,__i2). In place,
  1755. * the characters of @a __s are inserted. If the length of
  1756. * result exceeds max_size(), length_error is thrown. The
  1757. * value of the string doesn't change if an error is thrown.
  1758. */
  1759. basic_string&
  1760. replace(iterator __i1, iterator __i2, const _CharT* __s)
  1761. {
  1762. __glibcxx_requires_string(__s);
  1763. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  1764. }
  1765. /**
  1766. * @brief Replace range of characters with multiple characters
  1767. * @param __i1 Iterator referencing start of range to replace.
  1768. * @param __i2 Iterator referencing end of range to replace.
  1769. * @param __n Number of characters to insert.
  1770. * @param __c Character to insert.
  1771. * @return Reference to this string.
  1772. * @throw std::length_error If new length exceeds @c max_size().
  1773. *
  1774. * Removes the characters in the range [__i1,__i2). In place,
  1775. * @a __n copies of @a __c are inserted. If the length of
  1776. * result exceeds max_size(), length_error is thrown. The
  1777. * value of the string doesn't change if an error is thrown.
  1778. */
  1779. basic_string&
  1780. replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
  1781. {
  1782. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1783. && __i2 <= _M_iend());
  1784. return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
  1785. }
  1786. /**
  1787. * @brief Replace range of characters with range.
  1788. * @param __i1 Iterator referencing start of range to replace.
  1789. * @param __i2 Iterator referencing end of range to replace.
  1790. * @param __k1 Iterator referencing start of range to insert.
  1791. * @param __k2 Iterator referencing end of range to insert.
  1792. * @return Reference to this string.
  1793. * @throw std::length_error If new length exceeds @c max_size().
  1794. *
  1795. * Removes the characters in the range [__i1,__i2). In place,
  1796. * characters in the range [__k1,__k2) are inserted. If the
  1797. * length of result exceeds max_size(), length_error is thrown.
  1798. * The value of the string doesn't change if an error is
  1799. * thrown.
  1800. */
  1801. template<class _InputIterator>
  1802. basic_string&
  1803. replace(iterator __i1, iterator __i2,
  1804. _InputIterator __k1, _InputIterator __k2)
  1805. {
  1806. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1807. && __i2 <= _M_iend());
  1808. __glibcxx_requires_valid_range(__k1, __k2);
  1809. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  1810. return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  1811. }
  1812. // Specializations for the common case of pointer and iterator:
  1813. // useful to avoid the overhead of temporary buffering in _M_replace.
  1814. basic_string&
  1815. replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
  1816. {
  1817. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1818. && __i2 <= _M_iend());
  1819. __glibcxx_requires_valid_range(__k1, __k2);
  1820. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1821. __k1, __k2 - __k1);
  1822. }
  1823. basic_string&
  1824. replace(iterator __i1, iterator __i2,
  1825. const _CharT* __k1, const _CharT* __k2)
  1826. {
  1827. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1828. && __i2 <= _M_iend());
  1829. __glibcxx_requires_valid_range(__k1, __k2);
  1830. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1831. __k1, __k2 - __k1);
  1832. }
  1833. basic_string&
  1834. replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
  1835. {
  1836. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1837. && __i2 <= _M_iend());
  1838. __glibcxx_requires_valid_range(__k1, __k2);
  1839. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1840. __k1.base(), __k2 - __k1);
  1841. }
  1842. basic_string&
  1843. replace(iterator __i1, iterator __i2,
  1844. const_iterator __k1, const_iterator __k2)
  1845. {
  1846. _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
  1847. && __i2 <= _M_iend());
  1848. __glibcxx_requires_valid_range(__k1, __k2);
  1849. return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
  1850. __k1.base(), __k2 - __k1);
  1851. }
  1852. #if __cplusplus >= 201103L
  1853. /**
  1854. * @brief Replace range of characters with initializer_list.
  1855. * @param __i1 Iterator referencing start of range to replace.
  1856. * @param __i2 Iterator referencing end of range to replace.
  1857. * @param __l The initializer_list of characters to insert.
  1858. * @return Reference to this string.
  1859. * @throw std::length_error If new length exceeds @c max_size().
  1860. *
  1861. * Removes the characters in the range [__i1,__i2). In place,
  1862. * characters in the range [__k1,__k2) are inserted. If the
  1863. * length of result exceeds max_size(), length_error is thrown.
  1864. * The value of the string doesn't change if an error is
  1865. * thrown.
  1866. */
  1867. basic_string& replace(iterator __i1, iterator __i2,
  1868. initializer_list<_CharT> __l)
  1869. { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
  1870. #endif // C++11
  1871. #if __cplusplus >= 201703L
  1872. /**
  1873. * @brief Replace range of characters with string_view.
  1874. * @param __pos The position to replace at.
  1875. * @param __n The number of characters to replace.
  1876. * @param __svt The object convertible to string_view to insert.
  1877. * @return Reference to this string.
  1878. */
  1879. template<typename _Tp>
  1880. _If_sv<_Tp, basic_string&>
  1881. replace(size_type __pos, size_type __n, const _Tp& __svt)
  1882. {
  1883. __sv_type __sv = __svt;
  1884. return this->replace(__pos, __n, __sv.data(), __sv.size());
  1885. }
  1886. /**
  1887. * @brief Replace range of characters with string_view.
  1888. * @param __pos1 The position to replace at.
  1889. * @param __n1 The number of characters to replace.
  1890. * @param __svt The object convertible to string_view to insert from.
  1891. * @param __pos2 The position in the string_view to insert from.
  1892. * @param __n2 The number of characters to insert.
  1893. * @return Reference to this string.
  1894. */
  1895. template<typename _Tp>
  1896. _If_sv<_Tp, basic_string&>
  1897. replace(size_type __pos1, size_type __n1, const _Tp& __svt,
  1898. size_type __pos2, size_type __n2 = npos)
  1899. {
  1900. __sv_type __sv = __svt;
  1901. return this->replace(__pos1, __n1,
  1902. __sv.data()
  1903. + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
  1904. std::__sv_limit(__sv.size(), __pos2, __n2));
  1905. }
  1906. /**
  1907. * @brief Replace range of characters with string_view.
  1908. * @param __i1 An iterator referencing the start position
  1909. * to replace at.
  1910. * @param __i2 An iterator referencing the end position
  1911. * for the replace.
  1912. * @param __svt The object convertible to string_view to insert from.
  1913. * @return Reference to this string.
  1914. */
  1915. template<typename _Tp>
  1916. _If_sv<_Tp, basic_string&>
  1917. replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
  1918. {
  1919. __sv_type __sv = __svt;
  1920. return this->replace(__i1 - begin(), __i2 - __i1, __sv);
  1921. }
  1922. #endif // C++17
  1923. private:
  1924. template<class _Integer>
  1925. basic_string&
  1926. _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
  1927. _Integer __val, __true_type)
  1928. { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
  1929. template<class _InputIterator>
  1930. basic_string&
  1931. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  1932. _InputIterator __k2, __false_type);
  1933. basic_string&
  1934. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  1935. _CharT __c);
  1936. basic_string&
  1937. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  1938. size_type __n2);
  1939. // _S_construct_aux is used to implement the 21.3.1 para 15 which
  1940. // requires special behaviour if _InIter is an integral type
  1941. template<class _InIterator>
  1942. static _CharT*
  1943. _S_construct_aux(_InIterator __beg, _InIterator __end,
  1944. const _Alloc& __a, __false_type)
  1945. {
  1946. typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
  1947. return _S_construct(__beg, __end, __a, _Tag());
  1948. }
  1949. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1950. // 438. Ambiguity in the "do the right thing" clause
  1951. template<class _Integer>
  1952. static _CharT*
  1953. _S_construct_aux(_Integer __beg, _Integer __end,
  1954. const _Alloc& __a, __true_type)
  1955. { return _S_construct_aux_2(static_cast<size_type>(__beg),
  1956. __end, __a); }
  1957. static _CharT*
  1958. _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
  1959. { return _S_construct(__req, __c, __a); }
  1960. template<class _InIterator>
  1961. static _CharT*
  1962. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
  1963. {
  1964. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  1965. return _S_construct_aux(__beg, __end, __a, _Integral());
  1966. }
  1967. // For Input Iterators, used in istreambuf_iterators, etc.
  1968. template<class _InIterator>
  1969. static _CharT*
  1970. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  1971. input_iterator_tag);
  1972. // For forward_iterators up to random_access_iterators, used for
  1973. // string::iterator, _CharT*, etc.
  1974. template<class _FwdIterator>
  1975. static _CharT*
  1976. _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
  1977. forward_iterator_tag);
  1978. static _CharT*
  1979. _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
  1980. public:
  1981. /**
  1982. * @brief Copy substring into C string.
  1983. * @param __s C string to copy value into.
  1984. * @param __n Number of characters to copy.
  1985. * @param __pos Index of first character to copy.
  1986. * @return Number of characters actually copied
  1987. * @throw std::out_of_range If __pos > size().
  1988. *
  1989. * Copies up to @a __n characters starting at @a __pos into the
  1990. * C string @a __s. If @a __pos is %greater than size(),
  1991. * out_of_range is thrown.
  1992. */
  1993. size_type
  1994. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  1995. /**
  1996. * @brief Swap contents with another string.
  1997. * @param __s String to swap with.
  1998. *
  1999. * Exchanges the contents of this string with that of @a __s in constant
  2000. * time.
  2001. */
  2002. void
  2003. swap(basic_string& __s)
  2004. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
  2005. // String operations:
  2006. /**
  2007. * @brief Return const pointer to null-terminated contents.
  2008. *
  2009. * This is a handle to internal data. Do not modify or dire things may
  2010. * happen.
  2011. */
  2012. const _CharT*
  2013. c_str() const _GLIBCXX_NOEXCEPT
  2014. { return _M_data(); }
  2015. /**
  2016. * @brief Return const pointer to contents.
  2017. *
  2018. * This is a pointer to internal data. It is undefined to modify
  2019. * the contents through the returned pointer. To get a pointer that
  2020. * allows modifying the contents use @c &str[0] instead,
  2021. * (or in C++17 the non-const @c str.data() overload).
  2022. */
  2023. const _CharT*
  2024. data() const _GLIBCXX_NOEXCEPT
  2025. { return _M_data(); }
  2026. #if __cplusplus >= 201703L
  2027. /**
  2028. * @brief Return non-const pointer to contents.
  2029. *
  2030. * This is a pointer to the character sequence held by the string.
  2031. * Modifying the characters in the sequence is allowed.
  2032. */
  2033. _CharT*
  2034. data() noexcept
  2035. {
  2036. _M_leak();
  2037. return _M_data();
  2038. }
  2039. #endif
  2040. /**
  2041. * @brief Return copy of allocator used to construct this string.
  2042. */
  2043. allocator_type
  2044. get_allocator() const _GLIBCXX_NOEXCEPT
  2045. { return _M_dataplus; }
  2046. /**
  2047. * @brief Find position of a C substring.
  2048. * @param __s C string to locate.
  2049. * @param __pos Index of character to search from.
  2050. * @param __n Number of characters from @a s to search for.
  2051. * @return Index of start of first occurrence.
  2052. *
  2053. * Starting from @a __pos, searches forward for the first @a
  2054. * __n characters in @a __s within this string. If found,
  2055. * returns the index where it begins. If not found, returns
  2056. * npos.
  2057. */
  2058. size_type
  2059. find(const _CharT* __s, size_type __pos, size_type __n) const
  2060. _GLIBCXX_NOEXCEPT;
  2061. /**
  2062. * @brief Find position of a string.
  2063. * @param __str String to locate.
  2064. * @param __pos Index of character to search from (default 0).
  2065. * @return Index of start of first occurrence.
  2066. *
  2067. * Starting from @a __pos, searches forward for value of @a __str within
  2068. * this string. If found, returns the index where it begins. If not
  2069. * found, returns npos.
  2070. */
  2071. size_type
  2072. find(const basic_string& __str, size_type __pos = 0) const
  2073. _GLIBCXX_NOEXCEPT
  2074. { return this->find(__str.data(), __pos, __str.size()); }
  2075. /**
  2076. * @brief Find position of a C string.
  2077. * @param __s C string to locate.
  2078. * @param __pos Index of character to search from (default 0).
  2079. * @return Index of start of first occurrence.
  2080. *
  2081. * Starting from @a __pos, searches forward for the value of @a
  2082. * __s within this string. If found, returns the index where
  2083. * it begins. If not found, returns npos.
  2084. */
  2085. size_type
  2086. find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2087. {
  2088. __glibcxx_requires_string(__s);
  2089. return this->find(__s, __pos, traits_type::length(__s));
  2090. }
  2091. /**
  2092. * @brief Find position of a character.
  2093. * @param __c Character to locate.
  2094. * @param __pos Index of character to search from (default 0).
  2095. * @return Index of first occurrence.
  2096. *
  2097. * Starting from @a __pos, searches forward for @a __c within
  2098. * this string. If found, returns the index where it was
  2099. * found. If not found, returns npos.
  2100. */
  2101. size_type
  2102. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  2103. #if __cplusplus >= 201703L
  2104. /**
  2105. * @brief Find position of a string_view.
  2106. * @param __svt The object convertible to string_view to locate.
  2107. * @param __pos Index of character to search from (default 0).
  2108. * @return Index of start of first occurrence.
  2109. */
  2110. template<typename _Tp>
  2111. _If_sv<_Tp, size_type>
  2112. find(const _Tp& __svt, size_type __pos = 0) const
  2113. noexcept(is_same<_Tp, __sv_type>::value)
  2114. {
  2115. __sv_type __sv = __svt;
  2116. return this->find(__sv.data(), __pos, __sv.size());
  2117. }
  2118. #endif // C++17
  2119. /**
  2120. * @brief Find last position of a string.
  2121. * @param __str String to locate.
  2122. * @param __pos Index of character to search back from (default end).
  2123. * @return Index of start of last occurrence.
  2124. *
  2125. * Starting from @a __pos, searches backward for value of @a
  2126. * __str within this string. If found, returns the index where
  2127. * it begins. If not found, returns npos.
  2128. */
  2129. size_type
  2130. rfind(const basic_string& __str, size_type __pos = npos) const
  2131. _GLIBCXX_NOEXCEPT
  2132. { return this->rfind(__str.data(), __pos, __str.size()); }
  2133. /**
  2134. * @brief Find last position of a C substring.
  2135. * @param __s C string to locate.
  2136. * @param __pos Index of character to search back from.
  2137. * @param __n Number of characters from s to search for.
  2138. * @return Index of start of last occurrence.
  2139. *
  2140. * Starting from @a __pos, searches backward for the first @a
  2141. * __n characters in @a __s within this string. If found,
  2142. * returns the index where it begins. If not found, returns
  2143. * npos.
  2144. */
  2145. size_type
  2146. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  2147. _GLIBCXX_NOEXCEPT;
  2148. /**
  2149. * @brief Find last position of a C string.
  2150. * @param __s C string to locate.
  2151. * @param __pos Index of character to start search at (default end).
  2152. * @return Index of start of last occurrence.
  2153. *
  2154. * Starting from @a __pos, searches backward for the value of
  2155. * @a __s within this string. If found, returns the index
  2156. * where it begins. If not found, returns npos.
  2157. */
  2158. size_type
  2159. rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  2160. {
  2161. __glibcxx_requires_string(__s);
  2162. return this->rfind(__s, __pos, traits_type::length(__s));
  2163. }
  2164. /**
  2165. * @brief Find last position of a character.
  2166. * @param __c Character to locate.
  2167. * @param __pos Index of character to search back from (default end).
  2168. * @return Index of last occurrence.
  2169. *
  2170. * Starting from @a __pos, searches backward for @a __c within
  2171. * this string. If found, returns the index where it was
  2172. * found. If not found, returns npos.
  2173. */
  2174. size_type
  2175. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  2176. #if __cplusplus >= 201703L
  2177. /**
  2178. * @brief Find last position of a string_view.
  2179. * @param __svt The object convertible to string_view to locate.
  2180. * @param __pos Index of character to search back from (default end).
  2181. * @return Index of start of last occurrence.
  2182. */
  2183. template<typename _Tp>
  2184. _If_sv<_Tp, size_type>
  2185. rfind(const _Tp& __svt, size_type __pos = npos) const
  2186. noexcept(is_same<_Tp, __sv_type>::value)
  2187. {
  2188. __sv_type __sv = __svt;
  2189. return this->rfind(__sv.data(), __pos, __sv.size());
  2190. }
  2191. #endif // C++17
  2192. /**
  2193. * @brief Find position of a character of string.
  2194. * @param __str String containing characters to locate.
  2195. * @param __pos Index of character to search from (default 0).
  2196. * @return Index of first occurrence.
  2197. *
  2198. * Starting from @a __pos, searches forward for one of the
  2199. * characters of @a __str within this string. If found,
  2200. * returns the index where it was found. If not found, returns
  2201. * npos.
  2202. */
  2203. size_type
  2204. find_first_of(const basic_string& __str, size_type __pos = 0) const
  2205. _GLIBCXX_NOEXCEPT
  2206. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  2207. /**
  2208. * @brief Find position of a character of C substring.
  2209. * @param __s String containing characters to locate.
  2210. * @param __pos Index of character to search from.
  2211. * @param __n Number of characters from s to search for.
  2212. * @return Index of first occurrence.
  2213. *
  2214. * Starting from @a __pos, searches forward for one of the
  2215. * first @a __n characters of @a __s within this string. If
  2216. * found, returns the index where it was found. If not found,
  2217. * returns npos.
  2218. */
  2219. size_type
  2220. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  2221. _GLIBCXX_NOEXCEPT;
  2222. /**
  2223. * @brief Find position of a character of C string.
  2224. * @param __s String containing characters to locate.
  2225. * @param __pos Index of character to search from (default 0).
  2226. * @return Index of first occurrence.
  2227. *
  2228. * Starting from @a __pos, searches forward for one of the
  2229. * characters of @a __s within this string. If found, returns
  2230. * the index where it was found. If not found, returns npos.
  2231. */
  2232. size_type
  2233. find_first_of(const _CharT* __s, size_type __pos = 0) const
  2234. _GLIBCXX_NOEXCEPT
  2235. {
  2236. __glibcxx_requires_string(__s);
  2237. return this->find_first_of(__s, __pos, traits_type::length(__s));
  2238. }
  2239. /**
  2240. * @brief Find position of a character.
  2241. * @param __c Character to locate.
  2242. * @param __pos Index of character to search from (default 0).
  2243. * @return Index of first occurrence.
  2244. *
  2245. * Starting from @a __pos, searches forward for the character
  2246. * @a __c within this string. If found, returns the index
  2247. * where it was found. If not found, returns npos.
  2248. *
  2249. * Note: equivalent to find(__c, __pos).
  2250. */
  2251. size_type
  2252. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2253. { return this->find(__c, __pos); }
  2254. #if __cplusplus >= 201703L
  2255. /**
  2256. * @brief Find position of a character of a string_view.
  2257. * @param __svt An object convertible to string_view containing
  2258. * characters to locate.
  2259. * @param __pos Index of character to search from (default 0).
  2260. * @return Index of first occurrence.
  2261. */
  2262. template<typename _Tp>
  2263. _If_sv<_Tp, size_type>
  2264. find_first_of(const _Tp& __svt, size_type __pos = 0) const
  2265. noexcept(is_same<_Tp, __sv_type>::value)
  2266. {
  2267. __sv_type __sv = __svt;
  2268. return this->find_first_of(__sv.data(), __pos, __sv.size());
  2269. }
  2270. #endif // C++17
  2271. /**
  2272. * @brief Find last position of a character of string.
  2273. * @param __str String containing characters to locate.
  2274. * @param __pos Index of character to search back from (default end).
  2275. * @return Index of last occurrence.
  2276. *
  2277. * Starting from @a __pos, searches backward for one of the
  2278. * characters of @a __str within this string. If found,
  2279. * returns the index where it was found. If not found, returns
  2280. * npos.
  2281. */
  2282. size_type
  2283. find_last_of(const basic_string& __str, size_type __pos = npos) const
  2284. _GLIBCXX_NOEXCEPT
  2285. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  2286. /**
  2287. * @brief Find last position of a character of C substring.
  2288. * @param __s C string containing characters to locate.
  2289. * @param __pos Index of character to search back from.
  2290. * @param __n Number of characters from s to search for.
  2291. * @return Index of last occurrence.
  2292. *
  2293. * Starting from @a __pos, searches backward for one of the
  2294. * first @a __n characters of @a __s within this string. If
  2295. * found, returns the index where it was found. If not found,
  2296. * returns npos.
  2297. */
  2298. size_type
  2299. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  2300. _GLIBCXX_NOEXCEPT;
  2301. /**
  2302. * @brief Find last position of a character of C string.
  2303. * @param __s C string containing characters to locate.
  2304. * @param __pos Index of character to search back from (default end).
  2305. * @return Index of last occurrence.
  2306. *
  2307. * Starting from @a __pos, searches backward for one of the
  2308. * characters of @a __s within this string. If found, returns
  2309. * the index where it was found. If not found, returns npos.
  2310. */
  2311. size_type
  2312. find_last_of(const _CharT* __s, size_type __pos = npos) const
  2313. _GLIBCXX_NOEXCEPT
  2314. {
  2315. __glibcxx_requires_string(__s);
  2316. return this->find_last_of(__s, __pos, traits_type::length(__s));
  2317. }
  2318. /**
  2319. * @brief Find last position of a character.
  2320. * @param __c Character to locate.
  2321. * @param __pos Index of character to search back from (default end).
  2322. * @return Index of last occurrence.
  2323. *
  2324. * Starting from @a __pos, searches backward for @a __c within
  2325. * this string. If found, returns the index where it was
  2326. * found. If not found, returns npos.
  2327. *
  2328. * Note: equivalent to rfind(__c, __pos).
  2329. */
  2330. size_type
  2331. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  2332. { return this->rfind(__c, __pos); }
  2333. #if __cplusplus >= 201703L
  2334. /**
  2335. * @brief Find last position of a character of string.
  2336. * @param __svt An object convertible to string_view containing
  2337. * characters to locate.
  2338. * @param __pos Index of character to search back from (default end).
  2339. * @return Index of last occurrence.
  2340. */
  2341. template<typename _Tp>
  2342. _If_sv<_Tp, size_type>
  2343. find_last_of(const _Tp& __svt, size_type __pos = npos) const
  2344. noexcept(is_same<_Tp, __sv_type>::value)
  2345. {
  2346. __sv_type __sv = __svt;
  2347. return this->find_last_of(__sv.data(), __pos, __sv.size());
  2348. }
  2349. #endif // C++17
  2350. /**
  2351. * @brief Find position of a character not in string.
  2352. * @param __str String containing characters to avoid.
  2353. * @param __pos Index of character to search from (default 0).
  2354. * @return Index of first occurrence.
  2355. *
  2356. * Starting from @a __pos, searches forward for a character not contained
  2357. * in @a __str within this string. If found, returns the index where it
  2358. * was found. If not found, returns npos.
  2359. */
  2360. size_type
  2361. find_first_not_of(const basic_string& __str, size_type __pos = 0) const
  2362. _GLIBCXX_NOEXCEPT
  2363. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  2364. /**
  2365. * @brief Find position of a character not in C substring.
  2366. * @param __s C string containing characters to avoid.
  2367. * @param __pos Index of character to search from.
  2368. * @param __n Number of characters from __s to consider.
  2369. * @return Index of first occurrence.
  2370. *
  2371. * Starting from @a __pos, searches forward for a character not
  2372. * contained in the first @a __n characters of @a __s within
  2373. * this string. If found, returns the index where it was
  2374. * found. If not found, returns npos.
  2375. */
  2376. size_type
  2377. find_first_not_of(const _CharT* __s, size_type __pos,
  2378. size_type __n) const _GLIBCXX_NOEXCEPT;
  2379. /**
  2380. * @brief Find position of a character not in C string.
  2381. * @param __s C string containing characters to avoid.
  2382. * @param __pos Index of character to search from (default 0).
  2383. * @return Index of first occurrence.
  2384. *
  2385. * Starting from @a __pos, searches forward for a character not
  2386. * contained in @a __s within this string. If found, returns
  2387. * the index where it was found. If not found, returns npos.
  2388. */
  2389. size_type
  2390. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  2391. _GLIBCXX_NOEXCEPT
  2392. {
  2393. __glibcxx_requires_string(__s);
  2394. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  2395. }
  2396. /**
  2397. * @brief Find position of a different character.
  2398. * @param __c Character to avoid.
  2399. * @param __pos Index of character to search from (default 0).
  2400. * @return Index of first occurrence.
  2401. *
  2402. * Starting from @a __pos, searches forward for a character
  2403. * other than @a __c within this string. If found, returns the
  2404. * index where it was found. If not found, returns npos.
  2405. */
  2406. size_type
  2407. find_first_not_of(_CharT __c, size_type __pos = 0) const
  2408. _GLIBCXX_NOEXCEPT;
  2409. #if __cplusplus >= 201703L
  2410. /**
  2411. * @brief Find position of a character not in a string_view.
  2412. * @param __svt An object convertible to string_view containing
  2413. * characters to avoid.
  2414. * @param __pos Index of character to search from (default 0).
  2415. * @return Index of first occurrence.
  2416. */
  2417. template<typename _Tp>
  2418. _If_sv<_Tp, size_type>
  2419. find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
  2420. noexcept(is_same<_Tp, __sv_type>::value)
  2421. {
  2422. __sv_type __sv = __svt;
  2423. return this->find_first_not_of(__sv.data(), __pos, __sv.size());
  2424. }
  2425. #endif // C++17
  2426. /**
  2427. * @brief Find last position of a character not in string.
  2428. * @param __str String containing characters to avoid.
  2429. * @param __pos Index of character to search back from (default end).
  2430. * @return Index of last occurrence.
  2431. *
  2432. * Starting from @a __pos, searches backward for a character
  2433. * not contained in @a __str within this string. If found,
  2434. * returns the index where it was found. If not found, returns
  2435. * npos.
  2436. */
  2437. size_type
  2438. find_last_not_of(const basic_string& __str, size_type __pos = npos) const
  2439. _GLIBCXX_NOEXCEPT
  2440. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  2441. /**
  2442. * @brief Find last position of a character not in C substring.
  2443. * @param __s C string containing characters to avoid.
  2444. * @param __pos Index of character to search back from.
  2445. * @param __n Number of characters from s to consider.
  2446. * @return Index of last occurrence.
  2447. *
  2448. * Starting from @a __pos, searches backward for a character not
  2449. * contained in the first @a __n characters of @a __s within this string.
  2450. * If found, returns the index where it was found. If not found,
  2451. * returns npos.
  2452. */
  2453. size_type
  2454. find_last_not_of(const _CharT* __s, size_type __pos,
  2455. size_type __n) const _GLIBCXX_NOEXCEPT;
  2456. /**
  2457. * @brief Find last position of a character not in C string.
  2458. * @param __s C string containing characters to avoid.
  2459. * @param __pos Index of character to search back from (default end).
  2460. * @return Index of last occurrence.
  2461. *
  2462. * Starting from @a __pos, searches backward for a character
  2463. * not contained in @a __s within this string. If found,
  2464. * returns the index where it was found. If not found, returns
  2465. * npos.
  2466. */
  2467. size_type
  2468. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  2469. _GLIBCXX_NOEXCEPT
  2470. {
  2471. __glibcxx_requires_string(__s);
  2472. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  2473. }
  2474. /**
  2475. * @brief Find last position of a different character.
  2476. * @param __c Character to avoid.
  2477. * @param __pos Index of character to search back from (default end).
  2478. * @return Index of last occurrence.
  2479. *
  2480. * Starting from @a __pos, searches backward for a character other than
  2481. * @a __c within this string. If found, returns the index where it was
  2482. * found. If not found, returns npos.
  2483. */
  2484. size_type
  2485. find_last_not_of(_CharT __c, size_type __pos = npos) const
  2486. _GLIBCXX_NOEXCEPT;
  2487. #if __cplusplus >= 201703L
  2488. /**
  2489. * @brief Find last position of a character not in a string_view.
  2490. * @param __svt An object convertible to string_view containing
  2491. * characters to avoid.
  2492. * @param __pos Index of character to search back from (default end).
  2493. * @return Index of last occurrence.
  2494. */
  2495. template<typename _Tp>
  2496. _If_sv<_Tp, size_type>
  2497. find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
  2498. noexcept(is_same<_Tp, __sv_type>::value)
  2499. {
  2500. __sv_type __sv = __svt;
  2501. return this->find_last_not_of(__sv.data(), __pos, __sv.size());
  2502. }
  2503. #endif // C++17
  2504. /**
  2505. * @brief Get a substring.
  2506. * @param __pos Index of first character (default 0).
  2507. * @param __n Number of characters in substring (default remainder).
  2508. * @return The new string.
  2509. * @throw std::out_of_range If __pos > size().
  2510. *
  2511. * Construct and return a new string using the @a __n
  2512. * characters starting at @a __pos. If the string is too
  2513. * short, use the remainder of the characters. If @a __pos is
  2514. * beyond the end of the string, out_of_range is thrown.
  2515. */
  2516. basic_string
  2517. substr(size_type __pos = 0, size_type __n = npos) const
  2518. { return basic_string(*this,
  2519. _M_check(__pos, "basic_string::substr"), __n); }
  2520. /**
  2521. * @brief Compare to a string.
  2522. * @param __str String to compare against.
  2523. * @return Integer < 0, 0, or > 0.
  2524. *
  2525. * Returns an integer < 0 if this string is ordered before @a
  2526. * __str, 0 if their values are equivalent, or > 0 if this
  2527. * string is ordered after @a __str. Determines the effective
  2528. * length rlen of the strings to compare as the smallest of
  2529. * size() and str.size(). The function then compares the two
  2530. * strings by calling traits::compare(data(), str.data(),rlen).
  2531. * If the result of the comparison is nonzero returns it,
  2532. * otherwise the shorter one is ordered first.
  2533. */
  2534. int
  2535. compare(const basic_string& __str) const
  2536. {
  2537. const size_type __size = this->size();
  2538. const size_type __osize = __str.size();
  2539. const size_type __len = std::min(__size, __osize);
  2540. int __r = traits_type::compare(_M_data(), __str.data(), __len);
  2541. if (!__r)
  2542. __r = _S_compare(__size, __osize);
  2543. return __r;
  2544. }
  2545. #if __cplusplus >= 201703L
  2546. /**
  2547. * @brief Compare to a string_view.
  2548. * @param __svt An object convertible to string_view to compare against.
  2549. * @return Integer < 0, 0, or > 0.
  2550. */
  2551. template<typename _Tp>
  2552. _If_sv<_Tp, int>
  2553. compare(const _Tp& __svt) const
  2554. noexcept(is_same<_Tp, __sv_type>::value)
  2555. {
  2556. __sv_type __sv = __svt;
  2557. const size_type __size = this->size();
  2558. const size_type __osize = __sv.size();
  2559. const size_type __len = std::min(__size, __osize);
  2560. int __r = traits_type::compare(_M_data(), __sv.data(), __len);
  2561. if (!__r)
  2562. __r = _S_compare(__size, __osize);
  2563. return __r;
  2564. }
  2565. /**
  2566. * @brief Compare to a string_view.
  2567. * @param __pos A position in the string to start comparing from.
  2568. * @param __n The number of characters to compare.
  2569. * @param __svt An object convertible to string_view to compare
  2570. * against.
  2571. * @return Integer < 0, 0, or > 0.
  2572. */
  2573. template<typename _Tp>
  2574. _If_sv<_Tp, int>
  2575. compare(size_type __pos, size_type __n, const _Tp& __svt) const
  2576. noexcept(is_same<_Tp, __sv_type>::value)
  2577. {
  2578. __sv_type __sv = __svt;
  2579. return __sv_type(*this).substr(__pos, __n).compare(__sv);
  2580. }
  2581. /**
  2582. * @brief Compare to a string_view.
  2583. * @param __pos1 A position in the string to start comparing from.
  2584. * @param __n1 The number of characters to compare.
  2585. * @param __svt An object convertible to string_view to compare
  2586. * against.
  2587. * @param __pos2 A position in the string_view to start comparing from.
  2588. * @param __n2 The number of characters to compare.
  2589. * @return Integer < 0, 0, or > 0.
  2590. */
  2591. template<typename _Tp>
  2592. _If_sv<_Tp, int>
  2593. compare(size_type __pos1, size_type __n1, const _Tp& __svt,
  2594. size_type __pos2, size_type __n2 = npos) const
  2595. noexcept(is_same<_Tp, __sv_type>::value)
  2596. {
  2597. __sv_type __sv = __svt;
  2598. return __sv_type(*this)
  2599. .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  2600. }
  2601. #endif // C++17
  2602. /**
  2603. * @brief Compare substring to a string.
  2604. * @param __pos Index of first character of substring.
  2605. * @param __n Number of characters in substring.
  2606. * @param __str String to compare against.
  2607. * @return Integer < 0, 0, or > 0.
  2608. *
  2609. * Form the substring of this string from the @a __n characters
  2610. * starting at @a __pos. Returns an integer < 0 if the
  2611. * substring is ordered before @a __str, 0 if their values are
  2612. * equivalent, or > 0 if the substring is ordered after @a
  2613. * __str. Determines the effective length rlen of the strings
  2614. * to compare as the smallest of the length of the substring
  2615. * and @a __str.size(). The function then compares the two
  2616. * strings by calling
  2617. * traits::compare(substring.data(),str.data(),rlen). If the
  2618. * result of the comparison is nonzero returns it, otherwise
  2619. * the shorter one is ordered first.
  2620. */
  2621. int
  2622. compare(size_type __pos, size_type __n, const basic_string& __str) const;
  2623. /**
  2624. * @brief Compare substring to a substring.
  2625. * @param __pos1 Index of first character of substring.
  2626. * @param __n1 Number of characters in substring.
  2627. * @param __str String to compare against.
  2628. * @param __pos2 Index of first character of substring of str.
  2629. * @param __n2 Number of characters in substring of str.
  2630. * @return Integer < 0, 0, or > 0.
  2631. *
  2632. * Form the substring of this string from the @a __n1
  2633. * characters starting at @a __pos1. Form the substring of @a
  2634. * __str from the @a __n2 characters starting at @a __pos2.
  2635. * Returns an integer < 0 if this substring is ordered before
  2636. * the substring of @a __str, 0 if their values are equivalent,
  2637. * or > 0 if this substring is ordered after the substring of
  2638. * @a __str. Determines the effective length rlen of the
  2639. * strings to compare as the smallest of the lengths of the
  2640. * substrings. The function then compares the two strings by
  2641. * calling
  2642. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  2643. * If the result of the comparison is nonzero returns it,
  2644. * otherwise the shorter one is ordered first.
  2645. */
  2646. int
  2647. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  2648. size_type __pos2, size_type __n2 = npos) const;
  2649. /**
  2650. * @brief Compare to a C string.
  2651. * @param __s C string to compare against.
  2652. * @return Integer < 0, 0, or > 0.
  2653. *
  2654. * Returns an integer < 0 if this string is ordered before @a __s, 0 if
  2655. * their values are equivalent, or > 0 if this string is ordered after
  2656. * @a __s. Determines the effective length rlen of the strings to
  2657. * compare as the smallest of size() and the length of a string
  2658. * constructed from @a __s. The function then compares the two strings
  2659. * by calling traits::compare(data(),s,rlen). If the result of the
  2660. * comparison is nonzero returns it, otherwise the shorter one is
  2661. * ordered first.
  2662. */
  2663. int
  2664. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
  2665. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  2666. // 5 String::compare specification questionable
  2667. /**
  2668. * @brief Compare substring to a C string.
  2669. * @param __pos Index of first character of substring.
  2670. * @param __n1 Number of characters in substring.
  2671. * @param __s C string to compare against.
  2672. * @return Integer < 0, 0, or > 0.
  2673. *
  2674. * Form the substring of this string from the @a __n1
  2675. * characters starting at @a pos. Returns an integer < 0 if
  2676. * the substring is ordered before @a __s, 0 if their values
  2677. * are equivalent, or > 0 if the substring is ordered after @a
  2678. * __s. Determines the effective length rlen of the strings to
  2679. * compare as the smallest of the length of the substring and
  2680. * the length of a string constructed from @a __s. The
  2681. * function then compares the two string by calling
  2682. * traits::compare(substring.data(),__s,rlen). If the result of
  2683. * the comparison is nonzero returns it, otherwise the shorter
  2684. * one is ordered first.
  2685. */
  2686. int
  2687. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  2688. /**
  2689. * @brief Compare substring against a character %array.
  2690. * @param __pos Index of first character of substring.
  2691. * @param __n1 Number of characters in substring.
  2692. * @param __s character %array to compare against.
  2693. * @param __n2 Number of characters of s.
  2694. * @return Integer < 0, 0, or > 0.
  2695. *
  2696. * Form the substring of this string from the @a __n1
  2697. * characters starting at @a __pos. Form a string from the
  2698. * first @a __n2 characters of @a __s. Returns an integer < 0
  2699. * if this substring is ordered before the string from @a __s,
  2700. * 0 if their values are equivalent, or > 0 if this substring
  2701. * is ordered after the string from @a __s. Determines the
  2702. * effective length rlen of the strings to compare as the
  2703. * smallest of the length of the substring and @a __n2. The
  2704. * function then compares the two strings by calling
  2705. * traits::compare(substring.data(),s,rlen). If the result of
  2706. * the comparison is nonzero returns it, otherwise the shorter
  2707. * one is ordered first.
  2708. *
  2709. * NB: s must have at least n2 characters, &apos;\\0&apos; has
  2710. * no special meaning.
  2711. */
  2712. int
  2713. compare(size_type __pos, size_type __n1, const _CharT* __s,
  2714. size_type __n2) const;
  2715. #if __cplusplus > 201703L
  2716. bool
  2717. starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  2718. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2719. bool
  2720. starts_with(_CharT __x) const noexcept
  2721. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2722. bool
  2723. starts_with(const _CharT* __x) const noexcept
  2724. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  2725. bool
  2726. ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  2727. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2728. bool
  2729. ends_with(_CharT __x) const noexcept
  2730. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2731. bool
  2732. ends_with(const _CharT* __x) const noexcept
  2733. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  2734. #endif // C++20
  2735. #if __cplusplus > 202011L
  2736. bool
  2737. contains(basic_string_view<_CharT, _Traits> __x) const noexcept
  2738. { return __sv_type(this->data(), this->size()).contains(__x); }
  2739. bool
  2740. contains(_CharT __x) const noexcept
  2741. { return __sv_type(this->data(), this->size()).contains(__x); }
  2742. bool
  2743. contains(const _CharT* __x) const noexcept
  2744. { return __sv_type(this->data(), this->size()).contains(__x); }
  2745. #endif // C++23
  2746. # ifdef _GLIBCXX_TM_TS_INTERNAL
  2747. friend void
  2748. ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
  2749. void* exc);
  2750. friend const char*
  2751. ::_txnal_cow_string_c_str(const void *that);
  2752. friend void
  2753. ::_txnal_cow_string_D1(void *that);
  2754. friend void
  2755. ::_txnal_cow_string_D1_commit(void *that);
  2756. # endif
  2757. };
  2758. template<typename _CharT, typename _Traits, typename _Alloc>
  2759. const typename basic_string<_CharT, _Traits, _Alloc>::size_type
  2760. basic_string<_CharT, _Traits, _Alloc>::
  2761. _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
  2762. template<typename _CharT, typename _Traits, typename _Alloc>
  2763. const _CharT
  2764. basic_string<_CharT, _Traits, _Alloc>::
  2765. _Rep::_S_terminal = _CharT();
  2766. template<typename _CharT, typename _Traits, typename _Alloc>
  2767. const typename basic_string<_CharT, _Traits, _Alloc>::size_type
  2768. basic_string<_CharT, _Traits, _Alloc>::npos;
  2769. // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
  2770. // at static init time (before static ctors are run).
  2771. template<typename _CharT, typename _Traits, typename _Alloc>
  2772. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  2773. basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
  2774. (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
  2775. sizeof(size_type)];
  2776. // NB: This is the special case for Input Iterators, used in
  2777. // istreambuf_iterators, etc.
  2778. // Input Iterators have a cost structure very different from
  2779. // pointers, calling for a different coding style.
  2780. template<typename _CharT, typename _Traits, typename _Alloc>
  2781. template<typename _InIterator>
  2782. _CharT*
  2783. basic_string<_CharT, _Traits, _Alloc>::
  2784. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  2785. input_iterator_tag)
  2786. {
  2787. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  2788. if (__beg == __end && __a == _Alloc())
  2789. return _S_empty_rep()._M_refdata();
  2790. #endif
  2791. // Avoid reallocation for common case.
  2792. _CharT __buf[128];
  2793. size_type __len = 0;
  2794. while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
  2795. {
  2796. __buf[__len++] = *__beg;
  2797. ++__beg;
  2798. }
  2799. _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
  2800. _M_copy(__r->_M_refdata(), __buf, __len);
  2801. __try
  2802. {
  2803. while (__beg != __end)
  2804. {
  2805. if (__len == __r->_M_capacity)
  2806. {
  2807. // Allocate more space.
  2808. _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
  2809. _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
  2810. __r->_M_destroy(__a);
  2811. __r = __another;
  2812. }
  2813. __r->_M_refdata()[__len++] = *__beg;
  2814. ++__beg;
  2815. }
  2816. }
  2817. __catch(...)
  2818. {
  2819. __r->_M_destroy(__a);
  2820. __throw_exception_again;
  2821. }
  2822. __r->_M_set_length_and_sharable(__len);
  2823. return __r->_M_refdata();
  2824. }
  2825. template<typename _CharT, typename _Traits, typename _Alloc>
  2826. template <typename _InIterator>
  2827. _CharT*
  2828. basic_string<_CharT, _Traits, _Alloc>::
  2829. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  2830. forward_iterator_tag)
  2831. {
  2832. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  2833. if (__beg == __end && __a == _Alloc())
  2834. return _S_empty_rep()._M_refdata();
  2835. #endif
  2836. // NB: Not required, but considered best practice.
  2837. if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
  2838. __throw_logic_error(__N("basic_string::_S_construct null not valid"));
  2839. const size_type __dnew = static_cast<size_type>(std::distance(__beg,
  2840. __end));
  2841. // Check for out_of_range and length_error exceptions.
  2842. _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
  2843. __try
  2844. { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
  2845. __catch(...)
  2846. {
  2847. __r->_M_destroy(__a);
  2848. __throw_exception_again;
  2849. }
  2850. __r->_M_set_length_and_sharable(__dnew);
  2851. return __r->_M_refdata();
  2852. }
  2853. template<typename _CharT, typename _Traits, typename _Alloc>
  2854. _CharT*
  2855. basic_string<_CharT, _Traits, _Alloc>::
  2856. _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
  2857. {
  2858. #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
  2859. if (__n == 0 && __a == _Alloc())
  2860. return _S_empty_rep()._M_refdata();
  2861. #endif
  2862. // Check for out_of_range and length_error exceptions.
  2863. _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
  2864. if (__n)
  2865. _M_assign(__r->_M_refdata(), __n, __c);
  2866. __r->_M_set_length_and_sharable(__n);
  2867. return __r->_M_refdata();
  2868. }
  2869. template<typename _CharT, typename _Traits, typename _Alloc>
  2870. basic_string<_CharT, _Traits, _Alloc>::
  2871. basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a)
  2872. : _M_dataplus(_S_construct(__str._M_data()
  2873. + __str._M_check(__pos,
  2874. "basic_string::basic_string"),
  2875. __str._M_data() + __str._M_limit(__pos, npos)
  2876. + __pos, __a), __a)
  2877. { }
  2878. template<typename _CharT, typename _Traits, typename _Alloc>
  2879. basic_string<_CharT, _Traits, _Alloc>::
  2880. basic_string(const basic_string& __str, size_type __pos, size_type __n)
  2881. : _M_dataplus(_S_construct(__str._M_data()
  2882. + __str._M_check(__pos,
  2883. "basic_string::basic_string"),
  2884. __str._M_data() + __str._M_limit(__pos, __n)
  2885. + __pos, _Alloc()), _Alloc())
  2886. { }
  2887. template<typename _CharT, typename _Traits, typename _Alloc>
  2888. basic_string<_CharT, _Traits, _Alloc>::
  2889. basic_string(const basic_string& __str, size_type __pos,
  2890. size_type __n, const _Alloc& __a)
  2891. : _M_dataplus(_S_construct(__str._M_data()
  2892. + __str._M_check(__pos,
  2893. "basic_string::basic_string"),
  2894. __str._M_data() + __str._M_limit(__pos, __n)
  2895. + __pos, __a), __a)
  2896. { }
  2897. template<typename _CharT, typename _Traits, typename _Alloc>
  2898. basic_string<_CharT, _Traits, _Alloc>&
  2899. basic_string<_CharT, _Traits, _Alloc>::
  2900. assign(const basic_string& __str)
  2901. {
  2902. if (_M_rep() != __str._M_rep())
  2903. {
  2904. // XXX MT
  2905. const allocator_type __a = this->get_allocator();
  2906. _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
  2907. _M_rep()->_M_dispose(__a);
  2908. _M_data(__tmp);
  2909. }
  2910. return *this;
  2911. }
  2912. template<typename _CharT, typename _Traits, typename _Alloc>
  2913. basic_string<_CharT, _Traits, _Alloc>&
  2914. basic_string<_CharT, _Traits, _Alloc>::
  2915. assign(const _CharT* __s, size_type __n)
  2916. {
  2917. __glibcxx_requires_string_len(__s, __n);
  2918. _M_check_length(this->size(), __n, "basic_string::assign");
  2919. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  2920. return _M_replace_safe(size_type(0), this->size(), __s, __n);
  2921. else
  2922. {
  2923. // Work in-place.
  2924. const size_type __pos = __s - _M_data();
  2925. if (__pos >= __n)
  2926. _M_copy(_M_data(), __s, __n);
  2927. else if (__pos)
  2928. _M_move(_M_data(), __s, __n);
  2929. _M_rep()->_M_set_length_and_sharable(__n);
  2930. return *this;
  2931. }
  2932. }
  2933. template<typename _CharT, typename _Traits, typename _Alloc>
  2934. basic_string<_CharT, _Traits, _Alloc>&
  2935. basic_string<_CharT, _Traits, _Alloc>::
  2936. append(size_type __n, _CharT __c)
  2937. {
  2938. if (__n)
  2939. {
  2940. _M_check_length(size_type(0), __n, "basic_string::append");
  2941. const size_type __len = __n + this->size();
  2942. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  2943. this->reserve(__len);
  2944. _M_assign(_M_data() + this->size(), __n, __c);
  2945. _M_rep()->_M_set_length_and_sharable(__len);
  2946. }
  2947. return *this;
  2948. }
  2949. template<typename _CharT, typename _Traits, typename _Alloc>
  2950. basic_string<_CharT, _Traits, _Alloc>&
  2951. basic_string<_CharT, _Traits, _Alloc>::
  2952. append(const _CharT* __s, size_type __n)
  2953. {
  2954. __glibcxx_requires_string_len(__s, __n);
  2955. if (__n)
  2956. {
  2957. _M_check_length(size_type(0), __n, "basic_string::append");
  2958. const size_type __len = __n + this->size();
  2959. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  2960. {
  2961. if (_M_disjunct(__s))
  2962. this->reserve(__len);
  2963. else
  2964. {
  2965. const size_type __off = __s - _M_data();
  2966. this->reserve(__len);
  2967. __s = _M_data() + __off;
  2968. }
  2969. }
  2970. _M_copy(_M_data() + this->size(), __s, __n);
  2971. _M_rep()->_M_set_length_and_sharable(__len);
  2972. }
  2973. return *this;
  2974. }
  2975. template<typename _CharT, typename _Traits, typename _Alloc>
  2976. basic_string<_CharT, _Traits, _Alloc>&
  2977. basic_string<_CharT, _Traits, _Alloc>::
  2978. append(const basic_string& __str)
  2979. {
  2980. const size_type __size = __str.size();
  2981. if (__size)
  2982. {
  2983. const size_type __len = __size + this->size();
  2984. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  2985. this->reserve(__len);
  2986. _M_copy(_M_data() + this->size(), __str._M_data(), __size);
  2987. _M_rep()->_M_set_length_and_sharable(__len);
  2988. }
  2989. return *this;
  2990. }
  2991. template<typename _CharT, typename _Traits, typename _Alloc>
  2992. basic_string<_CharT, _Traits, _Alloc>&
  2993. basic_string<_CharT, _Traits, _Alloc>::
  2994. append(const basic_string& __str, size_type __pos, size_type __n)
  2995. {
  2996. __str._M_check(__pos, "basic_string::append");
  2997. __n = __str._M_limit(__pos, __n);
  2998. if (__n)
  2999. {
  3000. const size_type __len = __n + this->size();
  3001. if (__len > this->capacity() || _M_rep()->_M_is_shared())
  3002. this->reserve(__len);
  3003. _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
  3004. _M_rep()->_M_set_length_and_sharable(__len);
  3005. }
  3006. return *this;
  3007. }
  3008. template<typename _CharT, typename _Traits, typename _Alloc>
  3009. basic_string<_CharT, _Traits, _Alloc>&
  3010. basic_string<_CharT, _Traits, _Alloc>::
  3011. insert(size_type __pos, const _CharT* __s, size_type __n)
  3012. {
  3013. __glibcxx_requires_string_len(__s, __n);
  3014. _M_check(__pos, "basic_string::insert");
  3015. _M_check_length(size_type(0), __n, "basic_string::insert");
  3016. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  3017. return _M_replace_safe(__pos, size_type(0), __s, __n);
  3018. else
  3019. {
  3020. // Work in-place.
  3021. const size_type __off = __s - _M_data();
  3022. _M_mutate(__pos, 0, __n);
  3023. __s = _M_data() + __off;
  3024. _CharT* __p = _M_data() + __pos;
  3025. if (__s + __n <= __p)
  3026. _M_copy(__p, __s, __n);
  3027. else if (__s >= __p)
  3028. _M_copy(__p, __s + __n, __n);
  3029. else
  3030. {
  3031. const size_type __nleft = __p - __s;
  3032. _M_copy(__p, __s, __nleft);
  3033. _M_copy(__p + __nleft, __p + __n, __n - __nleft);
  3034. }
  3035. return *this;
  3036. }
  3037. }
  3038. template<typename _CharT, typename _Traits, typename _Alloc>
  3039. typename basic_string<_CharT, _Traits, _Alloc>::iterator
  3040. basic_string<_CharT, _Traits, _Alloc>::
  3041. erase(iterator __first, iterator __last)
  3042. {
  3043. _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
  3044. && __last <= _M_iend());
  3045. // NB: This isn't just an optimization (bail out early when
  3046. // there is nothing to do, really), it's also a correctness
  3047. // issue vs MT, see libstdc++/40518.
  3048. const size_type __size = __last - __first;
  3049. if (__size)
  3050. {
  3051. const size_type __pos = __first - _M_ibegin();
  3052. _M_mutate(__pos, __size, size_type(0));
  3053. _M_rep()->_M_set_leaked();
  3054. return iterator(_M_data() + __pos);
  3055. }
  3056. else
  3057. return __first;
  3058. }
  3059. template<typename _CharT, typename _Traits, typename _Alloc>
  3060. basic_string<_CharT, _Traits, _Alloc>&
  3061. basic_string<_CharT, _Traits, _Alloc>::
  3062. replace(size_type __pos, size_type __n1, const _CharT* __s,
  3063. size_type __n2)
  3064. {
  3065. __glibcxx_requires_string_len(__s, __n2);
  3066. _M_check(__pos, "basic_string::replace");
  3067. __n1 = _M_limit(__pos, __n1);
  3068. _M_check_length(__n1, __n2, "basic_string::replace");
  3069. bool __left;
  3070. if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
  3071. return _M_replace_safe(__pos, __n1, __s, __n2);
  3072. else if ((__left = __s + __n2 <= _M_data() + __pos)
  3073. || _M_data() + __pos + __n1 <= __s)
  3074. {
  3075. // Work in-place: non-overlapping case.
  3076. size_type __off = __s - _M_data();
  3077. __left ? __off : (__off += __n2 - __n1);
  3078. _M_mutate(__pos, __n1, __n2);
  3079. _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
  3080. return *this;
  3081. }
  3082. else
  3083. {
  3084. // Todo: overlapping case.
  3085. const basic_string __tmp(__s, __n2);
  3086. return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
  3087. }
  3088. }
  3089. template<typename _CharT, typename _Traits, typename _Alloc>
  3090. void
  3091. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  3092. _M_destroy(const _Alloc& __a) throw ()
  3093. {
  3094. const size_type __size = sizeof(_Rep_base)
  3095. + (this->_M_capacity + 1) * sizeof(_CharT);
  3096. _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
  3097. }
  3098. template<typename _CharT, typename _Traits, typename _Alloc>
  3099. void
  3100. basic_string<_CharT, _Traits, _Alloc>::
  3101. _M_leak_hard()
  3102. {
  3103. // No need to create a new copy of an empty string when a non-const
  3104. // reference/pointer/iterator into it is obtained. Modifying the
  3105. // trailing null character is undefined, so the ref/pointer/iterator
  3106. // is effectively const anyway.
  3107. if (this->empty())
  3108. return;
  3109. if (_M_rep()->_M_is_shared())
  3110. _M_mutate(0, 0, 0);
  3111. _M_rep()->_M_set_leaked();
  3112. }
  3113. template<typename _CharT, typename _Traits, typename _Alloc>
  3114. void
  3115. basic_string<_CharT, _Traits, _Alloc>::
  3116. _M_mutate(size_type __pos, size_type __len1, size_type __len2)
  3117. {
  3118. const size_type __old_size = this->size();
  3119. const size_type __new_size = __old_size + __len2 - __len1;
  3120. const size_type __how_much = __old_size - __pos - __len1;
  3121. if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
  3122. {
  3123. // Must reallocate.
  3124. const allocator_type __a = get_allocator();
  3125. _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
  3126. if (__pos)
  3127. _M_copy(__r->_M_refdata(), _M_data(), __pos);
  3128. if (__how_much)
  3129. _M_copy(__r->_M_refdata() + __pos + __len2,
  3130. _M_data() + __pos + __len1, __how_much);
  3131. _M_rep()->_M_dispose(__a);
  3132. _M_data(__r->_M_refdata());
  3133. }
  3134. else if (__how_much && __len1 != __len2)
  3135. {
  3136. // Work in-place.
  3137. _M_move(_M_data() + __pos + __len2,
  3138. _M_data() + __pos + __len1, __how_much);
  3139. }
  3140. _M_rep()->_M_set_length_and_sharable(__new_size);
  3141. }
  3142. template<typename _CharT, typename _Traits, typename _Alloc>
  3143. void
  3144. basic_string<_CharT, _Traits, _Alloc>::
  3145. reserve(size_type __res)
  3146. {
  3147. const size_type __capacity = capacity();
  3148. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3149. // 2968. Inconsistencies between basic_string reserve and
  3150. // vector/unordered_map/unordered_set reserve functions
  3151. // P0966 reserve should not shrink
  3152. if (__res <= __capacity)
  3153. {
  3154. if (!_M_rep()->_M_is_shared())
  3155. return;
  3156. // unshare, but keep same capacity
  3157. __res = __capacity;
  3158. }
  3159. const allocator_type __a = get_allocator();
  3160. _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
  3161. _M_rep()->_M_dispose(__a);
  3162. _M_data(__tmp);
  3163. }
  3164. template<typename _CharT, typename _Traits, typename _Alloc>
  3165. void
  3166. basic_string<_CharT, _Traits, _Alloc>::
  3167. swap(basic_string& __s)
  3168. _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
  3169. {
  3170. if (_M_rep()->_M_is_leaked())
  3171. _M_rep()->_M_set_sharable();
  3172. if (__s._M_rep()->_M_is_leaked())
  3173. __s._M_rep()->_M_set_sharable();
  3174. if (this->get_allocator() == __s.get_allocator())
  3175. {
  3176. _CharT* __tmp = _M_data();
  3177. _M_data(__s._M_data());
  3178. __s._M_data(__tmp);
  3179. }
  3180. // The code below can usually be optimized away.
  3181. else
  3182. {
  3183. const basic_string __tmp1(_M_ibegin(), _M_iend(),
  3184. __s.get_allocator());
  3185. const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
  3186. this->get_allocator());
  3187. *this = __tmp2;
  3188. __s = __tmp1;
  3189. }
  3190. }
  3191. template<typename _CharT, typename _Traits, typename _Alloc>
  3192. typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
  3193. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  3194. _S_create(size_type __capacity, size_type __old_capacity,
  3195. const _Alloc& __alloc)
  3196. {
  3197. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3198. // 83. String::npos vs. string::max_size()
  3199. if (__capacity > _S_max_size)
  3200. __throw_length_error(__N("basic_string::_S_create"));
  3201. // The standard places no restriction on allocating more memory
  3202. // than is strictly needed within this layer at the moment or as
  3203. // requested by an explicit application call to reserve(n).
  3204. // Many malloc implementations perform quite poorly when an
  3205. // application attempts to allocate memory in a stepwise fashion
  3206. // growing each allocation size by only 1 char. Additionally,
  3207. // it makes little sense to allocate less linear memory than the
  3208. // natural blocking size of the malloc implementation.
  3209. // Unfortunately, we would need a somewhat low-level calculation
  3210. // with tuned parameters to get this perfect for any particular
  3211. // malloc implementation. Fortunately, generalizations about
  3212. // common features seen among implementations seems to suffice.
  3213. // __pagesize need not match the actual VM page size for good
  3214. // results in practice, thus we pick a common value on the low
  3215. // side. __malloc_header_size is an estimate of the amount of
  3216. // overhead per memory allocation (in practice seen N * sizeof
  3217. // (void*) where N is 0, 2 or 4). According to folklore,
  3218. // picking this value on the high side is better than
  3219. // low-balling it (especially when this algorithm is used with
  3220. // malloc implementations that allocate memory blocks rounded up
  3221. // to a size which is a power of 2).
  3222. const size_type __pagesize = 4096;
  3223. const size_type __malloc_header_size = 4 * sizeof(void*);
  3224. // The below implements an exponential growth policy, necessary to
  3225. // meet amortized linear time requirements of the library: see
  3226. // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
  3227. // It's active for allocations requiring an amount of memory above
  3228. // system pagesize. This is consistent with the requirements of the
  3229. // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
  3230. if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
  3231. __capacity = 2 * __old_capacity;
  3232. // NB: Need an array of char_type[__capacity], plus a terminating
  3233. // null char_type() element, plus enough for the _Rep data structure.
  3234. // Whew. Seemingly so needy, yet so elemental.
  3235. size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  3236. const size_type __adj_size = __size + __malloc_header_size;
  3237. if (__adj_size > __pagesize && __capacity > __old_capacity)
  3238. {
  3239. const size_type __extra = __pagesize - __adj_size % __pagesize;
  3240. __capacity += __extra / sizeof(_CharT);
  3241. // Never allocate a string bigger than _S_max_size.
  3242. if (__capacity > _S_max_size)
  3243. __capacity = _S_max_size;
  3244. __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
  3245. }
  3246. // NB: Might throw, but no worries about a leak, mate: _Rep()
  3247. // does not throw.
  3248. void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
  3249. _Rep *__p = new (__place) _Rep;
  3250. __p->_M_capacity = __capacity;
  3251. // ABI compatibility - 3.4.x set in _S_create both
  3252. // _M_refcount and _M_length. All callers of _S_create
  3253. // in basic_string.tcc then set just _M_length.
  3254. // In 4.0.x and later both _M_refcount and _M_length
  3255. // are initialized in the callers, unfortunately we can
  3256. // have 3.4.x compiled code with _S_create callers inlined
  3257. // calling 4.0.x+ _S_create.
  3258. __p->_M_set_sharable();
  3259. return __p;
  3260. }
  3261. template<typename _CharT, typename _Traits, typename _Alloc>
  3262. _CharT*
  3263. basic_string<_CharT, _Traits, _Alloc>::_Rep::
  3264. _M_clone(const _Alloc& __alloc, size_type __res)
  3265. {
  3266. // Requested capacity of the clone.
  3267. const size_type __requested_cap = this->_M_length + __res;
  3268. _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
  3269. __alloc);
  3270. if (this->_M_length)
  3271. _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
  3272. __r->_M_set_length_and_sharable(this->_M_length);
  3273. return __r->_M_refdata();
  3274. }
  3275. template<typename _CharT, typename _Traits, typename _Alloc>
  3276. void
  3277. basic_string<_CharT, _Traits, _Alloc>::
  3278. resize(size_type __n, _CharT __c)
  3279. {
  3280. const size_type __size = this->size();
  3281. _M_check_length(__size, __n, "basic_string::resize");
  3282. if (__size < __n)
  3283. this->append(__n - __size, __c);
  3284. else if (__n < __size)
  3285. this->erase(__n);
  3286. // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
  3287. }
  3288. template<typename _CharT, typename _Traits, typename _Alloc>
  3289. template<typename _InputIterator>
  3290. basic_string<_CharT, _Traits, _Alloc>&
  3291. basic_string<_CharT, _Traits, _Alloc>::
  3292. _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
  3293. _InputIterator __k2, __false_type)
  3294. {
  3295. const basic_string __s(__k1, __k2);
  3296. const size_type __n1 = __i2 - __i1;
  3297. _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
  3298. return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
  3299. __s.size());
  3300. }
  3301. template<typename _CharT, typename _Traits, typename _Alloc>
  3302. basic_string<_CharT, _Traits, _Alloc>&
  3303. basic_string<_CharT, _Traits, _Alloc>::
  3304. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  3305. _CharT __c)
  3306. {
  3307. _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
  3308. _M_mutate(__pos1, __n1, __n2);
  3309. if (__n2)
  3310. _M_assign(_M_data() + __pos1, __n2, __c);
  3311. return *this;
  3312. }
  3313. template<typename _CharT, typename _Traits, typename _Alloc>
  3314. basic_string<_CharT, _Traits, _Alloc>&
  3315. basic_string<_CharT, _Traits, _Alloc>::
  3316. _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
  3317. size_type __n2)
  3318. {
  3319. _M_mutate(__pos1, __n1, __n2);
  3320. if (__n2)
  3321. _M_copy(_M_data() + __pos1, __s, __n2);
  3322. return *this;
  3323. }
  3324. template<typename _CharT, typename _Traits, typename _Alloc>
  3325. void
  3326. basic_string<_CharT, _Traits, _Alloc>::
  3327. reserve()
  3328. {
  3329. #if __cpp_exceptions
  3330. if (length() < capacity() || _M_rep()->_M_is_shared())
  3331. try
  3332. {
  3333. const allocator_type __a = get_allocator();
  3334. _CharT* __tmp = _M_rep()->_M_clone(__a);
  3335. _M_rep()->_M_dispose(__a);
  3336. _M_data(__tmp);
  3337. }
  3338. catch (const __cxxabiv1::__forced_unwind&)
  3339. { throw; }
  3340. catch (...)
  3341. { /* swallow the exception */ }
  3342. #endif
  3343. }
  3344. template<typename _CharT, typename _Traits, typename _Alloc>
  3345. typename basic_string<_CharT, _Traits, _Alloc>::size_type
  3346. basic_string<_CharT, _Traits, _Alloc>::
  3347. copy(_CharT* __s, size_type __n, size_type __pos) const
  3348. {
  3349. _M_check(__pos, "basic_string::copy");
  3350. __n = _M_limit(__pos, __n);
  3351. __glibcxx_requires_string_len(__s, __n);
  3352. if (__n)
  3353. _M_copy(__s, _M_data() + __pos, __n);
  3354. // 21.3.5.7 par 3: do not append null. (good.)
  3355. return __n;
  3356. }
  3357. _GLIBCXX_END_NAMESPACE_VERSION
  3358. } // namespace std
  3359. #endif // ! _GLIBCXX_USE_CXX11_ABI
  3360. #endif // _COW_STRING_H