📄 locale_facets.tcc
字号:
__len = __p - __ws2; // Switch strings. __ws = __ws2; } return _M_insert(__s, __io, __fill, __ws, __len); } // For use by integer and floating-point types after they have been // converted into a char_type string. template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: _M_insert(_OutIter __s, ios_base& __io, _CharT __fill, const _CharT* __ws, int __len) const { typedef char_traits<_CharT> __traits_type; // [22.2.2.2.2] Stage 3. streamsize __w = __io.width(); _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w)); if (__w > static_cast<streamsize>(__len)) { __pad<_CharT, __traits_type>::_S_pad(__io, __fill, __ws2, __ws, __w, __len, true); __len = static_cast<int>(__w); // Switch strings. __ws = __ws2; } __io.width(0); // [22.2.2.2.2] Stage 4. // Write resulting, fully-formatted string to output iterator. for (int __j = 0; __j < __len; ++__j, ++__s) *__s = __ws[__j]; return __s; } template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: do_put(iter_type __s, ios_base& __io, char_type __fill, bool __v) const { ios_base::fmtflags __flags = __io.flags(); if ((__flags & ios_base::boolalpha) == 0) { unsigned long __uv = __v; __s = _M_convert_int(__s, __io, __fill, 'u', char(), __uv); } else { typedef basic_string<_CharT> __string_type; locale __loc = __io.getloc(); const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc); __string_type __name; if (__v) __name = __np.truename(); else __name = __np.falsename(); __s = _M_insert(__s, __io, __fill, __name.c_str(), __name.size()); } return __s; } template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: do_put(iter_type __s, ios_base& __io, char_type __fill, long __v) const { return _M_convert_int(__s, __io, __fill, 'd', char(), __v); } template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: do_put(iter_type __s, ios_base& __io, char_type __fill, unsigned long __v) const { return _M_convert_int(__s, __io, __fill, 'u', char(), __v); }#ifdef _GLIBCPP_USE_LONG_LONG template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const { return _M_convert_int(__s, __b, __fill, 'd', 'l', __v); } template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: do_put(iter_type __s, ios_base& __io, char_type __fill, unsigned long long __v) const { return _M_convert_int(__s, __io, __fill, 'u', 'l', __v); }#endif template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: do_put(iter_type __s, ios_base& __io, char_type __fill, double __v) const { return _M_convert_float(__s, __io, __fill, char(), __v); } template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: do_put(iter_type __s, ios_base& __io, char_type __fill, long double __v) const { return _M_convert_float(__s, __io, __fill, 'L', __v); } template<typename _CharT, typename _OutIter> _OutIter num_put<_CharT, _OutIter>:: do_put(iter_type __s, ios_base& __io, char_type __fill, const void* __v) const { ios_base::fmtflags __flags = __io.flags(); ios_base::fmtflags __fmt = ~(ios_base::showpos | ios_base::basefield | ios_base::uppercase | ios_base::internal); __io.flags(__flags & __fmt | (ios_base::hex | ios_base::showbase)); try { __s = _M_convert_int(__s, __io, __fill, 'u', char(), reinterpret_cast<unsigned long>(__v)); __io.flags(__flags); } catch (...) { __io.flags(__flags); __throw_exception_again; } return __s; } template<typename _CharT, typename _InIter> _InIter money_get<_CharT, _InIter>:: do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, ios_base::iostate& __err, long double& __units) const { string_type __str; __beg = this->do_get(__beg, __end, __intl, __io, __err, __str); const int __n = numeric_limits<long double>::digits10; char* __cs = static_cast<char*>(__builtin_alloca(__n)); const locale __loc = __io.getloc(); const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); const _CharT* __wcs = __str.c_str(); __ctype.narrow(__wcs, __wcs + __str.size() + 1, char(), __cs); __convert_to_v(__cs, __units, __err, _S_c_locale); return __beg; } template<typename _CharT, typename _InIter> _InIter money_get<_CharT, _InIter>:: do_get(iter_type __beg, iter_type __end, bool __intl, ios_base& __io, ios_base::iostate& __err, string_type& __units) const { // These contortions are quite unfortunate. typedef moneypunct<_CharT, true> __money_true; typedef moneypunct<_CharT, false> __money_false; typedef money_base::part part; typedef typename string_type::size_type size_type; const locale __loc = __io.getloc(); const __money_true& __mpt = use_facet<__money_true>(__loc); const __money_false& __mpf = use_facet<__money_false>(__loc); const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); const money_base::pattern __p = __intl ? __mpt.neg_format() : __mpf.neg_format(); const string_type __pos_sign =__intl ? __mpt.positive_sign() : __mpf.positive_sign(); const string_type __neg_sign =__intl ? __mpt.negative_sign() : __mpf.negative_sign(); const char_type __d = __intl ? __mpt.decimal_point() : __mpf.decimal_point(); const char_type __sep = __intl ? __mpt.thousands_sep() : __mpf.thousands_sep(); const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping(); // Set to deduced positive or negative sign, depending. string_type __sign; // String of grouping info from thousands_sep plucked from __units. string __grouping_tmp; // Marker for thousands_sep position. int __sep_pos = 0; // If input iterator is in a valid state. bool __testvalid = true; // Flag marking when a decimal point is found. bool __testdecfound = false; // The tentative returned string is stored here. string_type __temp_units; char_type __c = *__beg; char_type __eof = static_cast<char_type>(char_traits<char_type>::eof()); for (int __i = 0; __beg != __end && __i < 4 && __testvalid; ++__i) { part __which = static_cast<part>(__p.field[__i]); switch (__which) { case money_base::symbol: if (__io.flags() & ios_base::showbase || __i < 2 || __sign.size() > 1 || ((static_cast<part>(__p.field[3]) != money_base::none) && __i == 2)) { // According to 22.2.6.1.2.2, symbol is required // if (__io.flags() & ios_base::showbase), // otherwise is optional and consumed only if // other characters are needed to complete the // format. const string_type __symbol = __intl ? __mpt.curr_symbol() : __mpf.curr_symbol(); size_type __len = __symbol.size(); size_type __j = 0; while (__beg != __end && __j < __len && __symbol[__j] == __c) { __c = *(++__beg); ++__j; } // When (__io.flags() & ios_base::showbase) // symbol is required. if (__j != __len && (__io.flags() & ios_base::showbase)) __testvalid = false; } break; case money_base::sign: // Sign might not exist, or be more than one character long. if (__pos_sign.size() && __neg_sign.size()) { // Sign is mandatory. if (__c == __pos_sign[0]) { __sign = __pos_sign; __c = *(++__beg); } else if (__c == __neg_sign[0]) { __sign = __neg_sign; __c = *(++__beg); } else __testvalid = false; } else if (__pos_sign.size() && __c == __pos_sign[0]) { __sign = __pos_sign; __c = *(++__beg); } else if (__neg_sign.size() && __c == __neg_sign[0]) { __sign = __neg_sign; __c = *(++__beg); } break; case money_base::value: // Extract digits, remove and stash away the // grouping of found thousands separators. while (__beg != __end && (__ctype.is(ctype_base::digit, __c) || (__c == __d && !__testdecfound) || __c == __sep)) { if (__c == __d) { __grouping_tmp += static_cast<char>(__sep_pos); __sep_pos = 0; __testdecfound = true; } else if (__c == __sep) { if (__grouping.size()) { // Mark position for later analysis. __grouping_tmp += static_cast<char>(__sep_pos); __sep_pos = 0; } else { __testvalid = false; break; } } else { __temp_units += __c; ++__sep_pos; } __c = *(++__beg); } break; case money_base::space: case money_base::none: // Only if not at the end of the pattern. if (__i != 3) while (__beg != __end && __ctype.is(ctype_base::space, __c)) __c = *(++__beg); break; } } // Need to get the rest of the sign characters, if they exist. if (__sign.size() > 1) { size_type __len = __sign.size(); size_type __i = 1; for (; __c != __eof && __i < __len; ++__i) while (__beg != __end && __c != __sign[__i]) __c = *(++__beg); if (__i != __len) __testvalid = false; } // Strip leading zeros. while (__temp_units[0] == __ctype.widen('0')) __temp_units.erase(__temp_units.begin()); if (__sign.size() && __sign == __neg_sign) __temp_units.insert(__temp_units.begin(), __ctype.widen('-')); // Test for grouping fidelity. if (__grouping.size() && __grouping_tmp.size()) { if (!__verify_grouping(__grouping, __grouping_tmp)) __testvalid = false; } // Iff no more characters are available. if (__c == __eof) __err |= ios_base::eofbit; // Iff valid sequence is not recognized. if (!__testvalid || !__temp_units.size()) __err |= ios_base::failbit; else // Use the "swap trick" to copy __temp_units into __units. __temp_units.swap(__units); return __beg; } template<typename _CharT, typename _OutIter> _OutIter money_put<_CharT, _OutIter>:: do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, long double __units) const { const locale __loc = __io.getloc(); const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc);#ifdef _GLIBCPP_USE_C99 // First try a buffer perhaps big enough. int __cs_size = 64; char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); int __len = __convert_from_v(__cs, __cs_size, "%.01Lf", __units, _S_c_locale); // If the buffer was not large enough, try again with the correct size. if (__len >= __cs_size) { __cs_size = __len + 1; __cs = static_cast<char*>(__builtin_alloca(__cs_size)); __len = __convert_from_v(__cs, __cs_size, "%.01Lf", __units, _S_c_locale); }#else // max_exponent10 + 1 for the integer part, + 4 for sign, decimal point, // decimal digit, '\0'. const int __cs_size = numeric_limits<long double>::max_exponent10 + 5; char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); int __len = __convert_from_v(__cs, 0, "%.01Lf", __units, _S_c_locale);#endif _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __cs_size)); __ctype.widen(__cs, __cs + __len, __ws); string_type __digits(__ws); return this->do_put(__s, __intl, __io, __fill, __digits); } template<typename _CharT, typename _OutIter> _OutIter money_put<_CharT, _OutIter>:: do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, const string_type& __digits) const { typedef typename string_type::size_type size_type; typedef money_base::part part; const locale __loc = __io.getloc(); const size_type __width = static_cast<size_type>(__io.width()); // These contortions are quite unfortunate. typedef moneypunct<_CharT, true> __money_true; typedef moneypunct<_CharT, false> __money_false; const __money_true& __mpt = use_facet<__money_true>(__loc); const __money_false& __mpf = use_facet<__money_false>(__loc); const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); // Determine if negative or positive formats are to be used, and // discard leading negative_sign if it is present. const char_type* __beg = __digits.data(); const char_type* __end = __beg + __digits.size(); money_base::pattern __p; string_type __sign; if (*__beg != __ctype.widen('-')) { __p = __intl ? __mpt.pos_format() : __mpf.pos_format(); __sign =__intl ? __mpt.positive_sign() : __mpf.positive_sign(); } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -