mirror of
https://github.com/shadps4-emu/ext-fmt.git
synced 2025-03-03 15:48:47 +00:00
More locale
This commit is contained in:
parent
58a5563a9f
commit
d59b89e9cd
@ -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
|
||||
|
||||
|
@ -992,21 +992,26 @@ template <typename Locale> class format_facet : public Locale::facet {
|
||||
private:
|
||||
std::string separator_;
|
||||
std::string grouping_;
|
||||
std::string decimal_point_;
|
||||
|
||||
protected:
|
||||
virtual void do_put(appender out, basic_format_arg<format_context> val,
|
||||
const format_specs& specs) const;
|
||||
virtual auto do_put(appender out, basic_format_arg<format_context> val,
|
||||
const format_specs& specs) const -> bool;
|
||||
|
||||
public:
|
||||
static FMT_API typename Locale::id id;
|
||||
|
||||
explicit format_facet(string_view sep = ",",
|
||||
std::initializer_list<unsigned char> g = {3})
|
||||
: separator_(sep.data(), sep.size()), grouping_(g.begin(), g.end()) {}
|
||||
explicit format_facet(Locale& loc);
|
||||
explicit format_facet(string_view sep = "",
|
||||
std::initializer_list<unsigned char> g = {3},
|
||||
std::string decimal_point = ".")
|
||||
: separator_(sep.data(), sep.size()),
|
||||
grouping_(g.begin(), g.end()),
|
||||
decimal_point_(decimal_point) {}
|
||||
|
||||
void put(appender out, basic_format_arg<format_context> val,
|
||||
const format_specs& specs) const {
|
||||
do_put(out, val, specs);
|
||||
auto put(appender out, basic_format_arg<format_context> val,
|
||||
const format_specs& specs) const -> bool {
|
||||
return do_put(out, val, specs);
|
||||
}
|
||||
};
|
||||
|
||||
@ -2042,10 +2047,11 @@ auto write_int(OutputIt out, UInt value, unsigned prefix,
|
||||
});
|
||||
}
|
||||
|
||||
FMT_API auto write_int(appender out, basic_format_arg<format_context> value,
|
||||
// Writes value with localization.
|
||||
FMT_API auto write_loc(appender out, basic_format_arg<format_context> value,
|
||||
const format_specs& specs, locale_ref loc) -> bool;
|
||||
template <typename OutputIt, typename Char>
|
||||
inline auto write_int(OutputIt, basic_format_arg<buffer_context<Char>>,
|
||||
inline auto write_loc(OutputIt, basic_format_arg<buffer_context<Char>>,
|
||||
const basic_format_specs<Char>&, locale_ref) -> bool {
|
||||
return false;
|
||||
}
|
||||
@ -4165,8 +4171,8 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
|
||||
begin = parse_format_specs(begin, end, handler);
|
||||
if (begin == end || *begin != '}')
|
||||
on_error("missing '}' in format string");
|
||||
if (specs.localized && arg.is_integral() &&
|
||||
write_int(context.out(), arg, specs, context.locale())) {
|
||||
if (specs.localized &&
|
||||
write_loc(context.out(), arg, specs, context.locale())) {
|
||||
return begin;
|
||||
}
|
||||
auto f = arg_formatter<Char>{context.out(), specs, context.locale()};
|
||||
|
@ -2322,21 +2322,25 @@ class format_facet : public fmt::format_facet<std::locale> {
|
||||
fmt::appender out;
|
||||
|
||||
template <typename T, FMT_ENABLE_IF(fmt::detail::is_integer<T>::value)>
|
||||
void operator()(T value) {
|
||||
auto operator()(T value) -> bool {
|
||||
fmt::format_to(out, "[{}]", value);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T, FMT_ENABLE_IF(!fmt::detail::is_integer<T>::value)>
|
||||
void operator()(T) {}
|
||||
auto operator()(T) -> bool {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
void do_put(fmt::appender out, fmt::basic_format_arg<fmt::format_context> arg,
|
||||
const fmt::format_specs&) const override;
|
||||
auto do_put(fmt::appender out, fmt::basic_format_arg<fmt::format_context> arg,
|
||||
const fmt::format_specs&) const -> bool override;
|
||||
};
|
||||
|
||||
void format_facet::do_put(fmt::appender out,
|
||||
auto format_facet::do_put(fmt::appender out,
|
||||
fmt::basic_format_arg<fmt::format_context> arg,
|
||||
const fmt::format_specs&) const {
|
||||
visit_format_arg(int_formatter{out}, arg);
|
||||
const fmt::format_specs&) const -> bool {
|
||||
return visit_format_arg(int_formatter{out}, arg);
|
||||
}
|
||||
|
||||
TEST(format_test, format_facet) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user