-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add tests for FMT_ENFORCE_COMPILE_STRING, fix several errors #2038
Changes from 31 commits
e05c8fc
f76c64a
b75bd49
2f9484c
3b6d21b
aa65e99
54d6d5b
e805e8e
1fbfccc
55600c9
35b94fa
1e034bd
900adeb
7f1c041
d343e4b
c4c8351
60bceae
b61537a
d539f76
dafbc05
9fafb14
2489501
b8ebadd
098de0d
8e575bd
f2a5a0f
c85d5e3
b9063f9
98a8830
b0c65df
6f393f2
06a576c
99ed2a8
5083486
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,31 +247,48 @@ template <typename Range> | |
using value_type = | ||
remove_cvref_t<decltype(*detail::range_begin(std::declval<Range>()))>; | ||
|
||
template <typename Arg, FMT_ENABLE_IF(!is_like_std_string< | ||
typename std::decay<Arg>::type>::value)> | ||
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&) { | ||
return add_space ? " {}" : "{}"; | ||
template <typename OutputIt, typename Formatting> | ||
OutputIt add_range_formatting_spaces(OutputIt out, | ||
const Formatting& formatting) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add_range_formatting_spaces -> write_delimiter |
||
if (const_check(formatting.add_prepostfix_space)) { | ||
*out++ = ' '; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just remove this since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My impression was that in user-declared overloads it could not be? (nm, I see thats impossible) |
||
out = detail::copy(formatting.delimiter, out); | ||
if (const_check(formatting.add_delimiter_spaces)) { | ||
*out++ = ' '; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's write space unconditionally since |
||
return out; | ||
} | ||
|
||
template <typename Arg, FMT_ENABLE_IF(is_like_std_string< | ||
typename std::decay<Arg>::type>::value)> | ||
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&) { | ||
return add_space ? " \"{}\"" : "\"{}\""; | ||
template < | ||
typename Char, typename OutputIt, typename Arg, | ||
FMT_ENABLE_IF(std::is_same<Arg, const char*>::value || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that this was required to make it so that for example a |
||
is_like_std_string<typename std::decay<Arg>::type>::value)> | ||
OutputIt write_range_entry(OutputIt out, const Arg& v) { | ||
*out++ = '"'; | ||
out = write<Char>(out, v); | ||
*out++ = '"'; | ||
return out; | ||
} | ||
|
||
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char*) { | ||
return add_space ? " \"{}\"" : "\"{}\""; | ||
} | ||
FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t*) { | ||
return add_space ? L" \"{}\"" : L"\"{}\""; | ||
template <typename Char, typename OutputIt, typename Arg, | ||
FMT_ENABLE_IF(std::is_same<Arg, Char>::value)> | ||
OutputIt write_range_entry(OutputIt out, const Arg v) { | ||
*out++ = '\''; | ||
*out++ = v; | ||
*out++ = '\''; | ||
return out; | ||
} | ||
|
||
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) { | ||
return add_space ? " '{}'" : "'{}'"; | ||
} | ||
FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t) { | ||
return add_space ? L" '{}'" : L"'{}'"; | ||
template < | ||
typename Char, typename OutputIt, typename Arg, | ||
FMT_ENABLE_IF(!std::is_same<Arg, const char*>::value && | ||
!is_like_std_string<typename std::decay<Arg>::type>::value && | ||
!std::is_same<Arg, Char>::value)> | ||
OutputIt write_range_entry(OutputIt out, const Arg& v) { | ||
return write<Char>(out, v); | ||
} | ||
|
||
} // namespace detail | ||
|
||
template <typename T> struct is_tuple_like { | ||
|
@@ -286,15 +303,10 @@ struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> { | |
template <typename FormatContext> struct format_each { | ||
template <typename T> void operator()(const T& v) { | ||
if (i > 0) { | ||
if (formatting.add_prepostfix_space) { | ||
*out++ = ' '; | ||
} | ||
out = detail::copy(formatting.delimiter, out); | ||
out = add_range_formatting_spaces(out, formatting); | ||
} | ||
out = format_to(out, | ||
detail::format_str_quoted( | ||
(formatting.add_delimiter_spaces && i > 0), v), | ||
v); | ||
|
||
out = detail::write_range_entry<Char>(out, v); | ||
++i; | ||
} | ||
|
||
|
@@ -363,19 +375,19 @@ struct formatter< | |
auto end = view.end(); | ||
for (; it != end; ++it) { | ||
if (i > 0) { | ||
if (formatting.add_prepostfix_space) *out++ = ' '; | ||
out = detail::copy(formatting.delimiter, out); | ||
out = detail::add_range_formatting_spaces(out, formatting); | ||
} | ||
out = format_to(out, | ||
detail::format_str_quoted( | ||
(formatting.add_delimiter_spaces && i > 0), *it), | ||
*it); | ||
|
||
out = detail::write_range_entry<Char>(out, *it); | ||
|
||
if (++i > formatting.range_length_limit) { | ||
out = format_to(out, " ... <other elements>"); | ||
out = format_to(out, FMT_STRING("{}"), " ... <other elements>"); | ||
break; | ||
} | ||
} | ||
if (formatting.add_prepostfix_space) *out++ = ' '; | ||
if (formatting.add_prepostfix_space) { | ||
*out++ = ' '; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert (or remove). |
||
return detail::copy(formatting.postfix, out); | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,14 @@ add_fmt_test(printf-test) | |
add_fmt_test(ranges-test) | ||
add_fmt_test(scan-test) | ||
|
||
if (NOT MSVC) | ||
# FMT_ENFORCE_COMPILE_STRING not supported under MSVC | ||
# See https://developercommunity.visualstudio.com/content/problem/1277597/internal-compiler-c0001-error-on-complex-nested-la.html | ||
add_fmt_test(enforce-compile-string-test) | ||
target_compile_definitions(enforce-compile-string-test PRIVATE | ||
"-DFMT_ENFORCE_COMPILE_STRING") | ||
endif() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
if (NOT DEFINED MSVC_STATIC_RUNTIME AND MSVC) | ||
foreach (flag_var | ||
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Formatting library for C++ - formatting library tests | ||
// | ||
// Copyright (c) 2012 - present, Victor Zverovich | ||
// All rights reserved. | ||
// | ||
// For the license information refer to format.h. | ||
|
||
#include <array> | ||
#include <chrono> | ||
#include <iterator> | ||
#include <list> | ||
#include <string> | ||
|
||
#include "fmt/chrono.h" | ||
#include "fmt/color.h" | ||
#include "fmt/format.h" | ||
#include "fmt/locale.h" | ||
#include "fmt/ostream.h" | ||
#include "fmt/ranges.h" | ||
|
||
// Exercise the API to verify that everything we expect to can compile. | ||
void test_format_api() { | ||
fmt::format(FMT_STRING("{}"), 42); | ||
fmt::format(FMT_STRING(L"{}"), 42); | ||
fmt::format(FMT_STRING("noop")); | ||
|
||
fmt::to_string(42); | ||
fmt::to_wstring(42); | ||
|
||
std::list<char> out; | ||
fmt::format_to(std::back_inserter(out), FMT_STRING("{}"), 42); | ||
|
||
char buffer[4]; | ||
fmt::format_to_n(buffer, 3, FMT_STRING("{}"), 12345); | ||
|
||
wchar_t wbuffer[4]; | ||
fmt::format_to_n(wbuffer, 3, FMT_STRING(L"{}"), 12345); | ||
} | ||
|
||
void test_literals_api() { | ||
#if FMT_USE_UDL_TEMPLATE | ||
using namespace fmt::literals; | ||
"{}c{}"_format("ab", 1); | ||
L"{}c{}"_format(L"ab", 1); | ||
#endif | ||
} | ||
|
||
void test_chrono() { | ||
fmt::format(FMT_STRING("{}"), std::chrono::seconds(42)); | ||
fmt::format(FMT_STRING(L"{}"), std::chrono::seconds(42)); | ||
} | ||
|
||
void test_text_style() { | ||
fmt::print(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"), "rgb(255,20,30)"); | ||
fmt::format(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"), "rgb(255,20,30)"); | ||
|
||
fmt::text_style ts = fg(fmt::rgb(255, 20, 30)); | ||
std::string out; | ||
fmt::format_to(std::back_inserter(out), ts, | ||
FMT_STRING("rgb(255,20,30){}{}{}"), 1, 2, 3); | ||
} | ||
|
||
void test_range() { | ||
std::array<char, 5> hello = {'h', 'e', 'l', 'l', 'o'}; | ||
fmt::format(FMT_STRING("{}"), hello); | ||
} | ||
|
||
int main() { | ||
test_format_api(); | ||
test_literals_api(); | ||
test_chrono(); | ||
test_text_style(); | ||
test_range(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated, please revert.