Simplify locale handling

This commit is contained in:
Victor Zverovich
2025-08-31 08:27:04 -07:00
parent 69324379f2
commit 489fd7ca4b
9 changed files with 116 additions and 127 deletions

View File

@@ -415,11 +415,11 @@ locale:
that take `std::locale` as a parameter. The locale type is a template that take `std::locale` as a parameter. The locale type is a template
parameter to avoid the expensive `<locale>` include. parameter to avoid the expensive `<locale>` include.
::: format(const Locale&, format_string<T...>, T&&...) ::: format(locale_ref, format_string<T...>, T&&...)
::: format_to(OutputIt, const Locale&, format_string<T...>, T&&...) ::: format_to(OutputIt, locale_ref, format_string<T...>, T&&...)
::: formatted_size(const Locale&, format_string<T...>, T&&...) ::: formatted_size(locale_ref, format_string<T...>, T&&...)
<a id="legacy-checks"></a> <a id="legacy-checks"></a>
### Legacy Compile-Time Checks ### Legacy Compile-Time Checks

View File

@@ -902,6 +902,29 @@ template <typename Char = char> class parse_context {
FMT_CONSTEXPR void check_dynamic_spec(int arg_id); FMT_CONSTEXPR void check_dynamic_spec(int arg_id);
}; };
#ifndef FMT_USE_LOCALE
# define FMT_USE_LOCALE (FMT_OPTIMIZE_SIZE <= 1)
#endif
// A type-erased reference to std::locale to avoid the heavy <locale> include.
class locale_ref {
#if FMT_USE_LOCALE
private:
const void* locale_; // A type-erased pointer to std::locale.
public:
constexpr locale_ref() : locale_(nullptr) {}
template <typename Locale, FMT_ENABLE_IF(sizeof(Locale::collate) != 0)>
locale_ref(const Locale& loc);
inline explicit operator bool() const noexcept { return locale_ != nullptr; }
#endif // FMT_USE_LOCALE
public:
template <typename Locale> auto get() const -> Locale;
};
FMT_END_EXPORT FMT_END_EXPORT
namespace detail { namespace detail {
@@ -2301,27 +2324,6 @@ struct is_output_iterator<
enable_if_t<std::is_assignable<decltype(*std::declval<decay_t<It>&>()++), enable_if_t<std::is_assignable<decltype(*std::declval<decay_t<It>&>()++),
T>::value>> : std::true_type {}; T>::value>> : std::true_type {};
#ifndef FMT_USE_LOCALE
# define FMT_USE_LOCALE (FMT_OPTIMIZE_SIZE <= 1)
#endif
// A type-erased reference to an std::locale to avoid a heavy <locale> include.
class locale_ref {
#if FMT_USE_LOCALE
private:
const void* locale_; // A type-erased pointer to std::locale.
public:
constexpr locale_ref() : locale_(nullptr) {}
template <typename Locale> locale_ref(const Locale& loc);
inline explicit operator bool() const noexcept { return locale_ != nullptr; }
#endif // FMT_USE_LOCALE
public:
template <typename Locale> auto get() const -> Locale;
};
template <typename> constexpr auto encode_types() -> unsigned long long { template <typename> constexpr auto encode_types() -> unsigned long long {
return 0; return 0;
} }
@@ -2665,7 +2667,7 @@ class context {
private: private:
appender out_; appender out_;
format_args args_; format_args args_;
FMT_NO_UNIQUE_ADDRESS detail::locale_ref loc_; FMT_NO_UNIQUE_ADDRESS locale_ref loc_;
public: public:
using char_type = char; ///< The character type for the output. using char_type = char; ///< The character type for the output.
@@ -2675,8 +2677,7 @@ class context {
/// Constructs a `context` object. References to the arguments are stored /// Constructs a `context` object. References to the arguments are stored
/// in the object so make sure they have appropriate lifetimes. /// in the object so make sure they have appropriate lifetimes.
FMT_CONSTEXPR context(iterator out, format_args args, FMT_CONSTEXPR context(iterator out, format_args args, locale_ref loc = {})
detail::locale_ref loc = {})
: out_(out), args_(args), loc_(loc) {} : out_(out), args_(args), loc_(loc) {}
context(context&&) = default; context(context&&) = default;
context(const context&) = delete; context(const context&) = delete;
@@ -2697,7 +2698,7 @@ class context {
// Advances the begin iterator to `it`. // Advances the begin iterator to `it`.
FMT_CONSTEXPR void advance_to(iterator) {} FMT_CONSTEXPR void advance_to(iterator) {}
FMT_CONSTEXPR auto locale() const -> detail::locale_ref { return loc_; } FMT_CONSTEXPR auto locale() const -> locale_ref { return loc_; }
}; };
template <typename Char = char> struct runtime_format_string { template <typename Char = char> struct runtime_format_string {

View File

@@ -2230,7 +2230,7 @@ template <typename Char> struct formatter<std::tm, Char> {
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_, detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
ctx); ctx);
auto loc_ref = specs.localized() ? ctx.locale() : detail::locale_ref(); auto loc_ref = specs.localized() ? ctx.locale() : locale_ref();
detail::get_locale loc(static_cast<bool>(loc_ref), loc_ref); detail::get_locale loc(static_cast<bool>(loc_ref), loc_ref);
auto w = detail::tm_writer<basic_appender<Char>, Char, Duration>( auto w = detail::tm_writer<basic_appender<Char>, Char, Duration>(
loc, out, tm, subsecs); loc, out, tm, subsecs);

View File

@@ -41,6 +41,38 @@ FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
} }
#endif #endif
#if FMT_USE_LOCALE
namespace detail {
using std::locale;
using std::numpunct;
using std::use_facet;
} // namespace detail
template <typename Locale, enable_if_t<(sizeof(Locale::collate) != 0), int>>
locale_ref::locale_ref(const Locale& loc) : locale_(&loc) {
static_assert(std::is_same<Locale, std::locale>::value, "");
}
#else
namespace detail {
struct locale {};
template <typename Char> struct numpunct {
auto grouping() const -> std::string { return "\03"; }
auto thousands_sep() const -> Char { return ','; }
auto decimal_point() const -> Char { return '.'; }
};
template <typename Facet> Facet use_facet(locale) { return {}; }
} // namespace detail
#endif // FMT_USE_LOCALE
template <typename Locale> auto locale_ref::get() const -> Locale {
using namespace detail;
static_assert(std::is_same<Locale, locale>::value, "");
#if FMT_USE_LOCALE
if (locale_) return *static_cast<const locale*>(locale_);
#endif
return locale();
}
namespace detail { namespace detail {
// DEPRECATED! // DEPRECATED!
@@ -87,33 +119,6 @@ inline void fwrite_all(const void* ptr, size_t count, FILE* stream) {
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
} }
#if FMT_USE_LOCALE
using std::locale;
using std::numpunct;
using std::use_facet;
template <typename Locale>
locale_ref::locale_ref(const Locale& loc) : locale_(&loc) {
static_assert(std::is_same<Locale, locale>::value, "");
}
#else
struct locale {};
template <typename Char> struct numpunct {
auto grouping() const -> std::string { return "\03"; }
auto thousands_sep() const -> Char { return ','; }
auto decimal_point() const -> Char { return '.'; }
};
template <typename Facet> Facet use_facet(locale) { return {}; }
#endif // FMT_USE_LOCALE
template <typename Locale> auto locale_ref::get() const -> Locale {
static_assert(std::is_same<Locale, locale>::value, "");
#if FMT_USE_LOCALE
if (locale_) return *static_cast<const locale*>(locale_);
#endif
return locale();
}
template <typename Char> template <typename Char>
FMT_FUNC auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char> { FMT_FUNC auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char> {
auto&& facet = use_facet<numpunct<Char>>(loc.get<locale>()); auto&& facet = use_facet<numpunct<Char>>(loc.get<locale>());

View File

@@ -1846,16 +1846,6 @@ FMT_CONSTEXPR auto write_char(OutputIt out, Char value,
return it; return it;
}); });
} }
template <typename Char, typename OutputIt>
FMT_CONSTEXPR auto write(OutputIt out, Char value, const format_specs& specs,
locale_ref loc = {}) -> OutputIt {
// char is formatted as unsigned char for consistency across platforms.
using unsigned_type =
conditional_t<std::is_same<Char, char>::value, unsigned char, unsigned>;
return check_char_specs(specs)
? write_char<Char>(out, value, specs)
: write<Char>(out, static_cast<unsigned_type>(value), specs, loc);
}
template <typename Char> class digit_grouping { template <typename Char> class digit_grouping {
private: private:
@@ -1879,9 +1869,7 @@ template <typename Char> class digit_grouping {
} }
public: public:
template <typename Locale, explicit digit_grouping(locale_ref loc, bool localized = true) {
FMT_ENABLE_IF(std::is_same<Locale, locale_ref>::value)>
explicit digit_grouping(Locale loc, bool localized = true) {
if (!localized) return; if (!localized) return;
auto sep = thousands_sep<Char>(loc); auto sep = thousands_sep<Char>(loc);
grouping_ = sep.grouping; grouping_ = sep.grouping;
@@ -1981,6 +1969,8 @@ auto write_int(OutputIt out, UInt value, unsigned prefix,
// Writes a localized value. // Writes a localized value.
FMT_API auto write_loc(appender out, loc_value value, const format_specs& specs, FMT_API auto write_loc(appender out, loc_value value, const format_specs& specs,
locale_ref loc) -> bool; locale_ref loc) -> bool;
auto write_loc(basic_appender<wchar_t> out, loc_value value,
const format_specs& specs, locale_ref loc) -> bool;
#endif #endif
template <typename OutputIt> template <typename OutputIt>
inline auto write_loc(OutputIt, const loc_value&, const format_specs&, inline auto write_loc(OutputIt, const loc_value&, const format_specs&,
@@ -2147,6 +2137,17 @@ FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
return write_int<Char>(out, make_write_int_arg(value, specs.sign()), specs); return write_int<Char>(out, make_write_int_arg(value, specs.sign()), specs);
} }
template <typename Char, typename OutputIt>
FMT_CONSTEXPR auto write(OutputIt out, Char value, const format_specs& specs,
locale_ref loc = {}) -> OutputIt {
// char is formatted as unsigned char for consistency across platforms.
using unsigned_type =
conditional_t<std::is_same<Char, char>::value, unsigned char, unsigned>;
return check_char_specs(specs)
? write_char<Char>(out, value, specs)
: write<Char>(out, static_cast<unsigned_type>(value), specs, loc);
}
template <typename Char, typename OutputIt, template <typename Char, typename OutputIt,
FMT_ENABLE_IF(std::is_same<Char, char>::value)> FMT_ENABLE_IF(std::is_same<Char, char>::value)>
FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s, FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
@@ -3819,12 +3820,6 @@ FMT_CONSTEXPR auto native_formatter<T, Char, TYPE>::format(
return write<Char>(ctx.out(), val, specs, ctx.locale()); return write<Char>(ctx.out(), val, specs, ctx.locale());
} }
// DEPRECATED! https://github.com/fmtlib/fmt/issues/4292.
template <typename T, typename Enable = void>
struct is_locale : std::false_type {};
template <typename T>
struct is_locale<T, void_t<decltype(T::classic())>> : std::true_type {};
// DEPRECATED! // DEPRECATED!
template <typename Char = char> struct vformat_args { template <typename Char = char> struct vformat_args {
using type = basic_format_args<buffered_context<Char>>; using type = basic_format_args<buffered_context<Char>>;
@@ -3851,7 +3846,7 @@ template <typename OutputIt, typename Char> class generic_context {
private: private:
OutputIt out_; OutputIt out_;
basic_format_args<generic_context> args_; basic_format_args<generic_context> args_;
detail::locale_ref loc_; locale_ref loc_;
public: public:
using char_type = Char; using char_type = Char;
@@ -3863,7 +3858,7 @@ template <typename OutputIt, typename Char> class generic_context {
constexpr generic_context(OutputIt out, constexpr generic_context(OutputIt out,
basic_format_args<generic_context> args, basic_format_args<generic_context> args,
detail::locale_ref loc = {}) locale_ref loc = {})
: out_(out), args_(args), loc_(loc) {} : out_(out), args_(args), loc_(loc) {}
generic_context(generic_context&&) = default; generic_context(generic_context&&) = default;
generic_context(const generic_context&) = delete; generic_context(const generic_context&) = delete;
@@ -3886,7 +3881,7 @@ template <typename OutputIt, typename Char> class generic_context {
if (!detail::is_back_insert_iterator<iterator>()) out_ = it; if (!detail::is_back_insert_iterator<iterator>()) out_ = it;
} }
constexpr auto locale() const -> detail::locale_ref { return loc_; } constexpr auto locale() const -> locale_ref { return loc_; }
}; };
class loc_value { class loc_value {
@@ -4307,46 +4302,41 @@ FMT_API void format_system_error(detail::buffer<char>& out, int error_code,
// Can be used to report errors from destructors. // Can be used to report errors from destructors.
FMT_API void report_system_error(int error_code, const char* message) noexcept; FMT_API void report_system_error(int error_code, const char* message) noexcept;
template <typename Locale, FMT_ENABLE_IF(detail::is_locale<Locale>::value)> inline auto vformat(locale_ref loc, string_view fmt, format_args args)
inline auto vformat(const Locale& loc, string_view fmt, format_args args)
-> std::string { -> std::string {
auto buf = memory_buffer(); auto buf = memory_buffer();
detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); detail::vformat_to(buf, fmt, args, loc);
return {buf.data(), buf.size()}; return {buf.data(), buf.size()};
} }
template <typename Locale, typename... T, template <typename... T>
FMT_ENABLE_IF(detail::is_locale<Locale>::value)> FMT_INLINE auto format(locale_ref loc, format_string<T...> fmt, T&&... args)
FMT_INLINE auto format(const Locale& loc, format_string<T...> fmt, T&&... args)
-> std::string { -> std::string {
return vformat(loc, fmt.str, vargs<T...>{{args...}}); return vformat(loc, fmt.str, vargs<T...>{{args...}});
} }
template <typename OutputIt, typename Locale, template <typename OutputIt,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)> FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
auto vformat_to(OutputIt out, const Locale& loc, string_view fmt, auto vformat_to(OutputIt out, locale_ref loc, string_view fmt, format_args args)
format_args args) -> OutputIt { -> OutputIt {
auto&& buf = detail::get_buffer<char>(out); auto&& buf = detail::get_buffer<char>(out);
detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); detail::vformat_to(buf, fmt, args, loc);
return detail::get_iterator(buf, out); return detail::get_iterator(buf, out);
} }
template <typename OutputIt, typename Locale, typename... T, template <typename OutputIt, typename... T,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value&& FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
detail::is_locale<Locale>::value)> FMT_INLINE auto format_to(OutputIt out, locale_ref loc, format_string<T...> fmt,
FMT_INLINE auto format_to(OutputIt out, const Locale& loc, T&&... args) -> OutputIt {
format_string<T...> fmt, T&&... args) -> OutputIt {
return fmt::vformat_to(out, loc, fmt.str, vargs<T...>{{args...}}); return fmt::vformat_to(out, loc, fmt.str, vargs<T...>{{args...}});
} }
template <typename Locale, typename... T, template <typename... T>
FMT_ENABLE_IF(detail::is_locale<Locale>::value)> FMT_NODISCARD FMT_INLINE auto formatted_size(locale_ref loc,
FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc,
format_string<T...> fmt, format_string<T...> fmt,
T&&... args) -> size_t { T&&... args) -> size_t {
auto buf = detail::counting_buffer<>(); auto buf = detail::counting_buffer<>();
detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, loc);
detail::locale_ref(loc));
return buf.count(); return buf.count();
} }

