locale_facets.tcc revision 228780
197403Sobrien// Locale support -*- C++ -*- 297403Sobrien 3228780Spfg// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4228780Spfg// 2006, 2007, 2008 597403Sobrien// Free Software Foundation, Inc. 697403Sobrien// 797403Sobrien// This file is part of the GNU ISO C++ Library. This library is free 897403Sobrien// software; you can redistribute it and/or modify it under the 997403Sobrien// terms of the GNU General Public License as published by the 1097403Sobrien// Free Software Foundation; either version 2, or (at your option) 1197403Sobrien// any later version. 1297403Sobrien 1397403Sobrien// This library is distributed in the hope that it will be useful, 1497403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of 1597403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1697403Sobrien// GNU General Public License for more details. 1797403Sobrien 1897403Sobrien// You should have received a copy of the GNU General Public License along 1997403Sobrien// with this library; see the file COPYING. If not, write to the Free 20169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 2197403Sobrien// USA. 2297403Sobrien 2397403Sobrien// As a special exception, you may use this file as part of a free software 2497403Sobrien// library without restriction. Specifically, if other files instantiate 2597403Sobrien// templates or use macros or inline functions from this file, or you compile 2697403Sobrien// this file and link it with other files to produce an executable, this 2797403Sobrien// file does not by itself cause the resulting executable to be covered by 2897403Sobrien// the GNU General Public License. This exception does not however 2997403Sobrien// invalidate any other reasons why the executable file might be covered by 3097403Sobrien// the GNU General Public License. 3197403Sobrien 32169691Skan/** @file locale_facets.tcc 33169691Skan * This is an internal header file, included by other library headers. 34169691Skan * You should not attempt to use it directly. 35169691Skan */ 3697403Sobrien 37132720Skan#ifndef _LOCALE_FACETS_TCC 38132720Skan#define _LOCALE_FACETS_TCC 1 3997403Sobrien 4097403Sobrien#pragma GCC system_header 4197403Sobrien 42132720Skan#include <limits> // For numeric_limits 43132720Skan#include <typeinfo> // For bad_cast. 4497403Sobrien#include <bits/streambuf_iterator.h> 45169691Skan#include <ext/type_traits.h> 4697403Sobrien 47169691Skan_GLIBCXX_BEGIN_NAMESPACE(std) 48169691Skan 4997403Sobrien template<typename _Facet> 5097403Sobrien locale 5197403Sobrien locale::combine(const locale& __other) const 5297403Sobrien { 5397403Sobrien _Impl* __tmp = new _Impl(*_M_impl, 1); 54132720Skan try 55132720Skan { 56132720Skan __tmp->_M_replace_facet(__other._M_impl, &_Facet::id); 57132720Skan } 58132720Skan catch(...) 59132720Skan { 60132720Skan __tmp->_M_remove_reference(); 61132720Skan __throw_exception_again; 62132720Skan } 6397403Sobrien return locale(__tmp); 6497403Sobrien } 6597403Sobrien 6697403Sobrien template<typename _CharT, typename _Traits, typename _Alloc> 6797403Sobrien bool 6897403Sobrien locale::operator()(const basic_string<_CharT, _Traits, _Alloc>& __s1, 6997403Sobrien const basic_string<_CharT, _Traits, _Alloc>& __s2) const 7097403Sobrien { 7197403Sobrien typedef std::collate<_CharT> __collate_type; 7297403Sobrien const __collate_type& __collate = use_facet<__collate_type>(*this); 7397403Sobrien return (__collate.compare(__s1.data(), __s1.data() + __s1.length(), 7497403Sobrien __s2.data(), __s2.data() + __s2.length()) < 0); 7597403Sobrien } 7697403Sobrien 77132720Skan /** 78132720Skan * @brief Test for the presence of a facet. 79132720Skan * 80132720Skan * has_facet tests the locale argument for the presence of the facet type 81132720Skan * provided as the template parameter. Facets derived from the facet 82132720Skan * parameter will also return true. 83132720Skan * 84132720Skan * @param Facet The facet type to test the presence of. 85132720Skan * @param locale The locale to test. 86132720Skan * @return true if locale contains a facet of type Facet, else false. 87132720Skan */ 8897403Sobrien template<typename _Facet> 89132720Skan inline bool 90132720Skan has_facet(const locale& __loc) throw() 91132720Skan { 92132720Skan const size_t __i = _Facet::id._M_id(); 93132720Skan const locale::facet** __facets = __loc._M_impl->_M_facets; 94132720Skan return (__i < __loc._M_impl->_M_facets_size && __facets[__i]); 95132720Skan } 96132720Skan 97132720Skan /** 98132720Skan * @brief Return a facet. 99132720Skan * 100132720Skan * use_facet looks for and returns a reference to a facet of type Facet 101132720Skan * where Facet is the template parameter. If has_facet(locale) is true, 102132720Skan * there is a suitable facet to return. It throws std::bad_cast if the 103132720Skan * locale doesn't contain a facet of type Facet. 104132720Skan * 105132720Skan * @param Facet The facet type to access. 106132720Skan * @param locale The locale to use. 107132720Skan * @return Reference to facet of type Facet. 108132720Skan * @throw std::bad_cast if locale doesn't contain a facet of type Facet. 109132720Skan */ 110132720Skan template<typename _Facet> 111132720Skan inline const _Facet& 11297403Sobrien use_facet(const locale& __loc) 11397403Sobrien { 114132720Skan const size_t __i = _Facet::id._M_id(); 115132720Skan const locale::facet** __facets = __loc._M_impl->_M_facets; 11697403Sobrien if (!(__i < __loc._M_impl->_M_facets_size && __facets[__i])) 11797403Sobrien __throw_bad_cast(); 11897403Sobrien return static_cast<const _Facet&>(*__facets[__i]); 11997403Sobrien } 12097403Sobrien 121228780Spfg 122132720Skan // Routine to access a cache for the facet. If the cache didn't 123132720Skan // exist before, it gets constructed on the fly. 12497403Sobrien template<typename _Facet> 125132720Skan struct __use_cache 12697403Sobrien { 127132720Skan const _Facet* 128132720Skan operator() (const locale& __loc) const; 129132720Skan }; 130132720Skan 131132720Skan // Specializations. 132132720Skan template<typename _CharT> 133132720Skan struct __use_cache<__numpunct_cache<_CharT> > 134132720Skan { 135132720Skan const __numpunct_cache<_CharT>* 136132720Skan operator() (const locale& __loc) const 137132720Skan { 138132720Skan const size_t __i = numpunct<_CharT>::id._M_id(); 139132720Skan const locale::facet** __caches = __loc._M_impl->_M_caches; 140132720Skan if (!__caches[__i]) 141132720Skan { 142132720Skan __numpunct_cache<_CharT>* __tmp = NULL; 143132720Skan try 144132720Skan { 145132720Skan __tmp = new __numpunct_cache<_CharT>; 146132720Skan __tmp->_M_cache(__loc); 147132720Skan } 148132720Skan catch(...) 149132720Skan { 150132720Skan delete __tmp; 151132720Skan __throw_exception_again; 152132720Skan } 153132720Skan __loc._M_impl->_M_install_cache(__tmp, __i); 154132720Skan } 155132720Skan return static_cast<const __numpunct_cache<_CharT>*>(__caches[__i]); 156132720Skan } 157132720Skan }; 158132720Skan 159132720Skan template<typename _CharT, bool _Intl> 160132720Skan struct __use_cache<__moneypunct_cache<_CharT, _Intl> > 161132720Skan { 162132720Skan const __moneypunct_cache<_CharT, _Intl>* 163132720Skan operator() (const locale& __loc) const 164132720Skan { 165132720Skan const size_t __i = moneypunct<_CharT, _Intl>::id._M_id(); 166132720Skan const locale::facet** __caches = __loc._M_impl->_M_caches; 167132720Skan if (!__caches[__i]) 168132720Skan { 169132720Skan __moneypunct_cache<_CharT, _Intl>* __tmp = NULL; 170132720Skan try 171132720Skan { 172132720Skan __tmp = new __moneypunct_cache<_CharT, _Intl>; 173132720Skan __tmp->_M_cache(__loc); 174132720Skan } 175132720Skan catch(...) 176132720Skan { 177132720Skan delete __tmp; 178132720Skan __throw_exception_again; 179132720Skan } 180132720Skan __loc._M_impl->_M_install_cache(__tmp, __i); 181132720Skan } 182132720Skan return static_cast< 183132720Skan const __moneypunct_cache<_CharT, _Intl>*>(__caches[__i]); 184132720Skan } 185132720Skan }; 186132720Skan 187132720Skan template<typename _CharT> 188132720Skan void 189132720Skan __numpunct_cache<_CharT>::_M_cache(const locale& __loc) 190132720Skan { 191132720Skan _M_allocated = true; 192132720Skan 193132720Skan const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc); 194132720Skan 195132720Skan _M_grouping_size = __np.grouping().size(); 196132720Skan char* __grouping = new char[_M_grouping_size]; 197132720Skan __np.grouping().copy(__grouping, _M_grouping_size); 198132720Skan _M_grouping = __grouping; 199169691Skan _M_use_grouping = (_M_grouping_size 200169691Skan && static_cast<signed char>(__np.grouping()[0]) > 0); 201132720Skan 202132720Skan _M_truename_size = __np.truename().size(); 203132720Skan _CharT* __truename = new _CharT[_M_truename_size]; 204132720Skan __np.truename().copy(__truename, _M_truename_size); 205132720Skan _M_truename = __truename; 206132720Skan 207132720Skan _M_falsename_size = __np.falsename().size(); 208132720Skan _CharT* __falsename = new _CharT[_M_falsename_size]; 209132720Skan __np.falsename().copy(__falsename, _M_falsename_size); 210132720Skan _M_falsename = __falsename; 211132720Skan 212132720Skan _M_decimal_point = __np.decimal_point(); 213132720Skan _M_thousands_sep = __np.thousands_sep(); 214132720Skan 215132720Skan const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc); 216132720Skan __ct.widen(__num_base::_S_atoms_out, 217132720Skan __num_base::_S_atoms_out + __num_base::_S_oend, _M_atoms_out); 218132720Skan __ct.widen(__num_base::_S_atoms_in, 219132720Skan __num_base::_S_atoms_in + __num_base::_S_iend, _M_atoms_in); 22097403Sobrien } 22197403Sobrien 222132720Skan template<typename _CharT, bool _Intl> 223132720Skan void 224132720Skan __moneypunct_cache<_CharT, _Intl>::_M_cache(const locale& __loc) 225117397Skan { 226132720Skan _M_allocated = true; 227132720Skan 228132720Skan const moneypunct<_CharT, _Intl>& __mp = 229132720Skan use_facet<moneypunct<_CharT, _Intl> >(__loc); 230132720Skan 231132720Skan _M_grouping_size = __mp.grouping().size(); 232132720Skan char* __grouping = new char[_M_grouping_size]; 233132720Skan __mp.grouping().copy(__grouping, _M_grouping_size); 234132720Skan _M_grouping = __grouping; 235169691Skan _M_use_grouping = (_M_grouping_size 236169691Skan && static_cast<signed char>(__mp.grouping()[0]) > 0); 237132720Skan 238132720Skan _M_decimal_point = __mp.decimal_point(); 239132720Skan _M_thousands_sep = __mp.thousands_sep(); 240132720Skan _M_frac_digits = __mp.frac_digits(); 241132720Skan 242132720Skan _M_curr_symbol_size = __mp.curr_symbol().size(); 243132720Skan _CharT* __curr_symbol = new _CharT[_M_curr_symbol_size]; 244132720Skan __mp.curr_symbol().copy(__curr_symbol, _M_curr_symbol_size); 245132720Skan _M_curr_symbol = __curr_symbol; 246132720Skan 247132720Skan _M_positive_sign_size = __mp.positive_sign().size(); 248132720Skan _CharT* __positive_sign = new _CharT[_M_positive_sign_size]; 249132720Skan __mp.positive_sign().copy(__positive_sign, _M_positive_sign_size); 250132720Skan _M_positive_sign = __positive_sign; 251132720Skan 252132720Skan _M_negative_sign_size = __mp.negative_sign().size(); 253132720Skan _CharT* __negative_sign = new _CharT[_M_negative_sign_size]; 254132720Skan __mp.negative_sign().copy(__negative_sign, _M_negative_sign_size); 255132720Skan _M_negative_sign = __negative_sign; 256132720Skan 257132720Skan _M_pos_format = __mp.pos_format(); 258132720Skan _M_neg_format = __mp.neg_format(); 259132720Skan 260132720Skan const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__loc); 261132720Skan __ct.widen(money_base::_S_atoms, 262132720Skan money_base::_S_atoms + money_base::_S_end, _M_atoms); 263117397Skan } 26497403Sobrien 265132720Skan 266132720Skan // Used by both numeric and monetary facets. 267132720Skan // Check to make sure that the __grouping_tmp string constructed in 268132720Skan // money_get or num_get matches the canonical grouping for a given 269132720Skan // locale. 270132720Skan // __grouping_tmp is parsed L to R 271132720Skan // 1,222,444 == __grouping_tmp of "\1\3\3" 272132720Skan // __grouping is parsed R to L 273132720Skan // 1,222,444 == __grouping of "\3" == "\3\3\3" 274132720Skan static bool 275132720Skan __verify_grouping(const char* __grouping, size_t __grouping_size, 276132720Skan const string& __grouping_tmp); 277132720Skan 278169691Skan_GLIBCXX_BEGIN_LDBL_NAMESPACE 279169691Skan 28097403Sobrien template<typename _CharT, typename _InIter> 28197403Sobrien _InIter 28297403Sobrien num_get<_CharT, _InIter>:: 28397403Sobrien _M_extract_float(_InIter __beg, _InIter __end, ios_base& __io, 28497403Sobrien ios_base::iostate& __err, string& __xtrc) const 28597403Sobrien { 286132720Skan typedef char_traits<_CharT> __traits_type; 287169691Skan typedef __numpunct_cache<_CharT> __cache_type; 288132720Skan __use_cache<__cache_type> __uc; 289132720Skan const locale& __loc = __io._M_getloc(); 290132720Skan const __cache_type* __lc = __uc(__loc); 291132720Skan const _CharT* __lit = __lc->_M_atoms_in; 292169691Skan char_type __c = char_type(); 29397403Sobrien 294169691Skan // True if __beg becomes equal to __end. 295169691Skan bool __testeof = __beg == __end; 296132720Skan 29797403Sobrien // First check for sign. 298169691Skan if (!__testeof) 29997403Sobrien { 300169691Skan __c = *__beg; 301132720Skan const bool __plus = __c == __lit[__num_base::_S_iplus]; 302132720Skan if ((__plus || __c == __lit[__num_base::_S_iminus]) 303132720Skan && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) 304132720Skan && !(__c == __lc->_M_decimal_point)) 305132720Skan { 306132720Skan __xtrc += __plus ? '+' : '-'; 307169691Skan if (++__beg != __end) 308169691Skan __c = *__beg; 309169691Skan else 310169691Skan __testeof = true; 311132720Skan } 31297403Sobrien } 31397403Sobrien 314132720Skan // Next, look for leading zeros. 315169691Skan bool __found_mantissa = false; 316169691Skan int __sep_pos = 0; 317169691Skan while (!__testeof) 31897403Sobrien { 319132720Skan if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep 320132720Skan || __c == __lc->_M_decimal_point) 321132720Skan break; 322132720Skan else if (__c == __lit[__num_base::_S_izero]) 323132720Skan { 324132720Skan if (!__found_mantissa) 325132720Skan { 326132720Skan __xtrc += '0'; 327132720Skan __found_mantissa = true; 328132720Skan } 329169691Skan ++__sep_pos; 330169691Skan 331169691Skan if (++__beg != __end) 332169691Skan __c = *__beg; 333169691Skan else 334169691Skan __testeof = true; 335132720Skan } 336132720Skan else 337132720Skan break; 33897403Sobrien } 33997403Sobrien 34097403Sobrien // Only need acceptable digits for floating point numbers. 34197403Sobrien bool __found_dec = false; 34297403Sobrien bool __found_sci = false; 34397403Sobrien string __found_grouping; 344132720Skan if (__lc->_M_use_grouping) 345132720Skan __found_grouping.reserve(32); 346132720Skan const char_type* __lit_zero = __lit + __num_base::_S_izero; 34797403Sobrien 348169691Skan if (!__lc->_M_allocated) 349169691Skan // "C" locale 350169691Skan while (!__testeof) 351169691Skan { 352169691Skan const int __digit = _M_find(__lit_zero, 10, __c); 353169691Skan if (__digit != -1) 354169691Skan { 355169691Skan __xtrc += '0' + __digit; 356169691Skan __found_mantissa = true; 357169691Skan } 358169691Skan else if (__c == __lc->_M_decimal_point 359169691Skan && !__found_dec && !__found_sci) 360169691Skan { 361169691Skan __xtrc += '.'; 362169691Skan __found_dec = true; 363169691Skan } 364169691Skan else if ((__c == __lit[__num_base::_S_ie] 365169691Skan || __c == __lit[__num_base::_S_iE]) 366169691Skan && !__found_sci && __found_mantissa) 367169691Skan { 368169691Skan // Scientific notation. 369169691Skan __xtrc += 'e'; 370169691Skan __found_sci = true; 371169691Skan 372169691Skan // Remove optional plus or minus sign, if they exist. 373169691Skan if (++__beg != __end) 374169691Skan { 375169691Skan __c = *__beg; 376169691Skan const bool __plus = __c == __lit[__num_base::_S_iplus]; 377169691Skan if (__plus || __c == __lit[__num_base::_S_iminus]) 378132720Skan __xtrc += __plus ? '+' : '-'; 379169691Skan else 380169691Skan continue; 381169691Skan } 382169691Skan else 383169691Skan { 384169691Skan __testeof = true; 385169691Skan break; 386169691Skan } 387169691Skan } 388169691Skan else 389169691Skan break; 39097403Sobrien 391169691Skan if (++__beg != __end) 392169691Skan __c = *__beg; 393169691Skan else 394169691Skan __testeof = true; 395169691Skan } 396169691Skan else 397169691Skan while (!__testeof) 398169691Skan { 399169691Skan // According to 22.2.2.1.2, p8-9, first look for thousands_sep 400169691Skan // and decimal_point. 401169691Skan if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) 402169691Skan { 403169691Skan if (!__found_dec && !__found_sci) 404169691Skan { 405169691Skan // NB: Thousands separator at the beginning of a string 406169691Skan // is a no-no, as is two consecutive thousands separators. 407169691Skan if (__sep_pos) 408169691Skan { 409169691Skan __found_grouping += static_cast<char>(__sep_pos); 410169691Skan __sep_pos = 0; 411169691Skan } 412169691Skan else 413169691Skan { 414169691Skan // NB: __convert_to_v will not assign __v and will 415169691Skan // set the failbit. 416169691Skan __xtrc.clear(); 417169691Skan break; 418169691Skan } 419169691Skan } 420169691Skan else 421169691Skan break; 422169691Skan } 423169691Skan else if (__c == __lc->_M_decimal_point) 424169691Skan { 425169691Skan if (!__found_dec && !__found_sci) 426169691Skan { 427169691Skan // If no grouping chars are seen, no grouping check 428169691Skan // is applied. Therefore __found_grouping is adjusted 429169691Skan // only if decimal_point comes after some thousands_sep. 430169691Skan if (__found_grouping.size()) 431169691Skan __found_grouping += static_cast<char>(__sep_pos); 432169691Skan __xtrc += '.'; 433169691Skan __found_dec = true; 434169691Skan } 435169691Skan else 436169691Skan break; 437169691Skan } 438169691Skan else 439169691Skan { 440169691Skan const char_type* __q = 441169691Skan __traits_type::find(__lit_zero, 10, __c); 442169691Skan if (__q) 443169691Skan { 444169691Skan __xtrc += '0' + (__q - __lit_zero); 445169691Skan __found_mantissa = true; 446169691Skan ++__sep_pos; 447169691Skan } 448169691Skan else if ((__c == __lit[__num_base::_S_ie] 449169691Skan || __c == __lit[__num_base::_S_iE]) 450169691Skan && !__found_sci && __found_mantissa) 451169691Skan { 452169691Skan // Scientific notation. 453169691Skan if (__found_grouping.size() && !__found_dec) 454169691Skan __found_grouping += static_cast<char>(__sep_pos); 455169691Skan __xtrc += 'e'; 456169691Skan __found_sci = true; 457169691Skan 458169691Skan // Remove optional plus or minus sign, if they exist. 459169691Skan if (++__beg != __end) 460169691Skan { 461169691Skan __c = *__beg; 462169691Skan const bool __plus = __c == __lit[__num_base::_S_iplus]; 463169691Skan if ((__plus || __c == __lit[__num_base::_S_iminus]) 464169691Skan && !(__lc->_M_use_grouping 465169691Skan && __c == __lc->_M_thousands_sep) 466169691Skan && !(__c == __lc->_M_decimal_point)) 467169691Skan __xtrc += __plus ? '+' : '-'; 468169691Skan else 469169691Skan continue; 470169691Skan } 471169691Skan else 472169691Skan { 473169691Skan __testeof = true; 474169691Skan break; 475169691Skan } 476169691Skan } 477169691Skan else 478169691Skan break; 479169691Skan } 480169691Skan 481169691Skan if (++__beg != __end) 482169691Skan __c = *__beg; 483169691Skan else 484169691Skan __testeof = true; 485169691Skan } 486169691Skan 48797403Sobrien // Digit grouping is checked. If grouping and found_grouping don't 48897403Sobrien // match, then get very very upset, and set failbit. 489169691Skan if (__found_grouping.size()) 49097403Sobrien { 491132720Skan // Add the ending grouping if a decimal or 'e'/'E' wasn't found. 492132720Skan if (!__found_dec && !__found_sci) 49397403Sobrien __found_grouping += static_cast<char>(__sep_pos); 494132720Skan 495132720Skan if (!std::__verify_grouping(__lc->_M_grouping, 496132720Skan __lc->_M_grouping_size, 497132720Skan __found_grouping)) 49897403Sobrien __err |= ios_base::failbit; 49997403Sobrien } 50097403Sobrien 501132720Skan // Finish up. 502169691Skan if (__testeof) 50397403Sobrien __err |= ios_base::eofbit; 50497403Sobrien return __beg; 50597403Sobrien } 50697403Sobrien 507169691Skan_GLIBCXX_END_LDBL_NAMESPACE 508169691Skan 509169691Skan_GLIBCXX_BEGIN_LDBL_NAMESPACE 510169691Skan 51197403Sobrien template<typename _CharT, typename _InIter> 512132720Skan template<typename _ValueT> 513132720Skan _InIter 514132720Skan num_get<_CharT, _InIter>:: 515132720Skan _M_extract_int(_InIter __beg, _InIter __end, ios_base& __io, 516132720Skan ios_base::iostate& __err, _ValueT& __v) const 517132720Skan { 518169691Skan typedef char_traits<_CharT> __traits_type; 519169691Skan using __gnu_cxx::__add_unsigned; 520169691Skan typedef typename __add_unsigned<_ValueT>::__type __unsigned_type; 521169691Skan typedef __numpunct_cache<_CharT> __cache_type; 522132720Skan __use_cache<__cache_type> __uc; 523132720Skan const locale& __loc = __io._M_getloc(); 524132720Skan const __cache_type* __lc = __uc(__loc); 525132720Skan const _CharT* __lit = __lc->_M_atoms_in; 526169691Skan char_type __c = char_type(); 52797403Sobrien 528132720Skan // NB: Iff __basefield == 0, __base can change based on contents. 529132720Skan const ios_base::fmtflags __basefield = __io.flags() 530132720Skan & ios_base::basefield; 531132720Skan const bool __oct = __basefield == ios_base::oct; 532132720Skan int __base = __oct ? 8 : (__basefield == ios_base::hex ? 16 : 10); 533102782Skan 534169691Skan // True if __beg becomes equal to __end. 535169691Skan bool __testeof = __beg == __end; 53697403Sobrien 537132720Skan // First check for sign. 538132720Skan bool __negative = false; 539169691Skan if (!__testeof) 540132720Skan { 541169691Skan __c = *__beg; 542132720Skan if (numeric_limits<_ValueT>::is_signed) 543132720Skan __negative = __c == __lit[__num_base::_S_iminus]; 544132720Skan if ((__negative || __c == __lit[__num_base::_S_iplus]) 545132720Skan && !(__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) 546132720Skan && !(__c == __lc->_M_decimal_point)) 547169691Skan { 548169691Skan if (++__beg != __end) 549169691Skan __c = *__beg; 550169691Skan else 551169691Skan __testeof = true; 552169691Skan } 553132720Skan } 554132720Skan 555132720Skan // Next, look for leading zeros and check required digits 556132720Skan // for base formats. 557169691Skan bool __found_zero = false; 558169691Skan int __sep_pos = 0; 559169691Skan while (!__testeof) 560132720Skan { 561132720Skan if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep 562132720Skan || __c == __lc->_M_decimal_point) 563132720Skan break; 564132720Skan else if (__c == __lit[__num_base::_S_izero] 565169691Skan && (!__found_zero || __base == 10)) 566132720Skan { 567169691Skan __found_zero = true; 568169691Skan ++__sep_pos; 569169691Skan if (__basefield == 0) 570169691Skan __base = 8; 571169691Skan if (__base == 8) 572169691Skan __sep_pos = 0; 573132720Skan } 574169691Skan else if (__found_zero 575169691Skan && (__c == __lit[__num_base::_S_ix] 576169691Skan || __c == __lit[__num_base::_S_iX])) 577132720Skan { 578169691Skan if (__basefield == 0) 579169691Skan __base = 16; 580169691Skan if (__base == 16) 581132720Skan { 582169691Skan __found_zero = false; 583169691Skan __sep_pos = 0; 584132720Skan } 585169691Skan else 586169691Skan break; 587132720Skan } 588132720Skan else 589132720Skan break; 590169691Skan 591169691Skan if (++__beg != __end) 592169691Skan { 593169691Skan __c = *__beg; 594169691Skan if (!__found_zero) 595169691Skan break; 596169691Skan } 597169691Skan else 598169691Skan __testeof = true; 599132720Skan } 600169691Skan 601132720Skan // At this point, base is determined. If not hex, only allow 602132720Skan // base digits as valid input. 603169691Skan const size_t __len = (__base == 16 ? __num_base::_S_iend 604169691Skan - __num_base::_S_izero : __base); 60597403Sobrien 606132720Skan // Extract. 607132720Skan string __found_grouping; 608132720Skan if (__lc->_M_use_grouping) 609132720Skan __found_grouping.reserve(32); 610169691Skan bool __testfail = false; 611169691Skan const __unsigned_type __max = __negative ? 612169691Skan -numeric_limits<_ValueT>::min() : numeric_limits<_ValueT>::max(); 613169691Skan const __unsigned_type __smax = __max / __base; 614169691Skan __unsigned_type __result = 0; 615169691Skan int __digit = 0; 616132720Skan const char_type* __lit_zero = __lit + __num_base::_S_izero; 617169691Skan 618169691Skan if (!__lc->_M_allocated) 619169691Skan // "C" locale 620169691Skan while (!__testeof) 621169691Skan { 622169691Skan __digit = _M_find(__lit_zero, __len, __c); 623169691Skan if (__digit == -1) 624169691Skan break; 625169691Skan 626169691Skan if (__result > __smax) 627169691Skan __testfail = true; 628169691Skan else 629169691Skan { 630169691Skan __result *= __base; 631169691Skan __testfail |= __result > __max - __digit; 632169691Skan __result += __digit; 633169691Skan ++__sep_pos; 634169691Skan } 635169691Skan 636169691Skan if (++__beg != __end) 637169691Skan __c = *__beg; 638169691Skan else 639169691Skan __testeof = true; 640169691Skan } 641132720Skan else 642169691Skan while (!__testeof) 643169691Skan { 644169691Skan // According to 22.2.2.1.2, p8-9, first look for thousands_sep 645169691Skan // and decimal_point. 646169691Skan if (__lc->_M_use_grouping && __c == __lc->_M_thousands_sep) 647169691Skan { 648169691Skan // NB: Thousands separator at the beginning of a string 649169691Skan // is a no-no, as is two consecutive thousands separators. 650169691Skan if (__sep_pos) 651169691Skan { 652169691Skan __found_grouping += static_cast<char>(__sep_pos); 653169691Skan __sep_pos = 0; 654169691Skan } 655169691Skan else 656169691Skan { 657169691Skan __testfail = true; 658169691Skan break; 659169691Skan } 660169691Skan } 661169691Skan else if (__c == __lc->_M_decimal_point) 662169691Skan break; 663169691Skan else 664169691Skan { 665169691Skan const char_type* __q = 666169691Skan __traits_type::find(__lit_zero, __len, __c); 667169691Skan if (!__q) 668169691Skan break; 669169691Skan 670169691Skan __digit = __q - __lit_zero; 671169691Skan if (__digit > 15) 672169691Skan __digit -= 6; 673169691Skan if (__result > __smax) 674169691Skan __testfail = true; 675169691Skan else 676169691Skan { 677169691Skan __result *= __base; 678169691Skan __testfail |= __result > __max - __digit; 679169691Skan __result += __digit; 680169691Skan ++__sep_pos; 681169691Skan } 682169691Skan } 683169691Skan 684169691Skan if (++__beg != __end) 685169691Skan __c = *__beg; 686169691Skan else 687169691Skan __testeof = true; 688169691Skan } 689169691Skan 690132720Skan // Digit grouping is checked. If grouping and found_grouping don't 691132720Skan // match, then get very very upset, and set failbit. 692169691Skan if (__found_grouping.size()) 693132720Skan { 694132720Skan // Add the ending grouping. 695132720Skan __found_grouping += static_cast<char>(__sep_pos); 69697403Sobrien 697132720Skan if (!std::__verify_grouping(__lc->_M_grouping, 698132720Skan __lc->_M_grouping_size, 699132720Skan __found_grouping)) 700132720Skan __err |= ios_base::failbit; 701132720Skan } 70297403Sobrien 703169691Skan if (!__testfail && (__sep_pos || __found_zero 704169691Skan || __found_grouping.size())) 705169691Skan __v = __negative ? -__result : __result; 706132720Skan else 707132720Skan __err |= ios_base::failbit; 708132720Skan 709169691Skan if (__testeof) 710132720Skan __err |= ios_base::eofbit; 711132720Skan return __beg; 712132720Skan } 713132720Skan 714132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 715132720Skan // 17. Bad bool parsing 71697403Sobrien template<typename _CharT, typename _InIter> 71797403Sobrien _InIter 71897403Sobrien num_get<_CharT, _InIter>:: 71997403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 72097403Sobrien ios_base::iostate& __err, bool& __v) const 72197403Sobrien { 72297403Sobrien if (!(__io.flags() & ios_base::boolalpha)) 72397403Sobrien { 724132720Skan // Parse bool values as long. 72597403Sobrien // NB: We can't just call do_get(long) here, as it might 72697403Sobrien // refer to a derived class. 727132720Skan long __l = -1; 728132720Skan __beg = _M_extract_int(__beg, __end, __io, __err, __l); 729132720Skan if (__l == 0 || __l == 1) 730132720Skan __v = __l; 731132720Skan else 73297403Sobrien __err |= ios_base::failbit; 73397403Sobrien } 73497403Sobrien else 73597403Sobrien { 736132720Skan // Parse bool values as alphanumeric. 737169691Skan typedef __numpunct_cache<_CharT> __cache_type; 738132720Skan __use_cache<__cache_type> __uc; 739132720Skan const locale& __loc = __io._M_getloc(); 740132720Skan const __cache_type* __lc = __uc(__loc); 741102782Skan 742132720Skan bool __testf = true; 743132720Skan bool __testt = true; 744132720Skan size_t __n; 745169691Skan bool __testeof = __beg == __end; 746169691Skan for (__n = 0; !__testeof; ++__n) 747132720Skan { 748169691Skan const char_type __c = *__beg; 749169691Skan 750132720Skan if (__testf) 751132720Skan if (__n < __lc->_M_falsename_size) 752169691Skan __testf = __c == __lc->_M_falsename[__n]; 753132720Skan else 754132720Skan break; 75597403Sobrien 756132720Skan if (__testt) 757132720Skan if (__n < __lc->_M_truename_size) 758169691Skan __testt = __c == __lc->_M_truename[__n]; 759132720Skan else 760132720Skan break; 761132720Skan 762132720Skan if (!__testf && !__testt) 763132720Skan break; 764169691Skan 765169691Skan if (++__beg == __end) 766169691Skan __testeof = true; 76797403Sobrien } 768132720Skan if (__testf && __n == __lc->_M_falsename_size) 769132720Skan __v = 0; 770132720Skan else if (__testt && __n == __lc->_M_truename_size) 771132720Skan __v = 1; 772132720Skan else 773132720Skan __err |= ios_base::failbit; 774132720Skan 775169691Skan if (__testeof) 77697403Sobrien __err |= ios_base::eofbit; 77797403Sobrien } 77897403Sobrien return __beg; 77997403Sobrien } 78097403Sobrien 78197403Sobrien template<typename _CharT, typename _InIter> 78297403Sobrien _InIter 78397403Sobrien num_get<_CharT, _InIter>:: 78497403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 78597403Sobrien ios_base::iostate& __err, long& __v) const 786132720Skan { return _M_extract_int(__beg, __end, __io, __err, __v); } 78797403Sobrien 78897403Sobrien template<typename _CharT, typename _InIter> 78997403Sobrien _InIter 79097403Sobrien num_get<_CharT, _InIter>:: 79197403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 79297403Sobrien ios_base::iostate& __err, unsigned short& __v) const 793132720Skan { return _M_extract_int(__beg, __end, __io, __err, __v); } 79497403Sobrien 79597403Sobrien template<typename _CharT, typename _InIter> 79697403Sobrien _InIter 79797403Sobrien num_get<_CharT, _InIter>:: 79897403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 79997403Sobrien ios_base::iostate& __err, unsigned int& __v) const 800132720Skan { return _M_extract_int(__beg, __end, __io, __err, __v); } 80197403Sobrien 80297403Sobrien template<typename _CharT, typename _InIter> 80397403Sobrien _InIter 80497403Sobrien num_get<_CharT, _InIter>:: 80597403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 80697403Sobrien ios_base::iostate& __err, unsigned long& __v) const 807132720Skan { return _M_extract_int(__beg, __end, __io, __err, __v); } 80897403Sobrien 809132720Skan#ifdef _GLIBCXX_USE_LONG_LONG 81097403Sobrien template<typename _CharT, typename _InIter> 81197403Sobrien _InIter 81297403Sobrien num_get<_CharT, _InIter>:: 81397403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 81497403Sobrien ios_base::iostate& __err, long long& __v) const 815132720Skan { return _M_extract_int(__beg, __end, __io, __err, __v); } 81697403Sobrien 81797403Sobrien template<typename _CharT, typename _InIter> 81897403Sobrien _InIter 81997403Sobrien num_get<_CharT, _InIter>:: 82097403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 82197403Sobrien ios_base::iostate& __err, unsigned long long& __v) const 822132720Skan { return _M_extract_int(__beg, __end, __io, __err, __v); } 82397403Sobrien#endif 82497403Sobrien 82597403Sobrien template<typename _CharT, typename _InIter> 82697403Sobrien _InIter 82797403Sobrien num_get<_CharT, _InIter>:: 828132720Skan do_get(iter_type __beg, iter_type __end, ios_base& __io, 82997403Sobrien ios_base::iostate& __err, float& __v) const 83097403Sobrien { 83197403Sobrien string __xtrc; 83297403Sobrien __xtrc.reserve(32); 83397403Sobrien __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); 834132720Skan std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); 83597403Sobrien return __beg; 83697403Sobrien } 83797403Sobrien 83897403Sobrien template<typename _CharT, typename _InIter> 83997403Sobrien _InIter 84097403Sobrien num_get<_CharT, _InIter>:: 84197403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 84297403Sobrien ios_base::iostate& __err, double& __v) const 84397403Sobrien { 84497403Sobrien string __xtrc; 84597403Sobrien __xtrc.reserve(32); 84697403Sobrien __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); 847132720Skan std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); 84897403Sobrien return __beg; 84997403Sobrien } 85097403Sobrien 851169691Skan#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 85297403Sobrien template<typename _CharT, typename _InIter> 85397403Sobrien _InIter 85497403Sobrien num_get<_CharT, _InIter>:: 855169691Skan __do_get(iter_type __beg, iter_type __end, ios_base& __io, 856169691Skan ios_base::iostate& __err, double& __v) const 857169691Skan { 858169691Skan string __xtrc; 859169691Skan __xtrc.reserve(32); 860169691Skan __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); 861169691Skan std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); 862169691Skan return __beg; 863169691Skan } 864169691Skan#endif 865169691Skan 866169691Skan template<typename _CharT, typename _InIter> 867169691Skan _InIter 868169691Skan num_get<_CharT, _InIter>:: 86997403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 87097403Sobrien ios_base::iostate& __err, long double& __v) const 87197403Sobrien { 87297403Sobrien string __xtrc; 87397403Sobrien __xtrc.reserve(32); 87497403Sobrien __beg = _M_extract_float(__beg, __end, __io, __err, __xtrc); 875132720Skan std::__convert_to_v(__xtrc.c_str(), __v, __err, _S_get_c_locale()); 87697403Sobrien return __beg; 87797403Sobrien } 87897403Sobrien 87997403Sobrien template<typename _CharT, typename _InIter> 88097403Sobrien _InIter 88197403Sobrien num_get<_CharT, _InIter>:: 88297403Sobrien do_get(iter_type __beg, iter_type __end, ios_base& __io, 88397403Sobrien ios_base::iostate& __err, void*& __v) const 88497403Sobrien { 885132720Skan // Prepare for hex formatted input. 88697403Sobrien typedef ios_base::fmtflags fmtflags; 887132720Skan const fmtflags __fmt = __io.flags(); 888132720Skan __io.flags(__fmt & ~ios_base::basefield | ios_base::hex); 88997403Sobrien 890132720Skan unsigned long __ul; 891132720Skan __beg = _M_extract_int(__beg, __end, __io, __err, __ul); 89297403Sobrien 893132720Skan // Reset from hex formatted input. 89497403Sobrien __io.flags(__fmt); 89597403Sobrien 89697403Sobrien if (!(__err & ios_base::failbit)) 89797403Sobrien __v = reinterpret_cast<void*>(__ul); 89897403Sobrien return __beg; 89997403Sobrien } 90097403Sobrien 901117397Skan // For use by integer and floating-point types after they have been 902117397Skan // converted into a char_type string. 903117397Skan template<typename _CharT, typename _OutIter> 904117397Skan void 905117397Skan num_put<_CharT, _OutIter>:: 906132720Skan _M_pad(_CharT __fill, streamsize __w, ios_base& __io, 907117397Skan _CharT* __new, const _CharT* __cs, int& __len) const 908117397Skan { 909117397Skan // [22.2.2.2.2] Stage 3. 910117397Skan // If necessary, pad. 911132720Skan __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs, 912117397Skan __w, __len, true); 913117397Skan __len = static_cast<int>(__w); 914117397Skan } 915117397Skan 916169691Skan_GLIBCXX_END_LDBL_NAMESPACE 917117397Skan 918117397Skan template<typename _CharT, typename _ValueT> 919117397Skan int 920132720Skan __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit, 921169691Skan ios_base::fmtflags __flags, bool __dec) 922117397Skan { 923169691Skan _CharT* __buf = __bufend; 924169691Skan if (__builtin_expect(__dec, true)) 925117397Skan { 926117397Skan // Decimal. 927132720Skan do 928117397Skan { 929169691Skan *--__buf = __lit[(__v % 10) + __num_base::_S_odigits]; 930117397Skan __v /= 10; 931132720Skan } 932117397Skan while (__v != 0); 933117397Skan } 934169691Skan else if ((__flags & ios_base::basefield) == ios_base::oct) 935117397Skan { 936117397Skan // Octal. 937132720Skan do 938117397Skan { 939169691Skan *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits]; 940117397Skan __v >>= 3; 941132720Skan } 942117397Skan while (__v != 0); 943117397Skan } 944117397Skan else 945117397Skan { 946117397Skan // Hex. 947117397Skan const bool __uppercase = __flags & ios_base::uppercase; 948132720Skan const int __case_offset = __uppercase ? __num_base::_S_oudigits 949132720Skan : __num_base::_S_odigits; 950132720Skan do 951117397Skan { 952169691Skan *--__buf = __lit[(__v & 0xf) + __case_offset]; 953117397Skan __v >>= 4; 954132720Skan } 955117397Skan while (__v != 0); 956117397Skan } 957169691Skan return __bufend - __buf; 958117397Skan } 959117397Skan 960169691Skan_GLIBCXX_BEGIN_LDBL_NAMESPACE 961169691Skan 962117397Skan template<typename _CharT, typename _OutIter> 963117397Skan void 964117397Skan num_put<_CharT, _OutIter>:: 965132720Skan _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep, 966169691Skan ios_base&, _CharT* __new, _CharT* __cs, int& __len) const 967117397Skan { 968169691Skan _CharT* __p = std::__add_grouping(__new, __sep, __grouping, 969169691Skan __grouping_size, __cs, __cs + __len); 970117397Skan __len = __p - __new; 971117397Skan } 972169691Skan 973117397Skan template<typename _CharT, typename _OutIter> 974117397Skan template<typename _ValueT> 975117397Skan _OutIter 976117397Skan num_put<_CharT, _OutIter>:: 977132720Skan _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill, 978132720Skan _ValueT __v) const 979117397Skan { 980169691Skan using __gnu_cxx::__add_unsigned; 981169691Skan typedef typename __add_unsigned<_ValueT>::__type __unsigned_type; 982169691Skan typedef __numpunct_cache<_CharT> __cache_type; 983132720Skan __use_cache<__cache_type> __uc; 984132720Skan const locale& __loc = __io._M_getloc(); 985132720Skan const __cache_type* __lc = __uc(__loc); 986132720Skan const _CharT* __lit = __lc->_M_atoms_out; 987169691Skan const ios_base::fmtflags __flags = __io.flags(); 988117397Skan 989117397Skan // Long enough to hold hex, dec, and octal representations. 990169691Skan const int __ilen = 5 * sizeof(_ValueT); 991132720Skan _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 992117397Skan * __ilen)); 993132720Skan 994117397Skan // [22.2.2.2.2] Stage 1, numeric conversion to character. 995117397Skan // Result is returned right-justified in the buffer. 996169691Skan const ios_base::fmtflags __basefield = __flags & ios_base::basefield; 997169691Skan const bool __dec = (__basefield != ios_base::oct 998169691Skan && __basefield != ios_base::hex); 999169691Skan const __unsigned_type __u = (__v > 0 || !__dec) ? __v : -__v; 1000169691Skan int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec); 1001132720Skan __cs += __ilen - __len; 1002132720Skan 1003132720Skan // Add grouping, if necessary. 1004132720Skan if (__lc->_M_use_grouping) 1005117397Skan { 1006169691Skan // Grouping can add (almost) as many separators as the number 1007169691Skan // of digits + space is reserved for numeric base or sign. 1008132720Skan _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1009169691Skan * (__len + 1) 1010169691Skan * 2)); 1011132720Skan _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size, 1012169691Skan __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len); 1013169691Skan __cs = __cs2 + 2; 1014117397Skan } 1015132720Skan 1016169691Skan // Complete Stage 1, prepend numeric base or sign. 1017169691Skan if (__builtin_expect(__dec, true)) 1018169691Skan { 1019169691Skan // Decimal. 1020228780Spfg if (__v >= 0) 1021169691Skan { 1022169691Skan if (__flags & ios_base::showpos 1023169691Skan && numeric_limits<_ValueT>::is_signed) 1024169691Skan *--__cs = __lit[__num_base::_S_oplus], ++__len; 1025169691Skan } 1026228780Spfg else 1027169691Skan *--__cs = __lit[__num_base::_S_ominus], ++__len; 1028169691Skan } 1029169691Skan else if (__flags & ios_base::showbase && __v) 1030169691Skan { 1031169691Skan if (__basefield == ios_base::oct) 1032169691Skan *--__cs = __lit[__num_base::_S_odigits], ++__len; 1033169691Skan else 1034169691Skan { 1035169691Skan // 'x' or 'X' 1036169691Skan const bool __uppercase = __flags & ios_base::uppercase; 1037169691Skan *--__cs = __lit[__num_base::_S_ox + __uppercase]; 1038169691Skan // '0' 1039169691Skan *--__cs = __lit[__num_base::_S_odigits]; 1040169691Skan __len += 2; 1041169691Skan } 1042169691Skan } 1043169691Skan 1044117397Skan // Pad. 1045132720Skan const streamsize __w = __io.width(); 1046117397Skan if (__w > static_cast<streamsize>(__len)) 1047117397Skan { 1048132720Skan _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1049132720Skan * __w)); 1050117397Skan _M_pad(__fill, __w, __io, __cs3, __cs, __len); 1051117397Skan __cs = __cs3; 1052117397Skan } 1053117397Skan __io.width(0); 1054117397Skan 1055117397Skan // [22.2.2.2.2] Stage 4. 1056117397Skan // Write resulting, fully-formatted string to output iterator. 1057132720Skan return std::__write(__s, __cs, __len); 1058132720Skan } 1059117397Skan 1060117397Skan template<typename _CharT, typename _OutIter> 1061117397Skan void 1062117397Skan num_put<_CharT, _OutIter>:: 1063132720Skan _M_group_float(const char* __grouping, size_t __grouping_size, 1064132720Skan _CharT __sep, const _CharT* __p, _CharT* __new, 1065132720Skan _CharT* __cs, int& __len) const 1066117397Skan { 1067132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 1068132720Skan // 282. What types does numpunct grouping refer to? 1069132720Skan // Add grouping, if necessary. 1070132720Skan const int __declen = __p ? __p - __cs : __len; 1071169691Skan _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping, 1072169691Skan __grouping_size, 1073169691Skan __cs, __cs + __declen); 1074132720Skan 1075117397Skan // Tack on decimal part. 1076117397Skan int __newlen = __p2 - __new; 1077117397Skan if (__p) 1078117397Skan { 1079117397Skan char_traits<_CharT>::copy(__p2, __p, __len - __declen); 1080117397Skan __newlen += __len - __declen; 1081132720Skan } 1082117397Skan __len = __newlen; 1083117397Skan } 1084117397Skan 1085169691Skan // The following code uses vsnprintf (or vsprintf(), when 1086132720Skan // _GLIBCXX_USE_C99 is not defined) to convert floating point values 1087117397Skan // for insertion into a stream. An optimization would be to replace 1088117397Skan // them with code that works directly on a wide buffer and then use 1089117397Skan // __pad to do the padding. It would be good to replace them anyway 1090117397Skan // to gain back the efficiency that C++ provides by knowing up front 1091117397Skan // the type of the values to insert. Also, sprintf is dangerous 1092117397Skan // since may lead to accidental buffer overruns. This 1093117397Skan // implementation follows the C++ standard fairly directly as 109497403Sobrien // outlined in 22.2.2.2 [lib.locale.num.put] 109597403Sobrien template<typename _CharT, typename _OutIter> 109697403Sobrien template<typename _ValueT> 109797403Sobrien _OutIter 109897403Sobrien num_put<_CharT, _OutIter>:: 1099132720Skan _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod, 110097403Sobrien _ValueT __v) const 110197403Sobrien { 1102169691Skan typedef __numpunct_cache<_CharT> __cache_type; 1103132720Skan __use_cache<__cache_type> __uc; 1104132720Skan const locale& __loc = __io._M_getloc(); 1105132720Skan const __cache_type* __lc = __uc(__loc); 1106117397Skan 1107117397Skan // Use default precision if out of range. 1108169691Skan const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision(); 110997403Sobrien 1110132720Skan const int __max_digits = numeric_limits<_ValueT>::digits10; 1111117397Skan 1112117397Skan // [22.2.2.2.2] Stage 1, numeric conversion to character. 1113117397Skan int __len; 111497403Sobrien // Long enough for the max format spec. 111597403Sobrien char __fbuf[16]; 1116169691Skan __num_base::_S_format_float(__io, __fbuf, __mod); 111797403Sobrien 1118132720Skan#ifdef _GLIBCXX_USE_C99 1119132720Skan // First try a buffer perhaps big enough (most probably sufficient 1120117397Skan // for non-ios_base::fixed outputs) 112197403Sobrien int __cs_size = __max_digits * 3; 112297403Sobrien char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1123169691Skan __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, 1124169691Skan __fbuf, __prec, __v); 112597403Sobrien 112697403Sobrien // If the buffer was not large enough, try again with the correct size. 112797403Sobrien if (__len >= __cs_size) 112897403Sobrien { 1129132720Skan __cs_size = __len + 1; 113097403Sobrien __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1131169691Skan __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, 1132169691Skan __fbuf, __prec, __v); 113397403Sobrien } 113497403Sobrien#else 113597403Sobrien // Consider the possibility of long ios_base::fixed outputs 113697403Sobrien const bool __fixed = __io.flags() & ios_base::fixed; 113797403Sobrien const int __max_exp = numeric_limits<_ValueT>::max_exponent10; 1138117397Skan 1139132720Skan // The size of the output string is computed as follows. 1140132720Skan // ios_base::fixed outputs may need up to __max_exp + 1 chars 1141132720Skan // for the integer part + __prec chars for the fractional part 1142132720Skan // + 3 chars for sign, decimal point, '\0'. On the other hand, 1143132720Skan // for non-fixed outputs __max_digits * 2 + __prec chars are 1144132720Skan // largely sufficient. 1145132720Skan const int __cs_size = __fixed ? __max_exp + __prec + 4 1146132720Skan : __max_digits * 2 + __prec; 114797403Sobrien char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1148169691Skan __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf, 1149169691Skan __prec, __v); 115097403Sobrien#endif 115197403Sobrien 1152169691Skan // [22.2.2.2.2] Stage 2, convert to char_type, using correct 1153169691Skan // numpunct.decimal_point() values for '.' and adding grouping. 1154169691Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 1155169691Skan 1156169691Skan _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1157169691Skan * __len)); 1158169691Skan __ctype.widen(__cs, __cs + __len, __ws); 1159169691Skan 1160169691Skan // Replace decimal point. 1161169691Skan _CharT* __wp = 0; 1162169691Skan const char* __p = char_traits<char>::find(__cs, __len, '.'); 1163169691Skan if (__p) 1164169691Skan { 1165169691Skan __wp = __ws + (__p - __cs); 1166169691Skan *__wp = __lc->_M_decimal_point; 1167169691Skan } 1168169691Skan 1169169691Skan // Add grouping, if necessary. 1170169691Skan // N.B. Make sure to not group things like 2e20, i.e., no decimal 1171169691Skan // point, scientific notation. 1172169691Skan if (__lc->_M_use_grouping 1173169691Skan && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9' 1174169691Skan && __cs[1] >= '0' && __cs[2] >= '0'))) 1175169691Skan { 1176169691Skan // Grouping can add (almost) as many separators as the 1177169691Skan // number of digits, but no more. 1178169691Skan _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1179169691Skan * __len * 2)); 1180169691Skan 1181169691Skan streamsize __off = 0; 1182169691Skan if (__cs[0] == '-' || __cs[0] == '+') 1183169691Skan { 1184169691Skan __off = 1; 1185169691Skan __ws2[0] = __ws[0]; 1186169691Skan __len -= 1; 1187169691Skan } 1188169691Skan 1189169691Skan _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size, 1190169691Skan __lc->_M_thousands_sep, __wp, __ws2 + __off, 1191169691Skan __ws + __off, __len); 1192169691Skan __len += __off; 1193169691Skan 1194169691Skan __ws = __ws2; 1195169691Skan } 1196117397Skan 1197169691Skan // Pad. 1198169691Skan const streamsize __w = __io.width(); 1199169691Skan if (__w > static_cast<streamsize>(__len)) 1200169691Skan { 1201169691Skan _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1202169691Skan * __w)); 1203169691Skan _M_pad(__fill, __w, __io, __ws3, __ws, __len); 1204169691Skan __ws = __ws3; 1205169691Skan } 1206169691Skan __io.width(0); 1207169691Skan 1208169691Skan // [22.2.2.2.2] Stage 4. 1209169691Skan // Write resulting, fully-formatted string to output iterator. 1210169691Skan return std::__write(__s, __ws, __len); 1211117397Skan } 1212169691Skan 121397403Sobrien template<typename _CharT, typename _OutIter> 121497403Sobrien _OutIter 121597403Sobrien num_put<_CharT, _OutIter>:: 121697403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const 121797403Sobrien { 1218132720Skan const ios_base::fmtflags __flags = __io.flags(); 121997403Sobrien if ((__flags & ios_base::boolalpha) == 0) 122097403Sobrien { 1221146897Skan const long __l = __v; 1222146897Skan __s = _M_insert_int(__s, __io, __fill, __l); 122397403Sobrien } 122497403Sobrien else 122597403Sobrien { 1226169691Skan typedef __numpunct_cache<_CharT> __cache_type; 1227132720Skan __use_cache<__cache_type> __uc; 1228132720Skan const locale& __loc = __io._M_getloc(); 1229132720Skan const __cache_type* __lc = __uc(__loc); 1230117397Skan 1231132720Skan const _CharT* __name = __v ? __lc->_M_truename 1232132720Skan : __lc->_M_falsename; 1233132720Skan int __len = __v ? __lc->_M_truename_size 1234132720Skan : __lc->_M_falsename_size; 1235117397Skan 1236132720Skan const streamsize __w = __io.width(); 1237117397Skan if (__w > static_cast<streamsize>(__len)) 1238117397Skan { 1239132720Skan _CharT* __cs 1240132720Skan = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1241132720Skan * __w)); 1242132720Skan _M_pad(__fill, __w, __io, __cs, __name, __len); 1243132720Skan __name = __cs; 1244117397Skan } 1245117397Skan __io.width(0); 1246132720Skan __s = std::__write(__s, __name, __len); 124797403Sobrien } 124897403Sobrien return __s; 124997403Sobrien } 125097403Sobrien 125197403Sobrien template<typename _CharT, typename _OutIter> 125297403Sobrien _OutIter 125397403Sobrien num_put<_CharT, _OutIter>:: 125497403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const 1255132720Skan { return _M_insert_int(__s, __io, __fill, __v); } 125697403Sobrien 125797403Sobrien template<typename _CharT, typename _OutIter> 125897403Sobrien _OutIter 125997403Sobrien num_put<_CharT, _OutIter>:: 126097403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, 126197403Sobrien unsigned long __v) const 1262132720Skan { return _M_insert_int(__s, __io, __fill, __v); } 126397403Sobrien 1264132720Skan#ifdef _GLIBCXX_USE_LONG_LONG 126597403Sobrien template<typename _CharT, typename _OutIter> 126697403Sobrien _OutIter 126797403Sobrien num_put<_CharT, _OutIter>:: 1268169691Skan do_put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const 1269169691Skan { return _M_insert_int(__s, __io, __fill, __v); } 127097403Sobrien 127197403Sobrien template<typename _CharT, typename _OutIter> 127297403Sobrien _OutIter 127397403Sobrien num_put<_CharT, _OutIter>:: 127497403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, 127597403Sobrien unsigned long long __v) const 1276132720Skan { return _M_insert_int(__s, __io, __fill, __v); } 127797403Sobrien#endif 127897403Sobrien 127997403Sobrien template<typename _CharT, typename _OutIter> 128097403Sobrien _OutIter 128197403Sobrien num_put<_CharT, _OutIter>:: 128297403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const 1283132720Skan { return _M_insert_float(__s, __io, __fill, char(), __v); } 128497403Sobrien 1285169691Skan#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 128697403Sobrien template<typename _CharT, typename _OutIter> 128797403Sobrien _OutIter 128897403Sobrien num_put<_CharT, _OutIter>:: 1289169691Skan __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const 1290169691Skan { return _M_insert_float(__s, __io, __fill, char(), __v); } 1291169691Skan#endif 1292169691Skan 1293169691Skan template<typename _CharT, typename _OutIter> 1294169691Skan _OutIter 1295169691Skan num_put<_CharT, _OutIter>:: 1296132720Skan do_put(iter_type __s, ios_base& __io, char_type __fill, 129797403Sobrien long double __v) const 1298132720Skan { return _M_insert_float(__s, __io, __fill, 'L', __v); } 129997403Sobrien 130097403Sobrien template<typename _CharT, typename _OutIter> 130197403Sobrien _OutIter 130297403Sobrien num_put<_CharT, _OutIter>:: 130397403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, 130497403Sobrien const void* __v) const 130597403Sobrien { 1306132720Skan const ios_base::fmtflags __flags = __io.flags(); 1307146897Skan const ios_base::fmtflags __fmt = ~(ios_base::basefield 1308132720Skan | ios_base::uppercase 1309132720Skan | ios_base::internal); 131097403Sobrien __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase)); 1311132720Skan 1312132720Skan __s = _M_insert_int(__s, __io, __fill, 1313132720Skan reinterpret_cast<unsigned long>(__v)); 1314132720Skan __io.flags(__flags); 131597403Sobrien return __s; 131697403Sobrien } 131797403Sobrien 131897403Sobrien template<typename _CharT, typename _InIter> 1319132720Skan template<bool _Intl> 1320132720Skan _InIter 1321132720Skan money_get<_CharT, _InIter>:: 1322132720Skan _M_extract(iter_type __beg, iter_type __end, ios_base& __io, 1323132720Skan ios_base::iostate& __err, string& __units) const 1324132720Skan { 1325132720Skan typedef char_traits<_CharT> __traits_type; 1326132720Skan typedef typename string_type::size_type size_type; 1327132720Skan typedef money_base::part part; 1328169691Skan typedef __moneypunct_cache<_CharT, _Intl> __cache_type; 1329132720Skan 1330132720Skan const locale& __loc = __io._M_getloc(); 1331132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 133297403Sobrien 1333132720Skan __use_cache<__cache_type> __uc; 1334132720Skan const __cache_type* __lc = __uc(__loc); 1335132720Skan const char_type* __lit = __lc->_M_atoms; 133697403Sobrien 1337132720Skan // Deduced sign. 1338132720Skan bool __negative = false; 1339132720Skan // Sign size. 1340132720Skan size_type __sign_size = 0; 1341132720Skan // True if sign is mandatory. 1342132720Skan const bool __mandatory_sign = (__lc->_M_positive_sign_size 1343132720Skan && __lc->_M_negative_sign_size); 1344132720Skan // String of grouping info from thousands_sep plucked from __units. 1345132720Skan string __grouping_tmp; 1346132720Skan if (__lc->_M_use_grouping) 1347132720Skan __grouping_tmp.reserve(32); 1348132720Skan // Last position before the decimal point. 1349132720Skan int __last_pos = 0; 1350132720Skan // Separator positions, then, possibly, fractional digits. 1351132720Skan int __n = 0; 1352132720Skan // If input iterator is in a valid state. 1353132720Skan bool __testvalid = true; 1354132720Skan // Flag marking when a decimal point is found. 1355132720Skan bool __testdecfound = false; 135697403Sobrien 1357132720Skan // The tentative returned string is stored here. 1358132720Skan string __res; 1359132720Skan __res.reserve(32); 136097403Sobrien 1361132720Skan const char_type* __lit_zero = __lit + money_base::_S_zero; 1362169691Skan const money_base::pattern __p = __lc->_M_neg_format; 1363132720Skan for (int __i = 0; __i < 4 && __testvalid; ++__i) 1364132720Skan { 1365132720Skan const part __which = static_cast<part>(__p.field[__i]); 1366132720Skan switch (__which) 1367132720Skan { 1368132720Skan case money_base::symbol: 1369132720Skan // According to 22.2.6.1.2, p2, symbol is required 1370132720Skan // if (__io.flags() & ios_base::showbase), otherwise 1371132720Skan // is optional and consumed only if other characters 1372132720Skan // are needed to complete the format. 1373132720Skan if (__io.flags() & ios_base::showbase || __sign_size > 1 1374132720Skan || __i == 0 1375132720Skan || (__i == 1 && (__mandatory_sign 1376132720Skan || (static_cast<part>(__p.field[0]) 1377132720Skan == money_base::sign) 1378132720Skan || (static_cast<part>(__p.field[2]) 1379132720Skan == money_base::space))) 1380132720Skan || (__i == 2 && ((static_cast<part>(__p.field[3]) 1381132720Skan == money_base::value) 1382132720Skan || __mandatory_sign 1383132720Skan && (static_cast<part>(__p.field[3]) 1384132720Skan == money_base::sign)))) 138597403Sobrien { 1386132720Skan const size_type __len = __lc->_M_curr_symbol_size; 1387132720Skan size_type __j = 0; 1388132720Skan for (; __beg != __end && __j < __len 1389132720Skan && *__beg == __lc->_M_curr_symbol[__j]; 1390132720Skan ++__beg, ++__j); 1391132720Skan if (__j != __len 1392132720Skan && (__j || __io.flags() & ios_base::showbase)) 139397403Sobrien __testvalid = false; 139497403Sobrien } 1395132720Skan break; 1396132720Skan case money_base::sign: 1397132720Skan // Sign might not exist, or be more than one character long. 1398132720Skan if (__lc->_M_positive_sign_size && __beg != __end 1399132720Skan && *__beg == __lc->_M_positive_sign[0]) 1400132720Skan { 1401132720Skan __sign_size = __lc->_M_positive_sign_size; 1402132720Skan ++__beg; 1403132720Skan } 1404132720Skan else if (__lc->_M_negative_sign_size && __beg != __end 1405132720Skan && *__beg == __lc->_M_negative_sign[0]) 1406132720Skan { 1407132720Skan __negative = true; 1408132720Skan __sign_size = __lc->_M_negative_sign_size; 1409132720Skan ++__beg; 1410132720Skan } 1411132720Skan else if (__lc->_M_positive_sign_size 1412132720Skan && !__lc->_M_negative_sign_size) 1413132720Skan // "... if no sign is detected, the result is given the sign 1414132720Skan // that corresponds to the source of the empty string" 1415132720Skan __negative = true; 1416132720Skan else if (__mandatory_sign) 1417132720Skan __testvalid = false; 1418132720Skan break; 1419132720Skan case money_base::value: 1420132720Skan // Extract digits, remove and stash away the 1421132720Skan // grouping of found thousands separators. 1422132720Skan for (; __beg != __end; ++__beg) 1423169691Skan { 1424169691Skan const char_type __c = *__beg; 1425169691Skan const char_type* __q = __traits_type::find(__lit_zero, 1426169691Skan 10, __c); 1427169691Skan if (__q != 0) 1428169691Skan { 1429169691Skan __res += money_base::_S_atoms[__q - __lit]; 1430169691Skan ++__n; 1431169691Skan } 1432169691Skan else if (__c == __lc->_M_decimal_point 1433169691Skan && !__testdecfound) 1434169691Skan { 1435169691Skan __last_pos = __n; 1436169691Skan __n = 0; 1437169691Skan __testdecfound = true; 1438169691Skan } 1439169691Skan else if (__lc->_M_use_grouping 1440169691Skan && __c == __lc->_M_thousands_sep 1441169691Skan && !__testdecfound) 1442169691Skan { 1443169691Skan if (__n) 1444169691Skan { 1445169691Skan // Mark position for later analysis. 1446169691Skan __grouping_tmp += static_cast<char>(__n); 1447169691Skan __n = 0; 1448169691Skan } 1449169691Skan else 1450169691Skan { 1451169691Skan __testvalid = false; 1452169691Skan break; 1453169691Skan } 1454169691Skan } 1455169691Skan else 1456169691Skan break; 1457169691Skan } 1458132720Skan if (__res.empty()) 1459132720Skan __testvalid = false; 1460132720Skan break; 1461132720Skan case money_base::space: 1462132720Skan // At least one space is required. 1463132720Skan if (__beg != __end && __ctype.is(ctype_base::space, *__beg)) 1464132720Skan ++__beg; 1465132720Skan else 1466132720Skan __testvalid = false; 1467132720Skan case money_base::none: 1468132720Skan // Only if not at the end of the pattern. 1469132720Skan if (__i != 3) 1470132720Skan for (; __beg != __end 1471132720Skan && __ctype.is(ctype_base::space, *__beg); ++__beg); 1472132720Skan break; 1473132720Skan } 1474132720Skan } 147597403Sobrien 1476132720Skan // Need to get the rest of the sign characters, if they exist. 1477132720Skan if (__sign_size > 1 && __testvalid) 1478132720Skan { 1479132720Skan const char_type* __sign = __negative ? __lc->_M_negative_sign 1480132720Skan : __lc->_M_positive_sign; 1481132720Skan size_type __i = 1; 1482132720Skan for (; __beg != __end && __i < __sign_size 1483132720Skan && *__beg == __sign[__i]; ++__beg, ++__i); 1484132720Skan 1485132720Skan if (__i != __sign_size) 1486132720Skan __testvalid = false; 1487132720Skan } 148897403Sobrien 1489132720Skan if (__testvalid) 1490132720Skan { 1491132720Skan // Strip leading zeros. 1492132720Skan if (__res.size() > 1) 1493132720Skan { 1494132720Skan const size_type __first = __res.find_first_not_of('0'); 1495132720Skan const bool __only_zeros = __first == string::npos; 1496132720Skan if (__first) 1497132720Skan __res.erase(0, __only_zeros ? __res.size() - 1 : __first); 1498132720Skan } 149997403Sobrien 1500132720Skan // 22.2.6.1.2, p4 1501132720Skan if (__negative && __res[0] != '0') 1502132720Skan __res.insert(__res.begin(), '-'); 1503132720Skan 1504132720Skan // Test for grouping fidelity. 1505132720Skan if (__grouping_tmp.size()) 1506132720Skan { 1507132720Skan // Add the ending grouping. 1508132720Skan __grouping_tmp += static_cast<char>(__testdecfound ? __last_pos 1509132720Skan : __n); 1510132720Skan if (!std::__verify_grouping(__lc->_M_grouping, 1511132720Skan __lc->_M_grouping_size, 1512132720Skan __grouping_tmp)) 1513169691Skan __err |= ios_base::failbit; 1514132720Skan } 1515132720Skan 1516132720Skan // Iff not enough digits were supplied after the decimal-point. 1517132720Skan if (__testdecfound && __lc->_M_frac_digits > 0 1518132720Skan && __n != __lc->_M_frac_digits) 1519132720Skan __testvalid = false; 1520132720Skan } 1521132720Skan 1522132720Skan // Iff valid sequence is not recognized. 1523132720Skan if (!__testvalid) 1524132720Skan __err |= ios_base::failbit; 1525132720Skan else 1526132720Skan __units.swap(__res); 1527132720Skan 1528169691Skan // Iff no more characters are available. 1529169691Skan if (__beg == __end) 1530169691Skan __err |= ios_base::eofbit; 1531132720Skan return __beg; 1532132720Skan } 153397403Sobrien 1534169691Skan#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 1535132720Skan template<typename _CharT, typename _InIter> 1536132720Skan _InIter 1537132720Skan money_get<_CharT, _InIter>:: 1538169691Skan __do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 1539169691Skan ios_base::iostate& __err, double& __units) const 1540169691Skan { 1541169691Skan string __str; 1542169691Skan __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str) 1543169691Skan : _M_extract<false>(__beg, __end, __io, __err, __str); 1544169691Skan std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale()); 1545169691Skan return __beg; 1546169691Skan } 1547169691Skan#endif 1548169691Skan 1549169691Skan template<typename _CharT, typename _InIter> 1550169691Skan _InIter 1551169691Skan money_get<_CharT, _InIter>:: 1552132720Skan do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 1553132720Skan ios_base::iostate& __err, long double& __units) const 1554132720Skan { 1555132720Skan string __str; 1556169691Skan __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str) 1557169691Skan : _M_extract<false>(__beg, __end, __io, __err, __str); 1558132720Skan std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale()); 1559132720Skan return __beg; 1560132720Skan } 1561132720Skan 1562132720Skan template<typename _CharT, typename _InIter> 1563132720Skan _InIter 1564132720Skan money_get<_CharT, _InIter>:: 1565132720Skan do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 1566169691Skan ios_base::iostate& __err, string_type& __digits) const 1567132720Skan { 1568132720Skan typedef typename string::size_type size_type; 1569132720Skan 1570132720Skan const locale& __loc = __io._M_getloc(); 1571132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 1572132720Skan 1573132720Skan string __str; 1574169691Skan __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str) 1575169691Skan : _M_extract<false>(__beg, __end, __io, __err, __str); 1576132720Skan const size_type __len = __str.size(); 1577132720Skan if (__len) 157897403Sobrien { 1579169691Skan __digits.resize(__len); 1580169691Skan __ctype.widen(__str.data(), __str.data() + __len, &__digits[0]); 158197403Sobrien } 1582169691Skan return __beg; 1583132720Skan } 158497403Sobrien 1585132720Skan template<typename _CharT, typename _OutIter> 1586132720Skan template<bool _Intl> 1587132720Skan _OutIter 1588132720Skan money_put<_CharT, _OutIter>:: 1589132720Skan _M_insert(iter_type __s, ios_base& __io, char_type __fill, 1590132720Skan const string_type& __digits) const 1591132720Skan { 1592132720Skan typedef typename string_type::size_type size_type; 1593132720Skan typedef money_base::part part; 1594169691Skan typedef __moneypunct_cache<_CharT, _Intl> __cache_type; 1595132720Skan 1596132720Skan const locale& __loc = __io._M_getloc(); 1597132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 159897403Sobrien 1599132720Skan __use_cache<__cache_type> __uc; 1600132720Skan const __cache_type* __lc = __uc(__loc); 1601132720Skan const char_type* __lit = __lc->_M_atoms; 160297403Sobrien 1603132720Skan // Determine if negative or positive formats are to be used, and 1604132720Skan // discard leading negative_sign if it is present. 1605132720Skan const char_type* __beg = __digits.data(); 1606132720Skan 1607132720Skan money_base::pattern __p; 1608132720Skan const char_type* __sign; 1609132720Skan size_type __sign_size; 1610169691Skan if (!(*__beg == __lit[money_base::_S_minus])) 1611132720Skan { 1612132720Skan __p = __lc->_M_pos_format; 1613132720Skan __sign = __lc->_M_positive_sign; 1614132720Skan __sign_size = __lc->_M_positive_sign_size; 1615132720Skan } 1616132720Skan else 1617132720Skan { 1618132720Skan __p = __lc->_M_neg_format; 1619132720Skan __sign = __lc->_M_negative_sign; 1620132720Skan __sign_size = __lc->_M_negative_sign_size; 1621132720Skan if (__digits.size()) 1622132720Skan ++__beg; 1623132720Skan } 1624132720Skan 1625132720Skan // Look for valid numbers in the ctype facet within input digits. 1626132720Skan size_type __len = __ctype.scan_not(ctype_base::digit, __beg, 1627132720Skan __beg + __digits.size()) - __beg; 1628132720Skan if (__len) 1629132720Skan { 1630132720Skan // Assume valid input, and attempt to format. 1631132720Skan // Break down input numbers into base components, as follows: 1632132720Skan // final_value = grouped units + (decimal point) + (digits) 1633132720Skan string_type __value; 1634132720Skan __value.reserve(2 * __len); 1635132720Skan 1636132720Skan // Add thousands separators to non-decimal digits, per 1637132720Skan // grouping rules. 1638169691Skan long __paddec = __len - __lc->_M_frac_digits; 1639132720Skan if (__paddec > 0) 1640132720Skan { 1641132720Skan if (__lc->_M_frac_digits < 0) 1642132720Skan __paddec = __len; 1643132720Skan if (__lc->_M_grouping_size) 1644132720Skan { 1645169691Skan __value.assign(2 * __paddec, char_type()); 1646169691Skan _CharT* __vend = 1647169691Skan std::__add_grouping(&__value[0], __lc->_M_thousands_sep, 1648132720Skan __lc->_M_grouping, 1649132720Skan __lc->_M_grouping_size, 1650132720Skan __beg, __beg + __paddec); 1651169691Skan __value.erase(__vend - &__value[0]); 1652132720Skan } 1653132720Skan else 1654132720Skan __value.assign(__beg, __paddec); 1655132720Skan } 1656132720Skan 1657132720Skan // Deal with decimal point, decimal digits. 1658132720Skan if (__lc->_M_frac_digits > 0) 1659132720Skan { 1660132720Skan __value += __lc->_M_decimal_point; 1661132720Skan if (__paddec >= 0) 1662132720Skan __value.append(__beg + __paddec, __lc->_M_frac_digits); 1663132720Skan else 1664132720Skan { 1665132720Skan // Have to pad zeros in the decimal position. 1666132720Skan __value.append(-__paddec, __lit[money_base::_S_zero]); 1667132720Skan __value.append(__beg, __len); 1668132720Skan } 1669132720Skan } 1670132720Skan 1671132720Skan // Calculate length of resulting string. 1672132720Skan const ios_base::fmtflags __f = __io.flags() 1673132720Skan & ios_base::adjustfield; 1674132720Skan __len = __value.size() + __sign_size; 1675132720Skan __len += ((__io.flags() & ios_base::showbase) 1676132720Skan ? __lc->_M_curr_symbol_size : 0); 1677132720Skan 1678132720Skan string_type __res; 1679132720Skan __res.reserve(2 * __len); 1680132720Skan 1681132720Skan const size_type __width = static_cast<size_type>(__io.width()); 1682132720Skan const bool __testipad = (__f == ios_base::internal 1683132720Skan && __len < __width); 1684132720Skan // Fit formatted digits into the required pattern. 1685132720Skan for (int __i = 0; __i < 4; ++__i) 1686132720Skan { 1687132720Skan const part __which = static_cast<part>(__p.field[__i]); 1688132720Skan switch (__which) 1689132720Skan { 1690132720Skan case money_base::symbol: 1691132720Skan if (__io.flags() & ios_base::showbase) 1692132720Skan __res.append(__lc->_M_curr_symbol, 1693132720Skan __lc->_M_curr_symbol_size); 1694132720Skan break; 1695132720Skan case money_base::sign: 1696132720Skan // Sign might not exist, or be more than one 1697132720Skan // charater long. In that case, add in the rest 1698132720Skan // below. 1699132720Skan if (__sign_size) 1700132720Skan __res += __sign[0]; 1701132720Skan break; 1702132720Skan case money_base::value: 1703132720Skan __res += __value; 1704132720Skan break; 1705132720Skan case money_base::space: 1706132720Skan // At least one space is required, but if internal 1707132720Skan // formatting is required, an arbitrary number of 1708132720Skan // fill spaces will be necessary. 1709132720Skan if (__testipad) 1710132720Skan __res.append(__width - __len, __fill); 1711132720Skan else 1712132720Skan __res += __fill; 1713132720Skan break; 1714132720Skan case money_base::none: 1715132720Skan if (__testipad) 1716132720Skan __res.append(__width - __len, __fill); 1717132720Skan break; 1718132720Skan } 1719132720Skan } 1720132720Skan 1721132720Skan // Special case of multi-part sign parts. 1722132720Skan if (__sign_size > 1) 1723132720Skan __res.append(__sign + 1, __sign_size - 1); 1724132720Skan 1725132720Skan // Pad, if still necessary. 1726132720Skan __len = __res.size(); 1727132720Skan if (__width > __len) 1728132720Skan { 1729132720Skan if (__f == ios_base::left) 1730132720Skan // After. 1731132720Skan __res.append(__width - __len, __fill); 1732132720Skan else 1733132720Skan // Before. 1734132720Skan __res.insert(0, __width - __len, __fill); 1735132720Skan __len = __width; 1736132720Skan } 1737132720Skan 1738132720Skan // Write resulting, fully-formatted string to output iterator. 1739132720Skan __s = std::__write(__s, __res.data(), __len); 1740132720Skan } 1741132720Skan __io.width(0); 1742132720Skan return __s; 1743132720Skan } 1744169691Skan 1745169691Skan#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 174697403Sobrien template<typename _CharT, typename _OutIter> 174797403Sobrien _OutIter 174897403Sobrien money_put<_CharT, _OutIter>:: 1749169691Skan __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, 1750169691Skan double __units) const 1751169691Skan { return this->do_put(__s, __intl, __io, __fill, (long double) __units); } 1752169691Skan#endif 1753169691Skan 1754169691Skan template<typename _CharT, typename _OutIter> 1755169691Skan _OutIter 1756169691Skan money_put<_CharT, _OutIter>:: 175797403Sobrien do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, 175897403Sobrien long double __units) const 1759132720Skan { 176097403Sobrien const locale __loc = __io.getloc(); 176197403Sobrien const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 1762132720Skan#ifdef _GLIBCXX_USE_C99 176397403Sobrien // First try a buffer perhaps big enough. 176497403Sobrien int __cs_size = 64; 176597403Sobrien char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1766132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 1767132720Skan // 328. Bad sprintf format modifier in money_put<>::do_put() 1768169691Skan int __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, 1769169691Skan "%.*Lf", 0, __units); 177097403Sobrien // If the buffer was not large enough, try again with the correct size. 177197403Sobrien if (__len >= __cs_size) 177297403Sobrien { 177397403Sobrien __cs_size = __len + 1; 177497403Sobrien __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1775169691Skan __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, 1776169691Skan "%.*Lf", 0, __units); 177797403Sobrien } 177897403Sobrien#else 1779132720Skan // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'. 1780132720Skan const int __cs_size = numeric_limits<long double>::max_exponent10 + 3; 178197403Sobrien char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1782169691Skan int __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, "%.*Lf", 1783169691Skan 0, __units); 178497403Sobrien#endif 1785169691Skan string_type __digits(__len, char_type()); 1786169691Skan __ctype.widen(__cs, __cs + __len, &__digits[0]); 1787132720Skan return __intl ? _M_insert<true>(__s, __io, __fill, __digits) 1788132720Skan : _M_insert<false>(__s, __io, __fill, __digits); 178997403Sobrien } 179097403Sobrien 179197403Sobrien template<typename _CharT, typename _OutIter> 179297403Sobrien _OutIter 179397403Sobrien money_put<_CharT, _OutIter>:: 179497403Sobrien do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, 179597403Sobrien const string_type& __digits) const 1796132720Skan { return __intl ? _M_insert<true>(__s, __io, __fill, __digits) 1797132720Skan : _M_insert<false>(__s, __io, __fill, __digits); } 179897403Sobrien 1799169691Skan_GLIBCXX_END_LDBL_NAMESPACE 180097403Sobrien 180197403Sobrien // NB: Not especially useful. Without an ios_base object or some 180297403Sobrien // kind of locale reference, we are left clawing at the air where 180397403Sobrien // the side of the mountain used to be... 180497403Sobrien template<typename _CharT, typename _InIter> 180597403Sobrien time_base::dateorder 180697403Sobrien time_get<_CharT, _InIter>::do_date_order() const 180797403Sobrien { return time_base::no_order; } 180897403Sobrien 1809132720Skan // Expand a strftime format string and parse it. E.g., do_get_date() may 1810132720Skan // pass %m/%d/%Y => extracted characters. 181197403Sobrien template<typename _CharT, typename _InIter> 1812132720Skan _InIter 181397403Sobrien time_get<_CharT, _InIter>:: 1814132720Skan _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io, 1815132720Skan ios_base::iostate& __err, tm* __tm, 181697403Sobrien const _CharT* __format) const 1817132720Skan { 1818132720Skan const locale& __loc = __io._M_getloc(); 1819132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 1820132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 1821132720Skan const size_t __len = char_traits<_CharT>::length(__format); 182297403Sobrien 1823169691Skan ios_base::iostate __tmperr = ios_base::goodbit; 1824169691Skan for (size_t __i = 0; __beg != __end && __i < __len && !__tmperr; ++__i) 182597403Sobrien { 1826132720Skan if (__ctype.narrow(__format[__i], 0) == '%') 182797403Sobrien { 182897403Sobrien // Verify valid formatting code, attempt to extract. 1829132720Skan char __c = __ctype.narrow(__format[++__i], 0); 1830132720Skan int __mem = 0; 183197403Sobrien if (__c == 'E' || __c == 'O') 1832132720Skan __c = __ctype.narrow(__format[++__i], 0); 183397403Sobrien switch (__c) 183497403Sobrien { 183597403Sobrien const char* __cs; 183697403Sobrien _CharT __wcs[10]; 183797403Sobrien case 'a': 183897403Sobrien // Abbreviated weekday name [tm_wday] 183997403Sobrien const char_type* __days1[7]; 184097403Sobrien __tp._M_days_abbreviated(__days1); 1841132720Skan __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 1842169691Skan 7, __io, __tmperr); 184397403Sobrien break; 184497403Sobrien case 'A': 184597403Sobrien // Weekday name [tm_wday]. 184697403Sobrien const char_type* __days2[7]; 184797403Sobrien __tp._M_days(__days2); 1848132720Skan __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 1849169691Skan 7, __io, __tmperr); 185097403Sobrien break; 185197403Sobrien case 'h': 185297403Sobrien case 'b': 185397403Sobrien // Abbreviated month name [tm_mon] 185497403Sobrien const char_type* __months1[12]; 185597403Sobrien __tp._M_months_abbreviated(__months1); 1856132720Skan __beg = _M_extract_name(__beg, __end, __tm->tm_mon, 1857169691Skan __months1, 12, __io, __tmperr); 185897403Sobrien break; 185997403Sobrien case 'B': 186097403Sobrien // Month name [tm_mon]. 186197403Sobrien const char_type* __months2[12]; 186297403Sobrien __tp._M_months(__months2); 1863132720Skan __beg = _M_extract_name(__beg, __end, __tm->tm_mon, 1864169691Skan __months2, 12, __io, __tmperr); 186597403Sobrien break; 186697403Sobrien case 'c': 186797403Sobrien // Default time and date representation. 186897403Sobrien const char_type* __dt[2]; 186997403Sobrien __tp._M_date_time_formats(__dt); 1870169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1871132720Skan __tm, __dt[0]); 187297403Sobrien break; 187397403Sobrien case 'd': 187497403Sobrien // Day [01, 31]. [tm_mday] 1875132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2, 1876169691Skan __io, __tmperr); 187797403Sobrien break; 1878132720Skan case 'e': 1879132720Skan // Day [1, 31], with single digits preceded by 1880132720Skan // space. [tm_mday] 1881132720Skan if (__ctype.is(ctype_base::space, *__beg)) 1882132720Skan __beg = _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9, 1883169691Skan 1, __io, __tmperr); 1884132720Skan else 1885132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31, 1886169691Skan 2, __io, __tmperr); 1887132720Skan break; 188897403Sobrien case 'D': 188997403Sobrien // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year] 189097403Sobrien __cs = "%m/%d/%y"; 189197403Sobrien __ctype.widen(__cs, __cs + 9, __wcs); 1892169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1893132720Skan __tm, __wcs); 189497403Sobrien break; 189597403Sobrien case 'H': 189697403Sobrien // Hour [00, 23]. [tm_hour] 1897132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2, 1898169691Skan __io, __tmperr); 189997403Sobrien break; 190097403Sobrien case 'I': 190197403Sobrien // Hour [01, 12]. [tm_hour] 1902132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2, 1903169691Skan __io, __tmperr); 190497403Sobrien break; 190597403Sobrien case 'm': 190697403Sobrien // Month [01, 12]. [tm_mon] 1907132720Skan __beg = _M_extract_num(__beg, __end, __mem, 1, 12, 2, 1908169691Skan __io, __tmperr); 1909169691Skan if (!__tmperr) 191097403Sobrien __tm->tm_mon = __mem - 1; 191197403Sobrien break; 191297403Sobrien case 'M': 191397403Sobrien // Minute [00, 59]. [tm_min] 1914132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2, 1915169691Skan __io, __tmperr); 191697403Sobrien break; 191797403Sobrien case 'n': 191897403Sobrien if (__ctype.narrow(*__beg, 0) == '\n') 191997403Sobrien ++__beg; 192097403Sobrien else 1921169691Skan __tmperr |= ios_base::failbit; 192297403Sobrien break; 192397403Sobrien case 'R': 192497403Sobrien // Equivalent to (%H:%M). 192597403Sobrien __cs = "%H:%M"; 192697403Sobrien __ctype.widen(__cs, __cs + 6, __wcs); 1927169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1928132720Skan __tm, __wcs); 192997403Sobrien break; 193097403Sobrien case 'S': 1931146897Skan // Seconds. [tm_sec] 1932146897Skan // [00, 60] in C99 (one leap-second), [00, 61] in C89. 1933146897Skan#ifdef _GLIBCXX_USE_C99 1934146897Skan __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 60, 2, 1935146897Skan#else 1936146897Skan __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 61, 2, 1937146897Skan#endif 1938169691Skan __io, __tmperr); 193997403Sobrien break; 194097403Sobrien case 't': 194197403Sobrien if (__ctype.narrow(*__beg, 0) == '\t') 194297403Sobrien ++__beg; 194397403Sobrien else 1944169691Skan __tmperr |= ios_base::failbit; 194597403Sobrien break; 194697403Sobrien case 'T': 194797403Sobrien // Equivalent to (%H:%M:%S). 194897403Sobrien __cs = "%H:%M:%S"; 194997403Sobrien __ctype.widen(__cs, __cs + 9, __wcs); 1950169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1951132720Skan __tm, __wcs); 195297403Sobrien break; 195397403Sobrien case 'x': 195497403Sobrien // Locale's date. 195597403Sobrien const char_type* __dates[2]; 195697403Sobrien __tp._M_date_formats(__dates); 1957169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1958132720Skan __tm, __dates[0]); 195997403Sobrien break; 196097403Sobrien case 'X': 196197403Sobrien // Locale's time. 196297403Sobrien const char_type* __times[2]; 196397403Sobrien __tp._M_time_formats(__times); 1964169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1965132720Skan __tm, __times[0]); 196697403Sobrien break; 196797403Sobrien case 'y': 1968132720Skan case 'C': // C99 196997403Sobrien // Two digit year. [tm_year] 1970132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2, 1971169691Skan __io, __tmperr); 197297403Sobrien break; 197397403Sobrien case 'Y': 197497403Sobrien // Year [1900). [tm_year] 1975132720Skan __beg = _M_extract_num(__beg, __end, __mem, 0, 9999, 4, 1976169691Skan __io, __tmperr); 1977169691Skan if (!__tmperr) 197897403Sobrien __tm->tm_year = __mem - 1900; 197997403Sobrien break; 198097403Sobrien case 'Z': 198197403Sobrien // Timezone info. 198297403Sobrien if (__ctype.is(ctype_base::upper, *__beg)) 198397403Sobrien { 198497403Sobrien int __tmp; 1985132720Skan __beg = _M_extract_name(__beg, __end, __tmp, 1986132720Skan __timepunct_cache<_CharT>::_S_timezones, 1987169691Skan 14, __io, __tmperr); 1988132720Skan 198997403Sobrien // GMT requires special effort. 1990169691Skan if (__beg != __end && !__tmperr && __tmp == 0 1991132720Skan && (*__beg == __ctype.widen('-') 1992132720Skan || *__beg == __ctype.widen('+'))) 199397403Sobrien { 1994132720Skan __beg = _M_extract_num(__beg, __end, __tmp, 0, 23, 2, 1995169691Skan __io, __tmperr); 1996132720Skan __beg = _M_extract_num(__beg, __end, __tmp, 0, 59, 2, 1997169691Skan __io, __tmperr); 1998132720Skan } 199997403Sobrien } 200097403Sobrien else 2001169691Skan __tmperr |= ios_base::failbit; 2002132720Skan break; 2003132720Skan default: 2004132720Skan // Not recognized. 2005169691Skan __tmperr |= ios_base::failbit; 200697403Sobrien } 2007132720Skan } 2008132720Skan else 2009132720Skan { 2010132720Skan // Verify format and input match, extract and discard. 2011132720Skan if (__format[__i] == *__beg) 2012132720Skan ++__beg; 2013132720Skan else 2014169691Skan __tmperr |= ios_base::failbit; 2015132720Skan } 201697403Sobrien } 2017169691Skan 2018169691Skan if (__tmperr) 2019169691Skan __err |= ios_base::failbit; 2020169691Skan 2021132720Skan return __beg; 202297403Sobrien } 202397403Sobrien 202497403Sobrien template<typename _CharT, typename _InIter> 2025132720Skan _InIter 202697403Sobrien time_get<_CharT, _InIter>:: 2027132720Skan _M_extract_num(iter_type __beg, iter_type __end, int& __member, 2028132720Skan int __min, int __max, size_t __len, 2029132720Skan ios_base& __io, ios_base::iostate& __err) const 203097403Sobrien { 2031132720Skan const locale& __loc = __io._M_getloc(); 2032132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 2033132720Skan 2034132720Skan // As-is works for __len = 1, 2, 4, the values actually used. 2035132720Skan int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1); 2036132720Skan 2037132720Skan ++__min; 203897403Sobrien size_t __i = 0; 2039132720Skan int __value = 0; 2040132720Skan for (; __beg != __end && __i < __len; ++__beg, ++__i) 204197403Sobrien { 2042132720Skan const char __c = __ctype.narrow(*__beg, '*'); 2043132720Skan if (__c >= '0' && __c <= '9') 2044132720Skan { 2045132720Skan __value = __value * 10 + (__c - '0'); 2046132720Skan const int __valuec = __value * __mult; 2047132720Skan if (__valuec > __max || __valuec + __mult < __min) 2048132720Skan break; 2049132720Skan __mult /= 10; 2050132720Skan } 2051132720Skan else 2052132720Skan break; 205397403Sobrien } 205497403Sobrien if (__i == __len) 2055132720Skan __member = __value; 205697403Sobrien else 205797403Sobrien __err |= ios_base::failbit; 2058169691Skan 2059132720Skan return __beg; 206097403Sobrien } 206197403Sobrien 206297403Sobrien // Assumptions: 206397403Sobrien // All elements in __names are unique. 206497403Sobrien template<typename _CharT, typename _InIter> 2065132720Skan _InIter 206697403Sobrien time_get<_CharT, _InIter>:: 2067132720Skan _M_extract_name(iter_type __beg, iter_type __end, int& __member, 2068132720Skan const _CharT** __names, size_t __indexlen, 2069132720Skan ios_base& __io, ios_base::iostate& __err) const 207097403Sobrien { 2071132720Skan typedef char_traits<_CharT> __traits_type; 2072132720Skan const locale& __loc = __io._M_getloc(); 2073132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 2074132720Skan 2075132720Skan int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int) 2076117397Skan * __indexlen)); 207797403Sobrien size_t __nmatches = 0; 207897403Sobrien size_t __pos = 0; 207997403Sobrien bool __testvalid = true; 208097403Sobrien const char_type* __name; 208197403Sobrien 208297403Sobrien // Look for initial matches. 2083132720Skan // NB: Some of the locale data is in the form of all lowercase 2084132720Skan // names, and some is in the form of initially-capitalized 2085132720Skan // names. Look for both. 2086132720Skan if (__beg != __end) 2087132720Skan { 2088132720Skan const char_type __c = *__beg; 2089132720Skan for (size_t __i1 = 0; __i1 < __indexlen; ++__i1) 2090132720Skan if (__c == __names[__i1][0] 2091132720Skan || __c == __ctype.toupper(__names[__i1][0])) 2092132720Skan __matches[__nmatches++] = __i1; 2093132720Skan } 2094132720Skan 2095117397Skan while (__nmatches > 1) 209697403Sobrien { 209797403Sobrien // Find smallest matching string. 2098132720Skan size_t __minlen = __traits_type::length(__names[__matches[0]]); 2099132720Skan for (size_t __i2 = 1; __i2 < __nmatches; ++__i2) 2100132720Skan __minlen = std::min(__minlen, 2101132720Skan __traits_type::length(__names[__matches[__i2]])); 2102146897Skan ++__beg, ++__pos; 210397403Sobrien if (__pos < __minlen && __beg != __end) 2104132720Skan for (size_t __i3 = 0; __i3 < __nmatches;) 2105132720Skan { 2106132720Skan __name = __names[__matches[__i3]]; 2107169691Skan if (!(__name[__pos] == *__beg)) 2108132720Skan __matches[__i3] = __matches[--__nmatches]; 2109132720Skan else 2110132720Skan ++__i3; 2111132720Skan } 211297403Sobrien else 211397403Sobrien break; 211497403Sobrien } 211597403Sobrien 211697403Sobrien if (__nmatches == 1) 211797403Sobrien { 211897403Sobrien // Make sure found name is completely extracted. 2119146897Skan ++__beg, ++__pos; 212097403Sobrien __name = __names[__matches[0]]; 212197403Sobrien const size_t __len = __traits_type::length(__name); 212297403Sobrien while (__pos < __len && __beg != __end && __name[__pos] == *__beg) 212397403Sobrien ++__beg, ++__pos; 212497403Sobrien 212597403Sobrien if (__len == __pos) 212697403Sobrien __member = __matches[0]; 212797403Sobrien else 212897403Sobrien __testvalid = false; 212997403Sobrien } 213097403Sobrien else 213197403Sobrien __testvalid = false; 213297403Sobrien if (!__testvalid) 213397403Sobrien __err |= ios_base::failbit; 2134169691Skan 2135132720Skan return __beg; 213697403Sobrien } 213797403Sobrien 213897403Sobrien template<typename _CharT, typename _InIter> 213997403Sobrien _InIter 214097403Sobrien time_get<_CharT, _InIter>:: 214197403Sobrien do_get_time(iter_type __beg, iter_type __end, ios_base& __io, 214297403Sobrien ios_base::iostate& __err, tm* __tm) const 214397403Sobrien { 2144132720Skan const locale& __loc = __io._M_getloc(); 2145132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 2146132720Skan const char_type* __times[2]; 2147132720Skan __tp._M_time_formats(__times); 2148132720Skan __beg = _M_extract_via_format(__beg, __end, __io, __err, 2149132720Skan __tm, __times[0]); 215097403Sobrien if (__beg == __end) 215197403Sobrien __err |= ios_base::eofbit; 215297403Sobrien return __beg; 215397403Sobrien } 215497403Sobrien 215597403Sobrien template<typename _CharT, typename _InIter> 215697403Sobrien _InIter 215797403Sobrien time_get<_CharT, _InIter>:: 215897403Sobrien do_get_date(iter_type __beg, iter_type __end, ios_base& __io, 215997403Sobrien ios_base::iostate& __err, tm* __tm) const 216097403Sobrien { 2161132720Skan const locale& __loc = __io._M_getloc(); 2162132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 2163132720Skan const char_type* __dates[2]; 2164132720Skan __tp._M_date_formats(__dates); 2165169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __err, 2166132720Skan __tm, __dates[0]); 216797403Sobrien if (__beg == __end) 216897403Sobrien __err |= ios_base::eofbit; 216997403Sobrien return __beg; 217097403Sobrien } 217197403Sobrien 217297403Sobrien template<typename _CharT, typename _InIter> 217397403Sobrien _InIter 217497403Sobrien time_get<_CharT, _InIter>:: 2175132720Skan do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io, 217697403Sobrien ios_base::iostate& __err, tm* __tm) const 217797403Sobrien { 2178132720Skan typedef char_traits<_CharT> __traits_type; 2179132720Skan const locale& __loc = __io._M_getloc(); 2180132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 2181132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 218297403Sobrien const char_type* __days[7]; 218397403Sobrien __tp._M_days_abbreviated(__days); 218497403Sobrien int __tmpwday; 2185169691Skan ios_base::iostate __tmperr = ios_base::goodbit; 2186169691Skan __beg = _M_extract_name(__beg, __end, __tmpwday, __days, 7, 2187169691Skan __io, __tmperr); 218897403Sobrien 218997403Sobrien // Check to see if non-abbreviated name exists, and extract. 219097403Sobrien // NB: Assumes both _M_days and _M_days_abbreviated organized in 219197403Sobrien // exact same order, first to last, such that the resulting 219297403Sobrien // __days array with the same index points to a day, and that 219397403Sobrien // day's abbreviated form. 2194132720Skan // NB: Also assumes that an abbreviated name is a subset of the name. 2195169691Skan if (!__tmperr && __beg != __end) 219697403Sobrien { 219797403Sobrien size_t __pos = __traits_type::length(__days[__tmpwday]); 219897403Sobrien __tp._M_days(__days); 219997403Sobrien const char_type* __name = __days[__tmpwday]; 220097403Sobrien if (__name[__pos] == *__beg) 220197403Sobrien { 220297403Sobrien // Extract the rest of it. 220397403Sobrien const size_t __len = __traits_type::length(__name); 2204132720Skan while (__pos < __len && __beg != __end 220597403Sobrien && __name[__pos] == *__beg) 220697403Sobrien ++__beg, ++__pos; 220797403Sobrien if (__len != __pos) 2208169691Skan __tmperr |= ios_base::failbit; 220997403Sobrien } 221097403Sobrien } 2211169691Skan if (!__tmperr) 2212146897Skan __tm->tm_wday = __tmpwday; 2213169691Skan else 2214169691Skan __err |= ios_base::failbit; 2215169691Skan 221697403Sobrien if (__beg == __end) 221797403Sobrien __err |= ios_base::eofbit; 221897403Sobrien return __beg; 221997403Sobrien } 222097403Sobrien 222197403Sobrien template<typename _CharT, typename _InIter> 222297403Sobrien _InIter 222397403Sobrien time_get<_CharT, _InIter>:: 222497403Sobrien do_get_monthname(iter_type __beg, iter_type __end, 222597403Sobrien ios_base& __io, ios_base::iostate& __err, tm* __tm) const 222697403Sobrien { 2227132720Skan typedef char_traits<_CharT> __traits_type; 2228132720Skan const locale& __loc = __io._M_getloc(); 2229132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 2230132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 223197403Sobrien const char_type* __months[12]; 223297403Sobrien __tp._M_months_abbreviated(__months); 223397403Sobrien int __tmpmon; 2234169691Skan ios_base::iostate __tmperr = ios_base::goodbit; 2235132720Skan __beg = _M_extract_name(__beg, __end, __tmpmon, __months, 12, 2236169691Skan __io, __tmperr); 223797403Sobrien 223897403Sobrien // Check to see if non-abbreviated name exists, and extract. 223997403Sobrien // NB: Assumes both _M_months and _M_months_abbreviated organized in 224097403Sobrien // exact same order, first to last, such that the resulting 224197403Sobrien // __months array with the same index points to a month, and that 224297403Sobrien // month's abbreviated form. 2243132720Skan // NB: Also assumes that an abbreviated name is a subset of the name. 2244169691Skan if (!__tmperr && __beg != __end) 224597403Sobrien { 224697403Sobrien size_t __pos = __traits_type::length(__months[__tmpmon]); 224797403Sobrien __tp._M_months(__months); 224897403Sobrien const char_type* __name = __months[__tmpmon]; 224997403Sobrien if (__name[__pos] == *__beg) 225097403Sobrien { 225197403Sobrien // Extract the rest of it. 225297403Sobrien const size_t __len = __traits_type::length(__name); 2253132720Skan while (__pos < __len && __beg != __end 225497403Sobrien && __name[__pos] == *__beg) 225597403Sobrien ++__beg, ++__pos; 225697403Sobrien if (__len != __pos) 2257169691Skan __tmperr |= ios_base::failbit; 225897403Sobrien } 225997403Sobrien } 2260169691Skan if (!__tmperr) 2261146897Skan __tm->tm_mon = __tmpmon; 2262169691Skan else 2263169691Skan __err |= ios_base::failbit; 2264132720Skan 226597403Sobrien if (__beg == __end) 226697403Sobrien __err |= ios_base::eofbit; 226797403Sobrien return __beg; 226897403Sobrien } 226997403Sobrien 227097403Sobrien template<typename _CharT, typename _InIter> 227197403Sobrien _InIter 227297403Sobrien time_get<_CharT, _InIter>:: 2273132720Skan do_get_year(iter_type __beg, iter_type __end, ios_base& __io, 227497403Sobrien ios_base::iostate& __err, tm* __tm) const 227597403Sobrien { 2276132720Skan const locale& __loc = __io._M_getloc(); 2277132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 227897403Sobrien 227997403Sobrien size_t __i = 0; 2280132720Skan int __value = 0; 2281132720Skan for (; __beg != __end && __i < 4; ++__beg, ++__i) 228297403Sobrien { 2283132720Skan const char __c = __ctype.narrow(*__beg, '*'); 2284132720Skan if (__c >= '0' && __c <= '9') 2285132720Skan __value = __value * 10 + (__c - '0'); 2286132720Skan else 2287132720Skan break; 228897403Sobrien } 228997403Sobrien if (__i == 2 || __i == 4) 2290132720Skan __tm->tm_year = __i == 2 ? __value : __value - 1900; 229197403Sobrien else 229297403Sobrien __err |= ios_base::failbit; 2293169691Skan 229497403Sobrien if (__beg == __end) 229597403Sobrien __err |= ios_base::eofbit; 229697403Sobrien return __beg; 229797403Sobrien } 229897403Sobrien 229997403Sobrien template<typename _CharT, typename _OutIter> 230097403Sobrien _OutIter 230197403Sobrien time_put<_CharT, _OutIter>:: 2302132720Skan put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm, 230397403Sobrien const _CharT* __beg, const _CharT* __end) const 230497403Sobrien { 2305132720Skan const locale& __loc = __io._M_getloc(); 230697403Sobrien ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc); 2307132720Skan for (; __beg != __end; ++__beg) 2308132720Skan if (__ctype.narrow(*__beg, 0) != '%') 2309132720Skan { 2310132720Skan *__s = *__beg; 2311132720Skan ++__s; 2312132720Skan } 2313132720Skan else if (++__beg != __end) 2314132720Skan { 2315132720Skan char __format; 2316132720Skan char __mod = 0; 2317132720Skan const char __c = __ctype.narrow(*__beg, 0); 2318132720Skan if (__c != 'E' && __c != 'O') 2319132720Skan __format = __c; 2320132720Skan else if (++__beg != __end) 2321132720Skan { 2322132720Skan __mod = __c; 2323132720Skan __format = __ctype.narrow(*__beg, 0); 2324132720Skan } 2325132720Skan else 2326132720Skan break; 2327132720Skan __s = this->do_put(__s, __io, __fill, __tm, __format, __mod); 2328132720Skan } 2329132720Skan else 2330132720Skan break; 233197403Sobrien return __s; 233297403Sobrien } 233397403Sobrien 233497403Sobrien template<typename _CharT, typename _OutIter> 233597403Sobrien _OutIter 233697403Sobrien time_put<_CharT, _OutIter>:: 2337132720Skan do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm, 233897403Sobrien char __format, char __mod) const 2339132720Skan { 2340132720Skan const locale& __loc = __io._M_getloc(); 234197403Sobrien ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc); 234297403Sobrien __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc); 234397403Sobrien 234497403Sobrien // NB: This size is arbitrary. Should this be a data member, 234597403Sobrien // initialized at construction? 2346146897Skan const size_t __maxlen = 128; 2347132720Skan char_type* __res = 2348132720Skan static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen)); 234997403Sobrien 235097403Sobrien // NB: In IEE 1003.1-200x, and perhaps other locale models, it 235197403Sobrien // is possible that the format character will be longer than one 235297403Sobrien // character. Possibilities include 'E' or 'O' followed by a 235397403Sobrien // format character: if __mod is not the default argument, assume 235497403Sobrien // it's a valid modifier. 235597403Sobrien char_type __fmt[4]; 2356132720Skan __fmt[0] = __ctype.widen('%'); 235797403Sobrien if (!__mod) 235897403Sobrien { 235997403Sobrien __fmt[1] = __format; 236097403Sobrien __fmt[2] = char_type(); 236197403Sobrien } 236297403Sobrien else 236397403Sobrien { 236497403Sobrien __fmt[1] = __mod; 236597403Sobrien __fmt[2] = __format; 236697403Sobrien __fmt[3] = char_type(); 236797403Sobrien } 236897403Sobrien 236997403Sobrien __tp._M_put(__res, __maxlen, __fmt, __tm); 237097403Sobrien 237197403Sobrien // Write resulting, fully-formatted string to output iterator. 2372132720Skan return std::__write(__s, __res, char_traits<char_type>::length(__res)); 237397403Sobrien } 237497403Sobrien 237597403Sobrien // Generic version does nothing. 237697403Sobrien template<typename _CharT> 237797403Sobrien int 237897403Sobrien collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const 237997403Sobrien { return 0; } 238097403Sobrien 238197403Sobrien // Generic version does nothing. 238297403Sobrien template<typename _CharT> 238397403Sobrien size_t 238497403Sobrien collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const 238597403Sobrien { return 0; } 238697403Sobrien 238797403Sobrien template<typename _CharT> 238897403Sobrien int 238997403Sobrien collate<_CharT>:: 2390132720Skan do_compare(const _CharT* __lo1, const _CharT* __hi1, 239197403Sobrien const _CharT* __lo2, const _CharT* __hi2) const 2392132720Skan { 2393117397Skan // strcoll assumes zero-terminated strings so we make a copy 2394117397Skan // and then put a zero at the end. 239597403Sobrien const string_type __one(__lo1, __hi1); 239697403Sobrien const string_type __two(__lo2, __hi2); 2397117397Skan 2398117397Skan const _CharT* __p = __one.c_str(); 2399132720Skan const _CharT* __pend = __one.data() + __one.length(); 2400117397Skan const _CharT* __q = __two.c_str(); 2401132720Skan const _CharT* __qend = __two.data() + __two.length(); 2402117397Skan 2403117397Skan // strcoll stops when it sees a nul character so we break 2404117397Skan // the strings into zero-terminated substrings and pass those 2405117397Skan // to strcoll. 2406117397Skan for (;;) 2407117397Skan { 2408132720Skan const int __res = _M_compare(__p, __q); 2409117397Skan if (__res) 2410117397Skan return __res; 2411117397Skan 2412117397Skan __p += char_traits<_CharT>::length(__p); 2413117397Skan __q += char_traits<_CharT>::length(__q); 2414117397Skan if (__p == __pend && __q == __qend) 2415117397Skan return 0; 2416117397Skan else if (__p == __pend) 2417117397Skan return -1; 2418117397Skan else if (__q == __qend) 2419117397Skan return 1; 2420117397Skan 2421117397Skan __p++; 2422117397Skan __q++; 2423117397Skan } 242497403Sobrien } 242597403Sobrien 2426132720Skan template<typename _CharT> 242797403Sobrien typename collate<_CharT>::string_type 242897403Sobrien collate<_CharT>:: 242997403Sobrien do_transform(const _CharT* __lo, const _CharT* __hi) const 243097403Sobrien { 2431169691Skan string_type __ret; 2432169691Skan 2433117397Skan // strxfrm assumes zero-terminated strings so we make a copy 2434169691Skan const string_type __str(__lo, __hi); 2435117397Skan 2436117397Skan const _CharT* __p = __str.c_str(); 2437132720Skan const _CharT* __pend = __str.data() + __str.length(); 2438117397Skan 243997403Sobrien size_t __len = (__hi - __lo) * 2; 2440117397Skan 2441169691Skan _CharT* __c = new _CharT[__len]; 2442117397Skan 2443169691Skan try 244497403Sobrien { 2445169691Skan // strxfrm stops when it sees a nul character so we break 2446169691Skan // the string into zero-terminated substrings and pass those 2447169691Skan // to strxfrm. 2448169691Skan for (;;) 2449117397Skan { 2450169691Skan // First try a buffer perhaps big enough. 2451169691Skan size_t __res = _M_transform(__c, __p, __len); 2452169691Skan // If the buffer was not large enough, try again with the 2453169691Skan // correct size. 2454169691Skan if (__res >= __len) 2455169691Skan { 2456169691Skan __len = __res + 1; 2457169691Skan delete [] __c, __c = 0; 2458169691Skan __c = new _CharT[__len]; 2459169691Skan __res = _M_transform(__c, __p, __len); 2460169691Skan } 2461169691Skan 2462169691Skan __ret.append(__c, __res); 2463169691Skan __p += char_traits<_CharT>::length(__p); 2464169691Skan if (__p == __pend) 2465169691Skan break; 2466169691Skan 2467169691Skan __p++; 2468169691Skan __ret.push_back(_CharT()); 2469117397Skan } 2470169691Skan } 2471169691Skan catch(...) 2472169691Skan { 2473169691Skan delete [] __c; 2474169691Skan __throw_exception_again; 2475169691Skan } 2476117397Skan 2477169691Skan delete [] __c; 2478117397Skan 2479169691Skan return __ret; 248097403Sobrien } 248197403Sobrien 2482132720Skan template<typename _CharT> 248397403Sobrien long 248497403Sobrien collate<_CharT>:: 248597403Sobrien do_hash(const _CharT* __lo, const _CharT* __hi) const 2486132720Skan { 248797403Sobrien unsigned long __val = 0; 248897403Sobrien for (; __lo < __hi; ++__lo) 2489132720Skan __val = *__lo + ((__val << 7) | 249097403Sobrien (__val >> (numeric_limits<unsigned long>::digits - 7))); 249197403Sobrien return static_cast<long>(__val); 249297403Sobrien } 249397403Sobrien 249497403Sobrien // Construct correctly padded string, as per 22.2.2.2.2 2495132720Skan // Assumes 249697403Sobrien // __newlen > __oldlen 249797403Sobrien // __news is allocated for __newlen size 249897403Sobrien // Used by both num_put and ostream inserters: if __num, 249997403Sobrien // internal-adjusted objects are padded according to the rules below 250097403Sobrien // concerning 0[xX] and +-, otherwise, exactly as right-adjusted 250197403Sobrien // ones are. 2502102782Skan 2503102782Skan // NB: Of the two parameters, _CharT can be deduced from the 2504102782Skan // function arguments. The other (_Traits) has to be explicitly specified. 250597403Sobrien template<typename _CharT, typename _Traits> 2506132720Skan void 2507132720Skan __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill, 2508132720Skan _CharT* __news, const _CharT* __olds, 2509132720Skan const streamsize __newlen, 2510102782Skan const streamsize __oldlen, const bool __num) 2511102782Skan { 2512132720Skan const size_t __plen = static_cast<size_t>(__newlen - __oldlen); 2513132720Skan const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield; 2514102782Skan 2515132720Skan // Padding last. 251697403Sobrien if (__adjust == ios_base::left) 251797403Sobrien { 2518132720Skan _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen); 2519132720Skan _Traits::assign(__news + __oldlen, __plen, __fill); 2520132720Skan return; 252197403Sobrien } 2522132720Skan 2523132720Skan size_t __mod = 0; 2524132720Skan if (__adjust == ios_base::internal && __num) 252597403Sobrien { 252697403Sobrien // Pad after the sign, if there is one. 252797403Sobrien // Pad after 0[xX], if there is one. 252897403Sobrien // Who came up with these rules, anyway? Jeeze. 2529132720Skan const locale& __loc = __io._M_getloc(); 2530132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 2531102782Skan 2532132720Skan const bool __testsign = (__ctype.widen('-') == __olds[0] 2533132720Skan || __ctype.widen('+') == __olds[0]); 2534132720Skan const bool __testhex = (__ctype.widen('0') == __olds[0] 2535132720Skan && __oldlen > 1 2536132720Skan && (__ctype.widen('x') == __olds[1] 2537132720Skan || __ctype.widen('X') == __olds[1])); 253897403Sobrien if (__testhex) 253997403Sobrien { 2540132720Skan __news[0] = __olds[0]; 254197403Sobrien __news[1] = __olds[1]; 2542132720Skan __mod = 2; 254397403Sobrien __news += 2; 254497403Sobrien } 254597403Sobrien else if (__testsign) 254697403Sobrien { 2547132720Skan __news[0] = __olds[0]; 2548132720Skan __mod = 1; 254997403Sobrien ++__news; 255097403Sobrien } 2551132720Skan // else Padding first. 255297403Sobrien } 2553132720Skan _Traits::assign(__news, __plen, __fill); 2554132720Skan _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod), 2555132720Skan __oldlen - __mod); 255697403Sobrien } 255797403Sobrien 2558132720Skan bool 2559132720Skan __verify_grouping(const char* __grouping, size_t __grouping_size, 2560132720Skan const string& __grouping_tmp) 2561132720Skan { 2562132720Skan const size_t __n = __grouping_tmp.size() - 1; 2563169691Skan const size_t __min = std::min(__n, size_t(__grouping_size - 1)); 2564132720Skan size_t __i = __n; 2565132720Skan bool __test = true; 2566132720Skan 2567132720Skan // Parsed number groupings have to match the 2568132720Skan // numpunct::grouping string exactly, starting at the 2569132720Skan // right-most point of the parsed sequence of elements ... 2570132720Skan for (size_t __j = 0; __j < __min && __test; --__i, ++__j) 2571132720Skan __test = __grouping_tmp[__i] == __grouping[__j]; 2572132720Skan for (; __i && __test; --__i) 2573132720Skan __test = __grouping_tmp[__i] == __grouping[__min]; 2574169691Skan // ... but the first parsed grouping can be <= numpunct 2575169691Skan // grouping (only do the check if the numpunct char is > 0 2576169691Skan // because <= 0 means any size is ok). 2577169691Skan if (static_cast<signed char>(__grouping[__min]) > 0) 2578169691Skan __test &= __grouping_tmp[0] <= __grouping[__min]; 2579132720Skan return __test; 2580132720Skan } 258197403Sobrien 258297403Sobrien template<typename _CharT> 258397403Sobrien _CharT* 2584132720Skan __add_grouping(_CharT* __s, _CharT __sep, 2585132720Skan const char* __gbeg, size_t __gsize, 258697403Sobrien const _CharT* __first, const _CharT* __last) 258797403Sobrien { 2588169691Skan size_t __idx = 0; 2589169691Skan size_t __ctr = 0; 2590169691Skan 2591169691Skan while (__last - __first > __gbeg[__idx] 2592169691Skan && static_cast<signed char>(__gbeg[__idx]) > 0) 2593132720Skan { 2594169691Skan __last -= __gbeg[__idx]; 2595169691Skan __idx < __gsize - 1 ? ++__idx : ++__ctr; 2596132720Skan } 2597169691Skan 2598169691Skan while (__first != __last) 259997403Sobrien *__s++ = *__first++; 2600169691Skan 2601169691Skan while (__ctr--) 2602169691Skan { 2603169691Skan *__s++ = __sep; 2604169691Skan for (char __i = __gbeg[__idx]; __i > 0; --__i) 2605169691Skan *__s++ = *__first++; 2606169691Skan } 2607169691Skan 2608169691Skan while (__idx--) 2609169691Skan { 2610169691Skan *__s++ = __sep; 2611169691Skan for (char __i = __gbeg[__idx]; __i > 0; --__i) 2612169691Skan *__s++ = *__first++; 2613169691Skan } 2614169691Skan 261597403Sobrien return __s; 261697403Sobrien } 261797403Sobrien 261897403Sobrien // Inhibit implicit instantiations for required instantiations, 2619132720Skan // which are defined via explicit instantiations elsewhere. 262097403Sobrien // NB: This syntax is a GNU extension. 2621132720Skan#if _GLIBCXX_EXTERN_TEMPLATE 262297403Sobrien extern template class moneypunct<char, false>; 262397403Sobrien extern template class moneypunct<char, true>; 262497403Sobrien extern template class moneypunct_byname<char, false>; 262597403Sobrien extern template class moneypunct_byname<char, true>; 2626169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE money_get<char>; 2627169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE money_put<char>; 262897403Sobrien extern template class numpunct<char>; 262997403Sobrien extern template class numpunct_byname<char>; 2630169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>; 2631169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>; 263297403Sobrien extern template class __timepunct<char>; 263397403Sobrien extern template class time_put<char>; 263497403Sobrien extern template class time_put_byname<char>; 263597403Sobrien extern template class time_get<char>; 263697403Sobrien extern template class time_get_byname<char>; 263797403Sobrien extern template class messages<char>; 263897403Sobrien extern template class messages_byname<char>; 263997403Sobrien extern template class ctype_byname<char>; 264097403Sobrien extern template class codecvt_byname<char, char, mbstate_t>; 264197403Sobrien extern template class collate<char>; 264297403Sobrien extern template class collate_byname<char>; 264397403Sobrien 264497403Sobrien extern template 2645132720Skan const codecvt<char, char, mbstate_t>& 264697403Sobrien use_facet<codecvt<char, char, mbstate_t> >(const locale&); 264797403Sobrien 264897403Sobrien extern template 2649132720Skan const collate<char>& 265097403Sobrien use_facet<collate<char> >(const locale&); 265197403Sobrien 265297403Sobrien extern template 2653132720Skan const numpunct<char>& 265497403Sobrien use_facet<numpunct<char> >(const locale&); 265597403Sobrien 2656132720Skan extern template 2657132720Skan const num_put<char>& 265897403Sobrien use_facet<num_put<char> >(const locale&); 265997403Sobrien 2660132720Skan extern template 2661132720Skan const num_get<char>& 266297403Sobrien use_facet<num_get<char> >(const locale&); 266397403Sobrien 266497403Sobrien extern template 2665132720Skan const moneypunct<char, true>& 266697403Sobrien use_facet<moneypunct<char, true> >(const locale&); 266797403Sobrien 266897403Sobrien extern template 2669132720Skan const moneypunct<char, false>& 267097403Sobrien use_facet<moneypunct<char, false> >(const locale&); 267197403Sobrien 2672132720Skan extern template 2673132720Skan const money_put<char>& 267497403Sobrien use_facet<money_put<char> >(const locale&); 267597403Sobrien 2676132720Skan extern template 2677132720Skan const money_get<char>& 267897403Sobrien use_facet<money_get<char> >(const locale&); 267997403Sobrien 268097403Sobrien extern template 2681132720Skan const __timepunct<char>& 268297403Sobrien use_facet<__timepunct<char> >(const locale&); 268397403Sobrien 2684132720Skan extern template 2685132720Skan const time_put<char>& 268697403Sobrien use_facet<time_put<char> >(const locale&); 268797403Sobrien 2688132720Skan extern template 2689132720Skan const time_get<char>& 269097403Sobrien use_facet<time_get<char> >(const locale&); 269197403Sobrien 2692132720Skan extern template 2693132720Skan const messages<char>& 269497403Sobrien use_facet<messages<char> >(const locale&); 269597403Sobrien 2696132720Skan extern template 2697107606Sobrien bool 2698107606Sobrien has_facet<ctype<char> >(const locale&); 2699107606Sobrien 2700132720Skan extern template 2701107606Sobrien bool 2702107606Sobrien has_facet<codecvt<char, char, mbstate_t> >(const locale&); 2703107606Sobrien 2704132720Skan extern template 2705107606Sobrien bool 2706107606Sobrien has_facet<collate<char> >(const locale&); 2707107606Sobrien 2708132720Skan extern template 2709107606Sobrien bool 2710107606Sobrien has_facet<numpunct<char> >(const locale&); 2711107606Sobrien 2712132720Skan extern template 2713107606Sobrien bool 2714107606Sobrien has_facet<num_put<char> >(const locale&); 2715107606Sobrien 2716132720Skan extern template 2717107606Sobrien bool 2718107606Sobrien has_facet<num_get<char> >(const locale&); 2719107606Sobrien 2720132720Skan extern template 2721107606Sobrien bool 2722107606Sobrien has_facet<moneypunct<char> >(const locale&); 2723107606Sobrien 2724132720Skan extern template 2725107606Sobrien bool 2726107606Sobrien has_facet<money_put<char> >(const locale&); 2727107606Sobrien 2728132720Skan extern template 2729107606Sobrien bool 2730107606Sobrien has_facet<money_get<char> >(const locale&); 2731107606Sobrien 2732132720Skan extern template 2733107606Sobrien bool 2734107606Sobrien has_facet<__timepunct<char> >(const locale&); 2735107606Sobrien 2736132720Skan extern template 2737107606Sobrien bool 2738107606Sobrien has_facet<time_put<char> >(const locale&); 2739107606Sobrien 2740132720Skan extern template 2741107606Sobrien bool 2742107606Sobrien has_facet<time_get<char> >(const locale&); 2743107606Sobrien 2744132720Skan extern template 2745107606Sobrien bool 2746107606Sobrien has_facet<messages<char> >(const locale&); 2747107606Sobrien 2748132720Skan#ifdef _GLIBCXX_USE_WCHAR_T 2749107606Sobrien extern template class moneypunct<wchar_t, false>; 2750107606Sobrien extern template class moneypunct<wchar_t, true>; 2751107606Sobrien extern template class moneypunct_byname<wchar_t, false>; 2752107606Sobrien extern template class moneypunct_byname<wchar_t, true>; 2753169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE money_get<wchar_t>; 2754169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE money_put<wchar_t>; 2755107606Sobrien extern template class numpunct<wchar_t>; 2756107606Sobrien extern template class numpunct_byname<wchar_t>; 2757169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>; 2758169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>; 2759107606Sobrien extern template class __timepunct<wchar_t>; 2760107606Sobrien extern template class time_put<wchar_t>; 2761107606Sobrien extern template class time_put_byname<wchar_t>; 2762107606Sobrien extern template class time_get<wchar_t>; 2763107606Sobrien extern template class time_get_byname<wchar_t>; 2764107606Sobrien extern template class messages<wchar_t>; 2765107606Sobrien extern template class messages_byname<wchar_t>; 2766107606Sobrien extern template class ctype_byname<wchar_t>; 2767107606Sobrien extern template class codecvt_byname<wchar_t, char, mbstate_t>; 2768107606Sobrien extern template class collate<wchar_t>; 2769107606Sobrien extern template class collate_byname<wchar_t>; 2770107606Sobrien 277197403Sobrien extern template 2772132720Skan const codecvt<wchar_t, char, mbstate_t>& 277397403Sobrien use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&); 277497403Sobrien 277597403Sobrien extern template 2776132720Skan const collate<wchar_t>& 277797403Sobrien use_facet<collate<wchar_t> >(const locale&); 277897403Sobrien 277997403Sobrien extern template 2780132720Skan const numpunct<wchar_t>& 278197403Sobrien use_facet<numpunct<wchar_t> >(const locale&); 278297403Sobrien 2783132720Skan extern template 2784132720Skan const num_put<wchar_t>& 278597403Sobrien use_facet<num_put<wchar_t> >(const locale&); 278697403Sobrien 2787132720Skan extern template 2788132720Skan const num_get<wchar_t>& 278997403Sobrien use_facet<num_get<wchar_t> >(const locale&); 279097403Sobrien 279197403Sobrien extern template 2792132720Skan const moneypunct<wchar_t, true>& 279397403Sobrien use_facet<moneypunct<wchar_t, true> >(const locale&); 279497403Sobrien 279597403Sobrien extern template 2796132720Skan const moneypunct<wchar_t, false>& 279797403Sobrien use_facet<moneypunct<wchar_t, false> >(const locale&); 2798132720Skan 2799132720Skan extern template 2800132720Skan const money_put<wchar_t>& 280197403Sobrien use_facet<money_put<wchar_t> >(const locale&); 280297403Sobrien 2803132720Skan extern template 2804132720Skan const money_get<wchar_t>& 280597403Sobrien use_facet<money_get<wchar_t> >(const locale&); 280697403Sobrien 280797403Sobrien extern template 2808132720Skan const __timepunct<wchar_t>& 280997403Sobrien use_facet<__timepunct<wchar_t> >(const locale&); 281097403Sobrien 2811132720Skan extern template 2812132720Skan const time_put<wchar_t>& 281397403Sobrien use_facet<time_put<wchar_t> >(const locale&); 281497403Sobrien 2815132720Skan extern template 2816132720Skan const time_get<wchar_t>& 281797403Sobrien use_facet<time_get<wchar_t> >(const locale&); 281897403Sobrien 2819132720Skan extern template 2820132720Skan const messages<wchar_t>& 282197403Sobrien use_facet<messages<wchar_t> >(const locale&); 282297403Sobrien 2823132720Skan extern template 282497403Sobrien bool 282597403Sobrien has_facet<ctype<wchar_t> >(const locale&); 282697403Sobrien 2827132720Skan extern template 282897403Sobrien bool 282997403Sobrien has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&); 283097403Sobrien 2831132720Skan extern template 283297403Sobrien bool 283397403Sobrien has_facet<collate<wchar_t> >(const locale&); 283497403Sobrien 2835132720Skan extern template 283697403Sobrien bool 283797403Sobrien has_facet<numpunct<wchar_t> >(const locale&); 283897403Sobrien 2839132720Skan extern template 284097403Sobrien bool 284197403Sobrien has_facet<num_put<wchar_t> >(const locale&); 284297403Sobrien 2843132720Skan extern template 284497403Sobrien bool 284597403Sobrien has_facet<num_get<wchar_t> >(const locale&); 284697403Sobrien 2847132720Skan extern template 284897403Sobrien bool 284997403Sobrien has_facet<moneypunct<wchar_t> >(const locale&); 285097403Sobrien 2851132720Skan extern template 285297403Sobrien bool 285397403Sobrien has_facet<money_put<wchar_t> >(const locale&); 285497403Sobrien 2855132720Skan extern template 285697403Sobrien bool 285797403Sobrien has_facet<money_get<wchar_t> >(const locale&); 285897403Sobrien 2859132720Skan extern template 286097403Sobrien bool 286197403Sobrien has_facet<__timepunct<wchar_t> >(const locale&); 286297403Sobrien 2863132720Skan extern template 286497403Sobrien bool 286597403Sobrien has_facet<time_put<wchar_t> >(const locale&); 286697403Sobrien 2867132720Skan extern template 286897403Sobrien bool 286997403Sobrien has_facet<time_get<wchar_t> >(const locale&); 287097403Sobrien 2871132720Skan extern template 287297403Sobrien bool 287397403Sobrien has_facet<messages<wchar_t> >(const locale&); 2874107606Sobrien#endif 2875117397Skan#endif 287697403Sobrien 2877169691Skan_GLIBCXX_END_NAMESPACE 2878169691Skan 287997403Sobrien#endif 2880