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
72d51e0b
Commit
72d51e0b
authored
Jun 08, 2016
by
Glen Stark
Committed by
Victor Zverovich
Jun 09, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented #335: custom printf support
parent
6ccb5673
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
573 additions
and
472 deletions
+573
-472
fmt/CMakeLists.txt
fmt/CMakeLists.txt
+2
-1
fmt/format.cc
fmt/format.cc
+1
-392
fmt/format.h
fmt/format.h
+4
-78
fmt/ostream.cc
fmt/ostream.cc
+1
-0
fmt/printf.h
fmt/printf.h
+497
-0
test/CMakeLists.txt
test/CMakeLists.txt
+1
-0
test/custom-formatter-test.cc
test/custom-formatter-test.cc
+65
-0
test/format-impl-test.cc
test/format-impl-test.cc
+1
-1
test/printf-test.cc
test/printf-test.cc
+1
-0
No files found.
fmt/CMakeLists.txt
View file @
72d51e0b
# Define the fmt library, its includes and the needed defines.
# format.cc is added to FMT_HEADERS for the header-only configuration.
set
(
FMT_HEADERS format.h format.cc ostream.h ostream.cc string.h time.h
)
set
(
FMT_HEADERS format.h format.cc ostream.h ostream.cc printf.h
string.h time.h
)
if
(
HAVE_OPEN
)
set
(
FMT_HEADERS
${
FMT_HEADERS
}
posix.h
)
set
(
FMT_SOURCES
${
FMT_SOURCES
}
posix.cc
)
...
...
fmt/format.cc
View file @
72d51e0b
This diff is collapsed.
Click to expand it.
fmt/format.h
View file @
72d51e0b
...
...
@@ -1318,8 +1318,8 @@ class RuntimeError : public std::runtime_error {
RuntimeError
()
:
std
::
runtime_error
(
""
)
{}
};
template
<
typename
Char
>
class
PrintfArgFormatter
;
template
<
typename
Impl
,
typename
Char
>
class
Basic
PrintfArgFormatter
;
template
<
typename
Char
>
class
ArgMap
;
...
...
@@ -1937,26 +1937,6 @@ class FormatterBase {
w
<<
BasicStringRef
<
Char
>
(
start
,
internal
::
to_unsigned
(
end
-
start
));
}
};
// A printf formatter.
template
<
typename
Char
>
class
PrintfFormatter
:
private
FormatterBase
{
private:
void
parse_flags
(
FormatSpec
&
spec
,
const
Char
*&
s
);
// Returns the argument with specified index or, if arg_index is equal
// to the maximum unsigned value, the next argument.
Arg
get_arg
(
const
Char
*
s
,
unsigned
arg_index
=
(
std
::
numeric_limits
<
unsigned
>::
max
)());
// Parses argument index, flags and width and returns the argument index.
unsigned
parse_header
(
const
Char
*&
s
,
FormatSpec
&
spec
);
public:
explicit
PrintfFormatter
(
const
ArgList
&
args
)
:
FormatterBase
(
args
)
{}
FMT_API
void
format
(
BasicWriter
<
Char
>
&
writer
,
BasicCStringRef
<
Char
>
format_str
);
};
}
// namespace internal
/**
...
...
@@ -2420,7 +2400,8 @@ class BasicWriter {
template
<
typename
Impl
,
typename
Char_
>
friend
class
internal
::
ArgFormatterBase
;
friend
class
internal
::
PrintfArgFormatter
<
Char
>
;
template
<
typename
Impl
,
typename
Char_
>
friend
class
internal
::
BasicPrintfArgFormatter
;
protected:
/**
...
...
@@ -3187,56 +3168,6 @@ FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args);
*/
FMT_API
void
print
(
CStringRef
format_str
,
ArgList
args
);
template
<
typename
Char
>
void
printf
(
BasicWriter
<
Char
>
&
w
,
BasicCStringRef
<
Char
>
format
,
ArgList
args
)
{
internal
::
PrintfFormatter
<
Char
>
(
args
).
format
(
w
,
format
);
}
/**
\rst
Formats arguments and returns the result as a string.
**Example**::
std::string message = fmt::sprintf("The answer is %d", 42);
\endrst
*/
inline
std
::
string
sprintf
(
CStringRef
format
,
ArgList
args
)
{
MemoryWriter
w
;
printf
(
w
,
format
,
args
);
return
w
.
str
();
}
inline
std
::
wstring
sprintf
(
WCStringRef
format
,
ArgList
args
)
{
WMemoryWriter
w
;
printf
(
w
,
format
,
args
);
return
w
.
str
();
}
/**
\rst
Prints formatted data to the file *f*.
**Example**::
fmt::fprintf(stderr, "Don't %s!", "panic");
\endrst
*/
FMT_API
int
fprintf
(
std
::
FILE
*
f
,
CStringRef
format
,
ArgList
args
);
/**
\rst
Prints formatted data to ``stdout``.
**Example**::
fmt::printf("Elapsed time: %.2f seconds", 1.23);
\endrst
*/
inline
int
printf
(
CStringRef
format
,
ArgList
args
)
{
return
fprintf
(
stdout
,
format
,
args
);
}
/**
Fast integer formatter.
*/
...
...
@@ -3502,12 +3433,7 @@ FMT_VARIADIC(std::string, format, CStringRef)
FMT_VARIADIC_W
(
std
::
wstring
,
format
,
WCStringRef
)
FMT_VARIADIC
(
void
,
print
,
CStringRef
)
FMT_VARIADIC
(
void
,
print
,
std
::
FILE
*
,
CStringRef
)
FMT_VARIADIC
(
void
,
print_colored
,
Color
,
CStringRef
)
FMT_VARIADIC
(
std
::
string
,
sprintf
,
CStringRef
)
FMT_VARIADIC_W
(
std
::
wstring
,
sprintf
,
WCStringRef
)
FMT_VARIADIC
(
int
,
printf
,
CStringRef
)
FMT_VARIADIC
(
int
,
fprintf
,
std
::
FILE
*
,
CStringRef
)
namespace
internal
{
template
<
typename
Char
>
...
...
fmt/ostream.cc
View file @
72d51e0b
...
...
@@ -26,6 +26,7 @@
*/
#include "fmt/ostream.h"
#include "fmt/printf.h"
namespace
fmt
{
...
...
fmt/printf.h
0 → 100644
View file @
72d51e0b
This diff is collapsed.
Click to expand it.
test/CMakeLists.txt
View file @
72d51e0b
...
...
@@ -80,6 +80,7 @@ add_fmt_test(printf-test)
add_fmt_test
(
string-test
)
add_fmt_test
(
util-test mock-allocator.h
)
add_fmt_test
(
macro-test
)
add_fmt_test
(
custom-formatter-test
)
# Enable stricter options for one test to make sure that the header is free of
# warnings.
...
...
test/custom-formatter-test.cc
0 → 100644
View file @
72d51e0b
/*
Custom argument formatter tests
Copyright (c) 2016, Victor Zverovich
All rights reserved.
For the license information refer to format.h.
*/
#include "fmt/printf.h"
#include "gtest-extra.h"
using
fmt
::
internal
::
BasicPrintfArgFormatter
;
// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class
CustomArgFormatter
:
public
fmt
::
BasicArgFormatter
<
CustomArgFormatter
,
char
>
{
public:
CustomArgFormatter
(
fmt
::
BasicFormatter
<
char
,
CustomArgFormatter
>
&
f
,
fmt
::
FormatSpec
&
s
,
const
char
*
fmt
)
:
fmt
::
BasicArgFormatter
<
CustomArgFormatter
,
char
>
(
f
,
s
,
fmt
)
{}
void
visit_double
(
double
value
)
{
if
(
round
(
value
*
pow
(
10
,
spec
().
precision
()))
==
0
)
value
=
0
;
fmt
::
BasicArgFormatter
<
CustomArgFormatter
,
char
>::
visit_double
(
value
);
}
};
// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class
CustomPAF
:
public
BasicPrintfArgFormatter
<
CustomPAF
,
char
>
{
public:
CustomPAF
(
fmt
::
BasicWriter
<
char
>
&
writer
,
fmt
::
FormatSpec
&
spec
)
:
BasicPrintfArgFormatter
<
CustomPAF
,
char
>
(
writer
,
spec
)
{}
void
visit_double
(
double
value
)
{
if
(
round
(
value
*
pow
(
10
,
spec
().
precision
()))
==
0
)
value
=
0
;
BasicPrintfArgFormatter
<
CustomPAF
,
char
>::
visit_double
(
value
);
}
};
std
::
string
custom_format
(
const
char
*
format_str
,
fmt
::
ArgList
args
)
{
fmt
::
MemoryWriter
writer
;
// Pass custom argument formatter as a template arg to BasicFormatter.
fmt
::
BasicFormatter
<
char
,
CustomArgFormatter
>
formatter
(
args
,
writer
);
formatter
.
format
(
format_str
);
return
writer
.
str
();
}
FMT_VARIADIC
(
std
::
string
,
custom_format
,
const
char
*
)
std
::
string
custom_sprintf
(
const
char
*
fstr
,
fmt
::
ArgList
args
){
fmt
::
MemoryWriter
writer
;
fmt
::
internal
::
PrintfFormatter
<
char
,
CustomPAF
>
pfer
(
args
);
pfer
.
format
(
writer
,
fstr
);
return
writer
.
str
();
}
FMT_VARIADIC
(
std
::
string
,
custom_sprintf
,
const
char
*
);
TEST
(
CustomFormatterTest
,
Format
)
{
EXPECT_EQ
(
"0.00"
,
custom_format
(
"{:.2f}"
,
-
.00001
));
EXPECT_EQ
(
"0.00"
,
custom_sprintf
(
"%.2f"
,
-
.00001
));
}
test/format-impl-test.cc
View file @
72d51e0b
...
...
@@ -46,7 +46,7 @@ TEST(FormatTest, ArgConverter) {
Arg
arg
=
Arg
();
arg
.
type
=
Arg
::
LONG_LONG
;
arg
.
long_long_value
=
std
::
numeric_limits
<
fmt
::
LongLong
>::
max
();
fmt
::
ArgConverter
<
fmt
::
LongLong
>
(
arg
,
'd'
).
visit
(
arg
);
fmt
::
internal
::
ArgConverter
<
fmt
::
LongLong
>
(
arg
,
'd'
).
visit
(
arg
);
EXPECT_EQ
(
Arg
::
LONG_LONG
,
arg
.
type
);
}
...
...
test/printf-test.cc
View file @
72d51e0b
...
...
@@ -29,6 +29,7 @@
#include <climits>
#include <cstring>
#include "fmt/printf.h"
#include "fmt/format.h"
#include "gtest-extra.h"
#include "util.h"
...
...
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