Skip to content

Commit

Permalink
Improve locale API
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Sep 11, 2022
1 parent 58c4c01 commit 94ceb38
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
10 changes: 4 additions & 6 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ template <typename Char> FMT_FUNC Char decimal_point_impl(locale_ref) {
}
#endif

FMT_FUNC auto write_loc(appender out, basic_format_arg<format_context> value,
FMT_FUNC auto write_loc(appender out, loc_value value,
const format_specs& specs, locale_ref loc) -> bool {
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
auto locale = loc.get<std::locale>();
Expand All @@ -142,11 +142,9 @@ template <typename Locale> format_facet<Locale>::format_facet(Locale& loc) {

template <>
FMT_API FMT_FUNC auto format_facet<std::locale>::do_put(
appender out, basic_format_arg<format_context> val,
const format_specs& specs) const -> bool {
return visit_format_arg(
detail::loc_writer<>{out, specs, separator_, grouping_, decimal_point_},
val);
appender out, loc_value val, const format_specs& specs) const -> bool {
return val.visit(
detail::loc_writer<>{out, specs, separator_, grouping_, decimal_point_});
}
#endif

Expand Down
48 changes: 26 additions & 22 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand All @@ -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);
}
};
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions include/fmt/xchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ template <typename T>
using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;

template <typename OutputIt>
auto write_loc(OutputIt out, basic_format_arg<format_context> val,
auto write_loc(OutputIt out, loc_value value,
const basic_format_specs<wchar_t>& specs, locale_ref loc)
-> bool {
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR
Expand All @@ -32,8 +32,7 @@ auto write_loc(OutputIt out, basic_format_arg<format_context> val,
auto separator = std::wstring();
auto grouping = numpunct.grouping();
if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep());
return visit_format_arg(
loc_writer<wchar_t>{out, specs, separator, grouping, {}}, val);
return value.visit(loc_writer<wchar_t>{out, specs, separator, grouping, {}});
#endif
return false;
}
Expand Down
7 changes: 3 additions & 4 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2335,14 +2335,13 @@ class format_facet : public fmt::format_facet<std::locale> {
}
};

auto do_put(fmt::appender out, fmt::basic_format_arg<fmt::format_context> arg,
auto do_put(fmt::appender out, fmt::loc_value val,
const fmt::format_specs&) const -> bool override;
};

auto format_facet::do_put(fmt::appender out,
fmt::basic_format_arg<fmt::format_context> arg,
auto format_facet::do_put(fmt::appender out, fmt::loc_value val,
const fmt::format_specs&) const -> bool {
return visit_format_arg(int_formatter{out}, arg);
return val.visit(int_formatter{out});
}

TEST(format_test, format_facet) {
Expand Down

0 comments on commit 94ceb38

Please sign in to comment.