locale 132 KB

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