locale_facets.h (class time_base, [...]): Move...
[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, 2005,
4 // 2006, 2007
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
22
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
31
32 /** @file locale_facets.tcc
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
35 */
36
37 #ifndef _LOCALE_FACETS_TCC
38 #define _LOCALE_FACETS_TCC 1
39
40 #pragma GCC system_header
41
42 _GLIBCXX_BEGIN_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 // Specializations.
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 template<typename _CharT>
154 void
155 __numpunct_cache<_CharT>::_M_cache(const locale& __loc)
156 {
157 _M_allocated = true;
158
159 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
160
161 _M_grouping_size = __np.grouping().size();
162 char* __grouping = new char[_M_grouping_size];
163 __np.grouping().copy(__grouping, _M_grouping_size);
164 _M_grouping = __grouping;
165 _M_use_grouping = (_M_grouping_size
166 && static_cast<signed char>(__np.grouping()[0]) > 0);
167
168 _M_truename_size = __np.truename().size();
169 _CharT* __truename = new _CharT[_M_truename_size];
170 __np.truename().copy(__truename, _M_truename_size);
171 _M_truename = __truename;
172
173 _M_falsename_size = __np.falsename().size();
174 _CharT* __falsename = new _CharT[_M_falsename_size];
175 __np.falsename().copy(__falsename, _M_falsename_size);
176 _M_falsename = __falsename;
177
178 _M_decimal_point = __np.decimal_point();
179 _M_thousands_sep = __np.thousands_sep();
180
181 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
182 __ct.widen(__num_base::_S_atoms_out,
183 __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
184 __ct.widen(__num_base::_S_atoms_in,
185 __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
186 }
187
188 // Used by both numeric and monetary facets.
189 // Check to make sure that the __grouping_tmp string constructed in
190 // money_get or num_get matches the canonical grouping for a given
191 // locale.
192 // __grouping_tmp is parsed L to R
193 // 1,222,444 == __grouping_tmp of "\1\3\3"
194 // __grouping is parsed R to L
195 // 1,222,444 == __grouping of "\3" == "\3\3\3"
196 static bool
197 __verify_grouping(const char* __grouping, size_t __grouping_size,
198 const string& __grouping_tmp);
199
200 _GLIBCXX_BEGIN_LDBL_NAMESPACE
201
202 template<typename _CharT, typename _InIter>
203 _InIter
204 num_get<_CharT, _InIter>::
205 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
206 ios_base::iostate& __err, string& __xtrc) const
207 {
208 typedef char_traits<_CharT> __traits_type;
209 typedef __numpunct_cache<_CharT> __cache_type;
210 __use_cache<__cache_type> __uc;
211 const locale& __loc = __io._M_getloc();
212 const __cache_type* __lc = __uc(__loc);
213 const _CharT* __lit = __lc->_M_atoms_in;
214 char_type __c = char_type();
215
216 // True if __beg becomes equal to __end.
217 bool __testeof = __beg == __end;
218
219 // First check for sign.
220 if (!__testeof)
221 {
222 __c = *__beg;
223 const bool __plus = __c == __lit[__num_base::_S_iplus];
224 if ((__plus || __c == __lit[__num_base::_S_iminus])
225 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
226 && !(__c == __lc->_M_decimal_point))
227 {
228 __xtrc += __plus ? '+' : '-';
229 if (++__beg != __end)
230 __c = *__beg;
231 else
232 __testeof = true;
233 }
234 }
235
236 // Next, look for leading zeros.
237 bool __found_mantissa = false;
238 int __sep_pos = 0;
239 while (!__testeof)
240 {
241 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
242 || __c == __lc->_M_decimal_point)
243 break;
244 else if (__c == __lit[__num_base::_S_izero])
245 {
246 if (!__found_mantissa)
247 {
248 __xtrc += '0';
249 __found_mantissa = true;
250 }
251 ++__sep_pos;
252
253 if (++__beg != __end)
254 __c = *__beg;
255 else
256 __testeof = true;
257 }
258 else
259 break;
260 }
261
262 // Only need acceptable digits for floating point numbers.
263 bool __found_dec = false;
264 bool __found_sci = false;
265 string __found_grouping;
266 if (__lc->_M_use_grouping)
267 __found_grouping.reserve(32);
268 const char_type* __lit_zero = __lit + __num_base::_S_izero;
269
270 if (!__lc->_M_allocated)
271 // "C" locale
272 while (!__testeof)
273 {
274 const int __digit = _M_find(__lit_zero, 10, __c);
275 if (__digit != -1)
276 {
277 __xtrc += '0' + __digit;
278 __found_mantissa = true;
279 }
280 else if (__c == __lc->_M_decimal_point
281 && !__found_dec && !__found_sci)
282 {
283 __xtrc += '.';
284 __found_dec = true;
285 }
286 else if ((__c == __lit[__num_base::_S_ie]
287 || __c == __lit[__num_base::_S_iE])
288 && !__found_sci && __found_mantissa)
289 {
290 // Scientific notation.
291 __xtrc += 'e';
292 __found_sci = true;
293
294 // Remove optional plus or minus sign, if they exist.
295 if (++__beg != __end)
296 {
297 __c = *__beg;
298 const bool __plus = __c == __lit[__num_base::_S_iplus];
299 if (__plus || __c == __lit[__num_base::_S_iminus])
300 __xtrc += __plus ? '+' : '-';
301 else
302 continue;
303 }
304 else
305 {
306 __testeof = true;
307 break;
308 }
309 }
310 else
311 break;
312
313 if (++__beg != __end)
314 __c = *__beg;
315 else
316 __testeof = true;
317 }
318 else
319 while (!__testeof)
320 {
321 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
322 // and decimal_point.
323 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
324 {
325 if (!__found_dec && !__found_sci)
326 {
327 // NB: Thousands separator at the beginning of a string
328 // is a no-no, as is two consecutive thousands separators.
329 if (__sep_pos)
330 {
331 __found_grouping += static_cast<char>(__sep_pos);
332 __sep_pos = 0;
333 }
334 else
335 {
336 // NB: __convert_to_v will not assign __v and will
337 // set the failbit.
338 __xtrc.clear();
339 break;
340 }
341 }
342 else
343 break;
344 }
345 else if (__c == __lc->_M_decimal_point)
346 {
347 if (!__found_dec && !__found_sci)
348 {
349 // If no grouping chars are seen, no grouping check
350 // is applied. Therefore __found_grouping is adjusted
351 // only if decimal_point comes after some thousands_sep.
352 if (__found_grouping.size())
353 __found_grouping += static_cast<char>(__sep_pos);
354 __xtrc += '.';
355 __found_dec = true;
356 }
357 else
358 break;
359 }
360 else
361 {
362 const char_type* __q =
363 __traits_type::find(__lit_zero, 10, __c);
364 if (__q)
365 {
366 __xtrc += '0' + (__q - __lit_zero);
367 __found_mantissa = true;
368 ++__sep_pos;
369 }
370 else if ((__c == __lit[__num_base::_S_ie]
371 || __c == __lit[__num_base::_S_iE])
372 && !__found_sci && __found_mantissa)
373 {
374 // Scientific notation.
375 if (__found_grouping.size() && !__found_dec)
376 __found_grouping += static_cast<char>(__sep_pos);
377 __xtrc += 'e';
378 __found_sci = true;
379
380 // Remove optional plus or minus sign, if they exist.
381 if (++__beg != __end)
382 {
383 __c = *__beg;
384 const bool __plus = __c == __lit[__num_base::_S_iplus];
385 if ((__plus || __c == __lit[__num_base::_S_iminus])
386 && !(__lc->_M_use_grouping
387 && __c == __lc->_M_thousands_sep)
388 && !(__c == __lc->_M_decimal_point))
389 __xtrc += __plus ? '+' : '-';
390 else
391 continue;
392 }
393 else
394 {
395 __testeof = true;
396 break;
397 }
398 }
399 else
400 break;
401 }
402
403 if (++__beg != __end)
404 __c = *__beg;
405 else
406 __testeof = true;
407 }
408
409 // Digit grouping is checked. If grouping and found_grouping don't
410 // match, then get very very upset, and set failbit.
411 if (__found_grouping.size())
412 {
413 // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
414 if (!__found_dec && !__found_sci)
415 __found_grouping += static_cast<char>(__sep_pos);
416
417 if (!std::__verify_grouping(__lc->_M_grouping,
418 __lc->_M_grouping_size,
419 __found_grouping))
420 __err |= ios_base::failbit;
421 }
422
423 // Finish up.
424 if (__testeof)
425 __err |= ios_base::eofbit;
426 return __beg;
427 }
428
429 template<typename _CharT, typename _InIter>
430 template<typename _ValueT>
431 _InIter
432 num_get<_CharT, _InIter>::
433 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
434 ios_base::iostate& __err, _ValueT& __v) const
435 {
436 typedef char_traits<_CharT> __traits_type;
437 using __gnu_cxx::__add_unsigned;
438 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
439 typedef __numpunct_cache<_CharT> __cache_type;
440 __use_cache<__cache_type> __uc;
441 const locale& __loc = __io._M_getloc();
442 const __cache_type* __lc = __uc(__loc);
443 const _CharT* __lit = __lc->_M_atoms_in;
444 char_type __c = char_type();
445
446 // NB: Iff __basefield == 0, __base can change based on contents.
447 const ios_base::fmtflags __basefield = __io.flags()
448 & ios_base::basefield;
449 const bool __oct = __basefield == ios_base::oct;
450 int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
451
452 // True if __beg becomes equal to __end.
453 bool __testeof = __beg == __end;
454
455 // First check for sign.
456 bool __negative = false;
457 if (!__testeof)
458 {
459 __c = *__beg;
460 if (__gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
461 __negative = __c == __lit[__num_base::_S_iminus];
462 if ((__negative || __c == __lit[__num_base::_S_iplus])
463 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
464 && !(__c == __lc->_M_decimal_point))
465 {
466 if (++__beg != __end)
467 __c = *__beg;
468 else
469 __testeof = true;
470 }
471 }
472
473 // Next, look for leading zeros and check required digits
474 // for base formats.
475 bool __found_zero = false;
476 int __sep_pos = 0;
477 while (!__testeof)
478 {
479 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
480 || __c == __lc->_M_decimal_point)
481 break;
482 else if (__c == __lit[__num_base::_S_izero]
483 && (!__found_zero || __base == 10))
484 {
485 __found_zero = true;
486 ++__sep_pos;
487 if (__basefield == 0)
488 __base = 8;
489 if (__base == 8)
490 __sep_pos = 0;
491 }
492 else if (__found_zero
493 && (__c == __lit[__num_base::_S_ix]
494 || __c == __lit[__num_base::_S_iX]))
495 {
496 if (__basefield == 0)
497 __base = 16;
498 if (__base == 16)
499 {
500 __found_zero = false;
501 __sep_pos = 0;
502 }
503 else
504 break;
505 }
506 else
507 break;
508
509 if (++__beg != __end)
510 {
511 __c = *__beg;
512 if (!__found_zero)
513 break;
514 }
515 else
516 __testeof = true;
517 }
518
519 // At this point, base is determined. If not hex, only allow
520 // base digits as valid input.
521 const size_t __len = (__base == 16 ? __num_base::_S_iend
522 - __num_base::_S_izero : __base);
523
524 // Extract.
525 string __found_grouping;
526 if (__lc->_M_use_grouping)
527 __found_grouping.reserve(32);
528 bool __testfail = false;
529 const __unsigned_type __max = __negative
530 ? -__gnu_cxx::__numeric_traits<_ValueT>::__min
531 : __gnu_cxx::__numeric_traits<_ValueT>::__max;
532 const __unsigned_type __smax = __max / __base;
533 __unsigned_type __result = 0;
534 int __digit = 0;
535 const char_type* __lit_zero = __lit + __num_base::_S_izero;
536
537 if (!__lc->_M_allocated)
538 // "C" locale
539 while (!__testeof)
540 {
541 __digit = _M_find(__lit_zero, __len, __c);
542 if (__digit == -1)
543 break;
544
545 if (__result > __smax)
546 __testfail = true;
547 else
548 {
549 __result *= __base;
550 __testfail |= __result > __max - __digit;
551 __result += __digit;
552 ++__sep_pos;
553 }
554
555 if (++__beg != __end)
556 __c = *__beg;
557 else
558 __testeof = true;
559 }
560 else
561 while (!__testeof)
562 {
563 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
564 // and decimal_point.
565 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
566 {
567 // NB: Thousands separator at the beginning of a string
568 // is a no-no, as is two consecutive thousands separators.
569 if (__sep_pos)
570 {
571 __found_grouping += static_cast<char>(__sep_pos);
572 __sep_pos = 0;
573 }
574 else
575 {
576 __testfail = true;
577 break;
578 }
579 }
580 else if (__c == __lc->_M_decimal_point)
581 break;
582 else
583 {
584 const char_type* __q =
585 __traits_type::find(__lit_zero, __len, __c);
586 if (!__q)
587 break;
588
589 __digit = __q - __lit_zero;
590 if (__digit > 15)
591 __digit -= 6;
592 if (__result > __smax)
593 __testfail = true;
594 else
595 {
596 __result *= __base;
597 __testfail |= __result > __max - __digit;
598 __result += __digit;
599 ++__sep_pos;
600 }
601 }
602
603 if (++__beg != __end)
604 __c = *__beg;
605 else
606 __testeof = true;
607 }
608
609 // Digit grouping is checked. If grouping and found_grouping don't
610 // match, then get very very upset, and set failbit.
611 if (__found_grouping.size())
612 {
613 // Add the ending grouping.
614 __found_grouping += static_cast<char>(__sep_pos);
615
616 if (!std::__verify_grouping(__lc->_M_grouping,
617 __lc->_M_grouping_size,
618 __found_grouping))
619 __err |= ios_base::failbit;
620 }
621
622 if (!__testfail && (__sep_pos || __found_zero
623 || __found_grouping.size()))
624 __v = __negative ? -__result : __result;
625 else
626 __err |= ios_base::failbit;
627
628 if (__testeof)
629 __err |= ios_base::eofbit;
630 return __beg;
631 }
632
633 // _GLIBCXX_RESOLVE_LIB_DEFECTS
634 // 17. Bad bool parsing
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, bool& __v) const
640 {
641 if (!(__io.flags() & ios_base::boolalpha))
642 {
643 // Parse bool values as long.
644 // NB: We can't just call do_get(long) here, as it might
645 // refer to a derived class.
646 long __l = -1;
647 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
648 if (__l == 0 || __l == 1)
649 __v = bool(__l);
650 else
651 __err |= ios_base::failbit;
652 }
653 else
654 {
655 // Parse bool values as alphanumeric.
656 typedef __numpunct_cache<_CharT> __cache_type;
657 __use_cache<__cache_type> __uc;
658 const locale& __loc = __io._M_getloc();
659 const __cache_type* __lc = __uc(__loc);
660
661 bool __testf = true;
662 bool __testt = true;
663 size_t __n;
664 bool __testeof = __beg == __end;
665 for (__n = 0; !__testeof; ++__n)
666 {
667 const char_type __c = *__beg;
668
669 if (__testf)
670 {
671 if (__n < __lc->_M_falsename_size)
672 __testf = __c == __lc->_M_falsename[__n];
673 else
674 break;
675 }
676
677 if (__testt)
678 {
679 if (__n < __lc->_M_truename_size)
680 __testt = __c == __lc->_M_truename[__n];
681 else
682 break;
683 }
684
685 if (!__testf && !__testt)
686 break;
687
688 if (++__beg == __end)
689 __testeof = true;
690 }
691 if (__testf && __n == __lc->_M_falsename_size)
692 __v = false;
693 else if (__testt && __n == __lc->_M_truename_size)
694 __v = true;
695 else
696 __err |= ios_base::failbit;
697
698 if (__testeof)
699 __err |= ios_base::eofbit;
700 }
701 return __beg;
702 }
703
704 template<typename _CharT, typename _InIter>
705 _InIter
706 num_get<_CharT, _InIter>::
707 do_get(iter_type __beg, iter_type __end, ios_base& __io,
708 ios_base::iostate& __err, long& __v) const
709 { return _M_extract_int(__beg, __end, __io, __err, __v); }
710
711 template<typename _CharT, typename _InIter>
712 _InIter
713 num_get<_CharT, _InIter>::
714 do_get(iter_type __beg, iter_type __end, ios_base& __io,
715 ios_base::iostate& __err, unsigned short& __v) const
716 { return _M_extract_int(__beg, __end, __io, __err, __v); }
717
718 template<typename _CharT, typename _InIter>
719 _InIter
720 num_get<_CharT, _InIter>::
721 do_get(iter_type __beg, iter_type __end, ios_base& __io,
722 ios_base::iostate& __err, unsigned int& __v) const
723 { return _M_extract_int(__beg, __end, __io, __err, __v); }
724
725 template<typename _CharT, typename _InIter>
726 _InIter
727 num_get<_CharT, _InIter>::
728 do_get(iter_type __beg, iter_type __end, ios_base& __io,
729 ios_base::iostate& __err, unsigned long& __v) const
730 { return _M_extract_int(__beg, __end, __io, __err, __v); }
731
732 #ifdef _GLIBCXX_USE_LONG_LONG
733 template<typename _CharT, typename _InIter>
734 _InIter
735 num_get<_CharT, _InIter>::
736 do_get(iter_type __beg, iter_type __end, ios_base& __io,
737 ios_base::iostate& __err, long long& __v) const
738 { return _M_extract_int(__beg, __end, __io, __err, __v); }
739
740 template<typename _CharT, typename _InIter>
741 _InIter
742 num_get<_CharT, _InIter>::
743 do_get(iter_type __beg, iter_type __end, ios_base& __io,
744 ios_base::iostate& __err, unsigned long long& __v) const
745 { return _M_extract_int(__beg, __end, __io, __err, __v); }
746 #endif
747
748 template<typename _CharT, typename _InIter>
749 _InIter
750 num_get<_CharT, _InIter>::
751 do_get(iter_type __beg, iter_type __end, ios_base& __io,
752 ios_base::iostate& __err, float& __v) const
753 {
754 string __xtrc;
755 __xtrc.reserve(32);
756 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
757 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
758 return __beg;
759 }
760
761 template<typename _CharT, typename _InIter>
762 _InIter
763 num_get<_CharT, _InIter>::
764 do_get(iter_type __beg, iter_type __end, ios_base& __io,
765 ios_base::iostate& __err, double& __v) const
766 {
767 string __xtrc;
768 __xtrc.reserve(32);
769 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
770 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
771 return __beg;
772 }
773
774 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
775 template<typename _CharT, typename _InIter>
776 _InIter
777 num_get<_CharT, _InIter>::
778 __do_get(iter_type __beg, iter_type __end, ios_base& __io,
779 ios_base::iostate& __err, double& __v) const
780 {
781 string __xtrc;
782 __xtrc.reserve(32);
783 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
784 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
785 return __beg;
786 }
787 #endif
788
789 template<typename _CharT, typename _InIter>
790 _InIter
791 num_get<_CharT, _InIter>::
792 do_get(iter_type __beg, iter_type __end, ios_base& __io,
793 ios_base::iostate& __err, long double& __v) const
794 {
795 string __xtrc;
796 __xtrc.reserve(32);
797 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
798 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
799 return __beg;
800 }
801
802 template<typename _CharT, typename _InIter>
803 _InIter
804 num_get<_CharT, _InIter>::
805 do_get(iter_type __beg, iter_type __end, ios_base& __io,
806 ios_base::iostate& __err, void*& __v) const
807 {
808 // Prepare for hex formatted input.
809 typedef ios_base::fmtflags fmtflags;
810 const fmtflags __fmt = __io.flags();
811 __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
812
813 typedef __gnu_cxx::__conditional_type<(sizeof(void*)
814 <= sizeof(unsigned long)),
815 unsigned long, unsigned long long>::__type _UIntPtrType;
816
817 _UIntPtrType __ul;
818 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
819
820 // Reset from hex formatted input.
821 __io.flags(__fmt);
822
823 if (!(__err & ios_base::failbit))
824 __v = reinterpret_cast<void*>(__ul);
825 return __beg;
826 }
827
828 // For use by integer and floating-point types after they have been
829 // converted into a char_type string.
830 template<typename _CharT, typename _OutIter>
831 void
832 num_put<_CharT, _OutIter>::
833 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
834 _CharT* __new, const _CharT* __cs, int& __len) const
835 {
836 // [22.2.2.2.2] Stage 3.
837 // If necessary, pad.
838 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
839 __cs, __w, __len);
840 __len = static_cast<int>(__w);
841 }
842
843 _GLIBCXX_END_LDBL_NAMESPACE
844
845 template<typename _CharT, typename _ValueT>
846 int
847 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
848 ios_base::fmtflags __flags, bool __dec)
849 {
850 _CharT* __buf = __bufend;
851 if (__builtin_expect(__dec, true))
852 {
853 // Decimal.
854 do
855 {
856 *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
857 __v /= 10;
858 }
859 while (__v != 0);
860 }
861 else if ((__flags & ios_base::basefield) == ios_base::oct)
862 {
863 // Octal.
864 do
865 {
866 *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
867 __v >>= 3;
868 }
869 while (__v != 0);
870 }
871 else
872 {
873 // Hex.
874 const bool __uppercase = __flags & ios_base::uppercase;
875 const int __case_offset = __uppercase ? __num_base::_S_oudigits
876 : __num_base::_S_odigits;
877 do
878 {
879 *--__buf = __lit[(__v & 0xf) + __case_offset];
880 __v >>= 4;
881 }
882 while (__v != 0);
883 }
884 return __bufend - __buf;
885 }
886
887 _GLIBCXX_BEGIN_LDBL_NAMESPACE
888
889 template<typename _CharT, typename _OutIter>
890 void
891 num_put<_CharT, _OutIter>::
892 _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
893 ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
894 {
895 _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
896 __grouping_size, __cs, __cs + __len);
897 __len = __p - __new;
898 }
899
900 template<typename _CharT, typename _OutIter>
901 template<typename _ValueT>
902 _OutIter
903 num_put<_CharT, _OutIter>::
904 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
905 _ValueT __v) const
906 {
907 using __gnu_cxx::__add_unsigned;
908 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
909 typedef __numpunct_cache<_CharT> __cache_type;
910 __use_cache<__cache_type> __uc;
911 const locale& __loc = __io._M_getloc();
912 const __cache_type* __lc = __uc(__loc);
913 const _CharT* __lit = __lc->_M_atoms_out;
914 const ios_base::fmtflags __flags = __io.flags();
915
916 // Long enough to hold hex, dec, and octal representations.
917 const int __ilen = 5 * sizeof(_ValueT);
918 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
919 * __ilen));
920
921 // [22.2.2.2.2] Stage 1, numeric conversion to character.
922 // Result is returned right-justified in the buffer.
923 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
924 const bool __dec = (__basefield != ios_base::oct
925 && __basefield != ios_base::hex);
926 const __unsigned_type __u = ((__v > 0 || !__dec)
927 ? __unsigned_type(__v)
928 : -__unsigned_type(__v));
929 int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
930 __cs += __ilen - __len;
931
932 // Add grouping, if necessary.
933 if (__lc->_M_use_grouping)
934 {
935 // Grouping can add (almost) as many separators as the number
936 // of digits + space is reserved for numeric base or sign.
937 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
938 * (__len + 1)
939 * 2));
940 _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
941 __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
942 __cs = __cs2 + 2;
943 }
944
945 // Complete Stage 1, prepend numeric base or sign.
946 if (__builtin_expect(__dec, true))
947 {
948 // Decimal.
949 if (__v > 0)
950 {
951 if (__flags & ios_base::showpos
952 && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
953 *--__cs = __lit[__num_base::_S_oplus], ++__len;
954 }
955 else if (__v)
956 *--__cs = __lit[__num_base::_S_ominus], ++__len;
957 }
958 else if (__flags & ios_base::showbase && __v)
959 {
960 if (__basefield == ios_base::oct)
961 *--__cs = __lit[__num_base::_S_odigits], ++__len;
962 else
963 {
964 // 'x' or 'X'
965 const bool __uppercase = __flags & ios_base::uppercase;
966 *--__cs = __lit[__num_base::_S_ox + __uppercase];
967 // '0'
968 *--__cs = __lit[__num_base::_S_odigits];
969 __len += 2;
970 }
971 }
972
973 // Pad.
974 const streamsize __w = __io.width();
975 if (__w > static_cast<streamsize>(__len))
976 {
977 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
978 * __w));
979 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
980 __cs = __cs3;
981 }
982 __io.width(0);
983
984 // [22.2.2.2.2] Stage 4.
985 // Write resulting, fully-formatted string to output iterator.
986 return std::__write(__s, __cs, __len);
987 }
988
989 template<typename _CharT, typename _OutIter>
990 void
991 num_put<_CharT, _OutIter>::
992 _M_group_float(const char* __grouping, size_t __grouping_size,
993 _CharT __sep, const _CharT* __p, _CharT* __new,
994 _CharT* __cs, int& __len) const
995 {
996 // _GLIBCXX_RESOLVE_LIB_DEFECTS
997 // 282. What types does numpunct grouping refer to?
998 // Add grouping, if necessary.
999 const int __declen = __p ? __p - __cs : __len;
1000 _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
1001 __grouping_size,
1002 __cs, __cs + __declen);
1003
1004 // Tack on decimal part.
1005 int __newlen = __p2 - __new;
1006 if (__p)
1007 {
1008 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
1009 __newlen += __len - __declen;
1010 }
1011 __len = __newlen;
1012 }
1013
1014 // The following code uses vsnprintf (or vsprintf(), when
1015 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
1016 // for insertion into a stream. An optimization would be to replace
1017 // them with code that works directly on a wide buffer and then use
1018 // __pad to do the padding. It would be good to replace them anyway
1019 // to gain back the efficiency that C++ provides by knowing up front
1020 // the type of the values to insert. Also, sprintf is dangerous
1021 // since may lead to accidental buffer overruns. This
1022 // implementation follows the C++ standard fairly directly as
1023 // outlined in 22.2.2.2 [lib.locale.num.put]
1024 template<typename _CharT, typename _OutIter>
1025 template<typename _ValueT>
1026 _OutIter
1027 num_put<_CharT, _OutIter>::
1028 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
1029 _ValueT __v) const
1030 {
1031 typedef __numpunct_cache<_CharT> __cache_type;
1032 __use_cache<__cache_type> __uc;
1033 const locale& __loc = __io._M_getloc();
1034 const __cache_type* __lc = __uc(__loc);
1035
1036 // Use default precision if out of range.
1037 const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
1038
1039 const int __max_digits =
1040 __gnu_cxx::__numeric_traits<_ValueT>::__digits10;
1041
1042 // [22.2.2.2.2] Stage 1, numeric conversion to character.
1043 int __len;
1044 // Long enough for the max format spec.
1045 char __fbuf[16];
1046 __num_base::_S_format_float(__io, __fbuf, __mod);
1047
1048 #ifdef _GLIBCXX_USE_C99
1049 // First try a buffer perhaps big enough (most probably sufficient
1050 // for non-ios_base::fixed outputs)
1051 int __cs_size = __max_digits * 3;
1052 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1053 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1054 __fbuf, __prec, __v);
1055
1056 // If the buffer was not large enough, try again with the correct size.
1057 if (__len >= __cs_size)
1058 {
1059 __cs_size = __len + 1;
1060 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1061 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
1062 __fbuf, __prec, __v);
1063 }
1064 #else
1065 // Consider the possibility of long ios_base::fixed outputs
1066 const bool __fixed = __io.flags() & ios_base::fixed;
1067 const int __max_exp =
1068 __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10;
1069
1070 // The size of the output string is computed as follows.
1071 // ios_base::fixed outputs may need up to __max_exp + 1 chars
1072 // for the integer part + __prec chars for the fractional part
1073 // + 3 chars for sign, decimal point, '\0'. On the other hand,
1074 // for non-fixed outputs __max_digits * 2 + __prec chars are
1075 // largely sufficient.
1076 const int __cs_size = __fixed ? __max_exp + __prec + 4
1077 : __max_digits * 2 + __prec;
1078 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1079 __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf,
1080 __prec, __v);
1081 #endif
1082
1083 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1084 // numpunct.decimal_point() values for '.' and adding grouping.
1085 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1086
1087 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1088 * __len));
1089 __ctype.widen(__cs, __cs + __len, __ws);
1090
1091 // Replace decimal point.
1092 _CharT* __wp = 0;
1093 const char* __p = char_traits<char>::find(__cs, __len, '.');
1094 if (__p)
1095 {
1096 __wp = __ws + (__p - __cs);
1097 *__wp = __lc->_M_decimal_point;
1098 }
1099
1100 // Add grouping, if necessary.
1101 // N.B. Make sure to not group things like 2e20, i.e., no decimal
1102 // point, scientific notation.
1103 if (__lc->_M_use_grouping
1104 && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1105 && __cs[1] >= '0' && __cs[2] >= '0')))
1106 {
1107 // Grouping can add (almost) as many separators as the
1108 // number of digits, but no more.
1109 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1110 * __len * 2));
1111
1112 streamsize __off = 0;
1113 if (__cs[0] == '-' || __cs[0] == '+')
1114 {
1115 __off = 1;
1116 __ws2[0] = __ws[0];
1117 __len -= 1;
1118 }
1119
1120 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1121 __lc->_M_thousands_sep, __wp, __ws2 + __off,
1122 __ws + __off, __len);
1123 __len += __off;
1124
1125 __ws = __ws2;
1126 }
1127
1128 // Pad.
1129 const streamsize __w = __io.width();
1130 if (__w > static_cast<streamsize>(__len))
1131 {
1132 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1133 * __w));
1134 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1135 __ws = __ws3;
1136 }
1137 __io.width(0);
1138
1139 // [22.2.2.2.2] Stage 4.
1140 // Write resulting, fully-formatted string to output iterator.
1141 return std::__write(__s, __ws, __len);
1142 }
1143
1144 template<typename _CharT, typename _OutIter>
1145 _OutIter
1146 num_put<_CharT, _OutIter>::
1147 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1148 {
1149 const ios_base::fmtflags __flags = __io.flags();
1150 if ((__flags & ios_base::boolalpha) == 0)
1151 {
1152 const long __l = __v;
1153 __s = _M_insert_int(__s, __io, __fill, __l);
1154 }
1155 else
1156 {
1157 typedef __numpunct_cache<_CharT> __cache_type;
1158 __use_cache<__cache_type> __uc;
1159 const locale& __loc = __io._M_getloc();
1160 const __cache_type* __lc = __uc(__loc);
1161
1162 const _CharT* __name = __v ? __lc->_M_truename
1163 : __lc->_M_falsename;
1164 int __len = __v ? __lc->_M_truename_size
1165 : __lc->_M_falsename_size;
1166
1167 const streamsize __w = __io.width();
1168 if (__w > static_cast<streamsize>(__len))
1169 {
1170 _CharT* __cs
1171 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1172 * __w));
1173 _M_pad(__fill, __w, __io, __cs, __name, __len);
1174 __name = __cs;
1175 }
1176 __io.width(0);
1177 __s = std::__write(__s, __name, __len);
1178 }
1179 return __s;
1180 }
1181
1182 template<typename _CharT, typename _OutIter>
1183 _OutIter
1184 num_put<_CharT, _OutIter>::
1185 do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const
1186 { return _M_insert_int(__s, __io, __fill, __v); }
1187
1188 template<typename _CharT, typename _OutIter>
1189 _OutIter
1190 num_put<_CharT, _OutIter>::
1191 do_put(iter_type __s, ios_base& __io, char_type __fill,
1192 unsigned long __v) const
1193 { return _M_insert_int(__s, __io, __fill, __v); }
1194
1195 #ifdef _GLIBCXX_USE_LONG_LONG
1196 template<typename _CharT, typename _OutIter>
1197 _OutIter
1198 num_put<_CharT, _OutIter>::
1199 do_put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const
1200 { return _M_insert_int(__s, __io, __fill, __v); }
1201
1202 template<typename _CharT, typename _OutIter>
1203 _OutIter
1204 num_put<_CharT, _OutIter>::
1205 do_put(iter_type __s, ios_base& __io, char_type __fill,
1206 unsigned long long __v) const
1207 { return _M_insert_int(__s, __io, __fill, __v); }
1208 #endif
1209
1210 template<typename _CharT, typename _OutIter>
1211 _OutIter
1212 num_put<_CharT, _OutIter>::
1213 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1214 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1215
1216 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1217 template<typename _CharT, typename _OutIter>
1218 _OutIter
1219 num_put<_CharT, _OutIter>::
1220 __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1221 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1222 #endif
1223
1224 template<typename _CharT, typename _OutIter>
1225 _OutIter
1226 num_put<_CharT, _OutIter>::
1227 do_put(iter_type __s, ios_base& __io, char_type __fill,
1228 long double __v) const
1229 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1230
1231 template<typename _CharT, typename _OutIter>
1232 _OutIter
1233 num_put<_CharT, _OutIter>::
1234 do_put(iter_type __s, ios_base& __io, char_type __fill,
1235 const void* __v) const
1236 {
1237 const ios_base::fmtflags __flags = __io.flags();
1238 const ios_base::fmtflags __fmt = ~(ios_base::basefield
1239 | ios_base::uppercase
1240 | ios_base::internal);
1241 __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
1242
1243 typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
1244 <= sizeof(unsigned long)),
1245 unsigned long, unsigned long long>::__type _UIntPtrType;
1246
1247 __s = _M_insert_int(__s, __io, __fill,
1248 reinterpret_cast<_UIntPtrType>(__v));
1249 __io.flags(__flags);
1250 return __s;
1251 }
1252
1253 _GLIBCXX_END_LDBL_NAMESPACE
1254
1255 // Generic version does nothing.
1256 template<typename _CharT>
1257 int
1258 collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const
1259 { return 0; }
1260
1261 // Generic version does nothing.
1262 template<typename _CharT>
1263 size_t
1264 collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const
1265 { return 0; }
1266
1267 template<typename _CharT>
1268 int
1269 collate<_CharT>::
1270 do_compare(const _CharT* __lo1, const _CharT* __hi1,
1271 const _CharT* __lo2, const _CharT* __hi2) const
1272 {
1273 // strcoll assumes zero-terminated strings so we make a copy
1274 // and then put a zero at the end.
1275 const string_type __one(__lo1, __hi1);
1276 const string_type __two(__lo2, __hi2);
1277
1278 const _CharT* __p = __one.c_str();
1279 const _CharT* __pend = __one.data() + __one.length();
1280 const _CharT* __q = __two.c_str();
1281 const _CharT* __qend = __two.data() + __two.length();
1282
1283 // strcoll stops when it sees a nul character so we break
1284 // the strings into zero-terminated substrings and pass those
1285 // to strcoll.
1286 for (;;)
1287 {
1288 const int __res = _M_compare(__p, __q);
1289 if (__res)
1290 return __res;
1291
1292 __p += char_traits<_CharT>::length(__p);
1293 __q += char_traits<_CharT>::length(__q);
1294 if (__p == __pend && __q == __qend)
1295 return 0;
1296 else if (__p == __pend)
1297 return -1;
1298 else if (__q == __qend)
1299 return 1;
1300
1301 __p++;
1302 __q++;
1303 }
1304 }
1305
1306 template<typename _CharT>
1307 typename collate<_CharT>::string_type
1308 collate<_CharT>::
1309 do_transform(const _CharT* __lo, const _CharT* __hi) const
1310 {
1311 string_type __ret;
1312
1313 // strxfrm assumes zero-terminated strings so we make a copy
1314 const string_type __str(__lo, __hi);
1315
1316 const _CharT* __p = __str.c_str();
1317 const _CharT* __pend = __str.data() + __str.length();
1318
1319 size_t __len = (__hi - __lo) * 2;
1320
1321 _CharT* __c = new _CharT[__len];
1322
1323 try
1324 {
1325 // strxfrm stops when it sees a nul character so we break
1326 // the string into zero-terminated substrings and pass those
1327 // to strxfrm.
1328 for (;;)
1329 {
1330 // First try a buffer perhaps big enough.
1331 size_t __res = _M_transform(__c, __p, __len);
1332 // If the buffer was not large enough, try again with the
1333 // correct size.
1334 if (__res >= __len)
1335 {
1336 __len = __res + 1;
1337 delete [] __c, __c = 0;
1338 __c = new _CharT[__len];
1339 __res = _M_transform(__c, __p, __len);
1340 }
1341
1342 __ret.append(__c, __res);
1343 __p += char_traits<_CharT>::length(__p);
1344 if (__p == __pend)
1345 break;
1346
1347 __p++;
1348 __ret.push_back(_CharT());
1349 }
1350 }
1351 catch(...)
1352 {
1353 delete [] __c;
1354 __throw_exception_again;
1355 }
1356
1357 delete [] __c;
1358
1359 return __ret;
1360 }
1361
1362 template<typename _CharT>
1363 long
1364 collate<_CharT>::
1365 do_hash(const _CharT* __lo, const _CharT* __hi) const
1366 {
1367 unsigned long __val = 0;
1368 for (; __lo < __hi; ++__lo)
1369 __val =
1370 *__lo + ((__val << 7)
1371 | (__val >> (__gnu_cxx::__numeric_traits<unsigned long>::
1372 __digits - 7)));
1373 return static_cast<long>(__val);
1374 }
1375
1376 // Construct correctly padded string, as per 22.2.2.2.2
1377 // Assumes
1378 // __newlen > __oldlen
1379 // __news is allocated for __newlen size
1380
1381 // NB: Of the two parameters, _CharT can be deduced from the
1382 // function arguments. The other (_Traits) has to be explicitly specified.
1383 template<typename _CharT, typename _Traits>
1384 void
1385 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
1386 _CharT* __news, const _CharT* __olds,
1387 const streamsize __newlen,
1388 const streamsize __oldlen)
1389 {
1390 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
1391 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
1392
1393 // Padding last.
1394 if (__adjust == ios_base::left)
1395 {
1396 _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen);
1397 _Traits::assign(__news + __oldlen, __plen, __fill);
1398 return;
1399 }
1400
1401 size_t __mod = 0;
1402 if (__adjust == ios_base::internal)
1403 {
1404 // Pad after the sign, if there is one.
1405 // Pad after 0[xX], if there is one.
1406 // Who came up with these rules, anyway? Jeeze.
1407 const locale& __loc = __io._M_getloc();
1408 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1409
1410 const bool __testsign = (__ctype.widen('-') == __olds[0]
1411 || __ctype.widen('+') == __olds[0]);
1412 const bool __testhex = (__ctype.widen('0') == __olds[0]
1413 && __oldlen > 1
1414 && (__ctype.widen('x') == __olds[1]
1415 || __ctype.widen('X') == __olds[1]));
1416 if (__testhex)
1417 {
1418 __news[0] = __olds[0];
1419 __news[1] = __olds[1];
1420 __mod = 2;
1421 __news += 2;
1422 }
1423 else if (__testsign)
1424 {
1425 __news[0] = __olds[0];
1426 __mod = 1;
1427 ++__news;
1428 }
1429 // else Padding first.
1430 }
1431 _Traits::assign(__news, __plen, __fill);
1432 _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod),
1433 __oldlen - __mod);
1434 }
1435
1436 bool
1437 __verify_grouping(const char* __grouping, size_t __grouping_size,
1438 const string& __grouping_tmp)
1439 {
1440 const size_t __n = __grouping_tmp.size() - 1;
1441 const size_t __min = std::min(__n, size_t(__grouping_size - 1));
1442 size_t __i = __n;
1443 bool __test = true;
1444
1445 // Parsed number groupings have to match the
1446 // numpunct::grouping string exactly, starting at the
1447 // right-most point of the parsed sequence of elements ...
1448 for (size_t __j = 0; __j < __min && __test; --__i, ++__j)
1449 __test = __grouping_tmp[__i] == __grouping[__j];
1450 for (; __i && __test; --__i)
1451 __test = __grouping_tmp[__i] == __grouping[__min];
1452 // ... but the first parsed grouping can be <= numpunct
1453 // grouping (only do the check if the numpunct char is > 0
1454 // because <= 0 means any size is ok).
1455 if (static_cast<signed char>(__grouping[__min]) > 0)
1456 __test &= __grouping_tmp[0] <= __grouping[__min];
1457 return __test;
1458 }
1459
1460 template<typename _CharT>
1461 _CharT*
1462 __add_grouping(_CharT* __s, _CharT __sep,
1463 const char* __gbeg, size_t __gsize,
1464 const _CharT* __first, const _CharT* __last)
1465 {
1466 size_t __idx = 0;
1467 size_t __ctr = 0;
1468
1469 while (__last - __first > __gbeg[__idx]
1470 && static_cast<signed char>(__gbeg[__idx]) > 0)
1471 {
1472 __last -= __gbeg[__idx];
1473 __idx < __gsize - 1 ? ++__idx : ++__ctr;
1474 }
1475
1476 while (__first != __last)
1477 *__s++ = *__first++;
1478
1479 while (__ctr--)
1480 {
1481 *__s++ = __sep;
1482 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1483 *__s++ = *__first++;
1484 }
1485
1486 while (__idx--)
1487 {
1488 *__s++ = __sep;
1489 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1490 *__s++ = *__first++;
1491 }
1492
1493 return __s;
1494 }
1495
1496 // Inhibit implicit instantiations for required instantiations,
1497 // which are defined via explicit instantiations elsewhere.
1498 // NB: This syntax is a GNU extension.
1499 #if _GLIBCXX_EXTERN_TEMPLATE
1500 extern template class numpunct<char>;
1501 extern template class numpunct_byname<char>;
1502 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>;
1503 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>;
1504 extern template class ctype_byname<char>;
1505 extern template class codecvt_byname<char, char, mbstate_t>;
1506 extern template class collate<char>;
1507 extern template class collate_byname<char>;
1508
1509 extern template
1510 const codecvt<char, char, mbstate_t>&
1511 use_facet<codecvt<char, char, mbstate_t> >(const locale&);
1512
1513 extern template
1514 const collate<char>&
1515 use_facet<collate<char> >(const locale&);
1516
1517 extern template
1518 const numpunct<char>&
1519 use_facet<numpunct<char> >(const locale&);
1520
1521 extern template
1522 const num_put<char>&
1523 use_facet<num_put<char> >(const locale&);
1524
1525 extern template
1526 const num_get<char>&
1527 use_facet<num_get<char> >(const locale&);
1528
1529 extern template
1530 bool
1531 has_facet<ctype<char> >(const locale&);
1532
1533 extern template
1534 bool
1535 has_facet<codecvt<char, char, mbstate_t> >(const locale&);
1536
1537 extern template
1538 bool
1539 has_facet<collate<char> >(const locale&);
1540
1541 extern template
1542 bool
1543 has_facet<numpunct<char> >(const locale&);
1544
1545 extern template
1546 bool
1547 has_facet<num_put<char> >(const locale&);
1548
1549 extern template
1550 bool
1551 has_facet<num_get<char> >(const locale&);
1552
1553 #ifdef _GLIBCXX_USE_WCHAR_T
1554 extern template class numpunct<wchar_t>;
1555 extern template class numpunct_byname<wchar_t>;
1556 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>;
1557 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>;
1558 extern template class ctype_byname<wchar_t>;
1559 extern template class codecvt_byname<wchar_t, char, mbstate_t>;
1560 extern template class collate<wchar_t>;
1561 extern template class collate_byname<wchar_t>;
1562
1563 extern template
1564 const codecvt<wchar_t, char, mbstate_t>&
1565 use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&);
1566
1567 extern template
1568 const collate<wchar_t>&
1569 use_facet<collate<wchar_t> >(const locale&);
1570
1571 extern template
1572 const numpunct<wchar_t>&
1573 use_facet<numpunct<wchar_t> >(const locale&);
1574
1575 extern template
1576 const num_put<wchar_t>&
1577 use_facet<num_put<wchar_t> >(const locale&);
1578
1579 extern template
1580 const num_get<wchar_t>&
1581 use_facet<num_get<wchar_t> >(const locale&);
1582
1583 extern template
1584 bool
1585 has_facet<ctype<wchar_t> >(const locale&);
1586
1587 extern template
1588 bool
1589 has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&);
1590
1591 extern template
1592 bool
1593 has_facet<collate<wchar_t> >(const locale&);
1594
1595 extern template
1596 bool
1597 has_facet<numpunct<wchar_t> >(const locale&);
1598
1599 extern template
1600 bool
1601 has_facet<num_put<wchar_t> >(const locale&);
1602
1603 extern template
1604 bool
1605 has_facet<num_get<wchar_t> >(const locale&);
1606 #endif
1607 #endif
1608
1609 _GLIBCXX_END_NAMESPACE
1610
1611 #endif