Make % an opt-in to improve compatibility with std::format
diff --git a/include/fmt/format.h b/include/fmt/format.h
index fd2e789..2e048a4 100644
--- a/include/fmt/format.h
+++ b/include/fmt/format.h
@@ -195,6 +195,11 @@
# define FMT_NUMERIC_ALIGN 1
#endif
+// Enable the deprecated percent specifier.
+#ifndef FMT_DEPRECATED_PERCENT
+# define FMT_DEPRECATED_PERCENT 0
+#endif
+
FMT_BEGIN_NAMESPACE
namespace internal {
@@ -1284,8 +1289,10 @@
result.format = float_format::fixed;
break;
case '%':
+#if FMT_DEPRECATED_PERCENT
result.format = float_format::fixed;
result.percent = true;
+#endif
break;
case 'A':
result.upper = true;
diff --git a/test/format-test.cc b/test/format-test.cc
index af7ba52..66d87c2 100644
--- a/test/format-test.cc
+++ b/test/format-test.cc
@@ -1202,8 +1202,6 @@
"precision not allowed for this argument type");
EXPECT_THROW_MSG(format("{0:.2f}", 42ull), format_error,
"precision not allowed for this argument type");
- EXPECT_THROW_MSG(format("{0:.2%}", 42), format_error,
- "precision not allowed for this argument type");
EXPECT_THROW_MSG(format("{0:3.0}", 'x'), format_error,
"precision not allowed for this argument type");
EXPECT_EQ("1.2", format("{0:.2}", 1.2345));
@@ -1488,7 +1486,6 @@
TEST(FormatterTest, FormatFloat) {
EXPECT_EQ("392.500000", format("{0:f}", 392.5f));
- EXPECT_EQ("12.500000%", format("{0:%}", 0.125f));
}
TEST(FormatterTest, FormatDouble) {
@@ -1501,8 +1498,6 @@
EXPECT_EQ("392.65", format("{:G}", 392.65));
EXPECT_EQ("392.650000", format("{:f}", 392.65));
EXPECT_EQ("392.650000", format("{:F}", 392.65));
- EXPECT_EQ("12.500000%", format("{:%}", 0.125));
- EXPECT_EQ("12.34%", format("{:.2%}", 0.1234432));
char buffer[BUFFER_SIZE];
safe_sprintf(buffer, "%e", 392.65);
EXPECT_EQ(buffer, format("{0:e}", 392.65));
@@ -1542,7 +1537,6 @@
EXPECT_EQ("nan ", format("{:<7}", nan));
EXPECT_EQ(" nan ", format("{:^7}", nan));
EXPECT_EQ(" nan", format("{:>7}", nan));
- EXPECT_EQ("nan%", format("{:%}", nan));
}
TEST(FormatterTest, FormatInfinity) {
@@ -1555,7 +1549,6 @@
EXPECT_EQ("inf ", format("{:<7}", inf));
EXPECT_EQ(" inf ", format("{:^7}", inf));
EXPECT_EQ(" inf", format("{:>7}", inf));
- EXPECT_EQ("inf%", format("{:%}", inf));
}
TEST(FormatterTest, FormatLongDouble) {
@@ -1566,8 +1559,6 @@
EXPECT_EQ("392.65", format("{0:G}", 392.65l));
EXPECT_EQ("392.650000", format("{0:f}", 392.65l));
EXPECT_EQ("392.650000", format("{0:F}", 392.65l));
- EXPECT_EQ("12.500000%", format("{:%}", 0.125l));
- EXPECT_EQ("12.34%", format("{:.2%}", 0.1234432l));
char buffer[BUFFER_SIZE];
safe_sprintf(buffer, "%Le", 392.65l);
EXPECT_EQ(buffer, format("{0:e}", 392.65l));