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
5cf3b6dc
Commit
5cf3b6dc
authored
May 06, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a Print overload that writes to a file.
parent
2e50361a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
4 deletions
+32
-4
format.h
format.h
+15
-2
test/format-test.cc
test/format-test.cc
+17
-2
No files found.
format.h
View file @
5cf3b6dc
...
...
@@ -1062,7 +1062,6 @@ class BasicWriter {
*/
BasicFormatter
<
Char
>
Format
(
StringRef
format
);
// TODO: ArgInfo should be made public for this to be usable
inline
void
VFormat
(
BasicStringRef
<
Char
>
format
,
std
::
size_t
num_args
,
const
ArgInfo
*
args
)
{
FormatParser
().
Format
(
*
this
,
format
,
num_args
,
args
);
...
...
@@ -1619,12 +1618,19 @@ class FileSink {
// Formats a string and prints it to stdout.
// Example:
// Print("Elapsed time: {0:.2f} seconds") << 1.23;
// TODO: wchar overload
inline
Formatter
<
FileSink
>
Print
(
StringRef
format
)
{
Formatter
<
FileSink
>
f
(
format
,
FileSink
(
stdout
));
return
f
;
}
// Formats a string and prints it to a file.
// Example:
// Print(stderr, "Don't {}!") << "panic";
inline
Formatter
<
FileSink
>
Print
(
std
::
FILE
*
file
,
StringRef
format
)
{
Formatter
<
FileSink
>
f
(
format
,
FileSink
(
file
));
return
f
;
}
enum
Color
{
BLACK
,
RED
,
GREEN
,
YELLOW
,
BLUE
,
MAGENTA
,
CYAN
,
WHITE
};
/**
...
...
@@ -1680,6 +1686,13 @@ void Print(StringRef format, const Args & ... args) {
std
::
fwrite
(
w
.
data
(),
1
,
w
.
size
(),
stdout
);
}
template
<
typename
...
Args
>
void
Print
(
std
::
FILE
*
f
,
StringRef
format
,
const
Args
&
...
args
)
{
Writer
w
;
w
.
Format
(
format
,
args
...);
std
::
fwrite
(
w
.
data
(),
1
,
w
.
size
(),
f
);
}
#endif // FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES
/**
...
...
test/format-test.cc
View file @
5cf3b6dc
...
...
@@ -1833,6 +1833,20 @@ TEST(FormatIntTest, FormatDec) {
#if FMT_USE_FILE_DESCRIPTORS
TEST
(
FormatTest
,
Print
)
{
EXPECT_WRITE
(
stdout
,
fmt
::
Print
(
"Don't {}!"
)
<<
"panic"
,
"Don't panic!"
);
EXPECT_WRITE
(
stderr
,
fmt
::
Print
(
stderr
,
"Don't {}!"
)
<<
"panic"
,
"Don't panic!"
);
}
#if FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES
TEST
(
FormatTest
,
PrintVariadic
)
{
EXPECT_WRITE
(
stdout
,
fmt
::
Print
(
"Don't {}!"
,
"panic"
),
"Don't panic!"
);
EXPECT_WRITE
(
stderr
,
fmt
::
Print
(
stderr
,
"Don't {}!"
,
"panic"
),
"Don't panic!"
);
}
#endif // FMT_USE_VARIADIC_TEMPLATES
TEST
(
FormatTest
,
PrintColored
)
{
EXPECT_WRITE
(
stdout
,
fmt
::
PrintColored
(
fmt
::
RED
,
"Hello, {}!
\n
"
)
<<
"world"
,
"
\x1b
[31mHello, world!
\n\x1b
[0m"
);
...
...
@@ -1842,8 +1856,9 @@ TEST(FormatTest, PrintColored) {
#if FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES
TEST
(
FormatTest
,
Variadic
)
{
EXPECT_EQ
(
"Hello, world!1"
,
str
(
Format
(
"Hello, {}!{}"
,
"world"
,
1
)));
EXPECT_EQ
(
L"Hello, world!1"
,
str
(
Format
(
L"Hello, {}!{}"
,
L"world"
,
1
)));
EXPECT_EQ
(
"abc1"
,
str
(
Format
(
"{}c{}"
,
"ab"
,
1
)));
EXPECT_EQ
(
L"abc1"
,
str
(
Format
(
L"{}c{}"
,
L"ab"
,
1
)));
EXPECT_WRITE
(
stdout
,
fmt
::
Print
(
"So {}!"
,
"variadic"
),
"So variadic!"
);
}
#endif // FMT_USE_VARIADIC_TEMPLATES
...
...
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