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
1a2a333a
Commit
1a2a333a
authored
Nov 23, 2015
by
vitaut
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use format specifiers when formatting null pointers & strings
parent
289885e8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
11 deletions
+20
-11
format.cc
format.cc
+18
-11
test/printf-test.cc
test/printf-test.cc
+2
-0
No files found.
format.cc
View file @
1a2a333a
...
...
@@ -425,12 +425,17 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
BasicWriter
<
Char
>
&
writer
()
{
return
writer_
;
}
FormatSpec
&
spec
()
{
return
spec_
;
}
void
write
_bool
(
bool
value
)
{
void
write
(
bool
value
)
{
const
char
*
str_value
=
value
?
"true"
:
"false"
;
Arg
::
StringValue
<
char
>
str
=
{
str_value
,
strlen
(
str_value
)
};
writer_
.
write_str
(
str
,
spec_
);
}
void
write
(
const
char
*
value
)
{
Arg
::
StringValue
<
char
>
str
=
{
value
,
0
};
writer_
.
write_str
(
str
,
spec_
);
}
public:
BasicArgFormatter
(
BasicWriter
<
Char
>
&
w
,
FormatSpec
&
s
)
:
writer_
(
w
),
spec_
(
s
)
{}
...
...
@@ -444,7 +449,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
void
visit_bool
(
bool
value
)
{
if
(
spec_
.
type_
)
return
visit_any_int
(
value
);
write
_bool
(
value
);
write
(
value
);
}
void
visit_char
(
int
value
)
{
...
...
@@ -479,8 +484,7 @@ class BasicArgFormatter : public ArgVisitor<Impl, void> {
void
visit_cstring
(
const
char
*
value
)
{
if
(
spec_
.
type_
==
'p'
)
return
write_pointer
(
value
);
Arg
::
StringValue
<
char
>
str
=
{
value
,
0
};
writer_
.
write_str
(
str
,
spec_
);
write
(
value
);
}
void
visit_string
(
Arg
::
StringValue
<
char
>
value
)
{
...
...
@@ -522,9 +526,12 @@ class PrintfArgFormatter :
public
BasicArgFormatter
<
PrintfArgFormatter
<
Char
>
,
Char
>
{
void
write_null_pointer
()
{
this
->
writer
()
<<
"(nil)"
;
this
->
spec
().
type_
=
0
;
this
->
write
(
"(nil)"
);
}
typedef
BasicArgFormatter
<
PrintfArgFormatter
<
Char
>
,
Char
>
Base
;
public:
PrintfArgFormatter
(
BasicWriter
<
Char
>
&
w
,
FormatSpec
&
s
)
:
BasicArgFormatter
<
PrintfArgFormatter
<
Char
>
,
Char
>
(
w
,
s
)
{}
...
...
@@ -534,7 +541,7 @@ class PrintfArgFormatter :
if
(
fmt_spec
.
type_
!=
's'
)
return
this
->
visit_any_int
(
value
);
fmt_spec
.
type_
=
0
;
this
->
write
_bool
(
value
);
this
->
write
(
value
);
}
void
visit_char
(
int
value
)
{
...
...
@@ -561,18 +568,18 @@ class PrintfArgFormatter :
void
visit_cstring
(
const
char
*
value
)
{
if
(
value
)
Bas
icArgFormatter
<
PrintfArgFormatter
<
Char
>
,
Char
>
::
visit_cstring
(
value
);
Bas
e
::
visit_cstring
(
value
);
else
if
(
this
->
spec
().
type_
==
'p'
)
write_null_pointer
();
else
this
->
write
r
()
<<
"(null)"
;
this
->
write
(
"(null)"
)
;
}
void
visit_pointer
(
const
void
*
value
)
{
if
(
value
)
BasicArgFormatter
<
PrintfArgFormatter
<
Char
>
,
Char
>
::
visit_pointer
(
value
);
else
write_null_pointer
();
return
Base
::
visit_pointer
(
value
);
this
->
spec
().
type_
=
0
;
write_null_pointer
();
}
void
visit_custom
(
Arg
::
CustomValue
c
)
{
...
...
test/printf-test.cc
View file @
1a2a333a
...
...
@@ -425,6 +425,7 @@ TEST(PrintfTest, String) {
EXPECT_PRINTF
(
"abc"
,
"%s"
,
"abc"
);
const
char
*
null_str
=
0
;
EXPECT_PRINTF
(
"(null)"
,
"%s"
,
null_str
);
EXPECT_PRINTF
(
" (null)"
,
"%10s"
,
null_str
);
// TODO: wide string
}
...
...
@@ -434,6 +435,7 @@ TEST(PrintfTest, Pointer) {
EXPECT_PRINTF
(
fmt
::
format
(
"{}"
,
p
),
"%p"
,
p
);
p
=
0
;
EXPECT_PRINTF
(
"(nil)"
,
"%p"
,
p
);
EXPECT_PRINTF
(
" (nil)"
,
"%10p"
,
p
);
const
char
*
s
=
"test"
;
EXPECT_PRINTF
(
fmt
::
format
(
"{:p}"
,
s
),
"%p"
,
s
);
const
char
*
null_str
=
0
;
...
...
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