string 129 KB

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