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
01309a34
Commit
01309a34
authored
Jul 04, 2020
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Deprecate arg_formatter
parent
a62d0605
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
19 additions
and
58 deletions
+19
-58
doc/api.rst
doc/api.rst
+0
-44
include/fmt/format.h
include/fmt/format.h
+14
-9
include/fmt/locale.h
include/fmt/locale.h
+1
-1
test/custom-formatter-test.cc
test/custom-formatter-test.cc
+2
-2
test/ostream-test.cc
test/ostream-test.cc
+2
-2
No files found.
doc/api.rst
View file @
01309a34
...
...
@@ -335,50 +335,6 @@ arguments, the container that stores pointers to them will be allocated using
the default allocator. Also floating-point formatting falls back on ``sprintf``
which may do allocations.
Custom Formatting of Built-in Types
-----------------------------------
It is possible to change the way arguments are formatted by providing a
custom argument formatter class::
using arg_formatter = fmt::arg_formatter<fmt::buffer_range<char>>;
// A custom argument formatter that formats negative integers as unsigned
// with the ``x`` format specifier.
class custom_arg_formatter : public arg_formatter {
public:
custom_arg_formatter(fmt::format_context& ctx,
fmt::format_parse_context* parse_ctx = nullptr,
fmt::format_specs* spec = nullptr)
: arg_formatter(ctx, parse_ctx, spec) {}
using arg_formatter::operator();
auto operator()(int value) {
if (specs() && specs()->type == 'x')
return (*this)(static_cast<unsigned>(value)); // convert to unsigned and format
return arg_formatter::operator()(value);
}
};
std::string custom_vformat(fmt::string_view format_str, fmt::format_args args) {
fmt::memory_buffer buffer;
// Pass custom argument formatter as a template arg to vformat_to.
fmt::vformat_to<custom_arg_formatter>(buffer, format_str, args);
return fmt::to_string(buffer);
}
template <typename ...Args>
inline std::string custom_format(
fmt::string_view format_str, const Args&... args) {
return custom_vformat(format_str, fmt::make_format_args(args...));
}
std::string s = custom_format("{:x}", -42); // s == "ffffffd6"
.. doxygenclass:: fmt::arg_formatter
:members:
.. _ranges-api:
Ranges and Tuple Formatting
...
...
include/fmt/format.h
View file @
01309a34
...
...
@@ -566,7 +566,8 @@ template <typename U>
void
buffer
<
T
>::
append
(
const
U
*
begin
,
const
U
*
end
)
{
size_t
new_size
=
size_
+
to_unsigned
(
end
-
begin
);
reserve
(
new_size
);
std
::
uninitialized_copy
(
begin
,
end
,
make_checked
(
ptr_
+
size_
,
capacity_
-
size_
));
std
::
uninitialized_copy
(
begin
,
end
,
make_checked
(
ptr_
+
size_
,
capacity_
-
size_
));
size_
=
new_size
;
}
}
// namespace detail
...
...
@@ -2886,14 +2887,13 @@ FMT_API void format_error_code(buffer<char>& out, int error_code,
FMT_API
void
report_error
(
format_func
func
,
int
error_code
,
string_view
message
)
FMT_NOEXCEPT
;
}
// namespace detail
/** The default argument formatter. */
template
<
typename
OutputIt
,
typename
Char
>
class
arg_formatter
:
public
detail
::
arg_formatter_base
<
OutputIt
,
Char
>
{
class
arg_formatter
:
public
arg_formatter_base
<
OutputIt
,
Char
>
{
private:
using
char_type
=
Char
;
using
base
=
detail
::
arg_formatter_base
<
OutputIt
,
Char
>
;
using
base
=
arg_formatter_base
<
OutputIt
,
Char
>
;
using
context_type
=
basic_format_context
<
OutputIt
,
Char
>
;
context_type
&
ctx_
;
...
...
@@ -2929,6 +2929,11 @@ class arg_formatter : public detail::arg_formatter_base<OutputIt, Char> {
return
ctx_
.
out
();
}
};
}
// namespace detail
template
<
typename
OutputIt
,
typename
Char
>
using
arg_formatter
FMT_DEPRECATED_ALIAS
=
detail
::
arg_formatter
<
OutputIt
,
Char
>
;
/**
An error returned by an operating system or a language runtime,
...
...
@@ -3139,8 +3144,8 @@ struct formatter<T, Char,
specs_
.
width_ref
,
ctx
);
detail
::
handle_dynamic_spec
<
detail
::
precision_checker
>
(
specs_
.
precision
,
specs_
.
precision_ref
,
ctx
);
using
af
=
arg_formatter
<
typename
FormatContext
::
iterator
,
typename
FormatContext
::
char_type
>
;
using
af
=
detail
::
arg_formatter
<
typename
FormatContext
::
iterator
,
typename
FormatContext
::
char_type
>
;
return
visit_format_arg
(
af
(
ctx
,
nullptr
,
&
specs_
),
detail
::
make_arg
<
FormatContext
>
(
val
));
}
...
...
@@ -3235,8 +3240,8 @@ template <typename Char = char> class dynamic_formatter {
}
if
(
specs_
.
alt
)
checker
.
on_hash
();
if
(
specs_
.
precision
>=
0
)
checker
.
end_precision
();
using
af
=
arg_formatter
<
typename
FormatContext
::
iterator
,
typename
FormatContext
::
char_type
>
;
using
af
=
detail
::
arg_formatter
<
typename
FormatContext
::
iterator
,
typename
FormatContext
::
char_type
>
;
visit_format_arg
(
af
(
ctx
,
nullptr
,
&
specs_
),
detail
::
make_arg
<
FormatContext
>
(
val
));
return
ctx
.
out
();
...
...
@@ -3506,7 +3511,7 @@ template <
inline
OutputIt
vformat_to
(
OutputIt
out
,
const
S
&
format_str
,
format_args_t
<
type_identity_t
<
OutputIt
>
,
char_t
<
S
>>
args
)
{
using
af
=
arg_formatter
<
OutputIt
,
char_t
<
S
>>
;
using
af
=
detail
::
arg_formatter
<
OutputIt
,
char_t
<
S
>>
;
return
vformat_to
<
af
>
(
out
,
to_string_view
(
format_str
),
args
);
}
...
...
include/fmt/locale.h
View file @
01309a34
...
...
@@ -56,7 +56,7 @@ template <typename S, typename OutputIt, typename... Args,
inline
OutputIt
vformat_to
(
OutputIt
out
,
const
std
::
locale
&
loc
,
const
S
&
format_str
,
format_args_t
<
type_identity_t
<
OutputIt
>
,
Char
>
args
)
{
using
af
=
arg_formatter
<
OutputIt
,
Char
>
;
using
af
=
detail
::
arg_formatter
<
OutputIt
,
Char
>
;
return
vformat_to
<
af
>
(
out
,
to_string_view
(
format_str
),
args
,
detail
::
locale_ref
(
loc
));
}
...
...
test/custom-formatter-test.cc
View file @
01309a34
...
...
@@ -18,9 +18,9 @@
// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class
custom_arg_formatter
:
public
fmt
::
arg_formatter
<
fmt
::
format_context
::
iterator
,
char
>
{
:
public
fmt
::
detail
::
arg_formatter
<
fmt
::
format_context
::
iterator
,
char
>
{
public:
using
base
=
fmt
::
arg_formatter
<
fmt
::
format_context
::
iterator
,
char
>
;
using
base
=
fmt
::
detail
::
arg_formatter
<
fmt
::
format_context
::
iterator
,
char
>
;
custom_arg_formatter
(
fmt
::
format_context
&
ctx
,
fmt
::
format_parse_context
*
parse_ctx
,
...
...
test/ostream-test.cc
View file @
01309a34
...
...
@@ -65,10 +65,10 @@ TEST(OStreamTest, Enum) {
}
struct
test_arg_formatter
:
fmt
::
arg_formatter
<
fmt
::
format_context
::
iterator
,
char
>
{
:
fmt
::
detail
::
arg_formatter
<
fmt
::
format_context
::
iterator
,
char
>
{
fmt
::
format_parse_context
parse_ctx
;
test_arg_formatter
(
fmt
::
format_context
&
ctx
,
fmt
::
format_specs
&
s
)
:
fmt
::
arg_formatter
<
fmt
::
format_context
::
iterator
,
char
>
(
:
fmt
::
detail
::
arg_formatter
<
fmt
::
format_context
::
iterator
,
char
>
(
ctx
,
&
parse_ctx
,
&
s
),
parse_ctx
(
""
)
{}
};
...
...
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