From d466b810cf504a6fce9b6412ce5d52a0cce080ae Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Thu, 22 Oct 2026 14:27:51 -0600 Subject: [PATCH] :art: Recognize shrunk types and values in formatting and expand them Problem: - It is annoying to manually expand shrunk items for formatting. Solution: - Allow them to be automatically expanded when formatted. --- include/stdx/ct_format.hpp | 6 +++--- test/ct_format.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/stdx/ct_format.hpp b/include/stdx/ct_format.hpp index 6f9c567..bffcbe4 100644 --- a/include/stdx/ct_format.hpp +++ b/include/stdx/ct_format.hpp @@ -235,12 +235,12 @@ consteval auto arg_value(std::integral_constant) { if constexpr (std::is_enum_v) { return enum_as_string(); } else { - return V; + return expand(V); } } template consteval auto arg_value(type_identity) { - return type_as_string(); + return type_as_string>(); } template consteval auto arg_value(cts_t) { return S; } @@ -255,7 +255,7 @@ consteval auto arg_value(fmt_cx_value auto a) { } else if constexpr (std::is_enum_v) { return enum_as_string(); } else { - return a(); + return expand(a()); } } diff --git a/test/ct_format.cpp b/test/ct_format.cpp index 01b890b..9491453 100644 --- a/test/ct_format.cpp +++ b/test/ct_format.cpp @@ -68,6 +68,13 @@ TEST_CASE("format a compile-time stringish argument (CX_VALUE)", "Hello world"_fmt_res); } +TEST_CASE("format a shrunk compile-time stringish argument (CX_VALUE)", + "[ct_format]") { + using namespace std::string_view_literals; + STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE( + stdx::shrink<"world">())) == "Hello world"_fmt_res); +} + TEST_CASE("format a compile-time stringish argument (ct)", "[ct_format]") { using namespace std::string_view_literals; STATIC_REQUIRE(stdx::ct_format<"Hello {}">("world"_ctst) == @@ -76,6 +83,14 @@ TEST_CASE("format a compile-time stringish argument (ct)", "[ct_format]") { "Hello world"_fmt_res); } +TEST_CASE("format a shrunk compile-time stringish argument (ct)", + "[ct_format]") { + using namespace std::string_view_literals; + STATIC_REQUIRE( + stdx::ct_format<"Hello {}">(stdx::ct()>()) == + "Hello world"_fmt_res); +} + TEST_CASE("format a compile-time integral argument (CX_VALUE)", "[ct_format]") { STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE(42)) == "Hello 42"_fmt_res); @@ -139,11 +154,21 @@ TEST_CASE("format a type argument (CX_VALUE)", "[ct_format]") { "Hello int"_fmt_res); } +TEST_CASE("format a shrunk type argument (CX_VALUE)", "[ct_format]") { + STATIC_REQUIRE(stdx::ct_format<"Hello {}">(CX_VALUE(stdx::shrink_t)) == + "Hello int"_fmt_res); +} + TEST_CASE("format a type argument (ct)", "[ct_format]") { STATIC_REQUIRE(stdx::ct_format<"Hello {}">(stdx::ct()) == "Hello int"_fmt_res); } +TEST_CASE("format a shrunk type argument (ct)", "[ct_format]") { + STATIC_REQUIRE(stdx::ct_format<"Hello {}">( + stdx::ct>()) == "Hello int"_fmt_res); +} + TEST_CASE("format a compile-time argument with different base", "[ct_format]") { STATIC_REQUIRE(stdx::ct_format<"Hello 0x{:x}">(CX_VALUE(42)) == "Hello 0x2a"_fmt_res);