Skip to content

Commit a3886fa

Browse files
authored
Merge pull request #185 from elbeno/fix-format-format-result
2 parents dfc27dd + dbabe50 commit a3886fa

File tree

2 files changed

+26
-40
lines changed

2 files changed

+26
-40
lines changed

include/stdx/ct_format.hpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,27 @@ CONSTEVAL auto convert_output() {
168168

169169
template <ct_string Fmt, typename Arg> constexpr auto format1(Arg arg) {
170170
if constexpr (requires { arg_value(arg); }) {
171-
return [&] {
172-
constexpr auto fmtstr = FMT_COMPILE(std::string_view{Fmt});
173-
constexpr auto a = arg_value(arg);
174-
auto const f = []<std::size_t N>(auto s, auto v) {
175-
ct_string<N + 1> cts{};
176-
fmt::format_to(cts.begin(), s, v);
177-
return cts;
178-
};
179-
if constexpr (is_specialization_of_v<std::remove_cv_t<decltype(a)>,
180-
format_result>) {
181-
constexpr auto s = convert_input(a.str);
182-
constexpr auto sz = fmt::formatted_size(fmtstr, s);
183-
constexpr auto cts = f.template operator()<sz>(fmtstr, s);
184-
return format_result{cts_t<cts>{}, a.args};
185-
} else {
186-
constexpr auto sz = fmt::formatted_size(fmtstr, a);
187-
constexpr auto cts = f.template operator()<sz>(fmtstr, a);
188-
return format_result{cts_t<cts>{}};
189-
}
190-
}();
171+
constexpr auto fmtstr = FMT_COMPILE(std::string_view{Fmt});
172+
constexpr auto a = arg_value(arg);
173+
auto const f = []<std::size_t N>(auto s, auto v) {
174+
ct_string<N + 1> cts{};
175+
fmt::format_to(cts.begin(), s, v);
176+
return cts;
177+
};
178+
if constexpr (is_specialization_of_v<std::remove_cv_t<decltype(a)>,
179+
format_result>) {
180+
constexpr auto s = convert_input(a.str);
181+
constexpr auto sz = fmt::formatted_size(fmtstr, s);
182+
constexpr auto cts = f.template operator()<sz>(fmtstr, s);
183+
return format_result{cts_t<cts>{}, a.args};
184+
} else {
185+
constexpr auto sz = fmt::formatted_size(fmtstr, a);
186+
constexpr auto cts = f.template operator()<sz>(fmtstr, a);
187+
return format_result{cts_t<cts>{}};
188+
}
189+
} else if constexpr (is_specialization_of_v<Arg, format_result>) {
190+
auto const sub_result = format1<Fmt>(arg.str);
191+
return format_result{sub_result.str, arg.args};
191192
} else {
192193
return format_result{cts_t<Fmt>{}, tuple{arg}};
193194
}

test/ct_format.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,13 @@ TEST_CASE("format multiple mixed arguments", "[ct_format]") {
135135
stdx::make_tuple(42, "B"sv)});
136136
}
137137

138-
TEST_CASE("format a formatted string", "[ct_format]") {
138+
TEST_CASE("format a format result", "[ct_format]") {
139139
static_assert(stdx::ct_format<"The value is {}.">(
140-
CX_VALUE(stdx::ct_format<"(year={})">(2022))) ==
140+
stdx::ct_format<"(year={})">(2022)) ==
141141
stdx::format_result{"The value is (year={})."_ctst,
142142
stdx::make_tuple(2022)});
143143
}
144144

145-
TEST_CASE("format a ct-formatted string", "[ct_format]") {
146-
constexpr static auto cts = stdx::ct_format<"(year={})">(CX_VALUE(2024));
147-
static_assert(stdx::ct_format<"The value is {}.">(CX_VALUE(cts)) ==
148-
"The value is (year=2024)."_fmt_res);
149-
}
150-
151145
namespace {
152146
template <typename T, T...> struct string_constant {
153147
private:
@@ -182,23 +176,14 @@ TEST_CASE("format a string-type argument", "[ct_format]") {
182176
"Hello A!"_fmt_res);
183177
}
184178

185-
TEST_CASE("format a formatted string with different type", "[ct_format]") {
186-
static_assert(stdx::ct_format<"A{}D", string_constant>(CX_VALUE(
187-
stdx::ct_format<"B{}C", string_constant>(2022))) ==
179+
TEST_CASE("format a format result with different type", "[ct_format]") {
180+
static_assert(stdx::ct_format<"A{}D", string_constant>(
181+
stdx::ct_format<"B{}C", string_constant>(2022)) ==
188182
stdx::format_result{
189183
string_constant<char, 'A', 'B', '{', '}', 'C', 'D'>{},
190184
stdx::make_tuple(2022)});
191185
}
192186

193-
TEST_CASE("format a ct-formatted string with different type", "[ct_format]") {
194-
constexpr static auto cts =
195-
stdx::ct_format<"B{}C", string_constant>(CX_VALUE(2024));
196-
static_assert(
197-
stdx::ct_format<"A{}D", string_constant>(CX_VALUE(cts)) ==
198-
stdx::format_result{
199-
string_constant<char, 'A', 'B', '2', '0', '2', '4', 'C', 'D'>{}});
200-
}
201-
202187
TEST_CASE("format multiple mixed arguments with different type",
203188
"[ct_format]") {
204189
using namespace std::string_view_literals;

0 commit comments

Comments
 (0)