mirror of
https://github.com/shadps4-emu/ext-fmt.git
synced 2024-11-23 09:49:42 +00:00
Ignore zero-padding for non-finite floating points (#2310)
* Ignore zero-padding for non-finite floating points * keep width for non-finite formatting with 0-padding * clang-format * preserve alignment * align code-style
This commit is contained in:
parent
7612f18dc8
commit
a70a4ae053
@ -1947,7 +1947,7 @@ template <typename Char> class specs_setter {
|
||||
FMT_CONSTEXPR void on_localized() { specs_.localized = true; }
|
||||
|
||||
FMT_CONSTEXPR void on_zero() {
|
||||
specs_.align = align::numeric;
|
||||
if (specs_.align == align::none) specs_.align = align::numeric;
|
||||
specs_.fill[0] = Char('0');
|
||||
}
|
||||
|
||||
|
@ -1588,13 +1588,17 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, const Char* s,
|
||||
|
||||
template <typename Char, typename OutputIt>
|
||||
OutputIt write_nonfinite(OutputIt out, bool isinf,
|
||||
const basic_format_specs<Char>& specs,
|
||||
basic_format_specs<Char> specs,
|
||||
const float_specs& fspecs) {
|
||||
auto str =
|
||||
isinf ? (fspecs.upper ? "INF" : "inf") : (fspecs.upper ? "NAN" : "nan");
|
||||
constexpr size_t str_size = 3;
|
||||
auto sign = fspecs.sign;
|
||||
auto size = str_size + (sign ? 1 : 0);
|
||||
// Replace '0'-padding with space for non-finite values.
|
||||
const bool is_zero_fill =
|
||||
specs.fill.size() == 1 && *specs.fill.data() == static_cast<Char>('0');
|
||||
if (is_zero_fill) specs.fill[0] = static_cast<Char>(' ');
|
||||
return write_padded(out, specs, size, [=](reserve_iterator<OutputIt> it) {
|
||||
if (sign) *it++ = static_cast<Char>(data::signs[sign]);
|
||||
return copy_str<Char>(str, str + str_size, it);
|
||||
|
@ -1272,10 +1272,16 @@ TEST(format_test, format_nan) {
|
||||
double nan = std::numeric_limits<double>::quiet_NaN();
|
||||
EXPECT_EQ("nan", fmt::format("{}", nan));
|
||||
EXPECT_EQ("+nan", fmt::format("{:+}", nan));
|
||||
if (std::signbit(-nan))
|
||||
EXPECT_EQ(" +nan", fmt::format("{:+06}", nan));
|
||||
EXPECT_EQ("+nan ", fmt::format("{:<+06}", nan));
|
||||
EXPECT_EQ(" +nan ", fmt::format("{:^+06}", nan));
|
||||
EXPECT_EQ(" +nan", fmt::format("{:>+06}", nan));
|
||||
if (std::signbit(-nan)) {
|
||||
EXPECT_EQ("-nan", fmt::format("{}", -nan));
|
||||
else
|
||||
EXPECT_EQ(" -nan", fmt::format("{:+06}", -nan));
|
||||
} else {
|
||||
fmt::print("Warning: compiler doesn't handle negative NaN correctly");
|
||||
}
|
||||
EXPECT_EQ(" nan", fmt::format("{: }", nan));
|
||||
EXPECT_EQ("NAN", fmt::format("{:F}", nan));
|
||||
EXPECT_EQ("nan ", fmt::format("{:<7}", nan));
|
||||
@ -1288,6 +1294,11 @@ TEST(format_test, format_infinity) {
|
||||
EXPECT_EQ("inf", fmt::format("{}", inf));
|
||||
EXPECT_EQ("+inf", fmt::format("{:+}", inf));
|
||||
EXPECT_EQ("-inf", fmt::format("{}", -inf));
|
||||
EXPECT_EQ(" +inf", fmt::format("{:+06}", inf));
|
||||
EXPECT_EQ(" -inf", fmt::format("{:+06}", -inf));
|
||||
EXPECT_EQ("+inf ", fmt::format("{:<+06}", inf));
|
||||
EXPECT_EQ(" +inf ", fmt::format("{:^+06}", inf));
|
||||
EXPECT_EQ(" +inf", fmt::format("{:>+06}", inf));
|
||||
EXPECT_EQ(" inf", fmt::format("{: }", inf));
|
||||
EXPECT_EQ("INF", fmt::format("{:F}", inf));
|
||||
EXPECT_EQ("inf ", fmt::format("{:<7}", inf));
|
||||
|
Loading…
Reference in New Issue
Block a user