locale_facets_nonio.tcc 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. // Locale support -*- C++ -*-
  2. // Copyright (C) 2007-2021 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/locale_facets_nonio.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{locale}
  23. */
  24. #ifndef _LOCALE_FACETS_NONIO_TCC
  25. #define _LOCALE_FACETS_NONIO_TCC 1
  26. #pragma GCC system_header
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. template<typename _CharT, bool _Intl>
  31. struct __use_cache<__moneypunct_cache<_CharT, _Intl> >
  32. {
  33. const __moneypunct_cache<_CharT, _Intl>*
  34. operator() (const locale& __loc) const
  35. {
  36. const size_t __i = moneypunct<_CharT, _Intl>::id._M_id();
  37. const locale::facet** __caches = __loc._M_impl->_M_caches;
  38. if (!__caches[__i])
  39. {
  40. __moneypunct_cache<_CharT, _Intl>* __tmp = 0;
  41. __try
  42. {
  43. __tmp = new __moneypunct_cache<_CharT, _Intl>;
  44. __tmp->_M_cache(__loc);
  45. }
  46. __catch(...)
  47. {
  48. delete __tmp;
  49. __throw_exception_again;
  50. }
  51. __loc._M_impl->_M_install_cache(__tmp, __i);
  52. }
  53. return static_cast<
  54. const __moneypunct_cache<_CharT, _Intl>*>(__caches[__i]);
  55. }
  56. };
  57. template<typename _CharT, bool _Intl>
  58. void
  59. __moneypunct_cache<_CharT, _Intl>::_M_cache(const locale& __loc)
  60. {
  61. const moneypunct<_CharT, _Intl>& __mp =
  62. use_facet<moneypunct<_CharT, _Intl> >(__loc);
  63. _M_decimal_point = __mp.decimal_point();
  64. _M_thousands_sep = __mp.thousands_sep();
  65. _M_frac_digits = __mp.frac_digits();
  66. char* __grouping = 0;
  67. _CharT* __curr_symbol = 0;
  68. _CharT* __positive_sign = 0;
  69. _CharT* __negative_sign = 0;
  70. __try
  71. {
  72. const string& __g = __mp.grouping();
  73. _M_grouping_size = __g.size();
  74. __grouping = new char[_M_grouping_size];
  75. __g.copy(__grouping, _M_grouping_size);
  76. _M_use_grouping = (_M_grouping_size
  77. && static_cast<signed char>(__grouping[0]) > 0
  78. && (__grouping[0]
  79. != __gnu_cxx::__numeric_traits<char>::__max));
  80. const basic_string<_CharT>& __cs = __mp.curr_symbol();
  81. _M_curr_symbol_size = __cs.size();
  82. __curr_symbol = new _CharT[_M_curr_symbol_size];
  83. __cs.copy(__curr_symbol, _M_curr_symbol_size);
  84. const basic_string<_CharT>& __ps = __mp.positive_sign();
  85. _M_positive_sign_size = __ps.size();
  86. __positive_sign = new _CharT[_M_positive_sign_size];
  87. __ps.copy(__positive_sign, _M_positive_sign_size);
  88. const basic_string<_CharT>& __ns = __mp.negative_sign();
  89. _M_negative_sign_size = __ns.size();
  90. __negative_sign = new _CharT[_M_negative_sign_size];
  91. __ns.copy(__negative_sign, _M_negative_sign_size);
  92. _M_pos_format = __mp.pos_format();
  93. _M_neg_format = __mp.neg_format();
  94. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
  95. __ct.widen(money_base::_S_atoms,
  96. money_base::_S_atoms + money_base::_S_end, _M_atoms);
  97. _M_grouping = __grouping;
  98. _M_curr_symbol = __curr_symbol;
  99. _M_positive_sign = __positive_sign;
  100. _M_negative_sign = __negative_sign;
  101. _M_allocated = true;
  102. }
  103. __catch(...)
  104. {
  105. delete [] __grouping;
  106. delete [] __curr_symbol;
  107. delete [] __positive_sign;
  108. delete [] __negative_sign;
  109. __throw_exception_again;
  110. }
  111. }
  112. _GLIBCXX_BEGIN_NAMESPACE_LDBL_OR_CXX11
  113. template<typename _CharT, typename _InIter>
  114. template<bool _Intl>
  115. _InIter
  116. money_get<_CharT, _InIter>::
  117. _M_extract(iter_type __beg, iter_type __end, ios_base& __io,
  118. ios_base::iostate& __err, string& __units) const
  119. {
  120. typedef char_traits<_CharT> __traits_type;
  121. typedef typename string_type::size_type size_type;
  122. typedef money_base::part part;
  123. typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
  124. const locale& __loc = __io._M_getloc();
  125. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  126. __use_cache<__cache_type> __uc;
  127. const __cache_type* __lc = __uc(__loc);
  128. const char_type* __lit = __lc->_M_atoms;
  129. // Deduced sign.
  130. bool __negative = false;
  131. // Sign size.
  132. size_type __sign_size = 0;
  133. // True if sign is mandatory.
  134. const bool __mandatory_sign = (__lc->_M_positive_sign_size
  135. && __lc->_M_negative_sign_size);
  136. // String of grouping info from thousands_sep plucked from __units.
  137. string __grouping_tmp;
  138. if (__lc->_M_use_grouping)
  139. __grouping_tmp.reserve(32);
  140. // Last position before the decimal point.
  141. int __last_pos = 0;
  142. // Separator positions, then, possibly, fractional digits.
  143. int __n = 0;
  144. // If input iterator is in a valid state.
  145. bool __testvalid = true;
  146. // Flag marking when a decimal point is found.
  147. bool __testdecfound = false;
  148. // The tentative returned string is stored here.
  149. string __res;
  150. __res.reserve(32);
  151. const char_type* __lit_zero = __lit + money_base::_S_zero;
  152. const money_base::pattern __p = __lc->_M_neg_format;
  153. for (int __i = 0; __i < 4 && __testvalid; ++__i)
  154. {
  155. const part __which = static_cast<part>(__p.field[__i]);
  156. switch (__which)
  157. {
  158. case money_base::symbol:
  159. // According to 22.2.6.1.2, p2, symbol is required
  160. // if (__io.flags() & ios_base::showbase), otherwise
  161. // is optional and consumed only if other characters
  162. // are needed to complete the format.
  163. if (__io.flags() & ios_base::showbase || __sign_size > 1
  164. || __i == 0
  165. || (__i == 1 && (__mandatory_sign
  166. || (static_cast<part>(__p.field[0])
  167. == money_base::sign)
  168. || (static_cast<part>(__p.field[2])
  169. == money_base::space)))
  170. || (__i == 2 && ((static_cast<part>(__p.field[3])
  171. == money_base::value)
  172. || (__mandatory_sign
  173. && (static_cast<part>(__p.field[3])
  174. == money_base::sign)))))
  175. {
  176. const size_type __len = __lc->_M_curr_symbol_size;
  177. size_type __j = 0;
  178. for (; __beg != __end && __j < __len
  179. && *__beg == __lc->_M_curr_symbol[__j];
  180. ++__beg, (void)++__j);
  181. if (__j != __len
  182. && (__j || __io.flags() & ios_base::showbase))
  183. __testvalid = false;
  184. }
  185. break;
  186. case money_base::sign:
  187. // Sign might not exist, or be more than one character long.
  188. if (__lc->_M_positive_sign_size && __beg != __end
  189. && *__beg == __lc->_M_positive_sign[0])
  190. {
  191. __sign_size = __lc->_M_positive_sign_size;
  192. ++__beg;
  193. }
  194. else if (__lc->_M_negative_sign_size && __beg != __end
  195. && *__beg == __lc->_M_negative_sign[0])
  196. {
  197. __negative = true;
  198. __sign_size = __lc->_M_negative_sign_size;
  199. ++__beg;
  200. }
  201. else if (__lc->_M_positive_sign_size
  202. && !__lc->_M_negative_sign_size)
  203. // "... if no sign is detected, the result is given the sign
  204. // that corresponds to the source of the empty string"
  205. __negative = true;
  206. else if (__mandatory_sign)
  207. __testvalid = false;
  208. break;
  209. case money_base::value:
  210. // Extract digits, remove and stash away the
  211. // grouping of found thousands separators.
  212. for (; __beg != __end; ++__beg)
  213. {
  214. const char_type __c = *__beg;
  215. const char_type* __q = __traits_type::find(__lit_zero,
  216. 10, __c);
  217. if (__q != 0)
  218. {
  219. __res += money_base::_S_atoms[__q - __lit];
  220. ++__n;
  221. }
  222. else if (__c == __lc->_M_decimal_point
  223. && !__testdecfound)
  224. {
  225. if (__lc->_M_frac_digits <= 0)
  226. break;
  227. __last_pos = __n;
  228. __n = 0;
  229. __testdecfound = true;
  230. }
  231. else if (__lc->_M_use_grouping
  232. && __c == __lc->_M_thousands_sep
  233. && !__testdecfound)
  234. {
  235. if (__n)
  236. {
  237. // Mark position for later analysis.
  238. __grouping_tmp += static_cast<char>(__n);
  239. __n = 0;
  240. }
  241. else
  242. {
  243. __testvalid = false;
  244. break;
  245. }
  246. }
  247. else
  248. break;
  249. }
  250. if (__res.empty())
  251. __testvalid = false;
  252. break;
  253. case money_base::space:
  254. // At least one space is required.
  255. if (__beg != __end && __ctype.is(ctype_base::space, *__beg))
  256. ++__beg;
  257. else
  258. __testvalid = false;
  259. // fallthrough
  260. case money_base::none:
  261. // Only if not at the end of the pattern.
  262. if (__i != 3)
  263. for (; __beg != __end
  264. && __ctype.is(ctype_base::space, *__beg); ++__beg);
  265. break;
  266. }
  267. }
  268. // Need to get the rest of the sign characters, if they exist.
  269. if (__sign_size > 1 && __testvalid)
  270. {
  271. const char_type* __sign = __negative ? __lc->_M_negative_sign
  272. : __lc->_M_positive_sign;
  273. size_type __i = 1;
  274. for (; __beg != __end && __i < __sign_size
  275. && *__beg == __sign[__i]; ++__beg, (void)++__i);
  276. if (__i != __sign_size)
  277. __testvalid = false;
  278. }
  279. if (__testvalid)
  280. {
  281. // Strip leading zeros.
  282. if (__res.size() > 1)
  283. {
  284. const size_type __first = __res.find_first_not_of('0');
  285. const bool __only_zeros = __first == string::npos;
  286. if (__first)
  287. __res.erase(0, __only_zeros ? __res.size() - 1 : __first);
  288. }
  289. // 22.2.6.1.2, p4
  290. if (__negative && __res[0] != '0')
  291. __res.insert(__res.begin(), '-');
  292. // Test for grouping fidelity.
  293. if (__grouping_tmp.size())
  294. {
  295. // Add the ending grouping.
  296. __grouping_tmp += static_cast<char>(__testdecfound ? __last_pos
  297. : __n);
  298. if (!std::__verify_grouping(__lc->_M_grouping,
  299. __lc->_M_grouping_size,
  300. __grouping_tmp))
  301. __err |= ios_base::failbit;
  302. }
  303. // Iff not enough digits were supplied after the decimal-point.
  304. if (__testdecfound && __n != __lc->_M_frac_digits)
  305. __testvalid = false;
  306. }
  307. // Iff valid sequence is not recognized.
  308. if (!__testvalid)
  309. __err |= ios_base::failbit;
  310. else
  311. __units.swap(__res);
  312. // Iff no more characters are available.
  313. if (__beg == __end)
  314. __err |= ios_base::eofbit;
  315. return __beg;
  316. }
  317. #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ \
  318. && (_GLIBCXX_USE_CXX11_ABI == 0 || defined __LONG_DOUBLE_IEEE128__)
  319. template<typename _CharT, typename _InIter>
  320. _InIter
  321. money_get<_CharT, _InIter>::
  322. __do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
  323. ios_base::iostate& __err, double& __units) const
  324. {
  325. string __str;
  326. __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
  327. : _M_extract<false>(__beg, __end, __io, __err, __str);
  328. std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
  329. return __beg;
  330. }
  331. #endif
  332. template<typename _CharT, typename _InIter>
  333. _InIter
  334. money_get<_CharT, _InIter>::
  335. do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
  336. ios_base::iostate& __err, long double& __units) const
  337. {
  338. string __str;
  339. __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
  340. : _M_extract<false>(__beg, __end, __io, __err, __str);
  341. std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
  342. return __beg;
  343. }
  344. template<typename _CharT, typename _InIter>
  345. _InIter
  346. money_get<_CharT, _InIter>::
  347. do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
  348. ios_base::iostate& __err, string_type& __digits) const
  349. {
  350. typedef typename string::size_type size_type;
  351. const locale& __loc = __io._M_getloc();
  352. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  353. string __str;
  354. __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
  355. : _M_extract<false>(__beg, __end, __io, __err, __str);
  356. const size_type __len = __str.size();
  357. if (__len)
  358. {
  359. __digits.resize(__len);
  360. __ctype.widen(__str.data(), __str.data() + __len, &__digits[0]);
  361. }
  362. return __beg;
  363. }
  364. #if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \
  365. && defined __LONG_DOUBLE_IEEE128__
  366. template<typename _CharT, typename _InIter>
  367. _InIter
  368. money_get<_CharT, _InIter>::
  369. __do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
  370. ios_base::iostate& __err, __ibm128& __units) const
  371. {
  372. string __str;
  373. __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
  374. : _M_extract<false>(__beg, __end, __io, __err, __str);
  375. std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
  376. return __beg;
  377. }
  378. #endif
  379. template<typename _CharT, typename _OutIter>
  380. template<bool _Intl>
  381. _OutIter
  382. money_put<_CharT, _OutIter>::
  383. _M_insert(iter_type __s, ios_base& __io, char_type __fill,
  384. const string_type& __digits) const
  385. {
  386. typedef typename string_type::size_type size_type;
  387. typedef money_base::part part;
  388. typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
  389. const locale& __loc = __io._M_getloc();
  390. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  391. __use_cache<__cache_type> __uc;
  392. const __cache_type* __lc = __uc(__loc);
  393. const char_type* __lit = __lc->_M_atoms;
  394. // Determine if negative or positive formats are to be used, and
  395. // discard leading negative_sign if it is present.
  396. const char_type* __beg = __digits.data();
  397. money_base::pattern __p;
  398. const char_type* __sign;
  399. size_type __sign_size;
  400. if (!(*__beg == __lit[money_base::_S_minus]))
  401. {
  402. __p = __lc->_M_pos_format;
  403. __sign = __lc->_M_positive_sign;
  404. __sign_size = __lc->_M_positive_sign_size;
  405. }
  406. else
  407. {
  408. __p = __lc->_M_neg_format;
  409. __sign = __lc->_M_negative_sign;
  410. __sign_size = __lc->_M_negative_sign_size;
  411. if (__digits.size())
  412. ++__beg;
  413. }
  414. // Look for valid numbers in the ctype facet within input digits.
  415. size_type __len = __ctype.scan_not(ctype_base::digit, __beg,
  416. __beg + __digits.size()) - __beg;
  417. if (__len)
  418. {
  419. // Assume valid input, and attempt to format.
  420. // Break down input numbers into base components, as follows:
  421. // final_value = grouped units + (decimal point) + (digits)
  422. string_type __value;
  423. __value.reserve(2 * __len);
  424. // Add thousands separators to non-decimal digits, per
  425. // grouping rules.
  426. long __paddec = __len - __lc->_M_frac_digits;
  427. if (__paddec > 0)
  428. {
  429. if (__lc->_M_frac_digits < 0)
  430. __paddec = __len;
  431. if (__lc->_M_grouping_size)
  432. {
  433. __value.assign(2 * __paddec, char_type());
  434. _CharT* __vend =
  435. std::__add_grouping(&__value[0], __lc->_M_thousands_sep,
  436. __lc->_M_grouping,
  437. __lc->_M_grouping_size,
  438. __beg, __beg + __paddec);
  439. __value.erase(__vend - &__value[0]);
  440. }
  441. else
  442. __value.assign(__beg, __paddec);
  443. }
  444. // Deal with decimal point, decimal digits.
  445. if (__lc->_M_frac_digits > 0)
  446. {
  447. __value += __lc->_M_decimal_point;
  448. if (__paddec >= 0)
  449. __value.append(__beg + __paddec, __lc->_M_frac_digits);
  450. else
  451. {
  452. // Have to pad zeros in the decimal position.
  453. __value.append(-__paddec, __lit[money_base::_S_zero]);
  454. __value.append(__beg, __len);
  455. }
  456. }
  457. // Calculate length of resulting string.
  458. const ios_base::fmtflags __f = __io.flags()
  459. & ios_base::adjustfield;
  460. __len = __value.size() + __sign_size;
  461. __len += ((__io.flags() & ios_base::showbase)
  462. ? __lc->_M_curr_symbol_size : 0);
  463. string_type __res;
  464. __res.reserve(2 * __len);
  465. const size_type __width = static_cast<size_type>(__io.width());
  466. const bool __testipad = (__f == ios_base::internal
  467. && __len < __width);
  468. // Fit formatted digits into the required pattern.
  469. for (int __i = 0; __i < 4; ++__i)
  470. {
  471. const part __which = static_cast<part>(__p.field[__i]);
  472. switch (__which)
  473. {
  474. case money_base::symbol:
  475. if (__io.flags() & ios_base::showbase)
  476. __res.append(__lc->_M_curr_symbol,
  477. __lc->_M_curr_symbol_size);
  478. break;
  479. case money_base::sign:
  480. // Sign might not exist, or be more than one
  481. // character long. In that case, add in the rest
  482. // below.
  483. if (__sign_size)
  484. __res += __sign[0];
  485. break;
  486. case money_base::value:
  487. __res += __value;
  488. break;
  489. case money_base::space:
  490. // At least one space is required, but if internal
  491. // formatting is required, an arbitrary number of
  492. // fill spaces will be necessary.
  493. if (__testipad)
  494. __res.append(__width - __len, __fill);
  495. else
  496. __res += __fill;
  497. break;
  498. case money_base::none:
  499. if (__testipad)
  500. __res.append(__width - __len, __fill);
  501. break;
  502. }
  503. }
  504. // Special case of multi-part sign parts.
  505. if (__sign_size > 1)
  506. __res.append(__sign + 1, __sign_size - 1);
  507. // Pad, if still necessary.
  508. __len = __res.size();
  509. if (__width > __len)
  510. {
  511. if (__f == ios_base::left)
  512. // After.
  513. __res.append(__width - __len, __fill);
  514. else
  515. // Before.
  516. __res.insert(0, __width - __len, __fill);
  517. __len = __width;
  518. }
  519. // Write resulting, fully-formatted string to output iterator.
  520. __s = std::__write(__s, __res.data(), __len);
  521. }
  522. __io.width(0);
  523. return __s;
  524. }
  525. #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ \
  526. && (_GLIBCXX_USE_CXX11_ABI == 0 || defined __LONG_DOUBLE_IEEE128__)
  527. template<typename _CharT, typename _OutIter>
  528. _OutIter
  529. money_put<_CharT, _OutIter>::
  530. __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
  531. double __units) const
  532. { return this->do_put(__s, __intl, __io, __fill, (long double) __units); }
  533. #endif
  534. template<typename _CharT, typename _OutIter>
  535. _OutIter
  536. money_put<_CharT, _OutIter>::
  537. do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
  538. long double __units) const
  539. {
  540. const locale __loc = __io.getloc();
  541. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  542. #if _GLIBCXX_USE_C99_STDIO
  543. // First try a buffer perhaps big enough.
  544. int __cs_size = 64;
  545. char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  546. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  547. // 328. Bad sprintf format modifier in money_put<>::do_put()
  548. int __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
  549. "%.*Lf", 0, __units);
  550. // If the buffer was not large enough, try again with the correct size.
  551. if (__len >= __cs_size)
  552. {
  553. __cs_size = __len + 1;
  554. __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  555. __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
  556. "%.*Lf", 0, __units);
  557. }
  558. #else
  559. // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'.
  560. const int __cs_size =
  561. __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 3;
  562. char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  563. int __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, "%.*Lf",
  564. 0, __units);
  565. #endif
  566. string_type __digits(__len, char_type());
  567. __ctype.widen(__cs, __cs + __len, &__digits[0]);
  568. return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
  569. : _M_insert<false>(__s, __io, __fill, __digits);
  570. }
  571. template<typename _CharT, typename _OutIter>
  572. _OutIter
  573. money_put<_CharT, _OutIter>::
  574. do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
  575. const string_type& __digits) const
  576. { return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
  577. : _M_insert<false>(__s, __io, __fill, __digits); }
  578. #if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \
  579. && defined __LONG_DOUBLE_IEEE128__
  580. template<typename _CharT, typename _OutIter>
  581. _OutIter
  582. money_put<_CharT, _OutIter>::
  583. __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
  584. __ibm128 __units) const
  585. {
  586. const locale __loc = __io.getloc();
  587. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  588. #if _GLIBCXX_USE_C99_STDIO
  589. // First try a buffer perhaps big enough.
  590. int __cs_size = 64;
  591. char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  592. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  593. // 328. Bad sprintf format modifier in money_put<>::do_put()
  594. int __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
  595. "%.*Lf", 0, __units);
  596. // If the buffer was not large enough, try again with the correct size.
  597. if (__len >= __cs_size)
  598. {
  599. __cs_size = __len + 1;
  600. __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  601. __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
  602. "%.*Lf", 0, __units);
  603. }
  604. #else
  605. // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'.
  606. const int __cs_size =
  607. __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 3;
  608. char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  609. int __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, "%.*Lf",
  610. 0, __units);
  611. #endif
  612. string_type __digits(__len, char_type());
  613. __ctype.widen(__cs, __cs + __len, &__digits[0]);
  614. return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
  615. : _M_insert<false>(__s, __io, __fill, __digits);
  616. }
  617. #endif
  618. _GLIBCXX_END_NAMESPACE_LDBL_OR_CXX11
  619. // NB: Not especially useful. Without an ios_base object or some
  620. // kind of locale reference, we are left clawing at the air where
  621. // the side of the mountain used to be...
  622. template<typename _CharT, typename _InIter>
  623. time_base::dateorder
  624. time_get<_CharT, _InIter>::do_date_order() const
  625. { return time_base::no_order; }
  626. // Expand a strftime format string and parse it. E.g., do_get_date() may
  627. // pass %m/%d/%Y => extracted characters.
  628. template<typename _CharT, typename _InIter>
  629. _InIter
  630. time_get<_CharT, _InIter>::
  631. _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
  632. ios_base::iostate& __err, tm* __tm,
  633. const _CharT* __format) const
  634. {
  635. const locale& __loc = __io._M_getloc();
  636. const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
  637. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  638. const size_t __len = char_traits<_CharT>::length(__format);
  639. ios_base::iostate __tmperr = ios_base::goodbit;
  640. size_t __i = 0;
  641. for (; __beg != __end && __i < __len && !__tmperr; ++__i)
  642. {
  643. if (__ctype.narrow(__format[__i], 0) == '%')
  644. {
  645. // Verify valid formatting code, attempt to extract.
  646. char __c = __ctype.narrow(__format[++__i], 0);
  647. int __mem = 0;
  648. if (__c == 'E' || __c == 'O')
  649. __c = __ctype.narrow(__format[++__i], 0);
  650. switch (__c)
  651. {
  652. const char* __cs;
  653. _CharT __wcs[10];
  654. case 'a':
  655. // Abbreviated weekday name [tm_wday]
  656. const char_type* __days1[7];
  657. __tp._M_days_abbreviated(__days1);
  658. __beg = _M_extract_name(__beg, __end, __mem, __days1,
  659. 7, __io, __tmperr);
  660. if (!__tmperr)
  661. __tm->tm_wday = __mem;
  662. break;
  663. case 'A':
  664. // Weekday name [tm_wday].
  665. const char_type* __days2[7];
  666. __tp._M_days(__days2);
  667. __beg = _M_extract_name(__beg, __end, __mem, __days2,
  668. 7, __io, __tmperr);
  669. if (!__tmperr)
  670. __tm->tm_wday = __mem;
  671. break;
  672. case 'h':
  673. case 'b':
  674. // Abbreviated month name [tm_mon]
  675. const char_type* __months1[12];
  676. __tp._M_months_abbreviated(__months1);
  677. __beg = _M_extract_name(__beg, __end, __mem,
  678. __months1, 12, __io, __tmperr);
  679. if (!__tmperr)
  680. __tm->tm_mon = __mem;
  681. break;
  682. case 'B':
  683. // Month name [tm_mon].
  684. const char_type* __months2[12];
  685. __tp._M_months(__months2);
  686. __beg = _M_extract_name(__beg, __end, __mem,
  687. __months2, 12, __io, __tmperr);
  688. if (!__tmperr)
  689. __tm->tm_mon = __mem;
  690. break;
  691. case 'c':
  692. // Default time and date representation.
  693. const char_type* __dt[2];
  694. __tp._M_date_time_formats(__dt);
  695. __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
  696. __tm, __dt[0]);
  697. break;
  698. case 'd':
  699. // Day [01, 31]. [tm_mday]
  700. __beg = _M_extract_num(__beg, __end, __mem, 1, 31, 2,
  701. __io, __tmperr);
  702. if (!__tmperr)
  703. __tm->tm_mday = __mem;
  704. break;
  705. case 'e':
  706. // Day [1, 31], with single digits preceded by
  707. // space. [tm_mday]
  708. if (__ctype.is(ctype_base::space, *__beg))
  709. __beg = _M_extract_num(++__beg, __end, __mem, 1, 9,
  710. 1, __io, __tmperr);
  711. else
  712. __beg = _M_extract_num(__beg, __end, __mem, 10, 31,
  713. 2, __io, __tmperr);
  714. if (!__tmperr)
  715. __tm->tm_mday = __mem;
  716. break;
  717. case 'D':
  718. // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
  719. __cs = "%m/%d/%y";
  720. __ctype.widen(__cs, __cs + 9, __wcs);
  721. __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
  722. __tm, __wcs);
  723. break;
  724. case 'H':
  725. // Hour [00, 23]. [tm_hour]
  726. __beg = _M_extract_num(__beg, __end, __mem, 0, 23, 2,
  727. __io, __tmperr);
  728. if (!__tmperr)
  729. __tm->tm_hour = __mem;
  730. break;
  731. case 'I':
  732. // Hour [01, 12]. [tm_hour]
  733. __beg = _M_extract_num(__beg, __end, __mem, 1, 12, 2,
  734. __io, __tmperr);
  735. if (!__tmperr)
  736. __tm->tm_hour = __mem;
  737. break;
  738. case 'm':
  739. // Month [01, 12]. [tm_mon]
  740. __beg = _M_extract_num(__beg, __end, __mem, 1, 12, 2,
  741. __io, __tmperr);
  742. if (!__tmperr)
  743. __tm->tm_mon = __mem - 1;
  744. break;
  745. case 'M':
  746. // Minute [00, 59]. [tm_min]
  747. __beg = _M_extract_num(__beg, __end, __mem, 0, 59, 2,
  748. __io, __tmperr);
  749. if (!__tmperr)
  750. __tm->tm_min = __mem;
  751. break;
  752. case 'n':
  753. if (__ctype.narrow(*__beg, 0) == '\n')
  754. ++__beg;
  755. else
  756. __tmperr |= ios_base::failbit;
  757. break;
  758. case 'R':
  759. // Equivalent to (%H:%M).
  760. __cs = "%H:%M";
  761. __ctype.widen(__cs, __cs + 6, __wcs);
  762. __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
  763. __tm, __wcs);
  764. break;
  765. case 'S':
  766. // Seconds. [tm_sec]
  767. // [00, 60] in C99 (one leap-second), [00, 61] in C89.
  768. #if _GLIBCXX_USE_C99
  769. __beg = _M_extract_num(__beg, __end, __mem, 0, 60, 2,
  770. #else
  771. __beg = _M_extract_num(__beg, __end, __mem, 0, 61, 2,
  772. #endif
  773. __io, __tmperr);
  774. if (!__tmperr)
  775. __tm->tm_sec = __mem;
  776. break;
  777. case 't':
  778. if (__ctype.narrow(*__beg, 0) == '\t')
  779. ++__beg;
  780. else
  781. __tmperr |= ios_base::failbit;
  782. break;
  783. case 'T':
  784. // Equivalent to (%H:%M:%S).
  785. __cs = "%H:%M:%S";
  786. __ctype.widen(__cs, __cs + 9, __wcs);
  787. __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
  788. __tm, __wcs);
  789. break;
  790. case 'x':
  791. // Locale's date.
  792. const char_type* __dates[2];
  793. __tp._M_date_formats(__dates);
  794. __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
  795. __tm, __dates[0]);
  796. break;
  797. case 'X':
  798. // Locale's time.
  799. const char_type* __times[2];
  800. __tp._M_time_formats(__times);
  801. __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
  802. __tm, __times[0]);
  803. break;
  804. case 'y':
  805. case 'C': // C99
  806. // Two digit year.
  807. case 'Y':
  808. // Year [1900).
  809. // NB: We parse either two digits, implicitly years since
  810. // 1900, or 4 digits, full year. In both cases we can
  811. // reconstruct [tm_year]. See also libstdc++/26701.
  812. __beg = _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
  813. __io, __tmperr);
  814. if (!__tmperr)
  815. __tm->tm_year = __mem < 0 ? __mem + 100 : __mem - 1900;
  816. break;
  817. case 'Z':
  818. // Timezone info.
  819. if (__ctype.is(ctype_base::upper, *__beg))
  820. {
  821. int __tmp;
  822. __beg = _M_extract_name(__beg, __end, __tmp,
  823. __timepunct_cache<_CharT>::_S_timezones,
  824. 14, __io, __tmperr);
  825. // GMT requires special effort.
  826. if (__beg != __end && !__tmperr && __tmp == 0
  827. && (*__beg == __ctype.widen('-')
  828. || *__beg == __ctype.widen('+')))
  829. {
  830. __beg = _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
  831. __io, __tmperr);
  832. __beg = _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
  833. __io, __tmperr);
  834. }
  835. }
  836. else
  837. __tmperr |= ios_base::failbit;
  838. break;
  839. default:
  840. // Not recognized.
  841. __tmperr |= ios_base::failbit;
  842. }
  843. }
  844. else
  845. {
  846. // Verify format and input match, extract and discard.
  847. if (__format[__i] == *__beg)
  848. ++__beg;
  849. else
  850. __tmperr |= ios_base::failbit;
  851. }
  852. }
  853. if (__tmperr || __i != __len)
  854. __err |= ios_base::failbit;
  855. return __beg;
  856. }
  857. template<typename _CharT, typename _InIter>
  858. _InIter
  859. time_get<_CharT, _InIter>::
  860. _M_extract_num(iter_type __beg, iter_type __end, int& __member,
  861. int __min, int __max, size_t __len,
  862. ios_base& __io, ios_base::iostate& __err) const
  863. {
  864. const locale& __loc = __io._M_getloc();
  865. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  866. // As-is works for __len = 1, 2, 4, the values actually used.
  867. int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1);
  868. ++__min;
  869. size_t __i = 0;
  870. int __value = 0;
  871. for (; __beg != __end && __i < __len; ++__beg, (void)++__i)
  872. {
  873. const char __c = __ctype.narrow(*__beg, '*');
  874. if (__c >= '0' && __c <= '9')
  875. {
  876. __value = __value * 10 + (__c - '0');
  877. const int __valuec = __value * __mult;
  878. if (__valuec > __max || __valuec + __mult < __min)
  879. break;
  880. __mult /= 10;
  881. }
  882. else
  883. break;
  884. }
  885. if (__i == __len)
  886. __member = __value;
  887. // Special encoding for do_get_year, 'y', and 'Y' above.
  888. else if (__len == 4 && __i == 2)
  889. __member = __value - 100;
  890. else
  891. __err |= ios_base::failbit;
  892. return __beg;
  893. }
  894. // Assumptions:
  895. // All elements in __names are unique.
  896. template<typename _CharT, typename _InIter>
  897. _InIter
  898. time_get<_CharT, _InIter>::
  899. _M_extract_name(iter_type __beg, iter_type __end, int& __member,
  900. const _CharT** __names, size_t __indexlen,
  901. ios_base& __io, ios_base::iostate& __err) const
  902. {
  903. typedef char_traits<_CharT> __traits_type;
  904. const locale& __loc = __io._M_getloc();
  905. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  906. int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
  907. * __indexlen));
  908. size_t __nmatches = 0;
  909. size_t __pos = 0;
  910. bool __testvalid = true;
  911. const char_type* __name;
  912. // Look for initial matches.
  913. // NB: Some of the locale data is in the form of all lowercase
  914. // names, and some is in the form of initially-capitalized
  915. // names. Look for both.
  916. if (__beg != __end)
  917. {
  918. const char_type __c = *__beg;
  919. for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
  920. if (__c == __names[__i1][0]
  921. || __c == __ctype.toupper(__names[__i1][0]))
  922. __matches[__nmatches++] = __i1;
  923. }
  924. while (__nmatches > 1)
  925. {
  926. // Find smallest matching string.
  927. size_t __minlen = __traits_type::length(__names[__matches[0]]);
  928. for (size_t __i2 = 1; __i2 < __nmatches; ++__i2)
  929. __minlen = std::min(__minlen,
  930. __traits_type::length(__names[__matches[__i2]]));
  931. ++__beg;
  932. ++__pos;
  933. if (__pos < __minlen && __beg != __end)
  934. for (size_t __i3 = 0; __i3 < __nmatches;)
  935. {
  936. __name = __names[__matches[__i3]];
  937. if (!(__name[__pos] == *__beg))
  938. __matches[__i3] = __matches[--__nmatches];
  939. else
  940. ++__i3;
  941. }
  942. else
  943. break;
  944. }
  945. if (__nmatches == 1)
  946. {
  947. // Make sure found name is completely extracted.
  948. ++__beg;
  949. ++__pos;
  950. __name = __names[__matches[0]];
  951. const size_t __len = __traits_type::length(__name);
  952. while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
  953. ++__beg, (void)++__pos;
  954. if (__len == __pos)
  955. __member = __matches[0];
  956. else
  957. __testvalid = false;
  958. }
  959. else
  960. __testvalid = false;
  961. if (!__testvalid)
  962. __err |= ios_base::failbit;
  963. return __beg;
  964. }
  965. template<typename _CharT, typename _InIter>
  966. _InIter
  967. time_get<_CharT, _InIter>::
  968. _M_extract_wday_or_month(iter_type __beg, iter_type __end, int& __member,
  969. const _CharT** __names, size_t __indexlen,
  970. ios_base& __io, ios_base::iostate& __err) const
  971. {
  972. typedef char_traits<_CharT> __traits_type;
  973. const locale& __loc = __io._M_getloc();
  974. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  975. int* __matches = static_cast<int*>(__builtin_alloca(2 * sizeof(int)
  976. * __indexlen));
  977. size_t __nmatches = 0;
  978. size_t* __matches_lengths = 0;
  979. size_t __pos = 0;
  980. if (__beg != __end)
  981. {
  982. const char_type __c = *__beg;
  983. for (size_t __i = 0; __i < 2 * __indexlen; ++__i)
  984. if (__c == __names[__i][0]
  985. || __c == __ctype.toupper(__names[__i][0]))
  986. __matches[__nmatches++] = __i;
  987. }
  988. if (__nmatches)
  989. {
  990. ++__beg;
  991. ++__pos;
  992. __matches_lengths
  993. = static_cast<size_t*>(__builtin_alloca(sizeof(size_t)
  994. * __nmatches));
  995. for (size_t __i = 0; __i < __nmatches; ++__i)
  996. __matches_lengths[__i]
  997. = __traits_type::length(__names[__matches[__i]]);
  998. }
  999. for (; __beg != __end; ++__beg, (void)++__pos)
  1000. {
  1001. size_t __nskipped = 0;
  1002. const char_type __c = *__beg;
  1003. for (size_t __i = 0; __i < __nmatches;)
  1004. {
  1005. const char_type* __name = __names[__matches[__i]];
  1006. if (__pos >= __matches_lengths[__i])
  1007. ++__nskipped, ++__i;
  1008. else if (!(__name[__pos] == __c))
  1009. {
  1010. --__nmatches;
  1011. __matches[__i] = __matches[__nmatches];
  1012. __matches_lengths[__i] = __matches_lengths[__nmatches];
  1013. }
  1014. else
  1015. ++__i;
  1016. }
  1017. if (__nskipped == __nmatches)
  1018. break;
  1019. }
  1020. if ((__nmatches == 1 && __matches_lengths[0] == __pos)
  1021. || (__nmatches == 2 && (__matches_lengths[0] == __pos
  1022. || __matches_lengths[1] == __pos)))
  1023. __member = (__matches[0] >= (int)__indexlen
  1024. ? __matches[0] - (int)__indexlen : __matches[0]);
  1025. else
  1026. __err |= ios_base::failbit;
  1027. return __beg;
  1028. }
  1029. template<typename _CharT, typename _InIter>
  1030. _InIter
  1031. time_get<_CharT, _InIter>::
  1032. do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
  1033. ios_base::iostate& __err, tm* __tm) const
  1034. {
  1035. const locale& __loc = __io._M_getloc();
  1036. const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
  1037. const char_type* __times[2];
  1038. __tp._M_time_formats(__times);
  1039. __beg = _M_extract_via_format(__beg, __end, __io, __err,
  1040. __tm, __times[0]);
  1041. if (__beg == __end)
  1042. __err |= ios_base::eofbit;
  1043. return __beg;
  1044. }
  1045. template<typename _CharT, typename _InIter>
  1046. _InIter
  1047. time_get<_CharT, _InIter>::
  1048. do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
  1049. ios_base::iostate& __err, tm* __tm) const
  1050. {
  1051. const locale& __loc = __io._M_getloc();
  1052. const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
  1053. const char_type* __dates[2];
  1054. __tp._M_date_formats(__dates);
  1055. __beg = _M_extract_via_format(__beg, __end, __io, __err,
  1056. __tm, __dates[0]);
  1057. if (__beg == __end)
  1058. __err |= ios_base::eofbit;
  1059. return __beg;
  1060. }
  1061. template<typename _CharT, typename _InIter>
  1062. _InIter
  1063. time_get<_CharT, _InIter>::
  1064. do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
  1065. ios_base::iostate& __err, tm* __tm) const
  1066. {
  1067. const locale& __loc = __io._M_getloc();
  1068. const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
  1069. const char_type* __days[14];
  1070. __tp._M_days_abbreviated(__days);
  1071. __tp._M_days(__days + 7);
  1072. int __tmpwday;
  1073. ios_base::iostate __tmperr = ios_base::goodbit;
  1074. __beg = _M_extract_wday_or_month(__beg, __end, __tmpwday, __days, 7,
  1075. __io, __tmperr);
  1076. if (!__tmperr)
  1077. __tm->tm_wday = __tmpwday;
  1078. else
  1079. __err |= ios_base::failbit;
  1080. if (__beg == __end)
  1081. __err |= ios_base::eofbit;
  1082. return __beg;
  1083. }
  1084. template<typename _CharT, typename _InIter>
  1085. _InIter
  1086. time_get<_CharT, _InIter>::
  1087. do_get_monthname(iter_type __beg, iter_type __end,
  1088. ios_base& __io, ios_base::iostate& __err, tm* __tm) const
  1089. {
  1090. const locale& __loc = __io._M_getloc();
  1091. const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
  1092. const char_type* __months[24];
  1093. __tp._M_months_abbreviated(__months);
  1094. __tp._M_months(__months + 12);
  1095. int __tmpmon;
  1096. ios_base::iostate __tmperr = ios_base::goodbit;
  1097. __beg = _M_extract_wday_or_month(__beg, __end, __tmpmon, __months, 12,
  1098. __io, __tmperr);
  1099. if (!__tmperr)
  1100. __tm->tm_mon = __tmpmon;
  1101. else
  1102. __err |= ios_base::failbit;
  1103. if (__beg == __end)
  1104. __err |= ios_base::eofbit;
  1105. return __beg;
  1106. }
  1107. template<typename _CharT, typename _InIter>
  1108. _InIter
  1109. time_get<_CharT, _InIter>::
  1110. do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
  1111. ios_base::iostate& __err, tm* __tm) const
  1112. {
  1113. int __tmpyear;
  1114. ios_base::iostate __tmperr = ios_base::goodbit;
  1115. __beg = _M_extract_num(__beg, __end, __tmpyear, 0, 9999, 4,
  1116. __io, __tmperr);
  1117. if (!__tmperr)
  1118. __tm->tm_year = __tmpyear < 0 ? __tmpyear + 100 : __tmpyear - 1900;
  1119. else
  1120. __err |= ios_base::failbit;
  1121. if (__beg == __end)
  1122. __err |= ios_base::eofbit;
  1123. return __beg;
  1124. }
  1125. #if __cplusplus >= 201103L
  1126. template<typename _CharT, typename _InIter>
  1127. inline
  1128. _InIter
  1129. time_get<_CharT, _InIter>::
  1130. get(iter_type __s, iter_type __end, ios_base& __io,
  1131. ios_base::iostate& __err, tm* __tm, const char_type* __fmt,
  1132. const char_type* __fmtend) const
  1133. {
  1134. const locale& __loc = __io._M_getloc();
  1135. ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
  1136. __err = ios_base::goodbit;
  1137. while (__fmt != __fmtend &&
  1138. __err == ios_base::goodbit)
  1139. {
  1140. if (__s == __end)
  1141. {
  1142. __err = ios_base::eofbit | ios_base::failbit;
  1143. break;
  1144. }
  1145. else if (__ctype.narrow(*__fmt, 0) == '%')
  1146. {
  1147. char __format;
  1148. char __mod = 0;
  1149. if (++__fmt == __fmtend)
  1150. {
  1151. __err = ios_base::failbit;
  1152. break;
  1153. }
  1154. const char __c = __ctype.narrow(*__fmt, 0);
  1155. if (__c != 'E' && __c != 'O')
  1156. __format = __c;
  1157. else if (++__fmt != __fmtend)
  1158. {
  1159. __mod = __c;
  1160. __format = __ctype.narrow(*__fmt, 0);
  1161. }
  1162. else
  1163. {
  1164. __err = ios_base::failbit;
  1165. break;
  1166. }
  1167. __s = this->do_get(__s, __end, __io, __err, __tm, __format,
  1168. __mod);
  1169. ++__fmt;
  1170. }
  1171. else if (__ctype.is(ctype_base::space, *__fmt))
  1172. {
  1173. ++__fmt;
  1174. while (__fmt != __fmtend &&
  1175. __ctype.is(ctype_base::space, *__fmt))
  1176. ++__fmt;
  1177. while (__s != __end &&
  1178. __ctype.is(ctype_base::space, *__s))
  1179. ++__s;
  1180. }
  1181. // TODO real case-insensitive comparison
  1182. else if (__ctype.tolower(*__s) == __ctype.tolower(*__fmt) ||
  1183. __ctype.toupper(*__s) == __ctype.toupper(*__fmt))
  1184. {
  1185. ++__s;
  1186. ++__fmt;
  1187. }
  1188. else
  1189. {
  1190. __err = ios_base::failbit;
  1191. break;
  1192. }
  1193. }
  1194. return __s;
  1195. }
  1196. template<typename _CharT, typename _InIter>
  1197. inline
  1198. _InIter
  1199. time_get<_CharT, _InIter>::
  1200. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  1201. ios_base::iostate& __err, tm* __tm,
  1202. char __format, char __mod) const
  1203. {
  1204. const locale& __loc = __io._M_getloc();
  1205. ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
  1206. __err = ios_base::goodbit;
  1207. char_type __fmt[4];
  1208. __fmt[0] = __ctype.widen('%');
  1209. if (!__mod)
  1210. {
  1211. __fmt[1] = __format;
  1212. __fmt[2] = char_type();
  1213. }
  1214. else
  1215. {
  1216. __fmt[1] = __mod;
  1217. __fmt[2] = __format;
  1218. __fmt[3] = char_type();
  1219. }
  1220. __beg = _M_extract_via_format(__beg, __end, __io, __err, __tm, __fmt);
  1221. if (__beg == __end)
  1222. __err |= ios_base::eofbit;
  1223. return __beg;
  1224. }
  1225. #endif // __cplusplus >= 201103L
  1226. template<typename _CharT, typename _OutIter>
  1227. _OutIter
  1228. time_put<_CharT, _OutIter>::
  1229. put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
  1230. const _CharT* __beg, const _CharT* __end) const
  1231. {
  1232. const locale& __loc = __io._M_getloc();
  1233. ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
  1234. for (; __beg != __end; ++__beg)
  1235. if (__ctype.narrow(*__beg, 0) != '%')
  1236. {
  1237. *__s = *__beg;
  1238. ++__s;
  1239. }
  1240. else if (++__beg != __end)
  1241. {
  1242. char __format;
  1243. char __mod = 0;
  1244. const char __c = __ctype.narrow(*__beg, 0);
  1245. if (__c != 'E' && __c != 'O')
  1246. __format = __c;
  1247. else if (++__beg != __end)
  1248. {
  1249. __mod = __c;
  1250. __format = __ctype.narrow(*__beg, 0);
  1251. }
  1252. else
  1253. break;
  1254. __s = this->do_put(__s, __io, __fill, __tm, __format, __mod);
  1255. }
  1256. else
  1257. break;
  1258. return __s;
  1259. }
  1260. template<typename _CharT, typename _OutIter>
  1261. _OutIter
  1262. time_put<_CharT, _OutIter>::
  1263. do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
  1264. char __format, char __mod) const
  1265. {
  1266. const locale& __loc = __io._M_getloc();
  1267. ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
  1268. __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
  1269. // NB: This size is arbitrary. Should this be a data member,
  1270. // initialized at construction?
  1271. const size_t __maxlen = 128;
  1272. char_type __res[__maxlen];
  1273. // NB: In IEE 1003.1-200x, and perhaps other locale models, it
  1274. // is possible that the format character will be longer than one
  1275. // character. Possibilities include 'E' or 'O' followed by a
  1276. // format character: if __mod is not the default argument, assume
  1277. // it's a valid modifier.
  1278. char_type __fmt[4];
  1279. __fmt[0] = __ctype.widen('%');
  1280. if (!__mod)
  1281. {
  1282. __fmt[1] = __format;
  1283. __fmt[2] = char_type();
  1284. }
  1285. else
  1286. {
  1287. __fmt[1] = __mod;
  1288. __fmt[2] = __format;
  1289. __fmt[3] = char_type();
  1290. }
  1291. __tp._M_put(__res, __maxlen, __fmt, __tm);
  1292. // Write resulting, fully-formatted string to output iterator.
  1293. return std::__write(__s, __res, char_traits<char_type>::length(__res));
  1294. }
  1295. // Inhibit implicit instantiations for required instantiations,
  1296. // which are defined via explicit instantiations elsewhere.
  1297. #if _GLIBCXX_EXTERN_TEMPLATE
  1298. extern template class moneypunct<char, false>;
  1299. extern template class moneypunct<char, true>;
  1300. extern template class moneypunct_byname<char, false>;
  1301. extern template class moneypunct_byname<char, true>;
  1302. extern template class _GLIBCXX_NAMESPACE_LDBL_OR_CXX11 money_get<char>;
  1303. extern template class _GLIBCXX_NAMESPACE_LDBL_OR_CXX11 money_put<char>;
  1304. extern template class __timepunct<char>;
  1305. extern template class time_put<char>;
  1306. extern template class time_put_byname<char>;
  1307. extern template class time_get<char>;
  1308. extern template class time_get_byname<char>;
  1309. extern template class messages<char>;
  1310. extern template class messages_byname<char>;
  1311. extern template
  1312. const moneypunct<char, true>&
  1313. use_facet<moneypunct<char, true> >(const locale&);
  1314. extern template
  1315. const moneypunct<char, false>&
  1316. use_facet<moneypunct<char, false> >(const locale&);
  1317. extern template
  1318. const money_put<char>&
  1319. use_facet<money_put<char> >(const locale&);
  1320. extern template
  1321. const money_get<char>&
  1322. use_facet<money_get<char> >(const locale&);
  1323. extern template
  1324. const __timepunct<char>&
  1325. use_facet<__timepunct<char> >(const locale&);
  1326. extern template
  1327. const time_put<char>&
  1328. use_facet<time_put<char> >(const locale&);
  1329. extern template
  1330. const time_get<char>&
  1331. use_facet<time_get<char> >(const locale&);
  1332. extern template
  1333. const messages<char>&
  1334. use_facet<messages<char> >(const locale&);
  1335. extern template
  1336. bool
  1337. has_facet<moneypunct<char> >(const locale&);
  1338. extern template
  1339. bool
  1340. has_facet<money_put<char> >(const locale&);
  1341. extern template
  1342. bool
  1343. has_facet<money_get<char> >(const locale&);
  1344. extern template
  1345. bool
  1346. has_facet<__timepunct<char> >(const locale&);
  1347. extern template
  1348. bool
  1349. has_facet<time_put<char> >(const locale&);
  1350. extern template
  1351. bool
  1352. has_facet<time_get<char> >(const locale&);
  1353. extern template
  1354. bool
  1355. has_facet<messages<char> >(const locale&);
  1356. #ifdef _GLIBCXX_USE_WCHAR_T
  1357. extern template class moneypunct<wchar_t, false>;
  1358. extern template class moneypunct<wchar_t, true>;
  1359. extern template class moneypunct_byname<wchar_t, false>;
  1360. extern template class moneypunct_byname<wchar_t, true>;
  1361. extern template class _GLIBCXX_NAMESPACE_LDBL_OR_CXX11 money_get<wchar_t>;
  1362. extern template class _GLIBCXX_NAMESPACE_LDBL_OR_CXX11 money_put<wchar_t>;
  1363. extern template class __timepunct<wchar_t>;
  1364. extern template class time_put<wchar_t>;
  1365. extern template class time_put_byname<wchar_t>;
  1366. extern template class time_get<wchar_t>;
  1367. extern template class time_get_byname<wchar_t>;
  1368. extern template class messages<wchar_t>;
  1369. extern template class messages_byname<wchar_t>;
  1370. extern template
  1371. const moneypunct<wchar_t, true>&
  1372. use_facet<moneypunct<wchar_t, true> >(const locale&);
  1373. extern template
  1374. const moneypunct<wchar_t, false>&
  1375. use_facet<moneypunct<wchar_t, false> >(const locale&);
  1376. extern template
  1377. const money_put<wchar_t>&
  1378. use_facet<money_put<wchar_t> >(const locale&);
  1379. extern template
  1380. const money_get<wchar_t>&
  1381. use_facet<money_get<wchar_t> >(const locale&);
  1382. extern template
  1383. const __timepunct<wchar_t>&
  1384. use_facet<__timepunct<wchar_t> >(const locale&);
  1385. extern template
  1386. const time_put<wchar_t>&
  1387. use_facet<time_put<wchar_t> >(const locale&);
  1388. extern template
  1389. const time_get<wchar_t>&
  1390. use_facet<time_get<wchar_t> >(const locale&);
  1391. extern template
  1392. const messages<wchar_t>&
  1393. use_facet<messages<wchar_t> >(const locale&);
  1394. extern template
  1395. bool
  1396. has_facet<moneypunct<wchar_t> >(const locale&);
  1397. extern template
  1398. bool
  1399. has_facet<money_put<wchar_t> >(const locale&);
  1400. extern template
  1401. bool
  1402. has_facet<money_get<wchar_t> >(const locale&);
  1403. extern template
  1404. bool
  1405. has_facet<__timepunct<wchar_t> >(const locale&);
  1406. extern template
  1407. bool
  1408. has_facet<time_put<wchar_t> >(const locale&);
  1409. extern template
  1410. bool
  1411. has_facet<time_get<wchar_t> >(const locale&);
  1412. extern template
  1413. bool
  1414. has_facet<messages<wchar_t> >(const locale&);
  1415. #endif
  1416. #endif
  1417. _GLIBCXX_END_NAMESPACE_VERSION
  1418. } // namespace std
  1419. #endif