workaround MSVC bug, add test

This commit is contained in:
Walter Gray 2020-11-13 17:16:08 -08:00
parent 03c29ded73
commit 2c271ac98c
2 changed files with 37 additions and 13 deletions

View File

@ -763,19 +763,29 @@ inline std::chrono::duration<Rep, std::milli> get_milliseconds(
} }
template <typename Char, typename Rep, typename OutputIt, template <typename Char, typename Rep, typename OutputIt,
FMT_ENABLE_IF(std::is_integral<Rep>::value)> FMT_ENABLE_IF(std::is_integral<Rep>::value && std::is_same<Char, char>::value)>
OutputIt format_duration_value(OutputIt out, Rep val, int) { OutputIt format_duration_value(OutputIt out, Rep val, int) {
static FMT_CONSTEXPR_DECL const Char format[] = {'{', '}', 0}; return format_to(out, FMT_STRING("{}"), val);
return format_to(out, FMT_STRING(format), val);
} }
template <typename Char, typename Rep, typename OutputIt, template <typename Char, typename Rep, typename OutputIt,
FMT_ENABLE_IF(std::is_floating_point<Rep>::value)> FMT_ENABLE_IF(std::is_integral<Rep>::value && std::is_same<Char, wchar_t>::value)>
OutputIt format_duration_value(OutputIt out, Rep val, int) {
return format_to(out, FMT_STRING(L"{}"), val);
}
template <typename Char, typename Rep, typename OutputIt,
FMT_ENABLE_IF(std::is_floating_point<Rep>::value && std::is_same<Char, char>::value)>
OutputIt format_duration_value(OutputIt out, Rep val, int precision) { OutputIt format_duration_value(OutputIt out, Rep val, int precision) {
static FMT_CONSTEXPR_DECL const Char pr_f[] = {'{', ':', '.', '{', '}', 'f', '}', 0}; if (precision >= 0) return format_to(out, FMT_STRING("{:.{}f}"), val, precision);
if (precision >= 0) return format_to(out, FMT_STRING(pr_f), val, precision); return format_to(out, FMT_STRING("{:g}"), val);
static FMT_CONSTEXPR_DECL const Char fp_f[] = {'{', ':', 'g', '}', 0}; }
return format_to(out, FMT_STRING(fp_f), val);
template <typename Char, typename Rep, typename OutputIt,
FMT_ENABLE_IF(std::is_floating_point<Rep>::value && std::is_same<Char, wchar_t>::value)>
OutputIt format_duration_value(OutputIt out, Rep val, int precision) {
if (precision >= 0) return format_to(out, FMT_STRING("L{:.{}f}"), val, precision);
return format_to(out, FMT_STRING("L{:g}"), val);
} }
template <typename Char, typename OutputIt> template <typename Char, typename OutputIt>
@ -791,14 +801,22 @@ OutputIt copy_unit(string_view unit, OutputIt out, wchar_t) {
return std::copy(u.c_str(), u.c_str() + u.size(), out); return std::copy(u.c_str(), u.c_str() + u.size(), out);
} }
template <typename Char, typename Period, typename OutputIt> template <typename Char, typename Period, typename OutputIt,
FMT_ENABLE_IF(std::is_same<Char, char>::value)>
OutputIt format_duration_unit(OutputIt out) { OutputIt format_duration_unit(OutputIt out) {
if (const char* unit = get_units<Period>()) if (const char* unit = get_units<Period>())
return copy_unit(string_view(unit), out, Char()); return copy_unit(string_view(unit), out, Char());
static FMT_CONSTEXPR_DECL const Char num_f[] = {'[', '{', '}', ']', 's', 0}; if (const_check(Period::den == 1)) return format_to(out, FMT_STRING("[{}]s"), Period::num);
if (const_check(Period::den == 1)) return format_to(out, FMT_STRING(num_f), Period::num); return format_to(out, FMT_STRING("[{}/{}]s"), Period::num, Period::den);
static FMT_CONSTEXPR_DECL const Char num_def_f[] = {'[', '{', '}', '/', '{', '}', ']', 's', 0}; }
return format_to(out, FMT_STRING(num_def_f), Period::num, Period::den);
template <typename Char, typename Period, typename OutputIt,
FMT_ENABLE_IF(std::is_same<Char, wchar_t>::value)>
OutputIt format_duration_unit(OutputIt out) {
if (const char* unit = get_units<Period>())
return copy_unit(string_view(unit), out, Char());
if (const_check(Period::den == 1)) return format_to(out, FMT_STRING(L"[{}]s"), Period::num);
return format_to(out, FMT_STRING(L"[{}/{}]s"), Period::num, Period::den);
} }
template <typename FormatContext, typename OutputIt, typename Rep, template <typename FormatContext, typename OutputIt, typename Rep,

View File

@ -1809,9 +1809,15 @@ constexpr char with_null[3] = {'{', '}', '\0'};
constexpr char no_null[2] = {'{', '}'}; constexpr char no_null[2] = {'{', '}'};
TEST(FormatTest, CompileTimeString) { TEST(FormatTest, CompileTimeString) {
static FMT_CONSTEXPR_DECL const char static_with_null[3] = {'{', '}', '\0'};
static FMT_CONSTEXPR_DECL const wchar_t static_with_null_wide[3] = {'{', '}', '\0'};
EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42)); EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), 42));
EXPECT_EQ(L"42", fmt::format(FMT_STRING(L"{}"), 42)); EXPECT_EQ(L"42", fmt::format(FMT_STRING(L"{}"), 42));
EXPECT_EQ("foo", fmt::format(FMT_STRING("{}"), string_like())); EXPECT_EQ("foo", fmt::format(FMT_STRING("{}"), string_like()));
EXPECT_EQ("42", fmt::format(FMT_STRING(static_with_null), 42));
EXPECT_EQ(L"42", fmt::format(FMT_STRING(static_with_null_wide), 42));
(void)with_null; (void)with_null;
(void)no_null; (void)no_null;
#if __cplusplus >= 201703L #if __cplusplus >= 201703L