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