locale 152 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_LOCALE
  10. #define _LIBCPP_LOCALE
  11. /*
  12. locale synopsis
  13. namespace std
  14. {
  15. class locale
  16. {
  17. public:
  18. // types:
  19. class facet;
  20. class id;
  21. typedef int category;
  22. static const category // values assigned here are for exposition only
  23. none = 0x000,
  24. collate = 0x010,
  25. ctype = 0x020,
  26. monetary = 0x040,
  27. numeric = 0x080,
  28. time = 0x100,
  29. messages = 0x200,
  30. all = collate | ctype | monetary | numeric | time | messages;
  31. // construct/copy/destroy:
  32. locale() noexcept;
  33. locale(const locale& other) noexcept;
  34. explicit locale(const char* std_name);
  35. explicit locale(const string& std_name);
  36. locale(const locale& other, const char* std_name, category);
  37. locale(const locale& other, const string& std_name, category);
  38. template <class Facet> locale(const locale& other, Facet* f);
  39. locale(const locale& other, const locale& one, category);
  40. ~locale(); // not virtual
  41. const locale& operator=(const locale& other) noexcept;
  42. template <class Facet> locale combine(const locale& other) const;
  43. // locale operations:
  44. basic_string<char> name() const;
  45. bool operator==(const locale& other) const;
  46. bool operator!=(const locale& other) const; // removed C++20
  47. template <class charT, class Traits, class Allocator>
  48. bool operator()(const basic_string<charT,Traits,Allocator>& s1,
  49. const basic_string<charT,Traits,Allocator>& s2) const;
  50. // global locale objects:
  51. static locale global(const locale&);
  52. static const locale& classic();
  53. };
  54. template <class Facet> const Facet& use_facet(const locale&);
  55. template <class Facet> bool has_facet(const locale&) noexcept;
  56. // 22.3.3, convenience interfaces:
  57. template <class charT> bool isspace (charT c, const locale& loc);
  58. template <class charT> bool isprint (charT c, const locale& loc);
  59. template <class charT> bool iscntrl (charT c, const locale& loc);
  60. template <class charT> bool isupper (charT c, const locale& loc);
  61. template <class charT> bool islower (charT c, const locale& loc);
  62. template <class charT> bool isalpha (charT c, const locale& loc);
  63. template <class charT> bool isdigit (charT c, const locale& loc);
  64. template <class charT> bool ispunct (charT c, const locale& loc);
  65. template <class charT> bool isxdigit(charT c, const locale& loc);
  66. template <class charT> bool isalnum (charT c, const locale& loc);
  67. template <class charT> bool isgraph (charT c, const locale& loc);
  68. template <class charT> charT toupper(charT c, const locale& loc);
  69. template <class charT> charT tolower(charT c, const locale& loc);
  70. template<class Codecvt, class Elem = wchar_t,
  71. class Wide_alloc = allocator<Elem>,
  72. class Byte_alloc = allocator<char>>
  73. class wstring_convert
  74. {
  75. public:
  76. typedef basic_string<char, char_traits<char>, Byte_alloc> byte_string;
  77. typedef basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
  78. typedef typename Codecvt::state_type state_type;
  79. typedef typename wide_string::traits_type::int_type int_type;
  80. wstring_convert(Codecvt* pcvt = new Codecvt); // before C++14
  81. explicit wstring_convert(Codecvt* pcvt = new Codecvt); // before C++20
  82. wstring_convert() : wstring_convert(new Codecvt) {} // C++20
  83. explicit wstring_convert(Codecvt* pcvt); // C++20
  84. wstring_convert(Codecvt* pcvt, state_type state);
  85. explicit wstring_convert(const byte_string& byte_err, // explicit in C++14
  86. const wide_string& wide_err = wide_string());
  87. wstring_convert(const wstring_convert&) = delete; // C++14
  88. wstring_convert & operator=(const wstring_convert &) = delete; // C++14
  89. ~wstring_convert();
  90. wide_string from_bytes(char byte);
  91. wide_string from_bytes(const char* ptr);
  92. wide_string from_bytes(const byte_string& str);
  93. wide_string from_bytes(const char* first, const char* last);
  94. byte_string to_bytes(Elem wchar);
  95. byte_string to_bytes(const Elem* wptr);
  96. byte_string to_bytes(const wide_string& wstr);
  97. byte_string to_bytes(const Elem* first, const Elem* last);
  98. size_t converted() const; // noexcept in C++14
  99. state_type state() const;
  100. };
  101. template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
  102. class wbuffer_convert
  103. : public basic_streambuf<Elem, Tr>
  104. {
  105. public:
  106. typedef typename Tr::state_type state_type;
  107. wbuffer_convert(streambuf* bytebuf = 0, Codecvt* pcvt = new Codecvt,
  108. state_type state = state_type()); // before C++14
  109. explicit wbuffer_convert(streambuf* bytebuf = nullptr, Codecvt* pcvt = new Codecvt,
  110. state_type state = state_type()); // before C++20
  111. wbuffer_convert() : wbuffer_convert(nullptr) {} // C++20
  112. explicit wbuffer_convert(streambuf* bytebuf, Codecvt* pcvt = new Codecvt,
  113. state_type state = state_type()); // C++20
  114. wbuffer_convert(const wbuffer_convert&) = delete; // C++14
  115. wbuffer_convert & operator=(const wbuffer_convert &) = delete; // C++14
  116. ~wbuffer_convert(); // C++14
  117. streambuf* rdbuf() const;
  118. streambuf* rdbuf(streambuf* bytebuf);
  119. state_type state() const;
  120. };
  121. // 22.4.1 and 22.4.1.3, ctype:
  122. class ctype_base;
  123. template <class charT> class ctype;
  124. template <> class ctype<char>; // specialization
  125. template <class charT> class ctype_byname;
  126. template <> class ctype_byname<char>; // specialization
  127. class codecvt_base;
  128. template <class internT, class externT, class stateT> class codecvt;
  129. template <class internT, class externT, class stateT> class codecvt_byname;
  130. // 22.4.2 and 22.4.3, numeric:
  131. template <class charT, class InputIterator> class num_get;
  132. template <class charT, class OutputIterator> class num_put;
  133. template <class charT> class numpunct;
  134. template <class charT> class numpunct_byname;
  135. // 22.4.4, col lation:
  136. template <class charT> class collate;
  137. template <class charT> class collate_byname;
  138. // 22.4.5, date and time:
  139. class time_base;
  140. template <class charT, class InputIterator> class time_get;
  141. template <class charT, class InputIterator> class time_get_byname;
  142. template <class charT, class OutputIterator> class time_put;
  143. template <class charT, class OutputIterator> class time_put_byname;
  144. // 22.4.6, money:
  145. class money_base;
  146. template <class charT, class InputIterator> class money_get;
  147. template <class charT, class OutputIterator> class money_put;
  148. template <class charT, bool Intl> class moneypunct;
  149. template <class charT, bool Intl> class moneypunct_byname;
  150. // 22.4.7, message retrieval:
  151. class messages_base;
  152. template <class charT> class messages;
  153. template <class charT> class messages_byname;
  154. } // std
  155. */
  156. #include <__algorithm/copy.h>
  157. #include <__algorithm/equal.h>
  158. #include <__algorithm/find.h>
  159. #include <__algorithm/max.h>
  160. #include <__algorithm/reverse.h>
  161. #include <__algorithm/unwrap_iter.h>
  162. #include <__assert> // all public C++ headers provide the assertion handler
  163. #include <__config>
  164. #include <__iterator/access.h>
  165. #include <__iterator/back_insert_iterator.h>
  166. #include <__iterator/istreambuf_iterator.h>
  167. #include <__iterator/ostreambuf_iterator.h>
  168. #include <__locale>
  169. #include <__memory/unique_ptr.h>
  170. #include <__type_traits/make_unsigned.h>
  171. #include <cerrno>
  172. #include <cstdio>
  173. #include <cstdlib>
  174. #include <ctime>
  175. #include <ios>
  176. #include <limits>
  177. #include <new>
  178. #include <streambuf>
  179. #include <version>
  180. // TODO: Fix __bsd_locale_defaults.h
  181. // NOLINTBEGIN(libcpp-robust-against-adl)
  182. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  183. // Most unix variants have catopen. These are the specific ones that don't.
  184. # if !defined(__BIONIC__) && !defined(_NEWLIB_VERSION) && !defined(__EMSCRIPTEN__)
  185. # define _LIBCPP_HAS_CATOPEN 1
  186. # include <nl_types.h>
  187. # endif
  188. #endif
  189. #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
  190. # include <__locale_dir/locale_base_api/bsd_locale_defaults.h>
  191. #else
  192. # include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h>
  193. #endif
  194. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  195. # pragma GCC system_header
  196. #endif
  197. _LIBCPP_PUSH_MACROS
  198. #include <__undef_macros>
  199. _LIBCPP_BEGIN_NAMESPACE_STD
  200. #if defined(__APPLE__) || defined(__FreeBSD__)
  201. # define _LIBCPP_GET_C_LOCALE 0
  202. #elif defined(__NetBSD__)
  203. # define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
  204. #else
  205. # define _LIBCPP_GET_C_LOCALE __cloc()
  206. // Get the C locale object
  207. _LIBCPP_EXPORTED_FROM_ABI locale_t __cloc();
  208. #define __cloc_defined
  209. #endif
  210. // __scan_keyword
  211. // Scans [__b, __e) until a match is found in the basic_strings range
  212. // [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
  213. // __b will be incremented (visibly), consuming CharT until a match is found
  214. // or proved to not exist. A keyword may be "", in which will match anything.
  215. // If one keyword is a prefix of another, and the next CharT in the input
  216. // might match another keyword, the algorithm will attempt to find the longest
  217. // matching keyword. If the longer matching keyword ends up not matching, then
  218. // no keyword match is found. If no keyword match is found, __ke is returned
  219. // and failbit is set in __err.
  220. // Else an iterator pointing to the matching keyword is found. If more than
  221. // one keyword matches, an iterator to the first matching keyword is returned.
  222. // If on exit __b == __e, eofbit is set in __err. If __case_sensitive is false,
  223. // __ct is used to force to lower case before comparing characters.
  224. // Examples:
  225. // Keywords: "a", "abb"
  226. // If the input is "a", the first keyword matches and eofbit is set.
  227. // If the input is "abc", no match is found and "ab" are consumed.
  228. template <class _InputIterator, class _ForwardIterator, class _Ctype>
  229. _LIBCPP_HIDE_FROM_ABI
  230. _ForwardIterator
  231. __scan_keyword(_InputIterator& __b, _InputIterator __e,
  232. _ForwardIterator __kb, _ForwardIterator __ke,
  233. const _Ctype& __ct, ios_base::iostate& __err,
  234. bool __case_sensitive = true)
  235. {
  236. typedef typename iterator_traits<_InputIterator>::value_type _CharT;
  237. size_t __nkw = static_cast<size_t>(_VSTD::distance(__kb, __ke));
  238. const unsigned char __doesnt_match = '\0';
  239. const unsigned char __might_match = '\1';
  240. const unsigned char __does_match = '\2';
  241. unsigned char __statbuf[100];
  242. unsigned char* __status = __statbuf;
  243. unique_ptr<unsigned char, void(*)(void*)> __stat_hold(nullptr, free);
  244. if (__nkw > sizeof(__statbuf))
  245. {
  246. __status = (unsigned char*)malloc(__nkw);
  247. if (__status == nullptr)
  248. __throw_bad_alloc();
  249. __stat_hold.reset(__status);
  250. }
  251. size_t __n_might_match = __nkw; // At this point, any keyword might match
  252. size_t __n_does_match = 0; // but none of them definitely do
  253. // Initialize all statuses to __might_match, except for "" keywords are __does_match
  254. unsigned char* __st = __status;
  255. for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
  256. {
  257. if (!__ky->empty())
  258. *__st = __might_match;
  259. else
  260. {
  261. *__st = __does_match;
  262. --__n_might_match;
  263. ++__n_does_match;
  264. }
  265. }
  266. // While there might be a match, test keywords against the next CharT
  267. for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
  268. {
  269. // Peek at the next CharT but don't consume it
  270. _CharT __c = *__b;
  271. if (!__case_sensitive)
  272. __c = __ct.toupper(__c);
  273. bool __consume = false;
  274. // For each keyword which might match, see if the __indx character is __c
  275. // If a match if found, consume __c
  276. // If a match is found, and that is the last character in the keyword,
  277. // then that keyword matches.
  278. // If the keyword doesn't match this character, then change the keyword
  279. // to doesn't match
  280. __st = __status;
  281. for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
  282. {
  283. if (*__st == __might_match)
  284. {
  285. _CharT __kc = (*__ky)[__indx];
  286. if (!__case_sensitive)
  287. __kc = __ct.toupper(__kc);
  288. if (__c == __kc)
  289. {
  290. __consume = true;
  291. if (__ky->size() == __indx+1)
  292. {
  293. *__st = __does_match;
  294. --__n_might_match;
  295. ++__n_does_match;
  296. }
  297. }
  298. else
  299. {
  300. *__st = __doesnt_match;
  301. --__n_might_match;
  302. }
  303. }
  304. }
  305. // consume if we matched a character
  306. if (__consume)
  307. {
  308. ++__b;
  309. // If we consumed a character and there might be a matched keyword that
  310. // was marked matched on a previous iteration, then such keywords
  311. // which are now marked as not matching.
  312. if (__n_might_match + __n_does_match > 1)
  313. {
  314. __st = __status;
  315. for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, (void) ++__st)
  316. {
  317. if (*__st == __does_match && __ky->size() != __indx+1)
  318. {
  319. *__st = __doesnt_match;
  320. --__n_does_match;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. // We've exited the loop because we hit eof and/or we have no more "might matches".
  327. if (__b == __e)
  328. __err |= ios_base::eofbit;
  329. // Return the first matching result
  330. for (__st = __status; __kb != __ke; ++__kb, (void) ++__st)
  331. if (*__st == __does_match)
  332. break;
  333. if (__kb == __ke)
  334. __err |= ios_base::failbit;
  335. return __kb;
  336. }
  337. struct _LIBCPP_EXPORTED_FROM_ABI __num_get_base
  338. {
  339. static const int __num_get_buf_sz = 40;
  340. static int __get_base(ios_base&);
  341. static const char __src[33];
  342. };
  343. _LIBCPP_EXPORTED_FROM_ABI void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
  344. ios_base::iostate& __err);
  345. template <class _CharT>
  346. struct __num_get
  347. : protected __num_get_base
  348. {
  349. static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
  350. _CharT& __thousands_sep);
  351. static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
  352. char* __a, char*& __a_end,
  353. _CharT __decimal_point, _CharT __thousands_sep,
  354. const string& __grouping, unsigned* __g,
  355. unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
  356. #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
  357. static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
  358. static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
  359. unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
  360. unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
  361. #else
  362. static string __stage2_int_prep(ios_base& __iob, _CharT& __thousands_sep)
  363. {
  364. locale __loc = __iob.getloc();
  365. const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
  366. __thousands_sep = __np.thousands_sep();
  367. return __np.grouping();
  368. }
  369. const _CharT* __do_widen(ios_base& __iob, _CharT* __atoms) const
  370. {
  371. return __do_widen_p(__iob, __atoms);
  372. }
  373. static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
  374. unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
  375. unsigned* __g, unsigned*& __g_end, const _CharT* __atoms);
  376. private:
  377. template<typename _Tp>
  378. const _Tp* __do_widen_p(ios_base& __iob, _Tp* __atoms) const
  379. {
  380. locale __loc = __iob.getloc();
  381. use_facet<ctype<_Tp> >(__loc).widen(__src, __src + 26, __atoms);
  382. return __atoms;
  383. }
  384. const char* __do_widen_p(ios_base& __iob, char* __atoms) const
  385. {
  386. (void)__iob;
  387. (void)__atoms;
  388. return __src;
  389. }
  390. #endif
  391. };
  392. #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
  393. template <class _CharT>
  394. string
  395. __num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
  396. {
  397. locale __loc = __iob.getloc();
  398. std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
  399. const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
  400. __thousands_sep = __np.thousands_sep();
  401. return __np.grouping();
  402. }
  403. #endif
  404. template <class _CharT>
  405. string
  406. __num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
  407. _CharT& __thousands_sep)
  408. {
  409. locale __loc = __iob.getloc();
  410. std::use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
  411. const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__loc);
  412. __decimal_point = __np.decimal_point();
  413. __thousands_sep = __np.thousands_sep();
  414. return __np.grouping();
  415. }
  416. template <class _CharT>
  417. int
  418. #ifndef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
  419. __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
  420. unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
  421. unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
  422. #else
  423. __num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
  424. unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
  425. unsigned* __g, unsigned*& __g_end, const _CharT* __atoms)
  426. #endif
  427. {
  428. if (__a_end == __a && (__ct == __atoms[24] || __ct == __atoms[25]))
  429. {
  430. *__a_end++ = __ct == __atoms[24] ? '+' : '-';
  431. __dc = 0;
  432. return 0;
  433. }
  434. if (__grouping.size() != 0 && __ct == __thousands_sep)
  435. {
  436. if (__g_end-__g < __num_get_buf_sz)
  437. {
  438. *__g_end++ = __dc;
  439. __dc = 0;
  440. }
  441. return 0;
  442. }
  443. ptrdiff_t __f = std::find(__atoms, __atoms + 26, __ct) - __atoms;
  444. if (__f >= 24)
  445. return -1;
  446. switch (__base)
  447. {
  448. case 8:
  449. case 10:
  450. if (__f >= __base)
  451. return -1;
  452. break;
  453. case 16:
  454. if (__f < 22)
  455. break;
  456. if (__a_end != __a && __a_end - __a <= 2 && __a_end[-1] == '0')
  457. {
  458. __dc = 0;
  459. *__a_end++ = __src[__f];
  460. return 0;
  461. }
  462. return -1;
  463. }
  464. *__a_end++ = __src[__f];
  465. ++__dc;
  466. return 0;
  467. }
  468. template <class _CharT>
  469. int
  470. __num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
  471. _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
  472. unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
  473. {
  474. if (__ct == __decimal_point)
  475. {
  476. if (!__in_units)
  477. return -1;
  478. __in_units = false;
  479. *__a_end++ = '.';
  480. if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
  481. *__g_end++ = __dc;
  482. return 0;
  483. }
  484. if (__ct == __thousands_sep && __grouping.size() != 0)
  485. {
  486. if (!__in_units)
  487. return -1;
  488. if (__g_end-__g < __num_get_buf_sz)
  489. {
  490. *__g_end++ = __dc;
  491. __dc = 0;
  492. }
  493. return 0;
  494. }
  495. ptrdiff_t __f = std::find(__atoms, __atoms + 32, __ct) - __atoms;
  496. if (__f >= 32)
  497. return -1;
  498. char __x = __src[__f];
  499. if (__x == '-' || __x == '+')
  500. {
  501. if (__a_end == __a || (std::toupper(__a_end[-1]) == std::toupper(__exp)))
  502. {
  503. *__a_end++ = __x;
  504. return 0;
  505. }
  506. return -1;
  507. }
  508. if (__x == 'x' || __x == 'X')
  509. __exp = 'P';
  510. else if (std::toupper(__x) == __exp)
  511. {
  512. __exp = std::tolower(__exp);
  513. if (__in_units)
  514. {
  515. __in_units = false;
  516. if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
  517. *__g_end++ = __dc;
  518. }
  519. }
  520. *__a_end++ = __x;
  521. if (__f >= 22)
  522. return 0;
  523. ++__dc;
  524. return 0;
  525. }
  526. extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<char>;
  527. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  528. extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_get<wchar_t>;
  529. #endif
  530. template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
  531. class _LIBCPP_TEMPLATE_VIS num_get
  532. : public locale::facet,
  533. private __num_get<_CharT>
  534. {
  535. public:
  536. typedef _CharT char_type;
  537. typedef _InputIterator iter_type;
  538. _LIBCPP_INLINE_VISIBILITY
  539. explicit num_get(size_t __refs = 0)
  540. : locale::facet(__refs) {}
  541. _LIBCPP_INLINE_VISIBILITY
  542. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  543. ios_base::iostate& __err, bool& __v) const
  544. {
  545. return do_get(__b, __e, __iob, __err, __v);
  546. }
  547. _LIBCPP_INLINE_VISIBILITY
  548. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  549. ios_base::iostate& __err, long& __v) const
  550. {
  551. return do_get(__b, __e, __iob, __err, __v);
  552. }
  553. _LIBCPP_INLINE_VISIBILITY
  554. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  555. ios_base::iostate& __err, long long& __v) const
  556. {
  557. return do_get(__b, __e, __iob, __err, __v);
  558. }
  559. _LIBCPP_INLINE_VISIBILITY
  560. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  561. ios_base::iostate& __err, unsigned short& __v) const
  562. {
  563. return do_get(__b, __e, __iob, __err, __v);
  564. }
  565. _LIBCPP_INLINE_VISIBILITY
  566. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  567. ios_base::iostate& __err, unsigned int& __v) const
  568. {
  569. return do_get(__b, __e, __iob, __err, __v);
  570. }
  571. _LIBCPP_INLINE_VISIBILITY
  572. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  573. ios_base::iostate& __err, unsigned long& __v) const
  574. {
  575. return do_get(__b, __e, __iob, __err, __v);
  576. }
  577. _LIBCPP_INLINE_VISIBILITY
  578. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  579. ios_base::iostate& __err, unsigned long long& __v) const
  580. {
  581. return do_get(__b, __e, __iob, __err, __v);
  582. }
  583. _LIBCPP_INLINE_VISIBILITY
  584. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  585. ios_base::iostate& __err, float& __v) const
  586. {
  587. return do_get(__b, __e, __iob, __err, __v);
  588. }
  589. _LIBCPP_INLINE_VISIBILITY
  590. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  591. ios_base::iostate& __err, double& __v) const
  592. {
  593. return do_get(__b, __e, __iob, __err, __v);
  594. }
  595. _LIBCPP_INLINE_VISIBILITY
  596. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  597. ios_base::iostate& __err, long double& __v) const
  598. {
  599. return do_get(__b, __e, __iob, __err, __v);
  600. }
  601. _LIBCPP_INLINE_VISIBILITY
  602. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  603. ios_base::iostate& __err, void*& __v) const
  604. {
  605. return do_get(__b, __e, __iob, __err, __v);
  606. }
  607. static locale::id id;
  608. protected:
  609. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_get() override {}
  610. template <class _Fp>
  611. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  612. iter_type __do_get_floating_point
  613. (iter_type __b, iter_type __e, ios_base& __iob,
  614. ios_base::iostate& __err, _Fp& __v) const;
  615. template <class _Signed>
  616. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  617. iter_type __do_get_signed
  618. (iter_type __b, iter_type __e, ios_base& __iob,
  619. ios_base::iostate& __err, _Signed& __v) const;
  620. template <class _Unsigned>
  621. _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
  622. iter_type __do_get_unsigned
  623. (iter_type __b, iter_type __e, ios_base& __iob,
  624. ios_base::iostate& __err, _Unsigned& __v) const;
  625. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  626. ios_base::iostate& __err, bool& __v) const;
  627. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  628. ios_base::iostate& __err, long& __v) const
  629. { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
  630. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  631. ios_base::iostate& __err, long long& __v) const
  632. { return this->__do_get_signed ( __b, __e, __iob, __err, __v ); }
  633. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  634. ios_base::iostate& __err, unsigned short& __v) const
  635. { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
  636. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  637. ios_base::iostate& __err, unsigned int& __v) const
  638. { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
  639. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  640. ios_base::iostate& __err, unsigned long& __v) const
  641. { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
  642. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  643. ios_base::iostate& __err, unsigned long long& __v) const
  644. { return this->__do_get_unsigned ( __b, __e, __iob, __err, __v ); }
  645. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  646. ios_base::iostate& __err, float& __v) const
  647. { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
  648. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  649. ios_base::iostate& __err, double& __v) const
  650. { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
  651. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  652. ios_base::iostate& __err, long double& __v) const
  653. { return this->__do_get_floating_point ( __b, __e, __iob, __err, __v ); }
  654. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  655. ios_base::iostate& __err, void*& __v) const;
  656. };
  657. template <class _CharT, class _InputIterator>
  658. locale::id
  659. num_get<_CharT, _InputIterator>::id;
  660. template <class _Tp>
  661. _LIBCPP_HIDE_FROM_ABI _Tp
  662. __num_get_signed_integral(const char* __a, const char* __a_end,
  663. ios_base::iostate& __err, int __base)
  664. {
  665. if (__a != __a_end)
  666. {
  667. __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
  668. errno = 0;
  669. char *__p2;
  670. long long __ll = strtoll_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
  671. __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
  672. if (__current_errno == 0)
  673. errno = __save_errno;
  674. if (__p2 != __a_end)
  675. {
  676. __err = ios_base::failbit;
  677. return 0;
  678. }
  679. else if (__current_errno == ERANGE ||
  680. __ll < numeric_limits<_Tp>::min() ||
  681. numeric_limits<_Tp>::max() < __ll)
  682. {
  683. __err = ios_base::failbit;
  684. if (__ll > 0)
  685. return numeric_limits<_Tp>::max();
  686. else
  687. return numeric_limits<_Tp>::min();
  688. }
  689. return static_cast<_Tp>(__ll);
  690. }
  691. __err = ios_base::failbit;
  692. return 0;
  693. }
  694. template <class _Tp>
  695. _LIBCPP_HIDE_FROM_ABI _Tp
  696. __num_get_unsigned_integral(const char* __a, const char* __a_end,
  697. ios_base::iostate& __err, int __base)
  698. {
  699. if (__a != __a_end)
  700. {
  701. const bool __negate = *__a == '-';
  702. if (__negate && ++__a == __a_end) {
  703. __err = ios_base::failbit;
  704. return 0;
  705. }
  706. __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
  707. errno = 0;
  708. char *__p2;
  709. unsigned long long __ll = strtoull_l(__a, &__p2, __base, _LIBCPP_GET_C_LOCALE);
  710. __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
  711. if (__current_errno == 0)
  712. errno = __save_errno;
  713. if (__p2 != __a_end)
  714. {
  715. __err = ios_base::failbit;
  716. return 0;
  717. }
  718. else if (__current_errno == ERANGE || numeric_limits<_Tp>::max() < __ll)
  719. {
  720. __err = ios_base::failbit;
  721. return numeric_limits<_Tp>::max();
  722. }
  723. _Tp __res = static_cast<_Tp>(__ll);
  724. if (__negate) __res = -__res;
  725. return __res;
  726. }
  727. __err = ios_base::failbit;
  728. return 0;
  729. }
  730. template <class _Tp>
  731. _LIBCPP_INLINE_VISIBILITY
  732. _Tp __do_strtod(const char* __a, char** __p2);
  733. template <>
  734. inline _LIBCPP_INLINE_VISIBILITY
  735. float __do_strtod<float>(const char* __a, char** __p2) {
  736. return strtof_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
  737. }
  738. template <>
  739. inline _LIBCPP_INLINE_VISIBILITY
  740. double __do_strtod<double>(const char* __a, char** __p2) {
  741. return strtod_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
  742. }
  743. template <>
  744. inline _LIBCPP_INLINE_VISIBILITY
  745. long double __do_strtod<long double>(const char* __a, char** __p2) {
  746. return strtold_l(__a, __p2, _LIBCPP_GET_C_LOCALE);
  747. }
  748. template <class _Tp>
  749. _LIBCPP_HIDE_FROM_ABI
  750. _Tp
  751. __num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
  752. {
  753. if (__a != __a_end)
  754. {
  755. __libcpp_remove_reference_t<decltype(errno)> __save_errno = errno;
  756. errno = 0;
  757. char *__p2;
  758. _Tp __ld = std::__do_strtod<_Tp>(__a, &__p2);
  759. __libcpp_remove_reference_t<decltype(errno)> __current_errno = errno;
  760. if (__current_errno == 0)
  761. errno = __save_errno;
  762. if (__p2 != __a_end)
  763. {
  764. __err = ios_base::failbit;
  765. return 0;
  766. }
  767. else if (__current_errno == ERANGE)
  768. __err = ios_base::failbit;
  769. return __ld;
  770. }
  771. __err = ios_base::failbit;
  772. return 0;
  773. }
  774. template <class _CharT, class _InputIterator>
  775. _InputIterator
  776. num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
  777. ios_base& __iob,
  778. ios_base::iostate& __err,
  779. bool& __v) const
  780. {
  781. if ((__iob.flags() & ios_base::boolalpha) == 0)
  782. {
  783. long __lv = -1;
  784. __b = do_get(__b, __e, __iob, __err, __lv);
  785. switch (__lv)
  786. {
  787. case 0:
  788. __v = false;
  789. break;
  790. case 1:
  791. __v = true;
  792. break;
  793. default:
  794. __v = true;
  795. __err = ios_base::failbit;
  796. break;
  797. }
  798. return __b;
  799. }
  800. const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__iob.getloc());
  801. const numpunct<_CharT>& __np = std::use_facet<numpunct<_CharT> >(__iob.getloc());
  802. typedef typename numpunct<_CharT>::string_type string_type;
  803. const string_type __names[2] = {__np.truename(), __np.falsename()};
  804. const string_type* __i = _VSTD::__scan_keyword(__b, __e, __names, __names+2,
  805. __ct, __err);
  806. __v = __i == __names;
  807. return __b;
  808. }
  809. // signed
  810. template <class _CharT, class _InputIterator>
  811. template <class _Signed>
  812. _InputIterator
  813. num_get<_CharT, _InputIterator>::__do_get_signed(iter_type __b, iter_type __e,
  814. ios_base& __iob,
  815. ios_base::iostate& __err,
  816. _Signed& __v) const
  817. {
  818. // Stage 1
  819. int __base = this->__get_base(__iob);
  820. // Stage 2
  821. char_type __thousands_sep;
  822. const int __atoms_size = 26;
  823. #ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
  824. char_type __atoms1[__atoms_size];
  825. const char_type *__atoms = this->__do_widen(__iob, __atoms1);
  826. string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
  827. #else
  828. char_type __atoms[__atoms_size];
  829. string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
  830. #endif
  831. string __buf;
  832. __buf.resize(__buf.capacity());
  833. char* __a = &__buf[0];
  834. char* __a_end = __a;
  835. unsigned __g[__num_get_base::__num_get_buf_sz];
  836. unsigned* __g_end = __g;
  837. unsigned __dc = 0;
  838. for (; __b != __e; ++__b)
  839. {
  840. if (__a_end == __a + __buf.size())
  841. {
  842. size_t __tmp = __buf.size();
  843. __buf.resize(2*__buf.size());
  844. __buf.resize(__buf.capacity());
  845. __a = &__buf[0];
  846. __a_end = __a + __tmp;
  847. }
  848. if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
  849. __thousands_sep, __grouping, __g, __g_end,
  850. __atoms))
  851. break;
  852. }
  853. if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
  854. *__g_end++ = __dc;
  855. // Stage 3
  856. __v = std::__num_get_signed_integral<_Signed>(__a, __a_end, __err, __base);
  857. // Digit grouping checked
  858. __check_grouping(__grouping, __g, __g_end, __err);
  859. // EOF checked
  860. if (__b == __e)
  861. __err |= ios_base::eofbit;
  862. return __b;
  863. }
  864. // unsigned
  865. template <class _CharT, class _InputIterator>
  866. template <class _Unsigned>
  867. _InputIterator
  868. num_get<_CharT, _InputIterator>::__do_get_unsigned(iter_type __b, iter_type __e,
  869. ios_base& __iob,
  870. ios_base::iostate& __err,
  871. _Unsigned& __v) const
  872. {
  873. // Stage 1
  874. int __base = this->__get_base(__iob);
  875. // Stage 2
  876. char_type __thousands_sep;
  877. const int __atoms_size = 26;
  878. #ifdef _LIBCPP_ABI_OPTIMIZED_LOCALE_NUM_GET
  879. char_type __atoms1[__atoms_size];
  880. const char_type *__atoms = this->__do_widen(__iob, __atoms1);
  881. string __grouping = this->__stage2_int_prep(__iob, __thousands_sep);
  882. #else
  883. char_type __atoms[__atoms_size];
  884. string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
  885. #endif
  886. string __buf;
  887. __buf.resize(__buf.capacity());
  888. char* __a = &__buf[0];
  889. char* __a_end = __a;
  890. unsigned __g[__num_get_base::__num_get_buf_sz];
  891. unsigned* __g_end = __g;
  892. unsigned __dc = 0;
  893. for (; __b != __e; ++__b)
  894. {
  895. if (__a_end == __a + __buf.size())
  896. {
  897. size_t __tmp = __buf.size();
  898. __buf.resize(2*__buf.size());
  899. __buf.resize(__buf.capacity());
  900. __a = &__buf[0];
  901. __a_end = __a + __tmp;
  902. }
  903. if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
  904. __thousands_sep, __grouping, __g, __g_end,
  905. __atoms))
  906. break;
  907. }
  908. if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
  909. *__g_end++ = __dc;
  910. // Stage 3
  911. __v = std::__num_get_unsigned_integral<_Unsigned>(__a, __a_end, __err, __base);
  912. // Digit grouping checked
  913. __check_grouping(__grouping, __g, __g_end, __err);
  914. // EOF checked
  915. if (__b == __e)
  916. __err |= ios_base::eofbit;
  917. return __b;
  918. }
  919. // floating point
  920. template <class _CharT, class _InputIterator>
  921. template <class _Fp>
  922. _InputIterator
  923. num_get<_CharT, _InputIterator>::__do_get_floating_point(iter_type __b, iter_type __e,
  924. ios_base& __iob,
  925. ios_base::iostate& __err,
  926. _Fp& __v) const
  927. {
  928. // Stage 1, nothing to do
  929. // Stage 2
  930. char_type __atoms[32];
  931. char_type __decimal_point;
  932. char_type __thousands_sep;
  933. string __grouping = this->__stage2_float_prep(__iob, __atoms,
  934. __decimal_point,
  935. __thousands_sep);
  936. string __buf;
  937. __buf.resize(__buf.capacity());
  938. char* __a = &__buf[0];
  939. char* __a_end = __a;
  940. unsigned __g[__num_get_base::__num_get_buf_sz];
  941. unsigned* __g_end = __g;
  942. unsigned __dc = 0;
  943. bool __in_units = true;
  944. char __exp = 'E';
  945. for (; __b != __e; ++__b)
  946. {
  947. if (__a_end == __a + __buf.size())
  948. {
  949. size_t __tmp = __buf.size();
  950. __buf.resize(2*__buf.size());
  951. __buf.resize(__buf.capacity());
  952. __a = &__buf[0];
  953. __a_end = __a + __tmp;
  954. }
  955. if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
  956. __decimal_point, __thousands_sep,
  957. __grouping, __g, __g_end,
  958. __dc, __atoms))
  959. break;
  960. }
  961. if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
  962. *__g_end++ = __dc;
  963. // Stage 3
  964. __v = std::__num_get_float<_Fp>(__a, __a_end, __err);
  965. // Digit grouping checked
  966. __check_grouping(__grouping, __g, __g_end, __err);
  967. // EOF checked
  968. if (__b == __e)
  969. __err |= ios_base::eofbit;
  970. return __b;
  971. }
  972. template <class _CharT, class _InputIterator>
  973. _InputIterator
  974. num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
  975. ios_base& __iob,
  976. ios_base::iostate& __err,
  977. void*& __v) const
  978. {
  979. // Stage 1
  980. int __base = 16;
  981. // Stage 2
  982. char_type __atoms[26];
  983. char_type __thousands_sep = char_type();
  984. string __grouping;
  985. std::use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
  986. __num_get_base::__src + 26, __atoms);
  987. string __buf;
  988. __buf.resize(__buf.capacity());
  989. char* __a = &__buf[0];
  990. char* __a_end = __a;
  991. unsigned __g[__num_get_base::__num_get_buf_sz];
  992. unsigned* __g_end = __g;
  993. unsigned __dc = 0;
  994. for (; __b != __e; ++__b)
  995. {
  996. if (__a_end == __a + __buf.size())
  997. {
  998. size_t __tmp = __buf.size();
  999. __buf.resize(2*__buf.size());
  1000. __buf.resize(__buf.capacity());
  1001. __a = &__buf[0];
  1002. __a_end = __a + __tmp;
  1003. }
  1004. if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
  1005. __thousands_sep, __grouping,
  1006. __g, __g_end, __atoms))
  1007. break;
  1008. }
  1009. // Stage 3
  1010. __buf.resize(__a_end - __a);
  1011. if (__libcpp_sscanf_l(__buf.c_str(), _LIBCPP_GET_C_LOCALE, "%p", &__v) != 1)
  1012. __err = ios_base::failbit;
  1013. // EOF checked
  1014. if (__b == __e)
  1015. __err |= ios_base::eofbit;
  1016. return __b;
  1017. }
  1018. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<char>;
  1019. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1020. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_get<wchar_t>;
  1021. #endif
  1022. struct _LIBCPP_EXPORTED_FROM_ABI __num_put_base
  1023. {
  1024. protected:
  1025. static void __format_int(char* __fmt, const char* __len, bool __signd,
  1026. ios_base::fmtflags __flags);
  1027. static bool __format_float(char* __fmt, const char* __len,
  1028. ios_base::fmtflags __flags);
  1029. static char* __identify_padding(char* __nb, char* __ne,
  1030. const ios_base& __iob);
  1031. };
  1032. template <class _CharT>
  1033. struct __num_put
  1034. : protected __num_put_base
  1035. {
  1036. static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
  1037. _CharT* __ob, _CharT*& __op, _CharT*& __oe,
  1038. const locale& __loc);
  1039. static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
  1040. _CharT* __ob, _CharT*& __op, _CharT*& __oe,
  1041. const locale& __loc);
  1042. };
  1043. template <class _CharT>
  1044. void
  1045. __num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
  1046. _CharT* __ob, _CharT*& __op, _CharT*& __oe,
  1047. const locale& __loc)
  1048. {
  1049. const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> > (__loc);
  1050. const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
  1051. string __grouping = __npt.grouping();
  1052. if (__grouping.empty())
  1053. {
  1054. __ct.widen(__nb, __ne, __ob);
  1055. __oe = __ob + (__ne - __nb);
  1056. }
  1057. else
  1058. {
  1059. __oe = __ob;
  1060. char* __nf = __nb;
  1061. if (*__nf == '-' || *__nf == '+')
  1062. *__oe++ = __ct.widen(*__nf++);
  1063. if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
  1064. __nf[1] == 'X'))
  1065. {
  1066. *__oe++ = __ct.widen(*__nf++);
  1067. *__oe++ = __ct.widen(*__nf++);
  1068. }
  1069. std::reverse(__nf, __ne);
  1070. _CharT __thousands_sep = __npt.thousands_sep();
  1071. unsigned __dc = 0;
  1072. unsigned __dg = 0;
  1073. for (char* __p = __nf; __p < __ne; ++__p)
  1074. {
  1075. if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
  1076. __dc == static_cast<unsigned>(__grouping[__dg]))
  1077. {
  1078. *__oe++ = __thousands_sep;
  1079. __dc = 0;
  1080. if (__dg < __grouping.size()-1)
  1081. ++__dg;
  1082. }
  1083. *__oe++ = __ct.widen(*__p);
  1084. ++__dc;
  1085. }
  1086. std::reverse(__ob + (__nf - __nb), __oe);
  1087. }
  1088. if (__np == __ne)
  1089. __op = __oe;
  1090. else
  1091. __op = __ob + (__np - __nb);
  1092. }
  1093. template <class _CharT>
  1094. void
  1095. __num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
  1096. _CharT* __ob, _CharT*& __op, _CharT*& __oe,
  1097. const locale& __loc)
  1098. {
  1099. const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> > (__loc);
  1100. const numpunct<_CharT>& __npt = std::use_facet<numpunct<_CharT> >(__loc);
  1101. string __grouping = __npt.grouping();
  1102. __oe = __ob;
  1103. char* __nf = __nb;
  1104. if (*__nf == '-' || *__nf == '+')
  1105. *__oe++ = __ct.widen(*__nf++);
  1106. char* __ns;
  1107. if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
  1108. __nf[1] == 'X'))
  1109. {
  1110. *__oe++ = __ct.widen(*__nf++);
  1111. *__oe++ = __ct.widen(*__nf++);
  1112. for (__ns = __nf; __ns < __ne; ++__ns)
  1113. if (!isxdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
  1114. break;
  1115. }
  1116. else
  1117. {
  1118. for (__ns = __nf; __ns < __ne; ++__ns)
  1119. if (!isdigit_l(*__ns, _LIBCPP_GET_C_LOCALE))
  1120. break;
  1121. }
  1122. if (__grouping.empty())
  1123. {
  1124. __ct.widen(__nf, __ns, __oe);
  1125. __oe += __ns - __nf;
  1126. }
  1127. else
  1128. {
  1129. std::reverse(__nf, __ns);
  1130. _CharT __thousands_sep = __npt.thousands_sep();
  1131. unsigned __dc = 0;
  1132. unsigned __dg = 0;
  1133. for (char* __p = __nf; __p < __ns; ++__p)
  1134. {
  1135. if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
  1136. {
  1137. *__oe++ = __thousands_sep;
  1138. __dc = 0;
  1139. if (__dg < __grouping.size()-1)
  1140. ++__dg;
  1141. }
  1142. *__oe++ = __ct.widen(*__p);
  1143. ++__dc;
  1144. }
  1145. std::reverse(__ob + (__nf - __nb), __oe);
  1146. }
  1147. for (__nf = __ns; __nf < __ne; ++__nf)
  1148. {
  1149. if (*__nf == '.')
  1150. {
  1151. *__oe++ = __npt.decimal_point();
  1152. ++__nf;
  1153. break;
  1154. }
  1155. else
  1156. *__oe++ = __ct.widen(*__nf);
  1157. }
  1158. __ct.widen(__nf, __ne, __oe);
  1159. __oe += __ne - __nf;
  1160. if (__np == __ne)
  1161. __op = __oe;
  1162. else
  1163. __op = __ob + (__np - __nb);
  1164. }
  1165. extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<char>;
  1166. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1167. extern template struct _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __num_put<wchar_t>;
  1168. #endif
  1169. template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
  1170. class _LIBCPP_TEMPLATE_VIS num_put
  1171. : public locale::facet,
  1172. private __num_put<_CharT>
  1173. {
  1174. public:
  1175. typedef _CharT char_type;
  1176. typedef _OutputIterator iter_type;
  1177. _LIBCPP_INLINE_VISIBILITY
  1178. explicit num_put(size_t __refs = 0)
  1179. : locale::facet(__refs) {}
  1180. _LIBCPP_INLINE_VISIBILITY
  1181. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  1182. bool __v) const
  1183. {
  1184. return do_put(__s, __iob, __fl, __v);
  1185. }
  1186. _LIBCPP_INLINE_VISIBILITY
  1187. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  1188. long __v) const
  1189. {
  1190. return do_put(__s, __iob, __fl, __v);
  1191. }
  1192. _LIBCPP_INLINE_VISIBILITY
  1193. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  1194. long long __v) const
  1195. {
  1196. return do_put(__s, __iob, __fl, __v);
  1197. }
  1198. _LIBCPP_INLINE_VISIBILITY
  1199. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  1200. unsigned long __v) const
  1201. {
  1202. return do_put(__s, __iob, __fl, __v);
  1203. }
  1204. _LIBCPP_INLINE_VISIBILITY
  1205. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  1206. unsigned long long __v) const
  1207. {
  1208. return do_put(__s, __iob, __fl, __v);
  1209. }
  1210. _LIBCPP_INLINE_VISIBILITY
  1211. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  1212. double __v) const
  1213. {
  1214. return do_put(__s, __iob, __fl, __v);
  1215. }
  1216. _LIBCPP_INLINE_VISIBILITY
  1217. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  1218. long double __v) const
  1219. {
  1220. return do_put(__s, __iob, __fl, __v);
  1221. }
  1222. _LIBCPP_INLINE_VISIBILITY
  1223. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  1224. const void* __v) const
  1225. {
  1226. return do_put(__s, __iob, __fl, __v);
  1227. }
  1228. static locale::id id;
  1229. protected:
  1230. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~num_put() override {}
  1231. virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
  1232. bool __v) const;
  1233. virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
  1234. long __v) const;
  1235. virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
  1236. long long __v) const;
  1237. virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
  1238. unsigned long) const;
  1239. virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
  1240. unsigned long long) const;
  1241. virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
  1242. double __v) const;
  1243. virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
  1244. long double __v) const;
  1245. virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
  1246. const void* __v) const;
  1247. template <class _Integral>
  1248. _LIBCPP_HIDE_FROM_ABI inline
  1249. _OutputIterator __do_put_integral(iter_type __s, ios_base& __iob,
  1250. char_type __fl, _Integral __v,
  1251. char const* __len) const;
  1252. template <class _Float>
  1253. _LIBCPP_HIDE_FROM_ABI inline
  1254. _OutputIterator __do_put_floating_point(iter_type __s, ios_base& __iob,
  1255. char_type __fl, _Float __v,
  1256. char const* __len) const;
  1257. };
  1258. template <class _CharT, class _OutputIterator>
  1259. locale::id
  1260. num_put<_CharT, _OutputIterator>::id;
  1261. template <class _CharT, class _OutputIterator>
  1262. _LIBCPP_HIDE_FROM_ABI
  1263. _OutputIterator
  1264. __pad_and_output(_OutputIterator __s,
  1265. const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
  1266. ios_base& __iob, _CharT __fl)
  1267. {
  1268. streamsize __sz = __oe - __ob;
  1269. streamsize __ns = __iob.width();
  1270. if (__ns > __sz)
  1271. __ns -= __sz;
  1272. else
  1273. __ns = 0;
  1274. for (;__ob < __op; ++__ob, ++__s)
  1275. *__s = *__ob;
  1276. for (; __ns; --__ns, ++__s)
  1277. *__s = __fl;
  1278. for (; __ob < __oe; ++__ob, ++__s)
  1279. *__s = *__ob;
  1280. __iob.width(0);
  1281. return __s;
  1282. }
  1283. template <class _CharT, class _Traits>
  1284. _LIBCPP_HIDE_FROM_ABI
  1285. ostreambuf_iterator<_CharT, _Traits>
  1286. __pad_and_output(ostreambuf_iterator<_CharT, _Traits> __s,
  1287. const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
  1288. ios_base& __iob, _CharT __fl)
  1289. {
  1290. if (__s.__sbuf_ == nullptr)
  1291. return __s;
  1292. streamsize __sz = __oe - __ob;
  1293. streamsize __ns = __iob.width();
  1294. if (__ns > __sz)
  1295. __ns -= __sz;
  1296. else
  1297. __ns = 0;
  1298. streamsize __np = __op - __ob;
  1299. if (__np > 0)
  1300. {
  1301. if (__s.__sbuf_->sputn(__ob, __np) != __np)
  1302. {
  1303. __s.__sbuf_ = nullptr;
  1304. return __s;
  1305. }
  1306. }
  1307. if (__ns > 0)
  1308. {
  1309. basic_string<_CharT, _Traits> __sp(__ns, __fl);
  1310. if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns)
  1311. {
  1312. __s.__sbuf_ = nullptr;
  1313. return __s;
  1314. }
  1315. }
  1316. __np = __oe - __op;
  1317. if (__np > 0)
  1318. {
  1319. if (__s.__sbuf_->sputn(__op, __np) != __np)
  1320. {
  1321. __s.__sbuf_ = nullptr;
  1322. return __s;
  1323. }
  1324. }
  1325. __iob.width(0);
  1326. return __s;
  1327. }
  1328. template <class _CharT, class _OutputIterator>
  1329. _OutputIterator
  1330. num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
  1331. char_type __fl, bool __v) const
  1332. {
  1333. if ((__iob.flags() & ios_base::boolalpha) == 0)
  1334. return do_put(__s, __iob, __fl, (unsigned long)__v);
  1335. const numpunct<char_type>& __np = std::use_facet<numpunct<char_type> >(__iob.getloc());
  1336. typedef typename numpunct<char_type>::string_type string_type;
  1337. string_type __nm = __v ? __np.truename() : __np.falsename();
  1338. for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
  1339. *__s = *__i;
  1340. return __s;
  1341. }
  1342. template <class _CharT, class _OutputIterator>
  1343. template <class _Integral>
  1344. _LIBCPP_HIDE_FROM_ABI inline
  1345. _OutputIterator
  1346. num_put<_CharT, _OutputIterator>::__do_put_integral(iter_type __s, ios_base& __iob,
  1347. char_type __fl, _Integral __v,
  1348. char const* __len) const
  1349. {
  1350. // Stage 1 - Get number in narrow char
  1351. char __fmt[8] = {'%', 0};
  1352. this->__format_int(__fmt+1, __len, is_signed<_Integral>::value, __iob.flags());
  1353. // Worst case is octal, with showbase enabled. Note that octal is always
  1354. // printed as an unsigned value.
  1355. using _Unsigned = typename make_unsigned<_Integral>::type;
  1356. _LIBCPP_CONSTEXPR const unsigned __nbuf
  1357. = (numeric_limits<_Unsigned>::digits / 3) // 1 char per 3 bits
  1358. + ((numeric_limits<_Unsigned>::digits % 3) != 0) // round up
  1359. + 2; // base prefix + terminating null character
  1360. char __nar[__nbuf];
  1361. _LIBCPP_DIAGNOSTIC_PUSH
  1362. _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
  1363. _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
  1364. int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, __fmt, __v);
  1365. _LIBCPP_DIAGNOSTIC_POP
  1366. char* __ne = __nar + __nc;
  1367. char* __np = this->__identify_padding(__nar, __ne, __iob);
  1368. // Stage 2 - Widen __nar while adding thousands separators
  1369. char_type __o[2*(__nbuf-1) - 1];
  1370. char_type* __op; // pad here
  1371. char_type* __oe; // end of output
  1372. this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
  1373. // [__o, __oe) contains thousands_sep'd wide number
  1374. // Stage 3 & 4
  1375. return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
  1376. }
  1377. template <class _CharT, class _OutputIterator>
  1378. _OutputIterator
  1379. num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
  1380. char_type __fl, long __v) const
  1381. {
  1382. return this->__do_put_integral(__s, __iob, __fl, __v, "l");
  1383. }
  1384. template <class _CharT, class _OutputIterator>
  1385. _OutputIterator
  1386. num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
  1387. char_type __fl, long long __v) const
  1388. {
  1389. return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
  1390. }
  1391. template <class _CharT, class _OutputIterator>
  1392. _OutputIterator
  1393. num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
  1394. char_type __fl, unsigned long __v) const
  1395. {
  1396. return this->__do_put_integral(__s, __iob, __fl, __v, "l");
  1397. }
  1398. template <class _CharT, class _OutputIterator>
  1399. _OutputIterator
  1400. num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
  1401. char_type __fl, unsigned long long __v) const
  1402. {
  1403. return this->__do_put_integral(__s, __iob, __fl, __v, "ll");
  1404. }
  1405. template <class _CharT, class _OutputIterator>
  1406. template <class _Float>
  1407. _LIBCPP_HIDE_FROM_ABI inline
  1408. _OutputIterator
  1409. num_put<_CharT, _OutputIterator>::__do_put_floating_point(iter_type __s, ios_base& __iob,
  1410. char_type __fl, _Float __v,
  1411. char const* __len) const
  1412. {
  1413. // Stage 1 - Get number in narrow char
  1414. char __fmt[8] = {'%', 0};
  1415. bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
  1416. const unsigned __nbuf = 30;
  1417. char __nar[__nbuf];
  1418. char* __nb = __nar;
  1419. int __nc;
  1420. _LIBCPP_DIAGNOSTIC_PUSH
  1421. _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
  1422. _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
  1423. if (__specify_precision)
  1424. __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt,
  1425. (int)__iob.precision(), __v);
  1426. else
  1427. __nc = __libcpp_snprintf_l(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
  1428. unique_ptr<char, void(*)(void*)> __nbh(nullptr, free);
  1429. if (__nc > static_cast<int>(__nbuf-1))
  1430. {
  1431. if (__specify_precision)
  1432. __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
  1433. else
  1434. __nc = __libcpp_asprintf_l(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
  1435. if (__nc == -1)
  1436. __throw_bad_alloc();
  1437. __nbh.reset(__nb);
  1438. }
  1439. _LIBCPP_DIAGNOSTIC_POP
  1440. char* __ne = __nb + __nc;
  1441. char* __np = this->__identify_padding(__nb, __ne, __iob);
  1442. // Stage 2 - Widen __nar while adding thousands separators
  1443. char_type __o[2*(__nbuf-1) - 1];
  1444. char_type* __ob = __o;
  1445. unique_ptr<char_type, void(*)(void*)> __obh(0, free);
  1446. if (__nb != __nar)
  1447. {
  1448. __ob = (char_type*)malloc(2*static_cast<size_t>(__nc)*sizeof(char_type));
  1449. if (__ob == 0)
  1450. __throw_bad_alloc();
  1451. __obh.reset(__ob);
  1452. }
  1453. char_type* __op; // pad here
  1454. char_type* __oe; // end of output
  1455. this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
  1456. // [__o, __oe) contains thousands_sep'd wide number
  1457. // Stage 3 & 4
  1458. __s = std::__pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
  1459. return __s;
  1460. }
  1461. template <class _CharT, class _OutputIterator>
  1462. _OutputIterator
  1463. num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
  1464. char_type __fl, double __v) const
  1465. {
  1466. return this->__do_put_floating_point(__s, __iob, __fl, __v, "");
  1467. }
  1468. template <class _CharT, class _OutputIterator>
  1469. _OutputIterator
  1470. num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
  1471. char_type __fl, long double __v) const
  1472. {
  1473. return this->__do_put_floating_point(__s, __iob, __fl, __v, "L");
  1474. }
  1475. template <class _CharT, class _OutputIterator>
  1476. _OutputIterator
  1477. num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
  1478. char_type __fl, const void* __v) const
  1479. {
  1480. // Stage 1 - Get pointer in narrow char
  1481. const unsigned __nbuf = 20;
  1482. char __nar[__nbuf];
  1483. int __nc = __libcpp_snprintf_l(__nar, sizeof(__nar), _LIBCPP_GET_C_LOCALE, "%p", __v);
  1484. char* __ne = __nar + __nc;
  1485. char* __np = this->__identify_padding(__nar, __ne, __iob);
  1486. // Stage 2 - Widen __nar
  1487. char_type __o[2*(__nbuf-1) - 1];
  1488. char_type* __op; // pad here
  1489. char_type* __oe; // end of output
  1490. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
  1491. __ct.widen(__nar, __ne, __o);
  1492. __oe = __o + (__ne - __nar);
  1493. if (__np == __ne)
  1494. __op = __oe;
  1495. else
  1496. __op = __o + (__np - __nar);
  1497. // [__o, __oe) contains wide number
  1498. // Stage 3 & 4
  1499. return std::__pad_and_output(__s, __o, __op, __oe, __iob, __fl);
  1500. }
  1501. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<char>;
  1502. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1503. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS num_put<wchar_t>;
  1504. #endif
  1505. template <class _CharT, class _InputIterator>
  1506. _LIBCPP_HIDE_FROM_ABI
  1507. int
  1508. __get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
  1509. ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
  1510. {
  1511. // Precondition: __n >= 1
  1512. if (__b == __e)
  1513. {
  1514. __err |= ios_base::eofbit | ios_base::failbit;
  1515. return 0;
  1516. }
  1517. // get first digit
  1518. _CharT __c = *__b;
  1519. if (!__ct.is(ctype_base::digit, __c))
  1520. {
  1521. __err |= ios_base::failbit;
  1522. return 0;
  1523. }
  1524. int __r = __ct.narrow(__c, 0) - '0';
  1525. for (++__b, (void) --__n; __b != __e && __n > 0; ++__b, (void) --__n)
  1526. {
  1527. // get next digit
  1528. __c = *__b;
  1529. if (!__ct.is(ctype_base::digit, __c))
  1530. return __r;
  1531. __r = __r * 10 + __ct.narrow(__c, 0) - '0';
  1532. }
  1533. if (__b == __e)
  1534. __err |= ios_base::eofbit;
  1535. return __r;
  1536. }
  1537. class _LIBCPP_EXPORTED_FROM_ABI time_base
  1538. {
  1539. public:
  1540. enum dateorder {no_order, dmy, mdy, ymd, ydm};
  1541. };
  1542. template <class _CharT>
  1543. class _LIBCPP_TEMPLATE_VIS __time_get_c_storage
  1544. {
  1545. protected:
  1546. typedef basic_string<_CharT> string_type;
  1547. virtual const string_type* __weeks() const;
  1548. virtual const string_type* __months() const;
  1549. virtual const string_type* __am_pm() const;
  1550. virtual const string_type& __c() const;
  1551. virtual const string_type& __r() const;
  1552. virtual const string_type& __x() const;
  1553. virtual const string_type& __X() const;
  1554. _LIBCPP_INLINE_VISIBILITY
  1555. ~__time_get_c_storage() {}
  1556. };
  1557. template <> _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__weeks() const;
  1558. template <> _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__months() const;
  1559. template <> _LIBCPP_EXPORTED_FROM_ABI const string* __time_get_c_storage<char>::__am_pm() const;
  1560. template <> _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__c() const;
  1561. template <> _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__r() const;
  1562. template <> _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__x() const;
  1563. template <> _LIBCPP_EXPORTED_FROM_ABI const string& __time_get_c_storage<char>::__X() const;
  1564. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  1565. template <> _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__weeks() const;
  1566. template <> _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__months() const;
  1567. template <> _LIBCPP_EXPORTED_FROM_ABI const wstring* __time_get_c_storage<wchar_t>::__am_pm() const;
  1568. template <> _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__c() const;
  1569. template <> _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__r() const;
  1570. template <> _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__x() const;
  1571. template <> _LIBCPP_EXPORTED_FROM_ABI const wstring& __time_get_c_storage<wchar_t>::__X() const;
  1572. #endif
  1573. template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
  1574. class _LIBCPP_TEMPLATE_VIS time_get
  1575. : public locale::facet,
  1576. public time_base,
  1577. private __time_get_c_storage<_CharT>
  1578. {
  1579. public:
  1580. typedef _CharT char_type;
  1581. typedef _InputIterator iter_type;
  1582. typedef time_base::dateorder dateorder;
  1583. typedef basic_string<char_type> string_type;
  1584. _LIBCPP_INLINE_VISIBILITY
  1585. explicit time_get(size_t __refs = 0)
  1586. : locale::facet(__refs) {}
  1587. _LIBCPP_INLINE_VISIBILITY
  1588. dateorder date_order() const
  1589. {
  1590. return this->do_date_order();
  1591. }
  1592. _LIBCPP_INLINE_VISIBILITY
  1593. iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
  1594. ios_base::iostate& __err, tm* __tm) const
  1595. {
  1596. return do_get_time(__b, __e, __iob, __err, __tm);
  1597. }
  1598. _LIBCPP_INLINE_VISIBILITY
  1599. iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
  1600. ios_base::iostate& __err, tm* __tm) const
  1601. {
  1602. return do_get_date(__b, __e, __iob, __err, __tm);
  1603. }
  1604. _LIBCPP_INLINE_VISIBILITY
  1605. iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
  1606. ios_base::iostate& __err, tm* __tm) const
  1607. {
  1608. return do_get_weekday(__b, __e, __iob, __err, __tm);
  1609. }
  1610. _LIBCPP_INLINE_VISIBILITY
  1611. iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
  1612. ios_base::iostate& __err, tm* __tm) const
  1613. {
  1614. return do_get_monthname(__b, __e, __iob, __err, __tm);
  1615. }
  1616. _LIBCPP_INLINE_VISIBILITY
  1617. iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
  1618. ios_base::iostate& __err, tm* __tm) const
  1619. {
  1620. return do_get_year(__b, __e, __iob, __err, __tm);
  1621. }
  1622. _LIBCPP_INLINE_VISIBILITY
  1623. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  1624. ios_base::iostate& __err, tm *__tm,
  1625. char __fmt, char __mod = 0) const
  1626. {
  1627. return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
  1628. }
  1629. iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
  1630. ios_base::iostate& __err, tm* __tm,
  1631. const char_type* __fmtb, const char_type* __fmte) const;
  1632. static locale::id id;
  1633. protected:
  1634. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get() override {}
  1635. virtual dateorder do_date_order() const;
  1636. virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
  1637. ios_base::iostate& __err, tm* __tm) const;
  1638. virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
  1639. ios_base::iostate& __err, tm* __tm) const;
  1640. virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
  1641. ios_base::iostate& __err, tm* __tm) const;
  1642. virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
  1643. ios_base::iostate& __err, tm* __tm) const;
  1644. virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
  1645. ios_base::iostate& __err, tm* __tm) const;
  1646. virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
  1647. ios_base::iostate& __err, tm* __tm,
  1648. char __fmt, char __mod) const;
  1649. private:
  1650. void __get_white_space(iter_type& __b, iter_type __e,
  1651. ios_base::iostate& __err, const ctype<char_type>& __ct) const;
  1652. void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
  1653. const ctype<char_type>& __ct) const;
  1654. void __get_weekdayname(int& __m,
  1655. iter_type& __b, iter_type __e,
  1656. ios_base::iostate& __err,
  1657. const ctype<char_type>& __ct) const;
  1658. void __get_monthname(int& __m,
  1659. iter_type& __b, iter_type __e,
  1660. ios_base::iostate& __err,
  1661. const ctype<char_type>& __ct) const;
  1662. void __get_day(int& __d,
  1663. iter_type& __b, iter_type __e,
  1664. ios_base::iostate& __err,
  1665. const ctype<char_type>& __ct) const;
  1666. void __get_month(int& __m,
  1667. iter_type& __b, iter_type __e,
  1668. ios_base::iostate& __err,
  1669. const ctype<char_type>& __ct) const;
  1670. void __get_year(int& __y,
  1671. iter_type& __b, iter_type __e,
  1672. ios_base::iostate& __err,
  1673. const ctype<char_type>& __ct) const;
  1674. void __get_year4(int& __y,
  1675. iter_type& __b, iter_type __e,
  1676. ios_base::iostate& __err,
  1677. const ctype<char_type>& __ct) const;
  1678. void __get_hour(int& __d,
  1679. iter_type& __b, iter_type __e,
  1680. ios_base::iostate& __err,
  1681. const ctype<char_type>& __ct) const;
  1682. void __get_12_hour(int& __h,
  1683. iter_type& __b, iter_type __e,
  1684. ios_base::iostate& __err,
  1685. const ctype<char_type>& __ct) const;
  1686. void __get_am_pm(int& __h,
  1687. iter_type& __b, iter_type __e,
  1688. ios_base::iostate& __err,
  1689. const ctype<char_type>& __ct) const;
  1690. void __get_minute(int& __m,
  1691. iter_type& __b, iter_type __e,
  1692. ios_base::iostate& __err,
  1693. const ctype<char_type>& __ct) const;
  1694. void __get_second(int& __s,
  1695. iter_type& __b, iter_type __e,
  1696. ios_base::iostate& __err,
  1697. const ctype<char_type>& __ct) const;
  1698. void __get_weekday(int& __w,
  1699. iter_type& __b, iter_type __e,
  1700. ios_base::iostate& __err,
  1701. const ctype<char_type>& __ct) const;
  1702. void __get_day_year_num(int& __w,
  1703. iter_type& __b, iter_type __e,
  1704. ios_base::iostate& __err,
  1705. const ctype<char_type>& __ct) const;
  1706. };
  1707. template <class _CharT, class _InputIterator>
  1708. locale::id
  1709. time_get<_CharT, _InputIterator>::id;
  1710. // time_get primitives
  1711. template <class _CharT, class _InputIterator>
  1712. void
  1713. time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
  1714. iter_type& __b, iter_type __e,
  1715. ios_base::iostate& __err,
  1716. const ctype<char_type>& __ct) const
  1717. {
  1718. // Note: ignoring case comes from the POSIX strptime spec
  1719. const string_type* __wk = this->__weeks();
  1720. ptrdiff_t __i = _VSTD::__scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
  1721. if (__i < 14)
  1722. __w = __i % 7;
  1723. }
  1724. template <class _CharT, class _InputIterator>
  1725. void
  1726. time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
  1727. iter_type& __b, iter_type __e,
  1728. ios_base::iostate& __err,
  1729. const ctype<char_type>& __ct) const
  1730. {
  1731. // Note: ignoring case comes from the POSIX strptime spec
  1732. const string_type* __month = this->__months();
  1733. ptrdiff_t __i = _VSTD::__scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
  1734. if (__i < 24)
  1735. __m = __i % 12;
  1736. }
  1737. template <class _CharT, class _InputIterator>
  1738. void
  1739. time_get<_CharT, _InputIterator>::__get_day(int& __d,
  1740. iter_type& __b, iter_type __e,
  1741. ios_base::iostate& __err,
  1742. const ctype<char_type>& __ct) const
  1743. {
  1744. int __t = _VSTD::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
  1745. if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
  1746. __d = __t;
  1747. else
  1748. __err |= ios_base::failbit;
  1749. }
  1750. template <class _CharT, class _InputIterator>
  1751. void
  1752. time_get<_CharT, _InputIterator>::__get_month(int& __m,
  1753. iter_type& __b, iter_type __e,
  1754. ios_base::iostate& __err,
  1755. const ctype<char_type>& __ct) const
  1756. {
  1757. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
  1758. if (!(__err & ios_base::failbit) && 0 <= __t && __t <= 11)
  1759. __m = __t;
  1760. else
  1761. __err |= ios_base::failbit;
  1762. }
  1763. template <class _CharT, class _InputIterator>
  1764. void
  1765. time_get<_CharT, _InputIterator>::__get_year(int& __y,
  1766. iter_type& __b, iter_type __e,
  1767. ios_base::iostate& __err,
  1768. const ctype<char_type>& __ct) const
  1769. {
  1770. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
  1771. if (!(__err & ios_base::failbit))
  1772. {
  1773. if (__t < 69)
  1774. __t += 2000;
  1775. else if (69 <= __t && __t <= 99)
  1776. __t += 1900;
  1777. __y = __t - 1900;
  1778. }
  1779. }
  1780. template <class _CharT, class _InputIterator>
  1781. void
  1782. time_get<_CharT, _InputIterator>::__get_year4(int& __y,
  1783. iter_type& __b, iter_type __e,
  1784. ios_base::iostate& __err,
  1785. const ctype<char_type>& __ct) const
  1786. {
  1787. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 4);
  1788. if (!(__err & ios_base::failbit))
  1789. __y = __t - 1900;
  1790. }
  1791. template <class _CharT, class _InputIterator>
  1792. void
  1793. time_get<_CharT, _InputIterator>::__get_hour(int& __h,
  1794. iter_type& __b, iter_type __e,
  1795. ios_base::iostate& __err,
  1796. const ctype<char_type>& __ct) const
  1797. {
  1798. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
  1799. if (!(__err & ios_base::failbit) && __t <= 23)
  1800. __h = __t;
  1801. else
  1802. __err |= ios_base::failbit;
  1803. }
  1804. template <class _CharT, class _InputIterator>
  1805. void
  1806. time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
  1807. iter_type& __b, iter_type __e,
  1808. ios_base::iostate& __err,
  1809. const ctype<char_type>& __ct) const
  1810. {
  1811. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
  1812. if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
  1813. __h = __t;
  1814. else
  1815. __err |= ios_base::failbit;
  1816. }
  1817. template <class _CharT, class _InputIterator>
  1818. void
  1819. time_get<_CharT, _InputIterator>::__get_minute(int& __m,
  1820. iter_type& __b, iter_type __e,
  1821. ios_base::iostate& __err,
  1822. const ctype<char_type>& __ct) const
  1823. {
  1824. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
  1825. if (!(__err & ios_base::failbit) && __t <= 59)
  1826. __m = __t;
  1827. else
  1828. __err |= ios_base::failbit;
  1829. }
  1830. template <class _CharT, class _InputIterator>
  1831. void
  1832. time_get<_CharT, _InputIterator>::__get_second(int& __s,
  1833. iter_type& __b, iter_type __e,
  1834. ios_base::iostate& __err,
  1835. const ctype<char_type>& __ct) const
  1836. {
  1837. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 2);
  1838. if (!(__err & ios_base::failbit) && __t <= 60)
  1839. __s = __t;
  1840. else
  1841. __err |= ios_base::failbit;
  1842. }
  1843. template <class _CharT, class _InputIterator>
  1844. void
  1845. time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
  1846. iter_type& __b, iter_type __e,
  1847. ios_base::iostate& __err,
  1848. const ctype<char_type>& __ct) const
  1849. {
  1850. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 1);
  1851. if (!(__err & ios_base::failbit) && __t <= 6)
  1852. __w = __t;
  1853. else
  1854. __err |= ios_base::failbit;
  1855. }
  1856. template <class _CharT, class _InputIterator>
  1857. void
  1858. time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
  1859. iter_type& __b, iter_type __e,
  1860. ios_base::iostate& __err,
  1861. const ctype<char_type>& __ct) const
  1862. {
  1863. int __t = std::__get_up_to_n_digits(__b, __e, __err, __ct, 3);
  1864. if (!(__err & ios_base::failbit) && __t <= 365)
  1865. __d = __t;
  1866. else
  1867. __err |= ios_base::failbit;
  1868. }
  1869. template <class _CharT, class _InputIterator>
  1870. void
  1871. time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
  1872. ios_base::iostate& __err,
  1873. const ctype<char_type>& __ct) const
  1874. {
  1875. for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
  1876. ;
  1877. if (__b == __e)
  1878. __err |= ios_base::eofbit;
  1879. }
  1880. template <class _CharT, class _InputIterator>
  1881. void
  1882. time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
  1883. iter_type& __b, iter_type __e,
  1884. ios_base::iostate& __err,
  1885. const ctype<char_type>& __ct) const
  1886. {
  1887. const string_type* __ap = this->__am_pm();
  1888. if (__ap[0].size() + __ap[1].size() == 0)
  1889. {
  1890. __err |= ios_base::failbit;
  1891. return;
  1892. }
  1893. ptrdiff_t __i = _VSTD::__scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
  1894. if (__i == 0 && __h == 12)
  1895. __h = 0;
  1896. else if (__i == 1 && __h < 12)
  1897. __h += 12;
  1898. }
  1899. template <class _CharT, class _InputIterator>
  1900. void
  1901. time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
  1902. ios_base::iostate& __err,
  1903. const ctype<char_type>& __ct) const
  1904. {
  1905. if (__b == __e)
  1906. {
  1907. __err |= ios_base::eofbit | ios_base::failbit;
  1908. return;
  1909. }
  1910. if (__ct.narrow(*__b, 0) != '%')
  1911. __err |= ios_base::failbit;
  1912. else if(++__b == __e)
  1913. __err |= ios_base::eofbit;
  1914. }
  1915. // time_get end primitives
  1916. template <class _CharT, class _InputIterator>
  1917. _InputIterator
  1918. time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
  1919. ios_base& __iob,
  1920. ios_base::iostate& __err, tm* __tm,
  1921. const char_type* __fmtb, const char_type* __fmte) const
  1922. {
  1923. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
  1924. __err = ios_base::goodbit;
  1925. while (__fmtb != __fmte && __err == ios_base::goodbit)
  1926. {
  1927. if (__b == __e)
  1928. {
  1929. __err = ios_base::failbit;
  1930. break;
  1931. }
  1932. if (__ct.narrow(*__fmtb, 0) == '%')
  1933. {
  1934. if (++__fmtb == __fmte)
  1935. {
  1936. __err = ios_base::failbit;
  1937. break;
  1938. }
  1939. char __cmd = __ct.narrow(*__fmtb, 0);
  1940. char __opt = '\0';
  1941. if (__cmd == 'E' || __cmd == '0')
  1942. {
  1943. if (++__fmtb == __fmte)
  1944. {
  1945. __err = ios_base::failbit;
  1946. break;
  1947. }
  1948. __opt = __cmd;
  1949. __cmd = __ct.narrow(*__fmtb, 0);
  1950. }
  1951. __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
  1952. ++__fmtb;
  1953. }
  1954. else if (__ct.is(ctype_base::space, *__fmtb))
  1955. {
  1956. for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
  1957. ;
  1958. for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
  1959. ;
  1960. }
  1961. else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
  1962. {
  1963. ++__b;
  1964. ++__fmtb;
  1965. }
  1966. else
  1967. __err = ios_base::failbit;
  1968. }
  1969. if (__b == __e)
  1970. __err |= ios_base::eofbit;
  1971. return __b;
  1972. }
  1973. template <class _CharT, class _InputIterator>
  1974. typename time_get<_CharT, _InputIterator>::dateorder
  1975. time_get<_CharT, _InputIterator>::do_date_order() const
  1976. {
  1977. return mdy;
  1978. }
  1979. template <class _CharT, class _InputIterator>
  1980. _InputIterator
  1981. time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
  1982. ios_base& __iob,
  1983. ios_base::iostate& __err,
  1984. tm* __tm) const
  1985. {
  1986. const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
  1987. return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
  1988. }
  1989. template <class _CharT, class _InputIterator>
  1990. _InputIterator
  1991. time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
  1992. ios_base& __iob,
  1993. ios_base::iostate& __err,
  1994. tm* __tm) const
  1995. {
  1996. const string_type& __fmt = this->__x();
  1997. return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
  1998. }
  1999. template <class _CharT, class _InputIterator>
  2000. _InputIterator
  2001. time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
  2002. ios_base& __iob,
  2003. ios_base::iostate& __err,
  2004. tm* __tm) const
  2005. {
  2006. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
  2007. __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
  2008. return __b;
  2009. }
  2010. template <class _CharT, class _InputIterator>
  2011. _InputIterator
  2012. time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
  2013. ios_base& __iob,
  2014. ios_base::iostate& __err,
  2015. tm* __tm) const
  2016. {
  2017. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
  2018. __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
  2019. return __b;
  2020. }
  2021. template <class _CharT, class _InputIterator>
  2022. _InputIterator
  2023. time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
  2024. ios_base& __iob,
  2025. ios_base::iostate& __err,
  2026. tm* __tm) const
  2027. {
  2028. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
  2029. __get_year(__tm->tm_year, __b, __e, __err, __ct);
  2030. return __b;
  2031. }
  2032. template <class _CharT, class _InputIterator>
  2033. _InputIterator
  2034. time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
  2035. ios_base& __iob,
  2036. ios_base::iostate& __err, tm* __tm,
  2037. char __fmt, char) const
  2038. {
  2039. __err = ios_base::goodbit;
  2040. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
  2041. switch (__fmt)
  2042. {
  2043. case 'a':
  2044. case 'A':
  2045. __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
  2046. break;
  2047. case 'b':
  2048. case 'B':
  2049. case 'h':
  2050. __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
  2051. break;
  2052. case 'c':
  2053. {
  2054. const string_type& __fm = this->__c();
  2055. __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
  2056. }
  2057. break;
  2058. case 'd':
  2059. case 'e':
  2060. __get_day(__tm->tm_mday, __b, __e, __err, __ct);
  2061. break;
  2062. case 'D':
  2063. {
  2064. const char_type __fm[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
  2065. __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
  2066. }
  2067. break;
  2068. case 'F':
  2069. {
  2070. const char_type __fm[] = {'%', 'Y', '-', '%', 'm', '-', '%', 'd'};
  2071. __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
  2072. }
  2073. break;
  2074. case 'H':
  2075. __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
  2076. break;
  2077. case 'I':
  2078. __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
  2079. break;
  2080. case 'j':
  2081. __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
  2082. break;
  2083. case 'm':
  2084. __get_month(__tm->tm_mon, __b, __e, __err, __ct);
  2085. break;
  2086. case 'M':
  2087. __get_minute(__tm->tm_min, __b, __e, __err, __ct);
  2088. break;
  2089. case 'n':
  2090. case 't':
  2091. __get_white_space(__b, __e, __err, __ct);
  2092. break;
  2093. case 'p':
  2094. __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
  2095. break;
  2096. case 'r':
  2097. {
  2098. const char_type __fm[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
  2099. __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
  2100. }
  2101. break;
  2102. case 'R':
  2103. {
  2104. const char_type __fm[] = {'%', 'H', ':', '%', 'M'};
  2105. __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
  2106. }
  2107. break;
  2108. case 'S':
  2109. __get_second(__tm->tm_sec, __b, __e, __err, __ct);
  2110. break;
  2111. case 'T':
  2112. {
  2113. const char_type __fm[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
  2114. __b = get(__b, __e, __iob, __err, __tm, __fm, __fm + sizeof(__fm)/sizeof(__fm[0]));
  2115. }
  2116. break;
  2117. case 'w':
  2118. __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
  2119. break;
  2120. case 'x':
  2121. return do_get_date(__b, __e, __iob, __err, __tm);
  2122. case 'X':
  2123. {
  2124. const string_type& __fm = this->__X();
  2125. __b = get(__b, __e, __iob, __err, __tm, __fm.data(), __fm.data() + __fm.size());
  2126. }
  2127. break;
  2128. case 'y':
  2129. __get_year(__tm->tm_year, __b, __e, __err, __ct);
  2130. break;
  2131. case 'Y':
  2132. __get_year4(__tm->tm_year, __b, __e, __err, __ct);
  2133. break;
  2134. case '%':
  2135. __get_percent(__b, __e, __err, __ct);
  2136. break;
  2137. default:
  2138. __err |= ios_base::failbit;
  2139. }
  2140. return __b;
  2141. }
  2142. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<char>;
  2143. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2144. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get<wchar_t>;
  2145. #endif
  2146. class _LIBCPP_EXPORTED_FROM_ABI __time_get
  2147. {
  2148. protected:
  2149. locale_t __loc_;
  2150. __time_get(const char* __nm);
  2151. __time_get(const string& __nm);
  2152. ~__time_get();
  2153. };
  2154. template <class _CharT>
  2155. class _LIBCPP_TEMPLATE_VIS __time_get_storage
  2156. : public __time_get
  2157. {
  2158. protected:
  2159. typedef basic_string<_CharT> string_type;
  2160. string_type __weeks_[14];
  2161. string_type __months_[24];
  2162. string_type __am_pm_[2];
  2163. string_type __c_;
  2164. string_type __r_;
  2165. string_type __x_;
  2166. string_type __X_;
  2167. explicit __time_get_storage(const char* __nm);
  2168. explicit __time_get_storage(const string& __nm);
  2169. _LIBCPP_INLINE_VISIBILITY ~__time_get_storage() {}
  2170. time_base::dateorder __do_date_order() const;
  2171. private:
  2172. void init(const ctype<_CharT>&);
  2173. string_type __analyze(char __fmt, const ctype<_CharT>&);
  2174. };
  2175. #define _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(_CharT) \
  2176. template <> _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
  2177. template <> _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*); \
  2178. template <> _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&); \
  2179. template <> _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
  2180. template <> _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
  2181. extern template _LIBCPP_EXPORTED_FROM_ABI time_base::dateorder __time_get_storage<_CharT>::__do_date_order() const; \
  2182. extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const char*); \
  2183. extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::__time_get_storage(const string&); \
  2184. extern template _LIBCPP_EXPORTED_FROM_ABI void __time_get_storage<_CharT>::init(const ctype<_CharT>&); \
  2185. extern template _LIBCPP_EXPORTED_FROM_ABI __time_get_storage<_CharT>::string_type __time_get_storage<_CharT>::__analyze(char, const ctype<_CharT>&); \
  2186. /**/
  2187. _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(char)
  2188. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2189. _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION(wchar_t)
  2190. #endif
  2191. #undef _LIBCPP_TIME_GET_STORAGE_EXPLICIT_INSTANTIATION
  2192. template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
  2193. class _LIBCPP_TEMPLATE_VIS time_get_byname
  2194. : public time_get<_CharT, _InputIterator>,
  2195. private __time_get_storage<_CharT>
  2196. {
  2197. public:
  2198. typedef time_base::dateorder dateorder;
  2199. typedef _InputIterator iter_type;
  2200. typedef _CharT char_type;
  2201. typedef basic_string<char_type> string_type;
  2202. _LIBCPP_INLINE_VISIBILITY
  2203. explicit time_get_byname(const char* __nm, size_t __refs = 0)
  2204. : time_get<_CharT, _InputIterator>(__refs),
  2205. __time_get_storage<_CharT>(__nm) {}
  2206. _LIBCPP_INLINE_VISIBILITY
  2207. explicit time_get_byname(const string& __nm, size_t __refs = 0)
  2208. : time_get<_CharT, _InputIterator>(__refs),
  2209. __time_get_storage<_CharT>(__nm) {}
  2210. protected:
  2211. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_get_byname() override {}
  2212. _LIBCPP_HIDE_FROM_ABI_VIRTUAL dateorder do_date_order() const override {return this->__do_date_order();}
  2213. private:
  2214. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __weeks() const override {return this->__weeks_;}
  2215. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __months() const override {return this->__months_;}
  2216. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type* __am_pm() const override {return this->__am_pm_;}
  2217. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __c() const override {return this->__c_;}
  2218. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __r() const override {return this->__r_;}
  2219. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __x() const override {return this->__x_;}
  2220. _LIBCPP_HIDE_FROM_ABI_VIRTUAL const string_type& __X() const override {return this->__X_;}
  2221. };
  2222. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<char>;
  2223. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2224. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_get_byname<wchar_t>;
  2225. #endif
  2226. class _LIBCPP_EXPORTED_FROM_ABI __time_put
  2227. {
  2228. locale_t __loc_;
  2229. protected:
  2230. _LIBCPP_INLINE_VISIBILITY __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
  2231. __time_put(const char* __nm);
  2232. __time_put(const string& __nm);
  2233. ~__time_put();
  2234. void __do_put(char* __nb, char*& __ne, const tm* __tm,
  2235. char __fmt, char __mod) const;
  2236. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2237. void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
  2238. char __fmt, char __mod) const;
  2239. #endif
  2240. };
  2241. template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
  2242. class _LIBCPP_TEMPLATE_VIS time_put
  2243. : public locale::facet,
  2244. private __time_put
  2245. {
  2246. public:
  2247. typedef _CharT char_type;
  2248. typedef _OutputIterator iter_type;
  2249. _LIBCPP_INLINE_VISIBILITY
  2250. explicit time_put(size_t __refs = 0)
  2251. : locale::facet(__refs) {}
  2252. iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
  2253. const char_type* __pb, const char_type* __pe) const;
  2254. _LIBCPP_INLINE_VISIBILITY
  2255. iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
  2256. const tm* __tm, char __fmt, char __mod = 0) const
  2257. {
  2258. return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
  2259. }
  2260. static locale::id id;
  2261. protected:
  2262. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put() override {}
  2263. virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
  2264. char __fmt, char __mod) const;
  2265. _LIBCPP_INLINE_VISIBILITY
  2266. explicit time_put(const char* __nm, size_t __refs)
  2267. : locale::facet(__refs),
  2268. __time_put(__nm) {}
  2269. _LIBCPP_INLINE_VISIBILITY
  2270. explicit time_put(const string& __nm, size_t __refs)
  2271. : locale::facet(__refs),
  2272. __time_put(__nm) {}
  2273. };
  2274. template <class _CharT, class _OutputIterator>
  2275. locale::id
  2276. time_put<_CharT, _OutputIterator>::id;
  2277. template <class _CharT, class _OutputIterator>
  2278. _OutputIterator
  2279. time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
  2280. char_type __fl, const tm* __tm,
  2281. const char_type* __pb,
  2282. const char_type* __pe) const
  2283. {
  2284. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__iob.getloc());
  2285. for (; __pb != __pe; ++__pb)
  2286. {
  2287. if (__ct.narrow(*__pb, 0) == '%')
  2288. {
  2289. if (++__pb == __pe)
  2290. {
  2291. *__s++ = __pb[-1];
  2292. break;
  2293. }
  2294. char __mod = 0;
  2295. char __fmt = __ct.narrow(*__pb, 0);
  2296. if (__fmt == 'E' || __fmt == 'O')
  2297. {
  2298. if (++__pb == __pe)
  2299. {
  2300. *__s++ = __pb[-2];
  2301. *__s++ = __pb[-1];
  2302. break;
  2303. }
  2304. __mod = __fmt;
  2305. __fmt = __ct.narrow(*__pb, 0);
  2306. }
  2307. __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
  2308. }
  2309. else
  2310. *__s++ = *__pb;
  2311. }
  2312. return __s;
  2313. }
  2314. template <class _CharT, class _OutputIterator>
  2315. _OutputIterator
  2316. time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base&,
  2317. char_type, const tm* __tm,
  2318. char __fmt, char __mod) const
  2319. {
  2320. char_type __nar[100];
  2321. char_type* __nb = __nar;
  2322. char_type* __ne = __nb + 100;
  2323. __do_put(__nb, __ne, __tm, __fmt, __mod);
  2324. return _VSTD::copy(__nb, __ne, __s);
  2325. }
  2326. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<char>;
  2327. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2328. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put<wchar_t>;
  2329. #endif
  2330. template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
  2331. class _LIBCPP_TEMPLATE_VIS time_put_byname
  2332. : public time_put<_CharT, _OutputIterator>
  2333. {
  2334. public:
  2335. _LIBCPP_INLINE_VISIBILITY
  2336. explicit time_put_byname(const char* __nm, size_t __refs = 0)
  2337. : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
  2338. _LIBCPP_INLINE_VISIBILITY
  2339. explicit time_put_byname(const string& __nm, size_t __refs = 0)
  2340. : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
  2341. protected:
  2342. #if defined(_MSC_VER) && defined(__clang__)
  2343. ~time_put_byname() {}
  2344. #else
  2345. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~time_put_byname() override {}
  2346. #endif
  2347. };
  2348. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<char>;
  2349. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2350. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS time_put_byname<wchar_t>;
  2351. #endif
  2352. // money_base
  2353. class _LIBCPP_EXPORTED_FROM_ABI money_base
  2354. {
  2355. public:
  2356. enum part {none, space, symbol, sign, value};
  2357. struct pattern {char field[4];};
  2358. _LIBCPP_INLINE_VISIBILITY money_base() {}
  2359. };
  2360. // moneypunct
  2361. template <class _CharT, bool _International = false>
  2362. class _LIBCPP_TEMPLATE_VIS moneypunct
  2363. : public locale::facet,
  2364. public money_base
  2365. {
  2366. public:
  2367. typedef _CharT char_type;
  2368. typedef basic_string<char_type> string_type;
  2369. _LIBCPP_INLINE_VISIBILITY
  2370. explicit moneypunct(size_t __refs = 0)
  2371. : locale::facet(__refs) {}
  2372. _LIBCPP_INLINE_VISIBILITY char_type decimal_point() const {return do_decimal_point();}
  2373. _LIBCPP_INLINE_VISIBILITY char_type thousands_sep() const {return do_thousands_sep();}
  2374. _LIBCPP_INLINE_VISIBILITY string grouping() const {return do_grouping();}
  2375. _LIBCPP_INLINE_VISIBILITY string_type curr_symbol() const {return do_curr_symbol();}
  2376. _LIBCPP_INLINE_VISIBILITY string_type positive_sign() const {return do_positive_sign();}
  2377. _LIBCPP_INLINE_VISIBILITY string_type negative_sign() const {return do_negative_sign();}
  2378. _LIBCPP_INLINE_VISIBILITY int frac_digits() const {return do_frac_digits();}
  2379. _LIBCPP_INLINE_VISIBILITY pattern pos_format() const {return do_pos_format();}
  2380. _LIBCPP_INLINE_VISIBILITY pattern neg_format() const {return do_neg_format();}
  2381. static locale::id id;
  2382. static const bool intl = _International;
  2383. protected:
  2384. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct() override {}
  2385. virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
  2386. virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
  2387. virtual string do_grouping() const {return string();}
  2388. virtual string_type do_curr_symbol() const {return string_type();}
  2389. virtual string_type do_positive_sign() const {return string_type();}
  2390. virtual string_type do_negative_sign() const {return string_type(1, '-');}
  2391. virtual int do_frac_digits() const {return 0;}
  2392. virtual pattern do_pos_format() const
  2393. {pattern __p = {{symbol, sign, none, value}}; return __p;}
  2394. virtual pattern do_neg_format() const
  2395. {pattern __p = {{symbol, sign, none, value}}; return __p;}
  2396. };
  2397. template <class _CharT, bool _International>
  2398. locale::id
  2399. moneypunct<_CharT, _International>::id;
  2400. template <class _CharT, bool _International>
  2401. const bool
  2402. moneypunct<_CharT, _International>::intl;
  2403. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, false>;
  2404. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<char, true>;
  2405. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2406. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, false>;
  2407. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct<wchar_t, true>;
  2408. #endif
  2409. // moneypunct_byname
  2410. template <class _CharT, bool _International = false>
  2411. class _LIBCPP_TEMPLATE_VIS moneypunct_byname
  2412. : public moneypunct<_CharT, _International>
  2413. {
  2414. public:
  2415. typedef money_base::pattern pattern;
  2416. typedef _CharT char_type;
  2417. typedef basic_string<char_type> string_type;
  2418. _LIBCPP_INLINE_VISIBILITY
  2419. explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
  2420. : moneypunct<_CharT, _International>(__refs) {init(__nm);}
  2421. _LIBCPP_INLINE_VISIBILITY
  2422. explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
  2423. : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
  2424. protected:
  2425. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~moneypunct_byname() override {}
  2426. char_type do_decimal_point() const override {return __decimal_point_;}
  2427. char_type do_thousands_sep() const override {return __thousands_sep_;}
  2428. string do_grouping() const override {return __grouping_;}
  2429. string_type do_curr_symbol() const override {return __curr_symbol_;}
  2430. string_type do_positive_sign() const override {return __positive_sign_;}
  2431. string_type do_negative_sign() const override {return __negative_sign_;}
  2432. int do_frac_digits() const override {return __frac_digits_;}
  2433. pattern do_pos_format() const override {return __pos_format_;}
  2434. pattern do_neg_format() const override {return __neg_format_;}
  2435. private:
  2436. char_type __decimal_point_;
  2437. char_type __thousands_sep_;
  2438. string __grouping_;
  2439. string_type __curr_symbol_;
  2440. string_type __positive_sign_;
  2441. string_type __negative_sign_;
  2442. int __frac_digits_;
  2443. pattern __pos_format_;
  2444. pattern __neg_format_;
  2445. void init(const char*);
  2446. };
  2447. template<> _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, false>::init(const char*);
  2448. template<> _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<char, true>::init(const char*);
  2449. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, false>;
  2450. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<char, true>;
  2451. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2452. template<> _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, false>::init(const char*);
  2453. template<> _LIBCPP_EXPORTED_FROM_ABI void moneypunct_byname<wchar_t, true>::init(const char*);
  2454. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, false>;
  2455. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS moneypunct_byname<wchar_t, true>;
  2456. #endif
  2457. // money_get
  2458. template <class _CharT>
  2459. class __money_get
  2460. {
  2461. protected:
  2462. typedef _CharT char_type;
  2463. typedef basic_string<char_type> string_type;
  2464. _LIBCPP_INLINE_VISIBILITY __money_get() {}
  2465. static void __gather_info(bool __intl, const locale& __loc,
  2466. money_base::pattern& __pat, char_type& __dp,
  2467. char_type& __ts, string& __grp,
  2468. string_type& __sym, string_type& __psn,
  2469. string_type& __nsn, int& __fd);
  2470. };
  2471. template <class _CharT>
  2472. void
  2473. __money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
  2474. money_base::pattern& __pat, char_type& __dp,
  2475. char_type& __ts, string& __grp,
  2476. string_type& __sym, string_type& __psn,
  2477. string_type& __nsn, int& __fd)
  2478. {
  2479. if (__intl)
  2480. {
  2481. const moneypunct<char_type, true>& __mp =
  2482. std::use_facet<moneypunct<char_type, true> >(__loc);
  2483. __pat = __mp.neg_format();
  2484. __nsn = __mp.negative_sign();
  2485. __psn = __mp.positive_sign();
  2486. __dp = __mp.decimal_point();
  2487. __ts = __mp.thousands_sep();
  2488. __grp = __mp.grouping();
  2489. __sym = __mp.curr_symbol();
  2490. __fd = __mp.frac_digits();
  2491. }
  2492. else
  2493. {
  2494. const moneypunct<char_type, false>& __mp =
  2495. std::use_facet<moneypunct<char_type, false> >(__loc);
  2496. __pat = __mp.neg_format();
  2497. __nsn = __mp.negative_sign();
  2498. __psn = __mp.positive_sign();
  2499. __dp = __mp.decimal_point();
  2500. __ts = __mp.thousands_sep();
  2501. __grp = __mp.grouping();
  2502. __sym = __mp.curr_symbol();
  2503. __fd = __mp.frac_digits();
  2504. }
  2505. }
  2506. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<char>;
  2507. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2508. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_get<wchar_t>;
  2509. #endif
  2510. template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
  2511. class _LIBCPP_TEMPLATE_VIS money_get
  2512. : public locale::facet,
  2513. private __money_get<_CharT>
  2514. {
  2515. public:
  2516. typedef _CharT char_type;
  2517. typedef _InputIterator iter_type;
  2518. typedef basic_string<char_type> string_type;
  2519. _LIBCPP_INLINE_VISIBILITY
  2520. explicit money_get(size_t __refs = 0)
  2521. : locale::facet(__refs) {}
  2522. _LIBCPP_INLINE_VISIBILITY
  2523. iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
  2524. ios_base::iostate& __err, long double& __v) const
  2525. {
  2526. return do_get(__b, __e, __intl, __iob, __err, __v);
  2527. }
  2528. _LIBCPP_INLINE_VISIBILITY
  2529. iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
  2530. ios_base::iostate& __err, string_type& __v) const
  2531. {
  2532. return do_get(__b, __e, __intl, __iob, __err, __v);
  2533. }
  2534. static locale::id id;
  2535. protected:
  2536. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_get() override {}
  2537. virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
  2538. ios_base& __iob, ios_base::iostate& __err,
  2539. long double& __v) const;
  2540. virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
  2541. ios_base& __iob, ios_base::iostate& __err,
  2542. string_type& __v) const;
  2543. private:
  2544. static bool __do_get(iter_type& __b, iter_type __e,
  2545. bool __intl, const locale& __loc,
  2546. ios_base::fmtflags __flags, ios_base::iostate& __err,
  2547. bool& __neg, const ctype<char_type>& __ct,
  2548. unique_ptr<char_type, void(*)(void*)>& __wb,
  2549. char_type*& __wn, char_type* __we);
  2550. };
  2551. template <class _CharT, class _InputIterator>
  2552. locale::id
  2553. money_get<_CharT, _InputIterator>::id;
  2554. _LIBCPP_EXPORTED_FROM_ABI void __do_nothing(void*);
  2555. template <class _Tp>
  2556. _LIBCPP_HIDE_FROM_ABI
  2557. void
  2558. __double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
  2559. {
  2560. bool __owns = __b.get_deleter() != __do_nothing;
  2561. size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
  2562. size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
  2563. 2 * __cur_cap : numeric_limits<size_t>::max();
  2564. if (__new_cap == 0)
  2565. __new_cap = sizeof(_Tp);
  2566. size_t __n_off = static_cast<size_t>(__n - __b.get());
  2567. _Tp* __t = (_Tp*)std::realloc(__owns ? __b.get() : 0, __new_cap);
  2568. if (__t == 0)
  2569. __throw_bad_alloc();
  2570. if (__owns)
  2571. __b.release();
  2572. __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
  2573. __new_cap /= sizeof(_Tp);
  2574. __n = __b.get() + __n_off;
  2575. __e = __b.get() + __new_cap;
  2576. }
  2577. // true == success
  2578. template <class _CharT, class _InputIterator>
  2579. bool
  2580. money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
  2581. bool __intl, const locale& __loc,
  2582. ios_base::fmtflags __flags,
  2583. ios_base::iostate& __err,
  2584. bool& __neg,
  2585. const ctype<char_type>& __ct,
  2586. unique_ptr<char_type, void(*)(void*)>& __wb,
  2587. char_type*& __wn, char_type* __we)
  2588. {
  2589. if (__b == __e) {
  2590. __err |= ios_base::failbit;
  2591. return false;
  2592. }
  2593. const unsigned __bz = 100;
  2594. unsigned __gbuf[__bz];
  2595. unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
  2596. unsigned* __gn = __gb.get();
  2597. unsigned* __ge = __gn + __bz;
  2598. money_base::pattern __pat;
  2599. char_type __dp;
  2600. char_type __ts;
  2601. string __grp;
  2602. string_type __sym;
  2603. string_type __psn;
  2604. string_type __nsn;
  2605. // Capture the spaces read into money_base::{space,none} so they
  2606. // can be compared to initial spaces in __sym.
  2607. string_type __spaces;
  2608. int __fd;
  2609. __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
  2610. __sym, __psn, __nsn, __fd);
  2611. const string_type* __trailing_sign = 0;
  2612. __wn = __wb.get();
  2613. for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
  2614. {
  2615. switch (__pat.field[__p])
  2616. {
  2617. case money_base::space:
  2618. if (__p != 3)
  2619. {
  2620. if (__ct.is(ctype_base::space, *__b))
  2621. __spaces.push_back(*__b++);
  2622. else
  2623. {
  2624. __err |= ios_base::failbit;
  2625. return false;
  2626. }
  2627. }
  2628. _LIBCPP_FALLTHROUGH();
  2629. case money_base::none:
  2630. if (__p != 3)
  2631. {
  2632. while (__b != __e && __ct.is(ctype_base::space, *__b))
  2633. __spaces.push_back(*__b++);
  2634. }
  2635. break;
  2636. case money_base::sign:
  2637. if (__psn.size() > 0 && *__b == __psn[0])
  2638. {
  2639. ++__b;
  2640. __neg = false;
  2641. if (__psn.size() > 1)
  2642. __trailing_sign = &__psn;
  2643. break;
  2644. }
  2645. if (__nsn.size() > 0 && *__b == __nsn[0])
  2646. {
  2647. ++__b;
  2648. __neg = true;
  2649. if (__nsn.size() > 1)
  2650. __trailing_sign = &__nsn;
  2651. break;
  2652. }
  2653. if (__psn.size() > 0 && __nsn.size() > 0)
  2654. { // sign is required
  2655. __err |= ios_base::failbit;
  2656. return false;
  2657. }
  2658. if (__psn.size() == 0 && __nsn.size() == 0)
  2659. // locale has no way of specifying a sign. Use the initial value of __neg as a default
  2660. break;
  2661. __neg = (__nsn.size() == 0);
  2662. break;
  2663. case money_base::symbol:
  2664. {
  2665. bool __more_needed = __trailing_sign ||
  2666. (__p < 2) ||
  2667. (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
  2668. bool __sb = (__flags & ios_base::showbase) != 0;
  2669. if (__sb || __more_needed)
  2670. {
  2671. typename string_type::const_iterator __sym_space_end = __sym.begin();
  2672. if (__p > 0 && (__pat.field[__p - 1] == money_base::none ||
  2673. __pat.field[__p - 1] == money_base::space)) {
  2674. // Match spaces we've already read against spaces at
  2675. // the beginning of __sym.
  2676. while (__sym_space_end != __sym.end() &&
  2677. __ct.is(ctype_base::space, *__sym_space_end))
  2678. ++__sym_space_end;
  2679. const size_t __num_spaces = __sym_space_end - __sym.begin();
  2680. if (__num_spaces > __spaces.size() ||
  2681. !std::equal(__spaces.end() - __num_spaces, __spaces.end(),
  2682. __sym.begin())) {
  2683. // No match. Put __sym_space_end back at the
  2684. // beginning of __sym, which will prevent a
  2685. // match in the next loop.
  2686. __sym_space_end = __sym.begin();
  2687. }
  2688. }
  2689. typename string_type::const_iterator __sym_curr_char = __sym_space_end;
  2690. while (__sym_curr_char != __sym.end() && __b != __e &&
  2691. *__b == *__sym_curr_char) {
  2692. ++__b;
  2693. ++__sym_curr_char;
  2694. }
  2695. if (__sb && __sym_curr_char != __sym.end())
  2696. {
  2697. __err |= ios_base::failbit;
  2698. return false;
  2699. }
  2700. }
  2701. }
  2702. break;
  2703. case money_base::value:
  2704. {
  2705. unsigned __ng = 0;
  2706. for (; __b != __e; ++__b)
  2707. {
  2708. char_type __c = *__b;
  2709. if (__ct.is(ctype_base::digit, __c))
  2710. {
  2711. if (__wn == __we)
  2712. std::__double_or_nothing(__wb, __wn, __we);
  2713. *__wn++ = __c;
  2714. ++__ng;
  2715. }
  2716. else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
  2717. {
  2718. if (__gn == __ge)
  2719. std::__double_or_nothing(__gb, __gn, __ge);
  2720. *__gn++ = __ng;
  2721. __ng = 0;
  2722. }
  2723. else
  2724. break;
  2725. }
  2726. if (__gb.get() != __gn && __ng > 0)
  2727. {
  2728. if (__gn == __ge)
  2729. std::__double_or_nothing(__gb, __gn, __ge);
  2730. *__gn++ = __ng;
  2731. }
  2732. if (__fd > 0)
  2733. {
  2734. if (__b == __e || *__b != __dp)
  2735. {
  2736. __err |= ios_base::failbit;
  2737. return false;
  2738. }
  2739. for (++__b; __fd > 0; --__fd, ++__b)
  2740. {
  2741. if (__b == __e || !__ct.is(ctype_base::digit, *__b))
  2742. {
  2743. __err |= ios_base::failbit;
  2744. return false;
  2745. }
  2746. if (__wn == __we)
  2747. std::__double_or_nothing(__wb, __wn, __we);
  2748. *__wn++ = *__b;
  2749. }
  2750. }
  2751. if (__wn == __wb.get())
  2752. {
  2753. __err |= ios_base::failbit;
  2754. return false;
  2755. }
  2756. }
  2757. break;
  2758. }
  2759. }
  2760. if (__trailing_sign)
  2761. {
  2762. for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
  2763. {
  2764. if (__b == __e || *__b != (*__trailing_sign)[__i])
  2765. {
  2766. __err |= ios_base::failbit;
  2767. return false;
  2768. }
  2769. }
  2770. }
  2771. if (__gb.get() != __gn)
  2772. {
  2773. ios_base::iostate __et = ios_base::goodbit;
  2774. __check_grouping(__grp, __gb.get(), __gn, __et);
  2775. if (__et)
  2776. {
  2777. __err |= ios_base::failbit;
  2778. return false;
  2779. }
  2780. }
  2781. return true;
  2782. }
  2783. template <class _CharT, class _InputIterator>
  2784. _InputIterator
  2785. money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
  2786. bool __intl, ios_base& __iob,
  2787. ios_base::iostate& __err,
  2788. long double& __v) const
  2789. {
  2790. const int __bz = 100;
  2791. char_type __wbuf[__bz];
  2792. unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
  2793. char_type* __wn;
  2794. char_type* __we = __wbuf + __bz;
  2795. locale __loc = __iob.getloc();
  2796. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
  2797. bool __neg = false;
  2798. if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
  2799. __wb, __wn, __we))
  2800. {
  2801. const char __src[] = "0123456789";
  2802. char_type __atoms[sizeof(__src)-1];
  2803. __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
  2804. char __nbuf[__bz];
  2805. char* __nc = __nbuf;
  2806. unique_ptr<char, void(*)(void*)> __h(nullptr, free);
  2807. if (__wn - __wb.get() > __bz-2)
  2808. {
  2809. __h.reset((char*)malloc(static_cast<size_t>(__wn - __wb.get() + 2)));
  2810. if (__h.get() == nullptr)
  2811. __throw_bad_alloc();
  2812. __nc = __h.get();
  2813. }
  2814. if (__neg)
  2815. *__nc++ = '-';
  2816. for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
  2817. *__nc = __src[std::find(__atoms, _VSTD::end(__atoms), *__w) - __atoms];
  2818. *__nc = char();
  2819. if (sscanf(__nbuf, "%Lf", &__v) != 1)
  2820. __throw_runtime_error("money_get error");
  2821. }
  2822. if (__b == __e)
  2823. __err |= ios_base::eofbit;
  2824. return __b;
  2825. }
  2826. template <class _CharT, class _InputIterator>
  2827. _InputIterator
  2828. money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
  2829. bool __intl, ios_base& __iob,
  2830. ios_base::iostate& __err,
  2831. string_type& __v) const
  2832. {
  2833. const int __bz = 100;
  2834. char_type __wbuf[__bz];
  2835. unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
  2836. char_type* __wn;
  2837. char_type* __we = __wbuf + __bz;
  2838. locale __loc = __iob.getloc();
  2839. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
  2840. bool __neg = false;
  2841. if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
  2842. __wb, __wn, __we))
  2843. {
  2844. __v.clear();
  2845. if (__neg)
  2846. __v.push_back(__ct.widen('-'));
  2847. char_type __z = __ct.widen('0');
  2848. char_type* __w;
  2849. for (__w = __wb.get(); __w < __wn-1; ++__w)
  2850. if (*__w != __z)
  2851. break;
  2852. __v.append(__w, __wn);
  2853. }
  2854. if (__b == __e)
  2855. __err |= ios_base::eofbit;
  2856. return __b;
  2857. }
  2858. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<char>;
  2859. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  2860. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_get<wchar_t>;
  2861. #endif
  2862. // money_put
  2863. template <class _CharT>
  2864. class __money_put
  2865. {
  2866. protected:
  2867. typedef _CharT char_type;
  2868. typedef basic_string<char_type> string_type;
  2869. _LIBCPP_INLINE_VISIBILITY __money_put() {}
  2870. static void __gather_info(bool __intl, bool __neg, const locale& __loc,
  2871. money_base::pattern& __pat, char_type& __dp,
  2872. char_type& __ts, string& __grp,
  2873. string_type& __sym, string_type& __sn,
  2874. int& __fd);
  2875. static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
  2876. ios_base::fmtflags __flags,
  2877. const char_type* __db, const char_type* __de,
  2878. const ctype<char_type>& __ct, bool __neg,
  2879. const money_base::pattern& __pat, char_type __dp,
  2880. char_type __ts, const string& __grp,
  2881. const string_type& __sym, const string_type& __sn,
  2882. int __fd);
  2883. };
  2884. template <class _CharT>
  2885. void
  2886. __money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
  2887. money_base::pattern& __pat, char_type& __dp,
  2888. char_type& __ts, string& __grp,
  2889. string_type& __sym, string_type& __sn,
  2890. int& __fd)
  2891. {
  2892. if (__intl)
  2893. {
  2894. const moneypunct<char_type, true>& __mp =
  2895. std::use_facet<moneypunct<char_type, true> >(__loc);
  2896. if (__neg)
  2897. {
  2898. __pat = __mp.neg_format();
  2899. __sn = __mp.negative_sign();
  2900. }
  2901. else
  2902. {
  2903. __pat = __mp.pos_format();
  2904. __sn = __mp.positive_sign();
  2905. }
  2906. __dp = __mp.decimal_point();
  2907. __ts = __mp.thousands_sep();
  2908. __grp = __mp.grouping();
  2909. __sym = __mp.curr_symbol();
  2910. __fd = __mp.frac_digits();
  2911. }
  2912. else
  2913. {
  2914. const moneypunct<char_type, false>& __mp =
  2915. std::use_facet<moneypunct<char_type, false> >(__loc);
  2916. if (__neg)
  2917. {
  2918. __pat = __mp.neg_format();
  2919. __sn = __mp.negative_sign();
  2920. }
  2921. else
  2922. {
  2923. __pat = __mp.pos_format();
  2924. __sn = __mp.positive_sign();
  2925. }
  2926. __dp = __mp.decimal_point();
  2927. __ts = __mp.thousands_sep();
  2928. __grp = __mp.grouping();
  2929. __sym = __mp.curr_symbol();
  2930. __fd = __mp.frac_digits();
  2931. }
  2932. }
  2933. template <class _CharT>
  2934. void
  2935. __money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
  2936. ios_base::fmtflags __flags,
  2937. const char_type* __db, const char_type* __de,
  2938. const ctype<char_type>& __ct, bool __neg,
  2939. const money_base::pattern& __pat, char_type __dp,
  2940. char_type __ts, const string& __grp,
  2941. const string_type& __sym, const string_type& __sn,
  2942. int __fd)
  2943. {
  2944. __me = __mb;
  2945. for (char __p : __pat.field)
  2946. {
  2947. switch (__p)
  2948. {
  2949. case money_base::none:
  2950. __mi = __me;
  2951. break;
  2952. case money_base::space:
  2953. __mi = __me;
  2954. *__me++ = __ct.widen(' ');
  2955. break;
  2956. case money_base::sign:
  2957. if (!__sn.empty())
  2958. *__me++ = __sn[0];
  2959. break;
  2960. case money_base::symbol:
  2961. if (!__sym.empty() && (__flags & ios_base::showbase))
  2962. __me = _VSTD::copy(__sym.begin(), __sym.end(), __me);
  2963. break;
  2964. case money_base::value:
  2965. {
  2966. // remember start of value so we can reverse it
  2967. char_type* __t = __me;
  2968. // find beginning of digits
  2969. if (__neg)
  2970. ++__db;
  2971. // find end of digits
  2972. const char_type* __d;
  2973. for (__d = __db; __d < __de; ++__d)
  2974. if (!__ct.is(ctype_base::digit, *__d))
  2975. break;
  2976. // print fractional part
  2977. if (__fd > 0)
  2978. {
  2979. int __f;
  2980. for (__f = __fd; __d > __db && __f > 0; --__f)
  2981. *__me++ = *--__d;
  2982. char_type __z = __f > 0 ? __ct.widen('0') : char_type();
  2983. for (; __f > 0; --__f)
  2984. *__me++ = __z;
  2985. *__me++ = __dp;
  2986. }
  2987. // print units part
  2988. if (__d == __db)
  2989. {
  2990. *__me++ = __ct.widen('0');
  2991. }
  2992. else
  2993. {
  2994. unsigned __ng = 0;
  2995. unsigned __ig = 0;
  2996. unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
  2997. : static_cast<unsigned>(__grp[__ig]);
  2998. while (__d != __db)
  2999. {
  3000. if (__ng == __gl)
  3001. {
  3002. *__me++ = __ts;
  3003. __ng = 0;
  3004. if (++__ig < __grp.size())
  3005. __gl = __grp[__ig] == numeric_limits<char>::max() ?
  3006. numeric_limits<unsigned>::max() :
  3007. static_cast<unsigned>(__grp[__ig]);
  3008. }
  3009. *__me++ = *--__d;
  3010. ++__ng;
  3011. }
  3012. }
  3013. // reverse it
  3014. std::reverse(__t, __me);
  3015. }
  3016. break;
  3017. }
  3018. }
  3019. // print rest of sign, if any
  3020. if (__sn.size() > 1)
  3021. __me = _VSTD::copy(__sn.begin()+1, __sn.end(), __me);
  3022. // set alignment
  3023. if ((__flags & ios_base::adjustfield) == ios_base::left)
  3024. __mi = __me;
  3025. else if ((__flags & ios_base::adjustfield) != ios_base::internal)
  3026. __mi = __mb;
  3027. }
  3028. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<char>;
  3029. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3030. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __money_put<wchar_t>;
  3031. #endif
  3032. template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
  3033. class _LIBCPP_TEMPLATE_VIS money_put
  3034. : public locale::facet,
  3035. private __money_put<_CharT>
  3036. {
  3037. public:
  3038. typedef _CharT char_type;
  3039. typedef _OutputIterator iter_type;
  3040. typedef basic_string<char_type> string_type;
  3041. _LIBCPP_INLINE_VISIBILITY
  3042. explicit money_put(size_t __refs = 0)
  3043. : locale::facet(__refs) {}
  3044. _LIBCPP_INLINE_VISIBILITY
  3045. iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
  3046. long double __units) const
  3047. {
  3048. return do_put(__s, __intl, __iob, __fl, __units);
  3049. }
  3050. _LIBCPP_INLINE_VISIBILITY
  3051. iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
  3052. const string_type& __digits) const
  3053. {
  3054. return do_put(__s, __intl, __iob, __fl, __digits);
  3055. }
  3056. static locale::id id;
  3057. protected:
  3058. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~money_put() override {}
  3059. virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
  3060. char_type __fl, long double __units) const;
  3061. virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
  3062. char_type __fl, const string_type& __digits) const;
  3063. };
  3064. template <class _CharT, class _OutputIterator>
  3065. locale::id
  3066. money_put<_CharT, _OutputIterator>::id;
  3067. template <class _CharT, class _OutputIterator>
  3068. _OutputIterator
  3069. money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
  3070. ios_base& __iob, char_type __fl,
  3071. long double __units) const
  3072. {
  3073. // convert to char
  3074. const size_t __bs = 100;
  3075. char __buf[__bs];
  3076. char* __bb = __buf;
  3077. char_type __digits[__bs];
  3078. char_type* __db = __digits;
  3079. int __n = snprintf(__bb, __bs, "%.0Lf", __units);
  3080. unique_ptr<char, void(*)(void*)> __hn(nullptr, free);
  3081. unique_ptr<char_type, void(*)(void*)> __hd(0, free);
  3082. // secure memory for digit storage
  3083. if (static_cast<size_t>(__n) > __bs-1)
  3084. {
  3085. __n = __libcpp_asprintf_l(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units);
  3086. if (__n == -1)
  3087. __throw_bad_alloc();
  3088. __hn.reset(__bb);
  3089. __hd.reset((char_type*)malloc(static_cast<size_t>(__n) * sizeof(char_type)));
  3090. if (__hd == nullptr)
  3091. __throw_bad_alloc();
  3092. __db = __hd.get();
  3093. }
  3094. // gather info
  3095. locale __loc = __iob.getloc();
  3096. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
  3097. __ct.widen(__bb, __bb + __n, __db);
  3098. bool __neg = __n > 0 && __bb[0] == '-';
  3099. money_base::pattern __pat;
  3100. char_type __dp;
  3101. char_type __ts;
  3102. string __grp;
  3103. string_type __sym;
  3104. string_type __sn;
  3105. int __fd;
  3106. this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
  3107. // secure memory for formatting
  3108. char_type __mbuf[__bs];
  3109. char_type* __mb = __mbuf;
  3110. unique_ptr<char_type, void(*)(void*)> __hw(0, free);
  3111. size_t __exn = __n > __fd ?
  3112. (static_cast<size_t>(__n) - static_cast<size_t>(__fd)) * 2 +
  3113. __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
  3114. : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
  3115. if (__exn > __bs)
  3116. {
  3117. __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
  3118. __mb = __hw.get();
  3119. if (__mb == 0)
  3120. __throw_bad_alloc();
  3121. }
  3122. // format
  3123. char_type* __mi;
  3124. char_type* __me;
  3125. this->__format(__mb, __mi, __me, __iob.flags(),
  3126. __db, __db + __n, __ct,
  3127. __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
  3128. return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
  3129. }
  3130. template <class _CharT, class _OutputIterator>
  3131. _OutputIterator
  3132. money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
  3133. ios_base& __iob, char_type __fl,
  3134. const string_type& __digits) const
  3135. {
  3136. // gather info
  3137. locale __loc = __iob.getloc();
  3138. const ctype<char_type>& __ct = std::use_facet<ctype<char_type> >(__loc);
  3139. bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
  3140. money_base::pattern __pat;
  3141. char_type __dp;
  3142. char_type __ts;
  3143. string __grp;
  3144. string_type __sym;
  3145. string_type __sn;
  3146. int __fd;
  3147. this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
  3148. // secure memory for formatting
  3149. char_type __mbuf[100];
  3150. char_type* __mb = __mbuf;
  3151. unique_ptr<char_type, void(*)(void*)> __h(0, free);
  3152. size_t __exn = static_cast<int>(__digits.size()) > __fd ?
  3153. (__digits.size() - static_cast<size_t>(__fd)) * 2 +
  3154. __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 1
  3155. : __sn.size() + __sym.size() + static_cast<size_t>(__fd) + 2;
  3156. if (__exn > 100)
  3157. {
  3158. __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
  3159. __mb = __h.get();
  3160. if (__mb == 0)
  3161. __throw_bad_alloc();
  3162. }
  3163. // format
  3164. char_type* __mi;
  3165. char_type* __me;
  3166. this->__format(__mb, __mi, __me, __iob.flags(),
  3167. __digits.data(), __digits.data() + __digits.size(), __ct,
  3168. __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
  3169. return std::__pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
  3170. }
  3171. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<char>;
  3172. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3173. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS money_put<wchar_t>;
  3174. #endif
  3175. // messages
  3176. class _LIBCPP_EXPORTED_FROM_ABI messages_base
  3177. {
  3178. public:
  3179. typedef intptr_t catalog;
  3180. _LIBCPP_INLINE_VISIBILITY messages_base() {}
  3181. };
  3182. template <class _CharT>
  3183. class _LIBCPP_TEMPLATE_VIS messages
  3184. : public locale::facet,
  3185. public messages_base
  3186. {
  3187. public:
  3188. typedef _CharT char_type;
  3189. typedef basic_string<_CharT> string_type;
  3190. _LIBCPP_INLINE_VISIBILITY
  3191. explicit messages(size_t __refs = 0)
  3192. : locale::facet(__refs) {}
  3193. _LIBCPP_INLINE_VISIBILITY
  3194. catalog open(const basic_string<char>& __nm, const locale& __loc) const
  3195. {
  3196. return do_open(__nm, __loc);
  3197. }
  3198. _LIBCPP_INLINE_VISIBILITY
  3199. string_type get(catalog __c, int __set, int __msgid,
  3200. const string_type& __dflt) const
  3201. {
  3202. return do_get(__c, __set, __msgid, __dflt);
  3203. }
  3204. _LIBCPP_INLINE_VISIBILITY
  3205. void close(catalog __c) const
  3206. {
  3207. do_close(__c);
  3208. }
  3209. static locale::id id;
  3210. protected:
  3211. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages() override {}
  3212. virtual catalog do_open(const basic_string<char>&, const locale&) const;
  3213. virtual string_type do_get(catalog, int __set, int __msgid,
  3214. const string_type& __dflt) const;
  3215. virtual void do_close(catalog) const;
  3216. };
  3217. template <class _CharT>
  3218. locale::id
  3219. messages<_CharT>::id;
  3220. template <class _CharT>
  3221. typename messages<_CharT>::catalog
  3222. messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
  3223. {
  3224. #ifdef _LIBCPP_HAS_CATOPEN
  3225. return (catalog)catopen(__nm.c_str(), NL_CAT_LOCALE);
  3226. #else // !_LIBCPP_HAS_CATOPEN
  3227. (void)__nm;
  3228. return -1;
  3229. #endif // _LIBCPP_HAS_CATOPEN
  3230. }
  3231. template <class _CharT>
  3232. typename messages<_CharT>::string_type
  3233. messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
  3234. const string_type& __dflt) const
  3235. {
  3236. #ifdef _LIBCPP_HAS_CATOPEN
  3237. string __ndflt;
  3238. __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(std::back_inserter(__ndflt),
  3239. __dflt.c_str(),
  3240. __dflt.c_str() + __dflt.size());
  3241. nl_catd __cat = (nl_catd)__c;
  3242. static_assert(sizeof(catalog) >= sizeof(nl_catd), "Unexpected nl_catd type");
  3243. char* __n = catgets(__cat, __set, __msgid, __ndflt.c_str());
  3244. string_type __w;
  3245. __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(std::back_inserter(__w),
  3246. __n, __n + _VSTD::strlen(__n));
  3247. return __w;
  3248. #else // !_LIBCPP_HAS_CATOPEN
  3249. (void)__c;
  3250. (void)__set;
  3251. (void)__msgid;
  3252. return __dflt;
  3253. #endif // _LIBCPP_HAS_CATOPEN
  3254. }
  3255. template <class _CharT>
  3256. void
  3257. messages<_CharT>::do_close(catalog __c) const
  3258. {
  3259. #ifdef _LIBCPP_HAS_CATOPEN
  3260. catclose((nl_catd)__c);
  3261. #else // !_LIBCPP_HAS_CATOPEN
  3262. (void)__c;
  3263. #endif // _LIBCPP_HAS_CATOPEN
  3264. }
  3265. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<char>;
  3266. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3267. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages<wchar_t>;
  3268. #endif
  3269. template <class _CharT>
  3270. class _LIBCPP_TEMPLATE_VIS messages_byname
  3271. : public messages<_CharT>
  3272. {
  3273. public:
  3274. typedef messages_base::catalog catalog;
  3275. typedef basic_string<_CharT> string_type;
  3276. _LIBCPP_INLINE_VISIBILITY
  3277. explicit messages_byname(const char*, size_t __refs = 0)
  3278. : messages<_CharT>(__refs) {}
  3279. _LIBCPP_INLINE_VISIBILITY
  3280. explicit messages_byname(const string&, size_t __refs = 0)
  3281. : messages<_CharT>(__refs) {}
  3282. protected:
  3283. _LIBCPP_HIDE_FROM_ABI_VIRTUAL ~messages_byname() override {}
  3284. };
  3285. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<char>;
  3286. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  3287. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS messages_byname<wchar_t>;
  3288. #endif
  3289. template<class _Codecvt, class _Elem = wchar_t,
  3290. class _WideAlloc = allocator<_Elem>,
  3291. class _ByteAlloc = allocator<char> >
  3292. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wstring_convert
  3293. {
  3294. public:
  3295. typedef basic_string<char, char_traits<char>, _ByteAlloc> byte_string;
  3296. typedef basic_string<_Elem, char_traits<_Elem>, _WideAlloc> wide_string;
  3297. typedef typename _Codecvt::state_type state_type;
  3298. typedef typename wide_string::traits_type::int_type int_type;
  3299. private:
  3300. byte_string __byte_err_string_;
  3301. wide_string __wide_err_string_;
  3302. _Codecvt* __cvtptr_;
  3303. state_type __cvtstate_;
  3304. size_t __cvtcount_;
  3305. wstring_convert(const wstring_convert& __wc);
  3306. wstring_convert& operator=(const wstring_convert& __wc);
  3307. public:
  3308. #ifndef _LIBCPP_CXX03_LANG
  3309. _LIBCPP_INLINE_VISIBILITY
  3310. wstring_convert() : wstring_convert(new _Codecvt) {}
  3311. _LIBCPP_INLINE_VISIBILITY
  3312. explicit wstring_convert(_Codecvt* __pcvt);
  3313. #else
  3314. _LIBCPP_INLINE_VISIBILITY
  3315. _LIBCPP_EXPLICIT_SINCE_CXX14
  3316. wstring_convert(_Codecvt* __pcvt = new _Codecvt);
  3317. #endif
  3318. _LIBCPP_INLINE_VISIBILITY
  3319. wstring_convert(_Codecvt* __pcvt, state_type __state);
  3320. _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI wstring_convert(const byte_string& __byte_err,
  3321. const wide_string& __wide_err = wide_string());
  3322. #ifndef _LIBCPP_CXX03_LANG
  3323. _LIBCPP_INLINE_VISIBILITY
  3324. wstring_convert(wstring_convert&& __wc);
  3325. #endif
  3326. _LIBCPP_HIDE_FROM_ABI ~wstring_convert();
  3327. _LIBCPP_INLINE_VISIBILITY
  3328. wide_string from_bytes(char __byte)
  3329. {return from_bytes(&__byte, &__byte+1);}
  3330. _LIBCPP_INLINE_VISIBILITY
  3331. wide_string from_bytes(const char* __ptr)
  3332. {return from_bytes(__ptr, __ptr + char_traits<char>::length(__ptr));}
  3333. _LIBCPP_INLINE_VISIBILITY
  3334. wide_string from_bytes(const byte_string& __str)
  3335. {return from_bytes(__str.data(), __str.data() + __str.size());}
  3336. _LIBCPP_HIDE_FROM_ABI wide_string from_bytes(const char* __first, const char* __last);
  3337. _LIBCPP_INLINE_VISIBILITY
  3338. byte_string to_bytes(_Elem __wchar)
  3339. {return to_bytes(&__wchar, &__wchar+1);}
  3340. _LIBCPP_INLINE_VISIBILITY
  3341. byte_string to_bytes(const _Elem* __wptr)
  3342. {return to_bytes(__wptr, __wptr + char_traits<_Elem>::length(__wptr));}
  3343. _LIBCPP_INLINE_VISIBILITY
  3344. byte_string to_bytes(const wide_string& __wstr)
  3345. {return to_bytes(__wstr.data(), __wstr.data() + __wstr.size());}
  3346. _LIBCPP_HIDE_FROM_ABI byte_string to_bytes(const _Elem* __first, const _Elem* __last);
  3347. _LIBCPP_INLINE_VISIBILITY
  3348. size_t converted() const _NOEXCEPT {return __cvtcount_;}
  3349. _LIBCPP_INLINE_VISIBILITY
  3350. state_type state() const {return __cvtstate_;}
  3351. };
  3352. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  3353. template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
  3354. inline
  3355. wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
  3356. wstring_convert(_Codecvt* __pcvt)
  3357. : __cvtptr_(__pcvt), __cvtstate_(), __cvtcount_(0)
  3358. {
  3359. }
  3360. _LIBCPP_SUPPRESS_DEPRECATED_POP
  3361. template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
  3362. inline
  3363. wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
  3364. wstring_convert(_Codecvt* __pcvt, state_type __state)
  3365. : __cvtptr_(__pcvt), __cvtstate_(__state), __cvtcount_(0)
  3366. {
  3367. }
  3368. template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
  3369. wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
  3370. wstring_convert(const byte_string& __byte_err, const wide_string& __wide_err)
  3371. : __byte_err_string_(__byte_err), __wide_err_string_(__wide_err),
  3372. __cvtstate_(), __cvtcount_(0)
  3373. {
  3374. __cvtptr_ = new _Codecvt;
  3375. }
  3376. #ifndef _LIBCPP_CXX03_LANG
  3377. template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
  3378. inline
  3379. wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
  3380. wstring_convert(wstring_convert&& __wc)
  3381. : __byte_err_string_(_VSTD::move(__wc.__byte_err_string_)),
  3382. __wide_err_string_(_VSTD::move(__wc.__wide_err_string_)),
  3383. __cvtptr_(__wc.__cvtptr_),
  3384. __cvtstate_(__wc.__cvtstate_), __cvtcount_(__wc.__cvtcount_)
  3385. {
  3386. __wc.__cvtptr_ = nullptr;
  3387. }
  3388. #endif // _LIBCPP_CXX03_LANG
  3389. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  3390. template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
  3391. wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::~wstring_convert()
  3392. {
  3393. delete __cvtptr_;
  3394. }
  3395. template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
  3396. typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::wide_string
  3397. wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
  3398. from_bytes(const char* __frm, const char* __frm_end)
  3399. {
  3400. _LIBCPP_SUPPRESS_DEPRECATED_POP
  3401. __cvtcount_ = 0;
  3402. if (__cvtptr_ != nullptr)
  3403. {
  3404. wide_string __ws(2*(__frm_end - __frm), _Elem());
  3405. if (__frm != __frm_end)
  3406. __ws.resize(__ws.capacity());
  3407. codecvt_base::result __r = codecvt_base::ok;
  3408. state_type __st = __cvtstate_;
  3409. if (__frm != __frm_end)
  3410. {
  3411. _Elem* __to = &__ws[0];
  3412. _Elem* __to_end = __to + __ws.size();
  3413. const char* __frm_nxt;
  3414. do
  3415. {
  3416. _Elem* __to_nxt;
  3417. __r = __cvtptr_->in(__st, __frm, __frm_end, __frm_nxt,
  3418. __to, __to_end, __to_nxt);
  3419. __cvtcount_ += __frm_nxt - __frm;
  3420. if (__frm_nxt == __frm)
  3421. {
  3422. __r = codecvt_base::error;
  3423. }
  3424. else if (__r == codecvt_base::noconv)
  3425. {
  3426. __ws.resize(__to - &__ws[0]);
  3427. // This only gets executed if _Elem is char
  3428. __ws.append((const _Elem*)__frm, (const _Elem*)__frm_end);
  3429. __frm = __frm_nxt;
  3430. __r = codecvt_base::ok;
  3431. }
  3432. else if (__r == codecvt_base::ok)
  3433. {
  3434. __ws.resize(__to_nxt - &__ws[0]);
  3435. __frm = __frm_nxt;
  3436. }
  3437. else if (__r == codecvt_base::partial)
  3438. {
  3439. ptrdiff_t __s = __to_nxt - &__ws[0];
  3440. __ws.resize(2 * __s);
  3441. __to = &__ws[0] + __s;
  3442. __to_end = &__ws[0] + __ws.size();
  3443. __frm = __frm_nxt;
  3444. }
  3445. } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
  3446. }
  3447. if (__r == codecvt_base::ok)
  3448. return __ws;
  3449. }
  3450. if (__wide_err_string_.empty())
  3451. __throw_range_error("wstring_convert: from_bytes error");
  3452. return __wide_err_string_;
  3453. }
  3454. template<class _Codecvt, class _Elem, class _WideAlloc, class _ByteAlloc>
  3455. typename wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::byte_string
  3456. wstring_convert<_Codecvt, _Elem, _WideAlloc, _ByteAlloc>::
  3457. to_bytes(const _Elem* __frm, const _Elem* __frm_end)
  3458. {
  3459. __cvtcount_ = 0;
  3460. if (__cvtptr_ != nullptr)
  3461. {
  3462. byte_string __bs(2*(__frm_end - __frm), char());
  3463. if (__frm != __frm_end)
  3464. __bs.resize(__bs.capacity());
  3465. codecvt_base::result __r = codecvt_base::ok;
  3466. state_type __st = __cvtstate_;
  3467. if (__frm != __frm_end)
  3468. {
  3469. char* __to = &__bs[0];
  3470. char* __to_end = __to + __bs.size();
  3471. const _Elem* __frm_nxt;
  3472. do
  3473. {
  3474. char* __to_nxt;
  3475. __r = __cvtptr_->out(__st, __frm, __frm_end, __frm_nxt,
  3476. __to, __to_end, __to_nxt);
  3477. __cvtcount_ += __frm_nxt - __frm;
  3478. if (__frm_nxt == __frm)
  3479. {
  3480. __r = codecvt_base::error;
  3481. }
  3482. else if (__r == codecvt_base::noconv)
  3483. {
  3484. __bs.resize(__to - &__bs[0]);
  3485. // This only gets executed if _Elem is char
  3486. __bs.append((const char*)__frm, (const char*)__frm_end);
  3487. __frm = __frm_nxt;
  3488. __r = codecvt_base::ok;
  3489. }
  3490. else if (__r == codecvt_base::ok)
  3491. {
  3492. __bs.resize(__to_nxt - &__bs[0]);
  3493. __frm = __frm_nxt;
  3494. }
  3495. else if (__r == codecvt_base::partial)
  3496. {
  3497. ptrdiff_t __s = __to_nxt - &__bs[0];
  3498. __bs.resize(2 * __s);
  3499. __to = &__bs[0] + __s;
  3500. __to_end = &__bs[0] + __bs.size();
  3501. __frm = __frm_nxt;
  3502. }
  3503. } while (__r == codecvt_base::partial && __frm_nxt < __frm_end);
  3504. }
  3505. if (__r == codecvt_base::ok)
  3506. {
  3507. size_t __s = __bs.size();
  3508. __bs.resize(__bs.capacity());
  3509. char* __to = &__bs[0] + __s;
  3510. char* __to_end = __to + __bs.size();
  3511. do
  3512. {
  3513. char* __to_nxt;
  3514. __r = __cvtptr_->unshift(__st, __to, __to_end, __to_nxt);
  3515. if (__r == codecvt_base::noconv)
  3516. {
  3517. __bs.resize(__to - &__bs[0]);
  3518. __r = codecvt_base::ok;
  3519. }
  3520. else if (__r == codecvt_base::ok)
  3521. {
  3522. __bs.resize(__to_nxt - &__bs[0]);
  3523. }
  3524. else if (__r == codecvt_base::partial)
  3525. {
  3526. ptrdiff_t __sp = __to_nxt - &__bs[0];
  3527. __bs.resize(2 * __sp);
  3528. __to = &__bs[0] + __sp;
  3529. __to_end = &__bs[0] + __bs.size();
  3530. }
  3531. } while (__r == codecvt_base::partial);
  3532. if (__r == codecvt_base::ok)
  3533. return __bs;
  3534. }
  3535. }
  3536. if (__byte_err_string_.empty())
  3537. __throw_range_error("wstring_convert: to_bytes error");
  3538. return __byte_err_string_;
  3539. }
  3540. template <class _Codecvt, class _Elem = wchar_t, class _Tr = char_traits<_Elem> >
  3541. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 wbuffer_convert
  3542. : public basic_streambuf<_Elem, _Tr>
  3543. {
  3544. public:
  3545. // types:
  3546. typedef _Elem char_type;
  3547. typedef _Tr traits_type;
  3548. typedef typename traits_type::int_type int_type;
  3549. typedef typename traits_type::pos_type pos_type;
  3550. typedef typename traits_type::off_type off_type;
  3551. typedef typename _Codecvt::state_type state_type;
  3552. private:
  3553. char* __extbuf_;
  3554. const char* __extbufnext_;
  3555. const char* __extbufend_;
  3556. char __extbuf_min_[8];
  3557. size_t __ebs_;
  3558. char_type* __intbuf_;
  3559. size_t __ibs_;
  3560. streambuf* __bufptr_;
  3561. _Codecvt* __cv_;
  3562. state_type __st_;
  3563. ios_base::openmode __cm_;
  3564. bool __owns_eb_;
  3565. bool __owns_ib_;
  3566. bool __always_noconv_;
  3567. wbuffer_convert(const wbuffer_convert&);
  3568. wbuffer_convert& operator=(const wbuffer_convert&);
  3569. public:
  3570. #ifndef _LIBCPP_CXX03_LANG
  3571. _LIBCPP_HIDE_FROM_ABI wbuffer_convert() : wbuffer_convert(nullptr) {}
  3572. explicit _LIBCPP_HIDE_FROM_ABI wbuffer_convert(streambuf* __bytebuf,
  3573. _Codecvt* __pcvt = new _Codecvt,
  3574. state_type __state = state_type());
  3575. #else
  3576. _LIBCPP_EXPLICIT_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI
  3577. wbuffer_convert(streambuf* __bytebuf = nullptr,
  3578. _Codecvt* __pcvt = new _Codecvt,
  3579. state_type __state = state_type());
  3580. #endif
  3581. _LIBCPP_HIDE_FROM_ABI ~wbuffer_convert();
  3582. _LIBCPP_INLINE_VISIBILITY
  3583. streambuf* rdbuf() const {return __bufptr_;}
  3584. _LIBCPP_INLINE_VISIBILITY
  3585. streambuf* rdbuf(streambuf* __bytebuf)
  3586. {
  3587. streambuf* __r = __bufptr_;
  3588. __bufptr_ = __bytebuf;
  3589. return __r;
  3590. }
  3591. _LIBCPP_INLINE_VISIBILITY
  3592. state_type state() const {return __st_;}
  3593. protected:
  3594. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type underflow();
  3595. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type pbackfail(int_type __c = traits_type::eof());
  3596. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int_type overflow (int_type __c = traits_type::eof());
  3597. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s,
  3598. streamsize __n);
  3599. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
  3600. ios_base::openmode __wch = ios_base::in | ios_base::out);
  3601. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual pos_type seekpos(pos_type __sp,
  3602. ios_base::openmode __wch = ios_base::in | ios_base::out);
  3603. _LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual int sync();
  3604. private:
  3605. _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __read_mode();
  3606. _LIBCPP_HIDE_FROM_ABI_VIRTUAL void __write_mode();
  3607. _LIBCPP_HIDE_FROM_ABI_VIRTUAL wbuffer_convert* __close();
  3608. };
  3609. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  3610. template <class _Codecvt, class _Elem, class _Tr>
  3611. wbuffer_convert<_Codecvt, _Elem, _Tr>::
  3612. wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt, state_type __state)
  3613. : __extbuf_(nullptr),
  3614. __extbufnext_(nullptr),
  3615. __extbufend_(nullptr),
  3616. __ebs_(0),
  3617. __intbuf_(0),
  3618. __ibs_(0),
  3619. __bufptr_(__bytebuf),
  3620. __cv_(__pcvt),
  3621. __st_(__state),
  3622. __cm_(0),
  3623. __owns_eb_(false),
  3624. __owns_ib_(false),
  3625. __always_noconv_(__cv_ ? __cv_->always_noconv() : false)
  3626. {
  3627. setbuf(0, 4096);
  3628. }
  3629. template <class _Codecvt, class _Elem, class _Tr>
  3630. wbuffer_convert<_Codecvt, _Elem, _Tr>::~wbuffer_convert()
  3631. {
  3632. __close();
  3633. delete __cv_;
  3634. if (__owns_eb_)
  3635. delete [] __extbuf_;
  3636. if (__owns_ib_)
  3637. delete [] __intbuf_;
  3638. }
  3639. template <class _Codecvt, class _Elem, class _Tr>
  3640. typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
  3641. wbuffer_convert<_Codecvt, _Elem, _Tr>::underflow()
  3642. {
  3643. _LIBCPP_SUPPRESS_DEPRECATED_POP
  3644. if (__cv_ == 0 || __bufptr_ == 0)
  3645. return traits_type::eof();
  3646. bool __initial = __read_mode();
  3647. char_type __1buf;
  3648. if (this->gptr() == 0)
  3649. this->setg(&__1buf, &__1buf+1, &__1buf+1);
  3650. const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
  3651. int_type __c = traits_type::eof();
  3652. if (this->gptr() == this->egptr())
  3653. {
  3654. _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
  3655. if (__always_noconv_)
  3656. {
  3657. streamsize __nmemb = static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz);
  3658. __nmemb = __bufptr_->sgetn((char*)this->eback() + __unget_sz, __nmemb);
  3659. if (__nmemb != 0)
  3660. {
  3661. this->setg(this->eback(),
  3662. this->eback() + __unget_sz,
  3663. this->eback() + __unget_sz + __nmemb);
  3664. __c = *this->gptr();
  3665. }
  3666. }
  3667. else
  3668. {
  3669. if (__extbufend_ != __extbufnext_) {
  3670. _LIBCPP_ASSERT_UNCATEGORIZED(__extbufnext_ != nullptr, "underflow moving from nullptr");
  3671. _LIBCPP_ASSERT_UNCATEGORIZED(__extbuf_ != nullptr, "underflow moving into nullptr");
  3672. _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
  3673. }
  3674. __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
  3675. __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
  3676. streamsize __nmemb = _VSTD::min(static_cast<streamsize>(this->egptr() - this->eback() - __unget_sz),
  3677. static_cast<streamsize>(__extbufend_ - __extbufnext_));
  3678. codecvt_base::result __r;
  3679. // FIXME: Do we ever need to restore the state here?
  3680. //state_type __svs = __st_;
  3681. streamsize __nr = __bufptr_->sgetn(const_cast<char*>(__extbufnext_), __nmemb);
  3682. if (__nr != 0)
  3683. {
  3684. __extbufend_ = __extbufnext_ + __nr;
  3685. char_type* __inext;
  3686. __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
  3687. this->eback() + __unget_sz,
  3688. this->egptr(), __inext);
  3689. if (__r == codecvt_base::noconv)
  3690. {
  3691. this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
  3692. (char_type*) const_cast<char *>(__extbufend_));
  3693. __c = *this->gptr();
  3694. }
  3695. else if (__inext != this->eback() + __unget_sz)
  3696. {
  3697. this->setg(this->eback(), this->eback() + __unget_sz, __inext);
  3698. __c = *this->gptr();
  3699. }
  3700. }
  3701. }
  3702. }
  3703. else
  3704. __c = *this->gptr();
  3705. if (this->eback() == &__1buf)
  3706. this->setg(0, 0, 0);
  3707. return __c;
  3708. }
  3709. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  3710. template <class _Codecvt, class _Elem, class _Tr>
  3711. typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
  3712. wbuffer_convert<_Codecvt, _Elem, _Tr>::pbackfail(int_type __c)
  3713. {
  3714. _LIBCPP_SUPPRESS_DEPRECATED_POP
  3715. if (__cv_ != 0 && __bufptr_ != 0 && this->eback() < this->gptr())
  3716. {
  3717. if (traits_type::eq_int_type(__c, traits_type::eof()))
  3718. {
  3719. this->gbump(-1);
  3720. return traits_type::not_eof(__c);
  3721. }
  3722. if (traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
  3723. {
  3724. this->gbump(-1);
  3725. *this->gptr() = traits_type::to_char_type(__c);
  3726. return __c;
  3727. }
  3728. }
  3729. return traits_type::eof();
  3730. }
  3731. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  3732. template <class _Codecvt, class _Elem, class _Tr>
  3733. typename wbuffer_convert<_Codecvt, _Elem, _Tr>::int_type
  3734. wbuffer_convert<_Codecvt, _Elem, _Tr>::overflow(int_type __c)
  3735. {
  3736. _LIBCPP_SUPPRESS_DEPRECATED_POP
  3737. if (__cv_ == 0 || __bufptr_ == 0)
  3738. return traits_type::eof();
  3739. __write_mode();
  3740. char_type __1buf;
  3741. char_type* __pb_save = this->pbase();
  3742. char_type* __epb_save = this->epptr();
  3743. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  3744. {
  3745. if (this->pptr() == 0)
  3746. this->setp(&__1buf, &__1buf+1);
  3747. *this->pptr() = traits_type::to_char_type(__c);
  3748. this->pbump(1);
  3749. }
  3750. if (this->pptr() != this->pbase())
  3751. {
  3752. if (__always_noconv_)
  3753. {
  3754. streamsize __nmemb = static_cast<streamsize>(this->pptr() - this->pbase());
  3755. if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
  3756. return traits_type::eof();
  3757. }
  3758. else
  3759. {
  3760. char* __extbe = __extbuf_;
  3761. codecvt_base::result __r;
  3762. do
  3763. {
  3764. const char_type* __e;
  3765. __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
  3766. __extbuf_, __extbuf_ + __ebs_, __extbe);
  3767. if (__e == this->pbase())
  3768. return traits_type::eof();
  3769. if (__r == codecvt_base::noconv)
  3770. {
  3771. streamsize __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
  3772. if (__bufptr_->sputn((const char*)this->pbase(), __nmemb) != __nmemb)
  3773. return traits_type::eof();
  3774. }
  3775. else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
  3776. {
  3777. streamsize __nmemb = static_cast<size_t>(__extbe - __extbuf_);
  3778. if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
  3779. return traits_type::eof();
  3780. if (__r == codecvt_base::partial)
  3781. {
  3782. this->setp(const_cast<char_type *>(__e), this->pptr());
  3783. this->__pbump(this->epptr() - this->pbase());
  3784. }
  3785. }
  3786. else
  3787. return traits_type::eof();
  3788. } while (__r == codecvt_base::partial);
  3789. }
  3790. this->setp(__pb_save, __epb_save);
  3791. }
  3792. return traits_type::not_eof(__c);
  3793. }
  3794. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  3795. template <class _Codecvt, class _Elem, class _Tr>
  3796. basic_streambuf<_Elem, _Tr>*
  3797. wbuffer_convert<_Codecvt, _Elem, _Tr>::setbuf(char_type* __s, streamsize __n)
  3798. {
  3799. _LIBCPP_SUPPRESS_DEPRECATED_POP
  3800. this->setg(0, 0, 0);
  3801. this->setp(0, 0);
  3802. if (__owns_eb_)
  3803. delete [] __extbuf_;
  3804. if (__owns_ib_)
  3805. delete [] __intbuf_;
  3806. __ebs_ = __n;
  3807. if (__ebs_ > sizeof(__extbuf_min_))
  3808. {
  3809. if (__always_noconv_ && __s)
  3810. {
  3811. __extbuf_ = (char*)__s;
  3812. __owns_eb_ = false;
  3813. }
  3814. else
  3815. {
  3816. __extbuf_ = new char[__ebs_];
  3817. __owns_eb_ = true;
  3818. }
  3819. }
  3820. else
  3821. {
  3822. __extbuf_ = __extbuf_min_;
  3823. __ebs_ = sizeof(__extbuf_min_);
  3824. __owns_eb_ = false;
  3825. }
  3826. if (!__always_noconv_)
  3827. {
  3828. __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
  3829. if (__s && __ibs_ >= sizeof(__extbuf_min_))
  3830. {
  3831. __intbuf_ = __s;
  3832. __owns_ib_ = false;
  3833. }
  3834. else
  3835. {
  3836. __intbuf_ = new char_type[__ibs_];
  3837. __owns_ib_ = true;
  3838. }
  3839. }
  3840. else
  3841. {
  3842. __ibs_ = 0;
  3843. __intbuf_ = 0;
  3844. __owns_ib_ = false;
  3845. }
  3846. return this;
  3847. }
  3848. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  3849. template <class _Codecvt, class _Elem, class _Tr>
  3850. typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
  3851. wbuffer_convert<_Codecvt, _Elem, _Tr>::seekoff(off_type __off, ios_base::seekdir __way,
  3852. ios_base::openmode __om)
  3853. {
  3854. int __width = __cv_->encoding();
  3855. if (__cv_ == 0 || __bufptr_ == 0 || (__width <= 0 && __off != 0) || sync())
  3856. return pos_type(off_type(-1));
  3857. // __width > 0 || __off == 0, now check __way
  3858. if (__way != ios_base::beg && __way != ios_base::cur && __way != ios_base::end)
  3859. return pos_type(off_type(-1));
  3860. pos_type __r = __bufptr_->pubseekoff(__width * __off, __way, __om);
  3861. __r.state(__st_);
  3862. return __r;
  3863. }
  3864. template <class _Codecvt, class _Elem, class _Tr>
  3865. typename wbuffer_convert<_Codecvt, _Elem, _Tr>::pos_type
  3866. wbuffer_convert<_Codecvt, _Elem, _Tr>::seekpos(pos_type __sp, ios_base::openmode __wch)
  3867. {
  3868. if (__cv_ == 0 || __bufptr_ == 0 || sync())
  3869. return pos_type(off_type(-1));
  3870. if (__bufptr_->pubseekpos(__sp, __wch) == pos_type(off_type(-1)))
  3871. return pos_type(off_type(-1));
  3872. return __sp;
  3873. }
  3874. template <class _Codecvt, class _Elem, class _Tr>
  3875. int
  3876. wbuffer_convert<_Codecvt, _Elem, _Tr>::sync()
  3877. {
  3878. _LIBCPP_SUPPRESS_DEPRECATED_POP
  3879. if (__cv_ == 0 || __bufptr_ == 0)
  3880. return 0;
  3881. if (__cm_ & ios_base::out)
  3882. {
  3883. if (this->pptr() != this->pbase())
  3884. if (overflow() == traits_type::eof())
  3885. return -1;
  3886. codecvt_base::result __r;
  3887. do
  3888. {
  3889. char* __extbe;
  3890. __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
  3891. streamsize __nmemb = static_cast<streamsize>(__extbe - __extbuf_);
  3892. if (__bufptr_->sputn(__extbuf_, __nmemb) != __nmemb)
  3893. return -1;
  3894. } while (__r == codecvt_base::partial);
  3895. if (__r == codecvt_base::error)
  3896. return -1;
  3897. if (__bufptr_->pubsync())
  3898. return -1;
  3899. }
  3900. else if (__cm_ & ios_base::in)
  3901. {
  3902. off_type __c;
  3903. if (__always_noconv_)
  3904. __c = this->egptr() - this->gptr();
  3905. else
  3906. {
  3907. int __width = __cv_->encoding();
  3908. __c = __extbufend_ - __extbufnext_;
  3909. if (__width > 0)
  3910. __c += __width * (this->egptr() - this->gptr());
  3911. else
  3912. {
  3913. if (this->gptr() != this->egptr())
  3914. {
  3915. std::reverse(this->gptr(), this->egptr());
  3916. codecvt_base::result __r;
  3917. const char_type* __e = this->gptr();
  3918. char* __extbe;
  3919. do
  3920. {
  3921. __r = __cv_->out(__st_, __e, this->egptr(), __e,
  3922. __extbuf_, __extbuf_ + __ebs_, __extbe);
  3923. switch (__r)
  3924. {
  3925. case codecvt_base::noconv:
  3926. __c += this->egptr() - this->gptr();
  3927. break;
  3928. case codecvt_base::ok:
  3929. case codecvt_base::partial:
  3930. __c += __extbe - __extbuf_;
  3931. break;
  3932. default:
  3933. return -1;
  3934. }
  3935. } while (__r == codecvt_base::partial);
  3936. }
  3937. }
  3938. }
  3939. if (__bufptr_->pubseekoff(-__c, ios_base::cur, __cm_) == pos_type(off_type(-1)))
  3940. return -1;
  3941. this->setg(0, 0, 0);
  3942. __cm_ = 0;
  3943. }
  3944. return 0;
  3945. }
  3946. _LIBCPP_SUPPRESS_DEPRECATED_PUSH
  3947. template <class _Codecvt, class _Elem, class _Tr>
  3948. bool
  3949. wbuffer_convert<_Codecvt, _Elem, _Tr>::__read_mode()
  3950. {
  3951. if (!(__cm_ & ios_base::in))
  3952. {
  3953. this->setp(0, 0);
  3954. if (__always_noconv_)
  3955. this->setg((char_type*)__extbuf_,
  3956. (char_type*)__extbuf_ + __ebs_,
  3957. (char_type*)__extbuf_ + __ebs_);
  3958. else
  3959. this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
  3960. __cm_ = ios_base::in;
  3961. return true;
  3962. }
  3963. return false;
  3964. }
  3965. template <class _Codecvt, class _Elem, class _Tr>
  3966. void
  3967. wbuffer_convert<_Codecvt, _Elem, _Tr>::__write_mode()
  3968. {
  3969. if (!(__cm_ & ios_base::out))
  3970. {
  3971. this->setg(0, 0, 0);
  3972. if (__ebs_ > sizeof(__extbuf_min_))
  3973. {
  3974. if (__always_noconv_)
  3975. this->setp((char_type*)__extbuf_,
  3976. (char_type*)__extbuf_ + (__ebs_ - 1));
  3977. else
  3978. this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
  3979. }
  3980. else
  3981. this->setp(0, 0);
  3982. __cm_ = ios_base::out;
  3983. }
  3984. }
  3985. template <class _Codecvt, class _Elem, class _Tr>
  3986. wbuffer_convert<_Codecvt, _Elem, _Tr>*
  3987. wbuffer_convert<_Codecvt, _Elem, _Tr>::__close()
  3988. {
  3989. wbuffer_convert* __rt = nullptr;
  3990. if (__cv_ != nullptr && __bufptr_ != nullptr)
  3991. {
  3992. __rt = this;
  3993. if ((__cm_ & ios_base::out) && sync())
  3994. __rt = nullptr;
  3995. }
  3996. return __rt;
  3997. }
  3998. _LIBCPP_SUPPRESS_DEPRECATED_POP
  3999. _LIBCPP_END_NAMESPACE_STD
  4000. _LIBCPP_POP_MACROS
  4001. // NOLINTEND(libcpp-robust-against-adl)
  4002. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  4003. # include <atomic>
  4004. # include <concepts>
  4005. # include <cstdarg>
  4006. # include <iterator>
  4007. # include <mutex>
  4008. # include <stdexcept>
  4009. # include <type_traits>
  4010. # include <typeinfo>
  4011. #endif
  4012. #endif // _LIBCPP_LOCALE