📄 locale_facets.tcc
字号:
} 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.size() > 1 && __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)); // _GLIBCXX_RESOLVE_LIB_DEFECTS // 328. Bad sprintf format modifier in money_put<>::do_put() int __len = __convert_from_v(__cs, __cs_size, "%.0Lf", __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, "%.0Lf", __units, _S_c_locale); }#else // max_exponent10 + 1 for the integer part, + 2 for sign and '\0'. const int __cs_size = numeric_limits<long double>::max_exponent10 + 3; char* __cs = static_cast<char*>(__builtin_alloca(__cs_size)); int __len = __convert_from_v(__cs, 0, "%.0Lf", __units, _S_c_locale);#endif _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __cs_size)); __ctype.widen(__cs, __cs + __len, __ws); const string_type __digits(__ws, __len); 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 { __p = __intl ? __mpt.neg_format() : __mpf.neg_format(); __sign =__intl ? __mpt.negative_sign() : __mpf.negative_sign(); ++__beg; } // Look for valid numbers in the current ctype facet within input digits. __end = __ctype.scan_not(ctype_base::digit, __beg, __end); if (__beg != __end) { // Assume valid input, and attempt to format. // Break down input numbers into base components, as follows: // final_value = grouped units + (decimal point) + (digits) string_type __res; string_type __value; const string_type __symbol = __intl ? __mpt.curr_symbol() : __mpf.curr_symbol(); // Deal with decimal point, decimal digits. const int __frac = __intl ? __mpt.frac_digits() : __mpf.frac_digits(); if (__frac > 0) { const char_type __d = __intl ? __mpt.decimal_point() : __mpf.decimal_point(); if (__end - __beg >= __frac) { __value = string_type(__end - __frac, __end); __value.insert(__value.begin(), __d); __end -= __frac; } else { // Have to pad zeros in the decimal position. __value = string_type(__beg, __end); int __paddec = __frac - (__end - __beg); char_type __zero = __ctype.widen('0'); __value.insert(__value.begin(), __paddec, __zero); __value.insert(__value.begin(), __d); __beg = __end; } } // Add thousands separators to non-decimal digits, per // grouping rules. if (__beg != __end) { const string __grouping = __intl ? __mpt.grouping() : __mpf.grouping(); if (__grouping.size()) { const char_type __sep = __intl ? __mpt.thousands_sep() : __mpf.thousands_sep(); const char* __gbeg = __grouping.c_str(); const char* __gend = __gbeg + __grouping.size(); const int __n = (__end - __beg) * 2; _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __n)); _CharT* __ws_end = __add_grouping(__ws2, __sep, __gbeg, __gend, __beg, __end); __value.insert(0, __ws2, __ws_end - __ws2); } else __value.insert(0, string_type(__beg, __end)); } // Calculate length of resulting string. ios_base::fmtflags __f = __io.flags() & ios_base::adjustfield; size_type __len = __value.size() + __sign.size(); __len += (__io.flags() & ios_base::showbase) ? __symbol.size() : 0; bool __testipad = __f == ios_base::internal && __len < __width; // Fit formatted digits into the required pattern. for (int __i = 0; __i < 4; ++__i) { part __which = static_cast<part>(__p.field[__i]); switch (__which) { case money_base::symbol: if (__io.flags() & ios_base::showbase) __res += __symbol; break; case money_base::sign: // Sign might not exist, or be more than one // charater long. In that case, add in the rest // below. if (__sign.size()) __res += __sign[0]; break; case money_base::value: __res += __value; break; case money_base::space: // At least one space is required, but if internal // formatting is required, an arbitrary number of // fill spaces will be necessary. if (__testipad) __res += string_type(__width - __len, __fill); else __res += __ctype.widen(__fill); break; case money_base::none: if (__testipad) __res += string_type(__width - __len, __fill); break; } } // Special case of multi-part sign parts. if (__sign.size() > 1) __res += string_type(__sign.begin() + 1, __sign.end()); // Pad, if still necessary. __len = __res.size(); if (__width > __len) { if (__f == ios_base::left) // After. __res.append(__width - __len, __fill); else // Before. __res.insert(0, string_type(__width - __len, __fill)); __len = __width; } // Write resulting, fully-formatted string to output iterator. __s = __write(__s, __res.c_str(), __len); } __io.width(0); return __s; } // NB: Not especially useful. Without an ios_base object or some // kind of locale reference, we are left clawing at the air where // the side of the mountain used to be... template<typename _CharT, typename _InIter> time_base::dateorder time_get<_CharT, _InIter>::do_date_order() const { return time_base::no_order; } template<typename _CharT, typename _InIter> void time_get<_CharT, _InIter>:: _M_extract_via_format(iter_type& __beg, iter_type& __end, ios_base& __io, ios_base::iostate& __err, tm* __tm, const _CharT* __format) const { locale __loc = __io.getloc(); __timepunct<_CharT> const& __tp = use_facet<__timepunct<_CharT> >(__loc); const ctype<_CharT>& __ctype = use_facet<ctype<_CharT> >(__loc); size_t __len = char_traits<_CharT>::length(__format); for (size_t __i = 0; __beg != __end && __i < __len && !__err; ++__i) { char __c = __format[__i]; if (__c == '%') { // Verify valid formatting code, attempt to extract. __c = __format[++__i]; char __mod = 0; int __mem = 0; if (__c == 'E' || __c == 'O') { __mod = __c; __c = __format[++__i]; } switch (__c) { const char* __cs; _CharT __wcs[10]; case 'a': // Abbreviated weekday name [tm_wday] const char_type* __days1[7]; __tp._M_days_abbreviated(__days1); _M_extract_name(__beg, __end, __tm->tm_wday, __days1, 7, __err); break; case 'A': // Weekday name [tm_wday]. const char_type* __days2[7]; __tp._M_days(__days2); _M_extract_name(__beg, __end, __tm->tm_wday, __days2, 7, __err); break; case 'h': case 'b': // Abbreviated month name [tm_mon] const char_type* __months1[12]; __tp._M_months_abbreviated(__months1); _M_extract_name(__beg, __end, __tm->tm_mon, __months1, 12, __err); break; case 'B': // Month name [tm_mon]. const char_type* __months2[12]; __tp._M_months(__months2); _M_extract_name(__beg, __end, __tm->tm_mon, __months2, 12, __err); break; case 'c': // Default time and date representation. const char_type* __dt[2]; __tp._M_date_time_formats(__dt); _M_extract_via_format(__beg, __end, __io, __err, __tm, __dt[0]); break; case 'd': // Day [01, 31]. [tm_mday] _M_extract_num(__beg, __end, __tm->tm_mday, 1, 31, 2, __ctype, __err); break; case 'D': // Equivalent to %m/%d/%y.[tm_mon, tm_mday, tm_year] __cs = "%m/%d/%y"; __ctype.widen(__cs, __cs + 9, __wcs); _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs); break; case 'H': // Hour [00, 23]. [tm_hour] _M_extract_num(__beg, __end, __tm->tm_hour, 0, 23, 2, __ctype, __err); break; case 'I': // Hour [01, 12]. [tm_hour] _M_extract_num(__beg, __end, __tm->tm_hour, 1, 12, 2, __ctype, __err); break; case 'm': // Month [01, 12]. [tm_mon] _M_extract_num(__beg, __end, __mem, 1, 12, 2, __ctype, __err); if (!__err) __tm->tm_mon = __mem - 1; break; case 'M': // Minute [00, 59]. [tm_min] _M_extract_num(__beg, __end, __tm->tm_min, 0, 59, 2, __ctype, __err); break; case 'n': if (__ctype.narrow(*__beg, 0) == '\n') ++__beg; else __err |= ios_base::failbit; break; case 'R': // Equivalent to (%H:%M). __cs = "%H:%M"; __ctype.widen(__cs, __cs + 6, __wcs); _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs); break; case 'S': // Seconds. _M_extract_num(__beg, __end, __tm->tm_sec, 0, 59, 2, __ctype, __err); break; case 't': if (__ctype.narrow(*__beg, 0) == '\t') ++__beg; else __err |= ios_base::failbit; break; case 'T': // Equivalent to (%H:%M:%S). __cs = "%H:%M:%S"; __ctype.widen(__cs, __cs + 9, __wcs); _M_extract_via_format(__beg, __end, __io, __err, __tm, __wcs); break; case 'x': // Locale's date. const char_type* __dates[2]; __tp._M_date_formats(__dates); _M_extract_via_format(__beg, __end, __io, __err, __tm, __dates[0]); break; case 'X': // Locale's time.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -