string 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330
  1. // -*- C++ -*-
  2. //===--------------------------- string -----------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is distributed under the University of Illinois Open Source
  7. // License. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_STRING
  11. #define _LIBCPP_STRING
  12. /*
  13. string synopsis
  14. namespace std
  15. {
  16. template <class stateT>
  17. class fpos
  18. {
  19. private:
  20. stateT st;
  21. public:
  22. fpos(streamoff = streamoff());
  23. operator streamoff() const;
  24. stateT state() const;
  25. void state(stateT);
  26. fpos& operator+=(streamoff);
  27. fpos operator+ (streamoff) const;
  28. fpos& operator-=(streamoff);
  29. fpos operator- (streamoff) const;
  30. };
  31. template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
  32. template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
  33. template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
  34. template <class charT>
  35. struct char_traits
  36. {
  37. typedef charT char_type;
  38. typedef ... int_type;
  39. typedef streamoff off_type;
  40. typedef streampos pos_type;
  41. typedef mbstate_t state_type;
  42. static void assign(char_type& c1, const char_type& c2) noexcept;
  43. static constexpr bool eq(char_type c1, char_type c2) noexcept;
  44. static constexpr bool lt(char_type c1, char_type c2) noexcept;
  45. static int compare(const char_type* s1, const char_type* s2, size_t n);
  46. static size_t length(const char_type* s);
  47. static const char_type* find(const char_type* s, size_t n, const char_type& a);
  48. static char_type* move(char_type* s1, const char_type* s2, size_t n);
  49. static char_type* copy(char_type* s1, const char_type* s2, size_t n);
  50. static char_type* assign(char_type* s, size_t n, char_type a);
  51. static constexpr int_type not_eof(int_type c) noexcept;
  52. static constexpr char_type to_char_type(int_type c) noexcept;
  53. static constexpr int_type to_int_type(char_type c) noexcept;
  54. static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
  55. static constexpr int_type eof() noexcept;
  56. };
  57. template <> struct char_traits<char>;
  58. template <> struct char_traits<wchar_t>;
  59. template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
  60. class basic_string
  61. {
  62. public:
  63. // types:
  64. typedef traits traits_type;
  65. typedef typename traits_type::char_type value_type;
  66. typedef Allocator allocator_type;
  67. typedef typename allocator_type::size_type size_type;
  68. typedef typename allocator_type::difference_type difference_type;
  69. typedef typename allocator_type::reference reference;
  70. typedef typename allocator_type::const_reference const_reference;
  71. typedef typename allocator_type::pointer pointer;
  72. typedef typename allocator_type::const_pointer const_pointer;
  73. typedef implementation-defined iterator;
  74. typedef implementation-defined const_iterator;
  75. typedef std::reverse_iterator<iterator> reverse_iterator;
  76. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  77. static const size_type npos = -1;
  78. basic_string()
  79. noexcept(is_nothrow_default_constructible<allocator_type>::value);
  80. explicit basic_string(const allocator_type& a);
  81. basic_string(const basic_string& str);
  82. basic_string(basic_string&& str)
  83. noexcept(is_nothrow_move_constructible<allocator_type>::value);
  84. basic_string(const basic_string& str, size_type pos,
  85. const allocator_type& a = allocator_type());
  86. basic_string(const basic_string& str, size_type pos, size_type n,
  87. const Allocator& a = Allocator());
  88. basic_string(const value_type* s, const allocator_type& a = allocator_type());
  89. basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
  90. basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
  91. template<class InputIterator>
  92. basic_string(InputIterator begin, InputIterator end,
  93. const allocator_type& a = allocator_type());
  94. basic_string(initializer_list<value_type>, const Allocator& = Allocator());
  95. basic_string(const basic_string&, const Allocator&);
  96. basic_string(basic_string&&, const Allocator&);
  97. ~basic_string();
  98. basic_string& operator=(const basic_string& str);
  99. basic_string& operator=(basic_string&& str)
  100. noexcept(
  101. allocator_type::propagate_on_container_move_assignment::value ||
  102. allocator_type::is_always_equal::value ); // C++17
  103. basic_string& operator=(const value_type* s);
  104. basic_string& operator=(value_type c);
  105. basic_string& operator=(initializer_list<value_type>);
  106. iterator begin() noexcept;
  107. const_iterator begin() const noexcept;
  108. iterator end() noexcept;
  109. const_iterator end() const noexcept;
  110. reverse_iterator rbegin() noexcept;
  111. const_reverse_iterator rbegin() const noexcept;
  112. reverse_iterator rend() noexcept;
  113. const_reverse_iterator rend() const noexcept;
  114. const_iterator cbegin() const noexcept;
  115. const_iterator cend() const noexcept;
  116. const_reverse_iterator crbegin() const noexcept;
  117. const_reverse_iterator crend() const noexcept;
  118. size_type size() const noexcept;
  119. size_type length() const noexcept;
  120. size_type max_size() const noexcept;
  121. size_type capacity() const noexcept;
  122. void resize(size_type n, value_type c);
  123. void resize(size_type n);
  124. void reserve(size_type res_arg = 0);
  125. void shrink_to_fit();
  126. void clear() noexcept;
  127. bool empty() const noexcept;
  128. const_reference operator[](size_type pos) const;
  129. reference operator[](size_type pos);
  130. const_reference at(size_type n) const;
  131. reference at(size_type n);
  132. basic_string& operator+=(const basic_string& str);
  133. basic_string& operator+=(const value_type* s);
  134. basic_string& operator+=(value_type c);
  135. basic_string& operator+=(initializer_list<value_type>);
  136. basic_string& append(const basic_string& str);
  137. basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
  138. basic_string& append(const value_type* s, size_type n);
  139. basic_string& append(const value_type* s);
  140. basic_string& append(size_type n, value_type c);
  141. template<class InputIterator>
  142. basic_string& append(InputIterator first, InputIterator last);
  143. basic_string& append(initializer_list<value_type>);
  144. void push_back(value_type c);
  145. void pop_back();
  146. reference front();
  147. const_reference front() const;
  148. reference back();
  149. const_reference back() const;
  150. basic_string& assign(const basic_string& str);
  151. basic_string& assign(basic_string&& str);
  152. basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
  153. basic_string& assign(const value_type* s, size_type n);
  154. basic_string& assign(const value_type* s);
  155. basic_string& assign(size_type n, value_type c);
  156. template<class InputIterator>
  157. basic_string& assign(InputIterator first, InputIterator last);
  158. basic_string& assign(initializer_list<value_type>);
  159. basic_string& insert(size_type pos1, const basic_string& str);
  160. basic_string& insert(size_type pos1, const basic_string& str,
  161. size_type pos2, size_type n);
  162. basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
  163. basic_string& insert(size_type pos, const value_type* s);
  164. basic_string& insert(size_type pos, size_type n, value_type c);
  165. iterator insert(const_iterator p, value_type c);
  166. iterator insert(const_iterator p, size_type n, value_type c);
  167. template<class InputIterator>
  168. iterator insert(const_iterator p, InputIterator first, InputIterator last);
  169. iterator insert(const_iterator p, initializer_list<value_type>);
  170. basic_string& erase(size_type pos = 0, size_type n = npos);
  171. iterator erase(const_iterator position);
  172. iterator erase(const_iterator first, const_iterator last);
  173. basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
  174. basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
  175. size_type pos2, size_type n2=npos); // C++14
  176. basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
  177. basic_string& replace(size_type pos, size_type n1, const value_type* s);
  178. basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
  179. basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
  180. basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
  181. basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
  182. basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
  183. template<class InputIterator>
  184. basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
  185. basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
  186. size_type copy(value_type* s, size_type n, size_type pos = 0) const;
  187. basic_string substr(size_type pos = 0, size_type n = npos) const;
  188. void swap(basic_string& str)
  189. noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
  190. allocator_traits<allocator_type>::is_always_equal::value); // C++17
  191. const value_type* c_str() const noexcept;
  192. const value_type* data() const noexcept;
  193. value_type* data() noexcept; // C++17
  194. allocator_type get_allocator() const noexcept;
  195. size_type find(const basic_string& str, size_type pos = 0) const noexcept;
  196. size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
  197. size_type find(const value_type* s, size_type pos = 0) const noexcept;
  198. size_type find(value_type c, size_type pos = 0) const noexcept;
  199. size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
  200. size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
  201. size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
  202. size_type rfind(value_type c, size_type pos = npos) const noexcept;
  203. size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
  204. size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
  205. size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
  206. size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
  207. size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
  208. size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
  209. size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
  210. size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
  211. size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
  212. size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
  213. size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
  214. size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
  215. size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
  216. size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
  217. size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
  218. size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
  219. int compare(const basic_string& str) const noexcept;
  220. int compare(size_type pos1, size_type n1, const basic_string& str) const;
  221. int compare(size_type pos1, size_type n1, const basic_string& str,
  222. size_type pos2, size_type n2=npos) const; // C++14
  223. int compare(const value_type* s) const noexcept;
  224. int compare(size_type pos1, size_type n1, const value_type* s) const;
  225. int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
  226. bool __invariants() const;
  227. };
  228. template<class charT, class traits, class Allocator>
  229. basic_string<charT, traits, Allocator>
  230. operator+(const basic_string<charT, traits, Allocator>& lhs,
  231. const basic_string<charT, traits, Allocator>& rhs);
  232. template<class charT, class traits, class Allocator>
  233. basic_string<charT, traits, Allocator>
  234. operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
  235. template<class charT, class traits, class Allocator>
  236. basic_string<charT, traits, Allocator>
  237. operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
  238. template<class charT, class traits, class Allocator>
  239. basic_string<charT, traits, Allocator>
  240. operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
  241. template<class charT, class traits, class Allocator>
  242. basic_string<charT, traits, Allocator>
  243. operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
  244. template<class charT, class traits, class Allocator>
  245. bool operator==(const basic_string<charT, traits, Allocator>& lhs,
  246. const basic_string<charT, traits, Allocator>& rhs) noexcept;
  247. template<class charT, class traits, class Allocator>
  248. bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
  249. template<class charT, class traits, class Allocator>
  250. bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
  251. template<class charT, class traits, class Allocator>
  252. bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
  253. const basic_string<charT, traits, Allocator>& rhs) noexcept;
  254. template<class charT, class traits, class Allocator>
  255. bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
  256. template<class charT, class traits, class Allocator>
  257. bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
  258. template<class charT, class traits, class Allocator>
  259. bool operator< (const basic_string<charT, traits, Allocator>& lhs,
  260. const basic_string<charT, traits, Allocator>& rhs) noexcept;
  261. template<class charT, class traits, class Allocator>
  262. bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
  263. template<class charT, class traits, class Allocator>
  264. bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
  265. template<class charT, class traits, class Allocator>
  266. bool operator> (const basic_string<charT, traits, Allocator>& lhs,
  267. const basic_string<charT, traits, Allocator>& rhs) noexcept;
  268. template<class charT, class traits, class Allocator>
  269. bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
  270. template<class charT, class traits, class Allocator>
  271. bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
  272. template<class charT, class traits, class Allocator>
  273. bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
  274. const basic_string<charT, traits, Allocator>& rhs) noexcept;
  275. template<class charT, class traits, class Allocator>
  276. bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
  277. template<class charT, class traits, class Allocator>
  278. bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
  279. template<class charT, class traits, class Allocator>
  280. bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
  281. const basic_string<charT, traits, Allocator>& rhs) noexcept;
  282. template<class charT, class traits, class Allocator>
  283. bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
  284. template<class charT, class traits, class Allocator>
  285. bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
  286. template<class charT, class traits, class Allocator>
  287. void swap(basic_string<charT, traits, Allocator>& lhs,
  288. basic_string<charT, traits, Allocator>& rhs)
  289. noexcept(noexcept(lhs.swap(rhs)));
  290. template<class charT, class traits, class Allocator>
  291. basic_istream<charT, traits>&
  292. operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
  293. template<class charT, class traits, class Allocator>
  294. basic_ostream<charT, traits>&
  295. operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
  296. template<class charT, class traits, class Allocator>
  297. basic_istream<charT, traits>&
  298. getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
  299. charT delim);
  300. template<class charT, class traits, class Allocator>
  301. basic_istream<charT, traits>&
  302. getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
  303. typedef basic_string<char> string;
  304. typedef basic_string<wchar_t> wstring;
  305. typedef basic_string<char16_t> u16string;
  306. typedef basic_string<char32_t> u32string;
  307. int stoi (const string& str, size_t* idx = 0, int base = 10);
  308. long stol (const string& str, size_t* idx = 0, int base = 10);
  309. unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
  310. long long stoll (const string& str, size_t* idx = 0, int base = 10);
  311. unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
  312. float stof (const string& str, size_t* idx = 0);
  313. double stod (const string& str, size_t* idx = 0);
  314. long double stold(const string& str, size_t* idx = 0);
  315. string to_string(int val);
  316. string to_string(unsigned val);
  317. string to_string(long val);
  318. string to_string(unsigned long val);
  319. string to_string(long long val);
  320. string to_string(unsigned long long val);
  321. string to_string(float val);
  322. string to_string(double val);
  323. string to_string(long double val);
  324. int stoi (const wstring& str, size_t* idx = 0, int base = 10);
  325. long stol (const wstring& str, size_t* idx = 0, int base = 10);
  326. unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
  327. long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
  328. unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
  329. float stof (const wstring& str, size_t* idx = 0);
  330. double stod (const wstring& str, size_t* idx = 0);
  331. long double stold(const wstring& str, size_t* idx = 0);
  332. wstring to_wstring(int val);
  333. wstring to_wstring(unsigned val);
  334. wstring to_wstring(long val);
  335. wstring to_wstring(unsigned long val);
  336. wstring to_wstring(long long val);
  337. wstring to_wstring(unsigned long long val);
  338. wstring to_wstring(float val);
  339. wstring to_wstring(double val);
  340. wstring to_wstring(long double val);
  341. template <> struct hash<string>;
  342. template <> struct hash<u16string>;
  343. template <> struct hash<u32string>;
  344. template <> struct hash<wstring>;
  345. basic_string<char> operator "" s( const char *str, size_t len ); // C++14
  346. basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
  347. basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
  348. basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
  349. } // std
  350. */
  351. #include <__config>
  352. #include <iosfwd>
  353. #include <cstring>
  354. #include <cstdio> // For EOF.
  355. #include <cwchar>
  356. #include <algorithm>
  357. #include <iterator>
  358. #include <utility>
  359. #include <memory>
  360. #include <stdexcept>
  361. #include <type_traits>
  362. #include <initializer_list>
  363. #include <__functional_base>
  364. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  365. #include <cstdint>
  366. #endif
  367. #if defined(_LIBCPP_NO_EXCEPTIONS)
  368. #include <cassert>
  369. #endif
  370. #include <__undef_min_max>
  371. #include <__debug>
  372. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  373. #pragma GCC system_header
  374. #endif
  375. _LIBCPP_BEGIN_NAMESPACE_STD
  376. // fpos
  377. #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  378. template <class _StateT>
  379. class _LIBCPP_TYPE_VIS_ONLY fpos
  380. {
  381. private:
  382. _StateT __st_;
  383. streamoff __off_;
  384. public:
  385. _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
  386. _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
  387. _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
  388. _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
  389. _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
  390. _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
  391. _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
  392. _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
  393. };
  394. template <class _StateT>
  395. inline _LIBCPP_INLINE_VISIBILITY
  396. streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
  397. {return streamoff(__x) - streamoff(__y);}
  398. template <class _StateT>
  399. inline _LIBCPP_INLINE_VISIBILITY
  400. bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
  401. {return streamoff(__x) == streamoff(__y);}
  402. template <class _StateT>
  403. inline _LIBCPP_INLINE_VISIBILITY
  404. bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
  405. {return streamoff(__x) != streamoff(__y);}
  406. #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  407. // char_traits
  408. template <class _CharT>
  409. struct _LIBCPP_TYPE_VIS_ONLY char_traits
  410. {
  411. typedef _CharT char_type;
  412. typedef int int_type;
  413. #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  414. typedef streamoff off_type;
  415. typedef streampos pos_type;
  416. #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  417. typedef mbstate_t state_type;
  418. static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
  419. {__c1 = __c2;}
  420. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  421. {return __c1 == __c2;}
  422. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  423. {return __c1 < __c2;}
  424. static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
  425. _LIBCPP_INLINE_VISIBILITY
  426. static size_t length(const char_type* __s);
  427. _LIBCPP_INLINE_VISIBILITY
  428. static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
  429. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
  430. _LIBCPP_INLINE_VISIBILITY
  431. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
  432. _LIBCPP_INLINE_VISIBILITY
  433. static char_type* assign(char_type* __s, size_t __n, char_type __a);
  434. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  435. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  436. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  437. {return char_type(__c);}
  438. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  439. {return int_type(__c);}
  440. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  441. {return __c1 == __c2;}
  442. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  443. {return int_type(EOF);}
  444. };
  445. template <class _CharT>
  446. int
  447. char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
  448. {
  449. for (; __n; --__n, ++__s1, ++__s2)
  450. {
  451. if (lt(*__s1, *__s2))
  452. return -1;
  453. if (lt(*__s2, *__s1))
  454. return 1;
  455. }
  456. return 0;
  457. }
  458. template <class _CharT>
  459. inline
  460. size_t
  461. char_traits<_CharT>::length(const char_type* __s)
  462. {
  463. size_t __len = 0;
  464. for (; !eq(*__s, char_type(0)); ++__s)
  465. ++__len;
  466. return __len;
  467. }
  468. template <class _CharT>
  469. inline
  470. const _CharT*
  471. char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
  472. {
  473. for (; __n; --__n)
  474. {
  475. if (eq(*__s, __a))
  476. return __s;
  477. ++__s;
  478. }
  479. return 0;
  480. }
  481. template <class _CharT>
  482. _CharT*
  483. char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
  484. {
  485. char_type* __r = __s1;
  486. if (__s1 < __s2)
  487. {
  488. for (; __n; --__n, ++__s1, ++__s2)
  489. assign(*__s1, *__s2);
  490. }
  491. else if (__s2 < __s1)
  492. {
  493. __s1 += __n;
  494. __s2 += __n;
  495. for (; __n; --__n)
  496. assign(*--__s1, *--__s2);
  497. }
  498. return __r;
  499. }
  500. template <class _CharT>
  501. inline
  502. _CharT*
  503. char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
  504. {
  505. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  506. char_type* __r = __s1;
  507. for (; __n; --__n, ++__s1, ++__s2)
  508. assign(*__s1, *__s2);
  509. return __r;
  510. }
  511. template <class _CharT>
  512. inline
  513. _CharT*
  514. char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
  515. {
  516. char_type* __r = __s;
  517. for (; __n; --__n, ++__s)
  518. assign(*__s, __a);
  519. return __r;
  520. }
  521. // char_traits<char>
  522. template <>
  523. struct _LIBCPP_TYPE_VIS_ONLY char_traits<char>
  524. {
  525. typedef char char_type;
  526. typedef int int_type;
  527. #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  528. typedef streamoff off_type;
  529. typedef streampos pos_type;
  530. #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  531. typedef mbstate_t state_type;
  532. static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
  533. {__c1 = __c2;}
  534. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  535. {return __c1 == __c2;}
  536. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  537. {return (unsigned char)__c1 < (unsigned char)__c2;}
  538. static inline int compare(const char_type* __s1, const char_type* __s2, size_t __n)
  539. {return __n == 0 ? 0 : memcmp(__s1, __s2, __n);}
  540. static inline size_t length(const char_type* __s) {return strlen(__s);}
  541. static inline const char_type* find(const char_type* __s, size_t __n, const char_type& __a)
  542. {return __n == 0 ? NULL : (const char_type*) memchr(__s, to_int_type(__a), __n);}
  543. static inline char_type* move(char_type* __s1, const char_type* __s2, size_t __n)
  544. {return __n == 0 ? __s1 : (char_type*) memmove(__s1, __s2, __n);}
  545. static inline char_type* copy(char_type* __s1, const char_type* __s2, size_t __n)
  546. {
  547. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  548. return __n == 0 ? __s1 : (char_type*)memcpy(__s1, __s2, __n);
  549. }
  550. static inline char_type* assign(char_type* __s, size_t __n, char_type __a)
  551. {return __n == 0 ? __s : (char_type*)memset(__s, to_int_type(__a), __n);}
  552. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  553. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  554. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  555. {return char_type(__c);}
  556. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  557. {return int_type((unsigned char)__c);}
  558. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  559. {return __c1 == __c2;}
  560. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  561. {return int_type(EOF);}
  562. };
  563. // char_traits<wchar_t>
  564. template <>
  565. struct _LIBCPP_TYPE_VIS_ONLY char_traits<wchar_t>
  566. {
  567. typedef wchar_t char_type;
  568. typedef wint_t int_type;
  569. #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  570. typedef streamoff off_type;
  571. typedef streampos pos_type;
  572. #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  573. typedef mbstate_t state_type;
  574. static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
  575. {__c1 = __c2;}
  576. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  577. {return __c1 == __c2;}
  578. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  579. {return __c1 < __c2;}
  580. static inline int compare(const char_type* __s1, const char_type* __s2, size_t __n)
  581. {return __n == 0 ? 0 : wmemcmp(__s1, __s2, __n);}
  582. static inline size_t length(const char_type* __s)
  583. {return wcslen(__s);}
  584. static inline const char_type* find(const char_type* __s, size_t __n, const char_type& __a)
  585. {return __n == 0 ? NULL : (const char_type*)wmemchr(__s, __a, __n);}
  586. static inline char_type* move(char_type* __s1, const char_type* __s2, size_t __n)
  587. {return __n == 0 ? __s1 : (char_type*)wmemmove(__s1, __s2, __n);}
  588. static inline char_type* copy(char_type* __s1, const char_type* __s2, size_t __n)
  589. {
  590. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  591. return __n == 0 ? __s1 : (char_type*)wmemcpy(__s1, __s2, __n);
  592. }
  593. static inline char_type* assign(char_type* __s, size_t __n, char_type __a)
  594. {return __n == 0 ? __s : (char_type*)wmemset(__s, __a, __n);}
  595. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  596. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  597. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  598. {return char_type(__c);}
  599. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  600. {return int_type(__c);}
  601. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  602. {return __c1 == __c2;}
  603. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  604. {return int_type(WEOF);}
  605. };
  606. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  607. template <>
  608. struct _LIBCPP_TYPE_VIS_ONLY char_traits<char16_t>
  609. {
  610. typedef char16_t char_type;
  611. typedef uint_least16_t int_type;
  612. #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  613. typedef streamoff off_type;
  614. typedef u16streampos pos_type;
  615. #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  616. typedef mbstate_t state_type;
  617. static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
  618. {__c1 = __c2;}
  619. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  620. {return __c1 == __c2;}
  621. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  622. {return __c1 < __c2;}
  623. _LIBCPP_INLINE_VISIBILITY
  624. static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
  625. _LIBCPP_INLINE_VISIBILITY
  626. static size_t length(const char_type* __s);
  627. _LIBCPP_INLINE_VISIBILITY
  628. static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
  629. _LIBCPP_INLINE_VISIBILITY
  630. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
  631. _LIBCPP_INLINE_VISIBILITY
  632. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
  633. _LIBCPP_INLINE_VISIBILITY
  634. static char_type* assign(char_type* __s, size_t __n, char_type __a);
  635. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  636. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  637. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  638. {return char_type(__c);}
  639. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  640. {return int_type(__c);}
  641. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  642. {return __c1 == __c2;}
  643. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  644. {return int_type(0xFFFF);}
  645. };
  646. inline
  647. int
  648. char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
  649. {
  650. for (; __n; --__n, ++__s1, ++__s2)
  651. {
  652. if (lt(*__s1, *__s2))
  653. return -1;
  654. if (lt(*__s2, *__s1))
  655. return 1;
  656. }
  657. return 0;
  658. }
  659. inline
  660. size_t
  661. char_traits<char16_t>::length(const char_type* __s)
  662. {
  663. size_t __len = 0;
  664. for (; !eq(*__s, char_type(0)); ++__s)
  665. ++__len;
  666. return __len;
  667. }
  668. inline
  669. const char16_t*
  670. char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a)
  671. {
  672. for (; __n; --__n)
  673. {
  674. if (eq(*__s, __a))
  675. return __s;
  676. ++__s;
  677. }
  678. return 0;
  679. }
  680. inline
  681. char16_t*
  682. char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n)
  683. {
  684. char_type* __r = __s1;
  685. if (__s1 < __s2)
  686. {
  687. for (; __n; --__n, ++__s1, ++__s2)
  688. assign(*__s1, *__s2);
  689. }
  690. else if (__s2 < __s1)
  691. {
  692. __s1 += __n;
  693. __s2 += __n;
  694. for (; __n; --__n)
  695. assign(*--__s1, *--__s2);
  696. }
  697. return __r;
  698. }
  699. inline
  700. char16_t*
  701. char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n)
  702. {
  703. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  704. char_type* __r = __s1;
  705. for (; __n; --__n, ++__s1, ++__s2)
  706. assign(*__s1, *__s2);
  707. return __r;
  708. }
  709. inline
  710. char16_t*
  711. char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a)
  712. {
  713. char_type* __r = __s;
  714. for (; __n; --__n, ++__s)
  715. assign(*__s, __a);
  716. return __r;
  717. }
  718. template <>
  719. struct _LIBCPP_TYPE_VIS_ONLY char_traits<char32_t>
  720. {
  721. typedef char32_t char_type;
  722. typedef uint_least32_t int_type;
  723. #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  724. typedef streamoff off_type;
  725. typedef u32streampos pos_type;
  726. #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  727. typedef mbstate_t state_type;
  728. static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
  729. {__c1 = __c2;}
  730. static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
  731. {return __c1 == __c2;}
  732. static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
  733. {return __c1 < __c2;}
  734. _LIBCPP_INLINE_VISIBILITY
  735. static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
  736. _LIBCPP_INLINE_VISIBILITY
  737. static size_t length(const char_type* __s);
  738. _LIBCPP_INLINE_VISIBILITY
  739. static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
  740. _LIBCPP_INLINE_VISIBILITY
  741. static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
  742. _LIBCPP_INLINE_VISIBILITY
  743. static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
  744. _LIBCPP_INLINE_VISIBILITY
  745. static char_type* assign(char_type* __s, size_t __n, char_type __a);
  746. static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
  747. {return eq_int_type(__c, eof()) ? ~eof() : __c;}
  748. static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
  749. {return char_type(__c);}
  750. static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
  751. {return int_type(__c);}
  752. static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
  753. {return __c1 == __c2;}
  754. static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
  755. {return int_type(0xFFFFFFFF);}
  756. };
  757. inline
  758. int
  759. char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
  760. {
  761. for (; __n; --__n, ++__s1, ++__s2)
  762. {
  763. if (lt(*__s1, *__s2))
  764. return -1;
  765. if (lt(*__s2, *__s1))
  766. return 1;
  767. }
  768. return 0;
  769. }
  770. inline
  771. size_t
  772. char_traits<char32_t>::length(const char_type* __s)
  773. {
  774. size_t __len = 0;
  775. for (; !eq(*__s, char_type(0)); ++__s)
  776. ++__len;
  777. return __len;
  778. }
  779. inline
  780. const char32_t*
  781. char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a)
  782. {
  783. for (; __n; --__n)
  784. {
  785. if (eq(*__s, __a))
  786. return __s;
  787. ++__s;
  788. }
  789. return 0;
  790. }
  791. inline
  792. char32_t*
  793. char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n)
  794. {
  795. char_type* __r = __s1;
  796. if (__s1 < __s2)
  797. {
  798. for (; __n; --__n, ++__s1, ++__s2)
  799. assign(*__s1, *__s2);
  800. }
  801. else if (__s2 < __s1)
  802. {
  803. __s1 += __n;
  804. __s2 += __n;
  805. for (; __n; --__n)
  806. assign(*--__s1, *--__s2);
  807. }
  808. return __r;
  809. }
  810. inline
  811. char32_t*
  812. char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n)
  813. {
  814. _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
  815. char_type* __r = __s1;
  816. for (; __n; --__n, ++__s1, ++__s2)
  817. assign(*__s1, *__s2);
  818. return __r;
  819. }
  820. inline
  821. char32_t*
  822. char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a)
  823. {
  824. char_type* __r = __s;
  825. for (; __n; --__n, ++__s)
  826. assign(*__s, __a);
  827. return __r;
  828. }
  829. #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
  830. // helper fns for basic_string
  831. // __str_find
  832. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  833. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  834. __str_find(const _CharT *__p, _SizeT __sz,
  835. _CharT __c, _SizeT __pos) _NOEXCEPT
  836. {
  837. if (__pos >= __sz)
  838. return __npos;
  839. const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
  840. if (__r == 0)
  841. return __npos;
  842. return static_cast<_SizeT>(__r - __p);
  843. }
  844. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  845. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  846. __str_find(const _CharT *__p, _SizeT __sz,
  847. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  848. {
  849. if (__pos > __sz || __sz - __pos < __n)
  850. return __npos;
  851. if (__n == 0)
  852. return __pos;
  853. const _CharT* __r =
  854. _VSTD::__search(__p + __pos, __p + __sz,
  855. __s, __s + __n, _Traits::eq,
  856. random_access_iterator_tag(), random_access_iterator_tag()).first;
  857. if (__r == __p + __sz)
  858. return __npos;
  859. return static_cast<_SizeT>(__r - __p);
  860. }
  861. // __str_rfind
  862. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  863. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  864. __str_rfind(const _CharT *__p, _SizeT __sz,
  865. _CharT __c, _SizeT __pos) _NOEXCEPT
  866. {
  867. if (__sz < 1)
  868. return __npos;
  869. if (__pos < __sz)
  870. ++__pos;
  871. else
  872. __pos = __sz;
  873. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  874. {
  875. if (_Traits::eq(*--__ps, __c))
  876. return static_cast<_SizeT>(__ps - __p);
  877. }
  878. return __npos;
  879. }
  880. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  881. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  882. __str_rfind(const _CharT *__p, _SizeT __sz,
  883. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  884. {
  885. __pos = _VSTD::min(__pos, __sz);
  886. if (__n < __sz - __pos)
  887. __pos += __n;
  888. else
  889. __pos = __sz;
  890. const _CharT* __r = _VSTD::__find_end(
  891. __p, __p + __pos, __s, __s + __n, _Traits::eq,
  892. random_access_iterator_tag(), random_access_iterator_tag());
  893. if (__n > 0 && __r == __p + __pos)
  894. return __npos;
  895. return static_cast<_SizeT>(__r - __p);
  896. }
  897. // __str_find_first_of
  898. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  899. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  900. __str_find_first_of(const _CharT *__p, _SizeT __sz,
  901. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  902. {
  903. if (__pos >= __sz || __n == 0)
  904. return __npos;
  905. const _CharT* __r = _VSTD::__find_first_of_ce
  906. (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq );
  907. if (__r == __p + __sz)
  908. return __npos;
  909. return static_cast<_SizeT>(__r - __p);
  910. }
  911. // __str_find_last_of
  912. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  913. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  914. __str_find_last_of(const _CharT *__p, _SizeT __sz,
  915. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  916. {
  917. if (__n != 0)
  918. {
  919. if (__pos < __sz)
  920. ++__pos;
  921. else
  922. __pos = __sz;
  923. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  924. {
  925. const _CharT* __r = _Traits::find(__s, __n, *--__ps);
  926. if (__r)
  927. return static_cast<_SizeT>(__ps - __p);
  928. }
  929. }
  930. return __npos;
  931. }
  932. // __str_find_first_not_of
  933. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  934. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  935. __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
  936. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  937. {
  938. if (__pos < __sz)
  939. {
  940. const _CharT* __pe = __p + __sz;
  941. for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
  942. if (_Traits::find(__s, __n, *__ps) == 0)
  943. return static_cast<_SizeT>(__ps - __p);
  944. }
  945. return __npos;
  946. }
  947. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  948. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  949. __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
  950. _CharT __c, _SizeT __pos) _NOEXCEPT
  951. {
  952. if (__pos < __sz)
  953. {
  954. const _CharT* __pe = __p + __sz;
  955. for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
  956. if (!_Traits::eq(*__ps, __c))
  957. return static_cast<_SizeT>(__ps - __p);
  958. }
  959. return __npos;
  960. }
  961. // __str_find_last_not_of
  962. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  963. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  964. __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
  965. const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
  966. {
  967. if (__pos < __sz)
  968. ++__pos;
  969. else
  970. __pos = __sz;
  971. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  972. if (_Traits::find(__s, __n, *--__ps) == 0)
  973. return static_cast<_SizeT>(__ps - __p);
  974. return __npos;
  975. }
  976. template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
  977. inline _SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
  978. __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
  979. _CharT __c, _SizeT __pos) _NOEXCEPT
  980. {
  981. if (__pos < __sz)
  982. ++__pos;
  983. else
  984. __pos = __sz;
  985. for (const _CharT* __ps = __p + __pos; __ps != __p;)
  986. if (!_Traits::eq(*--__ps, __c))
  987. return static_cast<_SizeT>(__ps - __p);
  988. return __npos;
  989. }
  990. template<class _Ptr>
  991. size_t _LIBCPP_INLINE_VISIBILITY __do_string_hash(_Ptr __p, _Ptr __e)
  992. {
  993. typedef typename iterator_traits<_Ptr>::value_type value_type;
  994. return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type));
  995. }
  996. // basic_string
  997. template<class _CharT, class _Traits, class _Allocator>
  998. basic_string<_CharT, _Traits, _Allocator>
  999. operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
  1000. const basic_string<_CharT, _Traits, _Allocator>& __y);
  1001. template<class _CharT, class _Traits, class _Allocator>
  1002. basic_string<_CharT, _Traits, _Allocator>
  1003. operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
  1004. template<class _CharT, class _Traits, class _Allocator>
  1005. basic_string<_CharT, _Traits, _Allocator>
  1006. operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
  1007. template<class _CharT, class _Traits, class _Allocator>
  1008. basic_string<_CharT, _Traits, _Allocator>
  1009. operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
  1010. template<class _CharT, class _Traits, class _Allocator>
  1011. basic_string<_CharT, _Traits, _Allocator>
  1012. operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
  1013. template <bool>
  1014. class _LIBCPP_TYPE_VIS_ONLY __basic_string_common
  1015. {
  1016. protected:
  1017. void __throw_length_error() const;
  1018. void __throw_out_of_range() const;
  1019. };
  1020. template <bool __b>
  1021. void
  1022. __basic_string_common<__b>::__throw_length_error() const
  1023. {
  1024. #ifndef _LIBCPP_NO_EXCEPTIONS
  1025. throw length_error("basic_string");
  1026. #else
  1027. assert(!"basic_string length_error");
  1028. #endif
  1029. }
  1030. template <bool __b>
  1031. void
  1032. __basic_string_common<__b>::__throw_out_of_range() const
  1033. {
  1034. #ifndef _LIBCPP_NO_EXCEPTIONS
  1035. throw out_of_range("basic_string");
  1036. #else
  1037. assert(!"basic_string out_of_range");
  1038. #endif
  1039. }
  1040. #ifdef _LIBCPP_MSVC
  1041. #pragma warning( push )
  1042. #pragma warning( disable: 4231 )
  1043. #endif // _LIBCPP_MSVC
  1044. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __basic_string_common<true>)
  1045. #ifdef _LIBCPP_MSVC
  1046. #pragma warning( pop )
  1047. #endif // _LIBCPP_MSVC
  1048. #ifdef _LIBCPP_NO_EXCEPTIONS
  1049. template <class _Iter>
  1050. struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
  1051. #elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
  1052. template <class _Iter>
  1053. struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
  1054. #else
  1055. template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
  1056. struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
  1057. noexcept(++(declval<_Iter&>())) &&
  1058. is_nothrow_assignable<_Iter&, _Iter>::value &&
  1059. noexcept(declval<_Iter>() == declval<_Iter>()) &&
  1060. noexcept(*declval<_Iter>())
  1061. )) {};
  1062. template <class _Iter>
  1063. struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
  1064. #endif
  1065. template <class _Iter>
  1066. struct __libcpp_string_gets_noexcept_iterator
  1067. : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
  1068. #ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  1069. template <class _CharT, size_t = sizeof(_CharT)>
  1070. struct __padding
  1071. {
  1072. unsigned char __xx[sizeof(_CharT)-1];
  1073. };
  1074. template <class _CharT>
  1075. struct __padding<_CharT, 1>
  1076. {
  1077. };
  1078. #endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  1079. template<class _CharT, class _Traits, class _Allocator>
  1080. class _LIBCPP_TYPE_VIS_ONLY basic_string
  1081. : private __basic_string_common<true>
  1082. {
  1083. public:
  1084. typedef basic_string __self;
  1085. typedef _Traits traits_type;
  1086. typedef typename traits_type::char_type value_type;
  1087. typedef _Allocator allocator_type;
  1088. typedef allocator_traits<allocator_type> __alloc_traits;
  1089. typedef typename __alloc_traits::size_type size_type;
  1090. typedef typename __alloc_traits::difference_type difference_type;
  1091. typedef value_type& reference;
  1092. typedef const value_type& const_reference;
  1093. typedef typename __alloc_traits::pointer pointer;
  1094. typedef typename __alloc_traits::const_pointer const_pointer;
  1095. static_assert(is_pod<value_type>::value, "Character type of basic_string must be a POD");
  1096. static_assert((is_same<_CharT, value_type>::value),
  1097. "traits_type::char_type must be the same type as CharT");
  1098. static_assert((is_same<typename allocator_type::value_type, value_type>::value),
  1099. "Allocator::value_type must be same type as value_type");
  1100. #if defined(_LIBCPP_RAW_ITERATORS)
  1101. typedef pointer iterator;
  1102. typedef const_pointer const_iterator;
  1103. #else // defined(_LIBCPP_RAW_ITERATORS)
  1104. typedef __wrap_iter<pointer> iterator;
  1105. typedef __wrap_iter<const_pointer> const_iterator;
  1106. #endif // defined(_LIBCPP_RAW_ITERATORS)
  1107. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  1108. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  1109. private:
  1110. #ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  1111. struct __long
  1112. {
  1113. pointer __data_;
  1114. size_type __size_;
  1115. size_type __cap_;
  1116. };
  1117. #if _LIBCPP_BIG_ENDIAN
  1118. enum {__short_mask = 0x01};
  1119. enum {__long_mask = 0x1ul};
  1120. #else // _LIBCPP_BIG_ENDIAN
  1121. enum {__short_mask = 0x80};
  1122. enum {__long_mask = ~(size_type(~0) >> 1)};
  1123. #endif // _LIBCPP_BIG_ENDIAN
  1124. enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
  1125. (sizeof(__long) - 1)/sizeof(value_type) : 2};
  1126. struct __short
  1127. {
  1128. value_type __data_[__min_cap];
  1129. struct
  1130. : __padding<value_type>
  1131. {
  1132. unsigned char __size_;
  1133. };
  1134. };
  1135. #else
  1136. struct __long
  1137. {
  1138. size_type __cap_;
  1139. size_type __size_;
  1140. pointer __data_;
  1141. };
  1142. #if _LIBCPP_BIG_ENDIAN
  1143. enum {__short_mask = 0x80};
  1144. enum {__long_mask = ~(size_type(~0) >> 1)};
  1145. #else // _LIBCPP_BIG_ENDIAN
  1146. enum {__short_mask = 0x01};
  1147. enum {__long_mask = 0x1ul};
  1148. #endif // _LIBCPP_BIG_ENDIAN
  1149. enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
  1150. (sizeof(__long) - 1)/sizeof(value_type) : 2};
  1151. struct __short
  1152. {
  1153. union
  1154. {
  1155. unsigned char __size_;
  1156. value_type __lx;
  1157. };
  1158. value_type __data_[__min_cap];
  1159. };
  1160. #endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  1161. union __ulx{__long __lx; __short __lxx;};
  1162. enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
  1163. struct __raw
  1164. {
  1165. size_type __words[__n_words];
  1166. };
  1167. struct __rep
  1168. {
  1169. union
  1170. {
  1171. __long __l;
  1172. __short __s;
  1173. __raw __r;
  1174. };
  1175. };
  1176. __compressed_pair<__rep, allocator_type> __r_;
  1177. public:
  1178. static const size_type npos = -1;
  1179. _LIBCPP_INLINE_VISIBILITY basic_string()
  1180. _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
  1181. _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
  1182. #if _LIBCPP_STD_VER <= 14
  1183. _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
  1184. #else
  1185. _NOEXCEPT;
  1186. #endif
  1187. basic_string(const basic_string& __str);
  1188. basic_string(const basic_string& __str, const allocator_type& __a);
  1189. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1190. _LIBCPP_INLINE_VISIBILITY
  1191. basic_string(basic_string&& __str)
  1192. #if _LIBCPP_STD_VER <= 14
  1193. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
  1194. #else
  1195. _NOEXCEPT;
  1196. #endif
  1197. _LIBCPP_INLINE_VISIBILITY
  1198. basic_string(basic_string&& __str, const allocator_type& __a);
  1199. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1200. _LIBCPP_INLINE_VISIBILITY basic_string(const value_type* __s);
  1201. _LIBCPP_INLINE_VISIBILITY
  1202. basic_string(const value_type* __s, const allocator_type& __a);
  1203. _LIBCPP_INLINE_VISIBILITY
  1204. basic_string(const value_type* __s, size_type __n);
  1205. _LIBCPP_INLINE_VISIBILITY
  1206. basic_string(const value_type* __s, size_type __n, const allocator_type& __a);
  1207. _LIBCPP_INLINE_VISIBILITY
  1208. basic_string(size_type __n, value_type __c);
  1209. _LIBCPP_INLINE_VISIBILITY
  1210. basic_string(size_type __n, value_type __c, const allocator_type& __a);
  1211. basic_string(const basic_string& __str, size_type __pos, size_type __n,
  1212. const allocator_type& __a = allocator_type());
  1213. _LIBCPP_INLINE_VISIBILITY
  1214. basic_string(const basic_string& __str, size_type __pos,
  1215. const allocator_type& __a = allocator_type());
  1216. template<class _InputIterator>
  1217. _LIBCPP_INLINE_VISIBILITY
  1218. basic_string(_InputIterator __first, _InputIterator __last);
  1219. template<class _InputIterator>
  1220. _LIBCPP_INLINE_VISIBILITY
  1221. basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
  1222. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1223. _LIBCPP_INLINE_VISIBILITY
  1224. basic_string(initializer_list<value_type> __il);
  1225. _LIBCPP_INLINE_VISIBILITY
  1226. basic_string(initializer_list<value_type> __il, const allocator_type& __a);
  1227. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1228. ~basic_string();
  1229. basic_string& operator=(const basic_string& __str);
  1230. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1231. _LIBCPP_INLINE_VISIBILITY
  1232. basic_string& operator=(basic_string&& __str)
  1233. _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
  1234. #endif
  1235. _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
  1236. basic_string& operator=(value_type __c);
  1237. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1238. _LIBCPP_INLINE_VISIBILITY
  1239. basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
  1240. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1241. #if _LIBCPP_DEBUG_LEVEL >= 2
  1242. _LIBCPP_INLINE_VISIBILITY
  1243. iterator begin() _NOEXCEPT
  1244. {return iterator(this, __get_pointer());}
  1245. _LIBCPP_INLINE_VISIBILITY
  1246. const_iterator begin() const _NOEXCEPT
  1247. {return const_iterator(this, __get_pointer());}
  1248. _LIBCPP_INLINE_VISIBILITY
  1249. iterator end() _NOEXCEPT
  1250. {return iterator(this, __get_pointer() + size());}
  1251. _LIBCPP_INLINE_VISIBILITY
  1252. const_iterator end() const _NOEXCEPT
  1253. {return const_iterator(this, __get_pointer() + size());}
  1254. #else
  1255. _LIBCPP_INLINE_VISIBILITY
  1256. iterator begin() _NOEXCEPT
  1257. {return iterator(__get_pointer());}
  1258. _LIBCPP_INLINE_VISIBILITY
  1259. const_iterator begin() const _NOEXCEPT
  1260. {return const_iterator(__get_pointer());}
  1261. _LIBCPP_INLINE_VISIBILITY
  1262. iterator end() _NOEXCEPT
  1263. {return iterator(__get_pointer() + size());}
  1264. _LIBCPP_INLINE_VISIBILITY
  1265. const_iterator end() const _NOEXCEPT
  1266. {return const_iterator(__get_pointer() + size());}
  1267. #endif // _LIBCPP_DEBUG_LEVEL >= 2
  1268. _LIBCPP_INLINE_VISIBILITY
  1269. reverse_iterator rbegin() _NOEXCEPT
  1270. {return reverse_iterator(end());}
  1271. _LIBCPP_INLINE_VISIBILITY
  1272. const_reverse_iterator rbegin() const _NOEXCEPT
  1273. {return const_reverse_iterator(end());}
  1274. _LIBCPP_INLINE_VISIBILITY
  1275. reverse_iterator rend() _NOEXCEPT
  1276. {return reverse_iterator(begin());}
  1277. _LIBCPP_INLINE_VISIBILITY
  1278. const_reverse_iterator rend() const _NOEXCEPT
  1279. {return const_reverse_iterator(begin());}
  1280. _LIBCPP_INLINE_VISIBILITY
  1281. const_iterator cbegin() const _NOEXCEPT
  1282. {return begin();}
  1283. _LIBCPP_INLINE_VISIBILITY
  1284. const_iterator cend() const _NOEXCEPT
  1285. {return end();}
  1286. _LIBCPP_INLINE_VISIBILITY
  1287. const_reverse_iterator crbegin() const _NOEXCEPT
  1288. {return rbegin();}
  1289. _LIBCPP_INLINE_VISIBILITY
  1290. const_reverse_iterator crend() const _NOEXCEPT
  1291. {return rend();}
  1292. _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
  1293. {return __is_long() ? __get_long_size() : __get_short_size();}
  1294. _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
  1295. _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
  1296. _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
  1297. {return (__is_long() ? __get_long_cap()
  1298. : static_cast<size_type>(__min_cap)) - 1;}
  1299. void resize(size_type __n, value_type __c);
  1300. _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
  1301. void reserve(size_type res_arg = 0);
  1302. _LIBCPP_INLINE_VISIBILITY
  1303. void shrink_to_fit() _NOEXCEPT {reserve();}
  1304. _LIBCPP_INLINE_VISIBILITY
  1305. void clear() _NOEXCEPT;
  1306. _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT {return size() == 0;}
  1307. _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const;
  1308. _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos);
  1309. const_reference at(size_type __n) const;
  1310. reference at(size_type __n);
  1311. _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
  1312. _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
  1313. _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
  1314. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1315. _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
  1316. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1317. _LIBCPP_INLINE_VISIBILITY
  1318. basic_string& append(const basic_string& __str);
  1319. basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
  1320. basic_string& append(const value_type* __s, size_type __n);
  1321. basic_string& append(const value_type* __s);
  1322. basic_string& append(size_type __n, value_type __c);
  1323. template<class _InputIterator>
  1324. typename enable_if
  1325. <
  1326. __is_exactly_input_iterator<_InputIterator>::value
  1327. || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
  1328. basic_string&
  1329. >::type
  1330. append(_InputIterator __first, _InputIterator __last);
  1331. template<class _ForwardIterator>
  1332. typename enable_if
  1333. <
  1334. __is_forward_iterator<_ForwardIterator>::value
  1335. && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
  1336. basic_string&
  1337. >::type
  1338. append(_ForwardIterator __first, _ForwardIterator __last);
  1339. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1340. _LIBCPP_INLINE_VISIBILITY
  1341. basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
  1342. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1343. void push_back(value_type __c);
  1344. _LIBCPP_INLINE_VISIBILITY
  1345. void pop_back();
  1346. _LIBCPP_INLINE_VISIBILITY reference front();
  1347. _LIBCPP_INLINE_VISIBILITY const_reference front() const;
  1348. _LIBCPP_INLINE_VISIBILITY reference back();
  1349. _LIBCPP_INLINE_VISIBILITY const_reference back() const;
  1350. _LIBCPP_INLINE_VISIBILITY
  1351. basic_string& assign(const basic_string& __str) { return *this = __str; }
  1352. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1353. _LIBCPP_INLINE_VISIBILITY
  1354. basic_string& assign(basic_string&& str)
  1355. _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
  1356. {*this = _VSTD::move(str); return *this;}
  1357. #endif
  1358. basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
  1359. basic_string& assign(const value_type* __s, size_type __n);
  1360. basic_string& assign(const value_type* __s);
  1361. basic_string& assign(size_type __n, value_type __c);
  1362. template<class _InputIterator>
  1363. typename enable_if
  1364. <
  1365. __is_exactly_input_iterator<_InputIterator>::value
  1366. || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
  1367. basic_string&
  1368. >::type
  1369. assign(_InputIterator __first, _InputIterator __last);
  1370. template<class _ForwardIterator>
  1371. typename enable_if
  1372. <
  1373. __is_forward_iterator<_ForwardIterator>::value
  1374. && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
  1375. basic_string&
  1376. >::type
  1377. assign(_ForwardIterator __first, _ForwardIterator __last);
  1378. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1379. _LIBCPP_INLINE_VISIBILITY
  1380. basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
  1381. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1382. _LIBCPP_INLINE_VISIBILITY
  1383. basic_string& insert(size_type __pos1, const basic_string& __str);
  1384. basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
  1385. basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
  1386. basic_string& insert(size_type __pos, const value_type* __s);
  1387. basic_string& insert(size_type __pos, size_type __n, value_type __c);
  1388. iterator insert(const_iterator __pos, value_type __c);
  1389. _LIBCPP_INLINE_VISIBILITY
  1390. iterator insert(const_iterator __pos, size_type __n, value_type __c);
  1391. template<class _InputIterator>
  1392. typename enable_if
  1393. <
  1394. __is_exactly_input_iterator<_InputIterator>::value
  1395. || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
  1396. iterator
  1397. >::type
  1398. insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
  1399. template<class _ForwardIterator>
  1400. typename enable_if
  1401. <
  1402. __is_forward_iterator<_ForwardIterator>::value
  1403. && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
  1404. iterator
  1405. >::type
  1406. insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
  1407. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1408. _LIBCPP_INLINE_VISIBILITY
  1409. iterator insert(const_iterator __pos, initializer_list<value_type> __il)
  1410. {return insert(__pos, __il.begin(), __il.end());}
  1411. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1412. basic_string& erase(size_type __pos = 0, size_type __n = npos);
  1413. _LIBCPP_INLINE_VISIBILITY
  1414. iterator erase(const_iterator __pos);
  1415. _LIBCPP_INLINE_VISIBILITY
  1416. iterator erase(const_iterator __first, const_iterator __last);
  1417. _LIBCPP_INLINE_VISIBILITY
  1418. basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
  1419. basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
  1420. basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
  1421. basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
  1422. basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
  1423. _LIBCPP_INLINE_VISIBILITY
  1424. basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
  1425. _LIBCPP_INLINE_VISIBILITY
  1426. basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
  1427. _LIBCPP_INLINE_VISIBILITY
  1428. basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
  1429. _LIBCPP_INLINE_VISIBILITY
  1430. basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
  1431. template<class _InputIterator>
  1432. typename enable_if
  1433. <
  1434. __is_input_iterator<_InputIterator>::value,
  1435. basic_string&
  1436. >::type
  1437. replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
  1438. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1439. _LIBCPP_INLINE_VISIBILITY
  1440. basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
  1441. {return replace(__i1, __i2, __il.begin(), __il.end());}
  1442. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  1443. size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
  1444. _LIBCPP_INLINE_VISIBILITY
  1445. basic_string substr(size_type __pos = 0, size_type __n = npos) const;
  1446. _LIBCPP_INLINE_VISIBILITY
  1447. void swap(basic_string& __str)
  1448. #if _LIBCPP_STD_VER >= 14
  1449. _NOEXCEPT;
  1450. #else
  1451. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  1452. __is_nothrow_swappable<allocator_type>::value);
  1453. #endif
  1454. _LIBCPP_INLINE_VISIBILITY
  1455. const value_type* c_str() const _NOEXCEPT {return data();}
  1456. _LIBCPP_INLINE_VISIBILITY
  1457. const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
  1458. #if _LIBCPP_STD_VER > 14
  1459. _LIBCPP_INLINE_VISIBILITY
  1460. value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
  1461. #endif
  1462. _LIBCPP_INLINE_VISIBILITY
  1463. allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
  1464. _LIBCPP_INLINE_VISIBILITY
  1465. size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
  1466. size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1467. _LIBCPP_INLINE_VISIBILITY
  1468. size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
  1469. size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
  1470. _LIBCPP_INLINE_VISIBILITY
  1471. size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
  1472. size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1473. _LIBCPP_INLINE_VISIBILITY
  1474. size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
  1475. size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
  1476. _LIBCPP_INLINE_VISIBILITY
  1477. size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
  1478. size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1479. _LIBCPP_INLINE_VISIBILITY
  1480. size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
  1481. _LIBCPP_INLINE_VISIBILITY
  1482. size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
  1483. _LIBCPP_INLINE_VISIBILITY
  1484. size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
  1485. size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1486. _LIBCPP_INLINE_VISIBILITY
  1487. size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
  1488. _LIBCPP_INLINE_VISIBILITY
  1489. size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
  1490. _LIBCPP_INLINE_VISIBILITY
  1491. size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
  1492. size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1493. _LIBCPP_INLINE_VISIBILITY
  1494. size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
  1495. _LIBCPP_INLINE_VISIBILITY
  1496. size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
  1497. _LIBCPP_INLINE_VISIBILITY
  1498. size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
  1499. size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
  1500. _LIBCPP_INLINE_VISIBILITY
  1501. size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
  1502. _LIBCPP_INLINE_VISIBILITY
  1503. size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
  1504. _LIBCPP_INLINE_VISIBILITY
  1505. int compare(const basic_string& __str) const _NOEXCEPT;
  1506. _LIBCPP_INLINE_VISIBILITY
  1507. int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
  1508. int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
  1509. int compare(const value_type* __s) const _NOEXCEPT;
  1510. int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
  1511. int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
  1512. _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
  1513. _LIBCPP_INLINE_VISIBILITY
  1514. bool __is_long() const _NOEXCEPT
  1515. {return bool(__r_.first().__s.__size_ & __short_mask);}
  1516. #if _LIBCPP_DEBUG_LEVEL >= 2
  1517. bool __dereferenceable(const const_iterator* __i) const;
  1518. bool __decrementable(const const_iterator* __i) const;
  1519. bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
  1520. bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
  1521. #endif // _LIBCPP_DEBUG_LEVEL >= 2
  1522. private:
  1523. _LIBCPP_INLINE_VISIBILITY
  1524. allocator_type& __alloc() _NOEXCEPT
  1525. {return __r_.second();}
  1526. _LIBCPP_INLINE_VISIBILITY
  1527. const allocator_type& __alloc() const _NOEXCEPT
  1528. {return __r_.second();}
  1529. #ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  1530. _LIBCPP_INLINE_VISIBILITY
  1531. void __set_short_size(size_type __s) _NOEXCEPT
  1532. # if _LIBCPP_BIG_ENDIAN
  1533. {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
  1534. # else
  1535. {__r_.first().__s.__size_ = (unsigned char)(__s);}
  1536. # endif
  1537. _LIBCPP_INLINE_VISIBILITY
  1538. size_type __get_short_size() const _NOEXCEPT
  1539. # if _LIBCPP_BIG_ENDIAN
  1540. {return __r_.first().__s.__size_ >> 1;}
  1541. # else
  1542. {return __r_.first().__s.__size_;}
  1543. # endif
  1544. #else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  1545. _LIBCPP_INLINE_VISIBILITY
  1546. void __set_short_size(size_type __s) _NOEXCEPT
  1547. # if _LIBCPP_BIG_ENDIAN
  1548. {__r_.first().__s.__size_ = (unsigned char)(__s);}
  1549. # else
  1550. {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
  1551. # endif
  1552. _LIBCPP_INLINE_VISIBILITY
  1553. size_type __get_short_size() const _NOEXCEPT
  1554. # if _LIBCPP_BIG_ENDIAN
  1555. {return __r_.first().__s.__size_;}
  1556. # else
  1557. {return __r_.first().__s.__size_ >> 1;}
  1558. # endif
  1559. #endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
  1560. _LIBCPP_INLINE_VISIBILITY
  1561. void __set_long_size(size_type __s) _NOEXCEPT
  1562. {__r_.first().__l.__size_ = __s;}
  1563. _LIBCPP_INLINE_VISIBILITY
  1564. size_type __get_long_size() const _NOEXCEPT
  1565. {return __r_.first().__l.__size_;}
  1566. _LIBCPP_INLINE_VISIBILITY
  1567. void __set_size(size_type __s) _NOEXCEPT
  1568. {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
  1569. _LIBCPP_INLINE_VISIBILITY
  1570. void __set_long_cap(size_type __s) _NOEXCEPT
  1571. {__r_.first().__l.__cap_ = __long_mask | __s;}
  1572. _LIBCPP_INLINE_VISIBILITY
  1573. size_type __get_long_cap() const _NOEXCEPT
  1574. {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
  1575. _LIBCPP_INLINE_VISIBILITY
  1576. void __set_long_pointer(pointer __p) _NOEXCEPT
  1577. {__r_.first().__l.__data_ = __p;}
  1578. _LIBCPP_INLINE_VISIBILITY
  1579. pointer __get_long_pointer() _NOEXCEPT
  1580. {return __r_.first().__l.__data_;}
  1581. _LIBCPP_INLINE_VISIBILITY
  1582. const_pointer __get_long_pointer() const _NOEXCEPT
  1583. {return __r_.first().__l.__data_;}
  1584. _LIBCPP_INLINE_VISIBILITY
  1585. pointer __get_short_pointer() _NOEXCEPT
  1586. {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
  1587. _LIBCPP_INLINE_VISIBILITY
  1588. const_pointer __get_short_pointer() const _NOEXCEPT
  1589. {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
  1590. _LIBCPP_INLINE_VISIBILITY
  1591. pointer __get_pointer() _NOEXCEPT
  1592. {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
  1593. _LIBCPP_INLINE_VISIBILITY
  1594. const_pointer __get_pointer() const _NOEXCEPT
  1595. {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
  1596. _LIBCPP_INLINE_VISIBILITY
  1597. void __zero() _NOEXCEPT
  1598. {
  1599. size_type (&__a)[__n_words] = __r_.first().__r.__words;
  1600. for (unsigned __i = 0; __i < __n_words; ++__i)
  1601. __a[__i] = 0;
  1602. }
  1603. template <size_type __a> static
  1604. _LIBCPP_INLINE_VISIBILITY
  1605. size_type __align_it(size_type __s) _NOEXCEPT
  1606. {return (__s + (__a-1)) & ~(__a-1);}
  1607. enum {__alignment = 16};
  1608. static _LIBCPP_INLINE_VISIBILITY
  1609. size_type __recommend(size_type __s) _NOEXCEPT
  1610. {return (__s < __min_cap ? static_cast<size_type>(__min_cap) :
  1611. __align_it<sizeof(value_type) < __alignment ?
  1612. __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
  1613. void __init(const value_type* __s, size_type __sz, size_type __reserve);
  1614. void __init(const value_type* __s, size_type __sz);
  1615. void __init(size_type __n, value_type __c);
  1616. template <class _InputIterator>
  1617. typename enable_if
  1618. <
  1619. __is_exactly_input_iterator<_InputIterator>::value,
  1620. void
  1621. >::type
  1622. __init(_InputIterator __first, _InputIterator __last);
  1623. template <class _ForwardIterator>
  1624. typename enable_if
  1625. <
  1626. __is_forward_iterator<_ForwardIterator>::value,
  1627. void
  1628. >::type
  1629. __init(_ForwardIterator __first, _ForwardIterator __last);
  1630. void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
  1631. size_type __n_copy, size_type __n_del, size_type __n_add = 0);
  1632. void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
  1633. size_type __n_copy, size_type __n_del,
  1634. size_type __n_add, const value_type* __p_new_stuff);
  1635. _LIBCPP_INLINE_VISIBILITY
  1636. void __erase_to_end(size_type __pos);
  1637. _LIBCPP_INLINE_VISIBILITY
  1638. void __copy_assign_alloc(const basic_string& __str)
  1639. {__copy_assign_alloc(__str, integral_constant<bool,
  1640. __alloc_traits::propagate_on_container_copy_assignment::value>());}
  1641. _LIBCPP_INLINE_VISIBILITY
  1642. void __copy_assign_alloc(const basic_string& __str, true_type)
  1643. {
  1644. if (__alloc() != __str.__alloc())
  1645. {
  1646. clear();
  1647. shrink_to_fit();
  1648. }
  1649. __alloc() = __str.__alloc();
  1650. }
  1651. _LIBCPP_INLINE_VISIBILITY
  1652. void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
  1653. {}
  1654. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1655. _LIBCPP_INLINE_VISIBILITY
  1656. void __move_assign(basic_string& __str, false_type)
  1657. _NOEXCEPT_(__alloc_traits::is_always_equal::value);
  1658. _LIBCPP_INLINE_VISIBILITY
  1659. void __move_assign(basic_string& __str, true_type)
  1660. #if _LIBCPP_STD_VER > 14
  1661. _NOEXCEPT;
  1662. #else
  1663. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
  1664. #endif
  1665. #endif
  1666. _LIBCPP_INLINE_VISIBILITY
  1667. void
  1668. __move_assign_alloc(basic_string& __str)
  1669. _NOEXCEPT_(
  1670. !__alloc_traits::propagate_on_container_move_assignment::value ||
  1671. is_nothrow_move_assignable<allocator_type>::value)
  1672. {__move_assign_alloc(__str, integral_constant<bool,
  1673. __alloc_traits::propagate_on_container_move_assignment::value>());}
  1674. _LIBCPP_INLINE_VISIBILITY
  1675. void __move_assign_alloc(basic_string& __c, true_type)
  1676. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  1677. {
  1678. __alloc() = _VSTD::move(__c.__alloc());
  1679. }
  1680. _LIBCPP_INLINE_VISIBILITY
  1681. void __move_assign_alloc(basic_string&, false_type)
  1682. _NOEXCEPT
  1683. {}
  1684. _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
  1685. _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
  1686. friend basic_string operator+<>(const basic_string&, const basic_string&);
  1687. friend basic_string operator+<>(const value_type*, const basic_string&);
  1688. friend basic_string operator+<>(value_type, const basic_string&);
  1689. friend basic_string operator+<>(const basic_string&, const value_type*);
  1690. friend basic_string operator+<>(const basic_string&, value_type);
  1691. };
  1692. template <class _CharT, class _Traits, class _Allocator>
  1693. inline _LIBCPP_INLINE_VISIBILITY
  1694. void
  1695. basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
  1696. {
  1697. #if _LIBCPP_DEBUG_LEVEL >= 2
  1698. __get_db()->__invalidate_all(this);
  1699. #endif // _LIBCPP_DEBUG_LEVEL >= 2
  1700. }
  1701. template <class _CharT, class _Traits, class _Allocator>
  1702. inline _LIBCPP_INLINE_VISIBILITY
  1703. void
  1704. basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
  1705. #if _LIBCPP_DEBUG_LEVEL >= 2
  1706. __pos
  1707. #endif
  1708. )
  1709. {
  1710. #if _LIBCPP_DEBUG_LEVEL >= 2
  1711. __c_node* __c = __get_db()->__find_c_and_lock(this);
  1712. if (__c)
  1713. {
  1714. const_pointer __new_last = __get_pointer() + __pos;
  1715. for (__i_node** __p = __c->end_; __p != __c->beg_; )
  1716. {
  1717. --__p;
  1718. const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
  1719. if (__i->base() > __new_last)
  1720. {
  1721. (*__p)->__c_ = nullptr;
  1722. if (--__c->end_ != __p)
  1723. memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
  1724. }
  1725. }
  1726. __get_db()->unlock();
  1727. }
  1728. #endif // _LIBCPP_DEBUG_LEVEL >= 2
  1729. }
  1730. template <class _CharT, class _Traits, class _Allocator>
  1731. inline _LIBCPP_INLINE_VISIBILITY
  1732. basic_string<_CharT, _Traits, _Allocator>::basic_string()
  1733. _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
  1734. {
  1735. #if _LIBCPP_DEBUG_LEVEL >= 2
  1736. __get_db()->__insert_c(this);
  1737. #endif
  1738. __zero();
  1739. }
  1740. template <class _CharT, class _Traits, class _Allocator>
  1741. inline _LIBCPP_INLINE_VISIBILITY
  1742. basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
  1743. #if _LIBCPP_STD_VER <= 14
  1744. _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
  1745. #else
  1746. _NOEXCEPT
  1747. #endif
  1748. : __r_(__a)
  1749. {
  1750. #if _LIBCPP_DEBUG_LEVEL >= 2
  1751. __get_db()->__insert_c(this);
  1752. #endif
  1753. __zero();
  1754. }
  1755. template <class _CharT, class _Traits, class _Allocator>
  1756. void
  1757. basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz, size_type __reserve)
  1758. {
  1759. if (__reserve > max_size())
  1760. this->__throw_length_error();
  1761. pointer __p;
  1762. if (__reserve < __min_cap)
  1763. {
  1764. __set_short_size(__sz);
  1765. __p = __get_short_pointer();
  1766. }
  1767. else
  1768. {
  1769. size_type __cap = __recommend(__reserve);
  1770. __p = __alloc_traits::allocate(__alloc(), __cap+1);
  1771. __set_long_pointer(__p);
  1772. __set_long_cap(__cap+1);
  1773. __set_long_size(__sz);
  1774. }
  1775. traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
  1776. traits_type::assign(__p[__sz], value_type());
  1777. }
  1778. template <class _CharT, class _Traits, class _Allocator>
  1779. void
  1780. basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
  1781. {
  1782. if (__sz > max_size())
  1783. this->__throw_length_error();
  1784. pointer __p;
  1785. if (__sz < __min_cap)
  1786. {
  1787. __set_short_size(__sz);
  1788. __p = __get_short_pointer();
  1789. }
  1790. else
  1791. {
  1792. size_type __cap = __recommend(__sz);
  1793. __p = __alloc_traits::allocate(__alloc(), __cap+1);
  1794. __set_long_pointer(__p);
  1795. __set_long_cap(__cap+1);
  1796. __set_long_size(__sz);
  1797. }
  1798. traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
  1799. traits_type::assign(__p[__sz], value_type());
  1800. }
  1801. template <class _CharT, class _Traits, class _Allocator>
  1802. inline _LIBCPP_INLINE_VISIBILITY
  1803. basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s)
  1804. {
  1805. _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
  1806. __init(__s, traits_type::length(__s));
  1807. #if _LIBCPP_DEBUG_LEVEL >= 2
  1808. __get_db()->__insert_c(this);
  1809. #endif
  1810. }
  1811. template <class _CharT, class _Traits, class _Allocator>
  1812. inline _LIBCPP_INLINE_VISIBILITY
  1813. basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, const allocator_type& __a)
  1814. : __r_(__a)
  1815. {
  1816. _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
  1817. __init(__s, traits_type::length(__s));
  1818. #if _LIBCPP_DEBUG_LEVEL >= 2
  1819. __get_db()->__insert_c(this);
  1820. #endif
  1821. }
  1822. template <class _CharT, class _Traits, class _Allocator>
  1823. inline _LIBCPP_INLINE_VISIBILITY
  1824. basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, size_type __n)
  1825. {
  1826. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
  1827. __init(__s, __n);
  1828. #if _LIBCPP_DEBUG_LEVEL >= 2
  1829. __get_db()->__insert_c(this);
  1830. #endif
  1831. }
  1832. template <class _CharT, class _Traits, class _Allocator>
  1833. inline _LIBCPP_INLINE_VISIBILITY
  1834. basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, size_type __n, const allocator_type& __a)
  1835. : __r_(__a)
  1836. {
  1837. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
  1838. __init(__s, __n);
  1839. #if _LIBCPP_DEBUG_LEVEL >= 2
  1840. __get_db()->__insert_c(this);
  1841. #endif
  1842. }
  1843. template <class _CharT, class _Traits, class _Allocator>
  1844. basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
  1845. : __r_(__alloc_traits::select_on_container_copy_construction(__str.__alloc()))
  1846. {
  1847. if (!__str.__is_long())
  1848. __r_.first().__r = __str.__r_.first().__r;
  1849. else
  1850. __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
  1851. #if _LIBCPP_DEBUG_LEVEL >= 2
  1852. __get_db()->__insert_c(this);
  1853. #endif
  1854. }
  1855. template <class _CharT, class _Traits, class _Allocator>
  1856. basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, const allocator_type& __a)
  1857. : __r_(__a)
  1858. {
  1859. if (!__str.__is_long())
  1860. __r_.first().__r = __str.__r_.first().__r;
  1861. else
  1862. __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
  1863. #if _LIBCPP_DEBUG_LEVEL >= 2
  1864. __get_db()->__insert_c(this);
  1865. #endif
  1866. }
  1867. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1868. template <class _CharT, class _Traits, class _Allocator>
  1869. inline _LIBCPP_INLINE_VISIBILITY
  1870. basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
  1871. #if _LIBCPP_STD_VER <= 14
  1872. _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
  1873. #else
  1874. _NOEXCEPT
  1875. #endif
  1876. : __r_(_VSTD::move(__str.__r_))
  1877. {
  1878. __str.__zero();
  1879. #if _LIBCPP_DEBUG_LEVEL >= 2
  1880. __get_db()->__insert_c(this);
  1881. if (__is_long())
  1882. __get_db()->swap(this, &__str);
  1883. #endif
  1884. }
  1885. template <class _CharT, class _Traits, class _Allocator>
  1886. inline _LIBCPP_INLINE_VISIBILITY
  1887. basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
  1888. : __r_(__a)
  1889. {
  1890. if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
  1891. __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
  1892. else
  1893. {
  1894. __r_.first().__r = __str.__r_.first().__r;
  1895. __str.__zero();
  1896. }
  1897. #if _LIBCPP_DEBUG_LEVEL >= 2
  1898. __get_db()->__insert_c(this);
  1899. if (__is_long())
  1900. __get_db()->swap(this, &__str);
  1901. #endif
  1902. }
  1903. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  1904. template <class _CharT, class _Traits, class _Allocator>
  1905. void
  1906. basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
  1907. {
  1908. if (__n > max_size())
  1909. this->__throw_length_error();
  1910. pointer __p;
  1911. if (__n < __min_cap)
  1912. {
  1913. __set_short_size(__n);
  1914. __p = __get_short_pointer();
  1915. }
  1916. else
  1917. {
  1918. size_type __cap = __recommend(__n);
  1919. __p = __alloc_traits::allocate(__alloc(), __cap+1);
  1920. __set_long_pointer(__p);
  1921. __set_long_cap(__cap+1);
  1922. __set_long_size(__n);
  1923. }
  1924. traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
  1925. traits_type::assign(__p[__n], value_type());
  1926. }
  1927. template <class _CharT, class _Traits, class _Allocator>
  1928. inline _LIBCPP_INLINE_VISIBILITY
  1929. basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c)
  1930. {
  1931. __init(__n, __c);
  1932. #if _LIBCPP_DEBUG_LEVEL >= 2
  1933. __get_db()->__insert_c(this);
  1934. #endif
  1935. }
  1936. template <class _CharT, class _Traits, class _Allocator>
  1937. inline _LIBCPP_INLINE_VISIBILITY
  1938. basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c, const allocator_type& __a)
  1939. : __r_(__a)
  1940. {
  1941. __init(__n, __c);
  1942. #if _LIBCPP_DEBUG_LEVEL >= 2
  1943. __get_db()->__insert_c(this);
  1944. #endif
  1945. }
  1946. template <class _CharT, class _Traits, class _Allocator>
  1947. basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos, size_type __n,
  1948. const allocator_type& __a)
  1949. : __r_(__a)
  1950. {
  1951. size_type __str_sz = __str.size();
  1952. if (__pos > __str_sz)
  1953. this->__throw_out_of_range();
  1954. __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
  1955. #if _LIBCPP_DEBUG_LEVEL >= 2
  1956. __get_db()->__insert_c(this);
  1957. #endif
  1958. }
  1959. template <class _CharT, class _Traits, class _Allocator>
  1960. inline _LIBCPP_INLINE_VISIBILITY
  1961. basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
  1962. const allocator_type& __a)
  1963. : __r_(__a)
  1964. {
  1965. size_type __str_sz = __str.size();
  1966. if (__pos > __str_sz)
  1967. this->__throw_out_of_range();
  1968. __init(__str.data() + __pos, __str_sz - __pos);
  1969. #if _LIBCPP_DEBUG_LEVEL >= 2
  1970. __get_db()->__insert_c(this);
  1971. #endif
  1972. }
  1973. template <class _CharT, class _Traits, class _Allocator>
  1974. template <class _InputIterator>
  1975. typename enable_if
  1976. <
  1977. __is_exactly_input_iterator<_InputIterator>::value,
  1978. void
  1979. >::type
  1980. basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
  1981. {
  1982. __zero();
  1983. #ifndef _LIBCPP_NO_EXCEPTIONS
  1984. try
  1985. {
  1986. #endif // _LIBCPP_NO_EXCEPTIONS
  1987. for (; __first != __last; ++__first)
  1988. push_back(*__first);
  1989. #ifndef _LIBCPP_NO_EXCEPTIONS
  1990. }
  1991. catch (...)
  1992. {
  1993. if (__is_long())
  1994. __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
  1995. throw;
  1996. }
  1997. #endif // _LIBCPP_NO_EXCEPTIONS
  1998. }
  1999. template <class _CharT, class _Traits, class _Allocator>
  2000. template <class _ForwardIterator>
  2001. typename enable_if
  2002. <
  2003. __is_forward_iterator<_ForwardIterator>::value,
  2004. void
  2005. >::type
  2006. basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
  2007. {
  2008. size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
  2009. if (__sz > max_size())
  2010. this->__throw_length_error();
  2011. pointer __p;
  2012. if (__sz < __min_cap)
  2013. {
  2014. __set_short_size(__sz);
  2015. __p = __get_short_pointer();
  2016. }
  2017. else
  2018. {
  2019. size_type __cap = __recommend(__sz);
  2020. __p = __alloc_traits::allocate(__alloc(), __cap+1);
  2021. __set_long_pointer(__p);
  2022. __set_long_cap(__cap+1);
  2023. __set_long_size(__sz);
  2024. }
  2025. for (; __first != __last; ++__first, (void) ++__p)
  2026. traits_type::assign(*__p, *__first);
  2027. traits_type::assign(*__p, value_type());
  2028. }
  2029. template <class _CharT, class _Traits, class _Allocator>
  2030. template<class _InputIterator>
  2031. inline _LIBCPP_INLINE_VISIBILITY
  2032. basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
  2033. {
  2034. __init(__first, __last);
  2035. #if _LIBCPP_DEBUG_LEVEL >= 2
  2036. __get_db()->__insert_c(this);
  2037. #endif
  2038. }
  2039. template <class _CharT, class _Traits, class _Allocator>
  2040. template<class _InputIterator>
  2041. inline _LIBCPP_INLINE_VISIBILITY
  2042. basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
  2043. const allocator_type& __a)
  2044. : __r_(__a)
  2045. {
  2046. __init(__first, __last);
  2047. #if _LIBCPP_DEBUG_LEVEL >= 2
  2048. __get_db()->__insert_c(this);
  2049. #endif
  2050. }
  2051. #ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  2052. template <class _CharT, class _Traits, class _Allocator>
  2053. inline _LIBCPP_INLINE_VISIBILITY
  2054. basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il)
  2055. {
  2056. __init(__il.begin(), __il.end());
  2057. #if _LIBCPP_DEBUG_LEVEL >= 2
  2058. __get_db()->__insert_c(this);
  2059. #endif
  2060. }
  2061. template <class _CharT, class _Traits, class _Allocator>
  2062. inline _LIBCPP_INLINE_VISIBILITY
  2063. basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il, const allocator_type& __a)
  2064. : __r_(__a)
  2065. {
  2066. __init(__il.begin(), __il.end());
  2067. #if _LIBCPP_DEBUG_LEVEL >= 2
  2068. __get_db()->__insert_c(this);
  2069. #endif
  2070. }
  2071. #endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
  2072. template <class _CharT, class _Traits, class _Allocator>
  2073. basic_string<_CharT, _Traits, _Allocator>::~basic_string()
  2074. {
  2075. #if _LIBCPP_DEBUG_LEVEL >= 2
  2076. __get_db()->__erase_c(this);
  2077. #endif
  2078. if (__is_long())
  2079. __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
  2080. }
  2081. template <class _CharT, class _Traits, class _Allocator>
  2082. void
  2083. basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
  2084. (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
  2085. size_type __n_copy, size_type __n_del, size_type __n_add, const value_type* __p_new_stuff)
  2086. {
  2087. size_type __ms = max_size();
  2088. if (__delta_cap > __ms - __old_cap - 1)
  2089. this->__throw_length_error();
  2090. pointer __old_p = __get_pointer();
  2091. size_type __cap = __old_cap < __ms / 2 - __alignment ?
  2092. __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
  2093. __ms - 1;
  2094. pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
  2095. __invalidate_all_iterators();
  2096. if (__n_copy != 0)
  2097. traits_type::copy(_VSTD::__to_raw_pointer(__p),
  2098. _VSTD::__to_raw_pointer(__old_p), __n_copy);
  2099. if (__n_add != 0)
  2100. traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
  2101. size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
  2102. if (__sec_cp_sz != 0)
  2103. traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
  2104. _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
  2105. if (__old_cap+1 != __min_cap)
  2106. __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
  2107. __set_long_pointer(__p);
  2108. __set_long_cap(__cap+1);
  2109. __old_sz = __n_copy + __n_add + __sec_cp_sz;
  2110. __set_long_size(__old_sz);
  2111. traits_type::assign(__p[__old_sz], value_type());
  2112. }
  2113. template <class _CharT, class _Traits, class _Allocator>
  2114. void
  2115. basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
  2116. size_type __n_copy, size_type __n_del, size_type __n_add)
  2117. {
  2118. size_type __ms = max_size();
  2119. if (__delta_cap > __ms - __old_cap)
  2120. this->__throw_length_error();
  2121. pointer __old_p = __get_pointer();
  2122. size_type __cap = __old_cap < __ms / 2 - __alignment ?
  2123. __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
  2124. __ms - 1;
  2125. pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
  2126. __invalidate_all_iterators();
  2127. if (__n_copy != 0)
  2128. traits_type::copy(_VSTD::__to_raw_pointer(__p),
  2129. _VSTD::__to_raw_pointer(__old_p), __n_copy);
  2130. size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
  2131. if (__sec_cp_sz != 0)
  2132. traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
  2133. _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
  2134. __sec_cp_sz);
  2135. if (__old_cap+1 != __min_cap)
  2136. __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
  2137. __set_long_pointer(__p);
  2138. __set_long_cap(__cap+1);
  2139. }
  2140. // assign
  2141. template <class _CharT, class _Traits, class _Allocator>
  2142. basic_string<_CharT, _Traits, _Allocator>&
  2143. basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
  2144. {
  2145. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
  2146. size_type __cap = capacity();
  2147. if (__cap >= __n)
  2148. {
  2149. value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
  2150. traits_type::move(__p, __s, __n);
  2151. traits_type::assign(__p[__n], value_type());
  2152. __set_size(__n);
  2153. __invalidate_iterators_past(__n);
  2154. }
  2155. else
  2156. {
  2157. size_type __sz = size();
  2158. __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
  2159. }
  2160. return *this;
  2161. }
  2162. template <class _CharT, class _Traits, class _Allocator>
  2163. basic_string<_CharT, _Traits, _Allocator>&
  2164. basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
  2165. {
  2166. size_type __cap = capacity();
  2167. if (__cap < __n)
  2168. {
  2169. size_type __sz = size();
  2170. __grow_by(__cap, __n - __cap, __sz, 0, __sz);
  2171. }
  2172. else
  2173. __invalidate_iterators_past(__n);
  2174. value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
  2175. traits_type::assign(__p, __n, __c);
  2176. traits_type::assign(__p[__n], value_type());
  2177. __set_size(__n);
  2178. return *this;
  2179. }
  2180. template <class _CharT, class _Traits, class _Allocator>
  2181. basic_string<_CharT, _Traits, _Allocator>&
  2182. basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
  2183. {
  2184. pointer __p;
  2185. if (__is_long())
  2186. {
  2187. __p = __get_long_pointer();
  2188. __set_long_size(1);
  2189. }
  2190. else
  2191. {
  2192. __p = __get_short_pointer();
  2193. __set_short_size(1);
  2194. }
  2195. traits_type::assign(*__p, __c);
  2196. traits_type::assign(*++__p, value_type());
  2197. __invalidate_iterators_past(1);
  2198. return *this;
  2199. }
  2200. template <class _CharT, class _Traits, class _Allocator>
  2201. basic_string<_CharT, _Traits, _Allocator>&
  2202. basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
  2203. {
  2204. if (this != &__str)
  2205. {
  2206. __copy_assign_alloc(__str);
  2207. assign(__str.data(), __str.size());
  2208. }
  2209. return *this;
  2210. }
  2211. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  2212. template <class _CharT, class _Traits, class _Allocator>
  2213. inline _LIBCPP_INLINE_VISIBILITY
  2214. void
  2215. basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
  2216. _NOEXCEPT_(__alloc_traits::is_always_equal::value)
  2217. {
  2218. if (__alloc() != __str.__alloc())
  2219. assign(__str);
  2220. else
  2221. __move_assign(__str, true_type());
  2222. }
  2223. template <class _CharT, class _Traits, class _Allocator>
  2224. inline _LIBCPP_INLINE_VISIBILITY
  2225. void
  2226. basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
  2227. #if _LIBCPP_STD_VER > 14
  2228. _NOEXCEPT
  2229. #else
  2230. _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
  2231. #endif
  2232. {
  2233. clear();
  2234. shrink_to_fit();
  2235. __r_.first() = __str.__r_.first();
  2236. __move_assign_alloc(__str);
  2237. __str.__zero();
  2238. }
  2239. template <class _CharT, class _Traits, class _Allocator>
  2240. inline _LIBCPP_INLINE_VISIBILITY
  2241. basic_string<_CharT, _Traits, _Allocator>&
  2242. basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
  2243. _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
  2244. {
  2245. __move_assign(__str, integral_constant<bool,
  2246. __alloc_traits::propagate_on_container_move_assignment::value>());
  2247. return *this;
  2248. }
  2249. #endif
  2250. template <class _CharT, class _Traits, class _Allocator>
  2251. template<class _InputIterator>
  2252. typename enable_if
  2253. <
  2254. __is_exactly_input_iterator <_InputIterator>::value
  2255. || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
  2256. basic_string<_CharT, _Traits, _Allocator>&
  2257. >::type
  2258. basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
  2259. {
  2260. basic_string __temp(__first, __last, __alloc());
  2261. assign(__temp.data(), __temp.size());
  2262. return *this;
  2263. }
  2264. template <class _CharT, class _Traits, class _Allocator>
  2265. template<class _ForwardIterator>
  2266. typename enable_if
  2267. <
  2268. __is_forward_iterator<_ForwardIterator>::value
  2269. && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
  2270. basic_string<_CharT, _Traits, _Allocator>&
  2271. >::type
  2272. basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
  2273. {
  2274. size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
  2275. size_type __cap = capacity();
  2276. if (__cap < __n)
  2277. {
  2278. size_type __sz = size();
  2279. __grow_by(__cap, __n - __cap, __sz, 0, __sz);
  2280. }
  2281. else
  2282. __invalidate_iterators_past(__n);
  2283. pointer __p = __get_pointer();
  2284. for (; __first != __last; ++__first, ++__p)
  2285. traits_type::assign(*__p, *__first);
  2286. traits_type::assign(*__p, value_type());
  2287. __set_size(__n);
  2288. return *this;
  2289. }
  2290. template <class _CharT, class _Traits, class _Allocator>
  2291. basic_string<_CharT, _Traits, _Allocator>&
  2292. basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
  2293. {
  2294. size_type __sz = __str.size();
  2295. if (__pos > __sz)
  2296. this->__throw_out_of_range();
  2297. return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
  2298. }
  2299. template <class _CharT, class _Traits, class _Allocator>
  2300. basic_string<_CharT, _Traits, _Allocator>&
  2301. basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
  2302. {
  2303. _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
  2304. return assign(__s, traits_type::length(__s));
  2305. }
  2306. // append
  2307. template <class _CharT, class _Traits, class _Allocator>
  2308. basic_string<_CharT, _Traits, _Allocator>&
  2309. basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
  2310. {
  2311. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
  2312. size_type __cap = capacity();
  2313. size_type __sz = size();
  2314. if (__cap - __sz >= __n)
  2315. {
  2316. if (__n)
  2317. {
  2318. value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
  2319. traits_type::copy(__p + __sz, __s, __n);
  2320. __sz += __n;
  2321. __set_size(__sz);
  2322. traits_type::assign(__p[__sz], value_type());
  2323. }
  2324. }
  2325. else
  2326. __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
  2327. return *this;
  2328. }
  2329. template <class _CharT, class _Traits, class _Allocator>
  2330. basic_string<_CharT, _Traits, _Allocator>&
  2331. basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
  2332. {
  2333. if (__n)
  2334. {
  2335. size_type __cap = capacity();
  2336. size_type __sz = size();
  2337. if (__cap - __sz < __n)
  2338. __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
  2339. pointer __p = __get_pointer();
  2340. traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
  2341. __sz += __n;
  2342. __set_size(__sz);
  2343. traits_type::assign(__p[__sz], value_type());
  2344. }
  2345. return *this;
  2346. }
  2347. template <class _CharT, class _Traits, class _Allocator>
  2348. void
  2349. basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
  2350. {
  2351. bool __is_short = !__is_long();
  2352. size_type __cap;
  2353. size_type __sz;
  2354. if (__is_short)
  2355. {
  2356. __cap = __min_cap - 1;
  2357. __sz = __get_short_size();
  2358. }
  2359. else
  2360. {
  2361. __cap = __get_long_cap() - 1;
  2362. __sz = __get_long_size();
  2363. }
  2364. if (__sz == __cap)
  2365. {
  2366. __grow_by(__cap, 1, __sz, __sz, 0);
  2367. __is_short = !__is_long();
  2368. }
  2369. pointer __p;
  2370. if (__is_short)
  2371. {
  2372. __p = __get_short_pointer() + __sz;
  2373. __set_short_size(__sz+1);
  2374. }
  2375. else
  2376. {
  2377. __p = __get_long_pointer() + __sz;
  2378. __set_long_size(__sz+1);
  2379. }
  2380. traits_type::assign(*__p, __c);
  2381. traits_type::assign(*++__p, value_type());
  2382. }
  2383. template <class _CharT, class _Traits, class _Allocator>
  2384. template<class _InputIterator>
  2385. typename enable_if
  2386. <
  2387. __is_exactly_input_iterator<_InputIterator>::value
  2388. || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
  2389. basic_string<_CharT, _Traits, _Allocator>&
  2390. >::type
  2391. basic_string<_CharT, _Traits, _Allocator>::append(_InputIterator __first, _InputIterator __last)
  2392. {
  2393. basic_string __temp (__first, __last, __alloc());
  2394. append(__temp.data(), __temp.size());
  2395. return *this;
  2396. }
  2397. template <class _CharT, class _Traits, class _Allocator>
  2398. template<class _ForwardIterator>
  2399. typename enable_if
  2400. <
  2401. __is_forward_iterator<_ForwardIterator>::value
  2402. && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
  2403. basic_string<_CharT, _Traits, _Allocator>&
  2404. >::type
  2405. basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last)
  2406. {
  2407. size_type __sz = size();
  2408. size_type __cap = capacity();
  2409. size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
  2410. if (__n)
  2411. {
  2412. if (__cap - __sz < __n)
  2413. __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
  2414. pointer __p = __get_pointer() + __sz;
  2415. for (; __first != __last; ++__p, ++__first)
  2416. traits_type::assign(*__p, *__first);
  2417. traits_type::assign(*__p, value_type());
  2418. __set_size(__sz + __n);
  2419. }
  2420. return *this;
  2421. }
  2422. template <class _CharT, class _Traits, class _Allocator>
  2423. inline _LIBCPP_INLINE_VISIBILITY
  2424. basic_string<_CharT, _Traits, _Allocator>&
  2425. basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
  2426. {
  2427. return append(__str.data(), __str.size());
  2428. }
  2429. template <class _CharT, class _Traits, class _Allocator>
  2430. basic_string<_CharT, _Traits, _Allocator>&
  2431. basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
  2432. {
  2433. size_type __sz = __str.size();
  2434. if (__pos > __sz)
  2435. this->__throw_out_of_range();
  2436. return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
  2437. }
  2438. template <class _CharT, class _Traits, class _Allocator>
  2439. basic_string<_CharT, _Traits, _Allocator>&
  2440. basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
  2441. {
  2442. _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
  2443. return append(__s, traits_type::length(__s));
  2444. }
  2445. // insert
  2446. template <class _CharT, class _Traits, class _Allocator>
  2447. basic_string<_CharT, _Traits, _Allocator>&
  2448. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
  2449. {
  2450. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
  2451. size_type __sz = size();
  2452. if (__pos > __sz)
  2453. this->__throw_out_of_range();
  2454. size_type __cap = capacity();
  2455. if (__cap - __sz >= __n)
  2456. {
  2457. if (__n)
  2458. {
  2459. value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
  2460. size_type __n_move = __sz - __pos;
  2461. if (__n_move != 0)
  2462. {
  2463. if (__p + __pos <= __s && __s < __p + __sz)
  2464. __s += __n;
  2465. traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
  2466. }
  2467. traits_type::move(__p + __pos, __s, __n);
  2468. __sz += __n;
  2469. __set_size(__sz);
  2470. traits_type::assign(__p[__sz], value_type());
  2471. }
  2472. }
  2473. else
  2474. __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
  2475. return *this;
  2476. }
  2477. template <class _CharT, class _Traits, class _Allocator>
  2478. basic_string<_CharT, _Traits, _Allocator>&
  2479. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
  2480. {
  2481. size_type __sz = size();
  2482. if (__pos > __sz)
  2483. this->__throw_out_of_range();
  2484. if (__n)
  2485. {
  2486. size_type __cap = capacity();
  2487. value_type* __p;
  2488. if (__cap - __sz >= __n)
  2489. {
  2490. __p = _VSTD::__to_raw_pointer(__get_pointer());
  2491. size_type __n_move = __sz - __pos;
  2492. if (__n_move != 0)
  2493. traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
  2494. }
  2495. else
  2496. {
  2497. __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
  2498. __p = _VSTD::__to_raw_pointer(__get_long_pointer());
  2499. }
  2500. traits_type::assign(__p + __pos, __n, __c);
  2501. __sz += __n;
  2502. __set_size(__sz);
  2503. traits_type::assign(__p[__sz], value_type());
  2504. }
  2505. return *this;
  2506. }
  2507. template <class _CharT, class _Traits, class _Allocator>
  2508. template<class _InputIterator>
  2509. typename enable_if
  2510. <
  2511. __is_exactly_input_iterator<_InputIterator>::value
  2512. || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
  2513. typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2514. >::type
  2515. basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
  2516. {
  2517. #if _LIBCPP_DEBUG_LEVEL >= 2
  2518. _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
  2519. "string::insert(iterator, range) called with an iterator not"
  2520. " referring to this string");
  2521. #endif
  2522. basic_string __temp(__first, __last, __alloc());
  2523. return insert(__pos, __temp.data(), __temp.data() + __temp.size());
  2524. }
  2525. template <class _CharT, class _Traits, class _Allocator>
  2526. template<class _ForwardIterator>
  2527. typename enable_if
  2528. <
  2529. __is_forward_iterator<_ForwardIterator>::value
  2530. && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
  2531. typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2532. >::type
  2533. basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
  2534. {
  2535. #if _LIBCPP_DEBUG_LEVEL >= 2
  2536. _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
  2537. "string::insert(iterator, range) called with an iterator not"
  2538. " referring to this string");
  2539. #endif
  2540. size_type __ip = static_cast<size_type>(__pos - begin());
  2541. size_type __sz = size();
  2542. size_type __cap = capacity();
  2543. size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
  2544. if (__n)
  2545. {
  2546. value_type* __p;
  2547. if (__cap - __sz >= __n)
  2548. {
  2549. __p = _VSTD::__to_raw_pointer(__get_pointer());
  2550. size_type __n_move = __sz - __ip;
  2551. if (__n_move != 0)
  2552. traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
  2553. }
  2554. else
  2555. {
  2556. __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
  2557. __p = _VSTD::__to_raw_pointer(__get_long_pointer());
  2558. }
  2559. __sz += __n;
  2560. __set_size(__sz);
  2561. traits_type::assign(__p[__sz], value_type());
  2562. for (__p += __ip; __first != __last; ++__p, ++__first)
  2563. traits_type::assign(*__p, *__first);
  2564. }
  2565. return begin() + __ip;
  2566. }
  2567. template <class _CharT, class _Traits, class _Allocator>
  2568. inline _LIBCPP_INLINE_VISIBILITY
  2569. basic_string<_CharT, _Traits, _Allocator>&
  2570. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
  2571. {
  2572. return insert(__pos1, __str.data(), __str.size());
  2573. }
  2574. template <class _CharT, class _Traits, class _Allocator>
  2575. basic_string<_CharT, _Traits, _Allocator>&
  2576. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
  2577. size_type __pos2, size_type __n)
  2578. {
  2579. size_type __str_sz = __str.size();
  2580. if (__pos2 > __str_sz)
  2581. this->__throw_out_of_range();
  2582. return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
  2583. }
  2584. template <class _CharT, class _Traits, class _Allocator>
  2585. basic_string<_CharT, _Traits, _Allocator>&
  2586. basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
  2587. {
  2588. _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
  2589. return insert(__pos, __s, traits_type::length(__s));
  2590. }
  2591. template <class _CharT, class _Traits, class _Allocator>
  2592. typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2593. basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
  2594. {
  2595. size_type __ip = static_cast<size_type>(__pos - begin());
  2596. size_type __sz = size();
  2597. size_type __cap = capacity();
  2598. value_type* __p;
  2599. if (__cap == __sz)
  2600. {
  2601. __grow_by(__cap, 1, __sz, __ip, 0, 1);
  2602. __p = _VSTD::__to_raw_pointer(__get_long_pointer());
  2603. }
  2604. else
  2605. {
  2606. __p = _VSTD::__to_raw_pointer(__get_pointer());
  2607. size_type __n_move = __sz - __ip;
  2608. if (__n_move != 0)
  2609. traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
  2610. }
  2611. traits_type::assign(__p[__ip], __c);
  2612. traits_type::assign(__p[++__sz], value_type());
  2613. __set_size(__sz);
  2614. return begin() + static_cast<difference_type>(__ip);
  2615. }
  2616. template <class _CharT, class _Traits, class _Allocator>
  2617. inline _LIBCPP_INLINE_VISIBILITY
  2618. typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2619. basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
  2620. {
  2621. #if _LIBCPP_DEBUG_LEVEL >= 2
  2622. _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
  2623. "string::insert(iterator, n, value) called with an iterator not"
  2624. " referring to this string");
  2625. #endif
  2626. difference_type __p = __pos - begin();
  2627. insert(static_cast<size_type>(__p), __n, __c);
  2628. return begin() + __p;
  2629. }
  2630. // replace
  2631. template <class _CharT, class _Traits, class _Allocator>
  2632. basic_string<_CharT, _Traits, _Allocator>&
  2633. basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
  2634. {
  2635. _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
  2636. size_type __sz = size();
  2637. if (__pos > __sz)
  2638. this->__throw_out_of_range();
  2639. __n1 = _VSTD::min(__n1, __sz - __pos);
  2640. size_type __cap = capacity();
  2641. if (__cap - __sz + __n1 >= __n2)
  2642. {
  2643. value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
  2644. if (__n1 != __n2)
  2645. {
  2646. size_type __n_move = __sz - __pos - __n1;
  2647. if (__n_move != 0)
  2648. {
  2649. if (__n1 > __n2)
  2650. {
  2651. traits_type::move(__p + __pos, __s, __n2);
  2652. traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
  2653. goto __finish;
  2654. }
  2655. if (__p + __pos < __s && __s < __p + __sz)
  2656. {
  2657. if (__p + __pos + __n1 <= __s)
  2658. __s += __n2 - __n1;
  2659. else // __p + __pos < __s < __p + __pos + __n1
  2660. {
  2661. traits_type::move(__p + __pos, __s, __n1);
  2662. __pos += __n1;
  2663. __s += __n2;
  2664. __n2 -= __n1;
  2665. __n1 = 0;
  2666. }
  2667. }
  2668. traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
  2669. }
  2670. }
  2671. traits_type::move(__p + __pos, __s, __n2);
  2672. __finish:
  2673. __sz += __n2 - __n1;
  2674. __set_size(__sz);
  2675. __invalidate_iterators_past(__sz);
  2676. traits_type::assign(__p[__sz], value_type());
  2677. }
  2678. else
  2679. __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
  2680. return *this;
  2681. }
  2682. template <class _CharT, class _Traits, class _Allocator>
  2683. basic_string<_CharT, _Traits, _Allocator>&
  2684. basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
  2685. {
  2686. size_type __sz = size();
  2687. if (__pos > __sz)
  2688. this->__throw_out_of_range();
  2689. __n1 = _VSTD::min(__n1, __sz - __pos);
  2690. size_type __cap = capacity();
  2691. value_type* __p;
  2692. if (__cap - __sz + __n1 >= __n2)
  2693. {
  2694. __p = _VSTD::__to_raw_pointer(__get_pointer());
  2695. if (__n1 != __n2)
  2696. {
  2697. size_type __n_move = __sz - __pos - __n1;
  2698. if (__n_move != 0)
  2699. traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
  2700. }
  2701. }
  2702. else
  2703. {
  2704. __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
  2705. __p = _VSTD::__to_raw_pointer(__get_long_pointer());
  2706. }
  2707. traits_type::assign(__p + __pos, __n2, __c);
  2708. __sz += __n2 - __n1;
  2709. __set_size(__sz);
  2710. __invalidate_iterators_past(__sz);
  2711. traits_type::assign(__p[__sz], value_type());
  2712. return *this;
  2713. }
  2714. template <class _CharT, class _Traits, class _Allocator>
  2715. template<class _InputIterator>
  2716. typename enable_if
  2717. <
  2718. __is_input_iterator<_InputIterator>::value,
  2719. basic_string<_CharT, _Traits, _Allocator>&
  2720. >::type
  2721. basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
  2722. _InputIterator __j1, _InputIterator __j2)
  2723. {
  2724. basic_string __temp(__j1, __j2, __alloc());
  2725. return this->replace(__i1, __i2, __temp);
  2726. }
  2727. template <class _CharT, class _Traits, class _Allocator>
  2728. inline _LIBCPP_INLINE_VISIBILITY
  2729. basic_string<_CharT, _Traits, _Allocator>&
  2730. basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
  2731. {
  2732. return replace(__pos1, __n1, __str.data(), __str.size());
  2733. }
  2734. template <class _CharT, class _Traits, class _Allocator>
  2735. basic_string<_CharT, _Traits, _Allocator>&
  2736. basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
  2737. size_type __pos2, size_type __n2)
  2738. {
  2739. size_type __str_sz = __str.size();
  2740. if (__pos2 > __str_sz)
  2741. this->__throw_out_of_range();
  2742. return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
  2743. }
  2744. template <class _CharT, class _Traits, class _Allocator>
  2745. basic_string<_CharT, _Traits, _Allocator>&
  2746. basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
  2747. {
  2748. _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
  2749. return replace(__pos, __n1, __s, traits_type::length(__s));
  2750. }
  2751. template <class _CharT, class _Traits, class _Allocator>
  2752. inline _LIBCPP_INLINE_VISIBILITY
  2753. basic_string<_CharT, _Traits, _Allocator>&
  2754. basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
  2755. {
  2756. return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
  2757. __str.data(), __str.size());
  2758. }
  2759. template <class _CharT, class _Traits, class _Allocator>
  2760. inline _LIBCPP_INLINE_VISIBILITY
  2761. basic_string<_CharT, _Traits, _Allocator>&
  2762. basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
  2763. {
  2764. return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
  2765. }
  2766. template <class _CharT, class _Traits, class _Allocator>
  2767. inline _LIBCPP_INLINE_VISIBILITY
  2768. basic_string<_CharT, _Traits, _Allocator>&
  2769. basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
  2770. {
  2771. return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
  2772. }
  2773. template <class _CharT, class _Traits, class _Allocator>
  2774. inline _LIBCPP_INLINE_VISIBILITY
  2775. basic_string<_CharT, _Traits, _Allocator>&
  2776. basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
  2777. {
  2778. return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
  2779. }
  2780. // erase
  2781. template <class _CharT, class _Traits, class _Allocator>
  2782. basic_string<_CharT, _Traits, _Allocator>&
  2783. basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
  2784. {
  2785. size_type __sz = size();
  2786. if (__pos > __sz)
  2787. this->__throw_out_of_range();
  2788. if (__n)
  2789. {
  2790. value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
  2791. __n = _VSTD::min(__n, __sz - __pos);
  2792. size_type __n_move = __sz - __pos - __n;
  2793. if (__n_move != 0)
  2794. traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
  2795. __sz -= __n;
  2796. __set_size(__sz);
  2797. __invalidate_iterators_past(__sz);
  2798. traits_type::assign(__p[__sz], value_type());
  2799. }
  2800. return *this;
  2801. }
  2802. template <class _CharT, class _Traits, class _Allocator>
  2803. inline _LIBCPP_INLINE_VISIBILITY
  2804. typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2805. basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
  2806. {
  2807. #if _LIBCPP_DEBUG_LEVEL >= 2
  2808. _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
  2809. "string::erase(iterator) called with an iterator not"
  2810. " referring to this string");
  2811. #endif
  2812. _LIBCPP_ASSERT(__pos != end(),
  2813. "string::erase(iterator) called with a non-dereferenceable iterator");
  2814. iterator __b = begin();
  2815. size_type __r = static_cast<size_type>(__pos - __b);
  2816. erase(__r, 1);
  2817. return __b + static_cast<difference_type>(__r);
  2818. }
  2819. template <class _CharT, class _Traits, class _Allocator>
  2820. inline _LIBCPP_INLINE_VISIBILITY
  2821. typename basic_string<_CharT, _Traits, _Allocator>::iterator
  2822. basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
  2823. {
  2824. #if _LIBCPP_DEBUG_LEVEL >= 2
  2825. _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
  2826. "string::erase(iterator, iterator) called with an iterator not"
  2827. " referring to this string");
  2828. #endif
  2829. _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
  2830. iterator __b = begin();
  2831. size_type __r = static_cast<size_type>(__first - __b);
  2832. erase(__r, static_cast<size_type>(__last - __first));
  2833. return __b + static_cast<difference_type>(__r);
  2834. }
  2835. template <class _CharT, class _Traits, class _Allocator>
  2836. inline _LIBCPP_INLINE_VISIBILITY
  2837. void
  2838. basic_string<_CharT, _Traits, _Allocator>::pop_back()
  2839. {
  2840. _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
  2841. size_type __sz;
  2842. if (__is_long())
  2843. {
  2844. __sz = __get_long_size() - 1;
  2845. __set_long_size(__sz);
  2846. traits_type::assign(*(__get_long_pointer() + __sz), value_type());
  2847. }
  2848. else
  2849. {
  2850. __sz = __get_short_size() - 1;
  2851. __set_short_size(__sz);
  2852. traits_type::assign(*(__get_short_pointer() + __sz), value_type());
  2853. }
  2854. __invalidate_iterators_past(__sz);
  2855. }
  2856. template <class _CharT, class _Traits, class _Allocator>
  2857. inline _LIBCPP_INLINE_VISIBILITY
  2858. void
  2859. basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
  2860. {
  2861. __invalidate_all_iterators();
  2862. if (__is_long())
  2863. {
  2864. traits_type::assign(*__get_long_pointer(), value_type());
  2865. __set_long_size(0);
  2866. }
  2867. else
  2868. {
  2869. traits_type::assign(*__get_short_pointer(), value_type());
  2870. __set_short_size(0);
  2871. }
  2872. }
  2873. template <class _CharT, class _Traits, class _Allocator>
  2874. inline _LIBCPP_INLINE_VISIBILITY
  2875. void
  2876. basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
  2877. {
  2878. if (__is_long())
  2879. {
  2880. traits_type::assign(*(__get_long_pointer() + __pos), value_type());
  2881. __set_long_size(__pos);
  2882. }
  2883. else
  2884. {
  2885. traits_type::assign(*(__get_short_pointer() + __pos), value_type());
  2886. __set_short_size(__pos);
  2887. }
  2888. __invalidate_iterators_past(__pos);
  2889. }
  2890. template <class _CharT, class _Traits, class _Allocator>
  2891. void
  2892. basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
  2893. {
  2894. size_type __sz = size();
  2895. if (__n > __sz)
  2896. append(__n - __sz, __c);
  2897. else
  2898. __erase_to_end(__n);
  2899. }
  2900. template <class _CharT, class _Traits, class _Allocator>
  2901. inline _LIBCPP_INLINE_VISIBILITY
  2902. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  2903. basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
  2904. {
  2905. size_type __m = __alloc_traits::max_size(__alloc());
  2906. #if _LIBCPP_BIG_ENDIAN
  2907. return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
  2908. #else
  2909. return __m - __alignment;
  2910. #endif
  2911. }
  2912. template <class _CharT, class _Traits, class _Allocator>
  2913. void
  2914. basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
  2915. {
  2916. if (__res_arg > max_size())
  2917. this->__throw_length_error();
  2918. size_type __cap = capacity();
  2919. size_type __sz = size();
  2920. __res_arg = _VSTD::max(__res_arg, __sz);
  2921. __res_arg = __recommend(__res_arg);
  2922. if (__res_arg != __cap)
  2923. {
  2924. pointer __new_data, __p;
  2925. bool __was_long, __now_long;
  2926. if (__res_arg == __min_cap - 1)
  2927. {
  2928. __was_long = true;
  2929. __now_long = false;
  2930. __new_data = __get_short_pointer();
  2931. __p = __get_long_pointer();
  2932. }
  2933. else
  2934. {
  2935. if (__res_arg > __cap)
  2936. __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
  2937. else
  2938. {
  2939. #ifndef _LIBCPP_NO_EXCEPTIONS
  2940. try
  2941. {
  2942. #endif // _LIBCPP_NO_EXCEPTIONS
  2943. __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
  2944. #ifndef _LIBCPP_NO_EXCEPTIONS
  2945. }
  2946. catch (...)
  2947. {
  2948. return;
  2949. }
  2950. #else // _LIBCPP_NO_EXCEPTIONS
  2951. if (__new_data == nullptr)
  2952. return;
  2953. #endif // _LIBCPP_NO_EXCEPTIONS
  2954. }
  2955. __now_long = true;
  2956. __was_long = __is_long();
  2957. __p = __get_pointer();
  2958. }
  2959. traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
  2960. _VSTD::__to_raw_pointer(__p), size()+1);
  2961. if (__was_long)
  2962. __alloc_traits::deallocate(__alloc(), __p, __cap+1);
  2963. if (__now_long)
  2964. {
  2965. __set_long_cap(__res_arg+1);
  2966. __set_long_size(__sz);
  2967. __set_long_pointer(__new_data);
  2968. }
  2969. else
  2970. __set_short_size(__sz);
  2971. __invalidate_all_iterators();
  2972. }
  2973. }
  2974. template <class _CharT, class _Traits, class _Allocator>
  2975. inline _LIBCPP_INLINE_VISIBILITY
  2976. typename basic_string<_CharT, _Traits, _Allocator>::const_reference
  2977. basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const
  2978. {
  2979. _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
  2980. return *(data() + __pos);
  2981. }
  2982. template <class _CharT, class _Traits, class _Allocator>
  2983. inline _LIBCPP_INLINE_VISIBILITY
  2984. typename basic_string<_CharT, _Traits, _Allocator>::reference
  2985. basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos)
  2986. {
  2987. _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
  2988. return *(__get_pointer() + __pos);
  2989. }
  2990. template <class _CharT, class _Traits, class _Allocator>
  2991. typename basic_string<_CharT, _Traits, _Allocator>::const_reference
  2992. basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
  2993. {
  2994. if (__n >= size())
  2995. this->__throw_out_of_range();
  2996. return (*this)[__n];
  2997. }
  2998. template <class _CharT, class _Traits, class _Allocator>
  2999. typename basic_string<_CharT, _Traits, _Allocator>::reference
  3000. basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
  3001. {
  3002. if (__n >= size())
  3003. this->__throw_out_of_range();
  3004. return (*this)[__n];
  3005. }
  3006. template <class _CharT, class _Traits, class _Allocator>
  3007. inline _LIBCPP_INLINE_VISIBILITY
  3008. typename basic_string<_CharT, _Traits, _Allocator>::reference
  3009. basic_string<_CharT, _Traits, _Allocator>::front()
  3010. {
  3011. _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
  3012. return *__get_pointer();
  3013. }
  3014. template <class _CharT, class _Traits, class _Allocator>
  3015. inline _LIBCPP_INLINE_VISIBILITY
  3016. typename basic_string<_CharT, _Traits, _Allocator>::const_reference
  3017. basic_string<_CharT, _Traits, _Allocator>::front() const
  3018. {
  3019. _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
  3020. return *data();
  3021. }
  3022. template <class _CharT, class _Traits, class _Allocator>
  3023. inline _LIBCPP_INLINE_VISIBILITY
  3024. typename basic_string<_CharT, _Traits, _Allocator>::reference
  3025. basic_string<_CharT, _Traits, _Allocator>::back()
  3026. {
  3027. _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
  3028. return *(__get_pointer() + size() - 1);
  3029. }
  3030. template <class _CharT, class _Traits, class _Allocator>
  3031. inline _LIBCPP_INLINE_VISIBILITY
  3032. typename basic_string<_CharT, _Traits, _Allocator>::const_reference
  3033. basic_string<_CharT, _Traits, _Allocator>::back() const
  3034. {
  3035. _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
  3036. return *(data() + size() - 1);
  3037. }
  3038. template <class _CharT, class _Traits, class _Allocator>
  3039. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3040. basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
  3041. {
  3042. size_type __sz = size();
  3043. if (__pos > __sz)
  3044. this->__throw_out_of_range();
  3045. size_type __rlen = _VSTD::min(__n, __sz - __pos);
  3046. traits_type::copy(__s, data() + __pos, __rlen);
  3047. return __rlen;
  3048. }
  3049. template <class _CharT, class _Traits, class _Allocator>
  3050. inline _LIBCPP_INLINE_VISIBILITY
  3051. basic_string<_CharT, _Traits, _Allocator>
  3052. basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
  3053. {
  3054. return basic_string(*this, __pos, __n, __alloc());
  3055. }
  3056. template <class _CharT, class _Traits, class _Allocator>
  3057. inline _LIBCPP_INLINE_VISIBILITY
  3058. void
  3059. basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
  3060. #if _LIBCPP_STD_VER >= 14
  3061. _NOEXCEPT
  3062. #else
  3063. _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
  3064. __is_nothrow_swappable<allocator_type>::value)
  3065. #endif
  3066. {
  3067. #if _LIBCPP_DEBUG_LEVEL >= 2
  3068. if (!__is_long())
  3069. __get_db()->__invalidate_all(this);
  3070. if (!__str.__is_long())
  3071. __get_db()->__invalidate_all(&__str);
  3072. __get_db()->swap(this, &__str);
  3073. #endif
  3074. _VSTD::swap(__r_.first(), __str.__r_.first());
  3075. __swap_allocator(__alloc(), __str.__alloc());
  3076. }
  3077. // find
  3078. template <class _Traits>
  3079. struct _LIBCPP_HIDDEN __traits_eq
  3080. {
  3081. typedef typename _Traits::char_type char_type;
  3082. _LIBCPP_INLINE_VISIBILITY
  3083. bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
  3084. {return _Traits::eq(__x, __y);}
  3085. };
  3086. template<class _CharT, class _Traits, class _Allocator>
  3087. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3088. basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
  3089. size_type __pos,
  3090. size_type __n) const _NOEXCEPT
  3091. {
  3092. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
  3093. return _VSTD::__str_find<value_type, size_type, traits_type, npos>
  3094. (data(), size(), __s, __pos, __n);
  3095. }
  3096. template<class _CharT, class _Traits, class _Allocator>
  3097. inline _LIBCPP_INLINE_VISIBILITY
  3098. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3099. basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
  3100. size_type __pos) const _NOEXCEPT
  3101. {
  3102. return _VSTD::__str_find<value_type, size_type, traits_type, npos>
  3103. (data(), size(), __str.data(), __pos, __str.size());
  3104. }
  3105. template<class _CharT, class _Traits, class _Allocator>
  3106. inline _LIBCPP_INLINE_VISIBILITY
  3107. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3108. basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
  3109. size_type __pos) const _NOEXCEPT
  3110. {
  3111. _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
  3112. return _VSTD::__str_find<value_type, size_type, traits_type, npos>
  3113. (data(), size(), __s, __pos, traits_type::length(__s));
  3114. }
  3115. template<class _CharT, class _Traits, class _Allocator>
  3116. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3117. basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
  3118. size_type __pos) const _NOEXCEPT
  3119. {
  3120. return _VSTD::__str_find<value_type, size_type, traits_type, npos>
  3121. (data(), size(), __c, __pos);
  3122. }
  3123. // rfind
  3124. template<class _CharT, class _Traits, class _Allocator>
  3125. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3126. basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
  3127. size_type __pos,
  3128. size_type __n) const _NOEXCEPT
  3129. {
  3130. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
  3131. return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
  3132. (data(), size(), __s, __pos, __n);
  3133. }
  3134. template<class _CharT, class _Traits, class _Allocator>
  3135. inline _LIBCPP_INLINE_VISIBILITY
  3136. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3137. basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
  3138. size_type __pos) const _NOEXCEPT
  3139. {
  3140. return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
  3141. (data(), size(), __str.data(), __pos, __str.size());
  3142. }
  3143. template<class _CharT, class _Traits, class _Allocator>
  3144. inline _LIBCPP_INLINE_VISIBILITY
  3145. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3146. basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
  3147. size_type __pos) const _NOEXCEPT
  3148. {
  3149. _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
  3150. return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
  3151. (data(), size(), __s, __pos, traits_type::length(__s));
  3152. }
  3153. template<class _CharT, class _Traits, class _Allocator>
  3154. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3155. basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
  3156. size_type __pos) const _NOEXCEPT
  3157. {
  3158. return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
  3159. (data(), size(), __c, __pos);
  3160. }
  3161. // find_first_of
  3162. template<class _CharT, class _Traits, class _Allocator>
  3163. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3164. basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
  3165. size_type __pos,
  3166. size_type __n) const _NOEXCEPT
  3167. {
  3168. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
  3169. return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
  3170. (data(), size(), __s, __pos, __n);
  3171. }
  3172. template<class _CharT, class _Traits, class _Allocator>
  3173. inline _LIBCPP_INLINE_VISIBILITY
  3174. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3175. basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
  3176. size_type __pos) const _NOEXCEPT
  3177. {
  3178. return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
  3179. (data(), size(), __str.data(), __pos, __str.size());
  3180. }
  3181. template<class _CharT, class _Traits, class _Allocator>
  3182. inline _LIBCPP_INLINE_VISIBILITY
  3183. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3184. basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
  3185. size_type __pos) const _NOEXCEPT
  3186. {
  3187. _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
  3188. return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
  3189. (data(), size(), __s, __pos, traits_type::length(__s));
  3190. }
  3191. template<class _CharT, class _Traits, class _Allocator>
  3192. inline _LIBCPP_INLINE_VISIBILITY
  3193. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3194. basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
  3195. size_type __pos) const _NOEXCEPT
  3196. {
  3197. return find(__c, __pos);
  3198. }
  3199. // find_last_of
  3200. template<class _CharT, class _Traits, class _Allocator>
  3201. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3202. basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
  3203. size_type __pos,
  3204. size_type __n) const _NOEXCEPT
  3205. {
  3206. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
  3207. return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
  3208. (data(), size(), __s, __pos, __n);
  3209. }
  3210. template<class _CharT, class _Traits, class _Allocator>
  3211. inline _LIBCPP_INLINE_VISIBILITY
  3212. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3213. basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
  3214. size_type __pos) const _NOEXCEPT
  3215. {
  3216. return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
  3217. (data(), size(), __str.data(), __pos, __str.size());
  3218. }
  3219. template<class _CharT, class _Traits, class _Allocator>
  3220. inline _LIBCPP_INLINE_VISIBILITY
  3221. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3222. basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
  3223. size_type __pos) const _NOEXCEPT
  3224. {
  3225. _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
  3226. return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
  3227. (data(), size(), __s, __pos, traits_type::length(__s));
  3228. }
  3229. template<class _CharT, class _Traits, class _Allocator>
  3230. inline _LIBCPP_INLINE_VISIBILITY
  3231. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3232. basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
  3233. size_type __pos) const _NOEXCEPT
  3234. {
  3235. return rfind(__c, __pos);
  3236. }
  3237. // find_first_not_of
  3238. template<class _CharT, class _Traits, class _Allocator>
  3239. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3240. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
  3241. size_type __pos,
  3242. size_type __n) const _NOEXCEPT
  3243. {
  3244. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
  3245. return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  3246. (data(), size(), __s, __pos, __n);
  3247. }
  3248. template<class _CharT, class _Traits, class _Allocator>
  3249. inline _LIBCPP_INLINE_VISIBILITY
  3250. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3251. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
  3252. size_type __pos) const _NOEXCEPT
  3253. {
  3254. return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  3255. (data(), size(), __str.data(), __pos, __str.size());
  3256. }
  3257. template<class _CharT, class _Traits, class _Allocator>
  3258. inline _LIBCPP_INLINE_VISIBILITY
  3259. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3260. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
  3261. size_type __pos) const _NOEXCEPT
  3262. {
  3263. _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
  3264. return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  3265. (data(), size(), __s, __pos, traits_type::length(__s));
  3266. }
  3267. template<class _CharT, class _Traits, class _Allocator>
  3268. inline _LIBCPP_INLINE_VISIBILITY
  3269. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3270. basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
  3271. size_type __pos) const _NOEXCEPT
  3272. {
  3273. return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
  3274. (data(), size(), __c, __pos);
  3275. }
  3276. // find_last_not_of
  3277. template<class _CharT, class _Traits, class _Allocator>
  3278. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3279. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
  3280. size_type __pos,
  3281. size_type __n) const _NOEXCEPT
  3282. {
  3283. _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
  3284. return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  3285. (data(), size(), __s, __pos, __n);
  3286. }
  3287. template<class _CharT, class _Traits, class _Allocator>
  3288. inline _LIBCPP_INLINE_VISIBILITY
  3289. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3290. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
  3291. size_type __pos) const _NOEXCEPT
  3292. {
  3293. return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  3294. (data(), size(), __str.data(), __pos, __str.size());
  3295. }
  3296. template<class _CharT, class _Traits, class _Allocator>
  3297. inline _LIBCPP_INLINE_VISIBILITY
  3298. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3299. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
  3300. size_type __pos) const _NOEXCEPT
  3301. {
  3302. _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
  3303. return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  3304. (data(), size(), __s, __pos, traits_type::length(__s));
  3305. }
  3306. template<class _CharT, class _Traits, class _Allocator>
  3307. inline _LIBCPP_INLINE_VISIBILITY
  3308. typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3309. basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
  3310. size_type __pos) const _NOEXCEPT
  3311. {
  3312. return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
  3313. (data(), size(), __c, __pos);
  3314. }
  3315. // compare
  3316. template <class _CharT, class _Traits, class _Allocator>
  3317. inline _LIBCPP_INLINE_VISIBILITY
  3318. int
  3319. basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
  3320. {
  3321. size_t __lhs_sz = size();
  3322. size_t __rhs_sz = __str.size();
  3323. int __result = traits_type::compare(data(), __str.data(),
  3324. _VSTD::min(__lhs_sz, __rhs_sz));
  3325. if (__result != 0)
  3326. return __result;
  3327. if (__lhs_sz < __rhs_sz)
  3328. return -1;
  3329. if (__lhs_sz > __rhs_sz)
  3330. return 1;
  3331. return 0;
  3332. }
  3333. template <class _CharT, class _Traits, class _Allocator>
  3334. inline _LIBCPP_INLINE_VISIBILITY
  3335. int
  3336. basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
  3337. size_type __n1,
  3338. const basic_string& __str) const
  3339. {
  3340. return compare(__pos1, __n1, __str.data(), __str.size());
  3341. }
  3342. template <class _CharT, class _Traits, class _Allocator>
  3343. int
  3344. basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
  3345. size_type __n1,
  3346. const basic_string& __str,
  3347. size_type __pos2,
  3348. size_type __n2) const
  3349. {
  3350. size_type __sz = __str.size();
  3351. if (__pos2 > __sz)
  3352. this->__throw_out_of_range();
  3353. return compare(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2,
  3354. __sz - __pos2));
  3355. }
  3356. template <class _CharT, class _Traits, class _Allocator>
  3357. int
  3358. basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
  3359. {
  3360. _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
  3361. return compare(0, npos, __s, traits_type::length(__s));
  3362. }
  3363. template <class _CharT, class _Traits, class _Allocator>
  3364. int
  3365. basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
  3366. size_type __n1,
  3367. const value_type* __s) const
  3368. {
  3369. _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
  3370. return compare(__pos1, __n1, __s, traits_type::length(__s));
  3371. }
  3372. template <class _CharT, class _Traits, class _Allocator>
  3373. int
  3374. basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
  3375. size_type __n1,
  3376. const value_type* __s,
  3377. size_type __n2) const
  3378. {
  3379. _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
  3380. size_type __sz = size();
  3381. if (__pos1 > __sz || __n2 == npos)
  3382. this->__throw_out_of_range();
  3383. size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
  3384. int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
  3385. if (__r == 0)
  3386. {
  3387. if (__rlen < __n2)
  3388. __r = -1;
  3389. else if (__rlen > __n2)
  3390. __r = 1;
  3391. }
  3392. return __r;
  3393. }
  3394. // __invariants
  3395. template<class _CharT, class _Traits, class _Allocator>
  3396. inline _LIBCPP_INLINE_VISIBILITY
  3397. bool
  3398. basic_string<_CharT, _Traits, _Allocator>::__invariants() const
  3399. {
  3400. if (size() > capacity())
  3401. return false;
  3402. if (capacity() < __min_cap - 1)
  3403. return false;
  3404. if (data() == 0)
  3405. return false;
  3406. if (data()[size()] != value_type(0))
  3407. return false;
  3408. return true;
  3409. }
  3410. // operator==
  3411. template<class _CharT, class _Traits, class _Allocator>
  3412. inline _LIBCPP_INLINE_VISIBILITY
  3413. bool
  3414. operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3415. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3416. {
  3417. size_t __lhs_sz = __lhs.size();
  3418. return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
  3419. __rhs.data(),
  3420. __lhs_sz) == 0;
  3421. }
  3422. template<class _Allocator>
  3423. inline _LIBCPP_INLINE_VISIBILITY
  3424. bool
  3425. operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
  3426. const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
  3427. {
  3428. size_t __lhs_sz = __lhs.size();
  3429. if (__lhs_sz != __rhs.size())
  3430. return false;
  3431. const char* __lp = __lhs.data();
  3432. const char* __rp = __rhs.data();
  3433. if (__lhs.__is_long())
  3434. return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
  3435. for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
  3436. if (*__lp != *__rp)
  3437. return false;
  3438. return true;
  3439. }
  3440. template<class _CharT, class _Traits, class _Allocator>
  3441. inline _LIBCPP_INLINE_VISIBILITY
  3442. bool
  3443. operator==(const _CharT* __lhs,
  3444. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3445. {
  3446. typedef basic_string<_CharT, _Traits, _Allocator> _String;
  3447. _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
  3448. size_t __lhs_len = _Traits::length(__lhs);
  3449. if (__lhs_len != __rhs.size()) return false;
  3450. return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
  3451. }
  3452. template<class _CharT, class _Traits, class _Allocator>
  3453. inline _LIBCPP_INLINE_VISIBILITY
  3454. bool
  3455. operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  3456. const _CharT* __rhs) _NOEXCEPT
  3457. {
  3458. typedef basic_string<_CharT, _Traits, _Allocator> _String;
  3459. _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
  3460. size_t __rhs_len = _Traits::length(__rhs);
  3461. if (__rhs_len != __lhs.size()) return false;
  3462. return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
  3463. }
  3464. // operator!=
  3465. template<class _CharT, class _Traits, class _Allocator>
  3466. inline _LIBCPP_INLINE_VISIBILITY
  3467. bool
  3468. operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  3469. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3470. {
  3471. return !(__lhs == __rhs);
  3472. }
  3473. template<class _CharT, class _Traits, class _Allocator>
  3474. inline _LIBCPP_INLINE_VISIBILITY
  3475. bool
  3476. operator!=(const _CharT* __lhs,
  3477. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3478. {
  3479. return !(__lhs == __rhs);
  3480. }
  3481. template<class _CharT, class _Traits, class _Allocator>
  3482. inline _LIBCPP_INLINE_VISIBILITY
  3483. bool
  3484. operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3485. const _CharT* __rhs) _NOEXCEPT
  3486. {
  3487. return !(__lhs == __rhs);
  3488. }
  3489. // operator<
  3490. template<class _CharT, class _Traits, class _Allocator>
  3491. inline _LIBCPP_INLINE_VISIBILITY
  3492. bool
  3493. operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3494. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3495. {
  3496. return __lhs.compare(__rhs) < 0;
  3497. }
  3498. template<class _CharT, class _Traits, class _Allocator>
  3499. inline _LIBCPP_INLINE_VISIBILITY
  3500. bool
  3501. operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3502. const _CharT* __rhs) _NOEXCEPT
  3503. {
  3504. return __lhs.compare(__rhs) < 0;
  3505. }
  3506. template<class _CharT, class _Traits, class _Allocator>
  3507. inline _LIBCPP_INLINE_VISIBILITY
  3508. bool
  3509. operator< (const _CharT* __lhs,
  3510. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3511. {
  3512. return __rhs.compare(__lhs) > 0;
  3513. }
  3514. // operator>
  3515. template<class _CharT, class _Traits, class _Allocator>
  3516. inline _LIBCPP_INLINE_VISIBILITY
  3517. bool
  3518. operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3519. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3520. {
  3521. return __rhs < __lhs;
  3522. }
  3523. template<class _CharT, class _Traits, class _Allocator>
  3524. inline _LIBCPP_INLINE_VISIBILITY
  3525. bool
  3526. operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3527. const _CharT* __rhs) _NOEXCEPT
  3528. {
  3529. return __rhs < __lhs;
  3530. }
  3531. template<class _CharT, class _Traits, class _Allocator>
  3532. inline _LIBCPP_INLINE_VISIBILITY
  3533. bool
  3534. operator> (const _CharT* __lhs,
  3535. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3536. {
  3537. return __rhs < __lhs;
  3538. }
  3539. // operator<=
  3540. template<class _CharT, class _Traits, class _Allocator>
  3541. inline _LIBCPP_INLINE_VISIBILITY
  3542. bool
  3543. operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3544. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3545. {
  3546. return !(__rhs < __lhs);
  3547. }
  3548. template<class _CharT, class _Traits, class _Allocator>
  3549. inline _LIBCPP_INLINE_VISIBILITY
  3550. bool
  3551. operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3552. const _CharT* __rhs) _NOEXCEPT
  3553. {
  3554. return !(__rhs < __lhs);
  3555. }
  3556. template<class _CharT, class _Traits, class _Allocator>
  3557. inline _LIBCPP_INLINE_VISIBILITY
  3558. bool
  3559. operator<=(const _CharT* __lhs,
  3560. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3561. {
  3562. return !(__rhs < __lhs);
  3563. }
  3564. // operator>=
  3565. template<class _CharT, class _Traits, class _Allocator>
  3566. inline _LIBCPP_INLINE_VISIBILITY
  3567. bool
  3568. operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3569. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3570. {
  3571. return !(__lhs < __rhs);
  3572. }
  3573. template<class _CharT, class _Traits, class _Allocator>
  3574. inline _LIBCPP_INLINE_VISIBILITY
  3575. bool
  3576. operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3577. const _CharT* __rhs) _NOEXCEPT
  3578. {
  3579. return !(__lhs < __rhs);
  3580. }
  3581. template<class _CharT, class _Traits, class _Allocator>
  3582. inline _LIBCPP_INLINE_VISIBILITY
  3583. bool
  3584. operator>=(const _CharT* __lhs,
  3585. const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
  3586. {
  3587. return !(__lhs < __rhs);
  3588. }
  3589. // operator +
  3590. template<class _CharT, class _Traits, class _Allocator>
  3591. basic_string<_CharT, _Traits, _Allocator>
  3592. operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3593. const basic_string<_CharT, _Traits, _Allocator>& __rhs)
  3594. {
  3595. basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
  3596. typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
  3597. typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
  3598. __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
  3599. __r.append(__rhs.data(), __rhs_sz);
  3600. return __r;
  3601. }
  3602. template<class _CharT, class _Traits, class _Allocator>
  3603. basic_string<_CharT, _Traits, _Allocator>
  3604. operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  3605. {
  3606. basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
  3607. typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
  3608. typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
  3609. __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
  3610. __r.append(__rhs.data(), __rhs_sz);
  3611. return __r;
  3612. }
  3613. template<class _CharT, class _Traits, class _Allocator>
  3614. basic_string<_CharT, _Traits, _Allocator>
  3615. operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  3616. {
  3617. basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
  3618. typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
  3619. __r.__init(&__lhs, 1, 1 + __rhs_sz);
  3620. __r.append(__rhs.data(), __rhs_sz);
  3621. return __r;
  3622. }
  3623. template<class _CharT, class _Traits, class _Allocator>
  3624. basic_string<_CharT, _Traits, _Allocator>
  3625. operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
  3626. {
  3627. basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
  3628. typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
  3629. typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
  3630. __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
  3631. __r.append(__rhs, __rhs_sz);
  3632. return __r;
  3633. }
  3634. template<class _CharT, class _Traits, class _Allocator>
  3635. basic_string<_CharT, _Traits, _Allocator>
  3636. operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
  3637. {
  3638. basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
  3639. typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
  3640. __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
  3641. __r.push_back(__rhs);
  3642. return __r;
  3643. }
  3644. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3645. template<class _CharT, class _Traits, class _Allocator>
  3646. inline _LIBCPP_INLINE_VISIBILITY
  3647. basic_string<_CharT, _Traits, _Allocator>
  3648. operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
  3649. {
  3650. return _VSTD::move(__lhs.append(__rhs));
  3651. }
  3652. template<class _CharT, class _Traits, class _Allocator>
  3653. inline _LIBCPP_INLINE_VISIBILITY
  3654. basic_string<_CharT, _Traits, _Allocator>
  3655. operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
  3656. {
  3657. return _VSTD::move(__rhs.insert(0, __lhs));
  3658. }
  3659. template<class _CharT, class _Traits, class _Allocator>
  3660. inline _LIBCPP_INLINE_VISIBILITY
  3661. basic_string<_CharT, _Traits, _Allocator>
  3662. operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
  3663. {
  3664. return _VSTD::move(__lhs.append(__rhs));
  3665. }
  3666. template<class _CharT, class _Traits, class _Allocator>
  3667. inline _LIBCPP_INLINE_VISIBILITY
  3668. basic_string<_CharT, _Traits, _Allocator>
  3669. operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
  3670. {
  3671. return _VSTD::move(__rhs.insert(0, __lhs));
  3672. }
  3673. template<class _CharT, class _Traits, class _Allocator>
  3674. inline _LIBCPP_INLINE_VISIBILITY
  3675. basic_string<_CharT, _Traits, _Allocator>
  3676. operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
  3677. {
  3678. __rhs.insert(__rhs.begin(), __lhs);
  3679. return _VSTD::move(__rhs);
  3680. }
  3681. template<class _CharT, class _Traits, class _Allocator>
  3682. inline _LIBCPP_INLINE_VISIBILITY
  3683. basic_string<_CharT, _Traits, _Allocator>
  3684. operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
  3685. {
  3686. return _VSTD::move(__lhs.append(__rhs));
  3687. }
  3688. template<class _CharT, class _Traits, class _Allocator>
  3689. inline _LIBCPP_INLINE_VISIBILITY
  3690. basic_string<_CharT, _Traits, _Allocator>
  3691. operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
  3692. {
  3693. __lhs.push_back(__rhs);
  3694. return _VSTD::move(__lhs);
  3695. }
  3696. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3697. // swap
  3698. template<class _CharT, class _Traits, class _Allocator>
  3699. inline _LIBCPP_INLINE_VISIBILITY
  3700. void
  3701. swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
  3702. basic_string<_CharT, _Traits, _Allocator>& __rhs)
  3703. _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
  3704. {
  3705. __lhs.swap(__rhs);
  3706. }
  3707. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  3708. typedef basic_string<char16_t> u16string;
  3709. typedef basic_string<char32_t> u32string;
  3710. #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
  3711. _LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
  3712. _LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
  3713. _LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
  3714. _LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
  3715. _LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
  3716. _LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
  3717. _LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
  3718. _LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
  3719. _LIBCPP_FUNC_VIS string to_string(int __val);
  3720. _LIBCPP_FUNC_VIS string to_string(unsigned __val);
  3721. _LIBCPP_FUNC_VIS string to_string(long __val);
  3722. _LIBCPP_FUNC_VIS string to_string(unsigned long __val);
  3723. _LIBCPP_FUNC_VIS string to_string(long long __val);
  3724. _LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
  3725. _LIBCPP_FUNC_VIS string to_string(float __val);
  3726. _LIBCPP_FUNC_VIS string to_string(double __val);
  3727. _LIBCPP_FUNC_VIS string to_string(long double __val);
  3728. _LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
  3729. _LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
  3730. _LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
  3731. _LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
  3732. _LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
  3733. _LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
  3734. _LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
  3735. _LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
  3736. _LIBCPP_FUNC_VIS wstring to_wstring(int __val);
  3737. _LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
  3738. _LIBCPP_FUNC_VIS wstring to_wstring(long __val);
  3739. _LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
  3740. _LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
  3741. _LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
  3742. _LIBCPP_FUNC_VIS wstring to_wstring(float __val);
  3743. _LIBCPP_FUNC_VIS wstring to_wstring(double __val);
  3744. _LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
  3745. template<class _CharT, class _Traits, class _Allocator>
  3746. const typename basic_string<_CharT, _Traits, _Allocator>::size_type
  3747. basic_string<_CharT, _Traits, _Allocator>::npos;
  3748. template<class _CharT, class _Traits, class _Allocator>
  3749. struct _LIBCPP_TYPE_VIS_ONLY hash<basic_string<_CharT, _Traits, _Allocator> >
  3750. : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
  3751. {
  3752. size_t
  3753. operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
  3754. };
  3755. template<class _CharT, class _Traits, class _Allocator>
  3756. size_t
  3757. hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
  3758. const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
  3759. {
  3760. return __do_string_hash(__val.data(), __val.data() + __val.size());
  3761. }
  3762. #if !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  3763. template<class _CharT, class _Traits, class _Allocator>
  3764. basic_ostream<_CharT, _Traits>&
  3765. operator<<(basic_ostream<_CharT, _Traits>& __os,
  3766. const basic_string<_CharT, _Traits, _Allocator>& __str);
  3767. template<class _CharT, class _Traits, class _Allocator>
  3768. basic_istream<_CharT, _Traits>&
  3769. operator>>(basic_istream<_CharT, _Traits>& __is,
  3770. basic_string<_CharT, _Traits, _Allocator>& __str);
  3771. template<class _CharT, class _Traits, class _Allocator>
  3772. basic_istream<_CharT, _Traits>&
  3773. getline(basic_istream<_CharT, _Traits>& __is,
  3774. basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
  3775. template<class _CharT, class _Traits, class _Allocator>
  3776. inline _LIBCPP_INLINE_VISIBILITY
  3777. basic_istream<_CharT, _Traits>&
  3778. getline(basic_istream<_CharT, _Traits>& __is,
  3779. basic_string<_CharT, _Traits, _Allocator>& __str);
  3780. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3781. template<class _CharT, class _Traits, class _Allocator>
  3782. inline _LIBCPP_INLINE_VISIBILITY
  3783. basic_istream<_CharT, _Traits>&
  3784. getline(basic_istream<_CharT, _Traits>&& __is,
  3785. basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
  3786. template<class _CharT, class _Traits, class _Allocator>
  3787. inline _LIBCPP_INLINE_VISIBILITY
  3788. basic_istream<_CharT, _Traits>&
  3789. getline(basic_istream<_CharT, _Traits>&& __is,
  3790. basic_string<_CharT, _Traits, _Allocator>& __str);
  3791. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3792. #endif // !defined(_LIBCPP_SGX_NO_IOSTREAMS)
  3793. #if _LIBCPP_DEBUG_LEVEL >= 2
  3794. template<class _CharT, class _Traits, class _Allocator>
  3795. bool
  3796. basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
  3797. {
  3798. return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
  3799. _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
  3800. }
  3801. template<class _CharT, class _Traits, class _Allocator>
  3802. bool
  3803. basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
  3804. {
  3805. return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
  3806. _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
  3807. }
  3808. template<class _CharT, class _Traits, class _Allocator>
  3809. bool
  3810. basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
  3811. {
  3812. const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
  3813. return this->data() <= __p && __p <= this->data() + this->size();
  3814. }
  3815. template<class _CharT, class _Traits, class _Allocator>
  3816. bool
  3817. basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
  3818. {
  3819. const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
  3820. return this->data() <= __p && __p < this->data() + this->size();
  3821. }
  3822. #endif // _LIBCPP_DEBUG_LEVEL >= 2
  3823. #if _LIBCPP_STD_VER > 11
  3824. // Literal suffixes for basic_string [basic.string.literals]
  3825. inline namespace literals
  3826. {
  3827. inline namespace string_literals
  3828. {
  3829. inline _LIBCPP_INLINE_VISIBILITY
  3830. basic_string<char> operator "" s( const char *__str, size_t __len )
  3831. {
  3832. return basic_string<char> (__str, __len);
  3833. }
  3834. inline _LIBCPP_INLINE_VISIBILITY
  3835. basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
  3836. {
  3837. return basic_string<wchar_t> (__str, __len);
  3838. }
  3839. inline _LIBCPP_INLINE_VISIBILITY
  3840. basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
  3841. {
  3842. return basic_string<char16_t> (__str, __len);
  3843. }
  3844. inline _LIBCPP_INLINE_VISIBILITY
  3845. basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
  3846. {
  3847. return basic_string<char32_t> (__str, __len);
  3848. }
  3849. }
  3850. }
  3851. #endif
  3852. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_string<char>)
  3853. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_string<wchar_t>)
  3854. _LIBCPP_EXTERN_TEMPLATE(string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
  3855. _LIBCPP_END_NAMESPACE_STD
  3856. #endif // _LIBCPP_STRING