Skip to content

Commit

Permalink
More locale support
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 15, 2018
1 parent f2ee988 commit 19e0088
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3468,7 +3468,7 @@ template <typename String, typename OutputIt, typename... Args>
inline typename std::enable_if<internal::is_output_iterator<OutputIt>::value,
OutputIt>::type
vformat_to(OutputIt out, const String &format_str,
typename format_args_t<OutputIt, FMT_CHAR(String)>::type args) {
typename format_args_t<OutputIt, FMT_CHAR(String)>::type args) {
typedef output_range<OutputIt, FMT_CHAR(String)> range;
return vformat_to<arg_formatter<range>>(range(out),
to_string_view(format_str), args);
Expand Down
30 changes: 30 additions & 0 deletions include/fmt/locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ std::basic_string<Char> vformat(
}
}

template <typename S, typename Char = FMT_CHAR(S)>
inline std::basic_string<Char> vformat(
const std::locale &loc, const S &format_str,
basic_format_args<typename buffer_context<Char>::type> args) {
return internal::vformat(loc, to_string_view(format_str), args);
}

template <typename S, typename... Args>
inline std::basic_string<FMT_CHAR(S)> format(
const std::locale &loc, const S &format_str, const Args &... args) {
Expand All @@ -42,6 +49,29 @@ inline std::basic_string<FMT_CHAR(S)> format(
*internal::checked_args<S, Args...>(format_str, args...));
}

template <typename String, typename OutputIt, typename... Args>
inline typename std::enable_if<internal::is_output_iterator<OutputIt>::value,
OutputIt>::type
vformat_to(OutputIt out, const std::locale &loc, const String &format_str,
typename format_args_t<OutputIt, FMT_CHAR(String)>::type args) {
typedef output_range<OutputIt, FMT_CHAR(String)> range;
return vformat_to<arg_formatter<range>>(
range(out), to_string_view(format_str), args, internal::locale_ref(loc));
}

template <typename OutputIt, typename S, typename... Args>
inline typename std::enable_if<
internal::is_string<S>::value &&
internal::is_output_iterator<OutputIt>::value, OutputIt>::type
format_to(OutputIt out, const std::locale &loc, const S &format_str,
const Args &... args) {
internal::check_format_string<Args...>(format_str);
typedef typename format_context_t<OutputIt, FMT_CHAR(S)>::type context;
format_arg_store<context, Args...> as{args...};
return vformat_to(out, loc, to_string_view(format_str),
basic_format_args<context>(as));
}

FMT_END_NAMESPACE

#endif // FMT_LOCALE_H_
25 changes: 19 additions & 6 deletions test/locale-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@
#include "fmt/locale.h"
#include "gmock.h"

struct numpunct : std::numpunct<char> {
template <typename Char>
struct numpunct : std::numpunct<Char> {
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<char>());
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<fmt::format_context, int> 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<wchar_t>());
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<fmt::wformat_context, int> as{1234567};
EXPECT_EQ(L"1~234~567", fmt::vformat(loc, L"{:n}", fmt::wformat_args(as)));
}

0 comments on commit 19e0088

Please sign in to comment.