View File

@@ -46,7 +46,7 @@ template <typename Char> class basic_printf_context {
auto out() -> basic_appender<Char> { return out_; } auto out() -> basic_appender<Char> { return out_; }
void advance_to(basic_appender<Char>) {} void advance_to(basic_appender<Char>) {}
auto locale() -> detail::locale_ref { return {}; } auto locale() -> locale_ref { return {}; }
auto arg(int id) const -> basic_format_arg<basic_printf_context> { auto arg(int id) const -> basic_format_arg<basic_printf_context> {
return args_.get(id); return args_.get(id);

View File

@@ -183,24 +183,20 @@ auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
fmt::make_format_args<buffered_context<Char>>(args...)); fmt::make_format_args<buffered_context<Char>>(args...));
} }
template <typename Locale, typename S, template <typename S, typename Char = detail::format_string_char_t<S>,
typename Char = detail::format_string_char_t<S>, FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
FMT_ENABLE_IF(detail::is_locale<Locale>::value&& inline auto vformat(locale_ref loc, const S& fmt,
detail::is_exotic_char<Char>::value)>
inline auto vformat(const Locale& loc, const S& fmt,
typename detail::vformat_args<Char>::type args) typename detail::vformat_args<Char>::type args)
-> std::basic_string<Char> { -> std::basic_string<Char> {
auto buf = basic_memory_buffer<Char>(); auto buf = basic_memory_buffer<Char>();
detail::vformat_to(buf, detail::to_string_view(fmt), args, detail::vformat_to(buf, detail::to_string_view(fmt), args, loc);
detail::locale_ref(loc));
return {buf.data(), buf.size()}; return {buf.data(), buf.size()};
} }
template <typename Locale, typename S, typename... T, template <typename S, typename... T,
typename Char = detail::format_string_char_t<S>, typename Char = detail::format_string_char_t<S>,
FMT_ENABLE_IF(detail::is_locale<Locale>::value&& FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
detail::is_exotic_char<Char>::value)> inline auto format(locale_ref loc, const S& fmt, T&&... args)
inline auto format(const Locale& loc, const S& fmt, T&&... args)
-> std::basic_string<Char> { -> std::basic_string<Char> {
return vformat(loc, detail::to_string_view(fmt), return vformat(loc, detail::to_string_view(fmt),
fmt::make_format_args<buffered_context<Char>>(args...)); fmt::make_format_args<buffered_context<Char>>(args...));
@@ -227,27 +223,24 @@ inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt {
fmt::make_format_args<buffered_context<Char>>(args...)); fmt::make_format_args<buffered_context<Char>>(args...));
} }
template <typename Locale, typename S, typename OutputIt, typename... Args, template <typename S, typename OutputIt, typename... Args,
typename Char = detail::format_string_char_t<S>, typename Char = detail::format_string_char_t<S>,
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&& FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
detail::is_locale<Locale>::value&&
detail::is_exotic_char<Char>::value)> detail::is_exotic_char<Char>::value)>
inline auto vformat_to(OutputIt out, const Locale& loc, const S& fmt, inline auto vformat_to(OutputIt out, locale_ref loc, const S& fmt,
typename detail::vformat_args<Char>::type args) typename detail::vformat_args<Char>::type args)
-> OutputIt { -> OutputIt {
auto&& buf = detail::get_buffer<Char>(out); auto&& buf = detail::get_buffer<Char>(out);
vformat_to(buf, detail::to_string_view(fmt), args, detail::locale_ref(loc)); vformat_to(buf, detail::to_string_view(fmt), args, loc);
return detail::get_iterator(buf, out); return detail::get_iterator(buf, out);
} }
template <typename Locale, typename OutputIt, typename S, typename... T, template <typename OutputIt, typename S, typename... T,
typename Char = detail::format_string_char_t<S>, typename Char = detail::format_string_char_t<S>,
bool enable = detail::is_output_iterator<OutputIt, Char>::value && bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
detail::is_locale<Locale>::value &&
detail::is_exotic_char<Char>::value> detail::is_exotic_char<Char>::value>
inline auto format_to(OutputIt out, const Locale& loc, const S& fmt, inline auto format_to(OutputIt out, locale_ref loc, const S& fmt, T&&... args)
T&&... args) -> -> typename std::enable_if<enable, OutputIt>::type {
typename std::enable_if<enable, OutputIt>::type {
return vformat_to(out, loc, detail::to_string_view(fmt), return vformat_to(out, loc, detail::to_string_view(fmt),
fmt::make_format_args<buffered_context<Char>>(args...)); fmt::make_format_args<buffered_context<Char>>(args...));
} }

