in absl/strings/internal/str_format/float_conversion.cc [1305:1396]
bool FloatToSink(const Float v, const FormatConversionSpecImpl &conv,
FormatSinkImpl *sink) {
// Print the sign or the sign column.
Float abs_v = v;
char sign_char = 0;
if (std::signbit(abs_v)) {
sign_char = '-';
abs_v = -abs_v;
} else if (conv.has_show_pos_flag()) {
sign_char = '+';
} else if (conv.has_sign_col_flag()) {
sign_char = ' ';
}
// Print nan/inf.
if (ConvertNonNumericFloats(sign_char, abs_v, conv, sink)) {
return true;
}
int precision = conv.precision() < 0 ? 6 : conv.precision();
int exp = 0;
auto decomposed = Decompose(abs_v);
Buffer buffer;
FormatConversionChar c = conv.conversion_char();
if (c == FormatConversionCharInternal::f ||
c == FormatConversionCharInternal::F) {
FormatF(decomposed.mantissa, decomposed.exponent,
{sign_char, precision, conv, sink});
return true;
} else if (c == FormatConversionCharInternal::e ||
c == FormatConversionCharInternal::E) {
if (!FloatToBuffer<FormatStyle::Precision>(decomposed, precision, &buffer,
&exp)) {
return FallbackToSnprintf(v, conv, sink);
}
if (!conv.has_alt_flag() && buffer.back() == '.') buffer.pop_back();
PrintExponent(
exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e',
&buffer);
} else if (c == FormatConversionCharInternal::g ||
c == FormatConversionCharInternal::G) {
precision = std::max(0, precision - 1);
if (!FloatToBuffer<FormatStyle::Precision>(decomposed, precision, &buffer,
&exp)) {
return FallbackToSnprintf(v, conv, sink);
}
if (precision + 1 > exp && exp >= -4) {
if (exp < 0) {
// Have 1.23456, needs 0.00123456
// Move the first digit
buffer.begin[1] = *buffer.begin;
// Add some zeros
for (; exp < -1; ++exp) *buffer.begin-- = '0';
*buffer.begin-- = '.';
*buffer.begin = '0';
} else if (exp > 0) {
// Have 1.23456, needs 1234.56
// Move the '.' exp positions to the right.
std::rotate(buffer.begin + 1, buffer.begin + 2, buffer.begin + exp + 2);
}
exp = 0;
}
if (!conv.has_alt_flag()) {
while (buffer.back() == '0') buffer.pop_back();
if (buffer.back() == '.') buffer.pop_back();
}
if (exp) {
PrintExponent(
exp, FormatConversionCharIsUpper(conv.conversion_char()) ? 'E' : 'e',
&buffer);
}
} else if (c == FormatConversionCharInternal::a ||
c == FormatConversionCharInternal::A) {
bool uppercase = (c == FormatConversionCharInternal::A);
FormatA(HexFloatTypeParams(Float{}), decomposed.mantissa,
decomposed.exponent, uppercase, {sign_char, precision, conv, sink});
return true;
} else {
return false;
}
WriteBufferToSink(sign_char,
absl::string_view(buffer.begin, buffer.end - buffer.begin),
conv, sink);
return true;
}