mirror of
https://github.com/fmtlib/fmt.git
synced 2025-11-27 21:00:01 +01:00
Improve locale API
This commit is contained in:
@@ -1001,6 +1001,22 @@ constexpr auto compile_string_to_view(detail::std_string_view<Char> s)
|
||||
}
|
||||
} // namespace detail_exported
|
||||
|
||||
class loc_value {
|
||||
private:
|
||||
basic_format_arg<format_context> value_;
|
||||
|
||||
public:
|
||||
template <typename T, FMT_ENABLE_IF(!detail::is_float128<T>::value)>
|
||||
loc_value(T value) : value_(detail::make_arg<format_context>(value)) {}
|
||||
|
||||
template <typename T, FMT_ENABLE_IF(detail::is_float128<T>::value)>
|
||||
loc_value(T) {}
|
||||
|
||||
template <typename Visitor> auto visit(Visitor&& vis) -> decltype(vis(0)) {
|
||||
return visit_format_arg(vis, value_);
|
||||
}
|
||||
};
|
||||
|
||||
// A locale facet that formats values in UTF-8.
|
||||
// It is parameterized on the locale to avoid the heavy <locale> include.
|
||||
template <typename Locale> class format_facet : public Locale::facet {
|
||||
@@ -1010,7 +1026,7 @@ template <typename Locale> class format_facet : public Locale::facet {
|
||||
std::string decimal_point_;
|
||||
|
||||
protected:
|
||||
virtual auto do_put(appender out, basic_format_arg<format_context> val,
|
||||
virtual auto do_put(appender out, loc_value val,
|
||||
const format_specs& specs) const -> bool;
|
||||
|
||||
public:
|
||||
@@ -1024,8 +1040,8 @@ template <typename Locale> class format_facet : public Locale::facet {
|
||||
grouping_(g.begin(), g.end()),
|
||||
decimal_point_(decimal_point) {}
|
||||
|
||||
auto put(appender out, basic_format_arg<format_context> val,
|
||||
const format_specs& specs) const -> bool {
|
||||
auto put(appender out, loc_value val, const format_specs& specs) const
|
||||
-> bool {
|
||||
return do_put(out, val, specs);
|
||||
}
|
||||
};
|
||||
@@ -2053,22 +2069,12 @@ auto write_int(OutputIt out, UInt value, unsigned prefix,
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T, FMT_ENABLE_IF(!is_float128<T>::value)>
|
||||
auto make_loc_value(T value) -> basic_format_arg<format_context> {
|
||||
return make_arg<format_context>(value);
|
||||
}
|
||||
template <typename T, FMT_ENABLE_IF(is_float128<T>::value)>
|
||||
auto make_loc_value(T) -> basic_format_arg<format_context> {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Writes a localized value.
|
||||
FMT_API auto write_loc(appender out, basic_format_arg<format_context> value,
|
||||
const format_specs& specs, locale_ref loc) -> bool;
|
||||
|
||||
FMT_API auto write_loc(appender out, loc_value value, const format_specs& specs,
|
||||
locale_ref loc) -> bool;
|
||||
template <typename OutputIt, typename Char>
|
||||
inline auto write_loc(OutputIt, basic_format_arg<format_context>,
|
||||
const basic_format_specs<Char>&, locale_ref) -> bool {
|
||||
inline auto write_loc(OutputIt, loc_value, const basic_format_specs<Char>&,
|
||||
locale_ref) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2190,8 +2196,7 @@ template <typename Char, typename OutputIt, typename T,
|
||||
FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
|
||||
const basic_format_specs<Char>& specs,
|
||||
locale_ref loc) -> OutputIt {
|
||||
if (specs.localized && write_loc(out, make_loc_value(value), specs, loc))
|
||||
return out;
|
||||
if (specs.localized && write_loc(out, value, specs, loc)) return out;
|
||||
return write_int_noinline(out, make_write_int_arg(value, specs.sign), specs,
|
||||
loc);
|
||||
}
|
||||
@@ -2203,8 +2208,7 @@ template <typename Char, typename OutputIt, typename T,
|
||||
FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
|
||||
const basic_format_specs<Char>& specs,
|
||||
locale_ref loc) -> OutputIt {
|
||||
if (specs.localized && write_loc(out, make_loc_value(value), specs, loc))
|
||||
return out;
|
||||
if (specs.localized && write_loc(out, value, specs, loc)) return out;
|
||||
return write_int(out, make_write_int_arg(value, specs.sign), specs, loc);
|
||||
}
|
||||
|
||||
@@ -3311,7 +3315,7 @@ FMT_CONSTEXPR20 auto write(OutputIt out, T value,
|
||||
basic_format_specs<Char> specs, locale_ref loc = {})
|
||||
-> OutputIt {
|
||||
if (const_check(!is_supported_floating_point(value))) return out;
|
||||
return specs.localized && write_loc(out, make_loc_value(value), specs, loc)
|
||||
return specs.localized && write_loc(out, value, specs, loc)
|
||||
? out
|
||||
: write_float(out, value, specs, loc);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user