View File

@@ -8,6 +8,12 @@
#include "fmt/format-inl.h" #include "fmt/format-inl.h"
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
#if FMT_USE_LOCALE
template FMT_API locale_ref::locale_ref(const std::locale& loc);
template FMT_API auto locale_ref::get<std::locale>() const -> std::locale;
#endif
namespace detail { namespace detail {
template FMT_API auto dragonbox::to_decimal(float x) noexcept template FMT_API auto dragonbox::to_decimal(float x) noexcept
@@ -15,12 +21,6 @@ template FMT_API auto dragonbox::to_decimal(float x) noexcept
template FMT_API auto dragonbox::to_decimal(double x) noexcept template FMT_API auto dragonbox::to_decimal(double x) noexcept
-> dragonbox::decimal_fp<double>; -> dragonbox::decimal_fp<double>;
#if FMT_USE_LOCALE
// DEPRECATED! locale_ref in the detail namespace
template FMT_API locale_ref::locale_ref(const std::locale& loc);
template FMT_API auto locale_ref::get<std::locale>() const -> std::locale;
#endif
// Explicit instantiations for char. // Explicit instantiations for char.
template FMT_API auto thousands_sep_impl(locale_ref) template FMT_API auto thousands_sep_impl(locale_ref)

View File

@@ -379,7 +379,7 @@ TEST(locale_test, int_formatter) {
f.parse(parse_ctx); f.parse(parse_ctx);
auto buf = fmt::memory_buffer(); auto buf = fmt::memory_buffer();
fmt::basic_format_context<fmt::appender, char> format_ctx( fmt::basic_format_context<fmt::appender, char> format_ctx(
fmt::appender(buf), {}, fmt::detail::locale_ref(loc)); fmt::appender(buf), {}, fmt::locale_ref(loc));
f.format(12345, format_ctx); f.format(12345, format_ctx);
EXPECT_EQ(fmt::to_string(buf), "12,345"); EXPECT_EQ(fmt::to_string(buf), "12,345");
} }