locale_facets.tcc (money_get<>::_M_extract): The value _space_ indicates that at...
[gcc.git] / libstdc++-v3 / include / bits / locale_facets.tcc
1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 // Warning: this file is not meant for user inclusion. Use <locale>.
32
33 #ifndef _LOCALE_FACETS_TCC
34 #define _LOCALE_FACETS_TCC 1
35
36 #pragma GCC system_header
37
38 #include <limits> // For numeric_limits
39 #include <typeinfo> // For bad_cast.
40 #include <bits/streambuf_iterator.h>
41
42 namespace std
43 {
44 template<typename _Facet>
45 locale
46 locale::combine(const locale& __other) const
47 {
48 _Impl* __tmp = new _Impl(*_M_impl, 1);
49 try
50 {
51 __tmp->_M_replace_facet(__other._M_impl, &_Facet::id);
52 }
53 catch(...)
54 {
55 __tmp->_M_remove_reference();
56 __throw_exception_again;
57 }
58 return locale(__tmp);
59 }
60
61 template<typename _CharT, typename _Traits, typename _Alloc>
62 bool
63 locale::operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1,
64 const basic_string<_CharT, _Traits, _Alloc>& __s2) const
65 {
66 typedef std::collate<_CharT> __collate_type;
67 const __collate_type& __collate = use_facet<__collate_type>(*this);
68 return (__collate.compare(__s1.data(), __s1.data() + __s1.length(),
69 __s2.data(), __s2.data() + __s2.length()) < 0);
70 }
71
72 /**
73 * @brief Test for the presence of a facet.
74 *
75 * has_facet tests the locale argument for the presence of the facet type
76 * provided as the template parameter. Facets derived from the facet
77 * parameter will also return true.
78 *
79 * @param Facet The facet type to test the presence of.
80 * @param locale The locale to test.
81 * @return true if locale contains a facet of type Facet, else false.
82 */
83 template<typename _Facet>
84 inline bool
85 has_facet(const locale& __loc) throw()
86 {
87 const size_t __i = _Facet::id._M_id();
88 const locale::facet** __facets = __loc._M_impl->_M_facets;
89 return (__i < __loc._M_impl->_M_facets_size && __facets[__i]);
90 }
91
92 /**
93 * @brief Return a facet.
94 *
95 * use_facet looks for and returns a reference to a facet of type Facet
96 * where Facet is the template parameter. If has_facet(locale) is true,
97 * there is a suitable facet to return. It throws std::bad_cast if the
98 * locale doesn't contain a facet of type Facet.
99 *
100 * @param Facet The facet type to access.
101 * @param locale The locale to use.
102 * @return Reference to facet of type Facet.
103 * @throw std::bad_cast if locale doesn't contain a facet of type Facet.
104 */
105 template<typename _Facet>
106 inline const _Facet&
107 use_facet(const locale& __loc)
108 {
109 const size_t __i = _Facet::id._M_id();
110 const locale::facet** __facets = __loc._M_impl->_M_facets;
111 if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i]))
112 __throw_bad_cast();
113 return static_cast<const _Facet&>(*__facets[__i]);
114 }
115
116 // Routine to access a cache for the facet. If the cache didn't
117 // exist before, it gets constructed on the fly.
118 template<typename _Facet>
119 struct __use_cache
120 {
121 const _Facet*
122 operator() (const locale& __loc) const;
123 };
124
125 template<typename _CharT>
126 struct __use_cache<__numpunct_cache<_CharT> >
127 {
128 const __numpunct_cache<_CharT>*
129 operator() (const locale& __loc) const
130 {
131 const size_t __i = numpunct<_CharT>::id._M_id();
132 const locale::facet** __caches = __loc._M_impl->_M_caches;
133 if (!__caches[__i])
134 {
135 __numpunct_cache<_CharT>* __tmp = NULL;
136 try
137 {
138 __tmp = new __numpunct_cache<_CharT>;
139 __tmp->_M_cache(__loc);
140 }
141 catch(...)
142 {
143 delete __tmp;
144 __throw_exception_again;
145 }
146 __loc._M_impl->_M_install_cache(__tmp, __i);
147 }
148 return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
149 }
150 };
151
152 // Used by both numeric and monetary facets.
153 // Check to make sure that the __grouping_tmp string constructed in
154 // money_get or num_get matches the canonical grouping for a given
155 // locale.
156 // __grouping_tmp is parsed L to R
157 // 1,222,444 == __grouping_tmp of "\1\3\3"
158 // __grouping is parsed R to L
159 // 1,222,444 == __grouping of "\3" == "\3\3\3"
160 static bool
161 __verify_grouping(const char* __grouping, size_t __grouping_size,
162 const string& __grouping_tmp);
163
164 template<typename _CharT, typename _InIter>
165 _InIter
166 num_get<_CharT, _InIter>::
167 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
168 ios_base::iostate& __err, string& __xtrc) const
169 {
170 typedef char_traits<_CharT> __traits_type;
171 typedef typename numpunct<_CharT>::__cache_type __cache_type;
172 __use_cache<__cache_type> __uc;
173 const locale& __loc = __io._M_getloc();
174 const __cache_type* __lc = __uc(__loc);
175 const _CharT* __lit = __lc->_M_atoms_in;
176
177 // True if a mantissa is found.
178 bool __found_mantissa = false;
179
180 // First check for sign.
181 if (__beg != __end)
182 {
183 const char_type __c = *__beg;
184 const bool __plus = __c == __lit[_S_iplus];
185 if ((__plus || __c == __lit[_S_iminus])
186 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
187 && !(__c == __lc->_M_decimal_point))
188 {
189 __xtrc += __plus ? '+' : '-';
190 ++__beg;
191 }
192 }
193
194 // Next, look for leading zeros.
195 while (__beg != __end)
196 {
197 const char_type __c = *__beg;
198 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep
199 || __c == __lc->_M_decimal_point)
200 break;
201 else if (__c == __lit[_S_izero])
202 {
203 if (!__found_mantissa)
204 {
205 __xtrc += '0';
206 __found_mantissa = true;
207 }
208 ++__beg;
209 }
210 else
211 break;
212 }
213
214 // Only need acceptable digits for floating point numbers.
215 bool __found_dec = false;
216 bool __found_sci = false;
217 string __found_grouping;
218 if (__lc->_M_use_grouping)
219 __found_grouping.reserve(32);
220 int __sep_pos = 0;
221 const char_type* __lit_zero = __lit + _S_izero;
222 const char_type* __q;
223 while (__beg != __end)
224 {
225 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
226 // and decimal_point.
227 const char_type __c = *__beg;
228 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
229 {
230 if (!__found_dec && !__found_sci)
231 {
232 // NB: Thousands separator at the beginning of a string
233 // is a no-no, as is two consecutive thousands separators.
234 if (__sep_pos)
235 {
236 __found_grouping += static_cast<char>(__sep_pos);
237 __sep_pos = 0;
238 ++__beg;
239 }
240 else
241 {
242 __err |= ios_base::failbit;
243 break;
244 }
245 }
246 else
247 break;
248 }
249 else if (__c == __lc->_M_decimal_point)
250 {
251 if (!__found_dec && !__found_sci)
252 {
253 // If no grouping chars are seen, no grouping check
254 // is applied. Therefore __found_grouping is adjusted
255 // only if decimal_point comes after some thousands_sep.
256 if (__found_grouping.size())
257 __found_grouping += static_cast<char>(__sep_pos);
258 __xtrc += '.';
259 __found_dec = true;
260 ++__beg;
261 }
262 else
263 break;
264 }
265 else if (__q = __traits_type::find(__lit_zero, 10, __c))
266 {
267 __xtrc += _S_atoms_in[__q - __lit];
268 __found_mantissa = true;
269 ++__sep_pos;
270 ++__beg;
271 }
272 else if ((__c == __lit[_S_ie] || __c == __lit[_S_iE])
273 && __found_mantissa && !__found_sci)
274 {
275 // Scientific notation.
276 if (__found_grouping.size() && !__found_dec)
277 __found_grouping += static_cast<char>(__sep_pos);
278 __xtrc += 'e';
279 __found_sci = true;
280
281 // Remove optional plus or minus sign, if they exist.
282 if (++__beg != __end)
283 {
284 const bool __plus = *__beg == __lit[_S_iplus];
285 if ((__plus || *__beg == __lit[_S_iminus])
286 && !(__lc->_M_use_grouping
287 && *__beg == __lc->_M_thousands_sep)
288 && !(*__beg == __lc->_M_decimal_point))
289 {
290 __xtrc += __plus ? '+' : '-';
291 ++__beg;
292 }
293 }
294 }
295 else
296 // Not a valid input item.
297 break;
298 }
299
300 // Digit grouping is checked. If grouping and found_grouping don't
301 // match, then get very very upset, and set failbit.
302 if (__found_grouping.size())
303 {
304 // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
305 if (!__found_dec && !__found_sci)
306 __found_grouping += static_cast<char>(__sep_pos);
307
308 if (!std::__verify_grouping(__lc->_M_grouping, __lc->_M_grouping_size,
309 __found_grouping))
310 __err |= ios_base::failbit;
311 }
312
313 // Finish up.
314 if (__beg == __end)
315 __err |= ios_base::eofbit;
316 return __beg;
317 }
318
319 template<typename _CharT, typename _InIter>
320 template<typename _ValueT>
321 _InIter
322 num_get<_CharT, _InIter>::
323 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
324 ios_base::iostate& __err, _ValueT& __v) const
325 {
326 typedef char_traits<_CharT> __traits_type;
327 typedef typename numpunct<_CharT>::__cache_type __cache_type;
328 __use_cache<__cache_type> __uc;
329 const locale& __loc = __io._M_getloc();
330 const __cache_type* __lc = __uc(__loc);
331 const _CharT* __lit = __lc->_M_atoms_in;
332
333 // NB: Iff __basefield == 0, __base can change based on contents.
334 const ios_base::fmtflags __basefield = __io.flags()
335 & ios_base::basefield;
336 const bool __oct = __basefield == ios_base::oct;
337 int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
338
339 // True if numeric digits are found.
340 bool __found_num = false;
341
342 // First check for sign.
343 bool __negative = false;
344 if (__beg != __end)
345 {
346 const char_type __c = *__beg;
347 if (numeric_limits<_ValueT>::is_signed)
348 __negative = __c == __lit[_S_iminus];
349 if ((__negative || __c == __lit[_S_iplus])
350 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
351 && !(__c == __lc->_M_decimal_point))
352 ++__beg;
353 }
354
355 // Next, look for leading zeros and check required digits
356 // for base formats.
357 while (__beg != __end)
358 {
359 const char_type __c = *__beg;
360 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep
361 || __c == __lc->_M_decimal_point)
362 break;
363 else if (__c == __lit[_S_izero] && (!__found_num || __base == 10))
364 {
365 __found_num = true;
366 ++__beg;
367 }
368 else if (__found_num)
369 {
370 if (__c == __lit[_S_ix] || __c == __lit[_S_iX])
371 {
372 if (__basefield == 0)
373 __base = 16;
374 if (__base == 16)
375 {
376 __found_num = false;
377 ++__beg;
378 }
379 }
380 else if (__basefield == 0)
381 __base = 8;
382 break;
383 }
384 else
385 break;
386 }
387
388 // At this point, base is determined. If not hex, only allow
389 // base digits as valid input.
390 const size_t __len = __base == 16 ? _S_iend - _S_izero : __base;
391
392 // Extract.
393 string __found_grouping;
394 if (__lc->_M_use_grouping)
395 __found_grouping.reserve(32);
396 int __sep_pos = 0;
397 bool __overflow = false;
398 _ValueT __result = 0;
399 const char_type* __lit_zero = __lit + _S_izero;
400 const char_type* __q;
401 if (__negative)
402 {
403 const _ValueT __min = numeric_limits<_ValueT>::min() / __base;
404 for (; __beg != __end; ++__beg)
405 {
406 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
407 // and decimal_point.
408 const char_type __c = *__beg;
409 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
410 {
411 // NB: Thousands separator at the beginning of a string
412 // is a no-no, as is two consecutive thousands separators.
413 if (__sep_pos)
414 {
415 __found_grouping += static_cast<char>(__sep_pos);
416 __sep_pos = 0;
417 }
418 else
419 {
420 __err |= ios_base::failbit;
421 break;
422 }
423 }
424 else if (__c == __lc->_M_decimal_point)
425 break;
426 else if (__q = __traits_type::find(__lit_zero, __len, __c))
427 {
428 int __digit = __q - __lit_zero;
429 if (__digit > 15)
430 __digit -= 6;
431 if (__result < __min)
432 __overflow = true;
433 else
434 {
435 const _ValueT __new_result = __result * __base
436 - __digit;
437 __overflow |= __new_result > __result;
438 __result = __new_result;
439 ++__sep_pos;
440 __found_num = true;
441 }
442 }
443 else
444 // Not a valid input item.
445 break;
446 }
447 }
448 else
449 {
450 const _ValueT __max = numeric_limits<_ValueT>::max() / __base;
451 for (; __beg != __end; ++__beg)
452 {
453 const char_type __c = *__beg;
454 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
455 {
456 if (__sep_pos)
457 {
458 __found_grouping += static_cast<char>(__sep_pos);
459 __sep_pos = 0;
460 }
461 else
462 {
463 __err |= ios_base::failbit;
464 break;
465 }
466 }
467 else if (__c == __lc->_M_decimal_point)
468 break;
469 else if (__q = __traits_type::find(__lit_zero, __len, __c))
470 {
471 int __digit = __q - __lit_zero;
472 if (__digit > 15)
473 __digit -= 6;
474 if (__result > __max)
475 __overflow = true;
476 else
477 {
478 const _ValueT __new_result = __result * __base
479 + __digit;
480 __overflow |= __new_result < __result;
481 __result = __new_result;
482 ++__sep_pos;
483 __found_num = true;
484 }
485 }
486 else
487 break;
488 }
489 }
490
491 // Digit grouping is checked. If grouping and found_grouping don't
492 // match, then get very very upset, and set failbit.
493 if (__found_grouping.size())
494 {
495 // Add the ending grouping.
496 __found_grouping += static_cast<char>(__sep_pos);
497
498 if (!std::__verify_grouping(__lc->_M_grouping,
499 __lc->_M_grouping_size,
500 __found_grouping))
501 __err |= ios_base::failbit;
502 }
503
504 if (!(__err & ios_base::failbit) && !__overflow
505 && __found_num)
506 __v = __result;
507 else
508 __err |= ios_base::failbit;
509
510 if (__beg == __end)
511 __err |= ios_base::eofbit;
512 return __beg;
513 }
514
515 // _GLIBCXX_RESOLVE_LIB_DEFECTS
516 // 17. Bad bool parsing
517 template<typename _CharT, typename _InIter>
518 _InIter
519 num_get<_CharT, _InIter>::
520 do_get(iter_type __beg, iter_type __end, ios_base& __io,
521 ios_base::iostate& __err, bool& __v) const
522 {
523 if (!(__io.flags() & ios_base::boolalpha))
524 {
525 // Parse bool values as long.
526 // NB: We can't just call do_get(long) here, as it might
527 // refer to a derived class.
528 long __l = -1;
529 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
530 if (__l == 0 || __l == 1)
531 __v = __l;
532 else
533 __err |= ios_base::failbit;
534 }
535 else
536 {
537 // Parse bool values as alphanumeric.
538 typedef char_traits<_CharT> __traits_type;
539 typedef typename numpunct<_CharT>::__cache_type __cache_type;
540 __use_cache<__cache_type> __uc;
541 const locale& __loc = __io._M_getloc();
542 const __cache_type* __lc = __uc(__loc);
543
544 bool __testf = true;
545 bool __testt = true;
546 size_t __n;
547 for (__n = 0; __beg != __end; ++__n, ++__beg)
548 {
549 if (__testf)
550 if (__n < __lc->_M_falsename_size)
551 __testf = *__beg == __lc->_M_falsename[__n];
552 else
553 break;
554
555 if (__testt)
556 if (__n < __lc->_M_truename_size)
557 __testt = *__beg == __lc->_M_truename[__n];
558 else
559 break;
560
561 if (!__testf && !__testt)
562 break;
563 }
564 if (__testf && __n == __lc->_M_falsename_size)
565 __v = 0;
566 else if (__testt && __n == __lc->_M_truename_size)
567 __v = 1;
568 else
569 __err |= ios_base::failbit;
570
571 if (__beg == __end)
572 __err |= ios_base::eofbit;
573 }
574 return __beg;
575 }
576
577 template<typename _CharT, typename _InIter>
578 _InIter
579 num_get<_CharT, _InIter>::
580 do_get(iter_type __beg, iter_type __end, ios_base& __io,
581 ios_base::iostate& __err, long& __v) const
582 { return _M_extract_int(__beg, __end, __io, __err, __v); }
583
584 template<typename _CharT, typename _InIter>
585 _InIter
586 num_get<_CharT, _InIter>::
587 do_get(iter_type __beg, iter_type __end, ios_base& __io,
588 ios_base::iostate& __err, unsigned short& __v) const
589 { return _M_extract_int(__beg, __end, __io, __err, __v); }
590
591 template<typename _CharT, typename _InIter>
592 _InIter
593 num_get<_CharT, _InIter>::
594 do_get(iter_type __beg, iter_type __end, ios_base& __io,
595 ios_base::iostate& __err, unsigned int& __v) const
596 { return _M_extract_int(__beg, __end, __io, __err, __v); }
597
598 template<typename _CharT, typename _InIter>
599 _InIter
600 num_get<_CharT, _InIter>::
601 do_get(iter_type __beg, iter_type __end, ios_base& __io,
602 ios_base::iostate& __err, unsigned long& __v) const
603 { return _M_extract_int(__beg, __end, __io, __err, __v); }
604
605 #ifdef _GLIBCXX_USE_LONG_LONG
606 template<typename _CharT, typename _InIter>
607 _InIter
608 num_get<_CharT, _InIter>::
609 do_get(iter_type __beg, iter_type __end, ios_base& __io,
610 ios_base::iostate& __err, long long& __v) const
611 { return _M_extract_int(__beg, __end, __io, __err, __v); }
612
613 template<typename _CharT, typename _InIter>
614 _InIter
615 num_get<_CharT, _InIter>::
616 do_get(iter_type __beg, iter_type __end, ios_base& __io,
617 ios_base::iostate& __err, unsigned long long& __v) const
618 { return _M_extract_int(__beg, __end, __io, __err, __v); }
619 #endif
620
621 template<typename _CharT, typename _InIter>
622 _InIter
623 num_get<_CharT, _InIter>::
624 do_get(iter_type __beg, iter_type __end, ios_base& __io,
625 ios_base::iostate& __err, float& __v) const
626 {
627 string __xtrc;
628 __xtrc.reserve(32);
629 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
630 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
631 return __beg;
632 }
633
634 template<typename _CharT, typename _InIter>
635 _InIter
636 num_get<_CharT, _InIter>::
637 do_get(iter_type __beg, iter_type __end, ios_base& __io,
638 ios_base::iostate& __err, double& __v) const
639 {
640 string __xtrc;
641 __xtrc.reserve(32);
642 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
643 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
644 return __beg;
645 }
646
647 template<typename _CharT, typename _InIter>
648 _InIter
649 num_get<_CharT, _InIter>::
650 do_get(iter_type __beg, iter_type __end, ios_base& __io,
651 ios_base::iostate& __err, long double& __v) const
652 {
653 string __xtrc;
654 __xtrc.reserve(32);
655 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
656 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
657 return __beg;
658 }
659
660 template<typename _CharT, typename _InIter>
661 _InIter
662 num_get<_CharT, _InIter>::
663 do_get(iter_type __beg, iter_type __end, ios_base& __io,
664 ios_base::iostate& __err, void*& __v) const
665 {
666 // Prepare for hex formatted input.
667 typedef ios_base::fmtflags fmtflags;
668 const fmtflags __fmt = __io.flags();
669 __io.flags(__fmt & ~ios_base::basefield | ios_base::hex);
670
671 unsigned long __ul;
672 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
673
674 // Reset from hex formatted input.
675 __io.flags(__fmt);
676
677 if (!(__err & ios_base::failbit))
678 __v = reinterpret_cast<void*>(__ul);
679 else
680 __err |= ios_base::failbit;
681 return __beg;
682 }
683
684 // For use by integer and floating-point types after they have been
685 // converted into a char_type string.
686 template<typename _CharT, typename _OutIter>
687 void
688 num_put<_CharT, _OutIter>::
689 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
690 _CharT* __new, const _CharT* __cs, int& __len) const
691 {
692 // [22.2.2.2.2] Stage 3.
693 // If necessary, pad.
694 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs,
695 __w, __len, true);
696 __len = static_cast<int>(__w);
697 }
698
699 // Forwarding functions to peel signed from unsigned integer types.
700 template<typename _CharT>
701 inline int
702 __int_to_char(_CharT* __bufend, long __v, const _CharT* __lit,
703 ios_base::fmtflags __flags)
704 {
705 unsigned long __ul = static_cast<unsigned long>(__v);
706 bool __neg = false;
707 if (__v < 0)
708 {
709 __ul = -__ul;
710 __neg = true;
711 }
712 return __int_to_char(__bufend, __ul, __lit, __flags, __neg);
713 }
714
715 template<typename _CharT>
716 inline int
717 __int_to_char(_CharT* __bufend, unsigned long __v, const _CharT* __lit,
718 ios_base::fmtflags __flags)
719 { return __int_to_char(__bufend, __v, __lit, __flags, false); }
720
721 #ifdef _GLIBCXX_USE_LONG_LONG
722 template<typename _CharT>
723 inline int
724 __int_to_char(_CharT* __bufend, long long __v, const _CharT* __lit,
725 ios_base::fmtflags __flags)
726 {
727 unsigned long long __ull = static_cast<unsigned long long>(__v);
728 bool __neg = false;
729 if (__v < 0)
730 {
731 __ull = -__ull;
732 __neg = true;
733 }
734 return __int_to_char(__bufend, __ull, __lit, __flags, __neg);
735 }
736
737 template<typename _CharT>
738 inline int
739 __int_to_char(_CharT* __bufend, unsigned long long __v, const _CharT* __lit,
740 ios_base::fmtflags __flags)
741 { return __int_to_char(__bufend, __v, __lit, __flags, false); }
742 #endif
743
744 template<typename _CharT, typename _ValueT>
745 int
746 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
747 ios_base::fmtflags __flags, bool __neg)
748 {
749 // Don't write base if already 0.
750 const bool __showbase = (__flags & ios_base::showbase) && __v;
751 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
752 _CharT* __buf = __bufend - 1;
753
754 if (__builtin_expect(__basefield != ios_base::oct &&
755 __basefield != ios_base::hex, true))
756 {
757 // Decimal.
758 do
759 {
760 *__buf-- = __lit[(__v % 10) + __num_base::_S_odigits];
761 __v /= 10;
762 }
763 while (__v != 0);
764 if (__neg)
765 *__buf-- = __lit[__num_base::_S_ominus];
766 else if (__flags & ios_base::showpos)
767 *__buf-- = __lit[__num_base::_S_oplus];
768 }
769 else if (__basefield == ios_base::oct)
770 {
771 // Octal.
772 do
773 {
774 *__buf-- = __lit[(__v & 0x7) + __num_base::_S_odigits];
775 __v >>= 3;
776 }
777 while (__v != 0);
778 if (__showbase)
779 *__buf-- = __lit[__num_base::_S_odigits];
780 }
781 else
782 {
783 // Hex.
784 const bool __uppercase = __flags & ios_base::uppercase;
785 const int __case_offset = __uppercase ? __num_base::_S_oudigits
786 : __num_base::_S_odigits;
787 do
788 {
789 *__buf-- = __lit[(__v & 0xf) + __case_offset];
790 __v >>= 4;
791 }
792 while (__v != 0);
793 if (__showbase)
794 {
795 // 'x' or 'X'
796 *__buf-- = __lit[__num_base::_S_ox + __uppercase];
797 // '0'
798 *__buf-- = __lit[__num_base::_S_odigits];
799 }
800 }
801 return __bufend - __buf - 1;
802 }
803
804 template<typename _CharT, typename _OutIter>
805 void
806 num_put<_CharT, _OutIter>::
807 _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
808 ios_base& __io, _CharT* __new, _CharT* __cs, int& __len) const
809 {
810 // By itself __add_grouping cannot deal correctly with __cs when
811 // ios::showbase is set and ios_base::oct || ios_base::hex.
812 // Therefore we take care "by hand" of the initial 0, 0x or 0X.
813 // However, remember that the latter do not occur if the number
814 // printed is '0' (__len == 1).
815 streamsize __off = 0;
816 const ios_base::fmtflags __basefield = __io.flags()
817 & ios_base::basefield;
818 if ((__io.flags() & ios_base::showbase) && __len > 1)
819 if (__basefield == ios_base::oct)
820 {
821 __off = 1;
822 __new[0] = __cs[0];
823 }
824 else if (__basefield == ios_base::hex)
825 {
826 __off = 2;
827 __new[0] = __cs[0];
828 __new[1] = __cs[1];
829 }
830 _CharT* __p;
831 __p = std::__add_grouping(__new + __off, __sep, __grouping,
832 __grouping_size, __cs + __off,
833 __cs + __len);
834 __len = __p - __new;
835 }
836
837 template<typename _CharT, typename _OutIter>
838 template<typename _ValueT>
839 _OutIter
840 num_put<_CharT, _OutIter>::
841 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
842 _ValueT __v) const
843 {
844 typedef typename numpunct<_CharT>::__cache_type __cache_type;
845 __use_cache<__cache_type> __uc;
846 const locale& __loc = __io._M_getloc();
847 const __cache_type* __lc = __uc(__loc);
848 const _CharT* __lit = __lc->_M_atoms_out;
849
850 // Long enough to hold hex, dec, and octal representations.
851 const int __ilen = 4 * sizeof(_ValueT);
852 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
853 * __ilen));
854
855 // [22.2.2.2.2] Stage 1, numeric conversion to character.
856 // Result is returned right-justified in the buffer.
857 int __len;
858 __len = __int_to_char(__cs + __ilen, __v, __lit, __io.flags());
859 __cs += __ilen - __len;
860
861 // Add grouping, if necessary.
862 if (__lc->_M_use_grouping)
863 {
864 // Grouping can add (almost) as many separators as the
865 // number of digits, but no more.
866 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
867 * __len * 2));
868 _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
869 __lc->_M_thousands_sep, __io, __cs2, __cs, __len);
870 __cs = __cs2;
871 }
872
873 // Pad.
874 const streamsize __w = __io.width();
875 if (__w > static_cast<streamsize>(__len))
876 {
877 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
878 * __w));
879 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
880 __cs = __cs3;
881 }
882 __io.width(0);
883
884 // [22.2.2.2.2] Stage 4.
885 // Write resulting, fully-formatted string to output iterator.
886 return std::__write(__s, __cs, __len);
887 }
888
889 template<typename _CharT, typename _OutIter>
890 void
891 num_put<_CharT, _OutIter>::
892 _M_group_float(const char* __grouping, size_t __grouping_size,
893 _CharT __sep, const _CharT* __p, _CharT* __new,
894 _CharT* __cs, int& __len) const
895 {
896 // _GLIBCXX_RESOLVE_LIB_DEFECTS
897 // 282. What types does numpunct grouping refer to?
898 // Add grouping, if necessary.
899 _CharT* __p2;
900 const int __declen = __p ? __p - __cs : __len;
901 __p2 = std::__add_grouping(__new, __sep, __grouping, __grouping_size,
902 __cs, __cs + __declen);
903
904 // Tack on decimal part.
905 int __newlen = __p2 - __new;
906 if (__p)
907 {
908 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
909 __newlen += __len - __declen;
910 }
911 __len = __newlen;
912 }
913
914 // The following code uses snprintf (or sprintf(), when
915 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
916 // for insertion into a stream. An optimization would be to replace
917 // them with code that works directly on a wide buffer and then use
918 // __pad to do the padding. It would be good to replace them anyway
919 // to gain back the efficiency that C++ provides by knowing up front
920 // the type of the values to insert. Also, sprintf is dangerous
921 // since may lead to accidental buffer overruns. This
922 // implementation follows the C++ standard fairly directly as
923 // outlined in 22.2.2.2 [lib.locale.num.put]
924 template<typename _CharT, typename _OutIter>
925 template<typename _ValueT>
926 _OutIter
927 num_put<_CharT, _OutIter>::
928 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
929 _ValueT __v) const
930 {
931 typedef typename numpunct<_CharT>::__cache_type __cache_type;
932 __use_cache<__cache_type> __uc;
933 const locale& __loc = __io._M_getloc();
934 const __cache_type* __lc = __uc(__loc);
935
936 // Note: digits10 is rounded down: add 1 to ensure the maximum
937 // available precision. Then, in general, one more 1 needs to
938 // be added since, when the %{g,G} conversion specifiers are
939 // chosen inside _S_format_float, the precision field is "the
940 // maximum number of significant digits", *not* the "number of
941 // digits to appear after the decimal point", as happens for
942 // %{e,E,f,F} (C99, 7.19.6.1,4).
943 const int __max_digits = numeric_limits<_ValueT>::digits10 + 2;
944
945 // Use default precision if out of range.
946 streamsize __prec = __io.precision();
947 if (__prec > static_cast<streamsize>(__max_digits))
948 __prec = static_cast<streamsize>(__max_digits);
949 else if (__prec < static_cast<streamsize>(0))
950 __prec = static_cast<streamsize>(6);
951
952 // [22.2.2.2.2] Stage 1, numeric conversion to character.
953 int __len;
954 // Long enough for the max format spec.
955 char __fbuf[16];
956
957 #ifdef _GLIBCXX_USE_C99
958 // First try a buffer perhaps big enough (for sure sufficient
959 // for non-ios_base::fixed outputs)
960 int __cs_size = __max_digits * 3;
961 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
962
963 _S_format_float(__io, __fbuf, __mod);
964 __len = std::__convert_from_v(__cs, __cs_size, __fbuf, __v,
965 _S_get_c_locale(), __prec);
966
967 // If the buffer was not large enough, try again with the correct size.
968 if (__len >= __cs_size)
969 {
970 __cs_size = __len + 1;
971 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
972 __len = std::__convert_from_v(__cs, __cs_size, __fbuf, __v,
973 _S_get_c_locale(), __prec);
974 }
975 #else
976 // Consider the possibility of long ios_base::fixed outputs
977 const bool __fixed = __io.flags() & ios_base::fixed;
978 const int __max_exp = numeric_limits<_ValueT>::max_exponent10;
979
980 // The size of the output string is computed as follows.
981 // ios_base::fixed outputs may need up to __max_exp+1 chars
982 // for the integer part + up to __max_digits chars for the
983 // fractional part + 3 chars for sign, decimal point, '\0'. On
984 // the other hand, for non-fixed outputs __max_digits*3 chars
985 // are largely sufficient.
986 const int __cs_size = __fixed ? __max_exp + __max_digits + 4
987 : __max_digits * 3;
988 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
989
990 _S_format_float(__io, __fbuf, __mod);
991 __len = std::__convert_from_v(__cs, 0, __fbuf, __v,
992 _S_get_c_locale(), __prec);
993 #endif
994
995 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
996 // numpunct.decimal_point() values for '.' and adding grouping.
997 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
998
999 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1000 * __len));
1001 __ctype.widen(__cs, __cs + __len, __ws);
1002
1003 // Replace decimal point.
1004 const _CharT __cdec = __ctype.widen('.');
1005 const _CharT __dec = __lc->_M_decimal_point;
1006 const _CharT* __p;
1007 if (__p = char_traits<_CharT>::find(__ws, __len, __cdec))
1008 __ws[__p - __ws] = __dec;
1009
1010 // Add grouping, if necessary.
1011 if (__lc->_M_use_grouping)
1012 {
1013 // Grouping can add (almost) as many separators as the
1014 // number of digits, but no more.
1015 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1016 * __len * 2));
1017 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1018 __lc->_M_thousands_sep, __p, __ws2, __ws, __len);
1019 __ws = __ws2;
1020 }
1021
1022 // Pad.
1023 const streamsize __w = __io.width();
1024 if (__w > static_cast<streamsize>(__len))
1025 {
1026 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1027 * __w));
1028 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1029 __ws = __ws3;
1030 }
1031 __io.width(0);
1032
1033 // [22.2.2.2.2] Stage 4.
1034 // Write resulting, fully-formatted string to output iterator.
1035 return std::__write(__s, __ws, __len);
1036 }
1037
1038 template<typename _CharT, typename _OutIter>
1039 _OutIter
1040 num_put<_CharT, _OutIter>::
1041 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1042 {
1043 const ios_base::fmtflags __flags = __io.flags();
1044 if ((__flags & ios_base::boolalpha) == 0)
1045 {
1046 unsigned long __uv = __v;
1047 __s = _M_insert_int(__s, __io, __fill, __uv);
1048 }
1049 else
1050 {
1051 typedef typename numpunct<_CharT>::__cache_type __cache_type;
1052 __use_cache<__cache_type> __uc;
1053 const locale& __loc = __io._M_getloc();
1054 const __cache_type* __lc = __uc(__loc);
1055
1056 const _CharT* __name = __v ? __lc->_M_truename
1057 : __lc->_M_falsename;
1058 int __len = __v ? __lc->_M_truename_size
1059 : __lc->_M_falsename_size;
1060
1061 const streamsize __w = __io.width();
1062 if (__w > static_cast<streamsize>(__len))
1063 {
1064 _CharT* __cs
1065 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1066 * __w));
1067 _M_pad(__fill, __w, __io, __cs, __name, __len);
1068 __name = __cs;
1069 }
1070 __io.width(0);
1071 __s = std::__write(__s, __name, __len);
1072 }
1073 return __s;
1074 }
1075
1076 template<typename _CharT, typename _OutIter>
1077 _OutIter
1078 num_put<_CharT, _OutIter>::
1079 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1080 { return _M_insert_int(__s, __io, __fill, __v); }
1081
1082 template<typename _CharT, typename _OutIter>
1083 _OutIter
1084 num_put<_CharT, _OutIter>::
1085 do_put(iter_type __s, ios_base& __io, char_type __fill,
1086 unsigned long __v) const
1087 { return _M_insert_int(__s, __io, __fill, __v); }
1088
1089 #ifdef _GLIBCXX_USE_LONG_LONG
1090 template<typename _CharT, typename _OutIter>
1091 _OutIter
1092 num_put<_CharT, _OutIter>::
1093 do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
1094 { return _M_insert_int(__s, __b, __fill, __v); }
1095
1096 template<typename _CharT, typename _OutIter>
1097 _OutIter
1098 num_put<_CharT, _OutIter>::
1099 do_put(iter_type __s, ios_base& __io, char_type __fill,
1100 unsigned long long __v) const
1101 { return _M_insert_int(__s, __io, __fill, __v); }
1102 #endif
1103
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, double __v) const
1108 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1109
1110 template<typename _CharT, typename _OutIter>
1111 _OutIter
1112 num_put<_CharT, _OutIter>::
1113 do_put(iter_type __s, ios_base& __io, char_type __fill,
1114 long double __v) const
1115 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1116
1117 template<typename _CharT, typename _OutIter>
1118 _OutIter
1119 num_put<_CharT, _OutIter>::
1120 do_put(iter_type __s, ios_base& __io, char_type __fill,
1121 const void* __v) const
1122 {
1123 const ios_base::fmtflags __flags = __io.flags();
1124 const ios_base::fmtflags __fmt = ~(ios_base::showpos
1125 | ios_base::basefield
1126 | ios_base::uppercase
1127 | ios_base::internal);
1128 __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase));
1129
1130 __s = _M_insert_int(__s, __io, __fill,
1131 reinterpret_cast<unsigned long>(__v));
1132 __io.flags(__flags);
1133 return __s;
1134 }
1135
1136 template<typename _CharT, bool _Intl>
1137 struct __use_cache<__moneypunct_cache<_CharT, _Intl> >
1138 {
1139 const __moneypunct_cache<_CharT, _Intl>*
1140 operator() (const locale& __loc) const
1141 {
1142 const size_t __i = moneypunct<_CharT, _Intl>::id._M_id();
1143 const locale::facet** __caches = __loc._M_impl->_M_caches;
1144 if (!__caches[__i])
1145 {
1146 __moneypunct_cache<_CharT, _Intl>* __tmp = NULL;
1147 try
1148 {
1149 __tmp = new __moneypunct_cache<_CharT, _Intl>;
1150 __tmp->_M_cache(__loc);
1151 }
1152 catch(...)
1153 {
1154 delete __tmp;
1155 __throw_exception_again;
1156 }
1157 __loc._M_impl->_M_install_cache(__tmp, __i);
1158 }
1159 return static_cast<
1160 const __moneypunct_cache<_CharT, _Intl>*>(__caches[__i]);
1161 }
1162 };
1163
1164 template<typename _CharT, typename _InIter>
1165 template<bool _Intl>
1166 _InIter
1167 money_get<_CharT, _InIter>::
1168 _M_extract(iter_type __beg, iter_type __end, ios_base& __io,
1169 ios_base::iostate& __err, string& __units) const
1170 {
1171 typedef char_traits<_CharT> __traits_type;
1172 typedef typename string_type::size_type size_type;
1173 typedef money_base::part part;
1174 typedef moneypunct<_CharT, _Intl> __moneypunct_type;
1175 typedef typename __moneypunct_type::__cache_type __cache_type;
1176
1177 const locale& __loc = __io._M_getloc();
1178 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1179
1180 __use_cache<__cache_type> __uc;
1181 const __cache_type* __lc = __uc(__loc);
1182 const char_type* __lit = __lc->_M_atoms;
1183
1184 // Deduced sign.
1185 bool __negative = false;
1186 // Sign size.
1187 size_type __sign_size = 0;
1188 // String of grouping info from thousands_sep plucked from __units.
1189 string __grouping_tmp;
1190 if (__lc->_M_use_grouping)
1191 __grouping_tmp.reserve(32);
1192 // Last position before the decimal point.
1193 int __last_pos = 0;
1194 // Separator positions, then, possibly, fractional digits.
1195 int __n = 0;
1196 // If input iterator is in a valid state.
1197 bool __testvalid = true;
1198 // Flag marking when a decimal point is found.
1199 bool __testdecfound = false;
1200
1201 // The tentative returned string is stored here.
1202 string __res;
1203 __res.reserve(32);
1204
1205 const char_type* __lit_zero = __lit + _S_zero;
1206 const char_type* __q;
1207 const money_base::pattern __p = __lc->_M_neg_format;
1208 for (int __i = 0; __i < 4 && __testvalid; ++__i)
1209 {
1210 const part __which = static_cast<part>(__p.field[__i]);
1211 switch (__which)
1212 {
1213 case money_base::symbol:
1214 if (__io.flags() & ios_base::showbase
1215 || __i < 2 || __sign_size > 1
1216 || ((static_cast<part>(__p.field[3]) != money_base::none)
1217 && __i == 2))
1218 {
1219 // According to 22.2.6.1.2, p2, symbol is required
1220 // if (__io.flags() & ios_base::showbase), otherwise
1221 // is optional and consumed only if other characters
1222 // are needed to complete the format.
1223 const size_type __len = __lc->_M_curr_symbol_size;
1224 size_type __j = 0;
1225 for (; __beg != __end && __j < __len
1226 && *__beg == __lc->_M_curr_symbol[__j];
1227 ++__beg, ++__j);
1228 if (__j != __len && (__io.flags() & ios_base::showbase))
1229 __testvalid = false;
1230 }
1231 break;
1232 case money_base::sign:
1233 // Sign might not exist, or be more than one character long.
1234 if (__lc->_M_positive_sign_size && __beg != __end
1235 && *__beg == __lc->_M_positive_sign[0])
1236 {
1237 __sign_size = __lc->_M_positive_sign_size;
1238 ++__beg;
1239 }
1240 else if (__lc->_M_negative_sign_size && __beg != __end
1241 && *__beg == __lc->_M_negative_sign[0])
1242 {
1243 __negative = true;
1244 __sign_size = __lc->_M_negative_sign_size;
1245 ++__beg;
1246 }
1247 else if (__lc->_M_positive_sign_size
1248 && !__lc->_M_negative_sign_size)
1249 // "... if no sign is detected, the result is given the sign
1250 // that corresponds to the source of the empty string"
1251 __negative = true;
1252 else if (__lc->_M_positive_sign_size
1253 && __lc->_M_negative_sign_size)
1254 {
1255 // Sign is mandatory.
1256 __testvalid = false;
1257 }
1258 break;
1259 case money_base::value:
1260 // Extract digits, remove and stash away the
1261 // grouping of found thousands separators.
1262 for (; __beg != __end; ++__beg)
1263 if (__q = __traits_type::find(__lit_zero, 10, *__beg))
1264 {
1265 __res += _S_atoms[__q - __lit];
1266 ++__n;
1267 }
1268 else if (*__beg == __lc->_M_decimal_point && !__testdecfound)
1269 {
1270 __last_pos = __n;
1271 __n = 0;
1272 __testdecfound = true;
1273 }
1274 else if (__lc->_M_use_grouping
1275 && *__beg == __lc->_M_thousands_sep
1276 && !__testdecfound)
1277 {
1278 if (__n)
1279 {
1280 // Mark position for later analysis.
1281 __grouping_tmp += static_cast<char>(__n);
1282 __n = 0;
1283 }
1284 else
1285 {
1286 __testvalid = false;
1287 break;
1288 }
1289 }
1290 else
1291 break;
1292 if (__res.empty())
1293 __testvalid = false;
1294 break;
1295 case money_base::space:
1296 // At least one space is required.
1297 if (__beg != __end && __ctype.is(ctype_base::space, *__beg))
1298 ++__beg;
1299 else
1300 __testvalid = false;
1301 case money_base::none:
1302 // Only if not at the end of the pattern.
1303 if (__i != 3)
1304 for (; __beg != __end
1305 && __ctype.is(ctype_base::space, *__beg); ++__beg);
1306 break;
1307 }
1308 }
1309
1310 // Need to get the rest of the sign characters, if they exist.
1311 if (__sign_size > 1 && __testvalid)
1312 {
1313 const char_type* __sign = __negative ? __lc->_M_negative_sign
1314 : __lc->_M_positive_sign;
1315 size_type __i = 1;
1316 for (; __beg != __end && __i < __sign_size
1317 && *__beg == __sign[__i]; ++__beg, ++__i);
1318
1319 if (__i != __sign_size)
1320 __testvalid = false;
1321 }
1322
1323 if (__testvalid)
1324 {
1325 // Strip leading zeros.
1326 if (__res.size() > 1)
1327 {
1328 const size_type __first = __res.find_first_not_of('0');
1329 const bool __only_zeros = __first == string::npos;
1330 if (__first)
1331 __res.erase(0, __only_zeros ? __res.size() - 1 : __first);
1332 }
1333
1334 // 22.2.6.1.2, p4
1335 if (__negative && __res[0] != '0')
1336 __res.insert(__res.begin(), '-');
1337
1338 // Test for grouping fidelity.
1339 if (__grouping_tmp.size())
1340 {
1341 // Add the ending grouping.
1342 __grouping_tmp += static_cast<char>(__testdecfound ? __last_pos
1343 : __n);
1344 if (!std::__verify_grouping(__lc->_M_grouping,
1345 __lc->_M_grouping_size,
1346 __grouping_tmp))
1347 __testvalid = false;
1348 }
1349
1350 // Iff not enough digits were supplied after the decimal-point.
1351 if (__testdecfound && __lc->_M_frac_digits > 0
1352 && __n != __lc->_M_frac_digits)
1353 __testvalid = false;
1354 }
1355
1356 // Iff no more characters are available.
1357 if (__beg == __end)
1358 __err |= ios_base::eofbit;
1359
1360 // Iff valid sequence is not recognized.
1361 if (!__testvalid)
1362 __err |= ios_base::failbit;
1363 else
1364 __units.swap(__res);
1365
1366 return __beg;
1367 }
1368
1369 template<typename _CharT, typename _InIter>
1370 _InIter
1371 money_get<_CharT, _InIter>::
1372 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1373 ios_base::iostate& __err, long double& __units) const
1374 {
1375 string __str;
1376 if (__intl)
1377 __beg = _M_extract<true>(__beg, __end, __io, __err, __str);
1378 else
1379 __beg = _M_extract<false>(__beg, __end, __io, __err, __str);
1380 std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
1381 return __beg;
1382 }
1383
1384 template<typename _CharT, typename _InIter>
1385 _InIter
1386 money_get<_CharT, _InIter>::
1387 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
1388 ios_base::iostate& __err, string_type& __units) const
1389 {
1390 typedef typename string::size_type size_type;
1391
1392 const locale& __loc = __io._M_getloc();
1393 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1394
1395 string __str;
1396 const iter_type __ret = __intl ? _M_extract<true>(__beg, __end, __io,
1397 __err, __str)
1398 : _M_extract<false>(__beg, __end, __io,
1399 __err, __str);
1400 const size_type __len = __str.size();
1401 if (__len)
1402 {
1403 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1404 * __len));
1405 __ctype.widen(__str.data(), __str.data() + __len, __ws);
1406 __units.assign(__ws, __len);
1407 }
1408
1409 return __ret;
1410 }
1411
1412 template<typename _CharT, typename _OutIter>
1413 template<bool _Intl>
1414 _OutIter
1415 money_put<_CharT, _OutIter>::
1416 _M_insert(iter_type __s, ios_base& __io, char_type __fill,
1417 const string_type& __digits) const
1418 {
1419 typedef typename string_type::size_type size_type;
1420 typedef money_base::part part;
1421 typedef moneypunct<_CharT, _Intl> __moneypunct_type;
1422 typedef typename __moneypunct_type::__cache_type __cache_type;
1423
1424 const locale& __loc = __io._M_getloc();
1425 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1426
1427 __use_cache<__cache_type> __uc;
1428 const __cache_type* __lc = __uc(__loc);
1429 const char_type* __lit = __lc->_M_atoms;
1430
1431 // Determine if negative or positive formats are to be used, and
1432 // discard leading negative_sign if it is present.
1433 const char_type* __beg = __digits.data();
1434
1435 money_base::pattern __p;
1436 const char_type* __sign;
1437 size_type __sign_size;
1438 if (*__beg != __lit[_S_minus])
1439 {
1440 __p = __lc->_M_pos_format;
1441 __sign = __lc->_M_positive_sign;
1442 __sign_size = __lc->_M_positive_sign_size;
1443 }
1444 else
1445 {
1446 __p = __lc->_M_neg_format;
1447 __sign = __lc->_M_negative_sign;
1448 __sign_size = __lc->_M_negative_sign_size;
1449 if (__digits.size())
1450 ++__beg;
1451 }
1452
1453 // Look for valid numbers in the ctype facet within input digits.
1454 size_type __len = __ctype.scan_not(ctype_base::digit, __beg,
1455 __beg + __digits.size()) - __beg;
1456 if (__len)
1457 {
1458 // Assume valid input, and attempt to format.
1459 // Break down input numbers into base components, as follows:
1460 // final_value = grouped units + (decimal point) + (digits)
1461 string_type __value;
1462 __value.reserve(2 * __len);
1463
1464 // Add thousands separators to non-decimal digits, per
1465 // grouping rules.
1466 int __paddec = __len - __lc->_M_frac_digits;
1467 if (__paddec > 0)
1468 {
1469 if (__lc->_M_frac_digits < 0)
1470 __paddec = __len;
1471 if (__lc->_M_grouping_size)
1472 {
1473 _CharT* __ws =
1474 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1475 * 2 * __len));
1476 _CharT* __ws_end =
1477 std::__add_grouping(__ws, __lc->_M_thousands_sep,
1478 __lc->_M_grouping,
1479 __lc->_M_grouping_size,
1480 __beg, __beg + __paddec);
1481 __value.assign(__ws, __ws_end - __ws);
1482 }
1483 else
1484 __value.assign(__beg, __paddec);
1485 }
1486
1487 // Deal with decimal point, decimal digits.
1488 if (__lc->_M_frac_digits > 0)
1489 {
1490 __value += __lc->_M_decimal_point;
1491 if (__paddec >= 0)
1492 __value.append(__beg + __paddec, __lc->_M_frac_digits);
1493 else
1494 {
1495 // Have to pad zeros in the decimal position.
1496 __value.append(-__paddec, __lit[_S_zero]);
1497 __value.append(__beg, __len);
1498 }
1499 }
1500
1501 // Calculate length of resulting string.
1502 const ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield;
1503 __len = __value.size() + __sign_size;
1504 __len += ((__io.flags() & ios_base::showbase)
1505 ? __lc->_M_curr_symbol_size : 0);
1506
1507 string_type __res;
1508 __res.reserve(2 * __len);
1509
1510 const size_type __width = static_cast<size_type>(__io.width());
1511 const bool __testipad = (__f == ios_base::internal
1512 && __len < __width);
1513 // Fit formatted digits into the required pattern.
1514 for (int __i = 0; __i < 4; ++__i)
1515 {
1516 const part __which = static_cast<part>(__p.field[__i]);
1517 switch (__which)
1518 {
1519 case money_base::symbol:
1520 if (__io.flags() & ios_base::showbase)
1521 __res.append(__lc->_M_curr_symbol,
1522 __lc->_M_curr_symbol_size);
1523 break;
1524 case money_base::sign:
1525 // Sign might not exist, or be more than one
1526 // charater long. In that case, add in the rest
1527 // below.
1528 if (__sign_size)
1529 __res += __sign[0];
1530 break;
1531 case money_base::value:
1532 __res += __value;
1533 break;
1534 case money_base::space:
1535 // At least one space is required, but if internal
1536 // formatting is required, an arbitrary number of
1537 // fill spaces will be necessary.
1538 if (__testipad)
1539 __res.append(__width - __len, __fill);
1540 else
1541 __res += __fill;
1542 break;
1543 case money_base::none:
1544 if (__testipad)
1545 __res.append(__width - __len, __fill);
1546 break;
1547 }
1548 }
1549
1550 // Special case of multi-part sign parts.
1551 if (__sign_size > 1)
1552 __res.append(__sign + 1, __sign_size - 1);
1553
1554 // Pad, if still necessary.
1555 __len = __res.size();
1556 if (__width > __len)
1557 {
1558 if (__f == ios_base::left)
1559 // After.
1560 __res.append(__width - __len, __fill);
1561 else
1562 // Before.
1563 __res.insert(0, __width - __len, __fill);
1564 __len = __width;
1565 }
1566
1567 // Write resulting, fully-formatted string to output iterator.
1568 __s = std::__write(__s, __res.data(), __len);
1569 }
1570 __io.width(0);
1571 return __s;
1572 }
1573
1574 template<typename _CharT, typename _OutIter>
1575 _OutIter
1576 money_put<_CharT, _OutIter>::
1577 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1578 long double __units) const
1579 {
1580 const locale __loc = __io.getloc();
1581 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1582 #ifdef _GLIBCXX_USE_C99
1583 // First try a buffer perhaps big enough.
1584 int __cs_size = 64;
1585 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1586 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1587 // 328. Bad sprintf format modifier in money_put<>::do_put()
1588 int __len = std::__convert_from_v(__cs, __cs_size, "%.0Lf", __units,
1589 _S_get_c_locale());
1590 // If the buffer was not large enough, try again with the correct size.
1591 if (__len >= __cs_size)
1592 {
1593 __cs_size = __len + 1;
1594 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1595 __len = std::__convert_from_v(__cs, __cs_size, "%.0Lf", __units,
1596 _S_get_c_locale());
1597 }
1598 #else
1599 // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'.
1600 const int __cs_size = numeric_limits<long double>::max_exponent10 + 3;
1601 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1602 int __len = std::__convert_from_v(__cs, 0, "%.0Lf", __units,
1603 _S_get_c_locale());
1604 #endif
1605 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1606 * __cs_size));
1607 __ctype.widen(__cs, __cs + __len, __ws);
1608 const string_type __digits(__ws, __len);
1609 return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
1610 : _M_insert<false>(__s, __io, __fill, __digits);
1611 }
1612
1613 template<typename _CharT, typename _OutIter>
1614 _OutIter
1615 money_put<_CharT, _OutIter>::
1616 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
1617 const string_type& __digits) const
1618 { return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
1619 : _M_insert<false>(__s, __io, __fill, __digits); }
1620
1621 // NB: Not especially useful. Without an ios_base object or some
1622 // kind of locale reference, we are left clawing at the air where
1623 // the side of the mountain used to be...
1624 template<typename _CharT, typename _InIter>
1625 time_base::dateorder
1626 time_get<_CharT, _InIter>::do_date_order() const
1627 { return time_base::no_order; }
1628
1629 // Recursively expand a strftime format string and parse it. Starts w/ %x
1630 // and %X from do_get_time() and do_get_date(), which translate to a more
1631 // specific string, which may contain yet more strings. I.e. %x => %r =>
1632 // %H:%M:%S => extracted characters.
1633 template<typename _CharT, typename _InIter>
1634 void
1635 time_get<_CharT, _InIter>::
1636 _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io,
1637 ios_base::iostate& __err, tm* __tm,
1638 const _CharT* __format) const
1639 {
1640 const locale __loc = __io.getloc();
1641 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1642 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1643 const size_t __len = char_traits<_CharT>::length(__format);
1644
1645 for (size_t __i = 0; __beg != __end && __i < __len && !__err; ++__i)
1646 {
1647 if (__ctype.narrow(__format[__i], 0) == '%')
1648 {
1649 // Verify valid formatting code, attempt to extract.
1650 char __c = __ctype.narrow(__format[++__i], 0);
1651 int __mem = 0;
1652 if (__c == 'E' || __c == 'O')
1653 __c = __ctype.narrow(__format[++__i], 0);
1654 switch (__c)
1655 {
1656 const char* __cs;
1657 _CharT __wcs[10];
1658 case 'a':
1659 // Abbreviated weekday name [tm_wday]
1660 const char_type* __days1[7];
1661 __tp._M_days_abbreviated(__days1);
1662 _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7,
1663 __ctype, __err);
1664 break;
1665 case 'A':
1666 // Weekday name [tm_wday].
1667 const char_type* __days2[7];
1668 __tp._M_days(__days2);
1669 _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7,
1670 __ctype, __err);
1671 break;
1672 case 'h':
1673 case 'b':
1674 // Abbreviated month name [tm_mon]
1675 const char_type* __months1[12];
1676 __tp._M_months_abbreviated(__months1);
1677 _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12,
1678 __ctype, __err);
1679 break;
1680 case 'B':
1681 // Month name [tm_mon].
1682 const char_type* __months2[12];
1683 __tp._M_months(__months2);
1684 _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12,
1685 __ctype, __err);
1686 break;
1687 case 'c':
1688 // Default time and date representation.
1689 const char_type* __dt[2];
1690 __tp._M_date_time_formats(__dt);
1691 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1692 __dt[0]);
1693 break;
1694 case 'd':
1695 // Day [01, 31]. [tm_mday]
1696 _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
1697 __ctype, __err);
1698 break;
1699 case 'e':
1700 // Day [1, 31], with single digits preceded by
1701 // space. [tm_mday]
1702 if (__ctype.is(ctype_base::space, *__beg))
1703 _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9, 1,
1704 __ctype, __err);
1705 else
1706 _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31, 2,
1707 __ctype, __err);
1708 break;
1709 case 'D':
1710 // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
1711 __cs = "%m/%d/%y";
1712 __ctype.widen(__cs, __cs + 9, __wcs);
1713 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1714 __wcs);
1715 break;
1716 case 'H':
1717 // Hour [00, 23]. [tm_hour]
1718 _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
1719 __ctype, __err);
1720 break;
1721 case 'I':
1722 // Hour [01, 12]. [tm_hour]
1723 _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
1724 __ctype, __err);
1725 break;
1726 case 'm':
1727 // Month [01, 12]. [tm_mon]
1728 _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype,
1729 __err);
1730 if (!__err)
1731 __tm->tm_mon = __mem - 1;
1732 break;
1733 case 'M':
1734 // Minute [00, 59]. [tm_min]
1735 _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
1736 __ctype, __err);
1737 break;
1738 case 'n':
1739 if (__ctype.narrow(*__beg, 0) == '\n')
1740 ++__beg;
1741 else
1742 __err |= ios_base::failbit;
1743 break;
1744 case 'R':
1745 // Equivalent to (%H:%M).
1746 __cs = "%H:%M";
1747 __ctype.widen(__cs, __cs + 6, __wcs);
1748 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1749 __wcs);
1750 break;
1751 case 'S':
1752 // Seconds.
1753 _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2,
1754 __ctype, __err);
1755 break;
1756 case 't':
1757 if (__ctype.narrow(*__beg, 0) == '\t')
1758 ++__beg;
1759 else
1760 __err |= ios_base::failbit;
1761 break;
1762 case 'T':
1763 // Equivalent to (%H:%M:%S).
1764 __cs = "%H:%M:%S";
1765 __ctype.widen(__cs, __cs + 9, __wcs);
1766 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1767 __wcs);
1768 break;
1769 case 'x':
1770 // Locale's date.
1771 const char_type* __dates[2];
1772 __tp._M_date_formats(__dates);
1773 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1774 __dates[0]);
1775 break;
1776 case 'X':
1777 // Locale's time.
1778 const char_type* __times[2];
1779 __tp._M_time_formats(__times);
1780 _M_extract_via_format(__beg, __end, __io, __err, __tm,
1781 __times[0]);
1782 break;
1783 case 'y':
1784 case 'C': // C99
1785 // Two digit year. [tm_year]
1786 _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
1787 __ctype, __err);
1788 break;
1789 case 'Y':
1790 // Year [1900). [tm_year]
1791 _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
1792 __ctype, __err);
1793 if (!__err)
1794 __tm->tm_year = __mem - 1900;
1795 break;
1796 case 'Z':
1797 // Timezone info.
1798 if (__ctype.is(ctype_base::upper, *__beg))
1799 {
1800 int __tmp;
1801 _M_extract_name(__beg, __end, __tmp,
1802 __timepunct_cache<_CharT>::_S_timezones,
1803 14, __ctype, __err);
1804
1805 // GMT requires special effort.
1806 if (__beg != __end && !__err && __tmp == 0
1807 && (*__beg == __ctype.widen('-')
1808 || *__beg == __ctype.widen('+')))
1809 {
1810 _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
1811 __ctype, __err);
1812 _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
1813 __ctype, __err);
1814 }
1815 }
1816 else
1817 __err |= ios_base::failbit;
1818 break;
1819 default:
1820 // Not recognized.
1821 __err |= ios_base::failbit;
1822 }
1823 }
1824 else
1825 {
1826 // Verify format and input match, extract and discard.
1827 if (__format[__i] == *__beg)
1828 ++__beg;
1829 else
1830 __err |= ios_base::failbit;
1831 }
1832 }
1833 }
1834
1835 template<typename _CharT, typename _InIter>
1836 void
1837 time_get<_CharT, _InIter>::
1838 _M_extract_num(iter_type& __beg, iter_type& __end, int& __member,
1839 int __min, int __max, size_t __len,
1840 const ctype<_CharT>& __ctype,
1841 ios_base::iostate& __err) const
1842 {
1843 // As-is works for __len = 1, 2, 4, the values actually used.
1844 int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1);
1845
1846 ++__min;
1847 size_t __i = 0;
1848 int __value = 0;
1849 for (; __beg != __end && __i < __len; ++__beg, ++__i)
1850 {
1851 const char __c = __ctype.narrow(*__beg, '*');
1852 if (__c >= '0' && __c <= '9')
1853 {
1854 __value = __value * 10 + (__c - '0');
1855 const int __valuec = __value * __mult;
1856 if (__valuec > __max || __valuec + __mult < __min)
1857 break;
1858 __mult /= 10;
1859 }
1860 else
1861 break;
1862 }
1863 if (__i == __len)
1864 __member = __value;
1865 else
1866 __err |= ios_base::failbit;
1867 }
1868
1869 // Assumptions:
1870 // All elements in __names are unique.
1871 template<typename _CharT, typename _InIter>
1872 void
1873 time_get<_CharT, _InIter>::
1874 _M_extract_name(iter_type& __beg, iter_type& __end, int& __member,
1875 const _CharT** __names, size_t __indexlen,
1876 const ctype<_CharT>& __ctype,
1877 ios_base::iostate& __err) const
1878 {
1879 typedef char_traits<_CharT> __traits_type;
1880 int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
1881 * __indexlen));
1882 size_t __nmatches = 0;
1883 size_t __pos = 0;
1884 bool __testvalid = true;
1885 const char_type* __name;
1886
1887 // Look for initial matches.
1888 // NB: Some of the locale data is in the form of all lowercase
1889 // names, and some is in the form of initially-capitalized
1890 // names. Look for both.
1891 if (__beg != __end)
1892 {
1893 const char_type __c = *__beg;
1894 for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
1895 if (__c == __names[__i1][0]
1896 || __c == __ctype.toupper(__names[__i1][0]))
1897 __matches[__nmatches++] = __i1;
1898 }
1899
1900 while (__nmatches > 1)
1901 {
1902 // Find smallest matching string.
1903 size_t __minlen = 10;
1904 for (size_t __i2 = 0; __i2 < __nmatches; ++__i2)
1905 __minlen = std::min(__minlen,
1906 __traits_type::length(__names[__matches[__i2]]));
1907 ++__beg;
1908 if (__pos < __minlen && __beg != __end)
1909 {
1910 ++__pos;
1911 for (size_t __i3 = 0; __i3 < __nmatches; ++__i3)
1912 {
1913 __name = __names[__matches[__i3]];
1914 if (__name[__pos] != *__beg)
1915 __matches[__i3] = __matches[--__nmatches];
1916 }
1917 }
1918 else
1919 break;
1920 }
1921
1922 if (__nmatches == 1)
1923 {
1924 // If there was only one match, the first compare is redundant.
1925 if (__pos == 0)
1926 {
1927 ++__pos;
1928 ++__beg;
1929 }
1930
1931 // Make sure found name is completely extracted.
1932 __name = __names[__matches[0]];
1933 const size_t __len = __traits_type::length(__name);
1934 while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
1935 ++__beg, ++__pos;
1936
1937 if (__len == __pos)
1938 __member = __matches[0];
1939 else
1940 __testvalid = false;
1941 }
1942 else
1943 __testvalid = false;
1944 if (!__testvalid)
1945 __err |= ios_base::failbit;
1946 }
1947
1948 template<typename _CharT, typename _InIter>
1949 _InIter
1950 time_get<_CharT, _InIter>::
1951 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
1952 ios_base::iostate& __err, tm* __tm) const
1953 {
1954 _CharT __wcs[3];
1955 const char* __cs = "%X";
1956 const locale __loc = __io.getloc();
1957 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1958 __ctype.widen(__cs, __cs + 3, __wcs);
1959 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1960 if (__beg == __end)
1961 __err |= ios_base::eofbit;
1962 return __beg;
1963 }
1964
1965 template<typename _CharT, typename _InIter>
1966 _InIter
1967 time_get<_CharT, _InIter>::
1968 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
1969 ios_base::iostate& __err, tm* __tm) const
1970 {
1971 _CharT __wcs[3];
1972 const char* __cs = "%x";
1973 const locale __loc = __io.getloc();
1974 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1975 __ctype.widen(__cs, __cs + 3, __wcs);
1976 _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs);
1977 if (__beg == __end)
1978 __err |= ios_base::eofbit;
1979 return __beg;
1980 }
1981
1982 template<typename _CharT, typename _InIter>
1983 _InIter
1984 time_get<_CharT, _InIter>::
1985 do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
1986 ios_base::iostate& __err, tm* __tm) const
1987 {
1988 typedef char_traits<_CharT> __traits_type;
1989 const locale __loc = __io.getloc();
1990 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1991 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1992 const char_type* __days[7];
1993 __tp._M_days_abbreviated(__days);
1994 int __tmpwday;
1995 _M_extract_name(__beg, __end, __tmpwday, __days, 7, __ctype, __err);
1996
1997 // Check to see if non-abbreviated name exists, and extract.
1998 // NB: Assumes both _M_days and _M_days_abbreviated organized in
1999 // exact same order, first to last, such that the resulting
2000 // __days array with the same index points to a day, and that
2001 // day's abbreviated form.
2002 // NB: Also assumes that an abbreviated name is a subset of the name.
2003 if (!__err)
2004 {
2005 size_t __pos = __traits_type::length(__days[__tmpwday]);
2006 __tp._M_days(__days);
2007 const char_type* __name = __days[__tmpwday];
2008 if (__name[__pos] == *__beg)
2009 {
2010 // Extract the rest of it.
2011 const size_t __len = __traits_type::length(__name);
2012 while (__pos < __len && __beg != __end
2013 && __name[__pos] == *__beg)
2014 ++__beg, ++__pos;
2015 if (__len != __pos)
2016 __err |= ios_base::failbit;
2017 }
2018 if (!__err)
2019 __tm->tm_wday = __tmpwday;
2020 }
2021 if (__beg == __end)
2022 __err |= ios_base::eofbit;
2023 return __beg;
2024 }
2025
2026 template<typename _CharT, typename _InIter>
2027 _InIter
2028 time_get<_CharT, _InIter>::
2029 do_get_monthname(iter_type __beg, iter_type __end,
2030 ios_base& __io, ios_base::iostate& __err, tm* __tm) const
2031 {
2032 typedef char_traits<_CharT> __traits_type;
2033 const locale __loc = __io.getloc();
2034 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
2035 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2036 const char_type* __months[12];
2037 __tp._M_months_abbreviated(__months);
2038 int __tmpmon;
2039 _M_extract_name(__beg, __end, __tmpmon, __months, 12, __ctype, __err);
2040
2041 // Check to see if non-abbreviated name exists, and extract.
2042 // NB: Assumes both _M_months and _M_months_abbreviated organized in
2043 // exact same order, first to last, such that the resulting
2044 // __months array with the same index points to a month, and that
2045 // month's abbreviated form.
2046 // NB: Also assumes that an abbreviated name is a subset of the name.
2047 if (!__err)
2048 {
2049 size_t __pos = __traits_type::length(__months[__tmpmon]);
2050 __tp._M_months(__months);
2051 const char_type* __name = __months[__tmpmon];
2052 if (__name[__pos] == *__beg)
2053 {
2054 // Extract the rest of it.
2055 const size_t __len = __traits_type::length(__name);
2056 while (__pos < __len && __beg != __end
2057 && __name[__pos] == *__beg)
2058 ++__beg, ++__pos;
2059 if (__len != __pos)
2060 __err |= ios_base::failbit;
2061 }
2062 if (!__err)
2063 __tm->tm_mon = __tmpmon;
2064 }
2065
2066 if (__beg == __end)
2067 __err |= ios_base::eofbit;
2068 return __beg;
2069 }
2070
2071 template<typename _CharT, typename _InIter>
2072 _InIter
2073 time_get<_CharT, _InIter>::
2074 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
2075 ios_base::iostate& __err, tm* __tm) const
2076 {
2077 const locale __loc = __io.getloc();
2078 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2079
2080 size_t __i = 0;
2081 int __value = 0;
2082 for (; __beg != __end && __i < 4; ++__beg, ++__i)
2083 {
2084 const char __c = __ctype.narrow(*__beg, '*');
2085 if (__c >= '0' && __c <= '9')
2086 __value = __value * 10 + (__c - '0');
2087 else
2088 break;
2089 }
2090 if (__i == 2 || __i == 4)
2091 __tm->tm_year = __i == 2 ? __value : __value - 1900;
2092 else
2093 __err |= ios_base::failbit;
2094 if (__beg == __end)
2095 __err |= ios_base::eofbit;
2096 return __beg;
2097 }
2098
2099 template<typename _CharT, typename _OutIter>
2100 _OutIter
2101 time_put<_CharT, _OutIter>::
2102 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
2103 const _CharT* __beg, const _CharT* __end) const
2104 {
2105 const locale __loc = __io.getloc();
2106 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2107 for (; __beg != __end; ++__beg)
2108 if (__ctype.narrow(*__beg, 0) != '%')
2109 {
2110 *__s = *__beg;
2111 ++__s;
2112 }
2113 else if (++__beg != __end)
2114 {
2115 char __format;
2116 char __mod = 0;
2117 const char __c = __ctype.narrow(*__beg, 0);
2118 if (__c != 'E' && __c != 'O')
2119 __format = __c;
2120 else if (++__beg != __end)
2121 {
2122 __mod = __c;
2123 __format = __ctype.narrow(*__beg, 0);
2124 }
2125 else
2126 break;
2127 __s = this->do_put(__s, __io, __fill, __tm,
2128 __format, __mod);
2129 }
2130 else
2131 break;
2132 return __s;
2133 }
2134
2135 template<typename _CharT, typename _OutIter>
2136 _OutIter
2137 time_put<_CharT, _OutIter>::
2138 do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
2139 char __format, char __mod) const
2140 {
2141 const locale __loc = __io.getloc();
2142 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
2143 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
2144
2145 // NB: This size is arbitrary. Should this be a data member,
2146 // initialized at construction?
2147 const size_t __maxlen = 64;
2148 char_type* __res =
2149 static_cast<char_type*>(__builtin_alloca(sizeof(char_type)
2150 * __maxlen));
2151
2152 // NB: In IEE 1003.1-200x, and perhaps other locale models, it
2153 // is possible that the format character will be longer than one
2154 // character. Possibilities include 'E' or 'O' followed by a
2155 // format character: if __mod is not the default argument, assume
2156 // it's a valid modifier.
2157 char_type __fmt[4];
2158 __fmt[0] = __ctype.widen('%');
2159 if (!__mod)
2160 {
2161 __fmt[1] = __format;
2162 __fmt[2] = char_type();
2163 }
2164 else
2165 {
2166 __fmt[1] = __mod;
2167 __fmt[2] = __format;
2168 __fmt[3] = char_type();
2169 }
2170
2171 __tp._M_put(__res, __maxlen, __fmt, __tm);
2172
2173 // Write resulting, fully-formatted string to output iterator.
2174 return std::__write(__s, __res, char_traits<char_type>::length(__res));
2175 }
2176
2177
2178 // Generic version does nothing.
2179 template<typename _CharT>
2180 int
2181 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
2182 { return 0; }
2183
2184 // Generic version does nothing.
2185 template<typename _CharT>
2186 size_t
2187 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
2188 { return 0; }
2189
2190 template<typename _CharT>
2191 int
2192 collate<_CharT>::
2193 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2194 const _CharT* __lo2, const _CharT* __hi2) const
2195 {
2196 // strcoll assumes zero-terminated strings so we make a copy
2197 // and then put a zero at the end.
2198 const string_type __one(__lo1, __hi1);
2199 const string_type __two(__lo2, __hi2);
2200
2201 const _CharT* __p = __one.c_str();
2202 const _CharT* __pend = __one.data() + __one.length();
2203 const _CharT* __q = __two.c_str();
2204 const _CharT* __qend = __two.data() + __two.length();
2205
2206 // strcoll stops when it sees a nul character so we break
2207 // the strings into zero-terminated substrings and pass those
2208 // to strcoll.
2209 for (;;)
2210 {
2211 const int __res = _M_compare(__p, __q);
2212 if (__res)
2213 return __res;
2214
2215 __p += char_traits<_CharT>::length(__p);
2216 __q += char_traits<_CharT>::length(__q);
2217 if (__p == __pend && __q == __qend)
2218 return 0;
2219 else if (__p == __pend)
2220 return -1;
2221 else if (__q == __qend)
2222 return 1;
2223
2224 __p++;
2225 __q++;
2226 }
2227 }
2228
2229 template<typename _CharT>
2230 typename collate<_CharT>::string_type
2231 collate<_CharT>::
2232 do_transform(const _CharT* __lo, const _CharT* __hi) const
2233 {
2234 // strxfrm assumes zero-terminated strings so we make a copy
2235 string_type __str(__lo, __hi);
2236
2237 const _CharT* __p = __str.c_str();
2238 const _CharT* __pend = __str.data() + __str.length();
2239
2240 size_t __len = (__hi - __lo) * 2;
2241
2242 string_type __ret;
2243
2244 // strxfrm stops when it sees a nul character so we break
2245 // the string into zero-terminated substrings and pass those
2246 // to strxfrm.
2247 for (;;)
2248 {
2249 // First try a buffer perhaps big enough.
2250 _CharT* __c =
2251 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __len));
2252 size_t __res = _M_transform(__c, __p, __len);
2253 // If the buffer was not large enough, try again with the
2254 // correct size.
2255 if (__res >= __len)
2256 {
2257 __len = __res + 1;
2258 __c = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
2259 * __len));
2260 __res = _M_transform(__c, __p, __res + 1);
2261 }
2262
2263 __ret.append(__c, __res);
2264 __p += char_traits<_CharT>::length(__p);
2265 if (__p == __pend)
2266 return __ret;
2267
2268 __p++;
2269 __ret.push_back(_CharT());
2270 }
2271 }
2272
2273 template<typename _CharT>
2274 long
2275 collate<_CharT>::
2276 do_hash(const _CharT* __lo, const _CharT* __hi) const
2277 {
2278 unsigned long __val = 0;
2279 for (; __lo < __hi; ++__lo)
2280 __val = *__lo + ((__val << 7) |
2281 (__val >> (numeric_limits<unsigned long>::digits - 7)));
2282 return static_cast<long>(__val);
2283 }
2284
2285 // Construct correctly padded string, as per 22.2.2.2.2
2286 // Assumes
2287 // __newlen > __oldlen
2288 // __news is allocated for __newlen size
2289 // Used by both num_put and ostream inserters: if __num,
2290 // internal-adjusted objects are padded according to the rules below
2291 // concerning 0[xX] and +-, otherwise, exactly as right-adjusted
2292 // ones are.
2293
2294 // NB: Of the two parameters, _CharT can be deduced from the
2295 // function arguments. The other (_Traits) has to be explicitly specified.
2296 template<typename _CharT, typename _Traits>
2297 void
2298 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
2299 _CharT* __news, const _CharT* __olds,
2300 const streamsize __newlen,
2301 const streamsize __oldlen, const bool __num)
2302 {
2303 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
2304 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
2305
2306 // Padding last.
2307 if (__adjust == ios_base::left)
2308 {
2309 _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
2310 _Traits::assign(__news + __oldlen, __plen, __fill);
2311 return;
2312 }
2313
2314 size_t __mod = 0;
2315 if (__adjust == ios_base::internal && __num)
2316 {
2317 // Pad after the sign, if there is one.
2318 // Pad after 0[xX], if there is one.
2319 // Who came up with these rules, anyway? Jeeze.
2320 const locale& __loc = __io._M_getloc();
2321 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
2322
2323 const bool __testsign = (__ctype.widen('-') == __olds[0]
2324 || __ctype.widen('+') == __olds[0]);
2325 const bool __testhex = (__ctype.widen('0') == __olds[0]
2326 && __oldlen > 1
2327 && (__ctype.widen('x') == __olds[1]
2328 || __ctype.widen('X') == __olds[1]));
2329 if (__testhex)
2330 {
2331 __news[0] = __olds[0];
2332 __news[1] = __olds[1];
2333 __mod = 2;
2334 __news += 2;
2335 }
2336 else if (__testsign)
2337 {
2338 __news[0] = __olds[0];
2339 __mod = 1;
2340 ++__news;
2341 }
2342 // else Padding first.
2343 }
2344 _Traits::assign(__news, __plen, __fill);
2345 _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
2346 __oldlen - __mod);
2347 }
2348
2349 bool
2350 __verify_grouping(const char* __grouping, size_t __grouping_size,
2351 const string& __grouping_tmp)
2352 {
2353 const size_t __n = __grouping_tmp.size() - 1;
2354 const size_t __min = std::min(__n, __grouping_size - 1);
2355 size_t __i = __n;
2356 bool __test = true;
2357
2358 // Parsed number groupings have to match the
2359 // numpunct::grouping string exactly, starting at the
2360 // right-most point of the parsed sequence of elements ...
2361 for (size_t __j = 0; __j < __min && __test; --__i, ++__j)
2362 __test = __grouping_tmp[__i] == __grouping[__j];
2363 for (; __i && __test; --__i)
2364 __test = __grouping_tmp[__i] == __grouping[__min];
2365 // ... but the last parsed grouping can be <= numpunct
2366 // grouping.
2367 __test &= __grouping_tmp[0] <= __grouping[__min];
2368 return __test;
2369 }
2370
2371 template<typename _CharT>
2372 _CharT*
2373 __add_grouping(_CharT* __s, _CharT __sep,
2374 const char* __gbeg, size_t __gsize,
2375 const _CharT* __first, const _CharT* __last)
2376 {
2377 if (__last - __first > *__gbeg)
2378 {
2379 const bool __bump = __gsize != 1;
2380 __s = std::__add_grouping(__s, __sep, __gbeg + __bump,
2381 __gsize - __bump, __first,
2382 __last - *__gbeg);
2383 __first = __last - *__gbeg;
2384 *__s++ = __sep;
2385 }
2386 do
2387 *__s++ = *__first++;
2388 while (__first != __last);
2389 return __s;
2390 }
2391
2392 // Inhibit implicit instantiations for required instantiations,
2393 // which are defined via explicit instantiations elsewhere.
2394 // NB: This syntax is a GNU extension.
2395 #if _GLIBCXX_EXTERN_TEMPLATE
2396 extern template class moneypunct<char, false>;
2397 extern template class moneypunct<char, true>;
2398 extern template class moneypunct_byname<char, false>;
2399 extern template class moneypunct_byname<char, true>;
2400 extern template class money_get<char>;
2401 extern template class money_put<char>;
2402 extern template class numpunct<char>;
2403 extern template class numpunct_byname<char>;
2404 extern template class num_get<char>;
2405 extern template class num_put<char>;
2406 extern template class __timepunct<char>;
2407 extern template class time_put<char>;
2408 extern template class time_put_byname<char>;
2409 extern template class time_get<char>;
2410 extern template class time_get_byname<char>;
2411 extern template class messages<char>;
2412 extern template class messages_byname<char>;
2413 extern template class ctype_byname<char>;
2414 extern template class codecvt_byname<char, char, mbstate_t>;
2415 extern template class collate<char>;
2416 extern template class collate_byname<char>;
2417
2418 extern template
2419 const codecvt<char, char, mbstate_t>&
2420 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
2421
2422 extern template
2423 const collate<char>&
2424 use_facet<collate<char> >(const locale&);
2425
2426 extern template
2427 const numpunct<char>&
2428 use_facet<numpunct<char> >(const locale&);
2429
2430 extern template
2431 const num_put<char>&
2432 use_facet<num_put<char> >(const locale&);
2433
2434 extern template
2435 const num_get<char>&
2436 use_facet<num_get<char> >(const locale&);
2437
2438 extern template
2439 const moneypunct<char, true>&
2440 use_facet<moneypunct<char, true> >(const locale&);
2441
2442 extern template
2443 const moneypunct<char, false>&
2444 use_facet<moneypunct<char, false> >(const locale&);
2445
2446 extern template
2447 const money_put<char>&
2448 use_facet<money_put<char> >(const locale&);
2449
2450 extern template
2451 const money_get<char>&
2452 use_facet<money_get<char> >(const locale&);
2453
2454 extern template
2455 const __timepunct<char>&
2456 use_facet<__timepunct<char> >(const locale&);
2457
2458 extern template
2459 const time_put<char>&
2460 use_facet<time_put<char> >(const locale&);
2461
2462 extern template
2463 const time_get<char>&
2464 use_facet<time_get<char> >(const locale&);
2465
2466 extern template
2467 const messages<char>&
2468 use_facet<messages<char> >(const locale&);
2469
2470 extern template
2471 bool
2472 has_facet<ctype<char> >(const locale&);
2473
2474 extern template
2475 bool
2476 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
2477
2478 extern template
2479 bool
2480 has_facet<collate<char> >(const locale&);
2481
2482 extern template
2483 bool
2484 has_facet<numpunct<char> >(const locale&);
2485
2486 extern template
2487 bool
2488 has_facet<num_put<char> >(const locale&);
2489
2490 extern template
2491 bool
2492 has_facet<num_get<char> >(const locale&);
2493
2494 extern template
2495 bool
2496 has_facet<moneypunct<char> >(const locale&);
2497
2498 extern template
2499 bool
2500 has_facet<money_put<char> >(const locale&);
2501
2502 extern template
2503 bool
2504 has_facet<money_get<char> >(const locale&);
2505
2506 extern template
2507 bool
2508 has_facet<__timepunct<char> >(const locale&);
2509
2510 extern template
2511 bool
2512 has_facet<time_put<char> >(const locale&);
2513
2514 extern template
2515 bool
2516 has_facet<time_get<char> >(const locale&);
2517
2518 extern template
2519 bool
2520 has_facet<messages<char> >(const locale&);
2521
2522 #ifdef _GLIBCXX_USE_WCHAR_T
2523 extern template class moneypunct<wchar_t, false>;
2524 extern template class moneypunct<wchar_t, true>;
2525 extern template class moneypunct_byname<wchar_t, false>;
2526 extern template class moneypunct_byname<wchar_t, true>;
2527 extern template class money_get<wchar_t>;
2528 extern template class money_put<wchar_t>;
2529 extern template class numpunct<wchar_t>;
2530 extern template class numpunct_byname<wchar_t>;
2531 extern template class num_get<wchar_t>;
2532 extern template class num_put<wchar_t>;
2533 extern template class __timepunct<wchar_t>;
2534 extern template class time_put<wchar_t>;
2535 extern template class time_put_byname<wchar_t>;
2536 extern template class time_get<wchar_t>;
2537 extern template class time_get_byname<wchar_t>;
2538 extern template class messages<wchar_t>;
2539 extern template class messages_byname<wchar_t>;
2540 extern template class ctype_byname<wchar_t>;
2541 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
2542 extern template class collate<wchar_t>;
2543 extern template class collate_byname<wchar_t>;
2544
2545 extern template
2546 const codecvt<wchar_t, char, mbstate_t>&
2547 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
2548
2549 extern template
2550 const collate<wchar_t>&
2551 use_facet<collate<wchar_t> >(const locale&);
2552
2553 extern template
2554 const numpunct<wchar_t>&
2555 use_facet<numpunct<wchar_t> >(const locale&);
2556
2557 extern template
2558 const num_put<wchar_t>&
2559 use_facet<num_put<wchar_t> >(const locale&);
2560
2561 extern template
2562 const num_get<wchar_t>&
2563 use_facet<num_get<wchar_t> >(const locale&);
2564
2565 extern template
2566 const moneypunct<wchar_t, true>&
2567 use_facet<moneypunct<wchar_t, true> >(const locale&);
2568
2569 extern template
2570 const moneypunct<wchar_t, false>&
2571 use_facet<moneypunct<wchar_t, false> >(const locale&);
2572
2573 extern template
2574 const money_put<wchar_t>&
2575 use_facet<money_put<wchar_t> >(const locale&);
2576
2577 extern template
2578 const money_get<wchar_t>&
2579 use_facet<money_get<wchar_t> >(const locale&);
2580
2581 extern template
2582 const __timepunct<wchar_t>&
2583 use_facet<__timepunct<wchar_t> >(const locale&);
2584
2585 extern template
2586 const time_put<wchar_t>&
2587 use_facet<time_put<wchar_t> >(const locale&);
2588
2589 extern template
2590 const time_get<wchar_t>&
2591 use_facet<time_get<wchar_t> >(const locale&);
2592
2593 extern template
2594 const messages<wchar_t>&
2595 use_facet<messages<wchar_t> >(const locale&);
2596
2597 extern template
2598 bool
2599 has_facet<ctype<wchar_t> >(const locale&);
2600
2601 extern template
2602 bool
2603 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
2604
2605 extern template
2606 bool
2607 has_facet<collate<wchar_t> >(const locale&);
2608
2609 extern template
2610 bool
2611 has_facet<numpunct<wchar_t> >(const locale&);
2612
2613 extern template
2614 bool
2615 has_facet<num_put<wchar_t> >(const locale&);
2616
2617 extern template
2618 bool
2619 has_facet<num_get<wchar_t> >(const locale&);
2620
2621 extern template
2622 bool
2623 has_facet<moneypunct<wchar_t> >(const locale&);
2624
2625 extern template
2626 bool
2627 has_facet<money_put<wchar_t> >(const locale&);
2628
2629 extern template
2630 bool
2631 has_facet<money_get<wchar_t> >(const locale&);
2632
2633 extern template
2634 bool
2635 has_facet<__timepunct<wchar_t> >(const locale&);
2636
2637 extern template
2638 bool
2639 has_facet<time_put<wchar_t> >(const locale&);
2640
2641 extern template
2642 bool
2643 has_facet<time_get<wchar_t> >(const locale&);
2644
2645 extern template
2646 bool
2647 has_facet<messages<wchar_t> >(const locale&);
2648 #endif
2649 #endif
2650 } // namespace std
2651
2652 #endif