Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix formatting a format_result #185

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions include/stdx/ct_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,27 @@ CONSTEVAL auto convert_output() {

template <ct_string Fmt, typename Arg> constexpr auto format1(Arg arg) {
if constexpr (requires { arg_value(arg); }) {
return [&] {
constexpr auto fmtstr = FMT_COMPILE(std::string_view{Fmt});
constexpr auto a = arg_value(arg);
auto const f = []<std::size_t N>(auto s, auto v) {
ct_string<N + 1> cts{};
fmt::format_to(cts.begin(), s, v);
return cts;
};
if constexpr (is_specialization_of_v<std::remove_cv_t<decltype(a)>,
format_result>) {
constexpr auto s = convert_input(a.str);
constexpr auto sz = fmt::formatted_size(fmtstr, s);
constexpr auto cts = f.template operator()<sz>(fmtstr, s);
return format_result{cts_t<cts>{}, a.args};
} else {
constexpr auto sz = fmt::formatted_size(fmtstr, a);
constexpr auto cts = f.template operator()<sz>(fmtstr, a);
return format_result{cts_t<cts>{}};
}
}();
constexpr auto fmtstr = FMT_COMPILE(std::string_view{Fmt});
constexpr auto a = arg_value(arg);
auto const f = []<std::size_t N>(auto s, auto v) {
ct_string<N + 1> cts{};
fmt::format_to(cts.begin(), s, v);
return cts;
};
if constexpr (is_specialization_of_v<std::remove_cv_t<decltype(a)>,
format_result>) {
constexpr auto s = convert_input(a.str);
constexpr auto sz = fmt::formatted_size(fmtstr, s);
constexpr auto cts = f.template operator()<sz>(fmtstr, s);
return format_result{cts_t<cts>{}, a.args};
} else {
constexpr auto sz = fmt::formatted_size(fmtstr, a);
constexpr auto cts = f.template operator()<sz>(fmtstr, a);
return format_result{cts_t<cts>{}};
}
} else if constexpr (is_specialization_of_v<Arg, format_result>) {
auto const sub_result = format1<Fmt>(arg.str);
return format_result{sub_result.str, arg.args};
} else {
return format_result{cts_t<Fmt>{}, tuple{arg}};
}
Expand Down
25 changes: 5 additions & 20 deletions test/ct_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,13 @@ TEST_CASE("format multiple mixed arguments", "[ct_format]") {
stdx::make_tuple(42, "B"sv)});
}

TEST_CASE("format a formatted string", "[ct_format]") {
TEST_CASE("format a format result", "[ct_format]") {
static_assert(stdx::ct_format<"The value is {}.">(
CX_VALUE(stdx::ct_format<"(year={})">(2022))) ==
stdx::ct_format<"(year={})">(2022)) ==
stdx::format_result{"The value is (year={})."_ctst,
stdx::make_tuple(2022)});
}

TEST_CASE("format a ct-formatted string", "[ct_format]") {
constexpr static auto cts = stdx::ct_format<"(year={})">(CX_VALUE(2024));
static_assert(stdx::ct_format<"The value is {}.">(CX_VALUE(cts)) ==
"The value is (year=2024)."_fmt_res);
}

namespace {
template <typename T, T...> struct string_constant {
private:
Expand Down Expand Up @@ -182,23 +176,14 @@ TEST_CASE("format a string-type argument", "[ct_format]") {
"Hello A!"_fmt_res);
}

TEST_CASE("format a formatted string with different type", "[ct_format]") {
static_assert(stdx::ct_format<"A{}D", string_constant>(CX_VALUE(
stdx::ct_format<"B{}C", string_constant>(2022))) ==
TEST_CASE("format a format result with different type", "[ct_format]") {
static_assert(stdx::ct_format<"A{}D", string_constant>(
stdx::ct_format<"B{}C", string_constant>(2022)) ==
stdx::format_result{
string_constant<char, 'A', 'B', '{', '}', 'C', 'D'>{},
stdx::make_tuple(2022)});
}

TEST_CASE("format a ct-formatted string with different type", "[ct_format]") {
constexpr static auto cts =
stdx::ct_format<"B{}C", string_constant>(CX_VALUE(2024));
static_assert(
stdx::ct_format<"A{}D", string_constant>(CX_VALUE(cts)) ==
stdx::format_result{
string_constant<char, 'A', 'B', '2', '0', '2', '4', 'C', 'D'>{}});
}

TEST_CASE("format multiple mixed arguments with different type",
"[ct_format]") {
using namespace std::string_view_literals;
Expand Down