mirror of
https://github.com/fmtlib/fmt.git
synced 2025-09-26 14:40:55 +02:00
Simplify locale handling
This commit is contained in:
@@ -415,11 +415,11 @@ locale:
|
||||
that take `std::locale` as a parameter. The locale type is a template
|
||||
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>
|
||||
### Legacy Compile-Time Checks
|
||||
|
@@ -902,6 +902,29 @@ template <typename Char = char> class parse_context {
|
||||
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
|
||||
|
||||
namespace detail {
|
||||
@@ -2301,27 +2324,6 @@ struct is_output_iterator<
|
||||
enable_if_t<std::is_assignable<decltype(*std::declval<decay_t<It>&>()++),
|
||||
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 {
|
||||
return 0;
|
||||
}
|
||||
@@ -2665,7 +2667,7 @@ class context {
|
||||
private:
|
||||
appender out_;
|
||||
format_args args_;
|
||||
FMT_NO_UNIQUE_ADDRESS detail::locale_ref loc_;
|
||||
FMT_NO_UNIQUE_ADDRESS locale_ref loc_;
|
||||
|
||||
public:
|
||||
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
|
||||
/// in the object so make sure they have appropriate lifetimes.
|
||||
FMT_CONSTEXPR context(iterator out, format_args args,
|
||||
detail::locale_ref loc = {})
|
||||
FMT_CONSTEXPR context(iterator out, format_args args, locale_ref loc = {})
|
||||
: out_(out), args_(args), loc_(loc) {}
|
||||
context(context&&) = default;
|
||||
context(const context&) = delete;
|
||||
@@ -2697,7 +2698,7 @@ class context {
|
||||
// Advances the begin iterator to `it`.
|
||||
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 {
|
||||
|
@@ -2230,7 +2230,7 @@ template <typename Char> struct formatter<std::tm, Char> {
|
||||
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
|
||||
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);
|
||||
auto w = detail::tm_writer<basic_appender<Char>, Char, Duration>(
|
||||
loc, out, tm, subsecs);
|
||||
|
@@ -41,6 +41,38 @@ FMT_FUNC void assert_fail(const char* file, int line, const char* message) {
|
||||
}
|
||||
#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 {
|
||||
|
||||
// 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")));
|
||||
}
|
||||
|
||||
#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>
|
||||
FMT_FUNC auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result<Char> {
|
||||
auto&& facet = use_facet<numpunct<Char>>(loc.get<locale>());
|
||||
|
@@ -1846,16 +1846,6 @@ FMT_CONSTEXPR auto write_char(OutputIt out, Char value,
|
||||
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 {
|
||||
private:
|
||||
@@ -1879,9 +1869,7 @@ template <typename Char> class digit_grouping {
|
||||
}
|
||||
|
||||
public:
|
||||
template <typename Locale,
|
||||
FMT_ENABLE_IF(std::is_same<Locale, locale_ref>::value)>
|
||||
explicit digit_grouping(Locale loc, bool localized = true) {
|
||||
explicit digit_grouping(locale_ref loc, bool localized = true) {
|
||||
if (!localized) return;
|
||||
auto sep = thousands_sep<Char>(loc);
|
||||
grouping_ = sep.grouping;
|
||||
@@ -1981,6 +1969,8 @@ auto write_int(OutputIt out, UInt value, unsigned prefix,
|
||||
// Writes a localized value.
|
||||
FMT_API auto write_loc(appender out, loc_value value, const format_specs& specs,
|
||||
locale_ref loc) -> bool;
|
||||
auto write_loc(basic_appender<wchar_t> out, loc_value value,
|
||||
const format_specs& specs, locale_ref loc) -> bool;
|
||||
#endif
|
||||
template <typename OutputIt>
|
||||
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);
|
||||
}
|
||||
|
||||
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,
|
||||
FMT_ENABLE_IF(std::is_same<Char, char>::value)>
|
||||
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());
|
||||
}
|
||||
|
||||
// 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!
|
||||
template <typename Char = char> struct vformat_args {
|
||||
using type = basic_format_args<buffered_context<Char>>;
|
||||
@@ -3851,7 +3846,7 @@ template <typename OutputIt, typename Char> class generic_context {
|
||||
private:
|
||||
OutputIt out_;
|
||||
basic_format_args<generic_context> args_;
|
||||
detail::locale_ref loc_;
|
||||
locale_ref loc_;
|
||||
|
||||
public:
|
||||
using char_type = Char;
|
||||
@@ -3863,7 +3858,7 @@ template <typename OutputIt, typename Char> class generic_context {
|
||||
|
||||
constexpr generic_context(OutputIt out,
|
||||
basic_format_args<generic_context> args,
|
||||
detail::locale_ref loc = {})
|
||||
locale_ref loc = {})
|
||||
: out_(out), args_(args), loc_(loc) {}
|
||||
generic_context(generic_context&&) = default;
|
||||
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;
|
||||
}
|
||||
|
||||
constexpr auto locale() const -> detail::locale_ref { return loc_; }
|
||||
constexpr auto locale() const -> locale_ref { return loc_; }
|
||||
};
|
||||
|
||||
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.
|
||||
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(const Locale& loc, string_view fmt, format_args args)
|
||||
inline auto vformat(locale_ref loc, string_view fmt, format_args args)
|
||||
-> std::string {
|
||||
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()};
|
||||
}
|
||||
|
||||
template <typename Locale, typename... T,
|
||||
FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
|
||||
FMT_INLINE auto format(const Locale& loc, format_string<T...> fmt, T&&... args)
|
||||
template <typename... T>
|
||||
FMT_INLINE auto format(locale_ref loc, format_string<T...> fmt, T&&... args)
|
||||
-> std::string {
|
||||
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)>
|
||||
auto vformat_to(OutputIt out, const Locale& loc, string_view fmt,
|
||||
format_args args) -> OutputIt {
|
||||
auto vformat_to(OutputIt out, locale_ref loc, string_view fmt, format_args args)
|
||||
-> OutputIt {
|
||||
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);
|
||||
}
|
||||
|
||||
template <typename OutputIt, typename Locale, typename... T,
|
||||
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value&&
|
||||
detail::is_locale<Locale>::value)>
|
||||
FMT_INLINE auto format_to(OutputIt out, const Locale& loc,
|
||||
format_string<T...> fmt, T&&... args) -> OutputIt {
|
||||
template <typename OutputIt, typename... T,
|
||||
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
|
||||
FMT_INLINE auto format_to(OutputIt out, locale_ref loc, format_string<T...> fmt,
|
||||
T&&... args) -> OutputIt {
|
||||
return fmt::vformat_to(out, loc, fmt.str, vargs<T...>{{args...}});
|
||||
}
|
||||
|
||||
template <typename Locale, typename... T,
|
||||
FMT_ENABLE_IF(detail::is_locale<Locale>::value)>
|
||||
FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc,
|
||||
template <typename... T>
|
||||
FMT_NODISCARD FMT_INLINE auto formatted_size(locale_ref loc,
|
||||
format_string<T...> fmt,
|
||||
T&&... args) -> size_t {
|
||||
auto buf = detail::counting_buffer<>();
|
||||
detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}},
|
||||
detail::locale_ref(loc));
|
||||
detail::vformat_to(buf, fmt.str, vargs<T...>{{args...}}, loc);
|
||||
return buf.count();
|
||||
}
|
||||
|
||||
|
@@ -46,7 +46,7 @@ template <typename Char> class basic_printf_context {
|
||||
auto out() -> basic_appender<Char> { return out_; }
|
||||
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> {
|
||||
return args_.get(id);
|
||||
|
@@ -183,24 +183,20 @@ auto format(const S& fmt, T&&... args) -> std::basic_string<Char> {
|
||||
fmt::make_format_args<buffered_context<Char>>(args...));
|
||||
}
|
||||
|
||||
template <typename Locale, typename S,
|
||||
typename Char = detail::format_string_char_t<S>,
|
||||
FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
|
||||
detail::is_exotic_char<Char>::value)>
|
||||
inline auto vformat(const Locale& loc, const S& fmt,
|
||||
template <typename S, typename Char = detail::format_string_char_t<S>,
|
||||
FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
|
||||
inline auto vformat(locale_ref loc, const S& fmt,
|
||||
typename detail::vformat_args<Char>::type args)
|
||||
-> std::basic_string<Char> {
|
||||
auto buf = basic_memory_buffer<Char>();
|
||||
detail::vformat_to(buf, detail::to_string_view(fmt), args,
|
||||
detail::locale_ref(loc));
|
||||
detail::vformat_to(buf, detail::to_string_view(fmt), args, loc);
|
||||
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>,
|
||||
FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
|
||||
detail::is_exotic_char<Char>::value)>
|
||||
inline auto format(const Locale& loc, const S& fmt, T&&... args)
|
||||
FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
|
||||
inline auto format(locale_ref loc, const S& fmt, T&&... args)
|
||||
-> std::basic_string<Char> {
|
||||
return vformat(loc, detail::to_string_view(fmt),
|
||||
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...));
|
||||
}
|
||||
|
||||
template <typename Locale, typename S, typename OutputIt, typename... Args,
|
||||
template <typename S, typename OutputIt, typename... Args,
|
||||
typename Char = detail::format_string_char_t<S>,
|
||||
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
|
||||
detail::is_locale<Locale>::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)
|
||||
-> OutputIt {
|
||||
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);
|
||||
}
|
||||
|
||||
template <typename Locale, typename OutputIt, typename S, typename... T,
|
||||
template <typename OutputIt, typename S, typename... T,
|
||||
typename Char = detail::format_string_char_t<S>,
|
||||
bool enable = detail::is_output_iterator<OutputIt, Char>::value &&
|
||||
detail::is_locale<Locale>::value &&
|
||||
detail::is_exotic_char<Char>::value>
|
||||
inline auto format_to(OutputIt out, const Locale& loc, const S& fmt,
|
||||
T&&... args) ->
|
||||
typename std::enable_if<enable, OutputIt>::type {
|
||||
inline auto format_to(OutputIt out, locale_ref loc, const S& fmt, T&&... args)
|
||||
-> typename std::enable_if<enable, OutputIt>::type {
|
||||
return vformat_to(out, loc, detail::to_string_view(fmt),
|
||||
fmt::make_format_args<buffered_context<Char>>(args...));
|
||||
}
|
||||
|
@@ -8,6 +8,12 @@
|
||||
#include "fmt/format-inl.h"
|
||||
|
||||
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 {
|
||||
|
||||
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
|
||||
-> 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.
|
||||
|
||||
template FMT_API auto thousands_sep_impl(locale_ref)
|
||||
|
@@ -379,7 +379,7 @@ TEST(locale_test, int_formatter) {
|
||||
f.parse(parse_ctx);
|
||||
auto buf = fmt::memory_buffer();
|
||||
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);
|
||||
EXPECT_EQ(fmt::to_string(buf), "12,345");
|
||||
}
|
||||
|
Reference in New Issue
Block a user