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
c882790a
Commit
c882790a
authored
Dec 18, 2021
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a set formatter
parent
121002d7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
25 deletions
+35
-25
include/fmt/ranges.h
include/fmt/ranges.h
+30
-25
test/ranges-test.cc
test/ranges-test.cc
+5
-0
No files found.
include/fmt/ranges.h
View file @
c882790a
...
@@ -19,21 +19,6 @@
...
@@ -19,21 +19,6 @@
FMT_BEGIN_NAMESPACE
FMT_BEGIN_NAMESPACE
template
<
typename
Char
,
typename
Enable
=
void
>
struct
formatting_range
{
#ifdef FMT_DEPRECATED_BRACED_RANGES
Char
prefix
=
'{'
;
Char
postfix
=
'}'
;
#else
Char
prefix
=
'['
;
Char
postfix
=
']'
;
#endif
template
<
typename
ParseContext
>
FMT_CONSTEXPR
auto
parse
(
ParseContext
&
ctx
)
->
decltype
(
ctx
.
begin
())
{
return
ctx
.
begin
();
}
};
template
<
typename
Char
,
typename
Enable
=
void
>
struct
formatting_tuple
{
template
<
typename
Char
,
typename
Enable
=
void
>
struct
formatting_tuple
{
Char
prefix
=
'('
;
Char
prefix
=
'('
;
Char
postfix
=
')'
;
Char
postfix
=
')'
;
...
@@ -87,7 +72,7 @@ template <typename Char>
...
@@ -87,7 +72,7 @@ template <typename Char>
struct
is_std_string_like
<
fmt
::
basic_string_view
<
Char
>>
:
std
::
true_type
{};
struct
is_std_string_like
<
fmt
::
basic_string_view
<
Char
>>
:
std
::
true_type
{};
template
<
typename
T
>
class
is_map
{
template
<
typename
T
>
class
is_map
{
template
<
typename
U
>
static
auto
check
(
U
*
)
->
typename
U
::
key
_type
;
template
<
typename
U
>
static
auto
check
(
U
*
)
->
typename
U
::
mapped
_type
;
template
<
typename
>
static
void
check
(...);
template
<
typename
>
static
void
check
(...);
public:
public:
...
@@ -99,6 +84,19 @@ template <typename T> class is_map {
...
@@ -99,6 +84,19 @@ template <typename T> class is_map {
#endif
#endif
};
};
template
<
typename
T
>
class
is_set
{
template
<
typename
U
>
static
auto
check
(
U
*
)
->
typename
U
::
key_type
;
template
<
typename
>
static
void
check
(...);
public:
#ifdef FMT_FORMAT_SET_AS_LIST
static
FMT_CONSTEXPR_DECL
const
bool
value
=
false
;
#else
static
FMT_CONSTEXPR_DECL
const
bool
value
=
!
std
::
is_void
<
decltype
(
check
<
T
>
(
nullptr
))
>::
value
&&
!
is_map
<
T
>::
value
;
#endif
};
template
<
typename
...
Ts
>
struct
conditional_helper
{};
template
<
typename
...
Ts
>
struct
conditional_helper
{};
template
<
typename
T
,
typename
_
=
void
>
struct
is_range_
:
std
::
false_type
{};
template
<
typename
T
,
typename
_
=
void
>
struct
is_range_
:
std
::
false_type
{};
...
@@ -602,11 +600,9 @@ struct formatter<
...
@@ -602,11 +600,9 @@ struct formatter<
detail
::
has_fallback_formatter
<
detail
::
value_type
<
T
>
,
Char
>::
value
)
detail
::
has_fallback_formatter
<
detail
::
value_type
<
T
>
,
Char
>::
value
)
#endif
#endif
>>
{
>>
{
formatting_range
<
Char
>
formatting
;
template
<
typename
ParseContext
>
template
<
typename
ParseContext
>
FMT_CONSTEXPR
auto
parse
(
ParseContext
&
ctx
)
->
decltype
(
ctx
.
begin
())
{
FMT_CONSTEXPR
auto
parse
(
ParseContext
&
ctx
)
->
decltype
(
ctx
.
begin
())
{
return
formatting
.
parse
(
ctx
);
return
ctx
.
begin
(
);
}
}
template
<
template
<
...
@@ -614,17 +610,26 @@ struct formatter<
...
@@ -614,17 +610,26 @@ struct formatter<
FMT_ENABLE_IF
(
FMT_ENABLE_IF
(
std
::
is_same
<
U
,
conditional_t
<
detail
::
has_const_begin_end
<
T
>
::
value
,
std
::
is_same
<
U
,
conditional_t
<
detail
::
has_const_begin_end
<
T
>
::
value
,
const
T
,
T
>>::
value
)
>
const
T
,
T
>>::
value
)
>
auto
format
(
U
&
values
,
FormatContext
&
ctx
)
->
decltype
(
ctx
.
out
())
{
auto
format
(
U
&
range
,
FormatContext
&
ctx
)
->
decltype
(
ctx
.
out
())
{
auto
out
=
detail
::
copy
(
formatting
.
prefix
,
ctx
.
out
());
#ifdef FMT_DEPRECATED_BRACED_RANGES
size_t
i
=
0
;
Char
prefix
=
'{'
;
auto
it
=
std
::
begin
(
values
);
Char
postfix
=
'}'
;
auto
end
=
std
::
end
(
values
);
#else
Char
prefix
=
detail
::
is_set
<
T
>::
value
?
'{'
:
'['
;
Char
postfix
=
detail
::
is_set
<
T
>::
value
?
'}'
:
']'
;
#endif
auto
out
=
ctx
.
out
();
*
out
++
=
prefix
;
int
i
=
0
;
auto
it
=
std
::
begin
(
range
);
auto
end
=
std
::
end
(
range
);
for
(;
it
!=
end
;
++
it
)
{
for
(;
it
!=
end
;
++
it
)
{
if
(
i
>
0
)
out
=
detail
::
write_delimiter
(
out
);
if
(
i
>
0
)
out
=
detail
::
write_delimiter
(
out
);
out
=
detail
::
write_range_entry
<
Char
>
(
out
,
*
it
);
out
=
detail
::
write_range_entry
<
Char
>
(
out
,
*
it
);
++
i
;
++
i
;
}
}
return
detail
::
copy
(
formatting
.
postfix
,
out
);
*
out
++
=
postfix
;
return
out
;
}
}
};
};
...
...
test/ranges-test.cc
View file @
c882790a
...
@@ -58,6 +58,11 @@ TEST(ranges_test, format_map) {
...
@@ -58,6 +58,11 @@ TEST(ranges_test, format_map) {
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
m
),
"{
\"
one
\"
: 1,
\"
two
\"
: 2}"
);
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
m
),
"{
\"
one
\"
: 1,
\"
two
\"
: 2}"
);
}
}
TEST
(
ranges_test
,
format_set
)
{
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
std
::
set
<
std
::
string
>
{
"one"
,
"two"
}),
"{
\"
one
\"
,
\"
two
\"
}"
);
}
TEST
(
ranges_test
,
format_pair
)
{
TEST
(
ranges_test
,
format_pair
)
{
auto
p
=
std
::
pair
<
int
,
float
>
(
42
,
1.5
f
);
auto
p
=
std
::
pair
<
int
,
float
>
(
42
,
1.5
f
);
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
p
),
"(42, 1.5)"
);
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
p
),
"(42, 1.5)"
);
...
...
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