Skip to content

Commit 5d63e87

Browse files
committed
Add a formatter for float128
1 parent aecec01 commit 5d63e87

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

Diff for: include/fmt/base.h

+18-17
Original file line numberDiff line numberDiff line change
@@ -2799,35 +2799,25 @@ FMT_API void vprint_mojibake(FILE*, string_view, format_args, bool = false);
27992799
#ifndef _WIN32
28002800
inline void vprint_mojibake(FILE*, string_view, format_args, bool) {}
28012801
#endif
2802-
} // namespace detail
2803-
2804-
FMT_BEGIN_EXPORT
28052802

2806-
// A formatter specialization for natively supported types.
2807-
template <typename T, typename Char>
2808-
struct formatter<T, Char,
2809-
enable_if_t<detail::type_constant<T, Char>::value !=
2810-
detail::type::custom_type>> {
2803+
template <typename T, typename Char, type TYPE> struct native_formatter {
28112804
private:
2812-
detail::dynamic_format_specs<Char> specs_;
2805+
dynamic_format_specs<Char> specs_;
28132806

28142807
public:
28152808
using nonlocking = void;
28162809

28172810
template <typename ParseContext>
28182811
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* {
28192812
if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin();
2820-
auto type = detail::type_constant<T, Char>::value;
2821-
auto end =
2822-
detail::parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, type);
2823-
if (type == detail::type::char_type) detail::check_char_specs(specs_);
2813+
auto end = parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, TYPE);
2814+
if (TYPE == type::char_type) check_char_specs(specs_);
28242815
return end;
28252816
}
28262817

2827-
template <detail::type U = detail::type_constant<T, Char>::value,
2828-
FMT_ENABLE_IF(U == detail::type::string_type ||
2829-
U == detail::type::cstring_type ||
2830-
U == detail::type::char_type)>
2818+
template <type U = TYPE,
2819+
FMT_ENABLE_IF(U == type::string_type || U == type::cstring_type ||
2820+
U == type::char_type)>
28312821
FMT_CONSTEXPR void set_debug_format(bool set = true) {
28322822
specs_.type = set ? presentation_type::debug : presentation_type::none;
28332823
}
@@ -2836,6 +2826,17 @@ struct formatter<T, Char,
28362826
FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const
28372827
-> decltype(ctx.out());
28382828
};
2829+
} // namespace detail
2830+
2831+
FMT_BEGIN_EXPORT
2832+
2833+
// A formatter specialization for natively supported types.
2834+
template <typename T, typename Char>
2835+
struct formatter<T, Char,
2836+
enable_if_t<detail::type_constant<T, Char>::value !=
2837+
detail::type::custom_type>>
2838+
: detail::native_formatter<T, Char, detail::type_constant<T, Char>::value> {
2839+
};
28392840

28402841
template <typename Char = char> struct runtime_format_string {
28412842
basic_string_view<Char> str;

Diff for: include/fmt/format.h

+19-20
Original file line numberDiff line numberDiff line change
@@ -4321,8 +4321,27 @@ extern template FMT_API auto decimal_point_impl(locale_ref) -> char;
43214321
extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t;
43224322
#endif // FMT_HEADER_ONLY
43234323

4324+
template <typename T, typename Char, type TYPE>
4325+
template <typename FormatContext>
4326+
FMT_CONSTEXPR FMT_INLINE auto native_formatter<T, Char, TYPE>::format(
4327+
const T& val, FormatContext& ctx) const -> decltype(ctx.out()) {
4328+
if (specs_.width_ref.kind == arg_id_kind::none &&
4329+
specs_.precision_ref.kind == arg_id_kind::none) {
4330+
return write<Char>(ctx.out(), val, specs_, ctx.locale());
4331+
}
4332+
auto specs = specs_;
4333+
handle_dynamic_spec<width_checker>(specs.width, specs.width_ref, ctx);
4334+
handle_dynamic_spec<precision_checker>(specs.precision, specs.precision_ref,
4335+
ctx);
4336+
return write<Char>(ctx.out(), val, specs, ctx.locale());
4337+
}
43244338
} // namespace detail
43254339

4340+
template <typename Char>
4341+
struct formatter<detail::float128, Char>
4342+
: detail::native_formatter<detail::float128, Char,
4343+
detail::type::float_type> {};
4344+
43264345
#if FMT_USE_USER_DEFINED_LITERALS
43274346
inline namespace literals {
43284347
/**
@@ -4412,26 +4431,6 @@ FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc,
44124431

44134432
FMT_END_EXPORT
44144433

4415-
template <typename T, typename Char>
4416-
template <typename FormatContext>
4417-
FMT_CONSTEXPR FMT_INLINE auto
4418-
formatter<T, Char,
4419-
enable_if_t<detail::type_constant<T, Char>::value !=
4420-
detail::type::custom_type>>::format(const T& val,
4421-
FormatContext& ctx)
4422-
const -> decltype(ctx.out()) {
4423-
if (specs_.width_ref.kind == detail::arg_id_kind::none &&
4424-
specs_.precision_ref.kind == detail::arg_id_kind::none) {
4425-
return detail::write<Char>(ctx.out(), val, specs_, ctx.locale());
4426-
}
4427-
auto specs = specs_;
4428-
detail::handle_dynamic_spec<detail::width_checker>(specs.width,
4429-
specs.width_ref, ctx);
4430-
detail::handle_dynamic_spec<detail::precision_checker>(
4431-
specs.precision, specs.precision_ref, ctx);
4432-
return detail::write<Char>(ctx.out(), val, specs, ctx.locale());
4433-
}
4434-
44354434
FMT_END_NAMESPACE
44364435

44374436
#ifdef FMT_HEADER_ONLY

0 commit comments

Comments
 (0)