From 3aaa25fa70286dceb800b71c120be54dabbbcfa2 Mon Sep 17 00:00:00 2001 From: Artem Golubikhin Date: Wed, 6 Dec 2017 17:21:34 +0300 Subject: [PATCH] Added support for format string containing '\0' in _format udl (#619) (#620) Added support for strings containing '\0' in udl (#619) --- include/fmt/format.h | 6 +++--- test/format-test.cc | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index db618a8e..ae591704 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -4093,7 +4093,7 @@ class udl_formatter { # else template struct udl_formatter { - const Char *str; + basic_string_view str; template auto operator()(Args && ... args) const @@ -4134,9 +4134,9 @@ constexpr internal::udl_formatter operator""_format() { \endrst */ inline internal::udl_formatter -operator"" _format(const char *s, std::size_t) { return {s}; } +operator"" _format(const char *s, std::size_t n) { return {{s, n}}; } inline internal::udl_formatter -operator"" _format(const wchar_t *s, std::size_t) { return {s}; } +operator"" _format(const wchar_t *s, std::size_t n) { return {{s, n}}; } # endif // FMT_UDL_TEMPLATE /** diff --git a/test/format-test.cc b/test/format-test.cc index 0156bbf2..13258d44 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1469,6 +1469,11 @@ TEST(LiteralsTest, Format) { EXPECT_EQ(format("{}c{}", "ab", 1), udl_format); auto udl_format_w = L"{}c{}"_format(L"ab", 1); EXPECT_EQ(format(L"{}c{}", L"ab", 1), udl_format_w); + + auto udl_format_null_char = "{}c\0d{}"_format("ab", 1); + EXPECT_EQ(format("{}c\0d{}", "ab", 1), udl_format_null_char); + auto udl_format_w_null_char = L"{}c\0d{}"_format(L"ab", 1); + EXPECT_EQ(format(L"{}c\0d{}", L"ab", 1), udl_format_w_null_char); } TEST(LiteralsTest, NamedArg) { @@ -1529,6 +1534,7 @@ TEST(FormatTest, CustomArgFormatter) { TEST(FormatTest, NonNullTerminatedFormatString) { EXPECT_EQ("42", format(string_view("{}foo", 2), 42)); + EXPECT_EQ("42f\0oo", format(string_view("{}f\0oo", 6), 42)); } struct variant {