text
stringlengths
0
2.2M
index == '^' ? FormatArg::Align::CENTER :
FormatArg::Align::INVALID;
// clang-format on
}
};
// ctor for items in the conv tables for representing parts of nonnegative
// integers into ascii digits of length Size, over a given base Base
template <std::size_t Base, std::size_t Size, bool Upper = false>
struct format_table_conv_make_item {
static_assert(Base <= 36, "Base is unrepresentable");
struct make_item {
std::size_t index{};
constexpr explicit make_item(std::size_t index_) : index(index_) {} // gcc49
constexpr char alpha(std::size_t ord) const {
return static_cast<char>(
ord < 10 ? '0' + ord : (Upper ? 'A' : 'a') + (ord - 10));
}
constexpr char operator()(std::size_t offset) const {
return alpha(index / constexpr_pow(Base, Size - offset - 1) % Base);
}
};
constexpr std::array<char, Size> operator()(std::size_t index) const {
return make_array_with<Size>(make_item{index});
}
};
// ctor for items in the sign table
struct format_table_sign_make_item {
static constexpr std::size_t size = 256;
constexpr FormatArg::Sign operator()(std::size_t index) const {
// clang-format off
return
index == '+' ? FormatArg::Sign::PLUS_OR_MINUS :
index == '-' ? FormatArg::Sign::MINUS :
index == ' ' ? FormatArg::Sign::SPACE_OR_MINUS :
FormatArg::Sign::INVALID;
// clang-format on
}
};
// the tables
FOLLY_STORAGE_CONSTEXPR auto formatAlignTable =
make_array_with<256>(format_table_align_make_item{});
FOLLY_STORAGE_CONSTEXPR auto formatSignTable =
make_array_with<256>(format_table_sign_make_item{});
FOLLY_STORAGE_CONSTEXPR decltype(formatHexLower) formatHexLower =
make_array_with<256>(format_table_conv_make_item<16, 2, false>{});
FOLLY_STORAGE_CONSTEXPR decltype(formatHexUpper) formatHexUpper =
make_array_with<256>(format_table_conv_make_item<16, 2, true>{});
FOLLY_STORAGE_CONSTEXPR decltype(formatOctal) formatOctal =
make_array_with<512>(format_table_conv_make_item<8, 3>{});
FOLLY_STORAGE_CONSTEXPR decltype(formatBinary) formatBinary =
make_array_with<256>(format_table_conv_make_item<2, 8>{});
} // namespace detail
using namespace folly::detail;
void FormatValue<double>::formatHelper(
fbstring& piece, int& prefixLen, FormatArg& arg) const {
using ::double_conversion::DoubleToStringConverter;
using ::double_conversion::StringBuilder;
arg.validate(FormatArg::Type::FLOAT);
if (arg.presentation == FormatArg::kDefaultPresentation) {
arg.presentation = 'g';
}
const char* infinitySymbol = isupper(arg.presentation) ? "INF" : "inf";
const char* nanSymbol = isupper(arg.presentation) ? "NAN" : "nan";
char exponentSymbol = isupper(arg.presentation) ? 'E' : 'e';
if (arg.precision == FormatArg::kDefaultPrecision) {
arg.precision = 6;
}
// 2+: for null terminator and optional sign shenanigans.
constexpr int bufLen = 2 +
constexpr_max(2 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
constexpr_max(
8 + DoubleToStringConverter::kMaxExponentialDigits,
7 + DoubleToStringConverter::kMaxPrecisionDigits));
char buf[bufLen];
StringBuilder builder(buf + 1, bufLen - 1);
char plusSign;
switch (arg.sign) {
case FormatArg::Sign::PLUS_OR_MINUS:
plusSign = '+';
break;
case FormatArg::Sign::SPACE_OR_MINUS:
plusSign = ' ';
break;
case FormatArg::Sign::DEFAULT:
case FormatArg::Sign::MINUS:
case FormatArg::Sign::INVALID:
default: