locale 150 KB

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