From 496cf9a1d85b170ddda68789199dd724907119d4 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 23 Feb 2024 08:30:00 +0800 Subject: [PATCH] interval: add fmt::formatters for managed_bytes and friends before this change, we rely on the default-generated fmt::formatter created from operator<<, but fmt v10 dropped the default-generated formatter. in this change, we define formatters for * wrapping_interval * interval Refs #13245 Signed-off-by: Kefu Chai Closes scylladb/scylladb#17488 --- interval.hh | 73 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/interval.hh b/interval.hh index 89b566f9b2..dfed7841c5 100644 --- a/interval.hh +++ b/interval.hh @@ -426,41 +426,47 @@ public: return (_start == other._start) && (_end == other._end) && (_singular == other._singular); } - template - friend std::ostream& operator<<(std::ostream& out, const wrapping_interval& r); private: friend class interval; }; template -std::ostream& operator<<(std::ostream& out, const wrapping_interval& r) { - if (r.is_singular()) { - fmt::print(out, "{{{}}}", r.start()->value()); +struct fmt::formatter> : fmt::formatter { + auto format(const wrapping_interval& r, fmt::format_context& ctx) const { + auto out = ctx.out(); + if (r.is_singular()) { + return fmt::format_to(out, "{{{}}}", r.start()->value()); + } + + if (!r.start()) { + out = fmt::format_to(out, "(-inf, "); + } else { + if (r.start()->is_inclusive()) { + out = fmt::format_to(out, "["); + } else { + out = fmt::format_to(out, "("); + } + out = fmt::format_to(out, "{},", r.start()->value()); + } + + if (!r.end()) { + out = fmt::format_to(out, "+inf)"); + } else { + out = fmt::format_to(out, "{}", r.end()->value()); + if (r.end()->is_inclusive()) { + out = fmt::format_to(out, "]"); + } else { + out = fmt::format_to(out, ")"); + } + } + return out; } +}; - if (!r.start()) { - out << "(-inf, "; - } else { - if (r.start()->is_inclusive()) { - out << "["; - } else { - out << "("; - } - fmt::print(out, "{},", r.start()->value()); - } - - if (!r.end()) { - out << "+inf)"; - } else { - fmt::print(out, "{}", r.end()->value()); - if (r.end()->is_inclusive()) { - out << "]"; - } else { - out << ")"; - } - } - +template +std::ostream& operator<<(std::ostream& out, const wrapping_interval& r) { + fmt::print(out, "{}", r); return out; } @@ -727,13 +733,20 @@ public: return {}; } - template - friend std::ostream& operator<<(std::ostream& out, const interval& r); + friend class fmt::formatter>; +}; + +template +struct fmt::formatter> : fmt::formatter { + auto format(const interval& r, fmt::format_context& ctx) const { + return fmt::format_to(ctx.out(), "{}", r._interval); + } }; template std::ostream& operator<<(std::ostream& out, const interval& r) { - return out << r._interval; + fmt::print(out, "{}", r); + return out; } template typename T, typename U>