From 19e008876ba1c678d01c94f624d317920f0dbccf Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 14 Nov 2018 15:35:24 -0800 Subject: [PATCH] More locale support --- include/fmt/format.h | 2 +- include/fmt/locale.h | 30 ++++++++++++++++++++++++++++++ test/locale-test.cc | 25 +++++++++++++++++++------ 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 5f055dab..d743e4da 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3468,7 +3468,7 @@ template inline typename std::enable_if::value, OutputIt>::type vformat_to(OutputIt out, const String &format_str, - typename format_args_t::type args) { + typename format_args_t::type args) { typedef output_range range; return vformat_to>(range(out), to_string_view(format_str), args); diff --git a/include/fmt/locale.h b/include/fmt/locale.h index 9b8d29ae..8e021bc4 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -34,6 +34,13 @@ std::basic_string vformat( } } +template +inline std::basic_string vformat( + const std::locale &loc, const S &format_str, + basic_format_args::type> args) { + return internal::vformat(loc, to_string_view(format_str), args); +} + template inline std::basic_string format( const std::locale &loc, const S &format_str, const Args &... args) { @@ -42,6 +49,29 @@ inline std::basic_string format( *internal::checked_args(format_str, args...)); } +template +inline typename std::enable_if::value, + OutputIt>::type + vformat_to(OutputIt out, const std::locale &loc, const String &format_str, + typename format_args_t::type args) { + typedef output_range range; + return vformat_to>( + range(out), to_string_view(format_str), args, internal::locale_ref(loc)); +} + +template +inline typename std::enable_if< + internal::is_string::value && + internal::is_output_iterator::value, OutputIt>::type + format_to(OutputIt out, const std::locale &loc, const S &format_str, + const Args &... args) { + internal::check_format_string(format_str); + typedef typename format_context_t::type context; + format_arg_store as{args...}; + return vformat_to(out, loc, to_string_view(format_str), + basic_format_args(as)); +} + FMT_END_NAMESPACE #endif // FMT_LOCALE_H_ diff --git a/test/locale-test.cc b/test/locale-test.cc index 1abc8c3f..6c8dd0ae 100644 --- a/test/locale-test.cc +++ b/test/locale-test.cc @@ -8,14 +8,27 @@ #include "fmt/locale.h" #include "gmock.h" -struct numpunct : std::numpunct { +template +struct numpunct : std::numpunct { protected: - char do_thousands_sep() const FMT_OVERRIDE { return '~'; } + Char do_thousands_sep() const FMT_OVERRIDE { return '~'; } }; TEST(LocaleTest, Format) { - std::locale loc; - EXPECT_EQ("1~234~567", - fmt::format(std::locale(loc, new numpunct()), "{:n}", 1234567)); - EXPECT_EQ("1,234,567", fmt::format(loc, "{:n}", 1234567)); + std::locale loc(std::locale(), new numpunct()); + EXPECT_EQ("1,234,567", fmt::format(std::locale(), "{:n}", 1234567)); + EXPECT_EQ("1~234~567", fmt::format(loc, "{:n}", 1234567)); + fmt::format_arg_store as{1234567}; + EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:n}", fmt::format_args(as))); + std::string s; + fmt::format_to(std::back_inserter(s), loc, "{:n}", 1234567); + EXPECT_EQ("1~234~567", s); +} + +TEST(LocaleTest, WFormat) { + std::locale loc(std::locale(), new numpunct()); + EXPECT_EQ(L"1,234,567", fmt::format(std::locale(), L"{:n}", 1234567)); + EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:n}", 1234567)); + fmt::format_arg_store as{1234567}; + EXPECT_EQ(L"1~234~567", fmt::vformat(loc, L"{:n}", fmt::wformat_args(as))); }