libstdc++
GNU C++ library
Loading...
Searching...
No Matches

◆ _M_insert_float() [1/2]

template<typename _CharT, typename _OutIter>
template<typename _ValueT>
_OutIter std::num_put< _CharT, _OutIter >::_M_insert_float ( _OutIter __s,
ios_base & __io,
_CharT __fill,
char __mod,
_ValueT __v ) const

Definition at line 999 of file locale_facets.tcc.

1002 {
1005 const locale& __loc = __io._M_getloc();
1006 const __cache_type* __lc = __uc(__loc);
1007
1008 // Use default precision if out of range.
1009 const streamsize __prec = __io.precision() < 0 ? 6 : __io.precision();
1010
1011 const int __max_digits =
1013
1014 // [22.2.2.2.2] Stage 1, numeric conversion to character.
1015 int __len;
1016 // Long enough for the max format spec.
1017 char __fbuf[16];
1019
1020#if _GLIBCXX_USE_C99_STDIO && !_GLIBCXX_HAVE_BROKEN_VSNPRINTF
1021 // Precision is always used except for hexfloat format.
1022 const bool __use_prec =
1024
1025 // First try a buffer perhaps big enough (most probably sufficient
1026 // for non-ios_base::fixed outputs)
1027 int __cs_size = __max_digits * 3;
1028 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1029 if (__use_prec)
1031 __fbuf, __prec, __v);
1032 else
1034 __fbuf, __v);
1035
1036 // If the buffer was not large enough, try again with the correct size.
1037 if (__len >= __cs_size)
1038 {
1039 __cs_size = __len + 1;
1040 __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1041 if (__use_prec)
1043 __fbuf, __prec, __v);
1044 else
1046 __fbuf, __v);
1047 }
1048#else
1049 // Consider the possibility of long ios_base::fixed outputs
1050 const bool __fixed = __io.flags() & ios_base::fixed;
1051 const int __max_exp =
1053
1054 // The size of the output string is computed as follows.
1055 // ios_base::fixed outputs may need up to __max_exp + 1 chars
1056 // for the integer part + __prec chars for the fractional part
1057 // + 3 chars for sign, decimal point, '\0'. On the other hand,
1058 // for non-fixed outputs __max_digits * 2 + __prec chars are
1059 // largely sufficient.
1060 const int __cs_size = __fixed ? __max_exp + __prec + 4
1061 : __max_digits * 2 + __prec;
1062 char* __cs = static_cast<char*>(__builtin_alloca(__cs_size));
1064 __prec, __v);
1065#endif
1066
1067 // [22.2.2.2.2] Stage 2, convert to char_type, using correct
1068 // numpunct.decimal_point() values for '.' and adding grouping.
1070
1071 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1072 * __len));
1073 __ctype.widen(__cs, __cs + __len, __ws);
1074
1075 // Replace decimal point.
1076 _CharT* __wp = 0;
1077 const char* __p = char_traits<char>::find(__cs, __len, '.');
1078 if (__p)
1079 {
1080 __wp = __ws + (__p - __cs);
1081 *__wp = __lc->_M_decimal_point;
1082 }
1083
1084 // Add grouping, if necessary.
1085 // N.B. Make sure to not group things like 2e20, i.e., no decimal
1086 // point, scientific notation.
1087 if (__lc->_M_use_grouping
1088 && (__wp || __len < 3 || (__cs[1] <= '9' && __cs[2] <= '9'
1089 && __cs[1] >= '0' && __cs[2] >= '0')))
1090 {
1091 // Grouping can add (almost) as many separators as the
1092 // number of digits, but no more.
1093 _CharT* __ws2 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1094 * __len * 2));
1095
1096 streamsize __off = 0;
1097 if (__cs[0] == '-' || __cs[0] == '+')
1098 {
1099 __off = 1;
1100 __ws2[0] = __ws[0];
1101 __len -= 1;
1102 }
1103
1104 _M_group_float(__lc->_M_grouping, __lc->_M_grouping_size,
1105 __lc->_M_thousands_sep, __wp, __ws2 + __off,
1106 __ws + __off, __len);
1107 __len += __off;
1108
1109 __ws = __ws2;
1110 }
1111
1112 // Pad.
1113 const streamsize __w = __io.width();
1114 if (__w > static_cast<streamsize>(__len))
1115 {
1116 _CharT* __ws3 = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
1117 * __w));
1119 __ws = __ws3;
1120 }
1121 __io.width(0);
1122
1123 // [22.2.2.2.2] Stage 4.
1124 // Write resulting, fully-formatted string to output iterator.
1125 return std::__write(__s, __ws, __len);
1126 }
ostreambuf_iterator< _CharT > __write(ostreambuf_iterator< _CharT > __s, const _CharT *__ws, int __len)
int __convert_from_v(const __c_locale &__cloc, char *__out, const int __size, const char *__fmt,...)
Definition c++locale.h:74
static constexpr const char_type * find(const char_type *__s, std::size_t __n, const char_type &__a)
static __c_locale _S_get_c_locale()
static void _S_format_float(const ios_base &__io, char *__fptr, char __mod)
void _M_pad(char_type __fill, streamsize __w, ios_base &__io, char_type *__new, const char_type *__cs, int &__len) const
void _M_group_float(const char *__grouping, size_t __grouping_size, char_type __sep, const char_type *__p, char_type *__new, char_type *__cs, int &__len) const
Primary class template num_put.