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
d9db8981
Commit
d9db8981
authored
Apr 28, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor action classes, Action -> Sink, add comments.
parent
d2bf0733
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
197 additions
and
141 deletions
+197
-141
format-test.cc
format-test.cc
+33
-22
format.cc
format.cc
+5
-4
format.h
format.h
+159
-115
No files found.
format-test.cc
View file @
d9db8981
...
...
@@ -1479,32 +1479,43 @@ TEST(StringRefTest, ConvertToString) {
EXPECT_EQ
(
"abc"
,
s
);
}
struct
CountCalls
{
int
&
num_calls
;
TEST
(
FormatterTest
,
Ctor
)
{
fmt
::
Formatter
<>
f1
(
"test"
);
fmt
::
Formatter
<>
f1copy
(
f1
);
fmt
::
Formatter
<>
f2
(
"test"
,
fmt
::
NullSink
());
fmt
::
Formatter
<
fmt
::
NullSink
>
f3
(
"test"
);
fmt
::
Formatter
<
fmt
::
NullSink
,
wchar_t
>
f4
(
L"test"
);
fmt
::
Formatter
<
fmt
::
NullSink
,
wchar_t
>
f4copy
(
f4
);
fmt
::
Formatter
<
fmt
::
NullSink
,
wchar_t
>
f5
(
L"test"
,
fmt
::
NullSink
());
}
// A sink that counts the number of times the output is written to it.
struct
CountingSink
{
int
&
num_writes
;
CountCalls
(
int
&
num_calls
)
:
num_calls
(
num_call
s
)
{}
explicit
CountingSink
(
int
&
num_calls
)
:
num_writes
(
num_write
s
)
{}
void
operator
()(
const
Writer
&
)
const
{
++
num_
call
s
;
++
num_
write
s
;
}
};
TEST
(
FormatterTest
,
Action
)
{
int
num_
call
s
=
0
;
TEST
(
FormatterTest
,
Sink
)
{
int
num_
write
s
=
0
;
{
fmt
::
Formatter
<
Count
Calls
>
af
(
"test"
,
CountCalls
(
num_call
s
));
EXPECT_EQ
(
0
,
num_
call
s
);
fmt
::
Formatter
<
Count
ingSink
>
f
(
"test"
,
CountingSink
(
num_write
s
));
EXPECT_EQ
(
0
,
num_
write
s
);
}
EXPECT_EQ
(
1
,
num_
call
s
);
EXPECT_EQ
(
1
,
num_
write
s
);
}
TEST
(
FormatterTest
,
ActionNotCalled
OnError
)
{
int
num_
call
s
=
0
;
TEST
(
FormatterTest
,
OutputNotWritten
OnError
)
{
int
num_
write
s
=
0
;
{
typedef
fmt
::
Formatter
<
Count
Calls
>
TestFormatter
;
EXPECT_THROW
(
TestFormatter
af
(
"{0"
,
CountCalls
(
num_call
s
)),
FormatError
);
typedef
fmt
::
Formatter
<
Count
ingSink
>
TestFormatter
;
EXPECT_THROW
(
TestFormatter
f
(
"{0"
,
CountingSink
(
num_write
s
)),
FormatError
);
}
EXPECT_EQ
(
0
,
num_
call
s
);
EXPECT_EQ
(
0
,
num_
write
s
);
}
// The test doesn't compile on older compilers which follow C++03 and
...
...
@@ -1641,7 +1652,7 @@ class File {
int
fd
()
const
{
return
fd_
;
}
};
TEST
(
Color
Test
,
PrintColored
)
{
TEST
(
Format
Test
,
PrintColored
)
{
std
::
fflush
(
stdout
);
File
saved_stdio
(
dup
(
1
));
EXPECT_NE
(
-
1
,
saved_stdio
.
fd
());
...
...
@@ -1661,6 +1672,13 @@ TEST(ColorTest, PrintColored) {
#endif
#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
)));
}
#endif // FMT_USE_VARIADIC_TEMPLATES
template
<
typename
T
>
std
::
string
str
(
const
T
&
value
)
{
return
fmt
::
str
(
fmt
::
Format
(
"{0}"
)
<<
value
);
...
...
@@ -1672,13 +1690,6 @@ TEST(StrTest, Convert) {
EXPECT_EQ
(
"2012-12-9"
,
s
);
}
#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
)));
}
#endif // FMT_USE_VARIADIC_TEMPLATES
int
main
(
int
argc
,
char
**
argv
)
{
#ifdef _WIN32
// Disable message boxes on assertion failures.
...
...
format.cc
View file @
d9db8981
...
...
@@ -688,12 +688,13 @@ void fmt::BasicWriter<Char>::FormatParser::Format(
writer
.
buffer_
.
append
(
start
,
s
);
}
void
fmt
::
ColorWriter
::
operator
()(
const
fmt
::
BasicWriter
<
char
>
&
w
)
const
{
void
fmt
::
ANSITerminalSink
::
operator
()(
const
fmt
::
BasicWriter
<
char
>
&
w
)
const
{
char
escape
[]
=
"
\x1b
[30m"
;
escape
[
3
]
=
'0'
+
static_cast
<
char
>
(
color_
);
std
::
fputs
(
escape
,
stdout
);
std
::
fwrite
(
w
.
data
(),
1
,
w
.
size
(),
stdout
);
std
::
fputs
(
RESET_COLOR
,
stdout
);
std
::
fputs
(
escape
,
file_
);
std
::
fwrite
(
w
.
data
(),
1
,
w
.
size
(),
file_
);
std
::
fputs
(
RESET_COLOR
,
file_
);
}
// Explicit instantiations for char.
...
...
format.h
View file @
d9db8981
This diff is collapsed.
Click to expand it.
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