locale_facets.tcc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. // Locale support -*- C++ -*-
  2. // Copyright (C) 1997-2022 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.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_TCC
  25. #define _LOCALE_FACETS_TCC 1
  26. #pragma GCC system_header
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. // Routine to access a cache for the facet. If the cache didn't
  31. // exist before, it gets constructed on the fly.
  32. template<typename _Facet>
  33. struct __use_cache
  34. {
  35. const _Facet*
  36. operator() (const locale& __loc) const;
  37. };
  38. // Specializations.
  39. template<typename _CharT>
  40. struct __use_cache<__numpunct_cache<_CharT> >
  41. {
  42. const __numpunct_cache<_CharT>*
  43. operator() (const locale& __loc) const
  44. {
  45. const size_t __i = numpunct<_CharT>::id._M_id();
  46. const locale::facet** __caches = __loc._M_impl->_M_caches;
  47. if (!__caches[__i])
  48. {
  49. __numpunct_cache<_CharT>* __tmp = 0;
  50. __try
  51. {
  52. __tmp = new __numpunct_cache<_CharT>;
  53. __tmp->_M_cache(__loc);
  54. }
  55. __catch(...)
  56. {
  57. delete __tmp;
  58. __throw_exception_again;
  59. }
  60. __loc._M_impl->_M_install_cache(__tmp, __i);
  61. }
  62. return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
  63. }
  64. };
  65. template<typename _CharT>
  66. void
  67. __numpunct_cache<_CharT>::_M_cache(const locale& __loc)
  68. {
  69. const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
  70. char* __grouping = 0;
  71. _CharT* __truename = 0;
  72. _CharT* __falsename = 0;
  73. __try
  74. {
  75. const string& __g = __np.grouping();
  76. _M_grouping_size = __g.size();
  77. __grouping = new char[_M_grouping_size];
  78. __g.copy(__grouping, _M_grouping_size);
  79. _M_use_grouping = (_M_grouping_size
  80. && static_cast<signed char>(__grouping[0]) > 0
  81. && (__grouping[0]
  82. != __gnu_cxx::__numeric_traits<char>::__max));
  83. const basic_string<_CharT>& __tn = __np.truename();
  84. _M_truename_size = __tn.size();
  85. __truename = new _CharT[_M_truename_size];
  86. __tn.copy(__truename, _M_truename_size);
  87. const basic_string<_CharT>& __fn = __np.falsename();
  88. _M_falsename_size = __fn.size();
  89. __falsename = new _CharT[_M_falsename_size];
  90. __fn.copy(__falsename, _M_falsename_size);
  91. _M_decimal_point = __np.decimal_point();
  92. _M_thousands_sep = __np.thousands_sep();
  93. const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
  94. __ct.widen(__num_base::_S_atoms_out,
  95. __num_base::_S_atoms_out
  96. + __num_base::_S_oend, _M_atoms_out);
  97. __ct.widen(__num_base::_S_atoms_in,
  98. __num_base::_S_atoms_in
  99. + __num_base::_S_iend, _M_atoms_in);
  100. _M_grouping = __grouping;
  101. _M_truename = __truename;
  102. _M_falsename = __falsename;
  103. _M_allocated = true;
  104. }
  105. __catch(...)
  106. {
  107. delete [] __grouping;
  108. delete [] __truename;
  109. delete [] __falsename;
  110. __throw_exception_again;
  111. }
  112. }
  113. // Used by both numeric and monetary facets.
  114. // Check to make sure that the __grouping_tmp string constructed in
  115. // money_get or num_get matches the canonical grouping for a given
  116. // locale.
  117. // __grouping_tmp is parsed L to R
  118. // 1,222,444 == __grouping_tmp of "\1\3\3"
  119. // __grouping is parsed R to L
  120. // 1,222,444 == __grouping of "\3" == "\3\3\3"
  121. _GLIBCXX_PURE bool
  122. __verify_grouping(const char* __grouping, size_t __grouping_size,
  123. const string& __grouping_tmp) throw ();
  124. _GLIBCXX_BEGIN_NAMESPACE_LDBL
  125. template<typename _CharT, typename _InIter>
  126. _GLIBCXX_DEFAULT_ABI_TAG
  127. _InIter
  128. num_get<_CharT, _InIter>::
  129. _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
  130. ios_base::iostate& __err, string& __xtrc) const
  131. {
  132. typedef char_traits<_CharT> __traits_type;
  133. typedef __numpunct_cache<_CharT> __cache_type;
  134. __use_cache<__cache_type> __uc;
  135. const locale& __loc = __io._M_getloc();
  136. const __cache_type* __lc = __uc(__loc);
  137. const _CharT* __lit = __lc->_M_atoms_in;
  138. char_type __c = char_type();
  139. // True if __beg becomes equal to __end.
  140. bool __testeof = __beg == __end;
  141. // First check for sign.
  142. if (!__testeof)
  143. {
  144. __c = *__beg;
  145. const bool __plus = __c == __lit[__num_base::_S_iplus];
  146. if ((__plus || __c == __lit[__num_base::_S_iminus])
  147. && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
  148. && !(__c == __lc->_M_decimal_point))
  149. {
  150. __xtrc += __plus ? '+' : '-';
  151. if (++__beg != __end)
  152. __c = *__beg;
  153. else
  154. __testeof = true;
  155. }
  156. }
  157. // Next, look for leading zeros.
  158. bool __found_mantissa = false;
  159. int __sep_pos = 0;
  160. while (!__testeof)
  161. {
  162. if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
  163. || __c == __lc->_M_decimal_point)
  164. break;
  165. else if (__c == __lit[__num_base::_S_izero])
  166. {
  167. if (!__found_mantissa)
  168. {
  169. __xtrc += '0';
  170. __found_mantissa = true;
  171. }
  172. ++__sep_pos;
  173. if (++__beg != __end)
  174. __c = *__beg;
  175. else
  176. __testeof = true;
  177. }
  178. else
  179. break;
  180. }
  181. // Only need acceptable digits for floating point numbers.
  182. bool __found_dec = false;
  183. bool __found_sci = false;
  184. string __found_grouping;
  185. if (__lc->_M_use_grouping)
  186. __found_grouping.reserve(32);
  187. const char_type* __lit_zero = __lit + __num_base::_S_izero;
  188. if (!__lc->_M_allocated)
  189. // "C" locale
  190. while (!__testeof)
  191. {
  192. const int __digit = _M_find(__lit_zero, 10, __c);
  193. if (__digit != -1)
  194. {
  195. __xtrc += '0' + __digit;
  196. __found_mantissa = true;
  197. }
  198. else if (__c == __lc->_M_decimal_point
  199. && !__found_dec && !__found_sci)
  200. {
  201. __xtrc += '.';
  202. __found_dec = true;
  203. }
  204. else if ((__c == __lit[__num_base::_S_ie]
  205. || __c == __lit[__num_base::_S_iE])
  206. && !__found_sci && __found_mantissa)
  207. {
  208. // Scientific notation.
  209. __xtrc += 'e';
  210. __found_sci = true;
  211. // Remove optional plus or minus sign, if they exist.
  212. if (++__beg != __end)
  213. {
  214. __c = *__beg;
  215. const bool __plus = __c == __lit[__num_base::_S_iplus];
  216. if (__plus || __c == __lit[__num_base::_S_iminus])
  217. __xtrc += __plus ? '+' : '-';
  218. else
  219. continue;
  220. }
  221. else
  222. {
  223. __testeof = true;
  224. break;
  225. }
  226. }
  227. else
  228. break;
  229. if (++__beg != __end)
  230. __c = *__beg;
  231. else
  232. __testeof = true;
  233. }
  234. else
  235. while (!__testeof)
  236. {
  237. // According to 22.2.2.1.2, p8-9, first look for thousands_sep
  238. // and decimal_point.
  239. if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
  240. {
  241. if (!__found_dec && !__found_sci)
  242. {
  243. // NB: Thousands separator at the beginning of a string
  244. // is a no-no, as is two consecutive thousands separators.
  245. if (__sep_pos)
  246. {
  247. __found_grouping += static_cast<char>(__sep_pos);
  248. __sep_pos = 0;
  249. }
  250. else
  251. {
  252. // NB: __convert_to_v will not assign __v and will
  253. // set the failbit.
  254. __xtrc.clear();
  255. break;
  256. }
  257. }
  258. else
  259. break;
  260. }
  261. else if (__c == __lc->_M_decimal_point)
  262. {
  263. if (!__found_dec && !__found_sci)
  264. {
  265. // If no grouping chars are seen, no grouping check
  266. // is applied. Therefore __found_grouping is adjusted
  267. // only if decimal_point comes after some thousands_sep.
  268. if (__found_grouping.size())
  269. __found_grouping += static_cast<char>(__sep_pos);
  270. __xtrc += '.';
  271. __found_dec = true;
  272. }
  273. else
  274. break;
  275. }
  276. else
  277. {
  278. const char_type* __q =
  279. __traits_type::find(__lit_zero, 10, __c);
  280. if (__q)
  281. {
  282. __xtrc += '0' + (__q - __lit_zero);
  283. __found_mantissa = true;
  284. ++__sep_pos;
  285. }
  286. else if ((__c == __lit[__num_base::_S_ie]
  287. || __c == __lit[__num_base::_S_iE])
  288. && !__found_sci && __found_mantissa)
  289. {
  290. // Scientific notation.
  291. if (__found_grouping.size() && !__found_dec)
  292. __found_grouping += static_cast<char>(__sep_pos);
  293. __xtrc += 'e';
  294. __found_sci = true;
  295. // Remove optional plus or minus sign, if they exist.
  296. if (++__beg != __end)
  297. {
  298. __c = *__beg;
  299. const bool __plus = __c == __lit[__num_base::_S_iplus];
  300. if ((__plus || __c == __lit[__num_base::_S_iminus])
  301. && !(__lc->_M_use_grouping
  302. && __c == __lc->_M_thousands_sep)
  303. && !(__c == __lc->_M_decimal_point))
  304. __xtrc += __plus ? '+' : '-';
  305. else
  306. continue;
  307. }
  308. else
  309. {
  310. __testeof = true;
  311. break;
  312. }
  313. }
  314. else
  315. break;
  316. }
  317. if (++__beg != __end)
  318. __c = *__beg;
  319. else
  320. __testeof = true;
  321. }
  322. // Digit grouping is checked. If grouping and found_grouping don't
  323. // match, then get very very upset, and set failbit.
  324. if (__found_grouping.size())
  325. {
  326. // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
  327. if (!__found_dec && !__found_sci)
  328. __found_grouping += static_cast<char>(__sep_pos);
  329. if (!std::__verify_grouping(__lc->_M_grouping,
  330. __lc->_M_grouping_size,
  331. __found_grouping))
  332. __err = ios_base::failbit;
  333. }
  334. return __beg;
  335. }
  336. template<typename _CharT, typename _InIter>
  337. template<typename _ValueT>
  338. _GLIBCXX_DEFAULT_ABI_TAG
  339. _InIter
  340. num_get<_CharT, _InIter>::
  341. _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
  342. ios_base::iostate& __err, _ValueT& __v) const
  343. {
  344. typedef char_traits<_CharT> __traits_type;
  345. using __gnu_cxx::__add_unsigned;
  346. typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
  347. typedef __numpunct_cache<_CharT> __cache_type;
  348. __use_cache<__cache_type> __uc;
  349. const locale& __loc = __io._M_getloc();
  350. const __cache_type* __lc = __uc(__loc);
  351. const _CharT* __lit = __lc->_M_atoms_in;
  352. char_type __c = char_type();
  353. // NB: Iff __basefield == 0, __base can change based on contents.
  354. const ios_base::fmtflags __basefield = __io.flags()
  355. & ios_base::basefield;
  356. const bool __oct = __basefield == ios_base::oct;
  357. int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
  358. // True if __beg becomes equal to __end.
  359. bool __testeof = __beg == __end;
  360. // First check for sign.
  361. bool __negative = false;
  362. if (!__testeof)
  363. {
  364. __c = *__beg;
  365. __negative = __c == __lit[__num_base::_S_iminus];
  366. if ((__negative || __c == __lit[__num_base::_S_iplus])
  367. && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
  368. && !(__c == __lc->_M_decimal_point))
  369. {
  370. if (++__beg != __end)
  371. __c = *__beg;
  372. else
  373. __testeof = true;
  374. }
  375. }
  376. // Next, look for leading zeros and check required digits
  377. // for base formats.
  378. bool __found_zero = false;
  379. int __sep_pos = 0;
  380. while (!__testeof)
  381. {
  382. if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
  383. || __c == __lc->_M_decimal_point)
  384. break;
  385. else if (__c == __lit[__num_base::_S_izero]
  386. && (!__found_zero || __base == 10))
  387. {
  388. __found_zero = true;
  389. ++__sep_pos;
  390. if (__basefield == 0)
  391. __base = 8;
  392. if (__base == 8)
  393. __sep_pos = 0;
  394. }
  395. else if (__found_zero
  396. && (__c == __lit[__num_base::_S_ix]
  397. || __c == __lit[__num_base::_S_iX]))
  398. {
  399. if (__basefield == 0)
  400. __base = 16;
  401. if (__base == 16)
  402. {
  403. __found_zero = false;
  404. __sep_pos = 0;
  405. }
  406. else
  407. break;
  408. }
  409. else
  410. break;
  411. if (++__beg != __end)
  412. {
  413. __c = *__beg;
  414. if (!__found_zero)
  415. break;
  416. }
  417. else
  418. __testeof = true;
  419. }
  420. // At this point, base is determined. If not hex, only allow
  421. // base digits as valid input.
  422. const size_t __len = (__base == 16 ? __num_base::_S_iend
  423. - __num_base::_S_izero : __base);
  424. // Extract.
  425. typedef __gnu_cxx::__numeric_traits<_ValueT> __num_traits;
  426. string __found_grouping;
  427. if (__lc->_M_use_grouping)
  428. __found_grouping.reserve(32);
  429. bool __testfail = false;
  430. bool __testoverflow = false;
  431. const __unsigned_type __max =
  432. (__negative && __num_traits::__is_signed)
  433. ? -static_cast<__unsigned_type>(__num_traits::__min)
  434. : __num_traits::__max;
  435. const __unsigned_type __smax = __max / __base;
  436. __unsigned_type __result = 0;
  437. int __digit = 0;
  438. const char_type* __lit_zero = __lit + __num_base::_S_izero;
  439. if (!__lc->_M_allocated)
  440. // "C" locale
  441. while (!__testeof)
  442. {
  443. __digit = _M_find(__lit_zero, __len, __c);
  444. if (__digit == -1)
  445. break;
  446. if (__result > __smax)
  447. __testoverflow = true;
  448. else
  449. {
  450. __result *= __base;
  451. __testoverflow |= __result > __max - __digit;
  452. __result += __digit;
  453. ++__sep_pos;
  454. }
  455. if (++__beg != __end)
  456. __c = *__beg;
  457. else
  458. __testeof = true;
  459. }
  460. else
  461. while (!__testeof)
  462. {
  463. // According to 22.2.2.1.2, p8-9, first look for thousands_sep
  464. // and decimal_point.
  465. if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
  466. {
  467. // NB: Thousands separator at the beginning of a string
  468. // is a no-no, as is two consecutive thousands separators.
  469. if (__sep_pos)
  470. {
  471. __found_grouping += static_cast<char>(__sep_pos);
  472. __sep_pos = 0;
  473. }
  474. else
  475. {
  476. __testfail = true;
  477. break;
  478. }
  479. }
  480. else if (__c == __lc->_M_decimal_point)
  481. break;
  482. else
  483. {
  484. const char_type* __q =
  485. __traits_type::find(__lit_zero, __len, __c);
  486. if (!__q)
  487. break;
  488. __digit = __q - __lit_zero;
  489. if (__digit > 15)
  490. __digit -= 6;
  491. if (__result > __smax)
  492. __testoverflow = true;
  493. else
  494. {
  495. __result *= __base;
  496. __testoverflow |= __result > __max - __digit;
  497. __result += __digit;
  498. ++__sep_pos;
  499. }
  500. }
  501. if (++__beg != __end)
  502. __c = *__beg;
  503. else
  504. __testeof = true;
  505. }
  506. // Digit grouping is checked. If grouping and found_grouping don't
  507. // match, then get very very upset, and set failbit.
  508. if (__found_grouping.size())
  509. {
  510. // Add the ending grouping.
  511. __found_grouping += static_cast<char>(__sep_pos);
  512. if (!std::__verify_grouping(__lc->_M_grouping,
  513. __lc->_M_grouping_size,
  514. __found_grouping))
  515. __err = ios_base::failbit;
  516. }
  517. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  518. // 23. Num_get overflow result.
  519. if ((!__sep_pos && !__found_zero && !__found_grouping.size())
  520. || __testfail)
  521. {
  522. __v = 0;
  523. __err = ios_base::failbit;
  524. }
  525. else if (__testoverflow)
  526. {
  527. if (__negative && __num_traits::__is_signed)
  528. __v = __num_traits::__min;
  529. else
  530. __v = __num_traits::__max;
  531. __err = ios_base::failbit;
  532. }
  533. else
  534. __v = __negative ? -__result : __result;
  535. if (__testeof)
  536. __err |= ios_base::eofbit;
  537. return __beg;
  538. }
  539. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  540. // 17. Bad bool parsing
  541. template<typename _CharT, typename _InIter>
  542. _InIter
  543. num_get<_CharT, _InIter>::
  544. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  545. ios_base::iostate& __err, bool& __v) const
  546. {
  547. if (!(__io.flags() & ios_base::boolalpha))
  548. {
  549. // Parse bool values as long.
  550. // NB: We can't just call do_get(long) here, as it might
  551. // refer to a derived class.
  552. long __l = -1;
  553. __beg = _M_extract_int(__beg, __end, __io, __err, __l);
  554. if (__l == 0 || __l == 1)
  555. __v = bool(__l);
  556. else
  557. {
  558. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  559. // 23. Num_get overflow result.
  560. __v = true;
  561. __err = ios_base::failbit;
  562. if (__beg == __end)
  563. __err |= ios_base::eofbit;
  564. }
  565. }
  566. else
  567. {
  568. // Parse bool values as alphanumeric.
  569. typedef __numpunct_cache<_CharT> __cache_type;
  570. __use_cache<__cache_type> __uc;
  571. const locale& __loc = __io._M_getloc();
  572. const __cache_type* __lc = __uc(__loc);
  573. bool __testf = true;
  574. bool __testt = true;
  575. bool __donef = __lc->_M_falsename_size == 0;
  576. bool __donet = __lc->_M_truename_size == 0;
  577. bool __testeof = false;
  578. size_t __n = 0;
  579. while (!__donef || !__donet)
  580. {
  581. if (__beg == __end)
  582. {
  583. __testeof = true;
  584. break;
  585. }
  586. const char_type __c = *__beg;
  587. if (!__donef)
  588. __testf = __c == __lc->_M_falsename[__n];
  589. if (!__testf && __donet)
  590. break;
  591. if (!__donet)
  592. __testt = __c == __lc->_M_truename[__n];
  593. if (!__testt && __donef)
  594. break;
  595. if (!__testt && !__testf)
  596. break;
  597. ++__n;
  598. ++__beg;
  599. __donef = !__testf || __n >= __lc->_M_falsename_size;
  600. __donet = !__testt || __n >= __lc->_M_truename_size;
  601. }
  602. if (__testf && __n == __lc->_M_falsename_size && __n)
  603. {
  604. __v = false;
  605. if (__testt && __n == __lc->_M_truename_size)
  606. __err = ios_base::failbit;
  607. else
  608. __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
  609. }
  610. else if (__testt && __n == __lc->_M_truename_size && __n)
  611. {
  612. __v = true;
  613. __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
  614. }
  615. else
  616. {
  617. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  618. // 23. Num_get overflow result.
  619. __v = false;
  620. __err = ios_base::failbit;
  621. if (__testeof)
  622. __err |= ios_base::eofbit;
  623. }
  624. }
  625. return __beg;
  626. }
  627. template<typename _CharT, typename _InIter>
  628. _InIter
  629. num_get<_CharT, _InIter>::
  630. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  631. ios_base::iostate& __err, float& __v) const
  632. {
  633. string __xtrc;
  634. __xtrc.reserve(32);
  635. __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
  636. std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
  637. if (__beg == __end)
  638. __err |= ios_base::eofbit;
  639. return __beg;
  640. }
  641. template<typename _CharT, typename _InIter>
  642. _InIter
  643. num_get<_CharT, _InIter>::
  644. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  645. ios_base::iostate& __err, double& __v) const
  646. {
  647. string __xtrc;
  648. __xtrc.reserve(32);
  649. __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
  650. std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
  651. if (__beg == __end)
  652. __err |= ios_base::eofbit;
  653. return __beg;
  654. }
  655. #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
  656. template<typename _CharT, typename _InIter>
  657. _InIter
  658. num_get<_CharT, _InIter>::
  659. __do_get(iter_type __beg, iter_type __end, ios_base& __io,
  660. ios_base::iostate& __err, double& __v) const
  661. {
  662. string __xtrc;
  663. __xtrc.reserve(32);
  664. __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
  665. std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
  666. if (__beg == __end)
  667. __err |= ios_base::eofbit;
  668. return __beg;
  669. }
  670. #endif
  671. template<typename _CharT, typename _InIter>
  672. _InIter
  673. num_get<_CharT, _InIter>::
  674. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  675. ios_base::iostate& __err, long double& __v) const
  676. {
  677. string __xtrc;
  678. __xtrc.reserve(32);
  679. __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
  680. std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
  681. if (__beg == __end)
  682. __err |= ios_base::eofbit;
  683. return __beg;
  684. }
  685. template<typename _CharT, typename _InIter>
  686. _InIter
  687. num_get<_CharT, _InIter>::
  688. do_get(iter_type __beg, iter_type __end, ios_base& __io,
  689. ios_base::iostate& __err, void*& __v) const
  690. {
  691. // Prepare for hex formatted input.
  692. typedef ios_base::fmtflags fmtflags;
  693. const fmtflags __fmt = __io.flags();
  694. __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
  695. typedef __gnu_cxx::__conditional_type<(sizeof(void*)
  696. <= sizeof(unsigned long)),
  697. unsigned long, unsigned long long>::__type _UIntPtrType;
  698. _UIntPtrType __ul;
  699. __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
  700. // Reset from hex formatted input.
  701. __io.flags(__fmt);
  702. __v = reinterpret_cast<void*>(__ul);
  703. return __beg;
  704. }
  705. #if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \
  706. && defined __LONG_DOUBLE_IEEE128__
  707. template<typename _CharT, typename _InIter>
  708. _InIter
  709. num_get<_CharT, _InIter>::
  710. __do_get(iter_type __beg, iter_type __end, ios_base& __io,
  711. ios_base::iostate& __err, __ibm128& __v) const
  712. {
  713. string __xtrc;
  714. __xtrc.reserve(32);
  715. __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
  716. std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
  717. if (__beg == __end)
  718. __err |= ios_base::eofbit;
  719. return __beg;
  720. }
  721. #endif
  722. // For use by integer and floating-point types after they have been
  723. // converted into a char_type string.
  724. template<typename _CharT, typename _OutIter>
  725. void
  726. num_put<_CharT, _OutIter>::
  727. _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
  728. _CharT* __new, const _CharT* __cs, int& __len) const
  729. {
  730. // [22.2.2.2.2] Stage 3.
  731. // If necessary, pad.
  732. __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
  733. __cs, __w, __len);
  734. __len = static_cast<int>(__w);
  735. }
  736. _GLIBCXX_END_NAMESPACE_LDBL
  737. template<typename _CharT, typename _ValueT>
  738. int
  739. __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
  740. ios_base::fmtflags __flags, bool __dec)
  741. {
  742. _CharT* __buf = __bufend;
  743. if (__builtin_expect(__dec, true))
  744. {
  745. // Decimal.
  746. do
  747. {
  748. *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
  749. __v /= 10;
  750. }
  751. while (__v != 0);
  752. }
  753. else if ((__flags & ios_base::basefield) == ios_base::oct)
  754. {
  755. // Octal.
  756. do
  757. {
  758. *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
  759. __v >>= 3;
  760. }
  761. while (__v != 0);
  762. }
  763. else
  764. {
  765. // Hex.
  766. const bool __uppercase = __flags & ios_base::uppercase;
  767. const int __case_offset = __uppercase ? __num_base::_S_oudigits
  768. : __num_base::_S_odigits;
  769. do
  770. {
  771. *--__buf = __lit[(__v & 0xf) + __case_offset];
  772. __v >>= 4;
  773. }
  774. while (__v != 0);
  775. }
  776. return __bufend - __buf;
  777. }
  778. _GLIBCXX_BEGIN_NAMESPACE_LDBL
  779. template<typename _CharT, typename _OutIter>
  780. void
  781. num_put<_CharT, _OutIter>::
  782. _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
  783. ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
  784. {
  785. _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
  786. __grouping_size, __cs, __cs + __len);
  787. __len = __p - __new;
  788. }
  789. template<typename _CharT, typename _OutIter>
  790. template<typename _ValueT>
  791. _OutIter
  792. num_put<_CharT, _OutIter>::
  793. _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
  794. _ValueT __v) const
  795. {
  796. using __gnu_cxx::__add_unsigned;
  797. typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
  798. typedef __numpunct_cache<_CharT> __cache_type;
  799. __use_cache<__cache_type> __uc;
  800. const locale& __loc = __io._M_getloc();
  801. const __cache_type* __lc = __uc(__loc);
  802. const _CharT* __lit = __lc->_M_atoms_out;
  803. const ios_base::fmtflags __flags = __io.flags();
  804. // Long enough to hold hex, dec, and octal representations.
  805. const int __ilen = 5 * sizeof(_ValueT);
  806. _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  807. * __ilen));
  808. // [22.2.2.2.2] Stage 1, numeric conversion to character.
  809. // Result is returned right-justified in the buffer.
  810. const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
  811. const bool __dec = (__basefield != ios_base::oct
  812. && __basefield != ios_base::hex);
  813. const __unsigned_type __u = ((__v > 0 || !__dec)
  814. ? __unsigned_type(__v)
  815. : -__unsigned_type(__v));
  816. int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
  817. __cs += __ilen - __len;
  818. // Add grouping, if necessary.
  819. if (__lc->_M_use_grouping)
  820. {
  821. // Grouping can add (almost) as many separators as the number
  822. // of digits + space is reserved for numeric base or sign.
  823. _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  824. * (__len + 1)
  825. * 2));
  826. _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
  827. __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
  828. __cs = __cs2 + 2;
  829. }
  830. // Complete Stage 1, prepend numeric base or sign.
  831. if (__builtin_expect(__dec, true))
  832. {
  833. // Decimal.
  834. if (__v >= 0)
  835. {
  836. if (bool(__flags & ios_base::showpos)
  837. && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
  838. *--__cs = __lit[__num_base::_S_oplus], ++__len;
  839. }
  840. else
  841. *--__cs = __lit[__num_base::_S_ominus], ++__len;
  842. }
  843. else if (bool(__flags & ios_base::showbase) && __v)
  844. {
  845. if (__basefield == ios_base::oct)
  846. *--__cs = __lit[__num_base::_S_odigits], ++__len;
  847. else
  848. {
  849. // 'x' or 'X'
  850. const bool __uppercase = __flags & ios_base::uppercase;
  851. *--__cs = __lit[__num_base::_S_ox + __uppercase];
  852. // '0'
  853. *--__cs = __lit[__num_base::_S_odigits];
  854. __len += 2;
  855. }
  856. }
  857. // Pad.
  858. const streamsize __w = __io.width();
  859. if (__w > static_cast<streamsize>(__len))
  860. {
  861. _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  862. * __w));
  863. _M_pad(__fill, __w, __io, __cs3, __cs, __len);
  864. __cs = __cs3;
  865. }
  866. __io.width(0);
  867. // [22.2.2.2.2] Stage 4.
  868. // Write resulting, fully-formatted string to output iterator.
  869. return std::__write(__s, __cs, __len);
  870. }
  871. template<typename _CharT, typename _OutIter>
  872. void
  873. num_put<_CharT, _OutIter>::
  874. _M_group_float(const char* __grouping, size_t __grouping_size,
  875. _CharT __sep, const _CharT* __p, _CharT* __new,
  876. _CharT* __cs, int& __len) const
  877. {
  878. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  879. // 282. What types does numpunct grouping refer to?
  880. // Add grouping, if necessary.
  881. const int __declen = __p ? __p - __cs : __len;
  882. _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
  883. __grouping_size,
  884. __cs, __cs + __declen);
  885. // Tack on decimal part.
  886. int __newlen = __p2 - __new;
  887. if (__p)
  888. {
  889. char_traits<_CharT>::copy(__p2, __p, __len - __declen);
  890. __newlen += __len - __declen;
  891. }
  892. __len = __newlen;
  893. }
  894. // The following code uses vsnprintf (or vsprintf(), when
  895. // _GLIBCXX_USE_C99_STDIO is not defined) to convert floating point
  896. // values for insertion into a stream. An optimization would be to
  897. // replace them with code that works directly on a wide buffer and
  898. // then use __pad to do the padding. It would be good to replace
  899. // them anyway to gain back the efficiency that C++ provides by
  900. // knowing up front the type of the values to insert. Also, sprintf
  901. // is dangerous since may lead to accidental buffer overruns. This
  902. // implementation follows the C++ standard fairly directly as
  903. // outlined in 22.2.2.2 [lib.locale.num.put]
  904. template<typename _CharT, typename _OutIter>
  905. template<typename _ValueT>
  906. _OutIter
  907. num_put<_CharT, _OutIter>::
  908. _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
  909. _ValueT __v) const
  910. {
  911. typedef __numpunct_cache<_CharT> __cache_type;
  912. __use_cache<__cache_type> __uc;
  913. const locale& __loc = __io._M_getloc();
  914. const __cache_type* __lc = __uc(__loc);
  915. // Use default precision if out of range.
  916. const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
  917. const int __max_digits =
  918. __gnu_cxx::__numeric_traits<_ValueT>::__digits10;
  919. // [22.2.2.2.2] Stage 1, numeric conversion to character.
  920. int __len;
  921. // Long enough for the max format spec.
  922. char __fbuf[16];
  923. __num_base::_S_format_float(__io, __fbuf, __mod);
  924. #if _GLIBCXX_USE_C99_STDIO && !_GLIBCXX_HAVE_BROKEN_VSNPRINTF
  925. // Precision is always used except for hexfloat format.
  926. const bool __use_prec =
  927. (__io.flags() & ios_base::floatfield) != ios_base::floatfield;
  928. // First try a buffer perhaps big enough (most probably sufficient
  929. // for non-ios_base::fixed outputs)
  930. int __cs_size = __max_digits * 3;
  931. char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  932. if (__use_prec)
  933. __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
  934. __fbuf, __prec, __v);
  935. else
  936. __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
  937. __fbuf, __v);
  938. // If the buffer was not large enough, try again with the correct size.
  939. if (__len >= __cs_size)
  940. {
  941. __cs_size = __len + 1;
  942. __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  943. if (__use_prec)
  944. __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
  945. __fbuf, __prec, __v);
  946. else
  947. __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
  948. __fbuf, __v);
  949. }
  950. #else
  951. // Consider the possibility of long ios_base::fixed outputs
  952. const bool __fixed = __io.flags() & ios_base::fixed;
  953. const int __max_exp =
  954. __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10;
  955. // The size of the output string is computed as follows.
  956. // ios_base::fixed outputs may need up to __max_exp + 1 chars
  957. // for the integer part + __prec chars for the fractional part
  958. // + 3 chars for sign, decimal point, '\0'. On the other hand,
  959. // for non-fixed outputs __max_digits * 2 + __prec chars are
  960. // largely sufficient.
  961. const int __cs_size = __fixed ? __max_exp + __prec + 4
  962. : __max_digits * 2 + __prec;
  963. char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
  964. __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf,
  965. __prec, __v);
  966. #endif
  967. // [22.2.2.2.2] Stage 2, convert to char_type, using correct
  968. // numpunct.decimal_point() values for '.' and adding grouping.
  969. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  970. _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  971. * __len));
  972. __ctype.widen(__cs, __cs + __len, __ws);
  973. // Replace decimal point.
  974. _CharT* __wp = 0;
  975. const char* __p = char_traits<char>::find(__cs, __len, '.');
  976. if (__p)
  977. {
  978. __wp = __ws + (__p - __cs);
  979. *__wp = __lc->_M_decimal_point;
  980. }
  981. // Add grouping, if necessary.
  982. // N.B. Make sure to not group things like 2e20, i.e., no decimal
  983. // point, scientific notation.
  984. if (__lc->_M_use_grouping
  985. && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
  986. && __cs[1] >= '0' && __cs[2] >= '0')))
  987. {
  988. // Grouping can add (almost) as many separators as the
  989. // number of digits, but no more.
  990. _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  991. * __len * 2));
  992. streamsize __off = 0;
  993. if (__cs[0] == '-' || __cs[0] == '+')
  994. {
  995. __off = 1;
  996. __ws2[0] = __ws[0];
  997. __len -= 1;
  998. }
  999. _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
  1000. __lc->_M_thousands_sep, __wp, __ws2 + __off,
  1001. __ws + __off, __len);
  1002. __len += __off;
  1003. __ws = __ws2;
  1004. }
  1005. // Pad.
  1006. const streamsize __w = __io.width();
  1007. if (__w > static_cast<streamsize>(__len))
  1008. {
  1009. _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  1010. * __w));
  1011. _M_pad(__fill, __w, __io, __ws3, __ws, __len);
  1012. __ws = __ws3;
  1013. }
  1014. __io.width(0);
  1015. // [22.2.2.2.2] Stage 4.
  1016. // Write resulting, fully-formatted string to output iterator.
  1017. return std::__write(__s, __ws, __len);
  1018. }
  1019. template<typename _CharT, typename _OutIter>
  1020. _OutIter
  1021. num_put<_CharT, _OutIter>::
  1022. do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
  1023. {
  1024. const ios_base::fmtflags __flags = __io.flags();
  1025. if ((__flags & ios_base::boolalpha) == 0)
  1026. {
  1027. const long __l = __v;
  1028. __s = _M_insert_int(__s, __io, __fill, __l);
  1029. }
  1030. else
  1031. {
  1032. typedef __numpunct_cache<_CharT> __cache_type;
  1033. __use_cache<__cache_type> __uc;
  1034. const locale& __loc = __io._M_getloc();
  1035. const __cache_type* __lc = __uc(__loc);
  1036. const _CharT* __name = __v ? __lc->_M_truename
  1037. : __lc->_M_falsename;
  1038. int __len = __v ? __lc->_M_truename_size
  1039. : __lc->_M_falsename_size;
  1040. const streamsize __w = __io.width();
  1041. if (__w > static_cast<streamsize>(__len))
  1042. {
  1043. const streamsize __plen = __w - __len;
  1044. _CharT* __ps
  1045. = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  1046. * __plen));
  1047. char_traits<_CharT>::assign(__ps, __plen, __fill);
  1048. __io.width(0);
  1049. if ((__flags & ios_base::adjustfield) == ios_base::left)
  1050. {
  1051. __s = std::__write(__s, __name, __len);
  1052. __s = std::__write(__s, __ps, __plen);
  1053. }
  1054. else
  1055. {
  1056. __s = std::__write(__s, __ps, __plen);
  1057. __s = std::__write(__s, __name, __len);
  1058. }
  1059. return __s;
  1060. }
  1061. __io.width(0);
  1062. __s = std::__write(__s, __name, __len);
  1063. }
  1064. return __s;
  1065. }
  1066. template<typename _CharT, typename _OutIter>
  1067. _OutIter
  1068. num_put<_CharT, _OutIter>::
  1069. do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
  1070. { return _M_insert_float(__s, __io, __fill, char(), __v); }
  1071. #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
  1072. template<typename _CharT, typename _OutIter>
  1073. _OutIter
  1074. num_put<_CharT, _OutIter>::
  1075. __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
  1076. { return _M_insert_float(__s, __io, __fill, char(), __v); }
  1077. #endif
  1078. template<typename _CharT, typename _OutIter>
  1079. _OutIter
  1080. num_put<_CharT, _OutIter>::
  1081. do_put(iter_type __s, ios_base& __io, char_type __fill,
  1082. long double __v) const
  1083. { return _M_insert_float(__s, __io, __fill, 'L', __v); }
  1084. template<typename _CharT, typename _OutIter>
  1085. _OutIter
  1086. num_put<_CharT, _OutIter>::
  1087. do_put(iter_type __s, ios_base& __io, char_type __fill,
  1088. const void* __v) const
  1089. {
  1090. const ios_base::fmtflags __flags = __io.flags();
  1091. const ios_base::fmtflags __fmt = ~(ios_base::basefield
  1092. | ios_base::uppercase);
  1093. __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
  1094. typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
  1095. <= sizeof(unsigned long)),
  1096. unsigned long, unsigned long long>::__type _UIntPtrType;
  1097. __s = _M_insert_int(__s, __io, __fill,
  1098. reinterpret_cast<_UIntPtrType>(__v));
  1099. __io.flags(__flags);
  1100. return __s;
  1101. }
  1102. #if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT \
  1103. && defined __LONG_DOUBLE_IEEE128__
  1104. template<typename _CharT, typename _OutIter>
  1105. _OutIter
  1106. num_put<_CharT, _OutIter>::
  1107. __do_put(iter_type __s, ios_base& __io, char_type __fill,
  1108. __ibm128 __v) const
  1109. { return _M_insert_float(__s, __io, __fill, 'L', __v); }
  1110. #endif
  1111. _GLIBCXX_END_NAMESPACE_LDBL
  1112. // Construct correctly padded string, as per 22.2.2.2.2
  1113. // Assumes
  1114. // __newlen > __oldlen
  1115. // __news is allocated for __newlen size
  1116. // NB: Of the two parameters, _CharT can be deduced from the
  1117. // function arguments. The other (_Traits) has to be explicitly specified.
  1118. template<typename _CharT, typename _Traits>
  1119. void
  1120. __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
  1121. _CharT* __news, const _CharT* __olds,
  1122. streamsize __newlen, streamsize __oldlen)
  1123. {
  1124. const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
  1125. const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
  1126. // Padding last.
  1127. if (__adjust == ios_base::left)
  1128. {
  1129. _Traits::copy(__news, __olds, __oldlen);
  1130. _Traits::assign(__news + __oldlen, __plen, __fill);
  1131. return;
  1132. }
  1133. size_t __mod = 0;
  1134. if (__adjust == ios_base::internal)
  1135. {
  1136. // Pad after the sign, if there is one.
  1137. // Pad after 0[xX], if there is one.
  1138. // Who came up with these rules, anyway? Jeeze.
  1139. const locale& __loc = __io._M_getloc();
  1140. const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
  1141. if (__ctype.widen('-') == __olds[0]
  1142. || __ctype.widen('+') == __olds[0])
  1143. {
  1144. __news[0] = __olds[0];
  1145. __mod = 1;
  1146. ++__news;
  1147. }
  1148. else if (__ctype.widen('0') == __olds[0]
  1149. && __oldlen > 1
  1150. && (__ctype.widen('x') == __olds[1]
  1151. || __ctype.widen('X') == __olds[1]))
  1152. {
  1153. __news[0] = __olds[0];
  1154. __news[1] = __olds[1];
  1155. __mod = 2;
  1156. __news += 2;
  1157. }
  1158. // else Padding first.
  1159. }
  1160. _Traits::assign(__news, __plen, __fill);
  1161. _Traits::copy(__news + __plen, __olds + __mod, __oldlen - __mod);
  1162. }
  1163. template<typename _CharT>
  1164. _CharT*
  1165. __add_grouping(_CharT* __s, _CharT __sep,
  1166. const char* __gbeg, size_t __gsize,
  1167. const _CharT* __first, const _CharT* __last)
  1168. {
  1169. size_t __idx = 0;
  1170. size_t __ctr = 0;
  1171. while (__last - __first > __gbeg[__idx]
  1172. && static_cast<signed char>(__gbeg[__idx]) > 0
  1173. && __gbeg[__idx] != __gnu_cxx::__numeric_traits<char>::__max)
  1174. {
  1175. __last -= __gbeg[__idx];
  1176. __idx < __gsize - 1 ? ++__idx : ++__ctr;
  1177. }
  1178. while (__first != __last)
  1179. *__s++ = *__first++;
  1180. while (__ctr--)
  1181. {
  1182. *__s++ = __sep;
  1183. for (char __i = __gbeg[__idx]; __i > 0; --__i)
  1184. *__s++ = *__first++;
  1185. }
  1186. while (__idx--)
  1187. {
  1188. *__s++ = __sep;
  1189. for (char __i = __gbeg[__idx]; __i > 0; --__i)
  1190. *__s++ = *__first++;
  1191. }
  1192. return __s;
  1193. }
  1194. // Inhibit implicit instantiations for required instantiations,
  1195. // which are defined via explicit instantiations elsewhere.
  1196. #if _GLIBCXX_EXTERN_TEMPLATE
  1197. extern template class _GLIBCXX_NAMESPACE_CXX11 numpunct<char>;
  1198. extern template class _GLIBCXX_NAMESPACE_CXX11 numpunct_byname<char>;
  1199. extern template class _GLIBCXX_NAMESPACE_LDBL num_get<char>;
  1200. extern template class _GLIBCXX_NAMESPACE_LDBL num_put<char>;
  1201. extern template class ctype_byname<char>;
  1202. extern template
  1203. const ctype<char>&
  1204. use_facet<ctype<char> >(const locale&);
  1205. extern template
  1206. const numpunct<char>&
  1207. use_facet<numpunct<char> >(const locale&);
  1208. extern template
  1209. const num_put<char>&
  1210. use_facet<num_put<char> >(const locale&);
  1211. extern template
  1212. const num_get<char>&
  1213. use_facet<num_get<char> >(const locale&);
  1214. extern template
  1215. bool
  1216. has_facet<ctype<char> >(const locale&);
  1217. extern template
  1218. bool
  1219. has_facet<numpunct<char> >(const locale&);
  1220. extern template
  1221. bool
  1222. has_facet<num_put<char> >(const locale&);
  1223. extern template
  1224. bool
  1225. has_facet<num_get<char> >(const locale&);
  1226. #ifdef _GLIBCXX_USE_WCHAR_T
  1227. extern template class _GLIBCXX_NAMESPACE_CXX11 numpunct<wchar_t>;
  1228. extern template class _GLIBCXX_NAMESPACE_CXX11 numpunct_byname<wchar_t>;
  1229. extern template class _GLIBCXX_NAMESPACE_LDBL num_get<wchar_t>;
  1230. extern template class _GLIBCXX_NAMESPACE_LDBL num_put<wchar_t>;
  1231. extern template class ctype_byname<wchar_t>;
  1232. extern template
  1233. const ctype<wchar_t>&
  1234. use_facet<ctype<wchar_t> >(const locale&);
  1235. extern template
  1236. const numpunct<wchar_t>&
  1237. use_facet<numpunct<wchar_t> >(const locale&);
  1238. extern template
  1239. const num_put<wchar_t>&
  1240. use_facet<num_put<wchar_t> >(const locale&);
  1241. extern template
  1242. const num_get<wchar_t>&
  1243. use_facet<num_get<wchar_t> >(const locale&);
  1244. extern template
  1245. bool
  1246. has_facet<ctype<wchar_t> >(const locale&);
  1247. extern template
  1248. bool
  1249. has_facet<numpunct<wchar_t> >(const locale&);
  1250. extern template
  1251. bool
  1252. has_facet<num_put<wchar_t> >(const locale&);
  1253. extern template
  1254. bool
  1255. has_facet<num_get<wchar_t> >(const locale&);
  1256. #endif
  1257. #endif
  1258. _GLIBCXX_END_NAMESPACE_VERSION
  1259. } // namespace
  1260. #endif