]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/locale_facets_nonio.tcc
re PR libstdc++/25191 (exception_defines.h #defines try/catch)
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / locale_facets_nonio.tcc
CommitLineData
f749a55b
PC
1// Locale support -*- C++ -*-
2
bc2631e0 3// Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
f749a55b
PC
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING. If not, write to the Free
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19// USA.
20
21// As a special exception, you may use this file as part of a free software
22// library without restriction. Specifically, if other files instantiate
23// templates or use macros or inline functions from this file, or you compile
24// this file and link it with other files to produce an executable, this
25// file does not by itself cause the resulting executable to be covered by
26// the GNU General Public License. This exception does not however
27// invalidate any other reasons why the executable file might be covered by
28// the GNU General Public License.
29
30/** @file locale_facets_nonio.tcc
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
33 */
34
35#ifndef _LOCALE_FACETS_NONIO_TCC
36#define _LOCALE_FACETS_NONIO_TCC 1
37
84b31797
PC
38#pragma GCC system_header
39
f749a55b
PC
40_GLIBCXX_BEGIN_NAMESPACE(std)
41
42 template<typename _CharT, bool _Intl>
43 struct __use_cache<__moneypunct_cache<_CharT, _Intl> >
44 {
45 const __moneypunct_cache<_CharT, _Intl>*
46 operator() (const locale& __loc) const
47 {
48 const size_t __i = moneypunct<_CharT, _Intl>::id._M_id();
49 const locale::facet** __caches = __loc._M_impl->_M_caches;
50 if (!__caches[__i])
51 {
52 __moneypunct_cache<_CharT, _Intl>* __tmp = NULL;
bc2631e0 53 __try
f749a55b
PC
54 {
55 __tmp = new __moneypunct_cache<_CharT, _Intl>;
56 __tmp->_M_cache(__loc);
57 }
bc2631e0 58 __catch(...)
f749a55b
PC
59 {
60 delete __tmp;
61 __throw_exception_again;
62 }
63 __loc._M_impl->_M_install_cache(__tmp, __i);
64 }
65 return static_cast<
66 const __moneypunct_cache<_CharT, _Intl>*>(__caches[__i]);
67 }
68 };
69
70 template<typename _CharT, bool _Intl>
71 void
72 __moneypunct_cache<_CharT, _Intl>::_M_cache(const locale& __loc)
73 {
74 _M_allocated = true;
75
76 const moneypunct<_CharT, _Intl>& __mp =
77 use_facet<moneypunct<_CharT, _Intl> >(__loc);
78
79 _M_grouping_size = __mp.grouping().size();
80 char* __grouping = new char[_M_grouping_size];
81 __mp.grouping().copy(__grouping, _M_grouping_size);
82 _M_grouping = __grouping;
83 _M_use_grouping = (_M_grouping_size
84 && static_cast<signed char>(__mp.grouping()[0]) > 0);
85
86 _M_decimal_point = __mp.decimal_point();
87 _M_thousands_sep = __mp.thousands_sep();
88 _M_frac_digits = __mp.frac_digits();
89
90 _M_curr_symbol_size = __mp.curr_symbol().size();
91 _CharT* __curr_symbol = new _CharT[_M_curr_symbol_size];
92 __mp.curr_symbol().copy(__curr_symbol, _M_curr_symbol_size);
93 _M_curr_symbol = __curr_symbol;
94
95 _M_positive_sign_size = __mp.positive_sign().size();
96 _CharT* __positive_sign = new _CharT[_M_positive_sign_size];
97 __mp.positive_sign().copy(__positive_sign, _M_positive_sign_size);
98 _M_positive_sign = __positive_sign;
99
100 _M_negative_sign_size = __mp.negative_sign().size();
101 _CharT* __negative_sign = new _CharT[_M_negative_sign_size];
102 __mp.negative_sign().copy(__negative_sign, _M_negative_sign_size);
103 _M_negative_sign = __negative_sign;
104
105 _M_pos_format = __mp.pos_format();
106 _M_neg_format = __mp.neg_format();
107
108 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc);
109 __ct.widen(money_base::_S_atoms,
110 money_base::_S_atoms + money_base::_S_end, _M_atoms);
111 }
112
113_GLIBCXX_BEGIN_LDBL_NAMESPACE
114
115 template<typename _CharT, typename _InIter>
116 template<bool _Intl>
117 _InIter
118 money_get<_CharT, _InIter>::
119 _M_extract(iter_type __beg, iter_type __end, ios_base& __io,
120 ios_base::iostate& __err, string& __units) const
121 {
122 typedef char_traits<_CharT> __traits_type;
123 typedef typename string_type::size_type size_type;
124 typedef money_base::part part;
125 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
126
127 const locale& __loc = __io._M_getloc();
128 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
129
130 __use_cache<__cache_type> __uc;
131 const __cache_type* __lc = __uc(__loc);
132 const char_type* __lit = __lc->_M_atoms;
133
134 // Deduced sign.
135 bool __negative = false;
136 // Sign size.
137 size_type __sign_size = 0;
138 // True if sign is mandatory.
139 const bool __mandatory_sign = (__lc->_M_positive_sign_size
140 && __lc->_M_negative_sign_size);
141 // String of grouping info from thousands_sep plucked from __units.
142 string __grouping_tmp;
143 if (__lc->_M_use_grouping)
144 __grouping_tmp.reserve(32);
145 // Last position before the decimal point.
146 int __last_pos = 0;
147 // Separator positions, then, possibly, fractional digits.
148 int __n = 0;
149 // If input iterator is in a valid state.
150 bool __testvalid = true;
151 // Flag marking when a decimal point is found.
152 bool __testdecfound = false;
153
154 // The tentative returned string is stored here.
155 string __res;
156 __res.reserve(32);
157
158 const char_type* __lit_zero = __lit + money_base::_S_zero;
159 const money_base::pattern __p = __lc->_M_neg_format;
160 for (int __i = 0; __i < 4 && __testvalid; ++__i)
161 {
162 const part __which = static_cast<part>(__p.field[__i]);
163 switch (__which)
164 {
165 case money_base::symbol:
166 // According to 22.2.6.1.2, p2, symbol is required
167 // if (__io.flags() & ios_base::showbase), otherwise
168 // is optional and consumed only if other characters
169 // are needed to complete the format.
170 if (__io.flags() & ios_base::showbase || __sign_size > 1
171 || __i == 0
172 || (__i == 1 && (__mandatory_sign
173 || (static_cast<part>(__p.field[0])
174 == money_base::sign)
175 || (static_cast<part>(__p.field[2])
176 == money_base::space)))
177 || (__i == 2 && ((static_cast<part>(__p.field[3])
178 == money_base::value)
179 || (__mandatory_sign
180 && (static_cast<part>(__p.field[3])
181 == money_base::sign)))))
182 {
183 const size_type __len = __lc->_M_curr_symbol_size;
184 size_type __j = 0;
185 for (; __beg != __end && __j < __len
186 && *__beg == __lc->_M_curr_symbol[__j];
187 ++__beg, ++__j);
188 if (__j != __len
189 && (__j || __io.flags() & ios_base::showbase))
190 __testvalid = false;
191 }
192 break;
193 case money_base::sign:
194 // Sign might not exist, or be more than one character long.
195 if (__lc->_M_positive_sign_size && __beg != __end
196 && *__beg == __lc->_M_positive_sign[0])
197 {
198 __sign_size = __lc->_M_positive_sign_size;
199 ++__beg;
200 }
201 else if (__lc->_M_negative_sign_size && __beg != __end
202 && *__beg == __lc->_M_negative_sign[0])
203 {
204 __negative = true;
205 __sign_size = __lc->_M_negative_sign_size;
206 ++__beg;
207 }
208 else if (__lc->_M_positive_sign_size
209 && !__lc->_M_negative_sign_size)
210 // "... if no sign is detected, the result is given the sign
211 // that corresponds to the source of the empty string"
212 __negative = true;
213 else if (__mandatory_sign)
214 __testvalid = false;
215 break;
216 case money_base::value:
217 // Extract digits, remove and stash away the
218 // grouping of found thousands separators.
219 for (; __beg != __end; ++__beg)
220 {
221 const char_type __c = *__beg;
222 const char_type* __q = __traits_type::find(__lit_zero,
223 10, __c);
224 if (__q != 0)
225 {
226 __res += money_base::_S_atoms[__q - __lit];
227 ++__n;
228 }
229 else if (__c == __lc->_M_decimal_point
230 && !__testdecfound)
231 {
015daa3a
PC
232 if (__lc->_M_frac_digits <= 0)
233 break;
234
f749a55b
PC
235 __last_pos = __n;
236 __n = 0;
237 __testdecfound = true;
238 }
239 else if (__lc->_M_use_grouping
240 && __c == __lc->_M_thousands_sep
241 && !__testdecfound)
242 {
243 if (__n)
244 {
245 // Mark position for later analysis.
246 __grouping_tmp += static_cast<char>(__n);
247 __n = 0;
248 }
249 else
250 {
251 __testvalid = false;
252 break;
253 }
254 }
255 else
256 break;
257 }
258 if (__res.empty())
259 __testvalid = false;
260 break;
261 case money_base::space:
262 // At least one space is required.
263 if (__beg != __end && __ctype.is(ctype_base::space, *__beg))
264 ++__beg;
265 else
266 __testvalid = false;
267 case money_base::none:
268 // Only if not at the end of the pattern.
269 if (__i != 3)
270 for (; __beg != __end
271 && __ctype.is(ctype_base::space, *__beg); ++__beg);
272 break;
273 }
274 }
275
276 // Need to get the rest of the sign characters, if they exist.
277 if (__sign_size > 1 && __testvalid)
278 {
279 const char_type* __sign = __negative ? __lc->_M_negative_sign
280 : __lc->_M_positive_sign;
281 size_type __i = 1;
282 for (; __beg != __end && __i < __sign_size
283 && *__beg == __sign[__i]; ++__beg, ++__i);
284
285 if (__i != __sign_size)
286 __testvalid = false;
287 }
288
289 if (__testvalid)
290 {
291 // Strip leading zeros.
292 if (__res.size() > 1)
293 {
294 const size_type __first = __res.find_first_not_of('0');
295 const bool __only_zeros = __first == string::npos;
296 if (__first)
297 __res.erase(0, __only_zeros ? __res.size() - 1 : __first);
298 }
299
300 // 22.2.6.1.2, p4
301 if (__negative && __res[0] != '0')
302 __res.insert(__res.begin(), '-');
303
304 // Test for grouping fidelity.
305 if (__grouping_tmp.size())
306 {
307 // Add the ending grouping.
308 __grouping_tmp += static_cast<char>(__testdecfound ? __last_pos
309 : __n);
310 if (!std::__verify_grouping(__lc->_M_grouping,
311 __lc->_M_grouping_size,
312 __grouping_tmp))
313 __err |= ios_base::failbit;
314 }
315
316 // Iff not enough digits were supplied after the decimal-point.
015daa3a 317 if (__testdecfound && __n != __lc->_M_frac_digits)
f749a55b
PC
318 __testvalid = false;
319 }
015daa3a 320
f749a55b
PC
321 // Iff valid sequence is not recognized.
322 if (!__testvalid)
323 __err |= ios_base::failbit;
324 else
325 __units.swap(__res);
326
327 // Iff no more characters are available.
328 if (__beg == __end)
329 __err |= ios_base::eofbit;
330 return __beg;
331 }
332
333#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
334 template<typename _CharT, typename _InIter>
335 _InIter
336 money_get<_CharT, _InIter>::
337 __do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
338 ios_base::iostate& __err, double& __units) const
339 {
340 string __str;
341 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
342 : _M_extract<false>(__beg, __end, __io, __err, __str);
343 std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
344 return __beg;
345 }
346#endif
347
348 template<typename _CharT, typename _InIter>
349 _InIter
350 money_get<_CharT, _InIter>::
351 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
352 ios_base::iostate& __err, long double& __units) const
353 {
354 string __str;
355 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
356 : _M_extract<false>(__beg, __end, __io, __err, __str);
357 std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale());
358 return __beg;
359 }
360
361 template<typename _CharT, typename _InIter>
362 _InIter
363 money_get<_CharT, _InIter>::
364 do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io,
365 ios_base::iostate& __err, string_type& __digits) const
366 {
367 typedef typename string::size_type size_type;
368
369 const locale& __loc = __io._M_getloc();
370 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
371
372 string __str;
373 __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str)
374 : _M_extract<false>(__beg, __end, __io, __err, __str);
375 const size_type __len = __str.size();
376 if (__len)
377 {
378 __digits.resize(__len);
379 __ctype.widen(__str.data(), __str.data() + __len, &__digits[0]);
380 }
381 return __beg;
382 }
383
384 template<typename _CharT, typename _OutIter>
385 template<bool _Intl>
386 _OutIter
387 money_put<_CharT, _OutIter>::
388 _M_insert(iter_type __s, ios_base& __io, char_type __fill,
389 const string_type& __digits) const
390 {
391 typedef typename string_type::size_type size_type;
392 typedef money_base::part part;
393 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
394
395 const locale& __loc = __io._M_getloc();
396 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
397
398 __use_cache<__cache_type> __uc;
399 const __cache_type* __lc = __uc(__loc);
400 const char_type* __lit = __lc->_M_atoms;
401
402 // Determine if negative or positive formats are to be used, and
403 // discard leading negative_sign if it is present.
404 const char_type* __beg = __digits.data();
405
406 money_base::pattern __p;
407 const char_type* __sign;
408 size_type __sign_size;
409 if (!(*__beg == __lit[money_base::_S_minus]))
410 {
411 __p = __lc->_M_pos_format;
412 __sign = __lc->_M_positive_sign;
413 __sign_size = __lc->_M_positive_sign_size;
414 }
415 else
416 {
417 __p = __lc->_M_neg_format;
418 __sign = __lc->_M_negative_sign;
419 __sign_size = __lc->_M_negative_sign_size;
420 if (__digits.size())
421 ++__beg;
422 }
423
424 // Look for valid numbers in the ctype facet within input digits.
425 size_type __len = __ctype.scan_not(ctype_base::digit, __beg,
426 __beg + __digits.size()) - __beg;
427 if (__len)
428 {
429 // Assume valid input, and attempt to format.
430 // Break down input numbers into base components, as follows:
431 // final_value = grouped units + (decimal point) + (digits)
432 string_type __value;
433 __value.reserve(2 * __len);
434
435 // Add thousands separators to non-decimal digits, per
436 // grouping rules.
437 long __paddec = __len - __lc->_M_frac_digits;
438 if (__paddec > 0)
439 {
440 if (__lc->_M_frac_digits < 0)
441 __paddec = __len;
442 if (__lc->_M_grouping_size)
443 {
444 __value.assign(2 * __paddec, char_type());
445 _CharT* __vend =
446 std::__add_grouping(&__value[0], __lc->_M_thousands_sep,
447 __lc->_M_grouping,
448 __lc->_M_grouping_size,
449 __beg, __beg + __paddec);
450 __value.erase(__vend - &__value[0]);
451 }
452 else
453 __value.assign(__beg, __paddec);
454 }
455
456 // Deal with decimal point, decimal digits.
457 if (__lc->_M_frac_digits > 0)
458 {
459 __value += __lc->_M_decimal_point;
460 if (__paddec >= 0)
461 __value.append(__beg + __paddec, __lc->_M_frac_digits);
462 else
463 {
464 // Have to pad zeros in the decimal position.
465 __value.append(-__paddec, __lit[money_base::_S_zero]);
466 __value.append(__beg, __len);
467 }
468 }
469
470 // Calculate length of resulting string.
471 const ios_base::fmtflags __f = __io.flags()
472 & ios_base::adjustfield;
473 __len = __value.size() + __sign_size;
474 __len += ((__io.flags() & ios_base::showbase)
475 ? __lc->_M_curr_symbol_size : 0);
476
477 string_type __res;
478 __res.reserve(2 * __len);
479
480 const size_type __width = static_cast<size_type>(__io.width());
481 const bool __testipad = (__f == ios_base::internal
482 && __len < __width);
483 // Fit formatted digits into the required pattern.
484 for (int __i = 0; __i < 4; ++__i)
485 {
486 const part __which = static_cast<part>(__p.field[__i]);
487 switch (__which)
488 {
489 case money_base::symbol:
490 if (__io.flags() & ios_base::showbase)
491 __res.append(__lc->_M_curr_symbol,
492 __lc->_M_curr_symbol_size);
493 break;
494 case money_base::sign:
495 // Sign might not exist, or be more than one
28dac70a 496 // character long. In that case, add in the rest
f749a55b
PC
497 // below.
498 if (__sign_size)
499 __res += __sign[0];
500 break;
501 case money_base::value:
502 __res += __value;
503 break;
504 case money_base::space:
505 // At least one space is required, but if internal
506 // formatting is required, an arbitrary number of
507 // fill spaces will be necessary.
508 if (__testipad)
509 __res.append(__width - __len, __fill);
510 else
511 __res += __fill;
512 break;
513 case money_base::none:
514 if (__testipad)
515 __res.append(__width - __len, __fill);
516 break;
517 }
518 }
519
520 // Special case of multi-part sign parts.
521 if (__sign_size > 1)
522 __res.append(__sign + 1, __sign_size - 1);
523
524 // Pad, if still necessary.
525 __len = __res.size();
526 if (__width > __len)
527 {
528 if (__f == ios_base::left)
529 // After.
530 __res.append(__width - __len, __fill);
531 else
532 // Before.
533 __res.insert(0, __width - __len, __fill);
534 __len = __width;
535 }
536
537 // Write resulting, fully-formatted string to output iterator.
538 __s = std::__write(__s, __res.data(), __len);
539 }
540 __io.width(0);
541 return __s;
542 }
543
544#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
545 template<typename _CharT, typename _OutIter>
546 _OutIter
547 money_put<_CharT, _OutIter>::
548 __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
549 double __units) const
550 { return this->do_put(__s, __intl, __io, __fill, (long double) __units); }
551#endif
552
553 template<typename _CharT, typename _OutIter>
554 _OutIter
555 money_put<_CharT, _OutIter>::
556 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
557 long double __units) const
558 {
559 const locale __loc = __io.getloc();
560 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
561#ifdef _GLIBCXX_USE_C99
562 // First try a buffer perhaps big enough.
563 int __cs_size = 64;
564 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
565 // _GLIBCXX_RESOLVE_LIB_DEFECTS
566 // 328. Bad sprintf format modifier in money_put<>::do_put()
567 int __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
568 "%.*Lf", 0, __units);
569 // If the buffer was not large enough, try again with the correct size.
570 if (__len >= __cs_size)
571 {
572 __cs_size = __len + 1;
573 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
574 __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size,
575 "%.*Lf", 0, __units);
576 }
577#else
578 // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'.
579 const int __cs_size =
580 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 3;
581 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
582 int __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, "%.*Lf",
583 0, __units);
584#endif
585 string_type __digits(__len, char_type());
586 __ctype.widen(__cs, __cs + __len, &__digits[0]);
587 return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
588 : _M_insert<false>(__s, __io, __fill, __digits);
589 }
590
591 template<typename _CharT, typename _OutIter>
592 _OutIter
593 money_put<_CharT, _OutIter>::
594 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
595 const string_type& __digits) const
596 { return __intl ? _M_insert<true>(__s, __io, __fill, __digits)
597 : _M_insert<false>(__s, __io, __fill, __digits); }
598
599_GLIBCXX_END_LDBL_NAMESPACE
600
601 // NB: Not especially useful. Without an ios_base object or some
602 // kind of locale reference, we are left clawing at the air where
603 // the side of the mountain used to be...
604 template<typename _CharT, typename _InIter>
605 time_base::dateorder
606 time_get<_CharT, _InIter>::do_date_order() const
607 { return time_base::no_order; }
608
609 // Expand a strftime format string and parse it. E.g., do_get_date() may
610 // pass %m/%d/%Y => extracted characters.
611 template<typename _CharT, typename _InIter>
612 _InIter
613 time_get<_CharT, _InIter>::
614 _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
615 ios_base::iostate& __err, tm* __tm,
616 const _CharT* __format) const
617 {
618 const locale& __loc = __io._M_getloc();
619 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
620 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
621 const size_t __len = char_traits<_CharT>::length(__format);
622
623 ios_base::iostate __tmperr = ios_base::goodbit;
624 for (size_t __i = 0; __beg != __end && __i < __len && !__tmperr; ++__i)
625 {
626 if (__ctype.narrow(__format[__i], 0) == '%')
627 {
628 // Verify valid formatting code, attempt to extract.
629 char __c = __ctype.narrow(__format[++__i], 0);
630 int __mem = 0;
631 if (__c == 'E' || __c == 'O')
632 __c = __ctype.narrow(__format[++__i], 0);
633 switch (__c)
634 {
635 const char* __cs;
636 _CharT __wcs[10];
637 case 'a':
638 // Abbreviated weekday name [tm_wday]
639 const char_type* __days1[7];
640 __tp._M_days_abbreviated(__days1);
641 __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days1,
642 7, __io, __tmperr);
643 break;
644 case 'A':
645 // Weekday name [tm_wday].
646 const char_type* __days2[7];
647 __tp._M_days(__days2);
648 __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days2,
649 7, __io, __tmperr);
650 break;
651 case 'h':
652 case 'b':
653 // Abbreviated month name [tm_mon]
654 const char_type* __months1[12];
655 __tp._M_months_abbreviated(__months1);
656 __beg = _M_extract_name(__beg, __end, __tm->tm_mon,
657 __months1, 12, __io, __tmperr);
658 break;
659 case 'B':
660 // Month name [tm_mon].
661 const char_type* __months2[12];
662 __tp._M_months(__months2);
663 __beg = _M_extract_name(__beg, __end, __tm->tm_mon,
664 __months2, 12, __io, __tmperr);
665 break;
666 case 'c':
667 // Default time and date representation.
668 const char_type* __dt[2];
669 __tp._M_date_time_formats(__dt);
670 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
671 __tm, __dt[0]);
672 break;
673 case 'd':
674 // Day [01, 31]. [tm_mday]
675 __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2,
676 __io, __tmperr);
677 break;
678 case 'e':
679 // Day [1, 31], with single digits preceded by
680 // space. [tm_mday]
681 if (__ctype.is(ctype_base::space, *__beg))
682 __beg = _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9,
683 1, __io, __tmperr);
684 else
685 __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31,
686 2, __io, __tmperr);
687 break;
688 case 'D':
689 // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year]
690 __cs = "%m/%d/%y";
691 __ctype.widen(__cs, __cs + 9, __wcs);
692 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
693 __tm, __wcs);
694 break;
695 case 'H':
696 // Hour [00, 23]. [tm_hour]
697 __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2,
698 __io, __tmperr);
699 break;
700 case 'I':
701 // Hour [01, 12]. [tm_hour]
702 __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2,
703 __io, __tmperr);
704 break;
705 case 'm':
706 // Month [01, 12]. [tm_mon]
707 __beg = _M_extract_num(__beg, __end, __mem, 1, 12, 2,
708 __io, __tmperr);
709 if (!__tmperr)
710 __tm->tm_mon = __mem - 1;
711 break;
712 case 'M':
713 // Minute [00, 59]. [tm_min]
714 __beg = _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2,
715 __io, __tmperr);
716 break;
717 case 'n':
718 if (__ctype.narrow(*__beg, 0) == '\n')
719 ++__beg;
720 else
721 __tmperr |= ios_base::failbit;
722 break;
723 case 'R':
724 // Equivalent to (%H:%M).
725 __cs = "%H:%M";
726 __ctype.widen(__cs, __cs + 6, __wcs);
727 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
728 __tm, __wcs);
729 break;
730 case 'S':
731 // Seconds. [tm_sec]
732 // [00, 60] in C99 (one leap-second), [00, 61] in C89.
733#ifdef _GLIBCXX_USE_C99
734 __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 60, 2,
735#else
736 __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 61, 2,
737#endif
738 __io, __tmperr);
739 break;
740 case 't':
741 if (__ctype.narrow(*__beg, 0) == '\t')
742 ++__beg;
743 else
744 __tmperr |= ios_base::failbit;
745 break;
746 case 'T':
747 // Equivalent to (%H:%M:%S).
748 __cs = "%H:%M:%S";
749 __ctype.widen(__cs, __cs + 9, __wcs);
750 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
751 __tm, __wcs);
752 break;
753 case 'x':
754 // Locale's date.
755 const char_type* __dates[2];
756 __tp._M_date_formats(__dates);
757 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
758 __tm, __dates[0]);
759 break;
760 case 'X':
761 // Locale's time.
762 const char_type* __times[2];
763 __tp._M_time_formats(__times);
764 __beg = _M_extract_via_format(__beg, __end, __io, __tmperr,
765 __tm, __times[0]);
766 break;
767 case 'y':
768 case 'C': // C99
769 // Two digit year. [tm_year]
770 __beg = _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2,
771 __io, __tmperr);
772 break;
773 case 'Y':
774 // Year [1900). [tm_year]
775 __beg = _M_extract_num(__beg, __end, __mem, 0, 9999, 4,
776 __io, __tmperr);
777 if (!__tmperr)
778 __tm->tm_year = __mem - 1900;
779 break;
780 case 'Z':
781 // Timezone info.
782 if (__ctype.is(ctype_base::upper, *__beg))
783 {
784 int __tmp;
785 __beg = _M_extract_name(__beg, __end, __tmp,
786 __timepunct_cache<_CharT>::_S_timezones,
787 14, __io, __tmperr);
788
789 // GMT requires special effort.
790 if (__beg != __end && !__tmperr && __tmp == 0
791 && (*__beg == __ctype.widen('-')
792 || *__beg == __ctype.widen('+')))
793 {
794 __beg = _M_extract_num(__beg, __end, __tmp, 0, 23, 2,
795 __io, __tmperr);
796 __beg = _M_extract_num(__beg, __end, __tmp, 0, 59, 2,
797 __io, __tmperr);
798 }
799 }
800 else
801 __tmperr |= ios_base::failbit;
802 break;
803 default:
804 // Not recognized.
805 __tmperr |= ios_base::failbit;
806 }
807 }
808 else
809 {
810 // Verify format and input match, extract and discard.
811 if (__format[__i] == *__beg)
812 ++__beg;
813 else
814 __tmperr |= ios_base::failbit;
815 }
816 }
817
818 if (__tmperr)
819 __err |= ios_base::failbit;
820
821 return __beg;
822 }
823
824 template<typename _CharT, typename _InIter>
825 _InIter
826 time_get<_CharT, _InIter>::
827 _M_extract_num(iter_type __beg, iter_type __end, int& __member,
828 int __min, int __max, size_t __len,
829 ios_base& __io, ios_base::iostate& __err) const
830 {
831 const locale& __loc = __io._M_getloc();
832 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
833
834 // As-is works for __len = 1, 2, 4, the values actually used.
835 int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1);
836
837 ++__min;
838 size_t __i = 0;
839 int __value = 0;
840 for (; __beg != __end && __i < __len; ++__beg, ++__i)
841 {
842 const char __c = __ctype.narrow(*__beg, '*');
843 if (__c >= '0' && __c <= '9')
844 {
845 __value = __value * 10 + (__c - '0');
846 const int __valuec = __value * __mult;
847 if (__valuec > __max || __valuec + __mult < __min)
848 break;
849 __mult /= 10;
850 }
851 else
852 break;
853 }
854 if (__i == __len)
855 __member = __value;
856 else
857 __err |= ios_base::failbit;
858
859 return __beg;
860 }
861
862 // Assumptions:
863 // All elements in __names are unique.
864 template<typename _CharT, typename _InIter>
865 _InIter
866 time_get<_CharT, _InIter>::
867 _M_extract_name(iter_type __beg, iter_type __end, int& __member,
868 const _CharT** __names, size_t __indexlen,
869 ios_base& __io, ios_base::iostate& __err) const
870 {
871 typedef char_traits<_CharT> __traits_type;
872 const locale& __loc = __io._M_getloc();
873 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
874
875 int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int)
876 * __indexlen));
877 size_t __nmatches = 0;
878 size_t __pos = 0;
879 bool __testvalid = true;
880 const char_type* __name;
881
882 // Look for initial matches.
883 // NB: Some of the locale data is in the form of all lowercase
884 // names, and some is in the form of initially-capitalized
885 // names. Look for both.
886 if (__beg != __end)
887 {
888 const char_type __c = *__beg;
889 for (size_t __i1 = 0; __i1 < __indexlen; ++__i1)
890 if (__c == __names[__i1][0]
891 || __c == __ctype.toupper(__names[__i1][0]))
892 __matches[__nmatches++] = __i1;
893 }
894
895 while (__nmatches > 1)
896 {
897 // Find smallest matching string.
898 size_t __minlen = __traits_type::length(__names[__matches[0]]);
899 for (size_t __i2 = 1; __i2 < __nmatches; ++__i2)
900 __minlen = std::min(__minlen,
901 __traits_type::length(__names[__matches[__i2]]));
902 ++__beg, ++__pos;
903 if (__pos < __minlen && __beg != __end)
904 for (size_t __i3 = 0; __i3 < __nmatches;)
905 {
906 __name = __names[__matches[__i3]];
907 if (!(__name[__pos] == *__beg))
908 __matches[__i3] = __matches[--__nmatches];
909 else
910 ++__i3;
911 }
912 else
913 break;
914 }
915
916 if (__nmatches == 1)
917 {
918 // Make sure found name is completely extracted.
919 ++__beg, ++__pos;
920 __name = __names[__matches[0]];
921 const size_t __len = __traits_type::length(__name);
922 while (__pos < __len && __beg != __end && __name[__pos] == *__beg)
923 ++__beg, ++__pos;
924
925 if (__len == __pos)
926 __member = __matches[0];
927 else
928 __testvalid = false;
929 }
930 else
931 __testvalid = false;
932 if (!__testvalid)
933 __err |= ios_base::failbit;
934
935 return __beg;
936 }
937
938 template<typename _CharT, typename _InIter>
939 _InIter
940 time_get<_CharT, _InIter>::
941 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
942 ios_base::iostate& __err, tm* __tm) const
943 {
944 const locale& __loc = __io._M_getloc();
945 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
946 const char_type* __times[2];
947 __tp._M_time_formats(__times);
948 __beg = _M_extract_via_format(__beg, __end, __io, __err,
949 __tm, __times[0]);
950 if (__beg == __end)
951 __err |= ios_base::eofbit;
952 return __beg;
953 }
954
955 template<typename _CharT, typename _InIter>
956 _InIter
957 time_get<_CharT, _InIter>::
958 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
959 ios_base::iostate& __err, tm* __tm) const
960 {
961 const locale& __loc = __io._M_getloc();
962 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
963 const char_type* __dates[2];
964 __tp._M_date_formats(__dates);
965 __beg = _M_extract_via_format(__beg, __end, __io, __err,
966 __tm, __dates[0]);
967 if (__beg == __end)
968 __err |= ios_base::eofbit;
969 return __beg;
970 }
971
972 template<typename _CharT, typename _InIter>
973 _InIter
974 time_get<_CharT, _InIter>::
975 do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
976 ios_base::iostate& __err, tm* __tm) const
977 {
978 typedef char_traits<_CharT> __traits_type;
979 const locale& __loc = __io._M_getloc();
980 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
981 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
982 const char_type* __days[7];
983 __tp._M_days_abbreviated(__days);
984 int __tmpwday;
985 ios_base::iostate __tmperr = ios_base::goodbit;
986 __beg = _M_extract_name(__beg, __end, __tmpwday, __days, 7,
987 __io, __tmperr);
988
989 // Check to see if non-abbreviated name exists, and extract.
990 // NB: Assumes both _M_days and _M_days_abbreviated organized in
991 // exact same order, first to last, such that the resulting
992 // __days array with the same index points to a day, and that
993 // day's abbreviated form.
994 // NB: Also assumes that an abbreviated name is a subset of the name.
995 if (!__tmperr && __beg != __end)
996 {
997 size_t __pos = __traits_type::length(__days[__tmpwday]);
998 __tp._M_days(__days);
999 const char_type* __name = __days[__tmpwday];
1000 if (__name[__pos] == *__beg)
1001 {
1002 // Extract the rest of it.
1003 const size_t __len = __traits_type::length(__name);
1004 while (__pos < __len && __beg != __end
1005 && __name[__pos] == *__beg)
1006 ++__beg, ++__pos;
1007 if (__len != __pos)
1008 __tmperr |= ios_base::failbit;
1009 }
1010 }
1011 if (!__tmperr)
1012 __tm->tm_wday = __tmpwday;
1013 else
1014 __err |= ios_base::failbit;
1015
1016 if (__beg == __end)
1017 __err |= ios_base::eofbit;
1018 return __beg;
1019 }
1020
1021 template<typename _CharT, typename _InIter>
1022 _InIter
1023 time_get<_CharT, _InIter>::
1024 do_get_monthname(iter_type __beg, iter_type __end,
1025 ios_base& __io, ios_base::iostate& __err, tm* __tm) const
1026 {
1027 typedef char_traits<_CharT> __traits_type;
1028 const locale& __loc = __io._M_getloc();
1029 const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc);
1030 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1031 const char_type* __months[12];
1032 __tp._M_months_abbreviated(__months);
1033 int __tmpmon;
1034 ios_base::iostate __tmperr = ios_base::goodbit;
1035 __beg = _M_extract_name(__beg, __end, __tmpmon, __months, 12,
1036 __io, __tmperr);
1037
1038 // Check to see if non-abbreviated name exists, and extract.
1039 // NB: Assumes both _M_months and _M_months_abbreviated organized in
1040 // exact same order, first to last, such that the resulting
1041 // __months array with the same index points to a month, and that
1042 // month's abbreviated form.
1043 // NB: Also assumes that an abbreviated name is a subset of the name.
1044 if (!__tmperr && __beg != __end)
1045 {
1046 size_t __pos = __traits_type::length(__months[__tmpmon]);
1047 __tp._M_months(__months);
1048 const char_type* __name = __months[__tmpmon];
1049 if (__name[__pos] == *__beg)
1050 {
1051 // Extract the rest of it.
1052 const size_t __len = __traits_type::length(__name);
1053 while (__pos < __len && __beg != __end
1054 && __name[__pos] == *__beg)
1055 ++__beg, ++__pos;
1056 if (__len != __pos)
1057 __tmperr |= ios_base::failbit;
1058 }
1059 }
1060 if (!__tmperr)
1061 __tm->tm_mon = __tmpmon;
1062 else
1063 __err |= ios_base::failbit;
1064
1065 if (__beg == __end)
1066 __err |= ios_base::eofbit;
1067 return __beg;
1068 }
1069
1070 template<typename _CharT, typename _InIter>
1071 _InIter
1072 time_get<_CharT, _InIter>::
1073 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
1074 ios_base::iostate& __err, tm* __tm) const
1075 {
1076 const locale& __loc = __io._M_getloc();
1077 const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);
1078
1079 size_t __i = 0;
1080 int __value = 0;
1081 for (; __beg != __end && __i < 4; ++__beg, ++__i)
1082 {
1083 const char __c = __ctype.narrow(*__beg, '*');
1084 if (__c >= '0' && __c <= '9')
1085 __value = __value * 10 + (__c - '0');
1086 else
1087 break;
1088 }
1089 if (__i == 2 || __i == 4)
1090 __tm->tm_year = __i == 2 ? __value : __value - 1900;
1091 else
1092 __err |= ios_base::failbit;
1093
1094 if (__beg == __end)
1095 __err |= ios_base::eofbit;
1096 return __beg;
1097 }
1098
1099 template<typename _CharT, typename _OutIter>
1100 _OutIter
1101 time_put<_CharT, _OutIter>::
1102 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
1103 const _CharT* __beg, const _CharT* __end) const
1104 {
1105 const locale& __loc = __io._M_getloc();
1106 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1107 for (; __beg != __end; ++__beg)
1108 if (__ctype.narrow(*__beg, 0) != '%')
1109 {
1110 *__s = *__beg;
1111 ++__s;
1112 }
1113 else if (++__beg != __end)
1114 {
1115 char __format;
1116 char __mod = 0;
1117 const char __c = __ctype.narrow(*__beg, 0);
1118 if (__c != 'E' && __c != 'O')
1119 __format = __c;
1120 else if (++__beg != __end)
1121 {
1122 __mod = __c;
1123 __format = __ctype.narrow(*__beg, 0);
1124 }
1125 else
1126 break;
1127 __s = this->do_put(__s, __io, __fill, __tm, __format, __mod);
1128 }
1129 else
1130 break;
1131 return __s;
1132 }
1133
1134 template<typename _CharT, typename _OutIter>
1135 _OutIter
1136 time_put<_CharT, _OutIter>::
1137 do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm,
1138 char __format, char __mod) const
1139 {
1140 const locale& __loc = __io._M_getloc();
1141 ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc);
1142 __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc);
1143
1144 // NB: This size is arbitrary. Should this be a data member,
1145 // initialized at construction?
1146 const size_t __maxlen = 128;
1147 char_type* __res =
1148 static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen));
1149
1150 // NB: In IEE 1003.1-200x, and perhaps other locale models, it
1151 // is possible that the format character will be longer than one
1152 // character. Possibilities include 'E' or 'O' followed by a
1153 // format character: if __mod is not the default argument, assume
1154 // it's a valid modifier.
1155 char_type __fmt[4];
1156 __fmt[0] = __ctype.widen('%');
1157 if (!__mod)
1158 {
1159 __fmt[1] = __format;
1160 __fmt[2] = char_type();
1161 }
1162 else
1163 {
1164 __fmt[1] = __mod;
1165 __fmt[2] = __format;
1166 __fmt[3] = char_type();
1167 }
1168
1169 __tp._M_put(__res, __maxlen, __fmt, __tm);
1170
1171 // Write resulting, fully-formatted string to output iterator.
1172 return std::__write(__s, __res, char_traits<char_type>::length(__res));
1173 }
1174
84b31797 1175
f749a55b
PC
1176 // Inhibit implicit instantiations for required instantiations,
1177 // which are defined via explicit instantiations elsewhere.
1178 // NB: This syntax is a GNU extension.
1179#if _GLIBCXX_EXTERN_TEMPLATE
1180 extern template class moneypunct<char, false>;
1181 extern template class moneypunct<char, true>;
1182 extern template class moneypunct_byname<char, false>;
1183 extern template class moneypunct_byname<char, true>;
1184 extern template class _GLIBCXX_LDBL_NAMESPACE money_get<char>;
1185 extern template class _GLIBCXX_LDBL_NAMESPACE money_put<char>;
1186 extern template class __timepunct<char>;
1187 extern template class time_put<char>;
1188 extern template class time_put_byname<char>;
1189 extern template class time_get<char>;
1190 extern template class time_get_byname<char>;
1191 extern template class messages<char>;
1192 extern template class messages_byname<char>;
1193
1194 extern template
1195 const moneypunct<char, true>&
1196 use_facet<moneypunct<char, true> >(const locale&);
1197
1198 extern template
1199 const moneypunct<char, false>&
1200 use_facet<moneypunct<char, false> >(const locale&);
1201
1202 extern template
1203 const money_put<char>&
1204 use_facet<money_put<char> >(const locale&);
1205
1206 extern template
1207 const money_get<char>&
1208 use_facet<money_get<char> >(const locale&);
1209
1210 extern template
1211 const __timepunct<char>&
1212 use_facet<__timepunct<char> >(const locale&);
1213
1214 extern template
1215 const time_put<char>&
1216 use_facet<time_put<char> >(const locale&);
1217
1218 extern template
1219 const time_get<char>&
1220 use_facet<time_get<char> >(const locale&);
1221
1222 extern template
1223 const messages<char>&
1224 use_facet<messages<char> >(const locale&);
1225
1226 extern template
1227 bool
1228 has_facet<moneypunct<char> >(const locale&);
1229
1230 extern template
1231 bool
1232 has_facet<money_put<char> >(const locale&);
1233
1234 extern template
1235 bool
1236 has_facet<money_get<char> >(const locale&);
1237
1238 extern template
1239 bool
1240 has_facet<__timepunct<char> >(const locale&);
1241
1242 extern template
1243 bool
1244 has_facet<time_put<char> >(const locale&);
1245
1246 extern template
1247 bool
1248 has_facet<time_get<char> >(const locale&);
1249
1250 extern template
1251 bool
1252 has_facet<messages<char> >(const locale&);
1253
1254#ifdef _GLIBCXX_USE_WCHAR_T
1255 extern template class moneypunct<wchar_t, false>;
1256 extern template class moneypunct<wchar_t, true>;
1257 extern template class moneypunct_byname<wchar_t, false>;
1258 extern template class moneypunct_byname<wchar_t, true>;
1259 extern template class _GLIBCXX_LDBL_NAMESPACE money_get<wchar_t>;
1260 extern template class _GLIBCXX_LDBL_NAMESPACE money_put<wchar_t>;
1261 extern template class __timepunct<wchar_t>;
1262 extern template class time_put<wchar_t>;
1263 extern template class time_put_byname<wchar_t>;
1264 extern template class time_get<wchar_t>;
1265 extern template class time_get_byname<wchar_t>;
1266 extern template class messages<wchar_t>;
1267 extern template class messages_byname<wchar_t>;
1268
1269 extern template
1270 const moneypunct<wchar_t, true>&
1271 use_facet<moneypunct<wchar_t, true> >(const locale&);
1272
1273 extern template
1274 const moneypunct<wchar_t, false>&
1275 use_facet<moneypunct<wchar_t, false> >(const locale&);
1276
1277 extern template
1278 const money_put<wchar_t>&
1279 use_facet<money_put<wchar_t> >(const locale&);
1280
1281 extern template
1282 const money_get<wchar_t>&
1283 use_facet<money_get<wchar_t> >(const locale&);
1284
1285 extern template
1286 const __timepunct<wchar_t>&
1287 use_facet<__timepunct<wchar_t> >(const locale&);
1288
1289 extern template
1290 const time_put<wchar_t>&
1291 use_facet<time_put<wchar_t> >(const locale&);
1292
1293 extern template
1294 const time_get<wchar_t>&
1295 use_facet<time_get<wchar_t> >(const locale&);
1296
1297 extern template
1298 const messages<wchar_t>&
1299 use_facet<messages<wchar_t> >(const locale&);
1300
1301 extern template
1302 bool
1303 has_facet<moneypunct<wchar_t> >(const locale&);
1304
1305 extern template
1306 bool
1307 has_facet<money_put<wchar_t> >(const locale&);
1308
1309 extern template
1310 bool
1311 has_facet<money_get<wchar_t> >(const locale&);
1312
1313 extern template
1314 bool
1315 has_facet<__timepunct<wchar_t> >(const locale&);
1316
1317 extern template
1318 bool
1319 has_facet<time_put<wchar_t> >(const locale&);
1320
1321 extern template
1322 bool
1323 has_facet<time_get<wchar_t> >(const locale&);
1324
1325 extern template
1326 bool
1327 has_facet<messages<wchar_t> >(const locale&);
1328#endif
1329#endif
1330
1331_GLIBCXX_END_NAMESPACE
1332
1333#endif