locale 151 KB

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