From da389679ecbd0deff5f8af39a8a119a1b4e895d5 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Sun, 30 Mar 2025 10:23:49 -0600 Subject: [PATCH] :sparkles: Add `num_fmt_specifiers` Problem: - There is no way for a caller to easily know how many format specifiers are in a string. This is already known to `ct_format` internally, and is useful information to callers. Solution: - Add `num_fmt_specifiers`. Note: - This is a building block for `CIB_FATAL`, allowing extra arguments to be passed along to `stdx::panic`. --- include/stdx/ct_format.hpp | 4 ++++ test/ct_format.cpp | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/include/stdx/ct_format.hpp b/include/stdx/ct_format.hpp index a717e02..c3133b4 100644 --- a/include/stdx/ct_format.hpp +++ b/include/stdx/ct_format.hpp @@ -229,6 +229,10 @@ constexpr auto ct_format = [](auto &&...args) { return format_result{detail::convert_output(), result.args}; }; + +template +constexpr auto num_fmt_specifiers = + detail::count_specifiers(std::string_view{Fmt}); } // namespace v1 } // namespace stdx diff --git a/test/ct_format.cpp b/test/ct_format.cpp index 9e875fc..662052d 100644 --- a/test/ct_format.cpp +++ b/test/ct_format.cpp @@ -218,3 +218,8 @@ TEST_CASE("format multiple mixed arguments with different type", string_constant>(), stdx::make_tuple(42, "B"sv)}); } + +TEST_CASE("num fmt specifiers", "[ct_format]") { + static_assert(stdx::num_fmt_specifiers<"{}"> == 1u); + static_assert(stdx::num_fmt_specifiers<"{} {}"> == 2u); +}