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