More locale

This commit is contained in:
Victor Zverovich
2022-09-04 10:44:05 -07:00
parent 58a5563a9f
commit d59b89e9cd
3 changed files with 56 additions and 31 deletions

View File

@@ -116,45 +116,60 @@ template <typename Char> FMT_FUNC Char decimal_point_impl(locale_ref) {
}
#endif
FMT_FUNC auto write_int(appender out, basic_format_arg<format_context> value,
FMT_FUNC auto write_loc(appender out, basic_format_arg<format_context> value,
const format_specs& specs, locale_ref loc) -> bool {
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
auto locale = loc.get<std::locale>();
// We cannot use the num_put<char> facet because it may produce output in
// a wrong encoding.
if (!std::has_facet<format_facet<std::locale>>(locale)) return {};
std::use_facet<format_facet<std::locale>>(locale).put(out, value, specs);
return true;
using facet = format_facet<std::locale>;
if (std::has_facet<facet>(locale))
return std::use_facet<facet>(locale).put(out, value, specs);
return facet(locale).put(out, value, specs);
#endif
return false;
}
struct localize_int {
struct localizer {
appender out;
const format_specs& specs;
std::string sep;
std::string grouping;
std::string decimal_point;
template <typename T, FMT_ENABLE_IF(detail::is_integer<T>::value)>
void operator()(T value) {
auto operator()(T value) -> bool {
auto arg = make_write_int_arg(value, specs.sign);
write_int(out, static_cast<uint64_or_128_t<T>>(arg.abs_value), arg.prefix,
specs, digit_grouping<char>(grouping, sep));
return true;
}
template <typename T, FMT_ENABLE_IF(!detail::is_integer<T>::value)>
void operator()(T) {}
template <typename T, FMT_ENABLE_IF(detail::is_floating_point<T>::value)>
auto operator()(T) -> bool {
return false;
}
auto operator()(...) -> bool { return false; }
};
} // namespace detail
template <typename Locale> typename Locale::id format_facet<Locale>::id;
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
template <typename Locale> format_facet<Locale>::format_facet(Locale& loc) {
auto& numpunct = std::use_facet<std::numpunct<char>>(loc);
grouping_ = numpunct.grouping();
if (!grouping_.empty()) separator_ = std::string(1, numpunct.thousands_sep());
}
template <>
FMT_API FMT_FUNC void format_facet<std::locale>::do_put(
FMT_API FMT_FUNC auto format_facet<std::locale>::do_put(
appender out, basic_format_arg<format_context> val,
const format_specs& specs) const {
visit_format_arg(detail::localize_int{out, specs, separator_, grouping_},
val);
const format_specs& specs) const -> bool {
return visit_format_arg(
detail::localizer{out, specs, separator_, grouping_, decimal_point_},
val);
}
#endif