Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
2f7e0885
Commit
2f7e0885
authored
Sep 20, 2020
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Disable range formatter if value type is not formattable (#1885)
parent
c46a8de4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
6 deletions
+25
-6
include/fmt/ranges.h
include/fmt/ranges.h
+13
-6
test/ranges-test.cc
test/ranges-test.cc
+12
-0
No files found.
include/fmt/ranges.h
View file @
2f7e0885
...
...
@@ -157,6 +157,9 @@ template <class Tuple, class F> void for_each(Tuple&& tup, F&& f) {
for_each
(
indexes
,
std
::
forward
<
Tuple
>
(
tup
),
std
::
forward
<
F
>
(
f
));
}
template
<
typename
Range
>
using
value_type
=
remove_cvref_t
<
decltype
(
*
std
::
declval
<
Range
>
().
begin
())
>
;
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
&
)
{
...
...
@@ -182,7 +185,6 @@ FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) {
FMT_CONSTEXPR
const
wchar_t
*
format_str_quoted
(
bool
add_space
,
const
wchar_t
)
{
return
add_space
?
L" '{}'"
:
L"'{}'"
;
}
}
// namespace detail
template
<
typename
T
>
struct
is_tuple_like
{
...
...
@@ -246,9 +248,15 @@ template <typename T, typename Char> struct is_range {
!
std
::
is_constructible
<
detail
::
std_string_view
<
Char
>
,
T
>::
value
;
};
template
<
typename
RangeT
,
typename
Char
>
struct
formatter
<
RangeT
,
Char
,
enable_if_t
<
fmt
::
is_range
<
RangeT
,
Char
>::
value
>>
{
template
<
typename
T
,
typename
Char
>
struct
formatter
<
T
,
Char
,
enable_if_t
<
fmt
::
is_range
<
T
,
Char
>::
value
// Workaround a bug in MSVC 2017 and earlier.
#if !FMT_MSC_VER || FMT_MSC_VER >= 1927
&&
has_formatter
<
detail
::
value_type
<
T
>
,
format_context
>::
value
#endif
>>
{
formatting_range
<
Char
>
formatting
;
template
<
typename
ParseContext
>
...
...
@@ -257,8 +265,7 @@ struct formatter<RangeT, Char,
}
template
<
typename
FormatContext
>
typename
FormatContext
::
iterator
format
(
const
RangeT
&
values
,
FormatContext
&
ctx
)
{
typename
FormatContext
::
iterator
format
(
const
T
&
values
,
FormatContext
&
ctx
)
{
auto
out
=
detail
::
copy
(
formatting
.
prefix
,
ctx
.
out
());
size_t
i
=
0
;
auto
it
=
values
.
begin
();
...
...
test/ranges-test.cc
View file @
2f7e0885
...
...
@@ -141,13 +141,16 @@ TEST(RangesTest, FormatStringLike) {
#endif // FMT_USE_STRING_VIEW
struct
zstring_sentinel
{};
bool
operator
==
(
const
char
*
p
,
zstring_sentinel
)
{
return
*
p
==
'\0'
;
}
bool
operator
!=
(
const
char
*
p
,
zstring_sentinel
)
{
return
*
p
!=
'\0'
;
}
struct
zstring
{
const
char
*
p
;
const
char
*
begin
()
const
{
return
p
;
}
zstring_sentinel
end
()
const
{
return
{};
}
};
TEST
(
RangesTest
,
JoinSentinel
)
{
zstring
hello
{
"hello"
};
EXPECT_EQ
(
"{'h', 'e', 'l', 'l', 'o'}"
,
fmt
::
format
(
"{}"
,
hello
));
...
...
@@ -189,3 +192,12 @@ TEST(RangesTest, JoinRange) {
const
std
::
vector
<
int
>
z
(
3u
,
0
);
EXPECT_EQ
(
"0,0,0"
,
fmt
::
format
(
"{}"
,
fmt
::
join
(
z
,
","
)));
}
#if !FMT_MSC_VER || FMT_MSC_VER >= 1927
struct
unformattable
{};
TEST
(
RangesTest
,
UnformattableRange
)
{
EXPECT_FALSE
((
fmt
::
has_formatter
<
std
::
vector
<
unformattable
>
,
fmt
::
format_context
>::
value
));
}
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment