From 56c72a671c24434377e619192d485be192ca47b4 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 2 Sep 2022 09:36:41 -0700 Subject: [PATCH] Reduce locale dependency --- .github/workflows/macos.yml | 2 +- include/fmt/format-inl.h | 7 ++++--- include/fmt/locale.h | 19 +++++++++++++++---- test/xchar-test.cc | 4 ++-- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 24dd5958..60aee05c 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -7,7 +7,7 @@ permissions: jobs: build: - runs-on: macos-10.15 + runs-on: macos-11 strategy: matrix: build_type: [Debug, Release] diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index c20a323a..88c599cc 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -31,7 +31,7 @@ #include "locale.h" FMT_BEGIN_NAMESPACE -template std::locale::id num_format_facet::id; +template typename Locale::id num_format_facet::id; namespace detail { @@ -131,8 +131,9 @@ FMT_FUNC auto write_int(unsigned long long value, locale_ref loc) auto out = std::ostreambuf_iterator(&buf); // We cannot use the num_put facet because it may produce output in // a wrong encoding. - using facet_t = conditional_t::value, - num_format_facet<>, std::num_put>; + using facet_t = + conditional_t::value, + num_format_facet, std::num_put>; if (std::has_facet(locale)) { std::use_facet(locale).put(out, ios, ' ', value); return buf.str(); diff --git a/include/fmt/locale.h b/include/fmt/locale.h index 1b22fb47..cc252368 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -8,17 +8,28 @@ #ifndef FMT_LOCALE_H_ #define FMT_LOCALE_H_ -#include // std::num_put +#include // std::ios_base +#include // std::ostreambuf_iterator #include "core.h" FMT_BEGIN_NAMESPACE // A locale facet that formats numeric values in UTF-8. -template -class num_format_facet : public std::num_put { +template class num_format_facet : public Locale::facet { public: - static FMT_API std::locale::id id; + static FMT_API typename Locale::id id; + + using iter_type = std::ostreambuf_iterator; + + auto put(iter_type out, std::ios_base& str, char fill, + unsigned long long val) const -> iter_type { + return do_put(out, str, fill, val); + } + + protected: + virtual auto do_put(iter_type out, std::ios_base& str, char fill, + unsigned long long val) const -> iter_type = 0; }; FMT_END_NAMESPACE diff --git a/test/xchar-test.cc b/test/xchar-test.cc index a47a86be..582b3495 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -521,9 +521,9 @@ TEST(locale_test, sign) { EXPECT_EQ(fmt::format(std::locale(), L"{:L}", -50), L"-50"); } -class num_format : public fmt::num_format_facet<> { +class num_format : public fmt::num_format_facet { protected: - using fmt::num_format_facet<>::do_put; + using fmt::num_format_facet::do_put; iter_type do_put(iter_type out, std::ios_base&, char, unsigned long long) const override; };