locale 145 KB

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