diff --git a/include/fmt/locale.h b/include/fmt/locale.h index 7301bf92..4faa40d6 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -52,13 +52,26 @@ inline OutputIt vformat_to( template >::value> -inline auto format_to(OutputIt out, const std::locale& loc, - const S& format_str, Args&&... args) -> +inline auto format_to(OutputIt out, const std::locale& loc, const S& format_str, + Args&&... args) -> typename std::enable_if::type { const auto& vargs = fmt::make_args_checked(format_str, args...); return vformat_to(out, loc, to_string_view(format_str), vargs); } +template > +inline void print(std::FILE* f, const std::locale& loc, const S& format_str, + Args&&... args) { + const auto str = fmt::format(loc, format_str, std::forward(args)...); + + static FMT_CONSTEXPR_DECL const Char print_fmt_str[] = {'{', '}', 0}; + fmt::print(f, FMT_STRING(print_fmt_str), str); +} +template > +inline void print(const std::locale& loc, const S& format_str, Args&&... args) { + print(stdout, loc, format_str, std::forward(args)...); +} + FMT_END_NAMESPACE #endif // FMT_LOCALE_H_ diff --git a/test/locale-test.cc b/test/locale-test.cc index 143348d1..942e1e29 100644 --- a/test/locale-test.cc +++ b/test/locale-test.cc @@ -10,6 +10,7 @@ #include #include "gmock.h" +#include "gtest-extra.h" using fmt::detail::max_value; @@ -139,8 +140,7 @@ template struct formatter, charT> { auto imag = fmt::format(ctx.locale().template get(), "{:" + specs + "}", c.imag()); auto fill_align_width = std::string(); - if (specs_.width > 0) - fill_align_width = fmt::format(">{}", specs_.width); + if (specs_.width > 0) fill_align_width = fmt::format(">{}", specs_.width); return format_to( ctx.out(), "{:" + fill_align_width + "}", fmt::format(c.real() != 0 ? "({0}+{1}i)" : "{1}i", real, imag)); @@ -155,4 +155,14 @@ TEST(FormatTest, Complex) { EXPECT_EQ(fmt::format("{:8}", std::complex(1, 2)), " (1+2i)"); } +TEST(FormatTest, Print) { + std::locale special_grouping_loc(std::locale(), new special_grouping()); + EXPECT_WRITE(stdout, fmt::print(std::locale(), "{:L}", 12345678), "12345678"); + EXPECT_WRITE(stdout, fmt::print(special_grouping_loc, "{:L}", 12345678), + "1,23,45,678"); + EXPECT_WRITE(stdout, fmt::print(special_grouping_loc, "{:L}", 12345), + "12,345"); + EXPECT_WRITE(stderr, fmt::print(stderr, special_grouping_loc, "{:L}", 12345), + "12,345"); +} #endif // FMT_STATIC_THOUSANDS_SEPARATOR