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
271fa8c9
Commit
271fa8c9
authored
Aug 29, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve handling of format strings in custom arguments.
parent
3947a7a9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
27 deletions
+17
-27
format.cc
format.cc
+8
-19
format.h
format.h
+7
-7
test/util-test.cc
test/util-test.cc
+2
-1
No files found.
format.cc
View file @
271fa8c9
...
...
@@ -175,19 +175,6 @@ int parse_nonnegative_int(const Char *&s) {
return
value
;
}
template
<
typename
Char
>
const
Char
*
find_closing_brace
(
const
Char
*
s
,
int
num_open_braces
=
1
)
{
for
(
int
n
=
num_open_braces
;
*
s
;
++
s
)
{
if
(
*
s
==
'{'
)
{
++
n
;
}
else
if
(
*
s
==
'}'
)
{
if
(
--
n
==
0
)
return
s
;
}
}
throw
fmt
::
FormatError
(
"unmatched '{' in format"
);
}
template
<
typename
Char
>
void
check_sign
(
const
Char
*&
s
,
const
Arg
&
arg
)
{
char
sign
=
static_cast
<
char
>
(
*
s
);
...
...
@@ -574,7 +561,7 @@ class fmt::internal::ArgFormatter :
}
void
visit_custom
(
Arg
::
CustomValue
c
)
{
c
.
format
(
&
formatter_
,
c
.
value
,
format_
);
c
.
format
(
&
formatter_
,
c
.
value
,
&
format_
);
}
};
...
...
@@ -1019,11 +1006,13 @@ void fmt::internal::PrintfFormatter<Char>::format(
spec
.
type_
=
'x'
;
writer
.
write_int
(
reinterpret_cast
<
uintptr_t
>
(
arg
.
pointer_value
),
spec
);
break
;
case
Arg
:
:
CUSTOM
:
case
Arg
:
:
CUSTOM
:
{
if
(
spec
.
type_
)
internal
::
report_unknown_type
(
spec
.
type_
,
"object"
);
arg
.
custom
.
format
(
&
writer
,
arg
.
custom
.
value
,
"s"
);
const
void
*
s
=
"s"
;
arg
.
custom
.
format
(
&
writer
,
arg
.
custom
.
value
,
&
s
);
break
;
}
default:
assert
(
false
);
break
;
...
...
@@ -1034,14 +1023,14 @@ void fmt::internal::PrintfFormatter<Char>::format(
template
<
typename
Char
>
const
Char
*
fmt
::
BasicFormatter
<
Char
>::
format
(
const
Char
*
format_str
,
const
Arg
&
arg
)
{
const
Char
*
&
format_str
,
const
Arg
&
arg
)
{
const
Char
*
s
=
format_str
;
const
char
*
error
=
0
;
FormatSpec
spec
;
if
(
*
s
==
':'
)
{
if
(
arg
.
type
==
Arg
::
CUSTOM
)
{
arg
.
custom
.
format
(
this
,
arg
.
custom
.
value
,
s
);
return
find_closing_brace
(
s
)
+
1
;
arg
.
custom
.
format
(
this
,
arg
.
custom
.
value
,
&
s
);
return
s
;
}
++
s
;
// Parse fill and alignment.
...
...
format.h
View file @
271fa8c9
...
...
@@ -132,7 +132,7 @@ template <typename Char>
class
BasicFormatter
;
template
<
typename
Char
,
typename
T
>
void
format
(
BasicFormatter
<
Char
>
&
f
,
const
Char
*
format_str
,
const
T
&
value
);
void
format
(
BasicFormatter
<
Char
>
&
f
,
const
Char
*
&
format_str
,
const
T
&
value
);
/**
\rst
...
...
@@ -583,7 +583,7 @@ struct Arg {
};
typedef
void
(
*
FormatFunc
)(
void
*
formatter
,
const
void
*
arg
,
const
void
*
format_s
tr
);
void
*
formatter
,
const
void
*
arg
,
void
*
format_str_p
tr
);
struct
CustomValue
{
const
void
*
value
;
...
...
@@ -634,9 +634,9 @@ class MakeArg : public Arg {
// Formats an argument of a custom type, such as a user-defined class.
template
<
typename
T
>
static
void
format_custom_arg
(
void
*
formatter
,
const
void
*
arg
,
const
void
*
format_s
tr
)
{
void
*
formatter
,
const
void
*
arg
,
void
*
format_str_p
tr
)
{
format
(
*
static_cast
<
BasicFormatter
<
Char
>*>
(
formatter
),
static_cast
<
const
Char
*>
(
format_s
tr
),
*
static_cast
<
const
T
*>
(
arg
));
*
static_cast
<
const
Char
**>
(
format_str_p
tr
),
*
static_cast
<
const
T
*>
(
arg
));
}
public:
...
...
@@ -896,7 +896,7 @@ public:
void
format
(
BasicStringRef
<
Char
>
format_str
,
const
ArgList
&
args
);
const
Char
*
format
(
const
Char
*
format_str
,
const
internal
::
Arg
&
arg
);
const
Char
*
format
(
const
Char
*
&
format_str
,
const
internal
::
Arg
&
arg
);
};
enum
Alignment
{
...
...
@@ -1681,10 +1681,10 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
// Formats a value.
template
<
typename
Char
,
typename
T
>
void
format
(
BasicFormatter
<
Char
>
&
f
,
const
Char
*
format_str
,
const
T
&
value
)
{
void
format
(
BasicFormatter
<
Char
>
&
f
,
const
Char
*
&
format_str
,
const
T
&
value
)
{
std
::
basic_ostringstream
<
Char
>
os
;
os
<<
value
;
f
.
format
(
format_str
,
internal
::
MakeArg
<
Char
>
(
os
.
str
()));
f
ormat_str
=
f
.
format
(
format_str
,
internal
::
MakeArg
<
Char
>
(
os
.
str
()));
}
// Reports a system error without throwing an exception.
...
...
test/util-test.cc
View file @
271fa8c9
...
...
@@ -238,7 +238,8 @@ TEST(ArgTest, MakeArg) {
EXPECT_EQ
(
&
t
,
arg
.
custom
.
value
);
fmt
::
Writer
w
;
fmt
::
BasicFormatter
<
char
>
formatter
(
w
);
arg
.
custom
.
format
(
&
formatter
,
&
t
,
"}"
);
const
char
*
s
=
"}"
;
arg
.
custom
.
format
(
&
formatter
,
&
t
,
&
s
);
EXPECT_EQ
(
"test"
,
w
.
str
());
}
...
...
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