locale_facets.tcc revision 236829
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 890236829Spfg typedef __gnu_cxx::__conditional_type<(sizeof(void*) 891236829Spfg <= sizeof(unsigned long)), 892236829Spfg unsigned long, unsigned long long>::__type _UIntPtrType; 893236829Spfg 894236829Spfg _UIntPtrType __ul; 895132720Skan __beg = _M_extract_int(__beg, __end, __io, __err, __ul); 89697403Sobrien 897132720Skan // Reset from hex formatted input. 89897403Sobrien __io.flags(__fmt); 89997403Sobrien 90097403Sobrien if (!(__err & ios_base::failbit)) 90197403Sobrien __v = reinterpret_cast<void*>(__ul); 90297403Sobrien return __beg; 90397403Sobrien } 90497403Sobrien 905117397Skan // For use by integer and floating-point types after they have been 906117397Skan // converted into a char_type string. 907117397Skan template<typename _CharT, typename _OutIter> 908117397Skan void 909117397Skan num_put<_CharT, _OutIter>:: 910132720Skan _M_pad(_CharT __fill, streamsize __w, ios_base& __io, 911117397Skan _CharT* __new, const _CharT* __cs, int& __len) const 912117397Skan { 913117397Skan // [22.2.2.2.2] Stage 3. 914117397Skan // If necessary, pad. 915132720Skan __pad<_CharT, char_traits<_CharT> >::_S_pad(__io, __fill, __new, __cs, 916117397Skan __w, __len, true); 917117397Skan __len = static_cast<int>(__w); 918117397Skan } 919117397Skan 920169691Skan_GLIBCXX_END_LDBL_NAMESPACE 921117397Skan 922117397Skan template<typename _CharT, typename _ValueT> 923117397Skan int 924132720Skan __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit, 925169691Skan ios_base::fmtflags __flags, bool __dec) 926117397Skan { 927169691Skan _CharT* __buf = __bufend; 928169691Skan if (__builtin_expect(__dec, true)) 929117397Skan { 930117397Skan // Decimal. 931132720Skan do 932117397Skan { 933169691Skan *--__buf = __lit[(__v % 10) + __num_base::_S_odigits]; 934117397Skan __v /= 10; 935132720Skan } 936117397Skan while (__v != 0); 937117397Skan } 938169691Skan else if ((__flags & ios_base::basefield) == ios_base::oct) 939117397Skan { 940117397Skan // Octal. 941132720Skan do 942117397Skan { 943169691Skan *--__buf = __lit[(__v & 0x7) + __num_base::_S_odigits]; 944117397Skan __v >>= 3; 945132720Skan } 946117397Skan while (__v != 0); 947117397Skan } 948117397Skan else 949117397Skan { 950117397Skan // Hex. 951117397Skan const bool __uppercase = __flags & ios_base::uppercase; 952132720Skan const int __case_offset = __uppercase ? __num_base::_S_oudigits 953132720Skan : __num_base::_S_odigits; 954132720Skan do 955117397Skan { 956169691Skan *--__buf = __lit[(__v & 0xf) + __case_offset]; 957117397Skan __v >>= 4; 958132720Skan } 959117397Skan while (__v != 0); 960117397Skan } 961169691Skan return __bufend - __buf; 962117397Skan } 963117397Skan 964169691Skan_GLIBCXX_BEGIN_LDBL_NAMESPACE 965169691Skan 966117397Skan template<typename _CharT, typename _OutIter> 967117397Skan void 968117397Skan num_put<_CharT, _OutIter>:: 969132720Skan _M_group_int(const char* __grouping, size_t __grouping_size, _CharT __sep, 970169691Skan ios_base&, _CharT* __new, _CharT* __cs, int& __len) const 971117397Skan { 972169691Skan _CharT* __p = std::__add_grouping(__new, __sep, __grouping, 973169691Skan __grouping_size, __cs, __cs + __len); 974117397Skan __len = __p - __new; 975117397Skan } 976169691Skan 977117397Skan template<typename _CharT, typename _OutIter> 978117397Skan template<typename _ValueT> 979117397Skan _OutIter 980117397Skan num_put<_CharT, _OutIter>:: 981132720Skan _M_insert_int(_OutIter __s, ios_base& __io, _CharT __fill, 982132720Skan _ValueT __v) const 983117397Skan { 984169691Skan using __gnu_cxx::__add_unsigned; 985169691Skan typedef typename __add_unsigned<_ValueT>::__type __unsigned_type; 986169691Skan typedef __numpunct_cache<_CharT> __cache_type; 987132720Skan __use_cache<__cache_type> __uc; 988132720Skan const locale& __loc = __io._M_getloc(); 989132720Skan const __cache_type* __lc = __uc(__loc); 990132720Skan const _CharT* __lit = __lc->_M_atoms_out; 991169691Skan const ios_base::fmtflags __flags = __io.flags(); 992117397Skan 993117397Skan // Long enough to hold hex, dec, and octal representations. 994169691Skan const int __ilen = 5 * sizeof(_ValueT); 995132720Skan _CharT* __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 996117397Skan * __ilen)); 997132720Skan 998117397Skan // [22.2.2.2.2] Stage 1, numeric conversion to character. 999117397Skan // Result is returned right-justified in the buffer. 1000169691Skan const ios_base::fmtflags __basefield = __flags & ios_base::basefield; 1001169691Skan const bool __dec = (__basefield != ios_base::oct 1002169691Skan && __basefield != ios_base::hex); 1003169691Skan const __unsigned_type __u = (__v > 0 || !__dec) ? __v : -__v; 1004169691Skan int __len = __int_to_char(__cs + __ilen, __u, __lit, __flags, __dec); 1005132720Skan __cs += __ilen - __len; 1006132720Skan 1007132720Skan // Add grouping, if necessary. 1008132720Skan if (__lc->_M_use_grouping) 1009117397Skan { 1010169691Skan // Grouping can add (almost) as many separators as the number 1011169691Skan // of digits + space is reserved for numeric base or sign. 1012132720Skan _CharT* __cs2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1013169691Skan * (__len + 1) 1014169691Skan * 2)); 1015132720Skan _M_group_int(__lc->_M_grouping, __lc->_M_grouping_size, 1016169691Skan __lc->_M_thousands_sep, __io, __cs2 + 2, __cs, __len); 1017169691Skan __cs = __cs2 + 2; 1018117397Skan } 1019132720Skan 1020169691Skan // Complete Stage 1, prepend numeric base or sign. 1021169691Skan if (__builtin_expect(__dec, true)) 1022169691Skan { 1023169691Skan // Decimal. 1024228780Spfg if (__v >= 0) 1025169691Skan { 1026169691Skan if (__flags & ios_base::showpos 1027169691Skan && numeric_limits<_ValueT>::is_signed) 1028169691Skan *--__cs = __lit[__num_base::_S_oplus], ++__len; 1029169691Skan } 1030228780Spfg else 1031169691Skan *--__cs = __lit[__num_base::_S_ominus], ++__len; 1032169691Skan } 1033169691Skan else if (__flags & ios_base::showbase && __v) 1034169691Skan { 1035169691Skan if (__basefield == ios_base::oct) 1036169691Skan *--__cs = __lit[__num_base::_S_odigits], ++__len; 1037169691Skan else 1038169691Skan { 1039169691Skan // 'x' or 'X' 1040169691Skan const bool __uppercase = __flags & ios_base::uppercase; 1041169691Skan *--__cs = __lit[__num_base::_S_ox + __uppercase]; 1042169691Skan // '0' 1043169691Skan *--__cs = __lit[__num_base::_S_odigits]; 1044169691Skan __len += 2; 1045169691Skan } 1046169691Skan } 1047169691Skan 1048117397Skan // Pad. 1049132720Skan const streamsize __w = __io.width(); 1050117397Skan if (__w > static_cast<streamsize>(__len)) 1051117397Skan { 1052132720Skan _CharT* __cs3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1053132720Skan * __w)); 1054117397Skan _M_pad(__fill, __w, __io, __cs3, __cs, __len); 1055117397Skan __cs = __cs3; 1056117397Skan } 1057117397Skan __io.width(0); 1058117397Skan 1059117397Skan // [22.2.2.2.2] Stage 4. 1060117397Skan // Write resulting, fully-formatted string to output iterator. 1061132720Skan return std::__write(__s, __cs, __len); 1062132720Skan } 1063117397Skan 1064117397Skan template<typename _CharT, typename _OutIter> 1065117397Skan void 1066117397Skan num_put<_CharT, _OutIter>:: 1067132720Skan _M_group_float(const char* __grouping, size_t __grouping_size, 1068132720Skan _CharT __sep, const _CharT* __p, _CharT* __new, 1069132720Skan _CharT* __cs, int& __len) const 1070117397Skan { 1071132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 1072132720Skan // 282. What types does numpunct grouping refer to? 1073132720Skan // Add grouping, if necessary. 1074132720Skan const int __declen = __p ? __p - __cs : __len; 1075169691Skan _CharT* __p2 = std::__add_grouping(__new, __sep, __grouping, 1076169691Skan __grouping_size, 1077169691Skan __cs, __cs + __declen); 1078132720Skan 1079117397Skan // Tack on decimal part. 1080117397Skan int __newlen = __p2 - __new; 1081117397Skan if (__p) 1082117397Skan { 1083117397Skan char_traits<_CharT>::copy(__p2, __p, __len - __declen); 1084117397Skan __newlen += __len - __declen; 1085132720Skan } 1086117397Skan __len = __newlen; 1087117397Skan } 1088117397Skan 1089169691Skan // The following code uses vsnprintf (or vsprintf(), when 1090132720Skan // _GLIBCXX_USE_C99 is not defined) to convert floating point values 1091117397Skan // for insertion into a stream. An optimization would be to replace 1092117397Skan // them with code that works directly on a wide buffer and then use 1093117397Skan // __pad to do the padding. It would be good to replace them anyway 1094117397Skan // to gain back the efficiency that C++ provides by knowing up front 1095117397Skan // the type of the values to insert. Also, sprintf is dangerous 1096117397Skan // since may lead to accidental buffer overruns. This 1097117397Skan // implementation follows the C++ standard fairly directly as 109897403Sobrien // outlined in 22.2.2.2 [lib.locale.num.put] 109997403Sobrien template<typename _CharT, typename _OutIter> 110097403Sobrien template<typename _ValueT> 110197403Sobrien _OutIter 110297403Sobrien num_put<_CharT, _OutIter>:: 1103132720Skan _M_insert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod, 110497403Sobrien _ValueT __v) const 110597403Sobrien { 1106169691Skan typedef __numpunct_cache<_CharT> __cache_type; 1107132720Skan __use_cache<__cache_type> __uc; 1108132720Skan const locale& __loc = __io._M_getloc(); 1109132720Skan const __cache_type* __lc = __uc(__loc); 1110117397Skan 1111117397Skan // Use default precision if out of range. 1112169691Skan const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision(); 111397403Sobrien 1114132720Skan const int __max_digits = numeric_limits<_ValueT>::digits10; 1115117397Skan 1116117397Skan // [22.2.2.2.2] Stage 1, numeric conversion to character. 1117117397Skan int __len; 111897403Sobrien // Long enough for the max format spec. 111997403Sobrien char __fbuf[16]; 1120169691Skan __num_base::_S_format_float(__io, __fbuf, __mod); 112197403Sobrien 1122132720Skan#ifdef _GLIBCXX_USE_C99 1123132720Skan // First try a buffer perhaps big enough (most probably sufficient 1124117397Skan // for non-ios_base::fixed outputs) 112597403Sobrien int __cs_size = __max_digits * 3; 112697403Sobrien char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1127169691Skan __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, 1128169691Skan __fbuf, __prec, __v); 112997403Sobrien 113097403Sobrien // If the buffer was not large enough, try again with the correct size. 113197403Sobrien if (__len >= __cs_size) 113297403Sobrien { 1133132720Skan __cs_size = __len + 1; 113497403Sobrien __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1135169691Skan __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, 1136169691Skan __fbuf, __prec, __v); 113797403Sobrien } 113897403Sobrien#else 113997403Sobrien // Consider the possibility of long ios_base::fixed outputs 114097403Sobrien const bool __fixed = __io.flags() & ios_base::fixed; 114197403Sobrien const int __max_exp = numeric_limits<_ValueT>::max_exponent10; 1142117397Skan 1143132720Skan // The size of the output string is computed as follows. 1144132720Skan // ios_base::fixed outputs may need up to __max_exp + 1 chars 1145132720Skan // for the integer part + __prec chars for the fractional part 1146132720Skan // + 3 chars for sign, decimal point, '\0'. On the other hand, 1147132720Skan // for non-fixed outputs __max_digits * 2 + __prec chars are 1148132720Skan // largely sufficient. 1149132720Skan const int __cs_size = __fixed ? __max_exp + __prec + 4 1150132720Skan : __max_digits * 2 + __prec; 115197403Sobrien char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1152169691Skan __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, __fbuf, 1153169691Skan __prec, __v); 115497403Sobrien#endif 115597403Sobrien 1156169691Skan // [22.2.2.2.2] Stage 2, convert to char_type, using correct 1157169691Skan // numpunct.decimal_point() values for '.' and adding grouping. 1158169691Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 1159169691Skan 1160169691Skan _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1161169691Skan * __len)); 1162169691Skan __ctype.widen(__cs, __cs + __len, __ws); 1163169691Skan 1164169691Skan // Replace decimal point. 1165169691Skan _CharT* __wp = 0; 1166169691Skan const char* __p = char_traits<char>::find(__cs, __len, '.'); 1167169691Skan if (__p) 1168169691Skan { 1169169691Skan __wp = __ws + (__p - __cs); 1170169691Skan *__wp = __lc->_M_decimal_point; 1171169691Skan } 1172169691Skan 1173169691Skan // Add grouping, if necessary. 1174169691Skan // N.B. Make sure to not group things like 2e20, i.e., no decimal 1175169691Skan // point, scientific notation. 1176169691Skan if (__lc->_M_use_grouping 1177169691Skan && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9' 1178169691Skan && __cs[1] >= '0' && __cs[2] >= '0'))) 1179169691Skan { 1180169691Skan // Grouping can add (almost) as many separators as the 1181169691Skan // number of digits, but no more. 1182169691Skan _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1183169691Skan * __len * 2)); 1184169691Skan 1185169691Skan streamsize __off = 0; 1186169691Skan if (__cs[0] == '-' || __cs[0] == '+') 1187169691Skan { 1188169691Skan __off = 1; 1189169691Skan __ws2[0] = __ws[0]; 1190169691Skan __len -= 1; 1191169691Skan } 1192169691Skan 1193169691Skan _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size, 1194169691Skan __lc->_M_thousands_sep, __wp, __ws2 + __off, 1195169691Skan __ws + __off, __len); 1196169691Skan __len += __off; 1197169691Skan 1198169691Skan __ws = __ws2; 1199169691Skan } 1200117397Skan 1201169691Skan // Pad. 1202169691Skan const streamsize __w = __io.width(); 1203169691Skan if (__w > static_cast<streamsize>(__len)) 1204169691Skan { 1205169691Skan _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1206169691Skan * __w)); 1207169691Skan _M_pad(__fill, __w, __io, __ws3, __ws, __len); 1208169691Skan __ws = __ws3; 1209169691Skan } 1210169691Skan __io.width(0); 1211169691Skan 1212169691Skan // [22.2.2.2.2] Stage 4. 1213169691Skan // Write resulting, fully-formatted string to output iterator. 1214169691Skan return std::__write(__s, __ws, __len); 1215117397Skan } 1216169691Skan 121797403Sobrien template<typename _CharT, typename _OutIter> 121897403Sobrien _OutIter 121997403Sobrien num_put<_CharT, _OutIter>:: 122097403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const 122197403Sobrien { 1222132720Skan const ios_base::fmtflags __flags = __io.flags(); 122397403Sobrien if ((__flags & ios_base::boolalpha) == 0) 122497403Sobrien { 1225146897Skan const long __l = __v; 1226146897Skan __s = _M_insert_int(__s, __io, __fill, __l); 122797403Sobrien } 122897403Sobrien else 122997403Sobrien { 1230169691Skan typedef __numpunct_cache<_CharT> __cache_type; 1231132720Skan __use_cache<__cache_type> __uc; 1232132720Skan const locale& __loc = __io._M_getloc(); 1233132720Skan const __cache_type* __lc = __uc(__loc); 1234117397Skan 1235132720Skan const _CharT* __name = __v ? __lc->_M_truename 1236132720Skan : __lc->_M_falsename; 1237132720Skan int __len = __v ? __lc->_M_truename_size 1238132720Skan : __lc->_M_falsename_size; 1239117397Skan 1240132720Skan const streamsize __w = __io.width(); 1241117397Skan if (__w > static_cast<streamsize>(__len)) 1242117397Skan { 1243132720Skan _CharT* __cs 1244132720Skan = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) 1245132720Skan * __w)); 1246132720Skan _M_pad(__fill, __w, __io, __cs, __name, __len); 1247132720Skan __name = __cs; 1248117397Skan } 1249117397Skan __io.width(0); 1250132720Skan __s = std::__write(__s, __name, __len); 125197403Sobrien } 125297403Sobrien return __s; 125397403Sobrien } 125497403Sobrien 125597403Sobrien template<typename _CharT, typename _OutIter> 125697403Sobrien _OutIter 125797403Sobrien num_put<_CharT, _OutIter>:: 125897403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const 1259132720Skan { return _M_insert_int(__s, __io, __fill, __v); } 126097403Sobrien 126197403Sobrien template<typename _CharT, typename _OutIter> 126297403Sobrien _OutIter 126397403Sobrien num_put<_CharT, _OutIter>:: 126497403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, 126597403Sobrien unsigned long __v) const 1266132720Skan { return _M_insert_int(__s, __io, __fill, __v); } 126797403Sobrien 1268132720Skan#ifdef _GLIBCXX_USE_LONG_LONG 126997403Sobrien template<typename _CharT, typename _OutIter> 127097403Sobrien _OutIter 127197403Sobrien num_put<_CharT, _OutIter>:: 1272169691Skan do_put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const 1273169691Skan { return _M_insert_int(__s, __io, __fill, __v); } 127497403Sobrien 127597403Sobrien template<typename _CharT, typename _OutIter> 127697403Sobrien _OutIter 127797403Sobrien num_put<_CharT, _OutIter>:: 127897403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, 127997403Sobrien unsigned long long __v) const 1280132720Skan { return _M_insert_int(__s, __io, __fill, __v); } 128197403Sobrien#endif 128297403Sobrien 128397403Sobrien template<typename _CharT, typename _OutIter> 128497403Sobrien _OutIter 128597403Sobrien num_put<_CharT, _OutIter>:: 128697403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const 1287132720Skan { return _M_insert_float(__s, __io, __fill, char(), __v); } 128897403Sobrien 1289169691Skan#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 129097403Sobrien template<typename _CharT, typename _OutIter> 129197403Sobrien _OutIter 129297403Sobrien num_put<_CharT, _OutIter>:: 1293169691Skan __do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const 1294169691Skan { return _M_insert_float(__s, __io, __fill, char(), __v); } 1295169691Skan#endif 1296169691Skan 1297169691Skan template<typename _CharT, typename _OutIter> 1298169691Skan _OutIter 1299169691Skan num_put<_CharT, _OutIter>:: 1300132720Skan do_put(iter_type __s, ios_base& __io, char_type __fill, 130197403Sobrien long double __v) const 1302132720Skan { return _M_insert_float(__s, __io, __fill, 'L', __v); } 130397403Sobrien 130497403Sobrien template<typename _CharT, typename _OutIter> 130597403Sobrien _OutIter 130697403Sobrien num_put<_CharT, _OutIter>:: 130797403Sobrien do_put(iter_type __s, ios_base& __io, char_type __fill, 130897403Sobrien const void* __v) const 130997403Sobrien { 1310132720Skan const ios_base::fmtflags __flags = __io.flags(); 1311146897Skan const ios_base::fmtflags __fmt = ~(ios_base::basefield 1312132720Skan | ios_base::uppercase 1313132720Skan | ios_base::internal); 131497403Sobrien __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase)); 1315132720Skan 1316236829Spfg typedef __gnu_cxx::__conditional_type<(sizeof(const void*) 1317236829Spfg <= sizeof(unsigned long)), 1318236829Spfg unsigned long, unsigned long long>::__type _UIntPtrType; 1319236829Spfg 1320132720Skan __s = _M_insert_int(__s, __io, __fill, 1321236829Spfg reinterpret_cast<_UIntPtrType>(__v)); 1322132720Skan __io.flags(__flags); 132397403Sobrien return __s; 132497403Sobrien } 132597403Sobrien 132697403Sobrien template<typename _CharT, typename _InIter> 1327132720Skan template<bool _Intl> 1328132720Skan _InIter 1329132720Skan money_get<_CharT, _InIter>:: 1330132720Skan _M_extract(iter_type __beg, iter_type __end, ios_base& __io, 1331132720Skan ios_base::iostate& __err, string& __units) const 1332132720Skan { 1333132720Skan typedef char_traits<_CharT> __traits_type; 1334132720Skan typedef typename string_type::size_type size_type; 1335132720Skan typedef money_base::part part; 1336169691Skan typedef __moneypunct_cache<_CharT, _Intl> __cache_type; 1337132720Skan 1338132720Skan const locale& __loc = __io._M_getloc(); 1339132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 134097403Sobrien 1341132720Skan __use_cache<__cache_type> __uc; 1342132720Skan const __cache_type* __lc = __uc(__loc); 1343132720Skan const char_type* __lit = __lc->_M_atoms; 134497403Sobrien 1345132720Skan // Deduced sign. 1346132720Skan bool __negative = false; 1347132720Skan // Sign size. 1348132720Skan size_type __sign_size = 0; 1349132720Skan // True if sign is mandatory. 1350132720Skan const bool __mandatory_sign = (__lc->_M_positive_sign_size 1351132720Skan && __lc->_M_negative_sign_size); 1352132720Skan // String of grouping info from thousands_sep plucked from __units. 1353132720Skan string __grouping_tmp; 1354132720Skan if (__lc->_M_use_grouping) 1355132720Skan __grouping_tmp.reserve(32); 1356132720Skan // Last position before the decimal point. 1357132720Skan int __last_pos = 0; 1358132720Skan // Separator positions, then, possibly, fractional digits. 1359132720Skan int __n = 0; 1360132720Skan // If input iterator is in a valid state. 1361132720Skan bool __testvalid = true; 1362132720Skan // Flag marking when a decimal point is found. 1363132720Skan bool __testdecfound = false; 136497403Sobrien 1365132720Skan // The tentative returned string is stored here. 1366132720Skan string __res; 1367132720Skan __res.reserve(32); 136897403Sobrien 1369132720Skan const char_type* __lit_zero = __lit + money_base::_S_zero; 1370169691Skan const money_base::pattern __p = __lc->_M_neg_format; 1371132720Skan for (int __i = 0; __i < 4 && __testvalid; ++__i) 1372132720Skan { 1373132720Skan const part __which = static_cast<part>(__p.field[__i]); 1374132720Skan switch (__which) 1375132720Skan { 1376132720Skan case money_base::symbol: 1377132720Skan // According to 22.2.6.1.2, p2, symbol is required 1378132720Skan // if (__io.flags() & ios_base::showbase), otherwise 1379132720Skan // is optional and consumed only if other characters 1380132720Skan // are needed to complete the format. 1381132720Skan if (__io.flags() & ios_base::showbase || __sign_size > 1 1382132720Skan || __i == 0 1383132720Skan || (__i == 1 && (__mandatory_sign 1384132720Skan || (static_cast<part>(__p.field[0]) 1385132720Skan == money_base::sign) 1386132720Skan || (static_cast<part>(__p.field[2]) 1387132720Skan == money_base::space))) 1388132720Skan || (__i == 2 && ((static_cast<part>(__p.field[3]) 1389132720Skan == money_base::value) 1390132720Skan || __mandatory_sign 1391132720Skan && (static_cast<part>(__p.field[3]) 1392132720Skan == money_base::sign)))) 139397403Sobrien { 1394132720Skan const size_type __len = __lc->_M_curr_symbol_size; 1395132720Skan size_type __j = 0; 1396132720Skan for (; __beg != __end && __j < __len 1397132720Skan && *__beg == __lc->_M_curr_symbol[__j]; 1398132720Skan ++__beg, ++__j); 1399132720Skan if (__j != __len 1400132720Skan && (__j || __io.flags() & ios_base::showbase)) 140197403Sobrien __testvalid = false; 140297403Sobrien } 1403132720Skan break; 1404132720Skan case money_base::sign: 1405132720Skan // Sign might not exist, or be more than one character long. 1406132720Skan if (__lc->_M_positive_sign_size && __beg != __end 1407132720Skan && *__beg == __lc->_M_positive_sign[0]) 1408132720Skan { 1409132720Skan __sign_size = __lc->_M_positive_sign_size; 1410132720Skan ++__beg; 1411132720Skan } 1412132720Skan else if (__lc->_M_negative_sign_size && __beg != __end 1413132720Skan && *__beg == __lc->_M_negative_sign[0]) 1414132720Skan { 1415132720Skan __negative = true; 1416132720Skan __sign_size = __lc->_M_negative_sign_size; 1417132720Skan ++__beg; 1418132720Skan } 1419132720Skan else if (__lc->_M_positive_sign_size 1420132720Skan && !__lc->_M_negative_sign_size) 1421132720Skan // "... if no sign is detected, the result is given the sign 1422132720Skan // that corresponds to the source of the empty string" 1423132720Skan __negative = true; 1424132720Skan else if (__mandatory_sign) 1425132720Skan __testvalid = false; 1426132720Skan break; 1427132720Skan case money_base::value: 1428132720Skan // Extract digits, remove and stash away the 1429132720Skan // grouping of found thousands separators. 1430132720Skan for (; __beg != __end; ++__beg) 1431169691Skan { 1432169691Skan const char_type __c = *__beg; 1433169691Skan const char_type* __q = __traits_type::find(__lit_zero, 1434169691Skan 10, __c); 1435169691Skan if (__q != 0) 1436169691Skan { 1437169691Skan __res += money_base::_S_atoms[__q - __lit]; 1438169691Skan ++__n; 1439169691Skan } 1440169691Skan else if (__c == __lc->_M_decimal_point 1441169691Skan && !__testdecfound) 1442169691Skan { 1443169691Skan __last_pos = __n; 1444169691Skan __n = 0; 1445169691Skan __testdecfound = true; 1446169691Skan } 1447169691Skan else if (__lc->_M_use_grouping 1448169691Skan && __c == __lc->_M_thousands_sep 1449169691Skan && !__testdecfound) 1450169691Skan { 1451169691Skan if (__n) 1452169691Skan { 1453169691Skan // Mark position for later analysis. 1454169691Skan __grouping_tmp += static_cast<char>(__n); 1455169691Skan __n = 0; 1456169691Skan } 1457169691Skan else 1458169691Skan { 1459169691Skan __testvalid = false; 1460169691Skan break; 1461169691Skan } 1462169691Skan } 1463169691Skan else 1464169691Skan break; 1465169691Skan } 1466132720Skan if (__res.empty()) 1467132720Skan __testvalid = false; 1468132720Skan break; 1469132720Skan case money_base::space: 1470132720Skan // At least one space is required. 1471132720Skan if (__beg != __end && __ctype.is(ctype_base::space, *__beg)) 1472132720Skan ++__beg; 1473132720Skan else 1474132720Skan __testvalid = false; 1475132720Skan case money_base::none: 1476132720Skan // Only if not at the end of the pattern. 1477132720Skan if (__i != 3) 1478132720Skan for (; __beg != __end 1479132720Skan && __ctype.is(ctype_base::space, *__beg); ++__beg); 1480132720Skan break; 1481132720Skan } 1482132720Skan } 148397403Sobrien 1484132720Skan // Need to get the rest of the sign characters, if they exist. 1485132720Skan if (__sign_size > 1 && __testvalid) 1486132720Skan { 1487132720Skan const char_type* __sign = __negative ? __lc->_M_negative_sign 1488132720Skan : __lc->_M_positive_sign; 1489132720Skan size_type __i = 1; 1490132720Skan for (; __beg != __end && __i < __sign_size 1491132720Skan && *__beg == __sign[__i]; ++__beg, ++__i); 1492132720Skan 1493132720Skan if (__i != __sign_size) 1494132720Skan __testvalid = false; 1495132720Skan } 149697403Sobrien 1497132720Skan if (__testvalid) 1498132720Skan { 1499132720Skan // Strip leading zeros. 1500132720Skan if (__res.size() > 1) 1501132720Skan { 1502132720Skan const size_type __first = __res.find_first_not_of('0'); 1503132720Skan const bool __only_zeros = __first == string::npos; 1504132720Skan if (__first) 1505132720Skan __res.erase(0, __only_zeros ? __res.size() - 1 : __first); 1506132720Skan } 150797403Sobrien 1508132720Skan // 22.2.6.1.2, p4 1509132720Skan if (__negative && __res[0] != '0') 1510132720Skan __res.insert(__res.begin(), '-'); 1511132720Skan 1512132720Skan // Test for grouping fidelity. 1513132720Skan if (__grouping_tmp.size()) 1514132720Skan { 1515132720Skan // Add the ending grouping. 1516132720Skan __grouping_tmp += static_cast<char>(__testdecfound ? __last_pos 1517132720Skan : __n); 1518132720Skan if (!std::__verify_grouping(__lc->_M_grouping, 1519132720Skan __lc->_M_grouping_size, 1520132720Skan __grouping_tmp)) 1521169691Skan __err |= ios_base::failbit; 1522132720Skan } 1523132720Skan 1524132720Skan // Iff not enough digits were supplied after the decimal-point. 1525132720Skan if (__testdecfound && __lc->_M_frac_digits > 0 1526132720Skan && __n != __lc->_M_frac_digits) 1527132720Skan __testvalid = false; 1528132720Skan } 1529132720Skan 1530132720Skan // Iff valid sequence is not recognized. 1531132720Skan if (!__testvalid) 1532132720Skan __err |= ios_base::failbit; 1533132720Skan else 1534132720Skan __units.swap(__res); 1535132720Skan 1536169691Skan // Iff no more characters are available. 1537169691Skan if (__beg == __end) 1538169691Skan __err |= ios_base::eofbit; 1539132720Skan return __beg; 1540132720Skan } 154197403Sobrien 1542169691Skan#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 1543132720Skan template<typename _CharT, typename _InIter> 1544132720Skan _InIter 1545132720Skan money_get<_CharT, _InIter>:: 1546169691Skan __do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 1547169691Skan ios_base::iostate& __err, double& __units) const 1548169691Skan { 1549169691Skan string __str; 1550169691Skan __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str) 1551169691Skan : _M_extract<false>(__beg, __end, __io, __err, __str); 1552169691Skan std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale()); 1553169691Skan return __beg; 1554169691Skan } 1555169691Skan#endif 1556169691Skan 1557169691Skan template<typename _CharT, typename _InIter> 1558169691Skan _InIter 1559169691Skan money_get<_CharT, _InIter>:: 1560132720Skan do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 1561132720Skan ios_base::iostate& __err, long double& __units) const 1562132720Skan { 1563132720Skan string __str; 1564169691Skan __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str) 1565169691Skan : _M_extract<false>(__beg, __end, __io, __err, __str); 1566132720Skan std::__convert_to_v(__str.c_str(), __units, __err, _S_get_c_locale()); 1567132720Skan return __beg; 1568132720Skan } 1569132720Skan 1570132720Skan template<typename _CharT, typename _InIter> 1571132720Skan _InIter 1572132720Skan money_get<_CharT, _InIter>:: 1573132720Skan do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, 1574169691Skan ios_base::iostate& __err, string_type& __digits) const 1575132720Skan { 1576132720Skan typedef typename string::size_type size_type; 1577132720Skan 1578132720Skan const locale& __loc = __io._M_getloc(); 1579132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 1580132720Skan 1581132720Skan string __str; 1582169691Skan __beg = __intl ? _M_extract<true>(__beg, __end, __io, __err, __str) 1583169691Skan : _M_extract<false>(__beg, __end, __io, __err, __str); 1584132720Skan const size_type __len = __str.size(); 1585132720Skan if (__len) 158697403Sobrien { 1587169691Skan __digits.resize(__len); 1588169691Skan __ctype.widen(__str.data(), __str.data() + __len, &__digits[0]); 158997403Sobrien } 1590169691Skan return __beg; 1591132720Skan } 159297403Sobrien 1593132720Skan template<typename _CharT, typename _OutIter> 1594132720Skan template<bool _Intl> 1595132720Skan _OutIter 1596132720Skan money_put<_CharT, _OutIter>:: 1597132720Skan _M_insert(iter_type __s, ios_base& __io, char_type __fill, 1598132720Skan const string_type& __digits) const 1599132720Skan { 1600132720Skan typedef typename string_type::size_type size_type; 1601132720Skan typedef money_base::part part; 1602169691Skan typedef __moneypunct_cache<_CharT, _Intl> __cache_type; 1603132720Skan 1604132720Skan const locale& __loc = __io._M_getloc(); 1605132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 160697403Sobrien 1607132720Skan __use_cache<__cache_type> __uc; 1608132720Skan const __cache_type* __lc = __uc(__loc); 1609132720Skan const char_type* __lit = __lc->_M_atoms; 161097403Sobrien 1611132720Skan // Determine if negative or positive formats are to be used, and 1612132720Skan // discard leading negative_sign if it is present. 1613132720Skan const char_type* __beg = __digits.data(); 1614132720Skan 1615132720Skan money_base::pattern __p; 1616132720Skan const char_type* __sign; 1617132720Skan size_type __sign_size; 1618169691Skan if (!(*__beg == __lit[money_base::_S_minus])) 1619132720Skan { 1620132720Skan __p = __lc->_M_pos_format; 1621132720Skan __sign = __lc->_M_positive_sign; 1622132720Skan __sign_size = __lc->_M_positive_sign_size; 1623132720Skan } 1624132720Skan else 1625132720Skan { 1626132720Skan __p = __lc->_M_neg_format; 1627132720Skan __sign = __lc->_M_negative_sign; 1628132720Skan __sign_size = __lc->_M_negative_sign_size; 1629132720Skan if (__digits.size()) 1630132720Skan ++__beg; 1631132720Skan } 1632132720Skan 1633132720Skan // Look for valid numbers in the ctype facet within input digits. 1634132720Skan size_type __len = __ctype.scan_not(ctype_base::digit, __beg, 1635132720Skan __beg + __digits.size()) - __beg; 1636132720Skan if (__len) 1637132720Skan { 1638132720Skan // Assume valid input, and attempt to format. 1639132720Skan // Break down input numbers into base components, as follows: 1640132720Skan // final_value = grouped units + (decimal point) + (digits) 1641132720Skan string_type __value; 1642132720Skan __value.reserve(2 * __len); 1643132720Skan 1644132720Skan // Add thousands separators to non-decimal digits, per 1645132720Skan // grouping rules. 1646169691Skan long __paddec = __len - __lc->_M_frac_digits; 1647132720Skan if (__paddec > 0) 1648132720Skan { 1649132720Skan if (__lc->_M_frac_digits < 0) 1650132720Skan __paddec = __len; 1651132720Skan if (__lc->_M_grouping_size) 1652132720Skan { 1653169691Skan __value.assign(2 * __paddec, char_type()); 1654169691Skan _CharT* __vend = 1655169691Skan std::__add_grouping(&__value[0], __lc->_M_thousands_sep, 1656132720Skan __lc->_M_grouping, 1657132720Skan __lc->_M_grouping_size, 1658132720Skan __beg, __beg + __paddec); 1659169691Skan __value.erase(__vend - &__value[0]); 1660132720Skan } 1661132720Skan else 1662132720Skan __value.assign(__beg, __paddec); 1663132720Skan } 1664132720Skan 1665132720Skan // Deal with decimal point, decimal digits. 1666132720Skan if (__lc->_M_frac_digits > 0) 1667132720Skan { 1668132720Skan __value += __lc->_M_decimal_point; 1669132720Skan if (__paddec >= 0) 1670132720Skan __value.append(__beg + __paddec, __lc->_M_frac_digits); 1671132720Skan else 1672132720Skan { 1673132720Skan // Have to pad zeros in the decimal position. 1674132720Skan __value.append(-__paddec, __lit[money_base::_S_zero]); 1675132720Skan __value.append(__beg, __len); 1676132720Skan } 1677132720Skan } 1678132720Skan 1679132720Skan // Calculate length of resulting string. 1680132720Skan const ios_base::fmtflags __f = __io.flags() 1681132720Skan & ios_base::adjustfield; 1682132720Skan __len = __value.size() + __sign_size; 1683132720Skan __len += ((__io.flags() & ios_base::showbase) 1684132720Skan ? __lc->_M_curr_symbol_size : 0); 1685132720Skan 1686132720Skan string_type __res; 1687132720Skan __res.reserve(2 * __len); 1688132720Skan 1689132720Skan const size_type __width = static_cast<size_type>(__io.width()); 1690132720Skan const bool __testipad = (__f == ios_base::internal 1691132720Skan && __len < __width); 1692132720Skan // Fit formatted digits into the required pattern. 1693132720Skan for (int __i = 0; __i < 4; ++__i) 1694132720Skan { 1695132720Skan const part __which = static_cast<part>(__p.field[__i]); 1696132720Skan switch (__which) 1697132720Skan { 1698132720Skan case money_base::symbol: 1699132720Skan if (__io.flags() & ios_base::showbase) 1700132720Skan __res.append(__lc->_M_curr_symbol, 1701132720Skan __lc->_M_curr_symbol_size); 1702132720Skan break; 1703132720Skan case money_base::sign: 1704132720Skan // Sign might not exist, or be more than one 1705132720Skan // charater long. In that case, add in the rest 1706132720Skan // below. 1707132720Skan if (__sign_size) 1708132720Skan __res += __sign[0]; 1709132720Skan break; 1710132720Skan case money_base::value: 1711132720Skan __res += __value; 1712132720Skan break; 1713132720Skan case money_base::space: 1714132720Skan // At least one space is required, but if internal 1715132720Skan // formatting is required, an arbitrary number of 1716132720Skan // fill spaces will be necessary. 1717132720Skan if (__testipad) 1718132720Skan __res.append(__width - __len, __fill); 1719132720Skan else 1720132720Skan __res += __fill; 1721132720Skan break; 1722132720Skan case money_base::none: 1723132720Skan if (__testipad) 1724132720Skan __res.append(__width - __len, __fill); 1725132720Skan break; 1726132720Skan } 1727132720Skan } 1728132720Skan 1729132720Skan // Special case of multi-part sign parts. 1730132720Skan if (__sign_size > 1) 1731132720Skan __res.append(__sign + 1, __sign_size - 1); 1732132720Skan 1733132720Skan // Pad, if still necessary. 1734132720Skan __len = __res.size(); 1735132720Skan if (__width > __len) 1736132720Skan { 1737132720Skan if (__f == ios_base::left) 1738132720Skan // After. 1739132720Skan __res.append(__width - __len, __fill); 1740132720Skan else 1741132720Skan // Before. 1742132720Skan __res.insert(0, __width - __len, __fill); 1743132720Skan __len = __width; 1744132720Skan } 1745132720Skan 1746132720Skan // Write resulting, fully-formatted string to output iterator. 1747132720Skan __s = std::__write(__s, __res.data(), __len); 1748132720Skan } 1749132720Skan __io.width(0); 1750132720Skan return __s; 1751132720Skan } 1752169691Skan 1753169691Skan#if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__ 175497403Sobrien template<typename _CharT, typename _OutIter> 175597403Sobrien _OutIter 175697403Sobrien money_put<_CharT, _OutIter>:: 1757169691Skan __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, 1758169691Skan double __units) const 1759169691Skan { return this->do_put(__s, __intl, __io, __fill, (long double) __units); } 1760169691Skan#endif 1761169691Skan 1762169691Skan template<typename _CharT, typename _OutIter> 1763169691Skan _OutIter 1764169691Skan money_put<_CharT, _OutIter>:: 176597403Sobrien do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, 176697403Sobrien long double __units) const 1767132720Skan { 176897403Sobrien const locale __loc = __io.getloc(); 176997403Sobrien const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 1770132720Skan#ifdef _GLIBCXX_USE_C99 177197403Sobrien // First try a buffer perhaps big enough. 177297403Sobrien int __cs_size = 64; 177397403Sobrien char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1774132720Skan // _GLIBCXX_RESOLVE_LIB_DEFECTS 1775132720Skan // 328. Bad sprintf format modifier in money_put<>::do_put() 1776169691Skan int __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, 1777169691Skan "%.*Lf", 0, __units); 177897403Sobrien // If the buffer was not large enough, try again with the correct size. 177997403Sobrien if (__len >= __cs_size) 178097403Sobrien { 178197403Sobrien __cs_size = __len + 1; 178297403Sobrien __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1783169691Skan __len = std::__convert_from_v(_S_get_c_locale(), __cs, __cs_size, 1784169691Skan "%.*Lf", 0, __units); 178597403Sobrien } 178697403Sobrien#else 1787132720Skan // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'. 1788132720Skan const int __cs_size = numeric_limits<long double>::max_exponent10 + 3; 178997403Sobrien char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); 1790169691Skan int __len = std::__convert_from_v(_S_get_c_locale(), __cs, 0, "%.*Lf", 1791169691Skan 0, __units); 179297403Sobrien#endif 1793169691Skan string_type __digits(__len, char_type()); 1794169691Skan __ctype.widen(__cs, __cs + __len, &__digits[0]); 1795132720Skan return __intl ? _M_insert<true>(__s, __io, __fill, __digits) 1796132720Skan : _M_insert<false>(__s, __io, __fill, __digits); 179797403Sobrien } 179897403Sobrien 179997403Sobrien template<typename _CharT, typename _OutIter> 180097403Sobrien _OutIter 180197403Sobrien money_put<_CharT, _OutIter>:: 180297403Sobrien do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, 180397403Sobrien const string_type& __digits) const 1804132720Skan { return __intl ? _M_insert<true>(__s, __io, __fill, __digits) 1805132720Skan : _M_insert<false>(__s, __io, __fill, __digits); } 180697403Sobrien 1807169691Skan_GLIBCXX_END_LDBL_NAMESPACE 180897403Sobrien 180997403Sobrien // NB: Not especially useful. Without an ios_base object or some 181097403Sobrien // kind of locale reference, we are left clawing at the air where 181197403Sobrien // the side of the mountain used to be... 181297403Sobrien template<typename _CharT, typename _InIter> 181397403Sobrien time_base::dateorder 181497403Sobrien time_get<_CharT, _InIter>::do_date_order() const 181597403Sobrien { return time_base::no_order; } 181697403Sobrien 1817132720Skan // Expand a strftime format string and parse it. E.g., do_get_date() may 1818132720Skan // pass %m/%d/%Y => extracted characters. 181997403Sobrien template<typename _CharT, typename _InIter> 1820132720Skan _InIter 182197403Sobrien time_get<_CharT, _InIter>:: 1822132720Skan _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io, 1823132720Skan ios_base::iostate& __err, tm* __tm, 182497403Sobrien const _CharT* __format) const 1825132720Skan { 1826132720Skan const locale& __loc = __io._M_getloc(); 1827132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 1828132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 1829132720Skan const size_t __len = char_traits<_CharT>::length(__format); 183097403Sobrien 1831169691Skan ios_base::iostate __tmperr = ios_base::goodbit; 1832169691Skan for (size_t __i = 0; __beg != __end && __i < __len && !__tmperr; ++__i) 183397403Sobrien { 1834132720Skan if (__ctype.narrow(__format[__i], 0) == '%') 183597403Sobrien { 183697403Sobrien // Verify valid formatting code, attempt to extract. 1837132720Skan char __c = __ctype.narrow(__format[++__i], 0); 1838132720Skan int __mem = 0; 183997403Sobrien if (__c == 'E' || __c == 'O') 1840132720Skan __c = __ctype.narrow(__format[++__i], 0); 184197403Sobrien switch (__c) 184297403Sobrien { 184397403Sobrien const char* __cs; 184497403Sobrien _CharT __wcs[10]; 184597403Sobrien case 'a': 184697403Sobrien // Abbreviated weekday name [tm_wday] 184797403Sobrien const char_type* __days1[7]; 184897403Sobrien __tp._M_days_abbreviated(__days1); 1849132720Skan __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 1850169691Skan 7, __io, __tmperr); 185197403Sobrien break; 185297403Sobrien case 'A': 185397403Sobrien // Weekday name [tm_wday]. 185497403Sobrien const char_type* __days2[7]; 185597403Sobrien __tp._M_days(__days2); 1856132720Skan __beg = _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 1857169691Skan 7, __io, __tmperr); 185897403Sobrien break; 185997403Sobrien case 'h': 186097403Sobrien case 'b': 186197403Sobrien // Abbreviated month name [tm_mon] 186297403Sobrien const char_type* __months1[12]; 186397403Sobrien __tp._M_months_abbreviated(__months1); 1864132720Skan __beg = _M_extract_name(__beg, __end, __tm->tm_mon, 1865169691Skan __months1, 12, __io, __tmperr); 186697403Sobrien break; 186797403Sobrien case 'B': 186897403Sobrien // Month name [tm_mon]. 186997403Sobrien const char_type* __months2[12]; 187097403Sobrien __tp._M_months(__months2); 1871132720Skan __beg = _M_extract_name(__beg, __end, __tm->tm_mon, 1872169691Skan __months2, 12, __io, __tmperr); 187397403Sobrien break; 187497403Sobrien case 'c': 187597403Sobrien // Default time and date representation. 187697403Sobrien const char_type* __dt[2]; 187797403Sobrien __tp._M_date_time_formats(__dt); 1878169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1879132720Skan __tm, __dt[0]); 188097403Sobrien break; 188197403Sobrien case 'd': 188297403Sobrien // Day [01, 31]. [tm_mday] 1883132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2, 1884169691Skan __io, __tmperr); 188597403Sobrien break; 1886132720Skan case 'e': 1887132720Skan // Day [1, 31], with single digits preceded by 1888132720Skan // space. [tm_mday] 1889132720Skan if (__ctype.is(ctype_base::space, *__beg)) 1890132720Skan __beg = _M_extract_num(++__beg, __end, __tm->tm_mday, 1, 9, 1891169691Skan 1, __io, __tmperr); 1892132720Skan else 1893132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_mday, 10, 31, 1894169691Skan 2, __io, __tmperr); 1895132720Skan break; 189697403Sobrien case 'D': 189797403Sobrien // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year] 189897403Sobrien __cs = "%m/%d/%y"; 189997403Sobrien __ctype.widen(__cs, __cs + 9, __wcs); 1900169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1901132720Skan __tm, __wcs); 190297403Sobrien break; 190397403Sobrien case 'H': 190497403Sobrien // Hour [00, 23]. [tm_hour] 1905132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2, 1906169691Skan __io, __tmperr); 190797403Sobrien break; 190897403Sobrien case 'I': 190997403Sobrien // Hour [01, 12]. [tm_hour] 1910132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2, 1911169691Skan __io, __tmperr); 191297403Sobrien break; 191397403Sobrien case 'm': 191497403Sobrien // Month [01, 12]. [tm_mon] 1915132720Skan __beg = _M_extract_num(__beg, __end, __mem, 1, 12, 2, 1916169691Skan __io, __tmperr); 1917169691Skan if (!__tmperr) 191897403Sobrien __tm->tm_mon = __mem - 1; 191997403Sobrien break; 192097403Sobrien case 'M': 192197403Sobrien // Minute [00, 59]. [tm_min] 1922132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2, 1923169691Skan __io, __tmperr); 192497403Sobrien break; 192597403Sobrien case 'n': 192697403Sobrien if (__ctype.narrow(*__beg, 0) == '\n') 192797403Sobrien ++__beg; 192897403Sobrien else 1929169691Skan __tmperr |= ios_base::failbit; 193097403Sobrien break; 193197403Sobrien case 'R': 193297403Sobrien // Equivalent to (%H:%M). 193397403Sobrien __cs = "%H:%M"; 193497403Sobrien __ctype.widen(__cs, __cs + 6, __wcs); 1935169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1936132720Skan __tm, __wcs); 193797403Sobrien break; 193897403Sobrien case 'S': 1939146897Skan // Seconds. [tm_sec] 1940146897Skan // [00, 60] in C99 (one leap-second), [00, 61] in C89. 1941146897Skan#ifdef _GLIBCXX_USE_C99 1942146897Skan __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 60, 2, 1943146897Skan#else 1944146897Skan __beg = _M_extract_num(__beg, __end, __tm->tm_sec, 0, 61, 2, 1945146897Skan#endif 1946169691Skan __io, __tmperr); 194797403Sobrien break; 194897403Sobrien case 't': 194997403Sobrien if (__ctype.narrow(*__beg, 0) == '\t') 195097403Sobrien ++__beg; 195197403Sobrien else 1952169691Skan __tmperr |= ios_base::failbit; 195397403Sobrien break; 195497403Sobrien case 'T': 195597403Sobrien // Equivalent to (%H:%M:%S). 195697403Sobrien __cs = "%H:%M:%S"; 195797403Sobrien __ctype.widen(__cs, __cs + 9, __wcs); 1958169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1959132720Skan __tm, __wcs); 196097403Sobrien break; 196197403Sobrien case 'x': 196297403Sobrien // Locale's date. 196397403Sobrien const char_type* __dates[2]; 196497403Sobrien __tp._M_date_formats(__dates); 1965169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1966132720Skan __tm, __dates[0]); 196797403Sobrien break; 196897403Sobrien case 'X': 196997403Sobrien // Locale's time. 197097403Sobrien const char_type* __times[2]; 197197403Sobrien __tp._M_time_formats(__times); 1972169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __tmperr, 1973132720Skan __tm, __times[0]); 197497403Sobrien break; 197597403Sobrien case 'y': 1976132720Skan case 'C': // C99 197797403Sobrien // Two digit year. [tm_year] 1978132720Skan __beg = _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2, 1979169691Skan __io, __tmperr); 198097403Sobrien break; 198197403Sobrien case 'Y': 198297403Sobrien // Year [1900). [tm_year] 1983132720Skan __beg = _M_extract_num(__beg, __end, __mem, 0, 9999, 4, 1984169691Skan __io, __tmperr); 1985169691Skan if (!__tmperr) 198697403Sobrien __tm->tm_year = __mem - 1900; 198797403Sobrien break; 198897403Sobrien case 'Z': 198997403Sobrien // Timezone info. 199097403Sobrien if (__ctype.is(ctype_base::upper, *__beg)) 199197403Sobrien { 199297403Sobrien int __tmp; 1993132720Skan __beg = _M_extract_name(__beg, __end, __tmp, 1994132720Skan __timepunct_cache<_CharT>::_S_timezones, 1995169691Skan 14, __io, __tmperr); 1996132720Skan 199797403Sobrien // GMT requires special effort. 1998169691Skan if (__beg != __end && !__tmperr && __tmp == 0 1999132720Skan && (*__beg == __ctype.widen('-') 2000132720Skan || *__beg == __ctype.widen('+'))) 200197403Sobrien { 2002132720Skan __beg = _M_extract_num(__beg, __end, __tmp, 0, 23, 2, 2003169691Skan __io, __tmperr); 2004132720Skan __beg = _M_extract_num(__beg, __end, __tmp, 0, 59, 2, 2005169691Skan __io, __tmperr); 2006132720Skan } 200797403Sobrien } 200897403Sobrien else 2009169691Skan __tmperr |= ios_base::failbit; 2010132720Skan break; 2011132720Skan default: 2012132720Skan // Not recognized. 2013169691Skan __tmperr |= ios_base::failbit; 201497403Sobrien } 2015132720Skan } 2016132720Skan else 2017132720Skan { 2018132720Skan // Verify format and input match, extract and discard. 2019132720Skan if (__format[__i] == *__beg) 2020132720Skan ++__beg; 2021132720Skan else 2022169691Skan __tmperr |= ios_base::failbit; 2023132720Skan } 202497403Sobrien } 2025169691Skan 2026169691Skan if (__tmperr) 2027169691Skan __err |= ios_base::failbit; 2028169691Skan 2029132720Skan return __beg; 203097403Sobrien } 203197403Sobrien 203297403Sobrien template<typename _CharT, typename _InIter> 2033132720Skan _InIter 203497403Sobrien time_get<_CharT, _InIter>:: 2035132720Skan _M_extract_num(iter_type __beg, iter_type __end, int& __member, 2036132720Skan int __min, int __max, size_t __len, 2037132720Skan ios_base& __io, ios_base::iostate& __err) const 203897403Sobrien { 2039132720Skan const locale& __loc = __io._M_getloc(); 2040132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 2041132720Skan 2042132720Skan // As-is works for __len = 1, 2, 4, the values actually used. 2043132720Skan int __mult = __len == 2 ? 10 : (__len == 4 ? 1000 : 1); 2044132720Skan 2045132720Skan ++__min; 204697403Sobrien size_t __i = 0; 2047132720Skan int __value = 0; 2048132720Skan for (; __beg != __end && __i < __len; ++__beg, ++__i) 204997403Sobrien { 2050132720Skan const char __c = __ctype.narrow(*__beg, '*'); 2051132720Skan if (__c >= '0' && __c <= '9') 2052132720Skan { 2053132720Skan __value = __value * 10 + (__c - '0'); 2054132720Skan const int __valuec = __value * __mult; 2055132720Skan if (__valuec > __max || __valuec + __mult < __min) 2056132720Skan break; 2057132720Skan __mult /= 10; 2058132720Skan } 2059132720Skan else 2060132720Skan break; 206197403Sobrien } 206297403Sobrien if (__i == __len) 2063132720Skan __member = __value; 206497403Sobrien else 206597403Sobrien __err |= ios_base::failbit; 2066169691Skan 2067132720Skan return __beg; 206897403Sobrien } 206997403Sobrien 207097403Sobrien // Assumptions: 207197403Sobrien // All elements in __names are unique. 207297403Sobrien template<typename _CharT, typename _InIter> 2073132720Skan _InIter 207497403Sobrien time_get<_CharT, _InIter>:: 2075132720Skan _M_extract_name(iter_type __beg, iter_type __end, int& __member, 2076132720Skan const _CharT** __names, size_t __indexlen, 2077132720Skan ios_base& __io, ios_base::iostate& __err) const 207897403Sobrien { 2079132720Skan typedef char_traits<_CharT> __traits_type; 2080132720Skan const locale& __loc = __io._M_getloc(); 2081132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 2082132720Skan 2083132720Skan int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int) 2084117397Skan * __indexlen)); 208597403Sobrien size_t __nmatches = 0; 208697403Sobrien size_t __pos = 0; 208797403Sobrien bool __testvalid = true; 208897403Sobrien const char_type* __name; 208997403Sobrien 209097403Sobrien // Look for initial matches. 2091132720Skan // NB: Some of the locale data is in the form of all lowercase 2092132720Skan // names, and some is in the form of initially-capitalized 2093132720Skan // names. Look for both. 2094132720Skan if (__beg != __end) 2095132720Skan { 2096132720Skan const char_type __c = *__beg; 2097132720Skan for (size_t __i1 = 0; __i1 < __indexlen; ++__i1) 2098132720Skan if (__c == __names[__i1][0] 2099132720Skan || __c == __ctype.toupper(__names[__i1][0])) 2100132720Skan __matches[__nmatches++] = __i1; 2101132720Skan } 2102132720Skan 2103117397Skan while (__nmatches > 1) 210497403Sobrien { 210597403Sobrien // Find smallest matching string. 2106132720Skan size_t __minlen = __traits_type::length(__names[__matches[0]]); 2107132720Skan for (size_t __i2 = 1; __i2 < __nmatches; ++__i2) 2108132720Skan __minlen = std::min(__minlen, 2109132720Skan __traits_type::length(__names[__matches[__i2]])); 2110146897Skan ++__beg, ++__pos; 211197403Sobrien if (__pos < __minlen && __beg != __end) 2112132720Skan for (size_t __i3 = 0; __i3 < __nmatches;) 2113132720Skan { 2114132720Skan __name = __names[__matches[__i3]]; 2115169691Skan if (!(__name[__pos] == *__beg)) 2116132720Skan __matches[__i3] = __matches[--__nmatches]; 2117132720Skan else 2118132720Skan ++__i3; 2119132720Skan } 212097403Sobrien else 212197403Sobrien break; 212297403Sobrien } 212397403Sobrien 212497403Sobrien if (__nmatches == 1) 212597403Sobrien { 212697403Sobrien // Make sure found name is completely extracted. 2127146897Skan ++__beg, ++__pos; 212897403Sobrien __name = __names[__matches[0]]; 212997403Sobrien const size_t __len = __traits_type::length(__name); 213097403Sobrien while (__pos < __len && __beg != __end && __name[__pos] == *__beg) 213197403Sobrien ++__beg, ++__pos; 213297403Sobrien 213397403Sobrien if (__len == __pos) 213497403Sobrien __member = __matches[0]; 213597403Sobrien else 213697403Sobrien __testvalid = false; 213797403Sobrien } 213897403Sobrien else 213997403Sobrien __testvalid = false; 214097403Sobrien if (!__testvalid) 214197403Sobrien __err |= ios_base::failbit; 2142169691Skan 2143132720Skan return __beg; 214497403Sobrien } 214597403Sobrien 214697403Sobrien template<typename _CharT, typename _InIter> 214797403Sobrien _InIter 214897403Sobrien time_get<_CharT, _InIter>:: 214997403Sobrien do_get_time(iter_type __beg, iter_type __end, ios_base& __io, 215097403Sobrien ios_base::iostate& __err, tm* __tm) const 215197403Sobrien { 2152132720Skan const locale& __loc = __io._M_getloc(); 2153132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 2154132720Skan const char_type* __times[2]; 2155132720Skan __tp._M_time_formats(__times); 2156132720Skan __beg = _M_extract_via_format(__beg, __end, __io, __err, 2157132720Skan __tm, __times[0]); 215897403Sobrien if (__beg == __end) 215997403Sobrien __err |= ios_base::eofbit; 216097403Sobrien return __beg; 216197403Sobrien } 216297403Sobrien 216397403Sobrien template<typename _CharT, typename _InIter> 216497403Sobrien _InIter 216597403Sobrien time_get<_CharT, _InIter>:: 216697403Sobrien do_get_date(iter_type __beg, iter_type __end, ios_base& __io, 216797403Sobrien ios_base::iostate& __err, tm* __tm) const 216897403Sobrien { 2169132720Skan const locale& __loc = __io._M_getloc(); 2170132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 2171132720Skan const char_type* __dates[2]; 2172132720Skan __tp._M_date_formats(__dates); 2173169691Skan __beg = _M_extract_via_format(__beg, __end, __io, __err, 2174132720Skan __tm, __dates[0]); 217597403Sobrien if (__beg == __end) 217697403Sobrien __err |= ios_base::eofbit; 217797403Sobrien return __beg; 217897403Sobrien } 217997403Sobrien 218097403Sobrien template<typename _CharT, typename _InIter> 218197403Sobrien _InIter 218297403Sobrien time_get<_CharT, _InIter>:: 2183132720Skan do_get_weekday(iter_type __beg, iter_type __end, ios_base& __io, 218497403Sobrien ios_base::iostate& __err, tm* __tm) const 218597403Sobrien { 2186132720Skan typedef char_traits<_CharT> __traits_type; 2187132720Skan const locale& __loc = __io._M_getloc(); 2188132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 2189132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 219097403Sobrien const char_type* __days[7]; 219197403Sobrien __tp._M_days_abbreviated(__days); 219297403Sobrien int __tmpwday; 2193169691Skan ios_base::iostate __tmperr = ios_base::goodbit; 2194169691Skan __beg = _M_extract_name(__beg, __end, __tmpwday, __days, 7, 2195169691Skan __io, __tmperr); 219697403Sobrien 219797403Sobrien // Check to see if non-abbreviated name exists, and extract. 219897403Sobrien // NB: Assumes both _M_days and _M_days_abbreviated organized in 219997403Sobrien // exact same order, first to last, such that the resulting 220097403Sobrien // __days array with the same index points to a day, and that 220197403Sobrien // day's abbreviated form. 2202132720Skan // NB: Also assumes that an abbreviated name is a subset of the name. 2203169691Skan if (!__tmperr && __beg != __end) 220497403Sobrien { 220597403Sobrien size_t __pos = __traits_type::length(__days[__tmpwday]); 220697403Sobrien __tp._M_days(__days); 220797403Sobrien const char_type* __name = __days[__tmpwday]; 220897403Sobrien if (__name[__pos] == *__beg) 220997403Sobrien { 221097403Sobrien // Extract the rest of it. 221197403Sobrien const size_t __len = __traits_type::length(__name); 2212132720Skan while (__pos < __len && __beg != __end 221397403Sobrien && __name[__pos] == *__beg) 221497403Sobrien ++__beg, ++__pos; 221597403Sobrien if (__len != __pos) 2216169691Skan __tmperr |= ios_base::failbit; 221797403Sobrien } 221897403Sobrien } 2219169691Skan if (!__tmperr) 2220146897Skan __tm->tm_wday = __tmpwday; 2221169691Skan else 2222169691Skan __err |= ios_base::failbit; 2223169691Skan 222497403Sobrien if (__beg == __end) 222597403Sobrien __err |= ios_base::eofbit; 222697403Sobrien return __beg; 222797403Sobrien } 222897403Sobrien 222997403Sobrien template<typename _CharT, typename _InIter> 223097403Sobrien _InIter 223197403Sobrien time_get<_CharT, _InIter>:: 223297403Sobrien do_get_monthname(iter_type __beg, iter_type __end, 223397403Sobrien ios_base& __io, ios_base::iostate& __err, tm* __tm) const 223497403Sobrien { 2235132720Skan typedef char_traits<_CharT> __traits_type; 2236132720Skan const locale& __loc = __io._M_getloc(); 2237132720Skan const __timepunct<_CharT>& __tp = use_facet<__timepunct<_CharT> >(__loc); 2238132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 223997403Sobrien const char_type* __months[12]; 224097403Sobrien __tp._M_months_abbreviated(__months); 224197403Sobrien int __tmpmon; 2242169691Skan ios_base::iostate __tmperr = ios_base::goodbit; 2243132720Skan __beg = _M_extract_name(__beg, __end, __tmpmon, __months, 12, 2244169691Skan __io, __tmperr); 224597403Sobrien 224697403Sobrien // Check to see if non-abbreviated name exists, and extract. 224797403Sobrien // NB: Assumes both _M_months and _M_months_abbreviated organized in 224897403Sobrien // exact same order, first to last, such that the resulting 224997403Sobrien // __months array with the same index points to a month, and that 225097403Sobrien // month's abbreviated form. 2251132720Skan // NB: Also assumes that an abbreviated name is a subset of the name. 2252169691Skan if (!__tmperr && __beg != __end) 225397403Sobrien { 225497403Sobrien size_t __pos = __traits_type::length(__months[__tmpmon]); 225597403Sobrien __tp._M_months(__months); 225697403Sobrien const char_type* __name = __months[__tmpmon]; 225797403Sobrien if (__name[__pos] == *__beg) 225897403Sobrien { 225997403Sobrien // Extract the rest of it. 226097403Sobrien const size_t __len = __traits_type::length(__name); 2261132720Skan while (__pos < __len && __beg != __end 226297403Sobrien && __name[__pos] == *__beg) 226397403Sobrien ++__beg, ++__pos; 226497403Sobrien if (__len != __pos) 2265169691Skan __tmperr |= ios_base::failbit; 226697403Sobrien } 226797403Sobrien } 2268169691Skan if (!__tmperr) 2269146897Skan __tm->tm_mon = __tmpmon; 2270169691Skan else 2271169691Skan __err |= ios_base::failbit; 2272132720Skan 227397403Sobrien if (__beg == __end) 227497403Sobrien __err |= ios_base::eofbit; 227597403Sobrien return __beg; 227697403Sobrien } 227797403Sobrien 227897403Sobrien template<typename _CharT, typename _InIter> 227997403Sobrien _InIter 228097403Sobrien time_get<_CharT, _InIter>:: 2281132720Skan do_get_year(iter_type __beg, iter_type __end, ios_base& __io, 228297403Sobrien ios_base::iostate& __err, tm* __tm) const 228397403Sobrien { 2284132720Skan const locale& __loc = __io._M_getloc(); 2285132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 228697403Sobrien 228797403Sobrien size_t __i = 0; 2288132720Skan int __value = 0; 2289132720Skan for (; __beg != __end && __i < 4; ++__beg, ++__i) 229097403Sobrien { 2291132720Skan const char __c = __ctype.narrow(*__beg, '*'); 2292132720Skan if (__c >= '0' && __c <= '9') 2293132720Skan __value = __value * 10 + (__c - '0'); 2294132720Skan else 2295132720Skan break; 229697403Sobrien } 229797403Sobrien if (__i == 2 || __i == 4) 2298132720Skan __tm->tm_year = __i == 2 ? __value : __value - 1900; 229997403Sobrien else 230097403Sobrien __err |= ios_base::failbit; 2301169691Skan 230297403Sobrien if (__beg == __end) 230397403Sobrien __err |= ios_base::eofbit; 230497403Sobrien return __beg; 230597403Sobrien } 230697403Sobrien 230797403Sobrien template<typename _CharT, typename _OutIter> 230897403Sobrien _OutIter 230997403Sobrien time_put<_CharT, _OutIter>:: 2310132720Skan put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm, 231197403Sobrien const _CharT* __beg, const _CharT* __end) const 231297403Sobrien { 2313132720Skan const locale& __loc = __io._M_getloc(); 231497403Sobrien ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc); 2315132720Skan for (; __beg != __end; ++__beg) 2316132720Skan if (__ctype.narrow(*__beg, 0) != '%') 2317132720Skan { 2318132720Skan *__s = *__beg; 2319132720Skan ++__s; 2320132720Skan } 2321132720Skan else if (++__beg != __end) 2322132720Skan { 2323132720Skan char __format; 2324132720Skan char __mod = 0; 2325132720Skan const char __c = __ctype.narrow(*__beg, 0); 2326132720Skan if (__c != 'E' && __c != 'O') 2327132720Skan __format = __c; 2328132720Skan else if (++__beg != __end) 2329132720Skan { 2330132720Skan __mod = __c; 2331132720Skan __format = __ctype.narrow(*__beg, 0); 2332132720Skan } 2333132720Skan else 2334132720Skan break; 2335132720Skan __s = this->do_put(__s, __io, __fill, __tm, __format, __mod); 2336132720Skan } 2337132720Skan else 2338132720Skan break; 233997403Sobrien return __s; 234097403Sobrien } 234197403Sobrien 234297403Sobrien template<typename _CharT, typename _OutIter> 234397403Sobrien _OutIter 234497403Sobrien time_put<_CharT, _OutIter>:: 2345132720Skan do_put(iter_type __s, ios_base& __io, char_type, const tm* __tm, 234697403Sobrien char __format, char __mod) const 2347132720Skan { 2348132720Skan const locale& __loc = __io._M_getloc(); 234997403Sobrien ctype<_CharT> const& __ctype = use_facet<ctype<_CharT> >(__loc); 235097403Sobrien __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc); 235197403Sobrien 235297403Sobrien // NB: This size is arbitrary. Should this be a data member, 235397403Sobrien // initialized at construction? 2354146897Skan const size_t __maxlen = 128; 2355132720Skan char_type* __res = 2356132720Skan static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __maxlen)); 235797403Sobrien 235897403Sobrien // NB: In IEE 1003.1-200x, and perhaps other locale models, it 235997403Sobrien // is possible that the format character will be longer than one 236097403Sobrien // character. Possibilities include 'E' or 'O' followed by a 236197403Sobrien // format character: if __mod is not the default argument, assume 236297403Sobrien // it's a valid modifier. 236397403Sobrien char_type __fmt[4]; 2364132720Skan __fmt[0] = __ctype.widen('%'); 236597403Sobrien if (!__mod) 236697403Sobrien { 236797403Sobrien __fmt[1] = __format; 236897403Sobrien __fmt[2] = char_type(); 236997403Sobrien } 237097403Sobrien else 237197403Sobrien { 237297403Sobrien __fmt[1] = __mod; 237397403Sobrien __fmt[2] = __format; 237497403Sobrien __fmt[3] = char_type(); 237597403Sobrien } 237697403Sobrien 237797403Sobrien __tp._M_put(__res, __maxlen, __fmt, __tm); 237897403Sobrien 237997403Sobrien // Write resulting, fully-formatted string to output iterator. 2380132720Skan return std::__write(__s, __res, char_traits<char_type>::length(__res)); 238197403Sobrien } 238297403Sobrien 238397403Sobrien // Generic version does nothing. 238497403Sobrien template<typename _CharT> 238597403Sobrien int 238697403Sobrien collate<_CharT>::_M_compare(const _CharT*, const _CharT*) const 238797403Sobrien { return 0; } 238897403Sobrien 238997403Sobrien // Generic version does nothing. 239097403Sobrien template<typename _CharT> 239197403Sobrien size_t 239297403Sobrien collate<_CharT>::_M_transform(_CharT*, const _CharT*, size_t) const 239397403Sobrien { return 0; } 239497403Sobrien 239597403Sobrien template<typename _CharT> 239697403Sobrien int 239797403Sobrien collate<_CharT>:: 2398132720Skan do_compare(const _CharT* __lo1, const _CharT* __hi1, 239997403Sobrien const _CharT* __lo2, const _CharT* __hi2) const 2400132720Skan { 2401117397Skan // strcoll assumes zero-terminated strings so we make a copy 2402117397Skan // and then put a zero at the end. 240397403Sobrien const string_type __one(__lo1, __hi1); 240497403Sobrien const string_type __two(__lo2, __hi2); 2405117397Skan 2406117397Skan const _CharT* __p = __one.c_str(); 2407132720Skan const _CharT* __pend = __one.data() + __one.length(); 2408117397Skan const _CharT* __q = __two.c_str(); 2409132720Skan const _CharT* __qend = __two.data() + __two.length(); 2410117397Skan 2411117397Skan // strcoll stops when it sees a nul character so we break 2412117397Skan // the strings into zero-terminated substrings and pass those 2413117397Skan // to strcoll. 2414117397Skan for (;;) 2415117397Skan { 2416132720Skan const int __res = _M_compare(__p, __q); 2417117397Skan if (__res) 2418117397Skan return __res; 2419117397Skan 2420117397Skan __p += char_traits<_CharT>::length(__p); 2421117397Skan __q += char_traits<_CharT>::length(__q); 2422117397Skan if (__p == __pend && __q == __qend) 2423117397Skan return 0; 2424117397Skan else if (__p == __pend) 2425117397Skan return -1; 2426117397Skan else if (__q == __qend) 2427117397Skan return 1; 2428117397Skan 2429117397Skan __p++; 2430117397Skan __q++; 2431117397Skan } 243297403Sobrien } 243397403Sobrien 2434132720Skan template<typename _CharT> 243597403Sobrien typename collate<_CharT>::string_type 243697403Sobrien collate<_CharT>:: 243797403Sobrien do_transform(const _CharT* __lo, const _CharT* __hi) const 243897403Sobrien { 2439169691Skan string_type __ret; 2440169691Skan 2441117397Skan // strxfrm assumes zero-terminated strings so we make a copy 2442169691Skan const string_type __str(__lo, __hi); 2443117397Skan 2444117397Skan const _CharT* __p = __str.c_str(); 2445132720Skan const _CharT* __pend = __str.data() + __str.length(); 2446117397Skan 244797403Sobrien size_t __len = (__hi - __lo) * 2; 2448117397Skan 2449169691Skan _CharT* __c = new _CharT[__len]; 2450117397Skan 2451169691Skan try 245297403Sobrien { 2453169691Skan // strxfrm stops when it sees a nul character so we break 2454169691Skan // the string into zero-terminated substrings and pass those 2455169691Skan // to strxfrm. 2456169691Skan for (;;) 2457117397Skan { 2458169691Skan // First try a buffer perhaps big enough. 2459169691Skan size_t __res = _M_transform(__c, __p, __len); 2460169691Skan // If the buffer was not large enough, try again with the 2461169691Skan // correct size. 2462169691Skan if (__res >= __len) 2463169691Skan { 2464169691Skan __len = __res + 1; 2465169691Skan delete [] __c, __c = 0; 2466169691Skan __c = new _CharT[__len]; 2467169691Skan __res = _M_transform(__c, __p, __len); 2468169691Skan } 2469169691Skan 2470169691Skan __ret.append(__c, __res); 2471169691Skan __p += char_traits<_CharT>::length(__p); 2472169691Skan if (__p == __pend) 2473169691Skan break; 2474169691Skan 2475169691Skan __p++; 2476169691Skan __ret.push_back(_CharT()); 2477117397Skan } 2478169691Skan } 2479169691Skan catch(...) 2480169691Skan { 2481169691Skan delete [] __c; 2482169691Skan __throw_exception_again; 2483169691Skan } 2484117397Skan 2485169691Skan delete [] __c; 2486117397Skan 2487169691Skan return __ret; 248897403Sobrien } 248997403Sobrien 2490132720Skan template<typename _CharT> 249197403Sobrien long 249297403Sobrien collate<_CharT>:: 249397403Sobrien do_hash(const _CharT* __lo, const _CharT* __hi) const 2494132720Skan { 249597403Sobrien unsigned long __val = 0; 249697403Sobrien for (; __lo < __hi; ++__lo) 2497132720Skan __val = *__lo + ((__val << 7) | 249897403Sobrien (__val >> (numeric_limits<unsigned long>::digits - 7))); 249997403Sobrien return static_cast<long>(__val); 250097403Sobrien } 250197403Sobrien 250297403Sobrien // Construct correctly padded string, as per 22.2.2.2.2 2503132720Skan // Assumes 250497403Sobrien // __newlen > __oldlen 250597403Sobrien // __news is allocated for __newlen size 250697403Sobrien // Used by both num_put and ostream inserters: if __num, 250797403Sobrien // internal-adjusted objects are padded according to the rules below 250897403Sobrien // concerning 0[xX] and +-, otherwise, exactly as right-adjusted 250997403Sobrien // ones are. 2510102782Skan 2511102782Skan // NB: Of the two parameters, _CharT can be deduced from the 2512102782Skan // function arguments. The other (_Traits) has to be explicitly specified. 251397403Sobrien template<typename _CharT, typename _Traits> 2514132720Skan void 2515132720Skan __pad<_CharT, _Traits>::_S_pad(ios_base& __io, _CharT __fill, 2516132720Skan _CharT* __news, const _CharT* __olds, 2517132720Skan const streamsize __newlen, 2518102782Skan const streamsize __oldlen, const bool __num) 2519102782Skan { 2520132720Skan const size_t __plen = static_cast<size_t>(__newlen - __oldlen); 2521132720Skan const ios_base::fmtflags __adjust = __io.flags() & ios_base::adjustfield; 2522102782Skan 2523132720Skan // Padding last. 252497403Sobrien if (__adjust == ios_base::left) 252597403Sobrien { 2526132720Skan _Traits::copy(__news, const_cast<_CharT*>(__olds), __oldlen); 2527132720Skan _Traits::assign(__news + __oldlen, __plen, __fill); 2528132720Skan return; 252997403Sobrien } 2530132720Skan 2531132720Skan size_t __mod = 0; 2532132720Skan if (__adjust == ios_base::internal && __num) 253397403Sobrien { 253497403Sobrien // Pad after the sign, if there is one. 253597403Sobrien // Pad after 0[xX], if there is one. 253697403Sobrien // Who came up with these rules, anyway? Jeeze. 2537132720Skan const locale& __loc = __io._M_getloc(); 2538132720Skan const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); 2539102782Skan 2540132720Skan const bool __testsign = (__ctype.widen('-') == __olds[0] 2541132720Skan || __ctype.widen('+') == __olds[0]); 2542132720Skan const bool __testhex = (__ctype.widen('0') == __olds[0] 2543132720Skan && __oldlen > 1 2544132720Skan && (__ctype.widen('x') == __olds[1] 2545132720Skan || __ctype.widen('X') == __olds[1])); 254697403Sobrien if (__testhex) 254797403Sobrien { 2548132720Skan __news[0] = __olds[0]; 254997403Sobrien __news[1] = __olds[1]; 2550132720Skan __mod = 2; 255197403Sobrien __news += 2; 255297403Sobrien } 255397403Sobrien else if (__testsign) 255497403Sobrien { 2555132720Skan __news[0] = __olds[0]; 2556132720Skan __mod = 1; 255797403Sobrien ++__news; 255897403Sobrien } 2559132720Skan // else Padding first. 256097403Sobrien } 2561132720Skan _Traits::assign(__news, __plen, __fill); 2562132720Skan _Traits::copy(__news + __plen, const_cast<_CharT*>(__olds + __mod), 2563132720Skan __oldlen - __mod); 256497403Sobrien } 256597403Sobrien 2566132720Skan bool 2567132720Skan __verify_grouping(const char* __grouping, size_t __grouping_size, 2568132720Skan const string& __grouping_tmp) 2569132720Skan { 2570132720Skan const size_t __n = __grouping_tmp.size() - 1; 2571169691Skan const size_t __min = std::min(__n, size_t(__grouping_size - 1)); 2572132720Skan size_t __i = __n; 2573132720Skan bool __test = true; 2574132720Skan 2575132720Skan // Parsed number groupings have to match the 2576132720Skan // numpunct::grouping string exactly, starting at the 2577132720Skan // right-most point of the parsed sequence of elements ... 2578132720Skan for (size_t __j = 0; __j < __min && __test; --__i, ++__j) 2579132720Skan __test = __grouping_tmp[__i] == __grouping[__j]; 2580132720Skan for (; __i && __test; --__i) 2581132720Skan __test = __grouping_tmp[__i] == __grouping[__min]; 2582169691Skan // ... but the first parsed grouping can be <= numpunct 2583169691Skan // grouping (only do the check if the numpunct char is > 0 2584169691Skan // because <= 0 means any size is ok). 2585169691Skan if (static_cast<signed char>(__grouping[__min]) > 0) 2586169691Skan __test &= __grouping_tmp[0] <= __grouping[__min]; 2587132720Skan return __test; 2588132720Skan } 258997403Sobrien 259097403Sobrien template<typename _CharT> 259197403Sobrien _CharT* 2592132720Skan __add_grouping(_CharT* __s, _CharT __sep, 2593132720Skan const char* __gbeg, size_t __gsize, 259497403Sobrien const _CharT* __first, const _CharT* __last) 259597403Sobrien { 2596169691Skan size_t __idx = 0; 2597169691Skan size_t __ctr = 0; 2598169691Skan 2599169691Skan while (__last - __first > __gbeg[__idx] 2600169691Skan && static_cast<signed char>(__gbeg[__idx]) > 0) 2601132720Skan { 2602169691Skan __last -= __gbeg[__idx]; 2603169691Skan __idx < __gsize - 1 ? ++__idx : ++__ctr; 2604132720Skan } 2605169691Skan 2606169691Skan while (__first != __last) 260797403Sobrien *__s++ = *__first++; 2608169691Skan 2609169691Skan while (__ctr--) 2610169691Skan { 2611169691Skan *__s++ = __sep; 2612169691Skan for (char __i = __gbeg[__idx]; __i > 0; --__i) 2613169691Skan *__s++ = *__first++; 2614169691Skan } 2615169691Skan 2616169691Skan while (__idx--) 2617169691Skan { 2618169691Skan *__s++ = __sep; 2619169691Skan for (char __i = __gbeg[__idx]; __i > 0; --__i) 2620169691Skan *__s++ = *__first++; 2621169691Skan } 2622169691Skan 262397403Sobrien return __s; 262497403Sobrien } 262597403Sobrien 262697403Sobrien // Inhibit implicit instantiations for required instantiations, 2627132720Skan // which are defined via explicit instantiations elsewhere. 262897403Sobrien // NB: This syntax is a GNU extension. 2629132720Skan#if _GLIBCXX_EXTERN_TEMPLATE 263097403Sobrien extern template class moneypunct<char, false>; 263197403Sobrien extern template class moneypunct<char, true>; 263297403Sobrien extern template class moneypunct_byname<char, false>; 263397403Sobrien extern template class moneypunct_byname<char, true>; 2634169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE money_get<char>; 2635169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE money_put<char>; 263697403Sobrien extern template class numpunct<char>; 263797403Sobrien extern template class numpunct_byname<char>; 2638169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE num_get<char>; 2639169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE num_put<char>; 264097403Sobrien extern template class __timepunct<char>; 264197403Sobrien extern template class time_put<char>; 264297403Sobrien extern template class time_put_byname<char>; 264397403Sobrien extern template class time_get<char>; 264497403Sobrien extern template class time_get_byname<char>; 264597403Sobrien extern template class messages<char>; 264697403Sobrien extern template class messages_byname<char>; 264797403Sobrien extern template class ctype_byname<char>; 264897403Sobrien extern template class codecvt_byname<char, char, mbstate_t>; 264997403Sobrien extern template class collate<char>; 265097403Sobrien extern template class collate_byname<char>; 265197403Sobrien 265297403Sobrien extern template 2653132720Skan const codecvt<char, char, mbstate_t>& 265497403Sobrien use_facet<codecvt<char, char, mbstate_t> >(const locale&); 265597403Sobrien 265697403Sobrien extern template 2657132720Skan const collate<char>& 265897403Sobrien use_facet<collate<char> >(const locale&); 265997403Sobrien 266097403Sobrien extern template 2661132720Skan const numpunct<char>& 266297403Sobrien use_facet<numpunct<char> >(const locale&); 266397403Sobrien 2664132720Skan extern template 2665132720Skan const num_put<char>& 266697403Sobrien use_facet<num_put<char> >(const locale&); 266797403Sobrien 2668132720Skan extern template 2669132720Skan const num_get<char>& 267097403Sobrien use_facet<num_get<char> >(const locale&); 267197403Sobrien 267297403Sobrien extern template 2673132720Skan const moneypunct<char, true>& 267497403Sobrien use_facet<moneypunct<char, true> >(const locale&); 267597403Sobrien 267697403Sobrien extern template 2677132720Skan const moneypunct<char, false>& 267897403Sobrien use_facet<moneypunct<char, false> >(const locale&); 267997403Sobrien 2680132720Skan extern template 2681132720Skan const money_put<char>& 268297403Sobrien use_facet<money_put<char> >(const locale&); 268397403Sobrien 2684132720Skan extern template 2685132720Skan const money_get<char>& 268697403Sobrien use_facet<money_get<char> >(const locale&); 268797403Sobrien 268897403Sobrien extern template 2689132720Skan const __timepunct<char>& 269097403Sobrien use_facet<__timepunct<char> >(const locale&); 269197403Sobrien 2692132720Skan extern template 2693132720Skan const time_put<char>& 269497403Sobrien use_facet<time_put<char> >(const locale&); 269597403Sobrien 2696132720Skan extern template 2697132720Skan const time_get<char>& 269897403Sobrien use_facet<time_get<char> >(const locale&); 269997403Sobrien 2700132720Skan extern template 2701132720Skan const messages<char>& 270297403Sobrien use_facet<messages<char> >(const locale&); 270397403Sobrien 2704132720Skan extern template 2705107606Sobrien bool 2706107606Sobrien has_facet<ctype<char> >(const locale&); 2707107606Sobrien 2708132720Skan extern template 2709107606Sobrien bool 2710107606Sobrien has_facet<codecvt<char, char, mbstate_t> >(const locale&); 2711107606Sobrien 2712132720Skan extern template 2713107606Sobrien bool 2714107606Sobrien has_facet<collate<char> >(const locale&); 2715107606Sobrien 2716132720Skan extern template 2717107606Sobrien bool 2718107606Sobrien has_facet<numpunct<char> >(const locale&); 2719107606Sobrien 2720132720Skan extern template 2721107606Sobrien bool 2722107606Sobrien has_facet<num_put<char> >(const locale&); 2723107606Sobrien 2724132720Skan extern template 2725107606Sobrien bool 2726107606Sobrien has_facet<num_get<char> >(const locale&); 2727107606Sobrien 2728132720Skan extern template 2729107606Sobrien bool 2730107606Sobrien has_facet<moneypunct<char> >(const locale&); 2731107606Sobrien 2732132720Skan extern template 2733107606Sobrien bool 2734107606Sobrien has_facet<money_put<char> >(const locale&); 2735107606Sobrien 2736132720Skan extern template 2737107606Sobrien bool 2738107606Sobrien has_facet<money_get<char> >(const locale&); 2739107606Sobrien 2740132720Skan extern template 2741107606Sobrien bool 2742107606Sobrien has_facet<__timepunct<char> >(const locale&); 2743107606Sobrien 2744132720Skan extern template 2745107606Sobrien bool 2746107606Sobrien has_facet<time_put<char> >(const locale&); 2747107606Sobrien 2748132720Skan extern template 2749107606Sobrien bool 2750107606Sobrien has_facet<time_get<char> >(const locale&); 2751107606Sobrien 2752132720Skan extern template 2753107606Sobrien bool 2754107606Sobrien has_facet<messages<char> >(const locale&); 2755107606Sobrien 2756132720Skan#ifdef _GLIBCXX_USE_WCHAR_T 2757107606Sobrien extern template class moneypunct<wchar_t, false>; 2758107606Sobrien extern template class moneypunct<wchar_t, true>; 2759107606Sobrien extern template class moneypunct_byname<wchar_t, false>; 2760107606Sobrien extern template class moneypunct_byname<wchar_t, true>; 2761169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE money_get<wchar_t>; 2762169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE money_put<wchar_t>; 2763107606Sobrien extern template class numpunct<wchar_t>; 2764107606Sobrien extern template class numpunct_byname<wchar_t>; 2765169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE num_get<wchar_t>; 2766169691Skan extern template class _GLIBCXX_LDBL_NAMESPACE num_put<wchar_t>; 2767107606Sobrien extern template class __timepunct<wchar_t>; 2768107606Sobrien extern template class time_put<wchar_t>; 2769107606Sobrien extern template class time_put_byname<wchar_t>; 2770107606Sobrien extern template class time_get<wchar_t>; 2771107606Sobrien extern template class time_get_byname<wchar_t>; 2772107606Sobrien extern template class messages<wchar_t>; 2773107606Sobrien extern template class messages_byname<wchar_t>; 2774107606Sobrien extern template class ctype_byname<wchar_t>; 2775107606Sobrien extern template class codecvt_byname<wchar_t, char, mbstate_t>; 2776107606Sobrien extern template class collate<wchar_t>; 2777107606Sobrien extern template class collate_byname<wchar_t>; 2778107606Sobrien 277997403Sobrien extern template 2780132720Skan const codecvt<wchar_t, char, mbstate_t>& 278197403Sobrien use_facet<codecvt<wchar_t, char, mbstate_t> >(locale const&); 278297403Sobrien 278397403Sobrien extern template 2784132720Skan const collate<wchar_t>& 278597403Sobrien use_facet<collate<wchar_t> >(const locale&); 278697403Sobrien 278797403Sobrien extern template 2788132720Skan const numpunct<wchar_t>& 278997403Sobrien use_facet<numpunct<wchar_t> >(const locale&); 279097403Sobrien 2791132720Skan extern template 2792132720Skan const num_put<wchar_t>& 279397403Sobrien use_facet<num_put<wchar_t> >(const locale&); 279497403Sobrien 2795132720Skan extern template 2796132720Skan const num_get<wchar_t>& 279797403Sobrien use_facet<num_get<wchar_t> >(const locale&); 279897403Sobrien 279997403Sobrien extern template 2800132720Skan const moneypunct<wchar_t, true>& 280197403Sobrien use_facet<moneypunct<wchar_t, true> >(const locale&); 280297403Sobrien 280397403Sobrien extern template 2804132720Skan const moneypunct<wchar_t, false>& 280597403Sobrien use_facet<moneypunct<wchar_t, false> >(const locale&); 2806132720Skan 2807132720Skan extern template 2808132720Skan const money_put<wchar_t>& 280997403Sobrien use_facet<money_put<wchar_t> >(const locale&); 281097403Sobrien 2811132720Skan extern template 2812132720Skan const money_get<wchar_t>& 281397403Sobrien use_facet<money_get<wchar_t> >(const locale&); 281497403Sobrien 281597403Sobrien extern template 2816132720Skan const __timepunct<wchar_t>& 281797403Sobrien use_facet<__timepunct<wchar_t> >(const locale&); 281897403Sobrien 2819132720Skan extern template 2820132720Skan const time_put<wchar_t>& 282197403Sobrien use_facet<time_put<wchar_t> >(const locale&); 282297403Sobrien 2823132720Skan extern template 2824132720Skan const time_get<wchar_t>& 282597403Sobrien use_facet<time_get<wchar_t> >(const locale&); 282697403Sobrien 2827132720Skan extern template 2828132720Skan const messages<wchar_t>& 282997403Sobrien use_facet<messages<wchar_t> >(const locale&); 283097403Sobrien 2831132720Skan extern template 283297403Sobrien bool 283397403Sobrien has_facet<ctype<wchar_t> >(const locale&); 283497403Sobrien 2835132720Skan extern template 283697403Sobrien bool 283797403Sobrien has_facet<codecvt<wchar_t, char, mbstate_t> >(const locale&); 283897403Sobrien 2839132720Skan extern template 284097403Sobrien bool 284197403Sobrien has_facet<collate<wchar_t> >(const locale&); 284297403Sobrien 2843132720Skan extern template 284497403Sobrien bool 284597403Sobrien has_facet<numpunct<wchar_t> >(const locale&); 284697403Sobrien 2847132720Skan extern template 284897403Sobrien bool 284997403Sobrien has_facet<num_put<wchar_t> >(const locale&); 285097403Sobrien 2851132720Skan extern template 285297403Sobrien bool 285397403Sobrien has_facet<num_get<wchar_t> >(const locale&); 285497403Sobrien 2855132720Skan extern template 285697403Sobrien bool 285797403Sobrien has_facet<moneypunct<wchar_t> >(const locale&); 285897403Sobrien 2859132720Skan extern template 286097403Sobrien bool 286197403Sobrien has_facet<money_put<wchar_t> >(const locale&); 286297403Sobrien 2863132720Skan extern template 286497403Sobrien bool 286597403Sobrien has_facet<money_get<wchar_t> >(const locale&); 286697403Sobrien 2867132720Skan extern template 286897403Sobrien bool 286997403Sobrien has_facet<__timepunct<wchar_t> >(const locale&); 287097403Sobrien 2871132720Skan extern template 287297403Sobrien bool 287397403Sobrien has_facet<time_put<wchar_t> >(const locale&); 287497403Sobrien 2875132720Skan extern template 287697403Sobrien bool 287797403Sobrien has_facet<time_get<wchar_t> >(const locale&); 287897403Sobrien 2879132720Skan extern template 288097403Sobrien bool 288197403Sobrien has_facet<messages<wchar_t> >(const locale&); 2882107606Sobrien#endif 2883117397Skan#endif 288497403Sobrien 2885169691Skan_GLIBCXX_END_NAMESPACE 2886169691Skan 288797403Sobrien#endif 2888