basic_string.h 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382
  1. // Components for manipulating sequences of characters -*- 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/basic_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. //
  25. // ISO C++ 14882: 21 Strings library
  26. //
  27. #ifndef _BASIC_STRING_H
  28. #define _BASIC_STRING_H 1
  29. #pragma GCC system_header
  30. #include <ext/atomicity.h>
  31. #include <ext/alloc_traits.h>
  32. #include <debug/debug.h>
  33. #if __cplusplus >= 201103L
  34. #include <initializer_list>
  35. #endif
  36. #if __cplusplus >= 201703L
  37. # include <string_view>
  38. #endif
  39. #if ! _GLIBCXX_USE_CXX11_ABI
  40. # include "cow_string.h"
  41. #else
  42. namespace std _GLIBCXX_VISIBILITY(default)
  43. {
  44. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  45. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  46. #ifdef __cpp_lib_is_constant_evaluated
  47. // Support P0980R1 in C++20.
  48. # define __cpp_lib_constexpr_string 201907L
  49. #elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
  50. // Support P0426R1 changes to char_traits in C++17.
  51. # define __cpp_lib_constexpr_string 201611L
  52. #endif
  53. /**
  54. * @class basic_string basic_string.h <string>
  55. * @brief Managing sequences of characters and character-like objects.
  56. *
  57. * @ingroup strings
  58. * @ingroup sequences
  59. *
  60. * @tparam _CharT Type of character
  61. * @tparam _Traits Traits for character type, defaults to
  62. * char_traits<_CharT>.
  63. * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
  64. *
  65. * Meets the requirements of a <a href="tables.html#65">container</a>, a
  66. * <a href="tables.html#66">reversible container</a>, and a
  67. * <a href="tables.html#67">sequence</a>. Of the
  68. * <a href="tables.html#68">optional sequence requirements</a>, only
  69. * @c push_back, @c at, and @c %array access are supported.
  70. */
  71. template<typename _CharT, typename _Traits, typename _Alloc>
  72. class basic_string
  73. {
  74. typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
  75. rebind<_CharT>::other _Char_alloc_type;
  76. #if __cpp_lib_constexpr_string < 201907L
  77. typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
  78. #else
  79. template<typename _Traits2, typename _Dummy_for_PR85282>
  80. struct _Alloc_traits_impl : __gnu_cxx::__alloc_traits<_Char_alloc_type>
  81. {
  82. typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Base;
  83. [[__gnu__::__always_inline__]]
  84. static constexpr typename _Base::pointer
  85. allocate(_Char_alloc_type& __a, typename _Base::size_type __n)
  86. {
  87. pointer __p = _Base::allocate(__a, __n);
  88. if (std::is_constant_evaluated())
  89. // Begin the lifetime of characters in allocated storage.
  90. for (size_type __i = 0; __i < __n; ++__i)
  91. std::construct_at(__builtin_addressof(__p[__i]));
  92. return __p;
  93. }
  94. };
  95. template<typename _Dummy_for_PR85282>
  96. struct _Alloc_traits_impl<char_traits<_CharT>, _Dummy_for_PR85282>
  97. : __gnu_cxx::__alloc_traits<_Char_alloc_type>
  98. {
  99. // std::char_traits begins the lifetime of characters.
  100. };
  101. using _Alloc_traits = _Alloc_traits_impl<_Traits, void>;
  102. #endif
  103. // Types:
  104. public:
  105. typedef _Traits traits_type;
  106. typedef typename _Traits::char_type value_type;
  107. typedef _Char_alloc_type allocator_type;
  108. typedef typename _Alloc_traits::size_type size_type;
  109. typedef typename _Alloc_traits::difference_type difference_type;
  110. typedef typename _Alloc_traits::reference reference;
  111. typedef typename _Alloc_traits::const_reference const_reference;
  112. typedef typename _Alloc_traits::pointer pointer;
  113. typedef typename _Alloc_traits::const_pointer const_pointer;
  114. typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
  115. typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
  116. const_iterator;
  117. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  118. typedef std::reverse_iterator<iterator> reverse_iterator;
  119. /// Value returned by various member functions when they fail.
  120. static const size_type npos = static_cast<size_type>(-1);
  121. protected:
  122. // type used for positions in insert, erase etc.
  123. #if __cplusplus < 201103L
  124. typedef iterator __const_iterator;
  125. #else
  126. typedef const_iterator __const_iterator;
  127. #endif
  128. private:
  129. #if __cplusplus >= 201703L
  130. // A helper type for avoiding boiler-plate.
  131. typedef basic_string_view<_CharT, _Traits> __sv_type;
  132. template<typename _Tp, typename _Res>
  133. using _If_sv = enable_if_t<
  134. __and_<is_convertible<const _Tp&, __sv_type>,
  135. __not_<is_convertible<const _Tp*, const basic_string*>>,
  136. __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
  137. _Res>;
  138. // Allows an implicit conversion to __sv_type.
  139. _GLIBCXX20_CONSTEXPR
  140. static __sv_type
  141. _S_to_string_view(__sv_type __svt) noexcept
  142. { return __svt; }
  143. // Wraps a string_view by explicit conversion and thus
  144. // allows to add an internal constructor that does not
  145. // participate in overload resolution when a string_view
  146. // is provided.
  147. struct __sv_wrapper
  148. {
  149. _GLIBCXX20_CONSTEXPR explicit
  150. __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
  151. __sv_type _M_sv;
  152. };
  153. /**
  154. * @brief Only internally used: Construct string from a string view
  155. * wrapper.
  156. * @param __svw string view wrapper.
  157. * @param __a Allocator to use.
  158. */
  159. _GLIBCXX20_CONSTEXPR
  160. explicit
  161. basic_string(__sv_wrapper __svw, const _Alloc& __a)
  162. : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
  163. #endif
  164. // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
  165. struct _Alloc_hider : allocator_type // TODO check __is_final
  166. {
  167. #if __cplusplus < 201103L
  168. _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
  169. : allocator_type(__a), _M_p(__dat) { }
  170. #else
  171. _GLIBCXX20_CONSTEXPR
  172. _Alloc_hider(pointer __dat, const _Alloc& __a)
  173. : allocator_type(__a), _M_p(__dat) { }
  174. _GLIBCXX20_CONSTEXPR
  175. _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
  176. : allocator_type(std::move(__a)), _M_p(__dat) { }
  177. #endif
  178. pointer _M_p; // The actual data.
  179. };
  180. _Alloc_hider _M_dataplus;
  181. size_type _M_string_length;
  182. enum { _S_local_capacity = 15 / sizeof(_CharT) };
  183. union
  184. {
  185. _CharT _M_local_buf[_S_local_capacity + 1];
  186. size_type _M_allocated_capacity;
  187. };
  188. _GLIBCXX20_CONSTEXPR
  189. void
  190. _M_data(pointer __p)
  191. { _M_dataplus._M_p = __p; }
  192. _GLIBCXX20_CONSTEXPR
  193. void
  194. _M_length(size_type __length)
  195. { _M_string_length = __length; }
  196. _GLIBCXX20_CONSTEXPR
  197. pointer
  198. _M_data() const
  199. { return _M_dataplus._M_p; }
  200. _GLIBCXX20_CONSTEXPR
  201. pointer
  202. _M_local_data()
  203. {
  204. #if __cplusplus >= 201103L
  205. return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
  206. #else
  207. return pointer(_M_local_buf);
  208. #endif
  209. }
  210. _GLIBCXX20_CONSTEXPR
  211. const_pointer
  212. _M_local_data() const
  213. {
  214. #if __cplusplus >= 201103L
  215. return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
  216. #else
  217. return const_pointer(_M_local_buf);
  218. #endif
  219. }
  220. _GLIBCXX20_CONSTEXPR
  221. void
  222. _M_capacity(size_type __capacity)
  223. { _M_allocated_capacity = __capacity; }
  224. _GLIBCXX20_CONSTEXPR
  225. void
  226. _M_set_length(size_type __n)
  227. {
  228. _M_length(__n);
  229. traits_type::assign(_M_data()[__n], _CharT());
  230. }
  231. _GLIBCXX20_CONSTEXPR
  232. bool
  233. _M_is_local() const
  234. { return _M_data() == _M_local_data(); }
  235. // Create & Destroy
  236. _GLIBCXX20_CONSTEXPR
  237. pointer
  238. _M_create(size_type&, size_type);
  239. _GLIBCXX20_CONSTEXPR
  240. void
  241. _M_dispose()
  242. {
  243. if (!_M_is_local())
  244. _M_destroy(_M_allocated_capacity);
  245. }
  246. _GLIBCXX20_CONSTEXPR
  247. void
  248. _M_destroy(size_type __size) throw()
  249. { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
  250. #if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  251. // _M_construct_aux is used to implement the 21.3.1 para 15 which
  252. // requires special behaviour if _InIterator is an integral type
  253. template<typename _InIterator>
  254. void
  255. _M_construct_aux(_InIterator __beg, _InIterator __end,
  256. std::__false_type)
  257. {
  258. typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
  259. _M_construct(__beg, __end, _Tag());
  260. }
  261. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  262. // 438. Ambiguity in the "do the right thing" clause
  263. template<typename _Integer>
  264. void
  265. _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
  266. { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
  267. void
  268. _M_construct_aux_2(size_type __req, _CharT __c)
  269. { _M_construct(__req, __c); }
  270. #endif
  271. // For Input Iterators, used in istreambuf_iterators, etc.
  272. template<typename _InIterator>
  273. _GLIBCXX20_CONSTEXPR
  274. void
  275. _M_construct(_InIterator __beg, _InIterator __end,
  276. std::input_iterator_tag);
  277. // For forward_iterators up to random_access_iterators, used for
  278. // string::iterator, _CharT*, etc.
  279. template<typename _FwdIterator>
  280. _GLIBCXX20_CONSTEXPR
  281. void
  282. _M_construct(_FwdIterator __beg, _FwdIterator __end,
  283. std::forward_iterator_tag);
  284. _GLIBCXX20_CONSTEXPR
  285. void
  286. _M_construct(size_type __req, _CharT __c);
  287. _GLIBCXX20_CONSTEXPR
  288. allocator_type&
  289. _M_get_allocator()
  290. { return _M_dataplus; }
  291. _GLIBCXX20_CONSTEXPR
  292. const allocator_type&
  293. _M_get_allocator() const
  294. { return _M_dataplus; }
  295. // Ensure that _M_local_buf is the active member of the union.
  296. __attribute__((__always_inline__))
  297. _GLIBCXX14_CONSTEXPR
  298. pointer
  299. _M_use_local_data() _GLIBCXX_NOEXCEPT
  300. {
  301. #if __cpp_lib_is_constant_evaluated
  302. if (std::is_constant_evaluated())
  303. _M_local_buf[0] = _CharT();
  304. #endif
  305. return _M_local_data();
  306. }
  307. private:
  308. #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
  309. // The explicit instantiations in misc-inst.cc require this due to
  310. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
  311. template<typename _Tp, bool _Requires =
  312. !__are_same<_Tp, _CharT*>::__value
  313. && !__are_same<_Tp, const _CharT*>::__value
  314. && !__are_same<_Tp, iterator>::__value
  315. && !__are_same<_Tp, const_iterator>::__value>
  316. struct __enable_if_not_native_iterator
  317. { typedef basic_string& __type; };
  318. template<typename _Tp>
  319. struct __enable_if_not_native_iterator<_Tp, false> { };
  320. #endif
  321. _GLIBCXX20_CONSTEXPR
  322. size_type
  323. _M_check(size_type __pos, const char* __s) const
  324. {
  325. if (__pos > this->size())
  326. __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
  327. "this->size() (which is %zu)"),
  328. __s, __pos, this->size());
  329. return __pos;
  330. }
  331. _GLIBCXX20_CONSTEXPR
  332. void
  333. _M_check_length(size_type __n1, size_type __n2, const char* __s) const
  334. {
  335. if (this->max_size() - (this->size() - __n1) < __n2)
  336. __throw_length_error(__N(__s));
  337. }
  338. // NB: _M_limit doesn't check for a bad __pos value.
  339. _GLIBCXX20_CONSTEXPR
  340. size_type
  341. _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
  342. {
  343. const bool __testoff = __off < this->size() - __pos;
  344. return __testoff ? __off : this->size() - __pos;
  345. }
  346. // True if _Rep and source do not overlap.
  347. bool
  348. _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
  349. {
  350. return (less<const _CharT*>()(__s, _M_data())
  351. || less<const _CharT*>()(_M_data() + this->size(), __s));
  352. }
  353. // When __n = 1 way faster than the general multichar
  354. // traits_type::copy/move/assign.
  355. _GLIBCXX20_CONSTEXPR
  356. static void
  357. _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
  358. {
  359. if (__n == 1)
  360. traits_type::assign(*__d, *__s);
  361. else
  362. traits_type::copy(__d, __s, __n);
  363. }
  364. _GLIBCXX20_CONSTEXPR
  365. static void
  366. _S_move(_CharT* __d, const _CharT* __s, size_type __n)
  367. {
  368. if (__n == 1)
  369. traits_type::assign(*__d, *__s);
  370. else
  371. traits_type::move(__d, __s, __n);
  372. }
  373. _GLIBCXX20_CONSTEXPR
  374. static void
  375. _S_assign(_CharT* __d, size_type __n, _CharT __c)
  376. {
  377. if (__n == 1)
  378. traits_type::assign(*__d, __c);
  379. else
  380. traits_type::assign(__d, __n, __c);
  381. }
  382. // _S_copy_chars is a separate template to permit specialization
  383. // to optimize for the common case of pointers as iterators.
  384. template<class _Iterator>
  385. _GLIBCXX20_CONSTEXPR
  386. static void
  387. _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
  388. {
  389. for (; __k1 != __k2; ++__k1, (void)++__p)
  390. traits_type::assign(*__p, *__k1); // These types are off.
  391. }
  392. _GLIBCXX20_CONSTEXPR
  393. static void
  394. _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
  395. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  396. _GLIBCXX20_CONSTEXPR
  397. static void
  398. _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
  399. _GLIBCXX_NOEXCEPT
  400. { _S_copy_chars(__p, __k1.base(), __k2.base()); }
  401. _GLIBCXX20_CONSTEXPR
  402. static void
  403. _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
  404. { _S_copy(__p, __k1, __k2 - __k1); }
  405. _GLIBCXX20_CONSTEXPR
  406. static void
  407. _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
  408. _GLIBCXX_NOEXCEPT
  409. { _S_copy(__p, __k1, __k2 - __k1); }
  410. _GLIBCXX20_CONSTEXPR
  411. static int
  412. _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
  413. {
  414. const difference_type __d = difference_type(__n1 - __n2);
  415. if (__d > __gnu_cxx::__numeric_traits<int>::__max)
  416. return __gnu_cxx::__numeric_traits<int>::__max;
  417. else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
  418. return __gnu_cxx::__numeric_traits<int>::__min;
  419. else
  420. return int(__d);
  421. }
  422. _GLIBCXX20_CONSTEXPR
  423. void
  424. _M_assign(const basic_string&);
  425. _GLIBCXX20_CONSTEXPR
  426. void
  427. _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
  428. size_type __len2);
  429. _GLIBCXX20_CONSTEXPR
  430. void
  431. _M_erase(size_type __pos, size_type __n);
  432. public:
  433. // Construct/copy/destroy:
  434. // NB: We overload ctors in some cases instead of using default
  435. // arguments, per 17.4.4.4 para. 2 item 2.
  436. /**
  437. * @brief Default constructor creates an empty string.
  438. */
  439. _GLIBCXX20_CONSTEXPR
  440. basic_string()
  441. _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
  442. : _M_dataplus(_M_local_data())
  443. {
  444. _M_use_local_data();
  445. _M_set_length(0);
  446. }
  447. /**
  448. * @brief Construct an empty string using allocator @a a.
  449. */
  450. _GLIBCXX20_CONSTEXPR
  451. explicit
  452. basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
  453. : _M_dataplus(_M_local_data(), __a)
  454. {
  455. _M_use_local_data();
  456. _M_set_length(0);
  457. }
  458. /**
  459. * @brief Construct string with copy of value of @a __str.
  460. * @param __str Source string.
  461. */
  462. _GLIBCXX20_CONSTEXPR
  463. basic_string(const basic_string& __str)
  464. : _M_dataplus(_M_local_data(),
  465. _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
  466. {
  467. _M_construct(__str._M_data(), __str._M_data() + __str.length(),
  468. std::forward_iterator_tag());
  469. }
  470. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  471. // 2583. no way to supply an allocator for basic_string(str, pos)
  472. /**
  473. * @brief Construct string as copy of a substring.
  474. * @param __str Source string.
  475. * @param __pos Index of first character to copy from.
  476. * @param __a Allocator to use.
  477. */
  478. _GLIBCXX20_CONSTEXPR
  479. basic_string(const basic_string& __str, size_type __pos,
  480. const _Alloc& __a = _Alloc())
  481. : _M_dataplus(_M_local_data(), __a)
  482. {
  483. const _CharT* __start = __str._M_data()
  484. + __str._M_check(__pos, "basic_string::basic_string");
  485. _M_construct(__start, __start + __str._M_limit(__pos, npos),
  486. std::forward_iterator_tag());
  487. }
  488. /**
  489. * @brief Construct string as copy of a substring.
  490. * @param __str Source string.
  491. * @param __pos Index of first character to copy from.
  492. * @param __n Number of characters to copy.
  493. */
  494. _GLIBCXX20_CONSTEXPR
  495. basic_string(const basic_string& __str, size_type __pos,
  496. size_type __n)
  497. : _M_dataplus(_M_local_data())
  498. {
  499. const _CharT* __start = __str._M_data()
  500. + __str._M_check(__pos, "basic_string::basic_string");
  501. _M_construct(__start, __start + __str._M_limit(__pos, __n),
  502. std::forward_iterator_tag());
  503. }
  504. /**
  505. * @brief Construct string as copy of a substring.
  506. * @param __str Source string.
  507. * @param __pos Index of first character to copy from.
  508. * @param __n Number of characters to copy.
  509. * @param __a Allocator to use.
  510. */
  511. _GLIBCXX20_CONSTEXPR
  512. basic_string(const basic_string& __str, size_type __pos,
  513. size_type __n, const _Alloc& __a)
  514. : _M_dataplus(_M_local_data(), __a)
  515. {
  516. const _CharT* __start
  517. = __str._M_data() + __str._M_check(__pos, "string::string");
  518. _M_construct(__start, __start + __str._M_limit(__pos, __n),
  519. std::forward_iterator_tag());
  520. }
  521. /**
  522. * @brief Construct string initialized by a character %array.
  523. * @param __s Source character %array.
  524. * @param __n Number of characters to copy.
  525. * @param __a Allocator to use (default is default allocator).
  526. *
  527. * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
  528. * has no special meaning.
  529. */
  530. _GLIBCXX20_CONSTEXPR
  531. basic_string(const _CharT* __s, size_type __n,
  532. const _Alloc& __a = _Alloc())
  533. : _M_dataplus(_M_local_data(), __a)
  534. {
  535. // NB: Not required, but considered best practice.
  536. if (__s == 0 && __n > 0)
  537. std::__throw_logic_error(__N("basic_string: "
  538. "construction from null is not valid"));
  539. _M_construct(__s, __s + __n, std::forward_iterator_tag());
  540. }
  541. /**
  542. * @brief Construct string as copy of a C string.
  543. * @param __s Source C string.
  544. * @param __a Allocator to use (default is default allocator).
  545. */
  546. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  547. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  548. // 3076. basic_string CTAD ambiguity
  549. template<typename = _RequireAllocator<_Alloc>>
  550. #endif
  551. _GLIBCXX20_CONSTEXPR
  552. basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
  553. : _M_dataplus(_M_local_data(), __a)
  554. {
  555. // NB: Not required, but considered best practice.
  556. if (__s == 0)
  557. std::__throw_logic_error(__N("basic_string: "
  558. "construction from null is not valid"));
  559. const _CharT* __end = __s + traits_type::length(__s);
  560. _M_construct(__s, __end, forward_iterator_tag());
  561. }
  562. /**
  563. * @brief Construct string as multiple characters.
  564. * @param __n Number of characters.
  565. * @param __c Character to use.
  566. * @param __a Allocator to use (default is default allocator).
  567. */
  568. #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  569. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  570. // 3076. basic_string CTAD ambiguity
  571. template<typename = _RequireAllocator<_Alloc>>
  572. #endif
  573. _GLIBCXX20_CONSTEXPR
  574. basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
  575. : _M_dataplus(_M_local_data(), __a)
  576. { _M_construct(__n, __c); }
  577. #if __cplusplus >= 201103L
  578. /**
  579. * @brief Move construct string.
  580. * @param __str Source string.
  581. *
  582. * The newly-created string contains the exact contents of @a __str.
  583. * @a __str is a valid, but unspecified string.
  584. */
  585. _GLIBCXX20_CONSTEXPR
  586. basic_string(basic_string&& __str) noexcept
  587. : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
  588. {
  589. if (__str._M_is_local())
  590. {
  591. traits_type::copy(_M_local_buf, __str._M_local_buf,
  592. __str.length() + 1);
  593. }
  594. else
  595. {
  596. _M_data(__str._M_data());
  597. _M_capacity(__str._M_allocated_capacity);
  598. }
  599. // Must use _M_length() here not _M_set_length() because
  600. // basic_stringbuf relies on writing into unallocated capacity so
  601. // we mess up the contents if we put a '\0' in the string.
  602. _M_length(__str.length());
  603. __str._M_data(__str._M_local_data());
  604. __str._M_set_length(0);
  605. }
  606. /**
  607. * @brief Construct string from an initializer %list.
  608. * @param __l std::initializer_list of characters.
  609. * @param __a Allocator to use (default is default allocator).
  610. */
  611. _GLIBCXX20_CONSTEXPR
  612. basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
  613. : _M_dataplus(_M_local_data(), __a)
  614. { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); }
  615. _GLIBCXX20_CONSTEXPR
  616. basic_string(const basic_string& __str, const _Alloc& __a)
  617. : _M_dataplus(_M_local_data(), __a)
  618. { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); }
  619. _GLIBCXX20_CONSTEXPR
  620. basic_string(basic_string&& __str, const _Alloc& __a)
  621. noexcept(_Alloc_traits::_S_always_equal())
  622. : _M_dataplus(_M_local_data(), __a)
  623. {
  624. if (__str._M_is_local())
  625. {
  626. traits_type::copy(_M_local_buf, __str._M_local_buf,
  627. __str.length() + 1);
  628. _M_length(__str.length());
  629. __str._M_set_length(0);
  630. }
  631. else if (_Alloc_traits::_S_always_equal()
  632. || __str.get_allocator() == __a)
  633. {
  634. _M_data(__str._M_data());
  635. _M_length(__str.length());
  636. _M_capacity(__str._M_allocated_capacity);
  637. __str._M_data(__str._M_local_buf);
  638. __str._M_set_length(0);
  639. }
  640. else
  641. _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
  642. }
  643. #endif // C++11
  644. #if __cplusplus >= 202100L
  645. basic_string(nullptr_t) = delete;
  646. basic_string& operator=(nullptr_t) = delete;
  647. #endif // C++23
  648. /**
  649. * @brief Construct string as copy of a range.
  650. * @param __beg Start of range.
  651. * @param __end End of range.
  652. * @param __a Allocator to use (default is default allocator).
  653. */
  654. #if __cplusplus >= 201103L
  655. template<typename _InputIterator,
  656. typename = std::_RequireInputIter<_InputIterator>>
  657. #else
  658. template<typename _InputIterator>
  659. #endif
  660. _GLIBCXX20_CONSTEXPR
  661. basic_string(_InputIterator __beg, _InputIterator __end,
  662. const _Alloc& __a = _Alloc())
  663. : _M_dataplus(_M_local_data(), __a)
  664. {
  665. #if __cplusplus >= 201103L
  666. _M_construct(__beg, __end, std::__iterator_category(__beg));
  667. #else
  668. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  669. _M_construct_aux(__beg, __end, _Integral());
  670. #endif
  671. }
  672. #if __cplusplus >= 201703L
  673. /**
  674. * @brief Construct string from a substring of a string_view.
  675. * @param __t Source object convertible to string view.
  676. * @param __pos The index of the first character to copy from __t.
  677. * @param __n The number of characters to copy from __t.
  678. * @param __a Allocator to use.
  679. */
  680. template<typename _Tp,
  681. typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>>
  682. _GLIBCXX20_CONSTEXPR
  683. basic_string(const _Tp& __t, size_type __pos, size_type __n,
  684. const _Alloc& __a = _Alloc())
  685. : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
  686. /**
  687. * @brief Construct string from a string_view.
  688. * @param __t Source object convertible to string view.
  689. * @param __a Allocator to use (default is default allocator).
  690. */
  691. template<typename _Tp, typename = _If_sv<_Tp, void>>
  692. _GLIBCXX20_CONSTEXPR
  693. explicit
  694. basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
  695. : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
  696. #endif // C++17
  697. /**
  698. * @brief Destroy the string instance.
  699. */
  700. _GLIBCXX20_CONSTEXPR
  701. ~basic_string()
  702. { _M_dispose(); }
  703. /**
  704. * @brief Assign the value of @a str to this string.
  705. * @param __str Source string.
  706. */
  707. _GLIBCXX20_CONSTEXPR
  708. basic_string&
  709. operator=(const basic_string& __str)
  710. {
  711. return this->assign(__str);
  712. }
  713. /**
  714. * @brief Copy contents of @a s into this string.
  715. * @param __s Source null-terminated string.
  716. */
  717. _GLIBCXX20_CONSTEXPR
  718. basic_string&
  719. operator=(const _CharT* __s)
  720. { return this->assign(__s); }
  721. /**
  722. * @brief Set value to string of length 1.
  723. * @param __c Source character.
  724. *
  725. * Assigning to a character makes this string length 1 and
  726. * (*this)[0] == @a c.
  727. */
  728. _GLIBCXX20_CONSTEXPR
  729. basic_string&
  730. operator=(_CharT __c)
  731. {
  732. this->assign(1, __c);
  733. return *this;
  734. }
  735. #if __cplusplus >= 201103L
  736. /**
  737. * @brief Move assign the value of @a str to this string.
  738. * @param __str Source string.
  739. *
  740. * The contents of @a str are moved into this string (without copying).
  741. * @a str is a valid, but unspecified string.
  742. */
  743. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  744. // 2063. Contradictory requirements for string move assignment
  745. _GLIBCXX20_CONSTEXPR
  746. basic_string&
  747. operator=(basic_string&& __str)
  748. noexcept(_Alloc_traits::_S_nothrow_move())
  749. {
  750. if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
  751. && !_Alloc_traits::_S_always_equal()
  752. && _M_get_allocator() != __str._M_get_allocator())
  753. {
  754. // Destroy existing storage before replacing allocator.
  755. _M_destroy(_M_allocated_capacity);
  756. _M_data(_M_local_data());
  757. _M_set_length(0);
  758. }
  759. // Replace allocator if POCMA is true.
  760. std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
  761. if (__str._M_is_local())
  762. {
  763. // We've always got room for a short string, just copy it
  764. // (unless this is a self-move, because that would violate the
  765. // char_traits::copy precondition that the ranges don't overlap).
  766. if (__builtin_expect(std::__addressof(__str) != this, true))
  767. {
  768. if (__str.size())
  769. this->_S_copy(_M_data(), __str._M_data(), __str.size());
  770. _M_set_length(__str.size());
  771. }
  772. }
  773. else if (_Alloc_traits::_S_propagate_on_move_assign()
  774. || _Alloc_traits::_S_always_equal()
  775. || _M_get_allocator() == __str._M_get_allocator())
  776. {
  777. // Just move the allocated pointer, our allocator can free it.
  778. pointer __data = nullptr;
  779. size_type __capacity;
  780. if (!_M_is_local())
  781. {
  782. if (_Alloc_traits::_S_always_equal())
  783. {
  784. // __str can reuse our existing storage.
  785. __data = _M_data();
  786. __capacity = _M_allocated_capacity;
  787. }
  788. else // __str can't use it, so free it.
  789. _M_destroy(_M_allocated_capacity);
  790. }
  791. _M_data(__str._M_data());
  792. _M_length(__str.length());
  793. _M_capacity(__str._M_allocated_capacity);
  794. if (__data)
  795. {
  796. __str._M_data(__data);
  797. __str._M_capacity(__capacity);
  798. }
  799. else
  800. __str._M_data(__str._M_local_buf);
  801. }
  802. else // Need to do a deep copy
  803. assign(__str);
  804. __str.clear();
  805. return *this;
  806. }
  807. /**
  808. * @brief Set value to string constructed from initializer %list.
  809. * @param __l std::initializer_list.
  810. */
  811. _GLIBCXX20_CONSTEXPR
  812. basic_string&
  813. operator=(initializer_list<_CharT> __l)
  814. {
  815. this->assign(__l.begin(), __l.size());
  816. return *this;
  817. }
  818. #endif // C++11
  819. #if __cplusplus >= 201703L
  820. /**
  821. * @brief Set value to string constructed from a string_view.
  822. * @param __svt An object convertible to string_view.
  823. */
  824. template<typename _Tp>
  825. _GLIBCXX20_CONSTEXPR
  826. _If_sv<_Tp, basic_string&>
  827. operator=(const _Tp& __svt)
  828. { return this->assign(__svt); }
  829. /**
  830. * @brief Convert to a string_view.
  831. * @return A string_view.
  832. */
  833. _GLIBCXX20_CONSTEXPR
  834. operator __sv_type() const noexcept
  835. { return __sv_type(data(), size()); }
  836. #endif // C++17
  837. // Iterators:
  838. /**
  839. * Returns a read/write iterator that points to the first character in
  840. * the %string.
  841. */
  842. _GLIBCXX20_CONSTEXPR
  843. iterator
  844. begin() _GLIBCXX_NOEXCEPT
  845. { return iterator(_M_data()); }
  846. /**
  847. * Returns a read-only (constant) iterator that points to the first
  848. * character in the %string.
  849. */
  850. _GLIBCXX20_CONSTEXPR
  851. const_iterator
  852. begin() const _GLIBCXX_NOEXCEPT
  853. { return const_iterator(_M_data()); }
  854. /**
  855. * Returns a read/write iterator that points one past the last
  856. * character in the %string.
  857. */
  858. _GLIBCXX20_CONSTEXPR
  859. iterator
  860. end() _GLIBCXX_NOEXCEPT
  861. { return iterator(_M_data() + this->size()); }
  862. /**
  863. * Returns a read-only (constant) iterator that points one past the
  864. * last character in the %string.
  865. */
  866. _GLIBCXX20_CONSTEXPR
  867. const_iterator
  868. end() const _GLIBCXX_NOEXCEPT
  869. { return const_iterator(_M_data() + this->size()); }
  870. /**
  871. * Returns a read/write reverse iterator that points to the last
  872. * character in the %string. Iteration is done in reverse element
  873. * order.
  874. */
  875. _GLIBCXX20_CONSTEXPR
  876. reverse_iterator
  877. rbegin() _GLIBCXX_NOEXCEPT
  878. { return reverse_iterator(this->end()); }
  879. /**
  880. * Returns a read-only (constant) reverse iterator that points
  881. * to the last character in the %string. Iteration is done in
  882. * reverse element order.
  883. */
  884. _GLIBCXX20_CONSTEXPR
  885. const_reverse_iterator
  886. rbegin() const _GLIBCXX_NOEXCEPT
  887. { return const_reverse_iterator(this->end()); }
  888. /**
  889. * Returns a read/write reverse iterator that points to one before the
  890. * first character in the %string. Iteration is done in reverse
  891. * element order.
  892. */
  893. _GLIBCXX20_CONSTEXPR
  894. reverse_iterator
  895. rend() _GLIBCXX_NOEXCEPT
  896. { return reverse_iterator(this->begin()); }
  897. /**
  898. * Returns a read-only (constant) reverse iterator that points
  899. * to one before the first character in the %string. Iteration
  900. * is done in reverse element order.
  901. */
  902. _GLIBCXX20_CONSTEXPR
  903. const_reverse_iterator
  904. rend() const _GLIBCXX_NOEXCEPT
  905. { return const_reverse_iterator(this->begin()); }
  906. #if __cplusplus >= 201103L
  907. /**
  908. * Returns a read-only (constant) iterator that points to the first
  909. * character in the %string.
  910. */
  911. _GLIBCXX20_CONSTEXPR
  912. const_iterator
  913. cbegin() const noexcept
  914. { return const_iterator(this->_M_data()); }
  915. /**
  916. * Returns a read-only (constant) iterator that points one past the
  917. * last character in the %string.
  918. */
  919. _GLIBCXX20_CONSTEXPR
  920. const_iterator
  921. cend() const noexcept
  922. { return const_iterator(this->_M_data() + this->size()); }
  923. /**
  924. * Returns a read-only (constant) reverse iterator that points
  925. * to the last character in the %string. Iteration is done in
  926. * reverse element order.
  927. */
  928. _GLIBCXX20_CONSTEXPR
  929. const_reverse_iterator
  930. crbegin() const noexcept
  931. { return const_reverse_iterator(this->end()); }
  932. /**
  933. * Returns a read-only (constant) reverse iterator that points
  934. * to one before the first character in the %string. Iteration
  935. * is done in reverse element order.
  936. */
  937. _GLIBCXX20_CONSTEXPR
  938. const_reverse_iterator
  939. crend() const noexcept
  940. { return const_reverse_iterator(this->begin()); }
  941. #endif
  942. public:
  943. // Capacity:
  944. /// Returns the number of characters in the string, not including any
  945. /// null-termination.
  946. _GLIBCXX20_CONSTEXPR
  947. size_type
  948. size() const _GLIBCXX_NOEXCEPT
  949. { return _M_string_length; }
  950. /// Returns the number of characters in the string, not including any
  951. /// null-termination.
  952. _GLIBCXX20_CONSTEXPR
  953. size_type
  954. length() const _GLIBCXX_NOEXCEPT
  955. { return _M_string_length; }
  956. /// Returns the size() of the largest possible %string.
  957. _GLIBCXX20_CONSTEXPR
  958. size_type
  959. max_size() const _GLIBCXX_NOEXCEPT
  960. { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
  961. /**
  962. * @brief Resizes the %string to the specified number of characters.
  963. * @param __n Number of characters the %string should contain.
  964. * @param __c Character to fill any new elements.
  965. *
  966. * This function will %resize the %string to the specified
  967. * number of characters. If the number is smaller than the
  968. * %string's current size the %string is truncated, otherwise
  969. * the %string is extended and new elements are %set to @a __c.
  970. */
  971. _GLIBCXX20_CONSTEXPR
  972. void
  973. resize(size_type __n, _CharT __c);
  974. /**
  975. * @brief Resizes the %string to the specified number of characters.
  976. * @param __n Number of characters the %string should contain.
  977. *
  978. * This function will resize the %string to the specified length. If
  979. * the new size is smaller than the %string's current size the %string
  980. * is truncated, otherwise the %string is extended and new characters
  981. * are default-constructed. For basic types such as char, this means
  982. * setting them to 0.
  983. */
  984. _GLIBCXX20_CONSTEXPR
  985. void
  986. resize(size_type __n)
  987. { this->resize(__n, _CharT()); }
  988. #if __cplusplus >= 201103L
  989. #pragma GCC diagnostic push
  990. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  991. /// A non-binding request to reduce capacity() to size().
  992. _GLIBCXX20_CONSTEXPR
  993. void
  994. shrink_to_fit() noexcept
  995. { reserve(); }
  996. #pragma GCC diagnostic pop
  997. #endif
  998. #if __cplusplus > 202002L
  999. #define __cpp_lib_string_resize_and_overwrite 202110L
  1000. template<typename _Operation>
  1001. constexpr void
  1002. resize_and_overwrite(size_type __n, _Operation __op);
  1003. #endif
  1004. /**
  1005. * Returns the total number of characters that the %string can hold
  1006. * before needing to allocate more memory.
  1007. */
  1008. _GLIBCXX20_CONSTEXPR
  1009. size_type
  1010. capacity() const _GLIBCXX_NOEXCEPT
  1011. {
  1012. return _M_is_local() ? size_type(_S_local_capacity)
  1013. : _M_allocated_capacity;
  1014. }
  1015. /**
  1016. * @brief Attempt to preallocate enough memory for specified number of
  1017. * characters.
  1018. * @param __res_arg Number of characters required.
  1019. * @throw std::length_error If @a __res_arg exceeds @c max_size().
  1020. *
  1021. * This function attempts to reserve enough memory for the
  1022. * %string to hold the specified number of characters. If the
  1023. * number requested is more than max_size(), length_error is
  1024. * thrown.
  1025. *
  1026. * The advantage of this function is that if optimal code is a
  1027. * necessity and the user can determine the string length that will be
  1028. * required, the user can reserve the memory in %advance, and thus
  1029. * prevent a possible reallocation of memory and copying of %string
  1030. * data.
  1031. */
  1032. _GLIBCXX20_CONSTEXPR
  1033. void
  1034. reserve(size_type __res_arg);
  1035. /**
  1036. * Equivalent to shrink_to_fit().
  1037. */
  1038. #if __cplusplus > 201703L
  1039. [[deprecated("use shrink_to_fit() instead")]]
  1040. #endif
  1041. _GLIBCXX20_CONSTEXPR
  1042. void
  1043. reserve();
  1044. /**
  1045. * Erases the string, making it empty.
  1046. */
  1047. _GLIBCXX20_CONSTEXPR
  1048. void
  1049. clear() _GLIBCXX_NOEXCEPT
  1050. { _M_set_length(0); }
  1051. /**
  1052. * Returns true if the %string is empty. Equivalent to
  1053. * <code>*this == ""</code>.
  1054. */
  1055. _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
  1056. bool
  1057. empty() const _GLIBCXX_NOEXCEPT
  1058. { return this->size() == 0; }
  1059. // Element access:
  1060. /**
  1061. * @brief Subscript access to the data contained in the %string.
  1062. * @param __pos The index of the character to access.
  1063. * @return Read-only (constant) reference to the character.
  1064. *
  1065. * This operator allows for easy, array-style, data access.
  1066. * Note that data access with this operator is unchecked and
  1067. * out_of_range lookups are not defined. (For checked lookups
  1068. * see at().)
  1069. */
  1070. _GLIBCXX20_CONSTEXPR
  1071. const_reference
  1072. operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
  1073. {
  1074. __glibcxx_assert(__pos <= size());
  1075. return _M_data()[__pos];
  1076. }
  1077. /**
  1078. * @brief Subscript access to the data contained in the %string.
  1079. * @param __pos The index of the character to access.
  1080. * @return Read/write reference to the character.
  1081. *
  1082. * This operator allows for easy, array-style, data access.
  1083. * Note that data access with this operator is unchecked and
  1084. * out_of_range lookups are not defined. (For checked lookups
  1085. * see at().)
  1086. */
  1087. _GLIBCXX20_CONSTEXPR
  1088. reference
  1089. operator[](size_type __pos)
  1090. {
  1091. // Allow pos == size() both in C++98 mode, as v3 extension,
  1092. // and in C++11 mode.
  1093. __glibcxx_assert(__pos <= size());
  1094. // In pedantic mode be strict in C++98 mode.
  1095. _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
  1096. return _M_data()[__pos];
  1097. }
  1098. /**
  1099. * @brief Provides access to the data contained in the %string.
  1100. * @param __n The index of the character to access.
  1101. * @return Read-only (const) reference to the character.
  1102. * @throw std::out_of_range If @a n is an invalid index.
  1103. *
  1104. * This function provides for safer data access. The parameter is
  1105. * first checked that it is in the range of the string. The function
  1106. * throws out_of_range if the check fails.
  1107. */
  1108. _GLIBCXX20_CONSTEXPR
  1109. const_reference
  1110. at(size_type __n) const
  1111. {
  1112. if (__n >= this->size())
  1113. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  1114. "(which is %zu) >= this->size() "
  1115. "(which is %zu)"),
  1116. __n, this->size());
  1117. return _M_data()[__n];
  1118. }
  1119. /**
  1120. * @brief Provides access to the data contained in the %string.
  1121. * @param __n The index of the character to access.
  1122. * @return Read/write reference to the character.
  1123. * @throw std::out_of_range If @a n is an invalid index.
  1124. *
  1125. * This function provides for safer data access. The parameter is
  1126. * first checked that it is in the range of the string. The function
  1127. * throws out_of_range if the check fails.
  1128. */
  1129. _GLIBCXX20_CONSTEXPR
  1130. reference
  1131. at(size_type __n)
  1132. {
  1133. if (__n >= size())
  1134. __throw_out_of_range_fmt(__N("basic_string::at: __n "
  1135. "(which is %zu) >= this->size() "
  1136. "(which is %zu)"),
  1137. __n, this->size());
  1138. return _M_data()[__n];
  1139. }
  1140. #if __cplusplus >= 201103L
  1141. /**
  1142. * Returns a read/write reference to the data at the first
  1143. * element of the %string.
  1144. */
  1145. _GLIBCXX20_CONSTEXPR
  1146. reference
  1147. front() noexcept
  1148. {
  1149. __glibcxx_assert(!empty());
  1150. return operator[](0);
  1151. }
  1152. /**
  1153. * Returns a read-only (constant) reference to the data at the first
  1154. * element of the %string.
  1155. */
  1156. _GLIBCXX20_CONSTEXPR
  1157. const_reference
  1158. front() const noexcept
  1159. {
  1160. __glibcxx_assert(!empty());
  1161. return operator[](0);
  1162. }
  1163. /**
  1164. * Returns a read/write reference to the data at the last
  1165. * element of the %string.
  1166. */
  1167. _GLIBCXX20_CONSTEXPR
  1168. reference
  1169. back() noexcept
  1170. {
  1171. __glibcxx_assert(!empty());
  1172. return operator[](this->size() - 1);
  1173. }
  1174. /**
  1175. * Returns a read-only (constant) reference to the data at the
  1176. * last element of the %string.
  1177. */
  1178. _GLIBCXX20_CONSTEXPR
  1179. const_reference
  1180. back() const noexcept
  1181. {
  1182. __glibcxx_assert(!empty());
  1183. return operator[](this->size() - 1);
  1184. }
  1185. #endif
  1186. // Modifiers:
  1187. /**
  1188. * @brief Append a string to this string.
  1189. * @param __str The string to append.
  1190. * @return Reference to this string.
  1191. */
  1192. _GLIBCXX20_CONSTEXPR
  1193. basic_string&
  1194. operator+=(const basic_string& __str)
  1195. { return this->append(__str); }
  1196. /**
  1197. * @brief Append a C string.
  1198. * @param __s The C string to append.
  1199. * @return Reference to this string.
  1200. */
  1201. _GLIBCXX20_CONSTEXPR
  1202. basic_string&
  1203. operator+=(const _CharT* __s)
  1204. { return this->append(__s); }
  1205. /**
  1206. * @brief Append a character.
  1207. * @param __c The character to append.
  1208. * @return Reference to this string.
  1209. */
  1210. _GLIBCXX20_CONSTEXPR
  1211. basic_string&
  1212. operator+=(_CharT __c)
  1213. {
  1214. this->push_back(__c);
  1215. return *this;
  1216. }
  1217. #if __cplusplus >= 201103L
  1218. /**
  1219. * @brief Append an initializer_list of characters.
  1220. * @param __l The initializer_list of characters to be appended.
  1221. * @return Reference to this string.
  1222. */
  1223. _GLIBCXX20_CONSTEXPR
  1224. basic_string&
  1225. operator+=(initializer_list<_CharT> __l)
  1226. { return this->append(__l.begin(), __l.size()); }
  1227. #endif // C++11
  1228. #if __cplusplus >= 201703L
  1229. /**
  1230. * @brief Append a string_view.
  1231. * @param __svt An object convertible to string_view to be appended.
  1232. * @return Reference to this string.
  1233. */
  1234. template<typename _Tp>
  1235. _GLIBCXX20_CONSTEXPR
  1236. _If_sv<_Tp, basic_string&>
  1237. operator+=(const _Tp& __svt)
  1238. { return this->append(__svt); }
  1239. #endif // C++17
  1240. /**
  1241. * @brief Append a string to this string.
  1242. * @param __str The string to append.
  1243. * @return Reference to this string.
  1244. */
  1245. _GLIBCXX20_CONSTEXPR
  1246. basic_string&
  1247. append(const basic_string& __str)
  1248. { return this->append(__str._M_data(), __str.size()); }
  1249. /**
  1250. * @brief Append a substring.
  1251. * @param __str The string to append.
  1252. * @param __pos Index of the first character of str to append.
  1253. * @param __n The number of characters to append.
  1254. * @return Reference to this string.
  1255. * @throw std::out_of_range if @a __pos is not a valid index.
  1256. *
  1257. * This function appends @a __n characters from @a __str
  1258. * starting at @a __pos to this string. If @a __n is is larger
  1259. * than the number of available characters in @a __str, the
  1260. * remainder of @a __str is appended.
  1261. */
  1262. _GLIBCXX20_CONSTEXPR
  1263. basic_string&
  1264. append(const basic_string& __str, size_type __pos, size_type __n = npos)
  1265. { return this->append(__str._M_data()
  1266. + __str._M_check(__pos, "basic_string::append"),
  1267. __str._M_limit(__pos, __n)); }
  1268. /**
  1269. * @brief Append a C substring.
  1270. * @param __s The C string to append.
  1271. * @param __n The number of characters to append.
  1272. * @return Reference to this string.
  1273. */
  1274. _GLIBCXX20_CONSTEXPR
  1275. basic_string&
  1276. append(const _CharT* __s, size_type __n)
  1277. {
  1278. __glibcxx_requires_string_len(__s, __n);
  1279. _M_check_length(size_type(0), __n, "basic_string::append");
  1280. return _M_append(__s, __n);
  1281. }
  1282. /**
  1283. * @brief Append a C string.
  1284. * @param __s The C string to append.
  1285. * @return Reference to this string.
  1286. */
  1287. _GLIBCXX20_CONSTEXPR
  1288. basic_string&
  1289. append(const _CharT* __s)
  1290. {
  1291. __glibcxx_requires_string(__s);
  1292. const size_type __n = traits_type::length(__s);
  1293. _M_check_length(size_type(0), __n, "basic_string::append");
  1294. return _M_append(__s, __n);
  1295. }
  1296. /**
  1297. * @brief Append multiple characters.
  1298. * @param __n The number of characters to append.
  1299. * @param __c The character to use.
  1300. * @return Reference to this string.
  1301. *
  1302. * Appends __n copies of __c to this string.
  1303. */
  1304. _GLIBCXX20_CONSTEXPR
  1305. basic_string&
  1306. append(size_type __n, _CharT __c)
  1307. { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
  1308. #if __cplusplus >= 201103L
  1309. /**
  1310. * @brief Append an initializer_list of characters.
  1311. * @param __l The initializer_list of characters to append.
  1312. * @return Reference to this string.
  1313. */
  1314. _GLIBCXX20_CONSTEXPR
  1315. basic_string&
  1316. append(initializer_list<_CharT> __l)
  1317. { return this->append(__l.begin(), __l.size()); }
  1318. #endif // C++11
  1319. /**
  1320. * @brief Append a range of characters.
  1321. * @param __first Iterator referencing the first character to append.
  1322. * @param __last Iterator marking the end of the range.
  1323. * @return Reference to this string.
  1324. *
  1325. * Appends characters in the range [__first,__last) to this string.
  1326. */
  1327. #if __cplusplus >= 201103L
  1328. template<class _InputIterator,
  1329. typename = std::_RequireInputIter<_InputIterator>>
  1330. _GLIBCXX20_CONSTEXPR
  1331. #else
  1332. template<class _InputIterator>
  1333. #endif
  1334. basic_string&
  1335. append(_InputIterator __first, _InputIterator __last)
  1336. { return this->replace(end(), end(), __first, __last); }
  1337. #if __cplusplus >= 201703L
  1338. /**
  1339. * @brief Append a string_view.
  1340. * @param __svt An object convertible to string_view to be appended.
  1341. * @return Reference to this string.
  1342. */
  1343. template<typename _Tp>
  1344. _GLIBCXX20_CONSTEXPR
  1345. _If_sv<_Tp, basic_string&>
  1346. append(const _Tp& __svt)
  1347. {
  1348. __sv_type __sv = __svt;
  1349. return this->append(__sv.data(), __sv.size());
  1350. }
  1351. /**
  1352. * @brief Append a range of characters from a string_view.
  1353. * @param __svt An object convertible to string_view to be appended from.
  1354. * @param __pos The position in the string_view to append from.
  1355. * @param __n The number of characters to append from the string_view.
  1356. * @return Reference to this string.
  1357. */
  1358. template<typename _Tp>
  1359. _GLIBCXX20_CONSTEXPR
  1360. _If_sv<_Tp, basic_string&>
  1361. append(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1362. {
  1363. __sv_type __sv = __svt;
  1364. return _M_append(__sv.data()
  1365. + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
  1366. std::__sv_limit(__sv.size(), __pos, __n));
  1367. }
  1368. #endif // C++17
  1369. /**
  1370. * @brief Append a single character.
  1371. * @param __c Character to append.
  1372. */
  1373. _GLIBCXX20_CONSTEXPR
  1374. void
  1375. push_back(_CharT __c)
  1376. {
  1377. const size_type __size = this->size();
  1378. if (__size + 1 > this->capacity())
  1379. this->_M_mutate(__size, size_type(0), 0, size_type(1));
  1380. traits_type::assign(this->_M_data()[__size], __c);
  1381. this->_M_set_length(__size + 1);
  1382. }
  1383. /**
  1384. * @brief Set value to contents of another string.
  1385. * @param __str Source string to use.
  1386. * @return Reference to this string.
  1387. */
  1388. _GLIBCXX20_CONSTEXPR
  1389. basic_string&
  1390. assign(const basic_string& __str)
  1391. {
  1392. #if __cplusplus >= 201103L
  1393. if (_Alloc_traits::_S_propagate_on_copy_assign())
  1394. {
  1395. if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
  1396. && _M_get_allocator() != __str._M_get_allocator())
  1397. {
  1398. // Propagating allocator cannot free existing storage so must
  1399. // deallocate it before replacing current allocator.
  1400. if (__str.size() <= _S_local_capacity)
  1401. {
  1402. _M_destroy(_M_allocated_capacity);
  1403. _M_data(_M_use_local_data());
  1404. _M_set_length(0);
  1405. }
  1406. else
  1407. {
  1408. const auto __len = __str.size();
  1409. auto __alloc = __str._M_get_allocator();
  1410. // If this allocation throws there are no effects:
  1411. auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
  1412. _M_destroy(_M_allocated_capacity);
  1413. _M_data(__ptr);
  1414. _M_capacity(__len);
  1415. _M_set_length(__len);
  1416. }
  1417. }
  1418. std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
  1419. }
  1420. #endif
  1421. this->_M_assign(__str);
  1422. return *this;
  1423. }
  1424. #if __cplusplus >= 201103L
  1425. /**
  1426. * @brief Set value to contents of another string.
  1427. * @param __str Source string to use.
  1428. * @return Reference to this string.
  1429. *
  1430. * This function sets this string to the exact contents of @a __str.
  1431. * @a __str is a valid, but unspecified string.
  1432. */
  1433. _GLIBCXX20_CONSTEXPR
  1434. basic_string&
  1435. assign(basic_string&& __str)
  1436. noexcept(_Alloc_traits::_S_nothrow_move())
  1437. {
  1438. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1439. // 2063. Contradictory requirements for string move assignment
  1440. return *this = std::move(__str);
  1441. }
  1442. #endif // C++11
  1443. /**
  1444. * @brief Set value to a substring of a string.
  1445. * @param __str The string to use.
  1446. * @param __pos Index of the first character of str.
  1447. * @param __n Number of characters to use.
  1448. * @return Reference to this string.
  1449. * @throw std::out_of_range if @a pos is not a valid index.
  1450. *
  1451. * This function sets this string to the substring of @a __str
  1452. * consisting of @a __n characters at @a __pos. If @a __n is
  1453. * is larger than the number of available characters in @a
  1454. * __str, the remainder of @a __str is used.
  1455. */
  1456. _GLIBCXX20_CONSTEXPR
  1457. basic_string&
  1458. assign(const basic_string& __str, size_type __pos, size_type __n = npos)
  1459. { return _M_replace(size_type(0), this->size(), __str._M_data()
  1460. + __str._M_check(__pos, "basic_string::assign"),
  1461. __str._M_limit(__pos, __n)); }
  1462. /**
  1463. * @brief Set value to a C substring.
  1464. * @param __s The C string to use.
  1465. * @param __n Number of characters to use.
  1466. * @return Reference to this string.
  1467. *
  1468. * This function sets the value of this string to the first @a __n
  1469. * characters of @a __s. If @a __n is is larger than the number of
  1470. * available characters in @a __s, the remainder of @a __s is used.
  1471. */
  1472. _GLIBCXX20_CONSTEXPR
  1473. basic_string&
  1474. assign(const _CharT* __s, size_type __n)
  1475. {
  1476. __glibcxx_requires_string_len(__s, __n);
  1477. return _M_replace(size_type(0), this->size(), __s, __n);
  1478. }
  1479. /**
  1480. * @brief Set value to contents of a C string.
  1481. * @param __s The C string to use.
  1482. * @return Reference to this string.
  1483. *
  1484. * This function sets the value of this string to the value of @a __s.
  1485. * The data is copied, so there is no dependence on @a __s once the
  1486. * function returns.
  1487. */
  1488. _GLIBCXX20_CONSTEXPR
  1489. basic_string&
  1490. assign(const _CharT* __s)
  1491. {
  1492. __glibcxx_requires_string(__s);
  1493. return _M_replace(size_type(0), this->size(), __s,
  1494. traits_type::length(__s));
  1495. }
  1496. /**
  1497. * @brief Set value to multiple characters.
  1498. * @param __n Length of the resulting string.
  1499. * @param __c The character to use.
  1500. * @return Reference to this string.
  1501. *
  1502. * This function sets the value of this string to @a __n copies of
  1503. * character @a __c.
  1504. */
  1505. _GLIBCXX20_CONSTEXPR
  1506. basic_string&
  1507. assign(size_type __n, _CharT __c)
  1508. { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
  1509. /**
  1510. * @brief Set value to a range of characters.
  1511. * @param __first Iterator referencing the first character to append.
  1512. * @param __last Iterator marking the end of the range.
  1513. * @return Reference to this string.
  1514. *
  1515. * Sets value of string to characters in the range [__first,__last).
  1516. */
  1517. #if __cplusplus >= 201103L
  1518. template<class _InputIterator,
  1519. typename = std::_RequireInputIter<_InputIterator>>
  1520. _GLIBCXX20_CONSTEXPR
  1521. #else
  1522. template<class _InputIterator>
  1523. #endif
  1524. basic_string&
  1525. assign(_InputIterator __first, _InputIterator __last)
  1526. { return this->replace(begin(), end(), __first, __last); }
  1527. #if __cplusplus >= 201103L
  1528. /**
  1529. * @brief Set value to an initializer_list of characters.
  1530. * @param __l The initializer_list of characters to assign.
  1531. * @return Reference to this string.
  1532. */
  1533. _GLIBCXX20_CONSTEXPR
  1534. basic_string&
  1535. assign(initializer_list<_CharT> __l)
  1536. { return this->assign(__l.begin(), __l.size()); }
  1537. #endif // C++11
  1538. #if __cplusplus >= 201703L
  1539. /**
  1540. * @brief Set value from a string_view.
  1541. * @param __svt The source object convertible to string_view.
  1542. * @return Reference to this string.
  1543. */
  1544. template<typename _Tp>
  1545. _GLIBCXX20_CONSTEXPR
  1546. _If_sv<_Tp, basic_string&>
  1547. assign(const _Tp& __svt)
  1548. {
  1549. __sv_type __sv = __svt;
  1550. return this->assign(__sv.data(), __sv.size());
  1551. }
  1552. /**
  1553. * @brief Set value from a range of characters in a string_view.
  1554. * @param __svt The source object convertible to string_view.
  1555. * @param __pos The position in the string_view to assign from.
  1556. * @param __n The number of characters to assign.
  1557. * @return Reference to this string.
  1558. */
  1559. template<typename _Tp>
  1560. _GLIBCXX20_CONSTEXPR
  1561. _If_sv<_Tp, basic_string&>
  1562. assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
  1563. {
  1564. __sv_type __sv = __svt;
  1565. return _M_replace(size_type(0), this->size(),
  1566. __sv.data()
  1567. + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
  1568. std::__sv_limit(__sv.size(), __pos, __n));
  1569. }
  1570. #endif // C++17
  1571. #if __cplusplus >= 201103L
  1572. /**
  1573. * @brief Insert multiple characters.
  1574. * @param __p Const_iterator referencing location in string to
  1575. * insert at.
  1576. * @param __n Number of characters to insert
  1577. * @param __c The character to insert.
  1578. * @return Iterator referencing the first inserted char.
  1579. * @throw std::length_error If new length exceeds @c max_size().
  1580. *
  1581. * Inserts @a __n copies of character @a __c starting at the
  1582. * position referenced by iterator @a __p. If adding
  1583. * characters causes the length to exceed max_size(),
  1584. * length_error is thrown. The value of the string doesn't
  1585. * change if an error is thrown.
  1586. */
  1587. _GLIBCXX20_CONSTEXPR
  1588. iterator
  1589. insert(const_iterator __p, size_type __n, _CharT __c)
  1590. {
  1591. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1592. const size_type __pos = __p - begin();
  1593. this->replace(__p, __p, __n, __c);
  1594. return iterator(this->_M_data() + __pos);
  1595. }
  1596. #else
  1597. /**
  1598. * @brief Insert multiple characters.
  1599. * @param __p Iterator referencing location in string to insert at.
  1600. * @param __n Number of characters to insert
  1601. * @param __c The character to insert.
  1602. * @throw std::length_error If new length exceeds @c max_size().
  1603. *
  1604. * Inserts @a __n copies of character @a __c starting at the
  1605. * position referenced by iterator @a __p. If adding
  1606. * characters causes the length to exceed max_size(),
  1607. * length_error is thrown. The value of the string doesn't
  1608. * change if an error is thrown.
  1609. */
  1610. void
  1611. insert(iterator __p, size_type __n, _CharT __c)
  1612. { this->replace(__p, __p, __n, __c); }
  1613. #endif
  1614. #if __cplusplus >= 201103L
  1615. /**
  1616. * @brief Insert a range of characters.
  1617. * @param __p Const_iterator referencing location in string to
  1618. * insert at.
  1619. * @param __beg Start of range.
  1620. * @param __end End of range.
  1621. * @return Iterator referencing the first inserted char.
  1622. * @throw std::length_error If new length exceeds @c max_size().
  1623. *
  1624. * Inserts characters in range [beg,end). If adding characters
  1625. * causes the length to exceed max_size(), length_error is
  1626. * thrown. The value of the string doesn't change if an error
  1627. * is thrown.
  1628. */
  1629. template<class _InputIterator,
  1630. typename = std::_RequireInputIter<_InputIterator>>
  1631. _GLIBCXX20_CONSTEXPR
  1632. iterator
  1633. insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
  1634. {
  1635. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1636. const size_type __pos = __p - begin();
  1637. this->replace(__p, __p, __beg, __end);
  1638. return iterator(this->_M_data() + __pos);
  1639. }
  1640. #else
  1641. /**
  1642. * @brief Insert a range of characters.
  1643. * @param __p Iterator referencing location in string to insert at.
  1644. * @param __beg Start of range.
  1645. * @param __end End of range.
  1646. * @throw std::length_error If new length exceeds @c max_size().
  1647. *
  1648. * Inserts characters in range [__beg,__end). If adding
  1649. * characters causes the length to exceed max_size(),
  1650. * length_error is thrown. The value of the string doesn't
  1651. * change if an error is thrown.
  1652. */
  1653. template<class _InputIterator>
  1654. void
  1655. insert(iterator __p, _InputIterator __beg, _InputIterator __end)
  1656. { this->replace(__p, __p, __beg, __end); }
  1657. #endif
  1658. #if __cplusplus >= 201103L
  1659. /**
  1660. * @brief Insert an initializer_list of characters.
  1661. * @param __p Iterator referencing location in string to insert at.
  1662. * @param __l The initializer_list of characters to insert.
  1663. * @throw std::length_error If new length exceeds @c max_size().
  1664. */
  1665. _GLIBCXX20_CONSTEXPR
  1666. iterator
  1667. insert(const_iterator __p, initializer_list<_CharT> __l)
  1668. { return this->insert(__p, __l.begin(), __l.end()); }
  1669. #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
  1670. // See PR libstdc++/83328
  1671. void
  1672. insert(iterator __p, initializer_list<_CharT> __l)
  1673. {
  1674. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1675. this->insert(__p - begin(), __l.begin(), __l.size());
  1676. }
  1677. #endif
  1678. #endif // C++11
  1679. /**
  1680. * @brief Insert value of a string.
  1681. * @param __pos1 Position in string to insert at.
  1682. * @param __str The string to insert.
  1683. * @return Reference to this string.
  1684. * @throw std::length_error If new length exceeds @c max_size().
  1685. *
  1686. * Inserts value of @a __str starting at @a __pos1. If adding
  1687. * characters causes the length to exceed max_size(),
  1688. * length_error is thrown. The value of the string doesn't
  1689. * change if an error is thrown.
  1690. */
  1691. _GLIBCXX20_CONSTEXPR
  1692. basic_string&
  1693. insert(size_type __pos1, const basic_string& __str)
  1694. { return this->replace(__pos1, size_type(0),
  1695. __str._M_data(), __str.size()); }
  1696. /**
  1697. * @brief Insert a substring.
  1698. * @param __pos1 Position in string to insert at.
  1699. * @param __str The string to insert.
  1700. * @param __pos2 Start of characters in str to insert.
  1701. * @param __n Number of characters to insert.
  1702. * @return Reference to this string.
  1703. * @throw std::length_error If new length exceeds @c max_size().
  1704. * @throw std::out_of_range If @a pos1 > size() or
  1705. * @a __pos2 > @a str.size().
  1706. *
  1707. * Starting at @a pos1, insert @a __n character of @a __str
  1708. * beginning with @a __pos2. If adding characters causes the
  1709. * length to exceed max_size(), length_error is thrown. If @a
  1710. * __pos1 is beyond the end of this string or @a __pos2 is
  1711. * beyond the end of @a __str, out_of_range is thrown. The
  1712. * value of the string doesn't change if an error is thrown.
  1713. */
  1714. _GLIBCXX20_CONSTEXPR
  1715. basic_string&
  1716. insert(size_type __pos1, const basic_string& __str,
  1717. size_type __pos2, size_type __n = npos)
  1718. { return this->replace(__pos1, size_type(0), __str._M_data()
  1719. + __str._M_check(__pos2, "basic_string::insert"),
  1720. __str._M_limit(__pos2, __n)); }
  1721. /**
  1722. * @brief Insert a C substring.
  1723. * @param __pos Position in string to insert at.
  1724. * @param __s The C string to insert.
  1725. * @param __n The number of characters to insert.
  1726. * @return Reference to this string.
  1727. * @throw std::length_error If new length exceeds @c max_size().
  1728. * @throw std::out_of_range If @a __pos is beyond the end of this
  1729. * string.
  1730. *
  1731. * Inserts the first @a __n characters of @a __s starting at @a
  1732. * __pos. If adding characters causes the length to exceed
  1733. * max_size(), length_error is thrown. If @a __pos is beyond
  1734. * end(), out_of_range is thrown. The value of the string
  1735. * doesn't change if an error is thrown.
  1736. */
  1737. _GLIBCXX20_CONSTEXPR
  1738. basic_string&
  1739. insert(size_type __pos, const _CharT* __s, size_type __n)
  1740. { return this->replace(__pos, size_type(0), __s, __n); }
  1741. /**
  1742. * @brief Insert a C string.
  1743. * @param __pos Position in string to insert at.
  1744. * @param __s The C string to insert.
  1745. * @return Reference to this string.
  1746. * @throw std::length_error If new length exceeds @c max_size().
  1747. * @throw std::out_of_range If @a pos is beyond the end of this
  1748. * string.
  1749. *
  1750. * Inserts the first @a n characters of @a __s starting at @a __pos. If
  1751. * adding characters causes the length to exceed max_size(),
  1752. * length_error is thrown. If @a __pos is beyond end(), out_of_range is
  1753. * thrown. The value of the string doesn't change if an error is
  1754. * thrown.
  1755. */
  1756. _GLIBCXX20_CONSTEXPR
  1757. basic_string&
  1758. insert(size_type __pos, const _CharT* __s)
  1759. {
  1760. __glibcxx_requires_string(__s);
  1761. return this->replace(__pos, size_type(0), __s,
  1762. traits_type::length(__s));
  1763. }
  1764. /**
  1765. * @brief Insert multiple characters.
  1766. * @param __pos Index in string to insert at.
  1767. * @param __n Number of characters to insert
  1768. * @param __c The character to insert.
  1769. * @return Reference to this string.
  1770. * @throw std::length_error If new length exceeds @c max_size().
  1771. * @throw std::out_of_range If @a __pos is beyond the end of this
  1772. * string.
  1773. *
  1774. * Inserts @a __n copies of character @a __c starting at index
  1775. * @a __pos. If adding characters causes the length to exceed
  1776. * max_size(), length_error is thrown. If @a __pos > length(),
  1777. * out_of_range is thrown. The value of the string doesn't
  1778. * change if an error is thrown.
  1779. */
  1780. _GLIBCXX20_CONSTEXPR
  1781. basic_string&
  1782. insert(size_type __pos, size_type __n, _CharT __c)
  1783. { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
  1784. size_type(0), __n, __c); }
  1785. /**
  1786. * @brief Insert one character.
  1787. * @param __p Iterator referencing position in string to insert at.
  1788. * @param __c The character to insert.
  1789. * @return Iterator referencing newly inserted char.
  1790. * @throw std::length_error If new length exceeds @c max_size().
  1791. *
  1792. * Inserts character @a __c at position referenced by @a __p.
  1793. * If adding character causes the length to exceed max_size(),
  1794. * length_error is thrown. If @a __p is beyond end of string,
  1795. * out_of_range is thrown. The value of the string doesn't
  1796. * change if an error is thrown.
  1797. */
  1798. _GLIBCXX20_CONSTEXPR
  1799. iterator
  1800. insert(__const_iterator __p, _CharT __c)
  1801. {
  1802. _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
  1803. const size_type __pos = __p - begin();
  1804. _M_replace_aux(__pos, size_type(0), size_type(1), __c);
  1805. return iterator(_M_data() + __pos);
  1806. }
  1807. #if __cplusplus >= 201703L
  1808. /**
  1809. * @brief Insert a string_view.
  1810. * @param __pos Position in string to insert at.
  1811. * @param __svt The object convertible to string_view to insert.
  1812. * @return Reference to this string.
  1813. */
  1814. template<typename _Tp>
  1815. _GLIBCXX20_CONSTEXPR
  1816. _If_sv<_Tp, basic_string&>
  1817. insert(size_type __pos, const _Tp& __svt)
  1818. {
  1819. __sv_type __sv = __svt;
  1820. return this->insert(__pos, __sv.data(), __sv.size());
  1821. }
  1822. /**
  1823. * @brief Insert a string_view.
  1824. * @param __pos1 Position in string to insert at.
  1825. * @param __svt The object convertible to string_view to insert from.
  1826. * @param __pos2 Start of characters in str to insert.
  1827. * @param __n The number of characters to insert.
  1828. * @return Reference to this string.
  1829. */
  1830. template<typename _Tp>
  1831. _GLIBCXX20_CONSTEXPR
  1832. _If_sv<_Tp, basic_string&>
  1833. insert(size_type __pos1, const _Tp& __svt,
  1834. size_type __pos2, size_type __n = npos)
  1835. {
  1836. __sv_type __sv = __svt;
  1837. return this->replace(__pos1, size_type(0),
  1838. __sv.data()
  1839. + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
  1840. std::__sv_limit(__sv.size(), __pos2, __n));
  1841. }
  1842. #endif // C++17
  1843. /**
  1844. * @brief Remove characters.
  1845. * @param __pos Index of first character to remove (default 0).
  1846. * @param __n Number of characters to remove (default remainder).
  1847. * @return Reference to this string.
  1848. * @throw std::out_of_range If @a pos is beyond the end of this
  1849. * string.
  1850. *
  1851. * Removes @a __n characters from this string starting at @a
  1852. * __pos. The length of the string is reduced by @a __n. If
  1853. * there are < @a __n characters to remove, the remainder of
  1854. * the string is truncated. If @a __p is beyond end of string,
  1855. * out_of_range is thrown. The value of the string doesn't
  1856. * change if an error is thrown.
  1857. */
  1858. _GLIBCXX20_CONSTEXPR
  1859. basic_string&
  1860. erase(size_type __pos = 0, size_type __n = npos)
  1861. {
  1862. _M_check(__pos, "basic_string::erase");
  1863. if (__n == npos)
  1864. this->_M_set_length(__pos);
  1865. else if (__n != 0)
  1866. this->_M_erase(__pos, _M_limit(__pos, __n));
  1867. return *this;
  1868. }
  1869. /**
  1870. * @brief Remove one character.
  1871. * @param __position Iterator referencing the character to remove.
  1872. * @return iterator referencing same location after removal.
  1873. *
  1874. * Removes the character at @a __position from this string. The value
  1875. * of the string doesn't change if an error is thrown.
  1876. */
  1877. _GLIBCXX20_CONSTEXPR
  1878. iterator
  1879. erase(__const_iterator __position)
  1880. {
  1881. _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
  1882. && __position < end());
  1883. const size_type __pos = __position - begin();
  1884. this->_M_erase(__pos, size_type(1));
  1885. return iterator(_M_data() + __pos);
  1886. }
  1887. /**
  1888. * @brief Remove a range of characters.
  1889. * @param __first Iterator referencing the first character to remove.
  1890. * @param __last Iterator referencing the end of the range.
  1891. * @return Iterator referencing location of first after removal.
  1892. *
  1893. * Removes the characters in the range [first,last) from this string.
  1894. * The value of the string doesn't change if an error is thrown.
  1895. */
  1896. _GLIBCXX20_CONSTEXPR
  1897. iterator
  1898. erase(__const_iterator __first, __const_iterator __last)
  1899. {
  1900. _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
  1901. && __last <= end());
  1902. const size_type __pos = __first - begin();
  1903. if (__last == end())
  1904. this->_M_set_length(__pos);
  1905. else
  1906. this->_M_erase(__pos, __last - __first);
  1907. return iterator(this->_M_data() + __pos);
  1908. }
  1909. #if __cplusplus >= 201103L
  1910. /**
  1911. * @brief Remove the last character.
  1912. *
  1913. * The string must be non-empty.
  1914. */
  1915. _GLIBCXX20_CONSTEXPR
  1916. void
  1917. pop_back() noexcept
  1918. {
  1919. __glibcxx_assert(!empty());
  1920. _M_erase(size() - 1, 1);
  1921. }
  1922. #endif // C++11
  1923. /**
  1924. * @brief Replace characters with value from another string.
  1925. * @param __pos Index of first character to replace.
  1926. * @param __n Number of characters to be replaced.
  1927. * @param __str String to insert.
  1928. * @return Reference to this string.
  1929. * @throw std::out_of_range If @a pos is beyond the end of this
  1930. * string.
  1931. * @throw std::length_error If new length exceeds @c max_size().
  1932. *
  1933. * Removes the characters in the range [__pos,__pos+__n) from
  1934. * this string. In place, the value of @a __str is inserted.
  1935. * If @a __pos is beyond end of string, out_of_range is thrown.
  1936. * If the length of the result exceeds max_size(), length_error
  1937. * is thrown. The value of the string doesn't change if an
  1938. * error is thrown.
  1939. */
  1940. _GLIBCXX20_CONSTEXPR
  1941. basic_string&
  1942. replace(size_type __pos, size_type __n, const basic_string& __str)
  1943. { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
  1944. /**
  1945. * @brief Replace characters with value from another string.
  1946. * @param __pos1 Index of first character to replace.
  1947. * @param __n1 Number of characters to be replaced.
  1948. * @param __str String to insert.
  1949. * @param __pos2 Index of first character of str to use.
  1950. * @param __n2 Number of characters from str to use.
  1951. * @return Reference to this string.
  1952. * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
  1953. * __str.size().
  1954. * @throw std::length_error If new length exceeds @c max_size().
  1955. *
  1956. * Removes the characters in the range [__pos1,__pos1 + n) from this
  1957. * string. In place, the value of @a __str is inserted. If @a __pos is
  1958. * beyond end of string, out_of_range is thrown. If the length of the
  1959. * result exceeds max_size(), length_error is thrown. The value of the
  1960. * string doesn't change if an error is thrown.
  1961. */
  1962. _GLIBCXX20_CONSTEXPR
  1963. basic_string&
  1964. replace(size_type __pos1, size_type __n1, const basic_string& __str,
  1965. size_type __pos2, size_type __n2 = npos)
  1966. { return this->replace(__pos1, __n1, __str._M_data()
  1967. + __str._M_check(__pos2, "basic_string::replace"),
  1968. __str._M_limit(__pos2, __n2)); }
  1969. /**
  1970. * @brief Replace characters with value of a C substring.
  1971. * @param __pos Index of first character to replace.
  1972. * @param __n1 Number of characters to be replaced.
  1973. * @param __s C string to insert.
  1974. * @param __n2 Number of characters from @a s to use.
  1975. * @return Reference to this string.
  1976. * @throw std::out_of_range If @a pos1 > size().
  1977. * @throw std::length_error If new length exceeds @c max_size().
  1978. *
  1979. * Removes the characters in the range [__pos,__pos + __n1)
  1980. * from this string. In place, the first @a __n2 characters of
  1981. * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
  1982. * @a __pos is beyond end of string, out_of_range is thrown. If
  1983. * the length of result exceeds max_size(), length_error is
  1984. * thrown. The value of the string doesn't change if an error
  1985. * is thrown.
  1986. */
  1987. _GLIBCXX20_CONSTEXPR
  1988. basic_string&
  1989. replace(size_type __pos, size_type __n1, const _CharT* __s,
  1990. size_type __n2)
  1991. {
  1992. __glibcxx_requires_string_len(__s, __n2);
  1993. return _M_replace(_M_check(__pos, "basic_string::replace"),
  1994. _M_limit(__pos, __n1), __s, __n2);
  1995. }
  1996. /**
  1997. * @brief Replace characters with value of a C string.
  1998. * @param __pos Index of first character to replace.
  1999. * @param __n1 Number of characters to be replaced.
  2000. * @param __s C string to insert.
  2001. * @return Reference to this string.
  2002. * @throw std::out_of_range If @a pos > size().
  2003. * @throw std::length_error If new length exceeds @c max_size().
  2004. *
  2005. * Removes the characters in the range [__pos,__pos + __n1)
  2006. * from this string. In place, the characters of @a __s are
  2007. * inserted. If @a __pos is beyond end of string, out_of_range
  2008. * is thrown. If the length of result exceeds max_size(),
  2009. * length_error is thrown. The value of the string doesn't
  2010. * change if an error is thrown.
  2011. */
  2012. _GLIBCXX20_CONSTEXPR
  2013. basic_string&
  2014. replace(size_type __pos, size_type __n1, const _CharT* __s)
  2015. {
  2016. __glibcxx_requires_string(__s);
  2017. return this->replace(__pos, __n1, __s, traits_type::length(__s));
  2018. }
  2019. /**
  2020. * @brief Replace characters with multiple characters.
  2021. * @param __pos Index of first character to replace.
  2022. * @param __n1 Number of characters to be replaced.
  2023. * @param __n2 Number of characters to insert.
  2024. * @param __c Character to insert.
  2025. * @return Reference to this string.
  2026. * @throw std::out_of_range If @a __pos > size().
  2027. * @throw std::length_error If new length exceeds @c max_size().
  2028. *
  2029. * Removes the characters in the range [pos,pos + n1) from this
  2030. * string. In place, @a __n2 copies of @a __c are inserted.
  2031. * If @a __pos is beyond end of string, out_of_range is thrown.
  2032. * If the length of result exceeds max_size(), length_error is
  2033. * thrown. The value of the string doesn't change if an error
  2034. * is thrown.
  2035. */
  2036. _GLIBCXX20_CONSTEXPR
  2037. basic_string&
  2038. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  2039. { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
  2040. _M_limit(__pos, __n1), __n2, __c); }
  2041. /**
  2042. * @brief Replace range of characters with string.
  2043. * @param __i1 Iterator referencing start of range to replace.
  2044. * @param __i2 Iterator referencing end of range to replace.
  2045. * @param __str String value to insert.
  2046. * @return Reference to this string.
  2047. * @throw std::length_error If new length exceeds @c max_size().
  2048. *
  2049. * Removes the characters in the range [__i1,__i2). In place,
  2050. * the value of @a __str is inserted. If the length of result
  2051. * exceeds max_size(), length_error is thrown. The value of
  2052. * the string doesn't change if an error is thrown.
  2053. */
  2054. _GLIBCXX20_CONSTEXPR
  2055. basic_string&
  2056. replace(__const_iterator __i1, __const_iterator __i2,
  2057. const basic_string& __str)
  2058. { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
  2059. /**
  2060. * @brief Replace range of characters with C substring.
  2061. * @param __i1 Iterator referencing start of range to replace.
  2062. * @param __i2 Iterator referencing end of range to replace.
  2063. * @param __s C string value to insert.
  2064. * @param __n Number of characters from s to insert.
  2065. * @return Reference to this string.
  2066. * @throw std::length_error If new length exceeds @c max_size().
  2067. *
  2068. * Removes the characters in the range [__i1,__i2). In place,
  2069. * the first @a __n characters of @a __s are inserted. If the
  2070. * length of result exceeds max_size(), length_error is thrown.
  2071. * The value of the string doesn't change if an error is
  2072. * thrown.
  2073. */
  2074. _GLIBCXX20_CONSTEXPR
  2075. basic_string&
  2076. replace(__const_iterator __i1, __const_iterator __i2,
  2077. const _CharT* __s, size_type __n)
  2078. {
  2079. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  2080. && __i2 <= end());
  2081. return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
  2082. }
  2083. /**
  2084. * @brief Replace range of characters with C string.
  2085. * @param __i1 Iterator referencing start of range to replace.
  2086. * @param __i2 Iterator referencing end of range to replace.
  2087. * @param __s C string value to insert.
  2088. * @return Reference to this string.
  2089. * @throw std::length_error If new length exceeds @c max_size().
  2090. *
  2091. * Removes the characters in the range [__i1,__i2). In place,
  2092. * the characters of @a __s are inserted. If the length of
  2093. * result exceeds max_size(), length_error is thrown. The
  2094. * value of the string doesn't change if an error is thrown.
  2095. */
  2096. _GLIBCXX20_CONSTEXPR
  2097. basic_string&
  2098. replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
  2099. {
  2100. __glibcxx_requires_string(__s);
  2101. return this->replace(__i1, __i2, __s, traits_type::length(__s));
  2102. }
  2103. /**
  2104. * @brief Replace range of characters with multiple characters
  2105. * @param __i1 Iterator referencing start of range to replace.
  2106. * @param __i2 Iterator referencing end of range to replace.
  2107. * @param __n Number of characters to insert.
  2108. * @param __c Character to insert.
  2109. * @return Reference to this string.
  2110. * @throw std::length_error If new length exceeds @c max_size().
  2111. *
  2112. * Removes the characters in the range [__i1,__i2). In place,
  2113. * @a __n copies of @a __c are inserted. If the length of
  2114. * result exceeds max_size(), length_error is thrown. The
  2115. * value of the string doesn't change if an error is thrown.
  2116. */
  2117. _GLIBCXX20_CONSTEXPR
  2118. basic_string&
  2119. replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
  2120. _CharT __c)
  2121. {
  2122. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  2123. && __i2 <= end());
  2124. return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
  2125. }
  2126. /**
  2127. * @brief Replace range of characters with range.
  2128. * @param __i1 Iterator referencing start of range to replace.
  2129. * @param __i2 Iterator referencing end of range to replace.
  2130. * @param __k1 Iterator referencing start of range to insert.
  2131. * @param __k2 Iterator referencing end of range to insert.
  2132. * @return Reference to this string.
  2133. * @throw std::length_error If new length exceeds @c max_size().
  2134. *
  2135. * Removes the characters in the range [__i1,__i2). In place,
  2136. * characters in the range [__k1,__k2) are inserted. If the
  2137. * length of result exceeds max_size(), length_error is thrown.
  2138. * The value of the string doesn't change if an error is
  2139. * thrown.
  2140. */
  2141. #if __cplusplus >= 201103L
  2142. template<class _InputIterator,
  2143. typename = std::_RequireInputIter<_InputIterator>>
  2144. _GLIBCXX20_CONSTEXPR
  2145. basic_string&
  2146. replace(const_iterator __i1, const_iterator __i2,
  2147. _InputIterator __k1, _InputIterator __k2)
  2148. {
  2149. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  2150. && __i2 <= end());
  2151. __glibcxx_requires_valid_range(__k1, __k2);
  2152. return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
  2153. std::__false_type());
  2154. }
  2155. #else
  2156. template<class _InputIterator>
  2157. #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
  2158. typename __enable_if_not_native_iterator<_InputIterator>::__type
  2159. #else
  2160. basic_string&
  2161. #endif
  2162. replace(iterator __i1, iterator __i2,
  2163. _InputIterator __k1, _InputIterator __k2)
  2164. {
  2165. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  2166. && __i2 <= end());
  2167. __glibcxx_requires_valid_range(__k1, __k2);
  2168. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  2169. return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
  2170. }
  2171. #endif
  2172. // Specializations for the common case of pointer and iterator:
  2173. // useful to avoid the overhead of temporary buffering in _M_replace.
  2174. _GLIBCXX20_CONSTEXPR
  2175. basic_string&
  2176. replace(__const_iterator __i1, __const_iterator __i2,
  2177. _CharT* __k1, _CharT* __k2)
  2178. {
  2179. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  2180. && __i2 <= end());
  2181. __glibcxx_requires_valid_range(__k1, __k2);
  2182. return this->replace(__i1 - begin(), __i2 - __i1,
  2183. __k1, __k2 - __k1);
  2184. }
  2185. _GLIBCXX20_CONSTEXPR
  2186. basic_string&
  2187. replace(__const_iterator __i1, __const_iterator __i2,
  2188. const _CharT* __k1, const _CharT* __k2)
  2189. {
  2190. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  2191. && __i2 <= end());
  2192. __glibcxx_requires_valid_range(__k1, __k2);
  2193. return this->replace(__i1 - begin(), __i2 - __i1,
  2194. __k1, __k2 - __k1);
  2195. }
  2196. _GLIBCXX20_CONSTEXPR
  2197. basic_string&
  2198. replace(__const_iterator __i1, __const_iterator __i2,
  2199. iterator __k1, iterator __k2)
  2200. {
  2201. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  2202. && __i2 <= end());
  2203. __glibcxx_requires_valid_range(__k1, __k2);
  2204. return this->replace(__i1 - begin(), __i2 - __i1,
  2205. __k1.base(), __k2 - __k1);
  2206. }
  2207. _GLIBCXX20_CONSTEXPR
  2208. basic_string&
  2209. replace(__const_iterator __i1, __const_iterator __i2,
  2210. const_iterator __k1, const_iterator __k2)
  2211. {
  2212. _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
  2213. && __i2 <= end());
  2214. __glibcxx_requires_valid_range(__k1, __k2);
  2215. return this->replace(__i1 - begin(), __i2 - __i1,
  2216. __k1.base(), __k2 - __k1);
  2217. }
  2218. #if __cplusplus >= 201103L
  2219. /**
  2220. * @brief Replace range of characters with initializer_list.
  2221. * @param __i1 Iterator referencing start of range to replace.
  2222. * @param __i2 Iterator referencing end of range to replace.
  2223. * @param __l The initializer_list of characters to insert.
  2224. * @return Reference to this string.
  2225. * @throw std::length_error If new length exceeds @c max_size().
  2226. *
  2227. * Removes the characters in the range [__i1,__i2). In place,
  2228. * characters in the range [__k1,__k2) are inserted. If the
  2229. * length of result exceeds max_size(), length_error is thrown.
  2230. * The value of the string doesn't change if an error is
  2231. * thrown.
  2232. */
  2233. _GLIBCXX20_CONSTEXPR
  2234. basic_string& replace(const_iterator __i1, const_iterator __i2,
  2235. initializer_list<_CharT> __l)
  2236. { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
  2237. #endif // C++11
  2238. #if __cplusplus >= 201703L
  2239. /**
  2240. * @brief Replace range of characters with string_view.
  2241. * @param __pos The position to replace at.
  2242. * @param __n The number of characters to replace.
  2243. * @param __svt The object convertible to string_view to insert.
  2244. * @return Reference to this string.
  2245. */
  2246. template<typename _Tp>
  2247. _GLIBCXX20_CONSTEXPR
  2248. _If_sv<_Tp, basic_string&>
  2249. replace(size_type __pos, size_type __n, const _Tp& __svt)
  2250. {
  2251. __sv_type __sv = __svt;
  2252. return this->replace(__pos, __n, __sv.data(), __sv.size());
  2253. }
  2254. /**
  2255. * @brief Replace range of characters with string_view.
  2256. * @param __pos1 The position to replace at.
  2257. * @param __n1 The number of characters to replace.
  2258. * @param __svt The object convertible to string_view to insert from.
  2259. * @param __pos2 The position in the string_view to insert from.
  2260. * @param __n2 The number of characters to insert.
  2261. * @return Reference to this string.
  2262. */
  2263. template<typename _Tp>
  2264. _GLIBCXX20_CONSTEXPR
  2265. _If_sv<_Tp, basic_string&>
  2266. replace(size_type __pos1, size_type __n1, const _Tp& __svt,
  2267. size_type __pos2, size_type __n2 = npos)
  2268. {
  2269. __sv_type __sv = __svt;
  2270. return this->replace(__pos1, __n1,
  2271. __sv.data()
  2272. + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
  2273. std::__sv_limit(__sv.size(), __pos2, __n2));
  2274. }
  2275. /**
  2276. * @brief Replace range of characters with string_view.
  2277. * @param __i1 An iterator referencing the start position
  2278. to replace at.
  2279. * @param __i2 An iterator referencing the end position
  2280. for the replace.
  2281. * @param __svt The object convertible to string_view to insert from.
  2282. * @return Reference to this string.
  2283. */
  2284. template<typename _Tp>
  2285. _GLIBCXX20_CONSTEXPR
  2286. _If_sv<_Tp, basic_string&>
  2287. replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
  2288. {
  2289. __sv_type __sv = __svt;
  2290. return this->replace(__i1 - begin(), __i2 - __i1, __sv);
  2291. }
  2292. #endif // C++17
  2293. private:
  2294. template<class _Integer>
  2295. _GLIBCXX20_CONSTEXPR
  2296. basic_string&
  2297. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  2298. _Integer __n, _Integer __val, __true_type)
  2299. { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
  2300. template<class _InputIterator>
  2301. _GLIBCXX20_CONSTEXPR
  2302. basic_string&
  2303. _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
  2304. _InputIterator __k1, _InputIterator __k2,
  2305. __false_type);
  2306. _GLIBCXX20_CONSTEXPR
  2307. basic_string&
  2308. _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
  2309. _CharT __c);
  2310. _GLIBCXX20_CONSTEXPR
  2311. basic_string&
  2312. _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
  2313. const size_type __len2);
  2314. _GLIBCXX20_CONSTEXPR
  2315. basic_string&
  2316. _M_append(const _CharT* __s, size_type __n);
  2317. public:
  2318. /**
  2319. * @brief Copy substring into C string.
  2320. * @param __s C string to copy value into.
  2321. * @param __n Number of characters to copy.
  2322. * @param __pos Index of first character to copy.
  2323. * @return Number of characters actually copied
  2324. * @throw std::out_of_range If __pos > size().
  2325. *
  2326. * Copies up to @a __n characters starting at @a __pos into the
  2327. * C string @a __s. If @a __pos is %greater than size(),
  2328. * out_of_range is thrown.
  2329. */
  2330. _GLIBCXX20_CONSTEXPR
  2331. size_type
  2332. copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
  2333. /**
  2334. * @brief Swap contents with another string.
  2335. * @param __s String to swap with.
  2336. *
  2337. * Exchanges the contents of this string with that of @a __s in constant
  2338. * time.
  2339. */
  2340. _GLIBCXX20_CONSTEXPR
  2341. void
  2342. swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
  2343. // String operations:
  2344. /**
  2345. * @brief Return const pointer to null-terminated contents.
  2346. *
  2347. * This is a handle to internal data. Do not modify or dire things may
  2348. * happen.
  2349. */
  2350. _GLIBCXX20_CONSTEXPR
  2351. const _CharT*
  2352. c_str() const _GLIBCXX_NOEXCEPT
  2353. { return _M_data(); }
  2354. /**
  2355. * @brief Return const pointer to contents.
  2356. *
  2357. * This is a pointer to internal data. It is undefined to modify
  2358. * the contents through the returned pointer. To get a pointer that
  2359. * allows modifying the contents use @c &str[0] instead,
  2360. * (or in C++17 the non-const @c str.data() overload).
  2361. */
  2362. _GLIBCXX20_CONSTEXPR
  2363. const _CharT*
  2364. data() const _GLIBCXX_NOEXCEPT
  2365. { return _M_data(); }
  2366. #if __cplusplus >= 201703L
  2367. /**
  2368. * @brief Return non-const pointer to contents.
  2369. *
  2370. * This is a pointer to the character sequence held by the string.
  2371. * Modifying the characters in the sequence is allowed.
  2372. */
  2373. _GLIBCXX20_CONSTEXPR
  2374. _CharT*
  2375. data() noexcept
  2376. { return _M_data(); }
  2377. #endif
  2378. /**
  2379. * @brief Return copy of allocator used to construct this string.
  2380. */
  2381. _GLIBCXX20_CONSTEXPR
  2382. allocator_type
  2383. get_allocator() const _GLIBCXX_NOEXCEPT
  2384. { return _M_get_allocator(); }
  2385. /**
  2386. * @brief Find position of a C substring.
  2387. * @param __s C string to locate.
  2388. * @param __pos Index of character to search from.
  2389. * @param __n Number of characters from @a s to search for.
  2390. * @return Index of start of first occurrence.
  2391. *
  2392. * Starting from @a __pos, searches forward for the first @a
  2393. * __n characters in @a __s within this string. If found,
  2394. * returns the index where it begins. If not found, returns
  2395. * npos.
  2396. */
  2397. _GLIBCXX20_CONSTEXPR
  2398. size_type
  2399. find(const _CharT* __s, size_type __pos, size_type __n) const
  2400. _GLIBCXX_NOEXCEPT;
  2401. /**
  2402. * @brief Find position of a string.
  2403. * @param __str String to locate.
  2404. * @param __pos Index of character to search from (default 0).
  2405. * @return Index of start of first occurrence.
  2406. *
  2407. * Starting from @a __pos, searches forward for value of @a __str within
  2408. * this string. If found, returns the index where it begins. If not
  2409. * found, returns npos.
  2410. */
  2411. _GLIBCXX20_CONSTEXPR
  2412. size_type
  2413. find(const basic_string& __str, size_type __pos = 0) const
  2414. _GLIBCXX_NOEXCEPT
  2415. { return this->find(__str.data(), __pos, __str.size()); }
  2416. #if __cplusplus >= 201703L
  2417. /**
  2418. * @brief Find position of a string_view.
  2419. * @param __svt The object convertible to string_view to locate.
  2420. * @param __pos Index of character to search from (default 0).
  2421. * @return Index of start of first occurrence.
  2422. */
  2423. template<typename _Tp>
  2424. _GLIBCXX20_CONSTEXPR
  2425. _If_sv<_Tp, size_type>
  2426. find(const _Tp& __svt, size_type __pos = 0) const
  2427. noexcept(is_same<_Tp, __sv_type>::value)
  2428. {
  2429. __sv_type __sv = __svt;
  2430. return this->find(__sv.data(), __pos, __sv.size());
  2431. }
  2432. #endif // C++17
  2433. /**
  2434. * @brief Find position of a C string.
  2435. * @param __s C string to locate.
  2436. * @param __pos Index of character to search from (default 0).
  2437. * @return Index of start of first occurrence.
  2438. *
  2439. * Starting from @a __pos, searches forward for the value of @a
  2440. * __s within this string. If found, returns the index where
  2441. * it begins. If not found, returns npos.
  2442. */
  2443. _GLIBCXX20_CONSTEXPR
  2444. size_type
  2445. find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2446. {
  2447. __glibcxx_requires_string(__s);
  2448. return this->find(__s, __pos, traits_type::length(__s));
  2449. }
  2450. /**
  2451. * @brief Find position of a character.
  2452. * @param __c Character to locate.
  2453. * @param __pos Index of character to search from (default 0).
  2454. * @return Index of first occurrence.
  2455. *
  2456. * Starting from @a __pos, searches forward for @a __c within
  2457. * this string. If found, returns the index where it was
  2458. * found. If not found, returns npos.
  2459. */
  2460. _GLIBCXX20_CONSTEXPR
  2461. size_type
  2462. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
  2463. /**
  2464. * @brief Find last position of a string.
  2465. * @param __str String to locate.
  2466. * @param __pos Index of character to search back from (default end).
  2467. * @return Index of start of last occurrence.
  2468. *
  2469. * Starting from @a __pos, searches backward for value of @a
  2470. * __str within this string. If found, returns the index where
  2471. * it begins. If not found, returns npos.
  2472. */
  2473. _GLIBCXX20_CONSTEXPR
  2474. size_type
  2475. rfind(const basic_string& __str, size_type __pos = npos) const
  2476. _GLIBCXX_NOEXCEPT
  2477. { return this->rfind(__str.data(), __pos, __str.size()); }
  2478. #if __cplusplus >= 201703L
  2479. /**
  2480. * @brief Find last position of a string_view.
  2481. * @param __svt The object convertible to string_view to locate.
  2482. * @param __pos Index of character to search back from (default end).
  2483. * @return Index of start of last occurrence.
  2484. */
  2485. template<typename _Tp>
  2486. _GLIBCXX20_CONSTEXPR
  2487. _If_sv<_Tp, size_type>
  2488. rfind(const _Tp& __svt, size_type __pos = npos) const
  2489. noexcept(is_same<_Tp, __sv_type>::value)
  2490. {
  2491. __sv_type __sv = __svt;
  2492. return this->rfind(__sv.data(), __pos, __sv.size());
  2493. }
  2494. #endif // C++17
  2495. /**
  2496. * @brief Find last position of a C substring.
  2497. * @param __s C string to locate.
  2498. * @param __pos Index of character to search back from.
  2499. * @param __n Number of characters from s to search for.
  2500. * @return Index of start of last occurrence.
  2501. *
  2502. * Starting from @a __pos, searches backward for the first @a
  2503. * __n characters in @a __s within this string. If found,
  2504. * returns the index where it begins. If not found, returns
  2505. * npos.
  2506. */
  2507. _GLIBCXX20_CONSTEXPR
  2508. size_type
  2509. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  2510. _GLIBCXX_NOEXCEPT;
  2511. /**
  2512. * @brief Find last position of a C string.
  2513. * @param __s C string to locate.
  2514. * @param __pos Index of character to start search at (default end).
  2515. * @return Index of start of last occurrence.
  2516. *
  2517. * Starting from @a __pos, searches backward for the value of
  2518. * @a __s within this string. If found, returns the index
  2519. * where it begins. If not found, returns npos.
  2520. */
  2521. _GLIBCXX20_CONSTEXPR
  2522. size_type
  2523. rfind(const _CharT* __s, size_type __pos = npos) const
  2524. {
  2525. __glibcxx_requires_string(__s);
  2526. return this->rfind(__s, __pos, traits_type::length(__s));
  2527. }
  2528. /**
  2529. * @brief Find last position of a character.
  2530. * @param __c Character to locate.
  2531. * @param __pos Index of character to search back from (default end).
  2532. * @return Index of last occurrence.
  2533. *
  2534. * Starting from @a __pos, searches backward for @a __c within
  2535. * this string. If found, returns the index where it was
  2536. * found. If not found, returns npos.
  2537. */
  2538. _GLIBCXX20_CONSTEXPR
  2539. size_type
  2540. rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
  2541. /**
  2542. * @brief Find position of a character of string.
  2543. * @param __str String containing characters to locate.
  2544. * @param __pos Index of character to search from (default 0).
  2545. * @return Index of first occurrence.
  2546. *
  2547. * Starting from @a __pos, searches forward for one of the
  2548. * characters of @a __str within this string. If found,
  2549. * returns the index where it was found. If not found, returns
  2550. * npos.
  2551. */
  2552. _GLIBCXX20_CONSTEXPR
  2553. size_type
  2554. find_first_of(const basic_string& __str, size_type __pos = 0) const
  2555. _GLIBCXX_NOEXCEPT
  2556. { return this->find_first_of(__str.data(), __pos, __str.size()); }
  2557. #if __cplusplus >= 201703L
  2558. /**
  2559. * @brief Find position of a character of a string_view.
  2560. * @param __svt An object convertible to string_view containing
  2561. * characters to locate.
  2562. * @param __pos Index of character to search from (default 0).
  2563. * @return Index of first occurrence.
  2564. */
  2565. template<typename _Tp>
  2566. _GLIBCXX20_CONSTEXPR
  2567. _If_sv<_Tp, size_type>
  2568. find_first_of(const _Tp& __svt, size_type __pos = 0) const
  2569. noexcept(is_same<_Tp, __sv_type>::value)
  2570. {
  2571. __sv_type __sv = __svt;
  2572. return this->find_first_of(__sv.data(), __pos, __sv.size());
  2573. }
  2574. #endif // C++17
  2575. /**
  2576. * @brief Find position of a character of C substring.
  2577. * @param __s String containing characters to locate.
  2578. * @param __pos Index of character to search from.
  2579. * @param __n Number of characters from s to search for.
  2580. * @return Index of first occurrence.
  2581. *
  2582. * Starting from @a __pos, searches forward for one of the
  2583. * first @a __n characters of @a __s within this string. If
  2584. * found, returns the index where it was found. If not found,
  2585. * returns npos.
  2586. */
  2587. _GLIBCXX20_CONSTEXPR
  2588. size_type
  2589. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  2590. _GLIBCXX_NOEXCEPT;
  2591. /**
  2592. * @brief Find position of a character of C string.
  2593. * @param __s String containing characters to locate.
  2594. * @param __pos Index of character to search from (default 0).
  2595. * @return Index of first occurrence.
  2596. *
  2597. * Starting from @a __pos, searches forward for one of the
  2598. * characters of @a __s within this string. If found, returns
  2599. * the index where it was found. If not found, returns npos.
  2600. */
  2601. _GLIBCXX20_CONSTEXPR
  2602. size_type
  2603. find_first_of(const _CharT* __s, size_type __pos = 0) const
  2604. _GLIBCXX_NOEXCEPT
  2605. {
  2606. __glibcxx_requires_string(__s);
  2607. return this->find_first_of(__s, __pos, traits_type::length(__s));
  2608. }
  2609. /**
  2610. * @brief Find position of a character.
  2611. * @param __c Character to locate.
  2612. * @param __pos Index of character to search from (default 0).
  2613. * @return Index of first occurrence.
  2614. *
  2615. * Starting from @a __pos, searches forward for the character
  2616. * @a __c within this string. If found, returns the index
  2617. * where it was found. If not found, returns npos.
  2618. *
  2619. * Note: equivalent to find(__c, __pos).
  2620. */
  2621. _GLIBCXX20_CONSTEXPR
  2622. size_type
  2623. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  2624. { return this->find(__c, __pos); }
  2625. /**
  2626. * @brief Find last position of a character of string.
  2627. * @param __str String containing characters to locate.
  2628. * @param __pos Index of character to search back from (default end).
  2629. * @return Index of last occurrence.
  2630. *
  2631. * Starting from @a __pos, searches backward for one of the
  2632. * characters of @a __str within this string. If found,
  2633. * returns the index where it was found. If not found, returns
  2634. * npos.
  2635. */
  2636. _GLIBCXX20_CONSTEXPR
  2637. size_type
  2638. find_last_of(const basic_string& __str, size_type __pos = npos) const
  2639. _GLIBCXX_NOEXCEPT
  2640. { return this->find_last_of(__str.data(), __pos, __str.size()); }
  2641. #if __cplusplus >= 201703L
  2642. /**
  2643. * @brief Find last position of a character of string.
  2644. * @param __svt An object convertible to string_view containing
  2645. * characters to locate.
  2646. * @param __pos Index of character to search back from (default end).
  2647. * @return Index of last occurrence.
  2648. */
  2649. template<typename _Tp>
  2650. _GLIBCXX20_CONSTEXPR
  2651. _If_sv<_Tp, size_type>
  2652. find_last_of(const _Tp& __svt, size_type __pos = npos) const
  2653. noexcept(is_same<_Tp, __sv_type>::value)
  2654. {
  2655. __sv_type __sv = __svt;
  2656. return this->find_last_of(__sv.data(), __pos, __sv.size());
  2657. }
  2658. #endif // C++17
  2659. /**
  2660. * @brief Find last position of a character of C substring.
  2661. * @param __s C string containing characters to locate.
  2662. * @param __pos Index of character to search back from.
  2663. * @param __n Number of characters from s to search for.
  2664. * @return Index of last occurrence.
  2665. *
  2666. * Starting from @a __pos, searches backward for one of the
  2667. * first @a __n characters of @a __s within this string. If
  2668. * found, returns the index where it was found. If not found,
  2669. * returns npos.
  2670. */
  2671. _GLIBCXX20_CONSTEXPR
  2672. size_type
  2673. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  2674. _GLIBCXX_NOEXCEPT;
  2675. /**
  2676. * @brief Find last position of a character of C string.
  2677. * @param __s C string containing characters to locate.
  2678. * @param __pos Index of character to search back from (default end).
  2679. * @return Index of last occurrence.
  2680. *
  2681. * Starting from @a __pos, searches backward for one of the
  2682. * characters of @a __s within this string. If found, returns
  2683. * the index where it was found. If not found, returns npos.
  2684. */
  2685. _GLIBCXX20_CONSTEXPR
  2686. size_type
  2687. find_last_of(const _CharT* __s, size_type __pos = npos) const
  2688. _GLIBCXX_NOEXCEPT
  2689. {
  2690. __glibcxx_requires_string(__s);
  2691. return this->find_last_of(__s, __pos, traits_type::length(__s));
  2692. }
  2693. /**
  2694. * @brief Find last position of a character.
  2695. * @param __c Character to locate.
  2696. * @param __pos Index of character to search back from (default end).
  2697. * @return Index of last occurrence.
  2698. *
  2699. * Starting from @a __pos, searches backward for @a __c within
  2700. * this string. If found, returns the index where it was
  2701. * found. If not found, returns npos.
  2702. *
  2703. * Note: equivalent to rfind(__c, __pos).
  2704. */
  2705. _GLIBCXX20_CONSTEXPR
  2706. size_type
  2707. find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
  2708. { return this->rfind(__c, __pos); }
  2709. /**
  2710. * @brief Find position of a character not in string.
  2711. * @param __str String containing characters to avoid.
  2712. * @param __pos Index of character to search from (default 0).
  2713. * @return Index of first occurrence.
  2714. *
  2715. * Starting from @a __pos, searches forward for a character not contained
  2716. * in @a __str within this string. If found, returns the index where it
  2717. * was found. If not found, returns npos.
  2718. */
  2719. _GLIBCXX20_CONSTEXPR
  2720. size_type
  2721. find_first_not_of(const basic_string& __str, size_type __pos = 0) const
  2722. _GLIBCXX_NOEXCEPT
  2723. { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
  2724. #if __cplusplus >= 201703L
  2725. /**
  2726. * @brief Find position of a character not in a string_view.
  2727. * @param __svt A object convertible to string_view containing
  2728. * characters to avoid.
  2729. * @param __pos Index of character to search from (default 0).
  2730. * @return Index of first occurrence.
  2731. */
  2732. template<typename _Tp>
  2733. _If_sv<_Tp, size_type>
  2734. _GLIBCXX20_CONSTEXPR
  2735. find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
  2736. noexcept(is_same<_Tp, __sv_type>::value)
  2737. {
  2738. __sv_type __sv = __svt;
  2739. return this->find_first_not_of(__sv.data(), __pos, __sv.size());
  2740. }
  2741. #endif // C++17
  2742. /**
  2743. * @brief Find position of a character not in C substring.
  2744. * @param __s C string containing characters to avoid.
  2745. * @param __pos Index of character to search from.
  2746. * @param __n Number of characters from __s to consider.
  2747. * @return Index of first occurrence.
  2748. *
  2749. * Starting from @a __pos, searches forward for a character not
  2750. * contained in the first @a __n characters of @a __s within
  2751. * this string. If found, returns the index where it was
  2752. * found. If not found, returns npos.
  2753. */
  2754. _GLIBCXX20_CONSTEXPR
  2755. size_type
  2756. find_first_not_of(const _CharT* __s, size_type __pos,
  2757. size_type __n) const _GLIBCXX_NOEXCEPT;
  2758. /**
  2759. * @brief Find position of a character not in C string.
  2760. * @param __s C string containing characters to avoid.
  2761. * @param __pos Index of character to search from (default 0).
  2762. * @return Index of first occurrence.
  2763. *
  2764. * Starting from @a __pos, searches forward for a character not
  2765. * contained in @a __s within this string. If found, returns
  2766. * the index where it was found. If not found, returns npos.
  2767. */
  2768. _GLIBCXX20_CONSTEXPR
  2769. size_type
  2770. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  2771. _GLIBCXX_NOEXCEPT
  2772. {
  2773. __glibcxx_requires_string(__s);
  2774. return this->find_first_not_of(__s, __pos, traits_type::length(__s));
  2775. }
  2776. /**
  2777. * @brief Find position of a different character.
  2778. * @param __c Character to avoid.
  2779. * @param __pos Index of character to search from (default 0).
  2780. * @return Index of first occurrence.
  2781. *
  2782. * Starting from @a __pos, searches forward for a character
  2783. * other than @a __c within this string. If found, returns the
  2784. * index where it was found. If not found, returns npos.
  2785. */
  2786. _GLIBCXX20_CONSTEXPR
  2787. size_type
  2788. find_first_not_of(_CharT __c, size_type __pos = 0) const
  2789. _GLIBCXX_NOEXCEPT;
  2790. /**
  2791. * @brief Find last position of a character not in string.
  2792. * @param __str String containing characters to avoid.
  2793. * @param __pos Index of character to search back from (default end).
  2794. * @return Index of last occurrence.
  2795. *
  2796. * Starting from @a __pos, searches backward for a character
  2797. * not contained in @a __str within this string. If found,
  2798. * returns the index where it was found. If not found, returns
  2799. * npos.
  2800. */
  2801. _GLIBCXX20_CONSTEXPR
  2802. size_type
  2803. find_last_not_of(const basic_string& __str, size_type __pos = npos) const
  2804. _GLIBCXX_NOEXCEPT
  2805. { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
  2806. #if __cplusplus >= 201703L
  2807. /**
  2808. * @brief Find last position of a character not in a string_view.
  2809. * @param __svt An object convertible to string_view containing
  2810. * characters to avoid.
  2811. * @param __pos Index of character to search back from (default end).
  2812. * @return Index of last occurrence.
  2813. */
  2814. template<typename _Tp>
  2815. _GLIBCXX20_CONSTEXPR
  2816. _If_sv<_Tp, size_type>
  2817. find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
  2818. noexcept(is_same<_Tp, __sv_type>::value)
  2819. {
  2820. __sv_type __sv = __svt;
  2821. return this->find_last_not_of(__sv.data(), __pos, __sv.size());
  2822. }
  2823. #endif // C++17
  2824. /**
  2825. * @brief Find last position of a character not in C substring.
  2826. * @param __s C string containing characters to avoid.
  2827. * @param __pos Index of character to search back from.
  2828. * @param __n Number of characters from s to consider.
  2829. * @return Index of last occurrence.
  2830. *
  2831. * Starting from @a __pos, searches backward for a character not
  2832. * contained in the first @a __n characters of @a __s within this string.
  2833. * If found, returns the index where it was found. If not found,
  2834. * returns npos.
  2835. */
  2836. _GLIBCXX20_CONSTEXPR
  2837. size_type
  2838. find_last_not_of(const _CharT* __s, size_type __pos,
  2839. size_type __n) const _GLIBCXX_NOEXCEPT;
  2840. /**
  2841. * @brief Find last position of a character not in C string.
  2842. * @param __s C string containing characters to avoid.
  2843. * @param __pos Index of character to search back from (default end).
  2844. * @return Index of last occurrence.
  2845. *
  2846. * Starting from @a __pos, searches backward for a character
  2847. * not contained in @a __s within this string. If found,
  2848. * returns the index where it was found. If not found, returns
  2849. * npos.
  2850. */
  2851. _GLIBCXX20_CONSTEXPR
  2852. size_type
  2853. find_last_not_of(const _CharT* __s, size_type __pos = npos) const
  2854. _GLIBCXX_NOEXCEPT
  2855. {
  2856. __glibcxx_requires_string(__s);
  2857. return this->find_last_not_of(__s, __pos, traits_type::length(__s));
  2858. }
  2859. /**
  2860. * @brief Find last position of a different character.
  2861. * @param __c Character to avoid.
  2862. * @param __pos Index of character to search back from (default end).
  2863. * @return Index of last occurrence.
  2864. *
  2865. * Starting from @a __pos, searches backward for a character other than
  2866. * @a __c within this string. If found, returns the index where it was
  2867. * found. If not found, returns npos.
  2868. */
  2869. _GLIBCXX20_CONSTEXPR
  2870. size_type
  2871. find_last_not_of(_CharT __c, size_type __pos = npos) const
  2872. _GLIBCXX_NOEXCEPT;
  2873. /**
  2874. * @brief Get a substring.
  2875. * @param __pos Index of first character (default 0).
  2876. * @param __n Number of characters in substring (default remainder).
  2877. * @return The new string.
  2878. * @throw std::out_of_range If __pos > size().
  2879. *
  2880. * Construct and return a new string using the @a __n
  2881. * characters starting at @a __pos. If the string is too
  2882. * short, use the remainder of the characters. If @a __pos is
  2883. * beyond the end of the string, out_of_range is thrown.
  2884. */
  2885. _GLIBCXX20_CONSTEXPR
  2886. basic_string
  2887. substr(size_type __pos = 0, size_type __n = npos) const
  2888. { return basic_string(*this,
  2889. _M_check(__pos, "basic_string::substr"), __n); }
  2890. /**
  2891. * @brief Compare to a string.
  2892. * @param __str String to compare against.
  2893. * @return Integer < 0, 0, or > 0.
  2894. *
  2895. * Returns an integer < 0 if this string is ordered before @a
  2896. * __str, 0 if their values are equivalent, or > 0 if this
  2897. * string is ordered after @a __str. Determines the effective
  2898. * length rlen of the strings to compare as the smallest of
  2899. * size() and str.size(). The function then compares the two
  2900. * strings by calling traits::compare(data(), str.data(),rlen).
  2901. * If the result of the comparison is nonzero returns it,
  2902. * otherwise the shorter one is ordered first.
  2903. */
  2904. _GLIBCXX20_CONSTEXPR
  2905. int
  2906. compare(const basic_string& __str) const
  2907. {
  2908. const size_type __size = this->size();
  2909. const size_type __osize = __str.size();
  2910. const size_type __len = std::min(__size, __osize);
  2911. int __r = traits_type::compare(_M_data(), __str.data(), __len);
  2912. if (!__r)
  2913. __r = _S_compare(__size, __osize);
  2914. return __r;
  2915. }
  2916. #if __cplusplus >= 201703L
  2917. /**
  2918. * @brief Compare to a string_view.
  2919. * @param __svt An object convertible to string_view to compare against.
  2920. * @return Integer < 0, 0, or > 0.
  2921. */
  2922. template<typename _Tp>
  2923. _GLIBCXX20_CONSTEXPR
  2924. _If_sv<_Tp, int>
  2925. compare(const _Tp& __svt) const
  2926. noexcept(is_same<_Tp, __sv_type>::value)
  2927. {
  2928. __sv_type __sv = __svt;
  2929. const size_type __size = this->size();
  2930. const size_type __osize = __sv.size();
  2931. const size_type __len = std::min(__size, __osize);
  2932. int __r = traits_type::compare(_M_data(), __sv.data(), __len);
  2933. if (!__r)
  2934. __r = _S_compare(__size, __osize);
  2935. return __r;
  2936. }
  2937. /**
  2938. * @brief Compare to a string_view.
  2939. * @param __pos A position in the string to start comparing from.
  2940. * @param __n The number of characters to compare.
  2941. * @param __svt An object convertible to string_view to compare
  2942. * against.
  2943. * @return Integer < 0, 0, or > 0.
  2944. */
  2945. template<typename _Tp>
  2946. _GLIBCXX20_CONSTEXPR
  2947. _If_sv<_Tp, int>
  2948. compare(size_type __pos, size_type __n, const _Tp& __svt) const
  2949. noexcept(is_same<_Tp, __sv_type>::value)
  2950. {
  2951. __sv_type __sv = __svt;
  2952. return __sv_type(*this).substr(__pos, __n).compare(__sv);
  2953. }
  2954. /**
  2955. * @brief Compare to a string_view.
  2956. * @param __pos1 A position in the string to start comparing from.
  2957. * @param __n1 The number of characters to compare.
  2958. * @param __svt An object convertible to string_view to compare
  2959. * against.
  2960. * @param __pos2 A position in the string_view to start comparing from.
  2961. * @param __n2 The number of characters to compare.
  2962. * @return Integer < 0, 0, or > 0.
  2963. */
  2964. template<typename _Tp>
  2965. _GLIBCXX20_CONSTEXPR
  2966. _If_sv<_Tp, int>
  2967. compare(size_type __pos1, size_type __n1, const _Tp& __svt,
  2968. size_type __pos2, size_type __n2 = npos) const
  2969. noexcept(is_same<_Tp, __sv_type>::value)
  2970. {
  2971. __sv_type __sv = __svt;
  2972. return __sv_type(*this)
  2973. .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
  2974. }
  2975. #endif // C++17
  2976. /**
  2977. * @brief Compare substring to a string.
  2978. * @param __pos Index of first character of substring.
  2979. * @param __n Number of characters in substring.
  2980. * @param __str String to compare against.
  2981. * @return Integer < 0, 0, or > 0.
  2982. *
  2983. * Form the substring of this string from the @a __n characters
  2984. * starting at @a __pos. Returns an integer < 0 if the
  2985. * substring is ordered before @a __str, 0 if their values are
  2986. * equivalent, or > 0 if the substring is ordered after @a
  2987. * __str. Determines the effective length rlen of the strings
  2988. * to compare as the smallest of the length of the substring
  2989. * and @a __str.size(). The function then compares the two
  2990. * strings by calling
  2991. * traits::compare(substring.data(),str.data(),rlen). If the
  2992. * result of the comparison is nonzero returns it, otherwise
  2993. * the shorter one is ordered first.
  2994. */
  2995. _GLIBCXX20_CONSTEXPR
  2996. int
  2997. compare(size_type __pos, size_type __n, const basic_string& __str) const;
  2998. /**
  2999. * @brief Compare substring to a substring.
  3000. * @param __pos1 Index of first character of substring.
  3001. * @param __n1 Number of characters in substring.
  3002. * @param __str String to compare against.
  3003. * @param __pos2 Index of first character of substring of str.
  3004. * @param __n2 Number of characters in substring of str.
  3005. * @return Integer < 0, 0, or > 0.
  3006. *
  3007. * Form the substring of this string from the @a __n1
  3008. * characters starting at @a __pos1. Form the substring of @a
  3009. * __str from the @a __n2 characters starting at @a __pos2.
  3010. * Returns an integer < 0 if this substring is ordered before
  3011. * the substring of @a __str, 0 if their values are equivalent,
  3012. * or > 0 if this substring is ordered after the substring of
  3013. * @a __str. Determines the effective length rlen of the
  3014. * strings to compare as the smallest of the lengths of the
  3015. * substrings. The function then compares the two strings by
  3016. * calling
  3017. * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
  3018. * If the result of the comparison is nonzero returns it,
  3019. * otherwise the shorter one is ordered first.
  3020. */
  3021. _GLIBCXX20_CONSTEXPR
  3022. int
  3023. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  3024. size_type __pos2, size_type __n2 = npos) const;
  3025. /**
  3026. * @brief Compare to a C string.
  3027. * @param __s C string to compare against.
  3028. * @return Integer < 0, 0, or > 0.
  3029. *
  3030. * Returns an integer < 0 if this string is ordered before @a __s, 0 if
  3031. * their values are equivalent, or > 0 if this string is ordered after
  3032. * @a __s. Determines the effective length rlen of the strings to
  3033. * compare as the smallest of size() and the length of a string
  3034. * constructed from @a __s. The function then compares the two strings
  3035. * by calling traits::compare(data(),s,rlen). If the result of the
  3036. * comparison is nonzero returns it, otherwise the shorter one is
  3037. * ordered first.
  3038. */
  3039. _GLIBCXX20_CONSTEXPR
  3040. int
  3041. compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
  3042. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3043. // 5 String::compare specification questionable
  3044. /**
  3045. * @brief Compare substring to a C string.
  3046. * @param __pos Index of first character of substring.
  3047. * @param __n1 Number of characters in substring.
  3048. * @param __s C string to compare against.
  3049. * @return Integer < 0, 0, or > 0.
  3050. *
  3051. * Form the substring of this string from the @a __n1
  3052. * characters starting at @a pos. Returns an integer < 0 if
  3053. * the substring is ordered before @a __s, 0 if their values
  3054. * are equivalent, or > 0 if the substring is ordered after @a
  3055. * __s. Determines the effective length rlen of the strings to
  3056. * compare as the smallest of the length of the substring and
  3057. * the length of a string constructed from @a __s. The
  3058. * function then compares the two string by calling
  3059. * traits::compare(substring.data(),__s,rlen). If the result of
  3060. * the comparison is nonzero returns it, otherwise the shorter
  3061. * one is ordered first.
  3062. */
  3063. _GLIBCXX20_CONSTEXPR
  3064. int
  3065. compare(size_type __pos, size_type __n1, const _CharT* __s) const;
  3066. /**
  3067. * @brief Compare substring against a character %array.
  3068. * @param __pos Index of first character of substring.
  3069. * @param __n1 Number of characters in substring.
  3070. * @param __s character %array to compare against.
  3071. * @param __n2 Number of characters of s.
  3072. * @return Integer < 0, 0, or > 0.
  3073. *
  3074. * Form the substring of this string from the @a __n1
  3075. * characters starting at @a __pos. Form a string from the
  3076. * first @a __n2 characters of @a __s. Returns an integer < 0
  3077. * if this substring is ordered before the string from @a __s,
  3078. * 0 if their values are equivalent, or > 0 if this substring
  3079. * is ordered after the string from @a __s. Determines the
  3080. * effective length rlen of the strings to compare as the
  3081. * smallest of the length of the substring and @a __n2. The
  3082. * function then compares the two strings by calling
  3083. * traits::compare(substring.data(),s,rlen). If the result of
  3084. * the comparison is nonzero returns it, otherwise the shorter
  3085. * one is ordered first.
  3086. *
  3087. * NB: s must have at least n2 characters, &apos;\\0&apos; has
  3088. * no special meaning.
  3089. */
  3090. _GLIBCXX20_CONSTEXPR
  3091. int
  3092. compare(size_type __pos, size_type __n1, const _CharT* __s,
  3093. size_type __n2) const;
  3094. #if __cplusplus >= 202002L
  3095. constexpr bool
  3096. starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  3097. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  3098. constexpr bool
  3099. starts_with(_CharT __x) const noexcept
  3100. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  3101. constexpr bool
  3102. starts_with(const _CharT* __x) const noexcept
  3103. { return __sv_type(this->data(), this->size()).starts_with(__x); }
  3104. constexpr bool
  3105. ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
  3106. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  3107. constexpr bool
  3108. ends_with(_CharT __x) const noexcept
  3109. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  3110. constexpr bool
  3111. ends_with(const _CharT* __x) const noexcept
  3112. { return __sv_type(this->data(), this->size()).ends_with(__x); }
  3113. #endif // C++20
  3114. #if __cplusplus > 202002L
  3115. constexpr bool
  3116. contains(basic_string_view<_CharT, _Traits> __x) const noexcept
  3117. { return __sv_type(this->data(), this->size()).contains(__x); }
  3118. constexpr bool
  3119. contains(_CharT __x) const noexcept
  3120. { return __sv_type(this->data(), this->size()).contains(__x); }
  3121. constexpr bool
  3122. contains(const _CharT* __x) const noexcept
  3123. { return __sv_type(this->data(), this->size()).contains(__x); }
  3124. #endif // C++23
  3125. // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
  3126. template<typename, typename, typename> friend class basic_stringbuf;
  3127. };
  3128. _GLIBCXX_END_NAMESPACE_CXX11
  3129. _GLIBCXX_END_NAMESPACE_VERSION
  3130. } // namespace std
  3131. #endif // _GLIBCXX_USE_CXX11_ABI
  3132. namespace std _GLIBCXX_VISIBILITY(default)
  3133. {
  3134. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  3135. #if __cpp_deduction_guides >= 201606
  3136. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  3137. template<typename _InputIterator, typename _CharT
  3138. = typename iterator_traits<_InputIterator>::value_type,
  3139. typename _Allocator = allocator<_CharT>,
  3140. typename = _RequireInputIter<_InputIterator>,
  3141. typename = _RequireAllocator<_Allocator>>
  3142. basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
  3143. -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
  3144. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3145. // 3075. basic_string needs deduction guides from basic_string_view
  3146. template<typename _CharT, typename _Traits,
  3147. typename _Allocator = allocator<_CharT>,
  3148. typename = _RequireAllocator<_Allocator>>
  3149. basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
  3150. -> basic_string<_CharT, _Traits, _Allocator>;
  3151. template<typename _CharT, typename _Traits,
  3152. typename _Allocator = allocator<_CharT>,
  3153. typename = _RequireAllocator<_Allocator>>
  3154. basic_string(basic_string_view<_CharT, _Traits>,
  3155. typename basic_string<_CharT, _Traits, _Allocator>::size_type,
  3156. typename basic_string<_CharT, _Traits, _Allocator>::size_type,
  3157. const _Allocator& = _Allocator())
  3158. -> basic_string<_CharT, _Traits, _Allocator>;
  3159. _GLIBCXX_END_NAMESPACE_CXX11
  3160. #endif
  3161. // operator+
  3162. /**
  3163. * @brief Concatenate two strings.
  3164. * @param __lhs First string.
  3165. * @param __rhs Last string.
  3166. * @return New string with value of @a __lhs followed by @a __rhs.
  3167. */
  3168. template<typename _CharT, typename _Traits, typename _Alloc>
  3169. _GLIBCXX20_CONSTEXPR
  3170. basic_string<_CharT, _Traits, _Alloc>
  3171. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3172. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3173. {
  3174. basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
  3175. __str.append(__rhs);
  3176. return __str;
  3177. }
  3178. /**
  3179. * @brief Concatenate C string and string.
  3180. * @param __lhs First string.
  3181. * @param __rhs Last string.
  3182. * @return New string with value of @a __lhs followed by @a __rhs.
  3183. */
  3184. template<typename _CharT, typename _Traits, typename _Alloc>
  3185. _GLIBCXX20_CONSTEXPR
  3186. basic_string<_CharT,_Traits,_Alloc>
  3187. operator+(const _CharT* __lhs,
  3188. const basic_string<_CharT,_Traits,_Alloc>& __rhs);
  3189. /**
  3190. * @brief Concatenate character and string.
  3191. * @param __lhs First string.
  3192. * @param __rhs Last string.
  3193. * @return New string with @a __lhs followed by @a __rhs.
  3194. */
  3195. template<typename _CharT, typename _Traits, typename _Alloc>
  3196. _GLIBCXX20_CONSTEXPR
  3197. basic_string<_CharT,_Traits,_Alloc>
  3198. operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
  3199. /**
  3200. * @brief Concatenate string and C string.
  3201. * @param __lhs First string.
  3202. * @param __rhs Last string.
  3203. * @return New string with @a __lhs followed by @a __rhs.
  3204. */
  3205. template<typename _CharT, typename _Traits, typename _Alloc>
  3206. _GLIBCXX20_CONSTEXPR
  3207. inline basic_string<_CharT, _Traits, _Alloc>
  3208. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3209. const _CharT* __rhs)
  3210. {
  3211. basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
  3212. __str.append(__rhs);
  3213. return __str;
  3214. }
  3215. /**
  3216. * @brief Concatenate string and character.
  3217. * @param __lhs First string.
  3218. * @param __rhs Last string.
  3219. * @return New string with @a __lhs followed by @a __rhs.
  3220. */
  3221. template<typename _CharT, typename _Traits, typename _Alloc>
  3222. _GLIBCXX20_CONSTEXPR
  3223. inline basic_string<_CharT, _Traits, _Alloc>
  3224. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
  3225. {
  3226. typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
  3227. typedef typename __string_type::size_type __size_type;
  3228. __string_type __str(__lhs);
  3229. __str.append(__size_type(1), __rhs);
  3230. return __str;
  3231. }
  3232. #if __cplusplus >= 201103L
  3233. template<typename _CharT, typename _Traits, typename _Alloc>
  3234. _GLIBCXX20_CONSTEXPR
  3235. inline basic_string<_CharT, _Traits, _Alloc>
  3236. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  3237. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3238. { return std::move(__lhs.append(__rhs)); }
  3239. template<typename _CharT, typename _Traits, typename _Alloc>
  3240. _GLIBCXX20_CONSTEXPR
  3241. inline basic_string<_CharT, _Traits, _Alloc>
  3242. operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3243. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  3244. { return std::move(__rhs.insert(0, __lhs)); }
  3245. template<typename _CharT, typename _Traits, typename _Alloc>
  3246. _GLIBCXX20_CONSTEXPR
  3247. inline basic_string<_CharT, _Traits, _Alloc>
  3248. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  3249. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  3250. {
  3251. #if _GLIBCXX_USE_CXX11_ABI
  3252. using _Alloc_traits = allocator_traits<_Alloc>;
  3253. bool __use_rhs = false;
  3254. if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{})
  3255. __use_rhs = true;
  3256. else if (__lhs.get_allocator() == __rhs.get_allocator())
  3257. __use_rhs = true;
  3258. if (__use_rhs)
  3259. #endif
  3260. {
  3261. const auto __size = __lhs.size() + __rhs.size();
  3262. if (__size > __lhs.capacity() && __size <= __rhs.capacity())
  3263. return std::move(__rhs.insert(0, __lhs));
  3264. }
  3265. return std::move(__lhs.append(__rhs));
  3266. }
  3267. template<typename _CharT, typename _Traits, typename _Alloc>
  3268. _GLIBCXX20_CONSTEXPR
  3269. inline basic_string<_CharT, _Traits, _Alloc>
  3270. operator+(const _CharT* __lhs,
  3271. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  3272. { return std::move(__rhs.insert(0, __lhs)); }
  3273. template<typename _CharT, typename _Traits, typename _Alloc>
  3274. _GLIBCXX20_CONSTEXPR
  3275. inline basic_string<_CharT, _Traits, _Alloc>
  3276. operator+(_CharT __lhs,
  3277. basic_string<_CharT, _Traits, _Alloc>&& __rhs)
  3278. { return std::move(__rhs.insert(0, 1, __lhs)); }
  3279. template<typename _CharT, typename _Traits, typename _Alloc>
  3280. _GLIBCXX20_CONSTEXPR
  3281. inline basic_string<_CharT, _Traits, _Alloc>
  3282. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  3283. const _CharT* __rhs)
  3284. { return std::move(__lhs.append(__rhs)); }
  3285. template<typename _CharT, typename _Traits, typename _Alloc>
  3286. _GLIBCXX20_CONSTEXPR
  3287. inline basic_string<_CharT, _Traits, _Alloc>
  3288. operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
  3289. _CharT __rhs)
  3290. { return std::move(__lhs.append(1, __rhs)); }
  3291. #endif
  3292. // operator ==
  3293. /**
  3294. * @brief Test equivalence of two strings.
  3295. * @param __lhs First string.
  3296. * @param __rhs Second string.
  3297. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  3298. */
  3299. template<typename _CharT, typename _Traits, typename _Alloc>
  3300. _GLIBCXX20_CONSTEXPR
  3301. inline bool
  3302. operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3303. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3304. _GLIBCXX_NOEXCEPT
  3305. { return __lhs.compare(__rhs) == 0; }
  3306. template<typename _CharT>
  3307. _GLIBCXX20_CONSTEXPR
  3308. inline
  3309. typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
  3310. operator==(const basic_string<_CharT>& __lhs,
  3311. const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
  3312. { return (__lhs.size() == __rhs.size()
  3313. && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
  3314. __lhs.size())); }
  3315. /**
  3316. * @brief Test equivalence of string and C string.
  3317. * @param __lhs String.
  3318. * @param __rhs C string.
  3319. * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
  3320. */
  3321. template<typename _CharT, typename _Traits, typename _Alloc>
  3322. _GLIBCXX20_CONSTEXPR
  3323. inline bool
  3324. operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3325. const _CharT* __rhs)
  3326. { return __lhs.compare(__rhs) == 0; }
  3327. #if __cpp_lib_three_way_comparison
  3328. /**
  3329. * @brief Three-way comparison of a string and a C string.
  3330. * @param __lhs A string.
  3331. * @param __rhs A null-terminated string.
  3332. * @return A value indicating whether `__lhs` is less than, equal to,
  3333. * greater than, or incomparable with `__rhs`.
  3334. */
  3335. template<typename _CharT, typename _Traits, typename _Alloc>
  3336. constexpr auto
  3337. operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3338. const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
  3339. -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
  3340. { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
  3341. /**
  3342. * @brief Three-way comparison of a string and a C string.
  3343. * @param __lhs A string.
  3344. * @param __rhs A null-terminated string.
  3345. * @return A value indicating whether `__lhs` is less than, equal to,
  3346. * greater than, or incomparable with `__rhs`.
  3347. */
  3348. template<typename _CharT, typename _Traits, typename _Alloc>
  3349. constexpr auto
  3350. operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3351. const _CharT* __rhs) noexcept
  3352. -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
  3353. { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
  3354. #else
  3355. /**
  3356. * @brief Test equivalence of C string and string.
  3357. * @param __lhs C string.
  3358. * @param __rhs String.
  3359. * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
  3360. */
  3361. template<typename _CharT, typename _Traits, typename _Alloc>
  3362. inline bool
  3363. operator==(const _CharT* __lhs,
  3364. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3365. { return __rhs.compare(__lhs) == 0; }
  3366. // operator !=
  3367. /**
  3368. * @brief Test difference of two strings.
  3369. * @param __lhs First string.
  3370. * @param __rhs Second string.
  3371. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  3372. */
  3373. template<typename _CharT, typename _Traits, typename _Alloc>
  3374. inline bool
  3375. operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3376. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3377. _GLIBCXX_NOEXCEPT
  3378. { return !(__lhs == __rhs); }
  3379. /**
  3380. * @brief Test difference of C string and string.
  3381. * @param __lhs C string.
  3382. * @param __rhs String.
  3383. * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
  3384. */
  3385. template<typename _CharT, typename _Traits, typename _Alloc>
  3386. inline bool
  3387. operator!=(const _CharT* __lhs,
  3388. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3389. { return !(__lhs == __rhs); }
  3390. /**
  3391. * @brief Test difference of string and C string.
  3392. * @param __lhs String.
  3393. * @param __rhs C string.
  3394. * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
  3395. */
  3396. template<typename _CharT, typename _Traits, typename _Alloc>
  3397. inline bool
  3398. operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3399. const _CharT* __rhs)
  3400. { return !(__lhs == __rhs); }
  3401. // operator <
  3402. /**
  3403. * @brief Test if string precedes string.
  3404. * @param __lhs First string.
  3405. * @param __rhs Second string.
  3406. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  3407. */
  3408. template<typename _CharT, typename _Traits, typename _Alloc>
  3409. inline bool
  3410. operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3411. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3412. _GLIBCXX_NOEXCEPT
  3413. { return __lhs.compare(__rhs) < 0; }
  3414. /**
  3415. * @brief Test if string precedes C string.
  3416. * @param __lhs String.
  3417. * @param __rhs C string.
  3418. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  3419. */
  3420. template<typename _CharT, typename _Traits, typename _Alloc>
  3421. inline bool
  3422. operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3423. const _CharT* __rhs)
  3424. { return __lhs.compare(__rhs) < 0; }
  3425. /**
  3426. * @brief Test if C string precedes string.
  3427. * @param __lhs C string.
  3428. * @param __rhs String.
  3429. * @return True if @a __lhs precedes @a __rhs. False otherwise.
  3430. */
  3431. template<typename _CharT, typename _Traits, typename _Alloc>
  3432. inline bool
  3433. operator<(const _CharT* __lhs,
  3434. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3435. { return __rhs.compare(__lhs) > 0; }
  3436. // operator >
  3437. /**
  3438. * @brief Test if string follows string.
  3439. * @param __lhs First string.
  3440. * @param __rhs Second string.
  3441. * @return True if @a __lhs follows @a __rhs. False otherwise.
  3442. */
  3443. template<typename _CharT, typename _Traits, typename _Alloc>
  3444. inline bool
  3445. operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3446. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3447. _GLIBCXX_NOEXCEPT
  3448. { return __lhs.compare(__rhs) > 0; }
  3449. /**
  3450. * @brief Test if string follows C string.
  3451. * @param __lhs String.
  3452. * @param __rhs C string.
  3453. * @return True if @a __lhs follows @a __rhs. False otherwise.
  3454. */
  3455. template<typename _CharT, typename _Traits, typename _Alloc>
  3456. inline bool
  3457. operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3458. const _CharT* __rhs)
  3459. { return __lhs.compare(__rhs) > 0; }
  3460. /**
  3461. * @brief Test if C string follows string.
  3462. * @param __lhs C string.
  3463. * @param __rhs String.
  3464. * @return True if @a __lhs follows @a __rhs. False otherwise.
  3465. */
  3466. template<typename _CharT, typename _Traits, typename _Alloc>
  3467. inline bool
  3468. operator>(const _CharT* __lhs,
  3469. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3470. { return __rhs.compare(__lhs) < 0; }
  3471. // operator <=
  3472. /**
  3473. * @brief Test if string doesn't follow string.
  3474. * @param __lhs First string.
  3475. * @param __rhs Second string.
  3476. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  3477. */
  3478. template<typename _CharT, typename _Traits, typename _Alloc>
  3479. inline bool
  3480. operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3481. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3482. _GLIBCXX_NOEXCEPT
  3483. { return __lhs.compare(__rhs) <= 0; }
  3484. /**
  3485. * @brief Test if string doesn't follow C string.
  3486. * @param __lhs String.
  3487. * @param __rhs C string.
  3488. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  3489. */
  3490. template<typename _CharT, typename _Traits, typename _Alloc>
  3491. inline bool
  3492. operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3493. const _CharT* __rhs)
  3494. { return __lhs.compare(__rhs) <= 0; }
  3495. /**
  3496. * @brief Test if C string doesn't follow string.
  3497. * @param __lhs C string.
  3498. * @param __rhs String.
  3499. * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
  3500. */
  3501. template<typename _CharT, typename _Traits, typename _Alloc>
  3502. inline bool
  3503. operator<=(const _CharT* __lhs,
  3504. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3505. { return __rhs.compare(__lhs) >= 0; }
  3506. // operator >=
  3507. /**
  3508. * @brief Test if string doesn't precede string.
  3509. * @param __lhs First string.
  3510. * @param __rhs Second string.
  3511. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  3512. */
  3513. template<typename _CharT, typename _Traits, typename _Alloc>
  3514. inline bool
  3515. operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3516. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3517. _GLIBCXX_NOEXCEPT
  3518. { return __lhs.compare(__rhs) >= 0; }
  3519. /**
  3520. * @brief Test if string doesn't precede C string.
  3521. * @param __lhs String.
  3522. * @param __rhs C string.
  3523. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  3524. */
  3525. template<typename _CharT, typename _Traits, typename _Alloc>
  3526. inline bool
  3527. operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3528. const _CharT* __rhs)
  3529. { return __lhs.compare(__rhs) >= 0; }
  3530. /**
  3531. * @brief Test if C string doesn't precede string.
  3532. * @param __lhs C string.
  3533. * @param __rhs String.
  3534. * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
  3535. */
  3536. template<typename _CharT, typename _Traits, typename _Alloc>
  3537. inline bool
  3538. operator>=(const _CharT* __lhs,
  3539. const basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3540. { return __rhs.compare(__lhs) <= 0; }
  3541. #endif // three-way comparison
  3542. /**
  3543. * @brief Swap contents of two strings.
  3544. * @param __lhs First string.
  3545. * @param __rhs Second string.
  3546. *
  3547. * Exchanges the contents of @a __lhs and @a __rhs in constant time.
  3548. */
  3549. template<typename _CharT, typename _Traits, typename _Alloc>
  3550. _GLIBCXX20_CONSTEXPR
  3551. inline void
  3552. swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
  3553. basic_string<_CharT, _Traits, _Alloc>& __rhs)
  3554. _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
  3555. { __lhs.swap(__rhs); }
  3556. /**
  3557. * @brief Read stream into a string.
  3558. * @param __is Input stream.
  3559. * @param __str Buffer to store into.
  3560. * @return Reference to the input stream.
  3561. *
  3562. * Stores characters from @a __is into @a __str until whitespace is
  3563. * found, the end of the stream is encountered, or str.max_size()
  3564. * is reached. If is.width() is non-zero, that is the limit on the
  3565. * number of characters stored into @a __str. Any previous
  3566. * contents of @a __str are erased.
  3567. */
  3568. template<typename _CharT, typename _Traits, typename _Alloc>
  3569. basic_istream<_CharT, _Traits>&
  3570. operator>>(basic_istream<_CharT, _Traits>& __is,
  3571. basic_string<_CharT, _Traits, _Alloc>& __str);
  3572. template<>
  3573. basic_istream<char>&
  3574. operator>>(basic_istream<char>& __is, basic_string<char>& __str);
  3575. /**
  3576. * @brief Write string to a stream.
  3577. * @param __os Output stream.
  3578. * @param __str String to write out.
  3579. * @return Reference to the output stream.
  3580. *
  3581. * Output characters of @a __str into os following the same rules as for
  3582. * writing a C string.
  3583. */
  3584. template<typename _CharT, typename _Traits, typename _Alloc>
  3585. inline basic_ostream<_CharT, _Traits>&
  3586. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3587. const basic_string<_CharT, _Traits, _Alloc>& __str)
  3588. {
  3589. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  3590. // 586. string inserter not a formatted function
  3591. return __ostream_insert(__os, __str.data(), __str.size());
  3592. }
  3593. /**
  3594. * @brief Read a line from stream into a string.
  3595. * @param __is Input stream.
  3596. * @param __str Buffer to store into.
  3597. * @param __delim Character marking end of line.
  3598. * @return Reference to the input stream.
  3599. *
  3600. * Stores characters from @a __is into @a __str until @a __delim is
  3601. * found, the end of the stream is encountered, or str.max_size()
  3602. * is reached. Any previous contents of @a __str are erased. If
  3603. * @a __delim is encountered, it is extracted but not stored into
  3604. * @a __str.
  3605. */
  3606. template<typename _CharT, typename _Traits, typename _Alloc>
  3607. basic_istream<_CharT, _Traits>&
  3608. getline(basic_istream<_CharT, _Traits>& __is,
  3609. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
  3610. /**
  3611. * @brief Read a line from stream into a string.
  3612. * @param __is Input stream.
  3613. * @param __str Buffer to store into.
  3614. * @return Reference to the input stream.
  3615. *
  3616. * Stores characters from is into @a __str until &apos;\n&apos; is
  3617. * found, the end of the stream is encountered, or str.max_size()
  3618. * is reached. Any previous contents of @a __str are erased. If
  3619. * end of line is encountered, it is extracted but not stored into
  3620. * @a __str.
  3621. */
  3622. template<typename _CharT, typename _Traits, typename _Alloc>
  3623. inline basic_istream<_CharT, _Traits>&
  3624. getline(basic_istream<_CharT, _Traits>& __is,
  3625. basic_string<_CharT, _Traits, _Alloc>& __str)
  3626. { return std::getline(__is, __str, __is.widen('\n')); }
  3627. #if __cplusplus >= 201103L
  3628. /// Read a line from an rvalue stream into a string.
  3629. template<typename _CharT, typename _Traits, typename _Alloc>
  3630. inline basic_istream<_CharT, _Traits>&
  3631. getline(basic_istream<_CharT, _Traits>&& __is,
  3632. basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
  3633. { return std::getline(__is, __str, __delim); }
  3634. /// Read a line from an rvalue stream into a string.
  3635. template<typename _CharT, typename _Traits, typename _Alloc>
  3636. inline basic_istream<_CharT, _Traits>&
  3637. getline(basic_istream<_CharT, _Traits>&& __is,
  3638. basic_string<_CharT, _Traits, _Alloc>& __str)
  3639. { return std::getline(__is, __str); }
  3640. #endif
  3641. template<>
  3642. basic_istream<char>&
  3643. getline(basic_istream<char>& __in, basic_string<char>& __str,
  3644. char __delim);
  3645. #ifdef _GLIBCXX_USE_WCHAR_T
  3646. template<>
  3647. basic_istream<wchar_t>&
  3648. getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
  3649. wchar_t __delim);
  3650. #endif
  3651. _GLIBCXX_END_NAMESPACE_VERSION
  3652. } // namespace
  3653. #if __cplusplus >= 201103L
  3654. #include <ext/string_conversions.h>
  3655. #include <bits/charconv.h>
  3656. namespace std _GLIBCXX_VISIBILITY(default)
  3657. {
  3658. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  3659. _GLIBCXX_BEGIN_NAMESPACE_CXX11
  3660. #if _GLIBCXX_USE_C99_STDLIB
  3661. // 21.4 Numeric Conversions [string.conversions].
  3662. inline int
  3663. stoi(const string& __str, size_t* __idx = 0, int __base = 10)
  3664. { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
  3665. __idx, __base); }
  3666. inline long
  3667. stol(const string& __str, size_t* __idx = 0, int __base = 10)
  3668. { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
  3669. __idx, __base); }
  3670. inline unsigned long
  3671. stoul(const string& __str, size_t* __idx = 0, int __base = 10)
  3672. { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
  3673. __idx, __base); }
  3674. inline long long
  3675. stoll(const string& __str, size_t* __idx = 0, int __base = 10)
  3676. { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
  3677. __idx, __base); }
  3678. inline unsigned long long
  3679. stoull(const string& __str, size_t* __idx = 0, int __base = 10)
  3680. { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
  3681. __idx, __base); }
  3682. // NB: strtof vs strtod.
  3683. inline float
  3684. stof(const string& __str, size_t* __idx = 0)
  3685. { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
  3686. inline double
  3687. stod(const string& __str, size_t* __idx = 0)
  3688. { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
  3689. inline long double
  3690. stold(const string& __str, size_t* __idx = 0)
  3691. { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
  3692. #endif // _GLIBCXX_USE_C99_STDLIB
  3693. // DR 1261. Insufficent overloads for to_string / to_wstring
  3694. inline string
  3695. to_string(int __val)
  3696. #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
  3697. noexcept // any 32-bit value fits in the SSO buffer
  3698. #endif
  3699. {
  3700. const bool __neg = __val < 0;
  3701. const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
  3702. const auto __len = __detail::__to_chars_len(__uval);
  3703. string __str(__neg + __len, '-');
  3704. __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
  3705. return __str;
  3706. }
  3707. inline string
  3708. to_string(unsigned __val)
  3709. #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
  3710. noexcept // any 32-bit value fits in the SSO buffer
  3711. #endif
  3712. {
  3713. string __str(__detail::__to_chars_len(__val), '\0');
  3714. __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
  3715. return __str;
  3716. }
  3717. inline string
  3718. to_string(long __val)
  3719. #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
  3720. noexcept // any 32-bit value fits in the SSO buffer
  3721. #endif
  3722. {
  3723. const bool __neg = __val < 0;
  3724. const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
  3725. const auto __len = __detail::__to_chars_len(__uval);
  3726. string __str(__neg + __len, '-');
  3727. __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
  3728. return __str;
  3729. }
  3730. inline string
  3731. to_string(unsigned long __val)
  3732. #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
  3733. noexcept // any 32-bit value fits in the SSO buffer
  3734. #endif
  3735. {
  3736. string __str(__detail::__to_chars_len(__val), '\0');
  3737. __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
  3738. return __str;
  3739. }
  3740. inline string
  3741. to_string(long long __val)
  3742. {
  3743. const bool __neg = __val < 0;
  3744. const unsigned long long __uval
  3745. = __neg ? (unsigned long long)~__val + 1ull : __val;
  3746. const auto __len = __detail::__to_chars_len(__uval);
  3747. string __str(__neg + __len, '-');
  3748. __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
  3749. return __str;
  3750. }
  3751. inline string
  3752. to_string(unsigned long long __val)
  3753. {
  3754. string __str(__detail::__to_chars_len(__val), '\0');
  3755. __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
  3756. return __str;
  3757. }
  3758. #if _GLIBCXX_USE_C99_STDIO
  3759. // NB: (v)snprintf vs sprintf.
  3760. inline string
  3761. to_string(float __val)
  3762. {
  3763. const int __n =
  3764. __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
  3765. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  3766. "%f", __val);
  3767. }
  3768. inline string
  3769. to_string(double __val)
  3770. {
  3771. const int __n =
  3772. __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
  3773. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  3774. "%f", __val);
  3775. }
  3776. inline string
  3777. to_string(long double __val)
  3778. {
  3779. const int __n =
  3780. __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
  3781. return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
  3782. "%Lf", __val);
  3783. }
  3784. #endif // _GLIBCXX_USE_C99_STDIO
  3785. #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
  3786. inline int
  3787. stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
  3788. { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
  3789. __idx, __base); }
  3790. inline long
  3791. stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
  3792. { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
  3793. __idx, __base); }
  3794. inline unsigned long
  3795. stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
  3796. { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
  3797. __idx, __base); }
  3798. inline long long
  3799. stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
  3800. { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
  3801. __idx, __base); }
  3802. inline unsigned long long
  3803. stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
  3804. { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
  3805. __idx, __base); }
  3806. // NB: wcstof vs wcstod.
  3807. inline float
  3808. stof(const wstring& __str, size_t* __idx = 0)
  3809. { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
  3810. inline double
  3811. stod(const wstring& __str, size_t* __idx = 0)
  3812. { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
  3813. inline long double
  3814. stold(const wstring& __str, size_t* __idx = 0)
  3815. { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
  3816. #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  3817. // DR 1261.
  3818. inline wstring
  3819. to_wstring(int __val)
  3820. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
  3821. L"%d", __val); }
  3822. inline wstring
  3823. to_wstring(unsigned __val)
  3824. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  3825. 4 * sizeof(unsigned),
  3826. L"%u", __val); }
  3827. inline wstring
  3828. to_wstring(long __val)
  3829. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
  3830. L"%ld", __val); }
  3831. inline wstring
  3832. to_wstring(unsigned long __val)
  3833. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  3834. 4 * sizeof(unsigned long),
  3835. L"%lu", __val); }
  3836. inline wstring
  3837. to_wstring(long long __val)
  3838. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  3839. 4 * sizeof(long long),
  3840. L"%lld", __val); }
  3841. inline wstring
  3842. to_wstring(unsigned long long __val)
  3843. { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
  3844. 4 * sizeof(unsigned long long),
  3845. L"%llu", __val); }
  3846. inline wstring
  3847. to_wstring(float __val)
  3848. {
  3849. const int __n =
  3850. __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
  3851. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  3852. L"%f", __val);
  3853. }
  3854. inline wstring
  3855. to_wstring(double __val)
  3856. {
  3857. const int __n =
  3858. __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
  3859. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  3860. L"%f", __val);
  3861. }
  3862. inline wstring
  3863. to_wstring(long double __val)
  3864. {
  3865. const int __n =
  3866. __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
  3867. return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
  3868. L"%Lf", __val);
  3869. }
  3870. #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
  3871. #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
  3872. _GLIBCXX_END_NAMESPACE_CXX11
  3873. _GLIBCXX_END_NAMESPACE_VERSION
  3874. } // namespace
  3875. #endif /* C++11 */
  3876. #if __cplusplus >= 201103L
  3877. #include <bits/functional_hash.h>
  3878. namespace std _GLIBCXX_VISIBILITY(default)
  3879. {
  3880. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  3881. // DR 1182.
  3882. #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
  3883. /// std::hash specialization for string.
  3884. template<>
  3885. struct hash<string>
  3886. : public __hash_base<size_t, string>
  3887. {
  3888. size_t
  3889. operator()(const string& __s) const noexcept
  3890. { return std::_Hash_impl::hash(__s.data(), __s.length()); }
  3891. };
  3892. template<>
  3893. struct __is_fast_hash<hash<string>> : std::false_type
  3894. { };
  3895. /// std::hash specialization for wstring.
  3896. template<>
  3897. struct hash<wstring>
  3898. : public __hash_base<size_t, wstring>
  3899. {
  3900. size_t
  3901. operator()(const wstring& __s) const noexcept
  3902. { return std::_Hash_impl::hash(__s.data(),
  3903. __s.length() * sizeof(wchar_t)); }
  3904. };
  3905. template<>
  3906. struct __is_fast_hash<hash<wstring>> : std::false_type
  3907. { };
  3908. #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
  3909. #ifdef _GLIBCXX_USE_CHAR8_T
  3910. /// std::hash specialization for u8string.
  3911. template<>
  3912. struct hash<u8string>
  3913. : public __hash_base<size_t, u8string>
  3914. {
  3915. size_t
  3916. operator()(const u8string& __s) const noexcept
  3917. { return std::_Hash_impl::hash(__s.data(),
  3918. __s.length() * sizeof(char8_t)); }
  3919. };
  3920. template<>
  3921. struct __is_fast_hash<hash<u8string>> : std::false_type
  3922. { };
  3923. #endif
  3924. /// std::hash specialization for u16string.
  3925. template<>
  3926. struct hash<u16string>
  3927. : public __hash_base<size_t, u16string>
  3928. {
  3929. size_t
  3930. operator()(const u16string& __s) const noexcept
  3931. { return std::_Hash_impl::hash(__s.data(),
  3932. __s.length() * sizeof(char16_t)); }
  3933. };
  3934. template<>
  3935. struct __is_fast_hash<hash<u16string>> : std::false_type
  3936. { };
  3937. /// std::hash specialization for u32string.
  3938. template<>
  3939. struct hash<u32string>
  3940. : public __hash_base<size_t, u32string>
  3941. {
  3942. size_t
  3943. operator()(const u32string& __s) const noexcept
  3944. { return std::_Hash_impl::hash(__s.data(),
  3945. __s.length() * sizeof(char32_t)); }
  3946. };
  3947. template<>
  3948. struct __is_fast_hash<hash<u32string>> : std::false_type
  3949. { };
  3950. #if __cplusplus >= 201402L
  3951. #define __cpp_lib_string_udls 201304L
  3952. inline namespace literals
  3953. {
  3954. inline namespace string_literals
  3955. {
  3956. #pragma GCC diagnostic push
  3957. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  3958. #if __cpp_lib_constexpr_string >= 201907L
  3959. # define _GLIBCXX_STRING_CONSTEXPR constexpr
  3960. #else
  3961. # define _GLIBCXX_STRING_CONSTEXPR
  3962. #endif
  3963. _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
  3964. inline basic_string<char>
  3965. operator""s(const char* __str, size_t __len)
  3966. { return basic_string<char>{__str, __len}; }
  3967. _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
  3968. inline basic_string<wchar_t>
  3969. operator""s(const wchar_t* __str, size_t __len)
  3970. { return basic_string<wchar_t>{__str, __len}; }
  3971. #ifdef _GLIBCXX_USE_CHAR8_T
  3972. _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
  3973. inline basic_string<char8_t>
  3974. operator""s(const char8_t* __str, size_t __len)
  3975. { return basic_string<char8_t>{__str, __len}; }
  3976. #endif
  3977. _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
  3978. inline basic_string<char16_t>
  3979. operator""s(const char16_t* __str, size_t __len)
  3980. { return basic_string<char16_t>{__str, __len}; }
  3981. _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
  3982. inline basic_string<char32_t>
  3983. operator""s(const char32_t* __str, size_t __len)
  3984. { return basic_string<char32_t>{__str, __len}; }
  3985. #undef _GLIBCXX_STRING_CONSTEXPR
  3986. #pragma GCC diagnostic pop
  3987. } // inline namespace string_literals
  3988. } // inline namespace literals
  3989. #if __cplusplus >= 201703L
  3990. namespace __detail::__variant
  3991. {
  3992. template<typename> struct _Never_valueless_alt; // see <variant>
  3993. // Provide the strong exception-safety guarantee when emplacing a
  3994. // basic_string into a variant, but only if moving the string cannot throw.
  3995. template<typename _Tp, typename _Traits, typename _Alloc>
  3996. struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
  3997. : __and_<
  3998. is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
  3999. is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
  4000. >::type
  4001. { };
  4002. } // namespace __detail::__variant
  4003. #endif // C++17
  4004. #endif // C++14
  4005. _GLIBCXX_END_NAMESPACE_VERSION
  4006. } // namespace std
  4007. #endif // C++11
  4008. #endif /* _BASIC_STRING_H */