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