locale 151 KB

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