From 6dd9194abd5c3505229185733ad25e112ff0c14e Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 29 Aug 2024 18:35:42 -0700 Subject: [PATCH] Simplify format_to_result --- include/fmt/base.h | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 51286967..3e302f92 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -2969,11 +2969,6 @@ void check_format_string(S format_str) { ignore_unused(error); } -// Report truncation to prevent silent data loss. -inline void report_truncation(bool truncated) { - if (truncated) report_error("output is truncated"); -} - // Use vformat_args and avoid type_identity to keep symbols short. template struct vformat_args { using type = basic_format_args>; @@ -3145,37 +3140,29 @@ FMT_INLINE auto format_to_n(OutputIt out, size_t n, format_string fmt, return vformat_to_n(out, n, fmt, fmt::make_format_args(args...)); } -template struct format_to_result { - /// Iterator pointing to just after the last successful write in the range. - OutputIt out; + /// Pointer to just after the last successful write in the array. + char* out; /// Specifies if the output was truncated. bool truncated; - FMT_CONSTEXPR operator OutputIt&() & { - detail::report_truncation(truncated); + FMT_CONSTEXPR operator char*() const { + // Report truncation to prevent silent data loss. + if (truncated) report_error("output is truncated"); return out; } - FMT_CONSTEXPR operator const OutputIt&() const& { - detail::report_truncation(truncated); - return out; - } - FMT_CONSTEXPR operator OutputIt&&() && { - detail::report_truncation(truncated); - return static_cast(out); - } }; template auto vformat_to(char (&out)[N], string_view fmt, format_args args) - -> format_to_result { + -> format_to_result { auto result = vformat_to_n(out, N, fmt, args); return {result.out, result.size > N}; } template FMT_INLINE auto format_to(char (&out)[N], format_string fmt, T&&... args) - -> format_to_result { + -> format_to_result { auto result = fmt::format_to_n(out, N, fmt, static_cast(args)...); return {result.out, result.size > N}; }