📄 locale_facets.tcc
字号:
{ __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. for (size_type __j = 0; __j < __len; ++__j, ++__s) *__s = __res[__j]; } __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. const char_type* __times[2]; __tp._M_time_formats(__times); _M_extract_via_format(__beg, __end, __io, __err, __tm, __times[0]); break; case 'y': // Two digit year. [tm_year] _M_extract_num(__beg, __end, __tm->tm_year, 0, 99, 2, __ctype, __err); break; case 'Y': // Year [1900). [tm_year] _M_extract_num(__beg, __end, __mem, 0, numeric_limits<int>::max(), 4, __ctype, __err); if (!__err) __tm->tm_year = __mem - 1900; break; case 'Z': // Timezone info. if (__ctype.is(ctype_base::upper, *__beg)) { int __tmp; _M_extract_name(__beg, __end, __tmp, __timepunct<_CharT>::_S_timezones, 14, __err); // GMT requires special effort. char_type __c = *__beg; if (!__err && __tmp == 0 && (__c == __ctype.widen('-') || __c == __ctype.widen('+'))) { _M_extract_num(__beg, __end, __tmp, 0, 23, 2, __ctype, __err); _M_extract_num(__beg, __end, __tmp, 0, 59, 2, __ctype, __err); } } else __err |= ios_base::failbit; break; default: // Not recognized. __err |= ios_base::failbit; } } else { // Verify format and input match, extract and discard. if (__c == __ctype.narrow(*__beg, 0)) ++__beg; else __err |= ios_base::failbit; } } } template<typename _CharT, typename _InIter> void time_get<_CharT, _InIter>:: _M_extract_num(iter_type& __beg, iter_type& __end, int& __member, int __min, int __max, size_t __len, const ctype<_CharT>& __ctype, ios_base::iostate& __err) const { size_t __i = 0; string __digits; bool __testvalid = true; char_type __c = *__beg; while (__beg != __end && __i < __len && __ctype.is(ctype_base::digit, __c)) { __digits += __ctype.narrow(__c, 0); __c = *(++__beg); ++__i; } if (__i == __len) { int __value = atoi(__digits.c_str()); if (__min <= __value && __value <= __max) __member = __value; else __testvalid = false; } else __testvalid = false; if (!__testvalid) __err |= ios_base::failbit; } // Assumptions: // All elements in __names are unique. template<typename _CharT, typename _InIter> void time_get<_CharT, _InIter>:: _M_extract_name(iter_type& __beg, iter_type& __end, int& __member, const _CharT** __names, size_t __indexlen, ios_base::iostate& __err) const { typedef char_traits<_CharT> __traits_type; int* __matches = static_cast<int*>(__builtin_alloca(sizeof(int) * __indexlen)); size_t __nmatches = 0; size_t __pos = 0; bool __testvalid = true; const char_type* __name; char_type __c = *__beg; // Look for initial matches. for (size_t __i1 = 0; __i1 < __indexlen; ++__i1) if (__c == __names[__i1][0]) __matches[__nmatches++] = __i1; while(__nmatches > 1) { // Find smallest matching string. size_t __minlen = 10; for (size_t __i2 = 0; __i2 < __nmatches; ++__i2) __minlen = min(__minlen, __traits_type::length(__names[__matches[__i2]])); if (__pos < __minlen && __beg != __end) { ++__pos; __c = *(++__beg); for (size_t __i3 = 0; __i3 < __nmatches; ++__i3) { __name = __names[__matches[__i3]];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -