basic_string.h 156 KB

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