Skip to content

Commit d7ac9af

Browse files
committed
Support fmt::runtime with wchar_t in fmt::format_to_n
1 parent ae6fd83 commit d7ac9af

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

include/fmt/base.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,9 @@ class locale_ref {
906906

907907
FMT_END_EXPORT
908908

909+
// forward declaration
910+
template <typename Char> class runtime_format_string;
911+
909912
namespace detail {
910913

911914
// Specifies if `T` is a code unit type.
@@ -935,6 +938,19 @@ constexpr auto to_string_view(basic_string_view<Char> s)
935938
-> basic_string_view<Char> {
936939
return s;
937940
}
941+
template <typename Char>
942+
constexpr auto to_string_view(runtime_format_string<Char> s)
943+
-> basic_string_view<Char> {
944+
return s.str;
945+
}
946+
// forward declaration of internal compile-time string type base
947+
class compile_string;
948+
template <typename Char, typename S,
949+
FMT_ENABLE_IF(std::is_base_of<detail::compile_string, S>::value&&
950+
std::is_same<typename S::char_type, Char>::value)>
951+
constexpr auto to_string_view(const S& s) -> basic_string_view<Char> {
952+
return basic_string_view<Char>(s);
953+
}
938954

939955
template <typename T, typename Enable = void>
940956
struct has_to_string_view : std::false_type {};

include/fmt/xchar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ template <typename OutputIt, typename S, typename... T,
285285
detail::is_exotic_char<Char>::value)>
286286
inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args)
287287
-> format_to_n_result<OutputIt> {
288-
return vformat_to_n(out, n, fmt::basic_string_view<Char>(fmt),
288+
return vformat_to_n(out, n, detail::to_string_view<Char>(fmt),
289289
fmt::make_format_args<buffered_context<Char>>(args...));
290290
}
291291

test/xchar-test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ TEST(format_test, wide_format_to_n) {
146146
EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4));
147147
}
148148

149+
TEST(format_test, wide_format_to_n_runtime) {
150+
wchar_t buffer[4];
151+
buffer[3] = L'x';
152+
auto result = fmt::format_to_n(buffer, 3, fmt::runtime(L"{}"), 12345);
153+
EXPECT_EQ(5u, result.size);
154+
EXPECT_EQ(buffer + 3, result.out);
155+
EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4));
156+
buffer[0] = L'x';
157+
buffer[1] = L'x';
158+
buffer[2] = L'x';
159+
result = fmt::format_to_n(buffer, 3, fmt::runtime(L"{}"), L'A');
160+
EXPECT_EQ(1u, result.size);
161+
EXPECT_EQ(buffer + 1, result.out);
162+
EXPECT_EQ(L"Axxx", fmt::wstring_view(buffer, 4));
163+
result = fmt::format_to_n(buffer, 3, fmt::runtime(L"{}{} "), L'B', L'C');
164+
EXPECT_EQ(3u, result.size);
165+
EXPECT_EQ(buffer + 3, result.out);
166+
EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4));
167+
}
168+
149169
TEST(xchar_test, named_arg_udl) {
150170
using namespace fmt::literals;
151171
auto udl_a =

0 commit comments

Comments
 (0)