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
7aca36bc
Unverified
Commit
7aca36bc
authored
Oct 16, 2021
by
Barry Revzin
Committed by
GitHub
Oct 16, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extending fmt::join to support C++20-only ranges. (#2549)
parent
f5371a75
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
3 deletions
+48
-3
include/fmt/format.h
include/fmt/format.h
+10
-3
test/ranges-test.cc
test/ranges-test.cc
+38
-0
No files found.
include/fmt/format.h
View file @
7aca36bc
...
...
@@ -2748,7 +2748,12 @@ using arg_join FMT_DEPRECATED_ALIAS = join_view<It, Sentinel, Char>;
template
<
typename
It
,
typename
Sentinel
,
typename
Char
>
struct
formatter
<
join_view
<
It
,
Sentinel
,
Char
>
,
Char
>
{
private:
using
value_type
=
typename
std
::
iterator_traits
<
It
>::
value_type
;
using
value_type
=
#ifdef __cpp_lib_ranges
std
::
iter_value_t
<
It
>
;
#else
typename
std
::
iterator_traits
<
It
>::
value_type
;
#endif
using
context
=
buffer_context
<
Char
>
;
using
mapper
=
detail
::
arg_mapper
<
context
>
;
...
...
@@ -2782,11 +2787,13 @@ struct formatter<join_view<It, Sentinel, Char>, Char> {
auto
it
=
value
.
begin
;
auto
out
=
ctx
.
out
();
if
(
it
!=
value
.
end
)
{
out
=
value_formatter_
.
format
(
map
(
*
it
++
),
ctx
);
out
=
value_formatter_
.
format
(
map
(
*
it
),
ctx
);
++
it
;
while
(
it
!=
value
.
end
)
{
out
=
detail
::
copy_str
<
Char
>
(
value
.
sep
.
begin
(),
value
.
sep
.
end
(),
out
);
ctx
.
advance_to
(
out
);
out
=
value_formatter_
.
format
(
map
(
*
it
++
),
ctx
);
out
=
value_formatter_
.
format
(
map
(
*
it
),
ctx
);
++
it
;
}
}
return
out
;
...
...
test/ranges-test.cc
View file @
7aca36bc
...
...
@@ -258,6 +258,36 @@ struct zstring {
zstring_sentinel
end
()
const
{
return
{};
}
};
# ifdef __cpp_lib_ranges
struct
cpp20_only_range
{
struct
iterator
{
int
val
=
0
;
using
value_type
=
int
;
using
difference_type
=
std
::
ptrdiff_t
;
using
iterator_concept
=
std
::
input_iterator_tag
;
iterator
()
=
default
;
iterator
(
int
i
)
:
val
(
i
)
{}
int
operator
*
()
const
{
return
val
;
}
iterator
&
operator
++
()
{
++
val
;
return
*
this
;
}
void
operator
++
(
int
)
{
++*
this
;
}
bool
operator
==
(
const
iterator
&
rhs
)
const
{
return
val
==
rhs
.
val
;
}
};
int
lo
;
int
hi
;
iterator
begin
()
const
{
return
iterator
(
lo
);
}
iterator
end
()
const
{
return
iterator
(
hi
);
}
};
static_assert
(
std
::
input_iterator
<
cpp20_only_range
::
iterator
>
);
# endif
TEST
(
ranges_test
,
join_sentinel
)
{
auto
hello
=
zstring
{
"hello"
};
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
hello
),
"['h', 'e', 'l', 'l', 'o']"
);
...
...
@@ -282,6 +312,14 @@ TEST(ranges_test, join_range) {
const
auto
z
=
std
::
vector
<
int
>
(
3u
,
0
);
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
fmt
::
join
(
z
,
","
)),
"0,0,0"
);
# ifdef __cpp_lib_ranges
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
cpp20_only_range
{.
lo
=
0
,
.
hi
=
5
}),
"[0, 1, 2, 3, 4]"
);
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
fmt
::
join
(
cpp20_only_range
{.
lo
=
0
,
.
hi
=
5
},
","
)),
"0,1,2,3,4"
);
# endif
}
#endif // FMT_RANGES_TEST_ENABLE_JOIN
...
...
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