]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/locale_facets.tcc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / locale_facets.tcc
1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
21
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
26
27 /** @file locale_facets.tcc
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
30 */
31
32 #ifndef _LOCALE_FACETS_TCC
33 #define _LOCALE_FACETS_TCC 1
34
35 #pragma GCC system_header
36
37 _GLIBCXX_BEGIN_NAMESPACE(std)
38
39 // Routine to access a cache for the facet. If the cache didn't
40 // exist before, it gets constructed on the fly.
41 template<typename _Facet>
42 struct __use_cache
43 {
44 const _Facet*
45 operator() (const locale& __loc) const;
46 };
47
48 // Specializations.
49 template<typename _CharT>
50 struct __use_cache<__numpunct_cache<_CharT> >
51 {
52 const __numpunct_cache<_CharT>*
53 operator() (const locale& __loc) const
54 {
55 const size_t __i = numpunct<_CharT>::id._M_id();
56 const locale::facet** __caches = __loc._M_impl->_M_caches;
57 if (!__caches[__i])
58 {
59 __numpunct_cache<_CharT>* __tmp = NULL;
60 __try
61 {
62 __tmp = new __numpunct_cache<_CharT>;
63 __tmp->_M_cache(__loc);
64 }
65 __catch(...)
66 {
67 delete __tmp;
68 __throw_exception_again;
69 }
70 __loc._M_impl->_M_install_cache(__tmp, __i);
71 }
72 return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]);
73 }
74 };
75
76 template<typename _CharT>
77 void
78 __numpunct_cache<_CharT>::_M_cache(const locale& __loc)
79 {
80 _M_allocated = true;
81
82 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
83
84 _M_grouping_size = __np.grouping().size();
85 char* __grouping = new char[_M_grouping_size];
86 __np.grouping().copy(__grouping, _M_grouping_size);
87 _M_grouping = __grouping;
88 _M_use_grouping = (_M_grouping_size
89 && static_cast<signed char>(_M_grouping[0]) > 0
90 && (_M_grouping[0]
91 != __gnu_cxx::__numeric_traits<char>::__max));
92
93 _M_truename_size = __np.truename().size();
94 _CharT* __truename = new _CharT[_M_truename_size];
95 __np.truename().copy(__truename, _M_truename_size);
96 _M_truename = __truename;
97
98 _M_falsename_size = __np.falsename().size();
99 _CharT* __falsename = new _CharT[_M_falsename_size];
100 __np.falsename().copy(__falsename, _M_falsename_size);
101 _M_falsename = __falsename;
102
103 _M_decimal_point = __np.decimal_point();
104 _M_thousands_sep = __np.thousands_sep();
105
106 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
107 __ct.widen(__num_base::_S_atoms_out,
108 __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out);
109 __ct.widen(__num_base::_S_atoms_in,
110 __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in);
111 }
112
113 // Used by both numeric and monetary facets.
114 // Check to make sure that the __grouping_tmp string constructed in
115 // money_get or num_get matches the canonical grouping for a given
116 // locale.
117 // __grouping_tmp is parsed L to R
118 // 1,222,444 == __grouping_tmp of "\1\3\3"
119 // __grouping is parsed R to L
120 // 1,222,444 == __grouping of "\3" == "\3\3\3"
121 bool
122 __verify_grouping(const char* __grouping, size_t __grouping_size,
123 const string& __grouping_tmp);
124
125 _GLIBCXX_BEGIN_LDBL_NAMESPACE
126
127 template<typename _CharT, typename _InIter>
128 _InIter
129 num_get<_CharT, _InIter>::
130 _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io,
131 ios_base::iostate& __err, string& __xtrc) const
132 {
133 typedef char_traits<_CharT> __traits_type;
134 typedef __numpunct_cache<_CharT> __cache_type;
135 __use_cache<__cache_type> __uc;
136 const locale& __loc = __io._M_getloc();
137 const __cache_type* __lc = __uc(__loc);
138 const _CharT* __lit = __lc->_M_atoms_in;
139 char_type __c = char_type();
140
141 // True if __beg becomes equal to __end.
142 bool __testeof = __beg == __end;
143
144 // First check for sign.
145 if (!__testeof)
146 {
147 __c = *__beg;
148 const bool __plus = __c == __lit[__num_base::_S_iplus];
149 if ((__plus || __c == __lit[__num_base::_S_iminus])
150 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
151 && !(__c == __lc->_M_decimal_point))
152 {
153 __xtrc += __plus ? '+' : '-';
154 if (++__beg != __end)
155 __c = *__beg;
156 else
157 __testeof = true;
158 }
159 }
160
161 // Next, look for leading zeros.
162 bool __found_mantissa = false;
163 int __sep_pos = 0;
164 while (!__testeof)
165 {
166 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
167 || __c == __lc->_M_decimal_point)
168 break;
169 else if (__c == __lit[__num_base::_S_izero])
170 {
171 if (!__found_mantissa)
172 {
173 __xtrc += '0';
174 __found_mantissa = true;
175 }
176 ++__sep_pos;
177
178 if (++__beg != __end)
179 __c = *__beg;
180 else
181 __testeof = true;
182 }
183 else
184 break;
185 }
186
187 // Only need acceptable digits for floating point numbers.
188 bool __found_dec = false;
189 bool __found_sci = false;
190 string __found_grouping;
191 if (__lc->_M_use_grouping)
192 __found_grouping.reserve(32);
193 const char_type* __lit_zero = __lit + __num_base::_S_izero;
194
195 if (!__lc->_M_allocated)
196 // "C" locale
197 while (!__testeof)
198 {
199 const int __digit = _M_find(__lit_zero, 10, __c);
200 if (__digit != -1)
201 {
202 __xtrc += '0' + __digit;
203 __found_mantissa = true;
204 }
205 else if (__c == __lc->_M_decimal_point
206 && !__found_dec && !__found_sci)
207 {
208 __xtrc += '.';
209 __found_dec = true;
210 }
211 else if ((__c == __lit[__num_base::_S_ie]
212 || __c == __lit[__num_base::_S_iE])
213 && !__found_sci && __found_mantissa)
214 {
215 // Scientific notation.
216 __xtrc += 'e';
217 __found_sci = true;
218
219 // Remove optional plus or minus sign, if they exist.
220 if (++__beg != __end)
221 {
222 __c = *__beg;
223 const bool __plus = __c == __lit[__num_base::_S_iplus];
224 if (__plus || __c == __lit[__num_base::_S_iminus])
225 __xtrc += __plus ? '+' : '-';
226 else
227 continue;
228 }
229 else
230 {
231 __testeof = true;
232 break;
233 }
234 }
235 else
236 break;
237
238 if (++__beg != __end)
239 __c = *__beg;
240 else
241 __testeof = true;
242 }
243 else
244 while (!__testeof)
245 {
246 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
247 // and decimal_point.
248 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
249 {
250 if (!__found_dec && !__found_sci)
251 {
252 // NB: Thousands separator at the beginning of a string
253 // is a no-no, as is two consecutive thousands separators.
254 if (__sep_pos)
255 {
256 __found_grouping += static_cast<char>(__sep_pos);
257 __sep_pos = 0;
258 }
259 else
260 {
261 // NB: __convert_to_v will not assign __v and will
262 // set the failbit.
263 __xtrc.clear();
264 break;
265 }
266 }
267 else
268 break;
269 }
270 else if (__c == __lc->_M_decimal_point)
271 {
272 if (!__found_dec && !__found_sci)
273 {
274 // If no grouping chars are seen, no grouping check
275 // is applied. Therefore __found_grouping is adjusted
276 // only if decimal_point comes after some thousands_sep.
277 if (__found_grouping.size())
278 __found_grouping += static_cast<char>(__sep_pos);
279 __xtrc += '.';
280 __found_dec = true;
281 }
282 else
283 break;
284 }
285 else
286 {
287 const char_type* __q =
288 __traits_type::find(__lit_zero, 10, __c);
289 if (__q)
290 {
291 __xtrc += '0' + (__q - __lit_zero);
292 __found_mantissa = true;
293 ++__sep_pos;
294 }
295 else if ((__c == __lit[__num_base::_S_ie]
296 || __c == __lit[__num_base::_S_iE])
297 && !__found_sci && __found_mantissa)
298 {
299 // Scientific notation.
300 if (__found_grouping.size() && !__found_dec)
301 __found_grouping += static_cast<char>(__sep_pos);
302 __xtrc += 'e';
303 __found_sci = true;
304
305 // Remove optional plus or minus sign, if they exist.
306 if (++__beg != __end)
307 {
308 __c = *__beg;
309 const bool __plus = __c == __lit[__num_base::_S_iplus];
310 if ((__plus || __c == __lit[__num_base::_S_iminus])
311 && !(__lc->_M_use_grouping
312 && __c == __lc->_M_thousands_sep)
313 && !(__c == __lc->_M_decimal_point))
314 __xtrc += __plus ? '+' : '-';
315 else
316 continue;
317 }
318 else
319 {
320 __testeof = true;
321 break;
322 }
323 }
324 else
325 break;
326 }
327
328 if (++__beg != __end)
329 __c = *__beg;
330 else
331 __testeof = true;
332 }
333
334 // Digit grouping is checked. If grouping and found_grouping don't
335 // match, then get very very upset, and set failbit.
336 if (__found_grouping.size())
337 {
338 // Add the ending grouping if a decimal or 'e'/'E' wasn't found.
339 if (!__found_dec && !__found_sci)
340 __found_grouping += static_cast<char>(__sep_pos);
341
342 if (!std::__verify_grouping(__lc->_M_grouping,
343 __lc->_M_grouping_size,
344 __found_grouping))
345 __err = ios_base::failbit;
346 }
347
348 return __beg;
349 }
350
351 template<typename _CharT, typename _InIter>
352 template<typename _ValueT>
353 _InIter
354 num_get<_CharT, _InIter>::
355 _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io,
356 ios_base::iostate& __err, _ValueT& __v) const
357 {
358 typedef char_traits<_CharT> __traits_type;
359 using __gnu_cxx::__add_unsigned;
360 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
361 typedef __numpunct_cache<_CharT> __cache_type;
362 __use_cache<__cache_type> __uc;
363 const locale& __loc = __io._M_getloc();
364 const __cache_type* __lc = __uc(__loc);
365 const _CharT* __lit = __lc->_M_atoms_in;
366 char_type __c = char_type();
367
368 // NB: Iff __basefield == 0, __base can change based on contents.
369 const ios_base::fmtflags __basefield = __io.flags()
370 & ios_base::basefield;
371 const bool __oct = __basefield == ios_base::oct;
372 int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10);
373
374 // True if __beg becomes equal to __end.
375 bool __testeof = __beg == __end;
376
377 // First check for sign.
378 bool __negative = false;
379 if (!__testeof)
380 {
381 __c = *__beg;
382 if (__gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
383 __negative = __c == __lit[__num_base::_S_iminus];
384 if ((__negative || __c == __lit[__num_base::_S_iplus])
385 && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
386 && !(__c == __lc->_M_decimal_point))
387 {
388 if (++__beg != __end)
389 __c = *__beg;
390 else
391 __testeof = true;
392 }
393 }
394
395 // Next, look for leading zeros and check required digits
396 // for base formats.
397 bool __found_zero = false;
398 int __sep_pos = 0;
399 while (!__testeof)
400 {
401 if ((__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
402 || __c == __lc->_M_decimal_point)
403 break;
404 else if (__c == __lit[__num_base::_S_izero]
405 && (!__found_zero || __base == 10))
406 {
407 __found_zero = true;
408 ++__sep_pos;
409 if (__basefield == 0)
410 __base = 8;
411 if (__base == 8)
412 __sep_pos = 0;
413 }
414 else if (__found_zero
415 && (__c == __lit[__num_base::_S_ix]
416 || __c == __lit[__num_base::_S_iX]))
417 {
418 if (__basefield == 0)
419 __base = 16;
420 if (__base == 16)
421 {
422 __found_zero = false;
423 __sep_pos = 0;
424 }
425 else
426 break;
427 }
428 else
429 break;
430
431 if (++__beg != __end)
432 {
433 __c = *__beg;
434 if (!__found_zero)
435 break;
436 }
437 else
438 __testeof = true;
439 }
440
441 // At this point, base is determined. If not hex, only allow
442 // base digits as valid input.
443 const size_t __len = (__base == 16 ? __num_base::_S_iend
444 - __num_base::_S_izero : __base);
445
446 // Extract.
447 string __found_grouping;
448 if (__lc->_M_use_grouping)
449 __found_grouping.reserve(32);
450 bool __testfail = false;
451 bool __testoverflow = false;
452 const __unsigned_type __max = __negative
453 ? -__gnu_cxx::__numeric_traits<_ValueT>::__min
454 : __gnu_cxx::__numeric_traits<_ValueT>::__max;
455 const __unsigned_type __smax = __max / __base;
456 __unsigned_type __result = 0;
457 int __digit = 0;
458 const char_type* __lit_zero = __lit + __num_base::_S_izero;
459
460 if (!__lc->_M_allocated)
461 // "C" locale
462 while (!__testeof)
463 {
464 __digit = _M_find(__lit_zero, __len, __c);
465 if (__digit == -1)
466 break;
467
468 if (__result > __smax)
469 __testoverflow = true;
470 else
471 {
472 __result *= __base;
473 __testoverflow |= __result > __max - __digit;
474 __result += __digit;
475 ++__sep_pos;
476 }
477
478 if (++__beg != __end)
479 __c = *__beg;
480 else
481 __testeof = true;
482 }
483 else
484 while (!__testeof)
485 {
486 // According to 22.2.2.1.2, p8-9, first look for thousands_sep
487 // and decimal_point.
488 if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep)
489 {
490 // NB: Thousands separator at the beginning of a string
491 // is a no-no, as is two consecutive thousands separators.
492 if (__sep_pos)
493 {
494 __found_grouping += static_cast<char>(__sep_pos);
495 __sep_pos = 0;
496 }
497 else
498 {
499 __testfail = true;
500 break;
501 }
502 }
503 else if (__c == __lc->_M_decimal_point)
504 break;
505 else
506 {
507 const char_type* __q =
508 __traits_type::find(__lit_zero, __len, __c);
509 if (!__q)
510 break;
511
512 __digit = __q - __lit_zero;
513 if (__digit > 15)
514 __digit -= 6;
515 if (__result > __smax)
516 __testoverflow = true;
517 else
518 {
519 __result *= __base;
520 __testoverflow |= __result > __max - __digit;
521 __result += __digit;
522 ++__sep_pos;
523 }
524 }
525
526 if (++__beg != __end)
527 __c = *__beg;
528 else
529 __testeof = true;
530 }
531
532 // Digit grouping is checked. If grouping and found_grouping don't
533 // match, then get very very upset, and set failbit.
534 if (__found_grouping.size())
535 {
536 // Add the ending grouping.
537 __found_grouping += static_cast<char>(__sep_pos);
538
539 if (!std::__verify_grouping(__lc->_M_grouping,
540 __lc->_M_grouping_size,
541 __found_grouping))
542 __err = ios_base::failbit;
543 }
544
545 // _GLIBCXX_RESOLVE_LIB_DEFECTS
546 // 23. Num_get overflow result.
547 if ((!__sep_pos && !__found_zero && !__found_grouping.size())
548 || __testfail)
549 {
550 __v = 0;
551 __err = ios_base::failbit;
552 }
553 else if (__testoverflow)
554 {
555 if (__negative)
556 __v = __gnu_cxx::__numeric_traits<_ValueT>::__min;
557 else
558 __v = __gnu_cxx::__numeric_traits<_ValueT>::__max;
559 __err = ios_base::failbit;
560 }
561 else
562 __v = __negative ? -__result : __result;
563
564 if (__testeof)
565 __err |= ios_base::eofbit;
566 return __beg;
567 }
568
569 // _GLIBCXX_RESOLVE_LIB_DEFECTS
570 // 17. Bad bool parsing
571 template<typename _CharT, typename _InIter>
572 _InIter
573 num_get<_CharT, _InIter>::
574 do_get(iter_type __beg, iter_type __end, ios_base& __io,
575 ios_base::iostate& __err, bool& __v) const
576 {
577 if (!(__io.flags() & ios_base::boolalpha))
578 {
579 // Parse bool values as long.
580 // NB: We can't just call do_get(long) here, as it might
581 // refer to a derived class.
582 long __l = -1;
583 __beg = _M_extract_int(__beg, __end, __io, __err, __l);
584 if (__l == 0 || __l == 1)
585 __v = bool(__l);
586 else
587 {
588 // _GLIBCXX_RESOLVE_LIB_DEFECTS
589 // 23. Num_get overflow result.
590 __v = true;
591 __err = ios_base::failbit;
592 if (__beg == __end)
593 __err |= ios_base::eofbit;
594 }
595 }
596 else
597 {
598 // Parse bool values as alphanumeric.
599 typedef __numpunct_cache<_CharT> __cache_type;
600 __use_cache<__cache_type> __uc;
601 const locale& __loc = __io._M_getloc();
602 const __cache_type* __lc = __uc(__loc);
603
604 bool __testf = true;
605 bool __testt = true;
606 bool __donef = __lc->_M_falsename_size == 0;
607 bool __donet = __lc->_M_truename_size == 0;
608 bool __testeof = false;
609 size_t __n = 0;
610 while (!__donef || !__donet)
611 {
612 if (__beg == __end)
613 {
614 __testeof = true;
615 break;
616 }
617
618 const char_type __c = *__beg;
619
620 if (!__donef)
621 __testf = __c == __lc->_M_falsename[__n];
622
623 if (!__testf && __donet)
624 break;
625
626 if (!__donet)
627 __testt = __c == __lc->_M_truename[__n];
628
629 if (!__testt && __donef)
630 break;
631
632 if (!__testt && !__testf)
633 break;
634
635 ++__n;
636 ++__beg;
637
638 __donef = !__testf || __n >= __lc->_M_falsename_size;
639 __donet = !__testt || __n >= __lc->_M_truename_size;
640 }
641 if (__testf && __n == __lc->_M_falsename_size && __n)
642 {
643 __v = false;
644 if (__testt && __n == __lc->_M_truename_size)
645 __err = ios_base::failbit;
646 else
647 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
648 }
649 else if (__testt && __n == __lc->_M_truename_size && __n)
650 {
651 __v = true;
652 __err = __testeof ? ios_base::eofbit : ios_base::goodbit;
653 }
654 else
655 {
656 // _GLIBCXX_RESOLVE_LIB_DEFECTS
657 // 23. Num_get overflow result.
658 __v = false;
659 __err = ios_base::failbit;
660 if (__testeof)
661 __err |= ios_base::eofbit;
662 }
663 }
664 return __beg;
665 }
666
667 template<typename _CharT, typename _InIter>
668 _InIter
669 num_get<_CharT, _InIter>::
670 do_get(iter_type __beg, iter_type __end, ios_base& __io,
671 ios_base::iostate& __err, float& __v) const
672 {
673 string __xtrc;
674 __xtrc.reserve(32);
675 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
676 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
677 if (__beg == __end)
678 __err |= ios_base::eofbit;
679 return __beg;
680 }
681
682 template<typename _CharT, typename _InIter>
683 _InIter
684 num_get<_CharT, _InIter>::
685 do_get(iter_type __beg, iter_type __end, ios_base& __io,
686 ios_base::iostate& __err, double& __v) const
687 {
688 string __xtrc;
689 __xtrc.reserve(32);
690 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
691 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
692 if (__beg == __end)
693 __err |= ios_base::eofbit;
694 return __beg;
695 }
696
697 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
698 template<typename _CharT, typename _InIter>
699 _InIter
700 num_get<_CharT, _InIter>::
701 __do_get(iter_type __beg, iter_type __end, ios_base& __io,
702 ios_base::iostate& __err, double& __v) const
703 {
704 string __xtrc;
705 __xtrc.reserve(32);
706 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
707 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
708 if (__beg == __end)
709 __err |= ios_base::eofbit;
710 return __beg;
711 }
712 #endif
713
714 template<typename _CharT, typename _InIter>
715 _InIter
716 num_get<_CharT, _InIter>::
717 do_get(iter_type __beg, iter_type __end, ios_base& __io,
718 ios_base::iostate& __err, long double& __v) const
719 {
720 string __xtrc;
721 __xtrc.reserve(32);
722 __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc);
723 std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale());
724 if (__beg == __end)
725 __err |= ios_base::eofbit;
726 return __beg;
727 }
728
729 template<typename _CharT, typename _InIter>
730 _InIter
731 num_get<_CharT, _InIter>::
732 do_get(iter_type __beg, iter_type __end, ios_base& __io,
733 ios_base::iostate& __err, void*& __v) const
734 {
735 // Prepare for hex formatted input.
736 typedef ios_base::fmtflags fmtflags;
737 const fmtflags __fmt = __io.flags();
738 __io.flags((__fmt & ~ios_base::basefield) | ios_base::hex);
739
740 typedef __gnu_cxx::__conditional_type<(sizeof(void*)
741 <= sizeof(unsigned long)),
742 unsigned long, unsigned long long>::__type _UIntPtrType;
743
744 _UIntPtrType __ul;
745 __beg = _M_extract_int(__beg, __end, __io, __err, __ul);
746
747 // Reset from hex formatted input.
748 __io.flags(__fmt);
749
750 __v = reinterpret_cast<void*>(__ul);
751 return __beg;
752 }
753
754 // For use by integer and floating-point types after they have been
755 // converted into a char_type string.
756 template<typename _CharT, typename _OutIter>
757 void
758 num_put<_CharT, _OutIter>::
759 _M_pad(_CharT __fill, streamsize __w, ios_base& __io,
760 _CharT* __new, const _CharT* __cs, int& __len) const
761 {
762 // [22.2.2.2.2] Stage 3.
763 // If necessary, pad.
764 __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new,
765 __cs, __w, __len);
766 __len = static_cast<int>(__w);
767 }
768
769 _GLIBCXX_END_LDBL_NAMESPACE
770
771 template<typename _CharT, typename _ValueT>
772 int
773 __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
774 ios_base::fmtflags __flags, bool __dec)
775 {
776 _CharT* __buf = __bufend;
777 if (__builtin_expect(__dec, true))
778 {
779 // Decimal.
780 do
781 {
782 *--__buf = __lit[(__v % 10) + __num_base::_S_odigits];
783 __v /= 10;
784 }
785 while (__v != 0);
786 }
787 else if ((__flags & ios_base::basefield) == ios_base::oct)
788 {
789 // Octal.
790 do
791 {
792 *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits];
793 __v >>= 3;
794 }
795 while (__v != 0);
796 }
797 else
798 {
799 // Hex.
800 const bool __uppercase = __flags & ios_base::uppercase;
801 const int __case_offset = __uppercase ? __num_base::_S_oudigits
802 : __num_base::_S_odigits;
803 do
804 {
805 *--__buf = __lit[(__v & 0xf) + __case_offset];
806 __v >>= 4;
807 }
808 while (__v != 0);
809 }
810 return __bufend - __buf;
811 }
812
813 _GLIBCXX_BEGIN_LDBL_NAMESPACE
814
815 template<typename _CharT, typename _OutIter>
816 void
817 num_put<_CharT, _OutIter>::
818 _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep,
819 ios_base&, _CharT* __new, _CharT* __cs, int& __len) const
820 {
821 _CharT* __p = std::__add_grouping(__new, __sep, __grouping,
822 __grouping_size, __cs, __cs + __len);
823 __len = __p - __new;
824 }
825
826 template<typename _CharT, typename _OutIter>
827 template<typename _ValueT>
828 _OutIter
829 num_put<_CharT, _OutIter>::
830 _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill,
831 _ValueT __v) const
832 {
833 using __gnu_cxx::__add_unsigned;
834 typedef typename __add_unsigned<_ValueT>::__type __unsigned_type;
835 typedef __numpunct_cache<_CharT> __cache_type;
836 __use_cache<__cache_type> __uc;
837 const locale& __loc = __io._M_getloc();
838 const __cache_type* __lc = __uc(__loc);
839 const _CharT* __lit = __lc->_M_atoms_out;
840 const ios_base::fmtflags __flags = __io.flags();
841
842 // Long enough to hold hex, dec, and octal representations.
843 const int __ilen = 5 * sizeof(_ValueT);
844 _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
845 * __ilen));
846
847 // [22.2.2.2.2] Stage 1, numeric conversion to character.
848 // Result is returned right-justified in the buffer.
849 const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
850 const bool __dec = (__basefield != ios_base::oct
851 && __basefield != ios_base::hex);
852 const __unsigned_type __u = ((__v > 0 || !__dec)
853 ? __unsigned_type(__v)
854 : -__unsigned_type(__v));
855 int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec);
856 __cs += __ilen - __len;
857
858 // Add grouping, if necessary.
859 if (__lc->_M_use_grouping)
860 {
861 // Grouping can add (almost) as many separators as the number
862 // of digits + space is reserved for numeric base or sign.
863 _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
864 * (__len + 1)
865 * 2));
866 _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size,
867 __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len);
868 __cs = __cs2 + 2;
869 }
870
871 // Complete Stage 1, prepend numeric base or sign.
872 if (__builtin_expect(__dec, true))
873 {
874 // Decimal.
875 if (__v >= 0)
876 {
877 if (bool(__flags & ios_base::showpos)
878 && __gnu_cxx::__numeric_traits<_ValueT>::__is_signed)
879 *--__cs = __lit[__num_base::_S_oplus], ++__len;
880 }
881 else
882 *--__cs = __lit[__num_base::_S_ominus], ++__len;
883 }
884 else if (bool(__flags & ios_base::showbase) && __v)
885 {
886 if (__basefield == ios_base::oct)
887 *--__cs = __lit[__num_base::_S_odigits], ++__len;
888 else
889 {
890 // 'x' or 'X'
891 const bool __uppercase = __flags & ios_base::uppercase;
892 *--__cs = __lit[__num_base::_S_ox + __uppercase];
893 // '0'
894 *--__cs = __lit[__num_base::_S_odigits];
895 __len += 2;
896 }
897 }
898
899 // Pad.
900 const streamsize __w = __io.width();
901 if (__w > static_cast<streamsize>(__len))
902 {
903 _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
904 * __w));
905 _M_pad(__fill, __w, __io, __cs3, __cs, __len);
906 __cs = __cs3;
907 }
908 __io.width(0);
909
910 // [22.2.2.2.2] Stage 4.
911 // Write resulting, fully-formatted string to output iterator.
912 return std::__write(__s, __cs, __len);
913 }
914
915 template<typename _CharT, typename _OutIter>
916 void
917 num_put<_CharT, _OutIter>::
918 _M_group_float(const char* __grouping, size_t __grouping_size,
919 _CharT __sep, const _CharT* __p, _CharT* __new,
920 _CharT* __cs, int& __len) const
921 {
922 // _GLIBCXX_RESOLVE_LIB_DEFECTS
923 // 282. What types does numpunct grouping refer to?
924 // Add grouping, if necessary.
925 const int __declen = __p ? __p - __cs : __len;
926 _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping,
927 __grouping_size,
928 __cs, __cs + __declen);
929
930 // Tack on decimal part.
931 int __newlen = __p2 - __new;
932 if (__p)
933 {
934 char_traits<_CharT>::copy(__p2, __p, __len - __declen);
935 __newlen += __len - __declen;
936 }
937 __len = __newlen;
938 }
939
940 // The following code uses vsnprintf (or vsprintf(), when
941 // _GLIBCXX_USE_C99 is not defined) to convert floating point values
942 // for insertion into a stream. An optimization would be to replace
943 // them with code that works directly on a wide buffer and then use
944 // __pad to do the padding. It would be good to replace them anyway
945 // to gain back the efficiency that C++ provides by knowing up front
946 // the type of the values to insert. Also, sprintf is dangerous
947 // since may lead to accidental buffer overruns. This
948 // implementation follows the C++ standard fairly directly as
949 // outlined in 22.2.2.2 [lib.locale.num.put]
950 template<typename _CharT, typename _OutIter>
951 template<typename _ValueT>
952 _OutIter
953 num_put<_CharT, _OutIter>::
954 _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
955 _ValueT __v) const
956 {
957 typedef __numpunct_cache<_CharT> __cache_type;
958 __use_cache<__cache_type> __uc;
959 const locale& __loc = __io._M_getloc();
960 const __cache_type* __lc = __uc(__loc);
961
962 // Use default precision if out of range.
963 const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
964
965 const int __max_digits =
966 __gnu_cxx::__numeric_traits<_ValueT>::__digits10;
967
968 // [22.2.2.2.2] Stage 1, numeric conversion to character.
969 int __len;
970 // Long enough for the max format spec.
971 char __fbuf[16];
972 __num_base::_S_format_float(__io, __fbuf, __mod);
973
974 #ifdef _GLIBCXX_USE_C99
975 // First try a buffer perhaps big enough (most probably sufficient
976 // for non-ios_base::fixed outputs)
977 int __cs_size = __max_digits * 3;
978 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
979 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
980 __fbuf, __prec, __v);
981
982 // If the buffer was not large enough, try again with the correct size.
983 if (__len >= __cs_size)
984 {
985 __cs_size = __len + 1;
986 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
987 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
988 __fbuf, __prec, __v);
989 }
990 #else
991 // Consider the possibility of long ios_base::fixed outputs
992 const bool __fixed = __io.flags() & ios_base::fixed;
993 const int __max_exp =
994 __gnu_cxx::__numeric_traits<_ValueT>::__max_exponent10;
995
996 // The size of the output string is computed as follows.
997 // ios_base::fixed outputs may need up to __max_exp + 1 chars
998 // for the integer part + __prec chars for the fractional part
999 // + 3 chars for sign, decimal point, '\0'. On the other hand,
1000 // for non-fixed outputs __max_digits * 2 + __prec chars are
1001 // largely sufficient.
1002 const int __cs_size = __fixed ? __max_exp + __prec + 4
1003 : __max_digits * 2 + __prec;
1004 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1005 __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf,
1006 __prec, __v);
1007 #endif
1008
1009 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1010 // numpunct.decimal_point() values for '.' and adding grouping.
1011 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1012
1013 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1014 * __len));
1015 __ctype.widen(__cs, __cs + __len, __ws);
1016
1017 // Replace decimal point.
1018 _CharT* __wp = 0;
1019 const char* __p = char_traits<char>::find(__cs, __len, '.');
1020 if (__p)
1021 {
1022 __wp = __ws + (__p - __cs);
1023 *__wp = __lc->_M_decimal_point;
1024 }
1025
1026 // Add grouping, if necessary.
1027 // N.B. Make sure to not group things like 2e20, i.e., no decimal
1028 // point, scientific notation.
1029 if (__lc->_M_use_grouping
1030 && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1031 && __cs[1] >= '0' && __cs[2] >= '0')))
1032 {
1033 // Grouping can add (almost) as many separators as the
1034 // number of digits, but no more.
1035 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1036 * __len * 2));
1037
1038 streamsize __off = 0;
1039 if (__cs[0] == '-' || __cs[0] == '+')
1040 {
1041 __off = 1;
1042 __ws2[0] = __ws[0];
1043 __len -= 1;
1044 }
1045
1046 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1047 __lc->_M_thousands_sep, __wp, __ws2 + __off,
1048 __ws + __off, __len);
1049 __len += __off;
1050
1051 __ws = __ws2;
1052 }
1053
1054 // Pad.
1055 const streamsize __w = __io.width();
1056 if (__w > static_cast<streamsize>(__len))
1057 {
1058 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1059 * __w));
1060 _M_pad(__fill, __w, __io, __ws3, __ws, __len);
1061 __ws = __ws3;
1062 }
1063 __io.width(0);
1064
1065 // [22.2.2.2.2] Stage 4.
1066 // Write resulting, fully-formatted string to output iterator.
1067 return std::__write(__s, __ws, __len);
1068 }
1069
1070 template<typename _CharT, typename _OutIter>
1071 _OutIter
1072 num_put<_CharT, _OutIter>::
1073 do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const
1074 {
1075 const ios_base::fmtflags __flags = __io.flags();
1076 if ((__flags & ios_base::boolalpha) == 0)
1077 {
1078 const long __l = __v;
1079 __s = _M_insert_int(__s, __io, __fill, __l);
1080 }
1081 else
1082 {
1083 typedef __numpunct_cache<_CharT> __cache_type;
1084 __use_cache<__cache_type> __uc;
1085 const locale& __loc = __io._M_getloc();
1086 const __cache_type* __lc = __uc(__loc);
1087
1088 const _CharT* __name = __v ? __lc->_M_truename
1089 : __lc->_M_falsename;
1090 int __len = __v ? __lc->_M_truename_size
1091 : __lc->_M_falsename_size;
1092
1093 const streamsize __w = __io.width();
1094 if (__w > static_cast<streamsize>(__len))
1095 {
1096 const streamsize __plen = __w - __len;
1097 _CharT* __ps
1098 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1099 * __plen));
1100
1101 char_traits<_CharT>::assign(__ps, __plen, __fill);
1102 __io.width(0);
1103
1104 if ((__flags & ios_base::adjustfield) == ios_base::left)
1105 {
1106 __s = std::__write(__s, __name, __len);
1107 __s = std::__write(__s, __ps, __plen);
1108 }
1109 else
1110 {
1111 __s = std::__write(__s, __ps, __plen);
1112 __s = std::__write(__s, __name, __len);
1113 }
1114 return __s;
1115 }
1116 __io.width(0);
1117 __s = std::__write(__s, __name, __len);
1118 }
1119 return __s;
1120 }
1121
1122 template<typename _CharT, typename _OutIter>
1123 _OutIter
1124 num_put<_CharT, _OutIter>::
1125 do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1126 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1127
1128 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
1129 template<typename _CharT, typename _OutIter>
1130 _OutIter
1131 num_put<_CharT, _OutIter>::
1132 __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const
1133 { return _M_insert_float(__s, __io, __fill, char(), __v); }
1134 #endif
1135
1136 template<typename _CharT, typename _OutIter>
1137 _OutIter
1138 num_put<_CharT, _OutIter>::
1139 do_put(iter_type __s, ios_base& __io, char_type __fill,
1140 long double __v) const
1141 { return _M_insert_float(__s, __io, __fill, 'L', __v); }
1142
1143 template<typename _CharT, typename _OutIter>
1144 _OutIter
1145 num_put<_CharT, _OutIter>::
1146 do_put(iter_type __s, ios_base& __io, char_type __fill,
1147 const void* __v) const
1148 {
1149 const ios_base::fmtflags __flags = __io.flags();
1150 const ios_base::fmtflags __fmt = ~(ios_base::basefield
1151 | ios_base::uppercase);
1152 __io.flags((__flags & __fmt) | (ios_base::hex | ios_base::showbase));
1153
1154 typedef __gnu_cxx::__conditional_type<(sizeof(const void*)
1155 <= sizeof(unsigned long)),
1156 unsigned long, unsigned long long>::__type _UIntPtrType;
1157
1158 __s = _M_insert_int(__s, __io, __fill,
1159 reinterpret_cast<_UIntPtrType>(__v));
1160 __io.flags(__flags);
1161 return __s;
1162 }
1163
1164 _GLIBCXX_END_LDBL_NAMESPACE
1165
1166 // Construct correctly padded string, as per 22.2.2.2.2
1167 // Assumes
1168 // __newlen > __oldlen
1169 // __news is allocated for __newlen size
1170
1171 // NB: Of the two parameters, _CharT can be deduced from the
1172 // function arguments. The other (_Traits) has to be explicitly specified.
1173 template<typename _CharT, typename _Traits>
1174 void
1175 __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill,
1176 _CharT* __news, const _CharT* __olds,
1177 streamsize __newlen, streamsize __oldlen)
1178 {
1179 const size_t __plen = static_cast<size_t>(__newlen - __oldlen);
1180 const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield;
1181
1182 // Padding last.
1183 if (__adjust == ios_base::left)
1184 {
1185 _Traits::copy(__news, __olds, __oldlen);
1186 _Traits::assign(__news + __oldlen, __plen, __fill);
1187 return;
1188 }
1189
1190 size_t __mod = 0;
1191 if (__adjust == ios_base::internal)
1192 {
1193 // Pad after the sign, if there is one.
1194 // Pad after 0[xX], if there is one.
1195 // Who came up with these rules, anyway? Jeeze.
1196 const locale& __loc = __io._M_getloc();
1197 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1198
1199 if (__ctype.widen('-') == __olds[0]
1200 || __ctype.widen('+') == __olds[0])
1201 {
1202 __news[0] = __olds[0];
1203 __mod = 1;
1204 ++__news;
1205 }
1206 else if (__ctype.widen('0') == __olds[0]
1207 && __oldlen > 1
1208 && (__ctype.widen('x') == __olds[1]
1209 || __ctype.widen('X') == __olds[1]))
1210 {
1211 __news[0] = __olds[0];
1212 __news[1] = __olds[1];
1213 __mod = 2;
1214 __news += 2;
1215 }
1216 // else Padding first.
1217 }
1218 _Traits::assign(__news, __plen, __fill);
1219 _Traits::copy(__news + __plen, __olds + __mod, __oldlen - __mod);
1220 }
1221
1222 template<typename _CharT>
1223 _CharT*
1224 __add_grouping(_CharT* __s, _CharT __sep,
1225 const char* __gbeg, size_t __gsize,
1226 const _CharT* __first, const _CharT* __last)
1227 {
1228 size_t __idx = 0;
1229 size_t __ctr = 0;
1230
1231 while (__last - __first > __gbeg[__idx]
1232 && static_cast<signed char>(__gbeg[__idx]) > 0
1233 && __gbeg[__idx] != __gnu_cxx::__numeric_traits<char>::__max)
1234 {
1235 __last -= __gbeg[__idx];
1236 __idx < __gsize - 1 ? ++__idx : ++__ctr;
1237 }
1238
1239 while (__first != __last)
1240 *__s++ = *__first++;
1241
1242 while (__ctr--)
1243 {
1244 *__s++ = __sep;
1245 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1246 *__s++ = *__first++;
1247 }
1248
1249 while (__idx--)
1250 {
1251 *__s++ = __sep;
1252 for (char __i = __gbeg[__idx]; __i > 0; --__i)
1253 *__s++ = *__first++;
1254 }
1255
1256 return __s;
1257 }
1258
1259 // Inhibit implicit instantiations for required instantiations,
1260 // which are defined via explicit instantiations elsewhere.
1261 // NB: This syntax is a GNU extension.
1262 #if _GLIBCXX_EXTERN_TEMPLATE
1263 extern template class numpunct<char>;
1264 extern template class numpunct_byname<char>;
1265 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>;
1266 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>;
1267 extern template class ctype_byname<char>;
1268
1269 extern template
1270 const ctype<char>&
1271 use_facet<ctype<char> >(const locale&);
1272
1273 extern template
1274 const numpunct<char>&
1275 use_facet<numpunct<char> >(const locale&);
1276
1277 extern template
1278 const num_put<char>&
1279 use_facet<num_put<char> >(const locale&);
1280
1281 extern template
1282 const num_get<char>&
1283 use_facet<num_get<char> >(const locale&);
1284
1285 extern template
1286 bool
1287 has_facet<ctype<char> >(const locale&);
1288
1289 extern template
1290 bool
1291 has_facet<numpunct<char> >(const locale&);
1292
1293 extern template
1294 bool
1295 has_facet<num_put<char> >(const locale&);
1296
1297 extern template
1298 bool
1299 has_facet<num_get<char> >(const locale&);
1300
1301 #ifdef _GLIBCXX_USE_WCHAR_T
1302 extern template class numpunct<wchar_t>;
1303 extern template class numpunct_byname<wchar_t>;
1304 extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>;
1305 extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>;
1306 extern template class ctype_byname<wchar_t>;
1307
1308 extern template
1309 const ctype<wchar_t>&
1310 use_facet<ctype<wchar_t> >(const locale&);
1311
1312 extern template
1313 const numpunct<wchar_t>&
1314 use_facet<numpunct<wchar_t> >(const locale&);
1315
1316 extern template
1317 const num_put<wchar_t>&
1318 use_facet<num_put<wchar_t> >(const locale&);
1319
1320 extern template
1321 const num_get<wchar_t>&
1322 use_facet<num_get<wchar_t> >(const locale&);
1323
1324 extern template
1325 bool
1326 has_facet<ctype<wchar_t> >(const locale&);
1327
1328 extern template
1329 bool
1330 has_facet<numpunct<wchar_t> >(const locale&);
1331
1332 extern template
1333 bool
1334 has_facet<num_put<wchar_t> >(const locale&);
1335
1336 extern template
1337 bool
1338 has_facet<num_get<wchar_t> >(const locale&);
1339 #endif
1340 #endif
1341
1342 _GLIBCXX_END_NAMESPACE
1343
1344 #endif