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