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
a03842b0
Commit
a03842b0
authored
Nov 05, 2017
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More compile-time checks
parent
1c855a47
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
18 deletions
+24
-18
include/fmt/format.h
include/fmt/format.h
+18
-14
test/format-test.cc
test/format-test.cc
+6
-4
No files found.
include/fmt/format.h
View file @
a03842b0
...
...
@@ -3812,33 +3812,37 @@ struct udl_format_handler {
constexpr
void
on_text
(
const
Char
*
,
const
Char
*
)
{}
constexpr
void
on_arg_id
()
{
++
arg_index_
;
}
template
<
typename
T
>
constexpr
void
on_arg_id
(
T
)
{}
constexpr
void
on_arg_id
()
{
++
arg_index_
;
check_arg_index
();
}
constexpr
void
on_arg_id
(
unsigned
index
)
{
arg_index_
=
index
;
check_arg_index
();
}
constexpr
void
on_arg_id
(
basic_string_view
<
Char
>
)
{}
constexpr
void
on_replacement_field
(
const
Char
*
)
{}
constexpr
const
Char
*
on_format_specs
(
const
Char
*
s
)
{
if
(
arg_index_
<
0
||
arg_index_
>=
sizeof
...(
Args
))
{
on_error
(
"argument index out of range"
);
return
s
;
}
parse_context
<
Char
>
ctx
(
basic_string_view
<
Char
>
(
s
,
end_
-
s
));
return
parse_funcs_
[
arg_index_
](
ctx
);
}
constexpr
void
on_error
(
const
char
*
)
{
error_
=
true
;
}
constexpr
bool
is_valid
()
const
{
return
!
error_
;
}
// This function is intentionally not constexpr to give a compile-time error.
void
on_error
(
const
char
*
);
private:
constexpr
void
check_arg_index
()
{
if
(
arg_index_
<
0
||
arg_index_
>=
sizeof
...(
Args
))
on_error
(
"argument index out of range"
);
}
// Format specifier parsing function.
using
parse_func
=
const
Char
*
(
*
)(
parse_context
<
Char
>
&
);
const
Char
*
end_
;
int
arg_index_
=
-
1
;
bool
error_
=
false
;
parse_func
parse_funcs_
[
sizeof
...(
Args
)]
=
{
&
parse_format_specs
<
Char
,
Args
>
...
};
...
...
@@ -3850,7 +3854,7 @@ class udl_formatter {
template
<
typename
...
Args
>
std
::
basic_string
<
Char
>
operator
()(
const
Args
&
...
args
)
const
{
constexpr
Char
s
[]
=
{
CHARS
...,
'\0'
};
static_assert
(
check_format
<
Args
...
>
(
s
),
"error parsing format string"
);
constexpr
bool
invalid_format
=
check_format
<
Args
...
>
(
s
);
return
format
(
s
,
args
...);
}
...
...
@@ -3859,7 +3863,7 @@ class udl_formatter {
static
constexpr
bool
check_format
(
const
Char
*
s
)
{
udl_format_handler
<
Char
,
Args
...
>
handler
(
s
+
sizeof
...(
CHARS
));
internal
::
parse_format_string
(
s
,
handler
);
return
handler
.
is_valid
()
;
return
true
;
}
};
# else
...
...
test/format-test.cc
View file @
a03842b0
...
...
@@ -1234,8 +1234,11 @@ namespace fmt {
template
<
>
struct
formatter
<
Date
>
{
template
<
typename
ParseContext
>
auto
parse
(
ParseContext
&
ctx
)
->
decltype
(
ctx
.
begin
())
{
return
ctx
.
begin
();
constexpr
auto
parse
(
ParseContext
&
ctx
)
->
decltype
(
ctx
.
begin
())
{
auto
it
=
ctx
.
begin
();
if
(
*
it
==
'd'
)
++
it
;
return
it
;
}
void
format
(
buffer
&
buf
,
const
Date
&
d
,
context
&
)
{
...
...
@@ -1813,8 +1816,7 @@ TEST(FormatTest, ConstexprParseFormatString) {
static_assert
(
parse_string
(
"{:}"
),
""
);
}
#if FMT_UDL_TEMPLATE
TEST
(
FormatTest
,
UdlTemplate
)
{
EXPECT_EQ
(
"foo"
,
"foo"
_format
());
EXPECT_EQ
(
" 42"
,
"{0:10}"
_format
(
42
));
}
#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