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
a980d3b4
Commit
a980d3b4
authored
Jan 27, 2018
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add fmt::join to format ranges (#466)
parent
87eab90e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
1 deletion
+83
-1
include/fmt/core.h
include/fmt/core.h
+5
-0
include/fmt/format.h
include/fmt/format.h
+54
-0
test/format-test.cc
test/format-test.cc
+24
-1
No files found.
include/fmt/core.h
View file @
a980d3b4
...
...
@@ -26,6 +26,11 @@
#ifdef __GNUC__
# define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
# if __cplusplus >= 201103L || defined __GXX_EXPERIMENTAL_CXX0X__
# define FMT_HAS_GXX_CXX11 1
# endif
# else
# define FMT_HAS_GXX_CXX11 0
#endif
#ifdef _MSC_VER
...
...
include/fmt/format.h
View file @
a980d3b4
...
...
@@ -3045,6 +3045,60 @@ constexpr fill_spec_factory fill;
constexpr
format_spec_factory
<
width_spec
>
width
;
constexpr
format_spec_factory
<
type_spec
>
type
;
template
<
typename
It
,
typename
Char
>
struct
arg_join
{
It
begin
;
It
end
;
basic_string_view
<
Char
>
sep
;
arg_join
(
It
begin
,
It
end
,
basic_string_view
<
Char
>
sep
)
:
begin
(
begin
),
end
(
end
),
sep
(
sep
)
{}
};
template
<
typename
It
,
typename
Char
>
struct
formatter
<
arg_join
<
It
,
Char
>
,
Char
>:
formatter
<
typename
std
::
iterator_traits
<
It
>::
value_type
,
Char
>
{
template
<
typename
FormatContext
>
auto
format
(
const
arg_join
<
It
,
Char
>
&
value
,
FormatContext
&
ctx
)
{
using
base
=
formatter
<
typename
std
::
iterator_traits
<
It
>::
value_type
,
Char
>
;
auto
it
=
value
.
begin
;
auto
out
=
ctx
.
begin
();
if
(
it
!=
value
.
end
)
{
out
=
base
::
format
(
*
it
++
,
ctx
);
while
(
it
!=
value
.
end
)
{
out
=
std
::
copy
(
value
.
sep
.
begin
(),
value
.
sep
.
end
(),
out
);
ctx
.
advance_to
(
out
);
out
=
base
::
format
(
*
it
++
,
ctx
);
}
}
return
out
;
}
};
template
<
typename
It
>
arg_join
<
It
,
char
>
join
(
It
begin
,
It
end
,
string_view
sep
)
{
return
arg_join
<
It
,
char
>
(
begin
,
end
,
sep
);
}
template
<
typename
It
>
arg_join
<
It
,
wchar_t
>
join
(
It
begin
,
It
end
,
wstring_view
sep
)
{
return
arg_join
<
It
,
wchar_t
>
(
begin
,
end
,
sep
);
}
#if FMT_HAS_GXX_CXX11
template
<
typename
Range
>
auto
join
(
const
Range
&
range
,
string_view
sep
)
->
arg_join
<
decltype
(
std
::
begin
(
range
)),
char
>
{
return
join
(
std
::
begin
(
range
),
std
::
end
(
range
),
sep
);
}
template
<
typename
Range
>
auto
join
(
const
Range
&
range
,
wstring_view
sep
)
->
arg_join
<
decltype
(
std
::
begin
(
range
)),
wchar_t
>
{
return
join
(
std
::
begin
(
range
),
std
::
end
(
range
),
sep
);
}
#endif
/**
\rst
Converts *value* to ``std::string`` using the default format for type *T*.
...
...
test/format-test.cc
View file @
a980d3b4
...
...
@@ -39,7 +39,7 @@
// Test that the library compiles if None is defined to 0 as done by xlib.h.
#define None 0
#include "fmt/
core
.h"
#include "fmt/
format
.h"
#include "util.h"
#include "mock-allocator.h"
...
...
@@ -1403,6 +1403,29 @@ TEST(FormatTest, Variadic) {
EXPECT_EQ
(
L"abc1"
,
format
(
L"{}c{}"
,
L"ab"
,
1
));
}
TEST
(
FormatTest
,
JoinArg
)
{
using
fmt
::
join
;
int
v1
[
3
]
=
{
1
,
2
,
3
};
std
::
vector
<
float
>
v2
;
v2
.
push_back
(
1.2
f
);
v2
.
push_back
(
3.4
f
);
EXPECT_EQ
(
"(1, 2, 3)"
,
format
(
"({})"
,
join
(
v1
,
v1
+
3
,
", "
)));
EXPECT_EQ
(
"(1)"
,
format
(
"({})"
,
join
(
v1
,
v1
+
1
,
", "
)));
EXPECT_EQ
(
"()"
,
format
(
"({})"
,
join
(
v1
,
v1
,
", "
)));
EXPECT_EQ
(
"(001, 002, 003)"
,
format
(
"({:03})"
,
join
(
v1
,
v1
+
3
,
", "
)));
EXPECT_EQ
(
"(+01.20, +03.40)"
,
format
(
"({:+06.2f})"
,
join
(
v2
.
begin
(),
v2
.
end
(),
", "
)));
EXPECT_EQ
(
L"(1, 2, 3)"
,
format
(
L"({})"
,
join
(
v1
,
v1
+
3
,
L", "
)));
EXPECT_EQ
(
"1, 2, 3"
,
format
(
"{0:{1}}"
,
join
(
v1
,
v1
+
3
,
", "
),
1
));
#if FMT_HAS_GXX_CXX11
EXPECT_EQ
(
"(1, 2, 3)"
,
format
(
"({})"
,
join
(
v1
,
", "
)));
EXPECT_EQ
(
"(+01.20, +03.40)"
,
format
(
"({:+06.2f})"
,
join
(
v2
,
", "
)));
#endif
}
template
<
typename
T
>
std
::
string
str
(
const
T
&
value
)
{
return
fmt
::
format
(
"{}"
,
value
);
...
...
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