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