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