2014-10-10 19:40:35 +04:00
|
|
|
.. _string-formatting-api:
|
|
|
|
|
|
|
|
*************
|
|
|
|
API Reference
|
|
|
|
*************
|
|
|
|
|
2018-03-04 19:13:08 +03:00
|
|
|
All functions and classes provided by the {fmt} library reside in namespace
|
|
|
|
``fmt`` and macros have prefix ``FMT_``.
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2015-12-18 18:16:40 +03:00
|
|
|
Format API
|
|
|
|
==========
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2018-02-12 00:43:16 +03:00
|
|
|
The following functions defined in ``fmt/core.h`` use :ref:`format string
|
2018-03-04 09:12:23 +03:00
|
|
|
syntax <syntax>` similar to that of Python's `str.format
|
|
|
|
<http://docs.python.org/3/library/stdtypes.html#str.format>`_.
|
2014-10-10 19:40:35 +04:00
|
|
|
They take *format_str* and *args* as arguments.
|
|
|
|
|
|
|
|
*format_str* is a format string that contains literal text and replacement
|
|
|
|
fields surrounded by braces ``{}``. The fields are replaced with formatted
|
|
|
|
arguments in the resulting string.
|
|
|
|
|
2018-02-11 20:43:54 +03:00
|
|
|
*args* is an argument list representing objects to be formatted.
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2018-03-04 17:40:43 +03:00
|
|
|
The `performance of the formating functions
|
2016-05-10 17:29:31 +03:00
|
|
|
<https://github.com/fmtlib/fmt/blob/master/README.rst#speed-tests>`_ is close
|
|
|
|
to that of glibc's ``printf`` and better than the performance of IOStreams.
|
|
|
|
|
2014-10-10 19:40:35 +04:00
|
|
|
.. _format:
|
|
|
|
|
2018-02-11 20:43:54 +03:00
|
|
|
.. doxygenfunction:: format(string_view, const Args&...)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2015-10-13 01:35:22 +03:00
|
|
|
.. doxygenfunction:: operator""_format(const char *, std::size_t)
|
|
|
|
|
2014-10-10 19:40:35 +04:00
|
|
|
.. _print:
|
|
|
|
|
2018-02-11 20:43:54 +03:00
|
|
|
.. doxygenfunction:: print(string_view, const Args&...)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2018-02-11 20:43:54 +03:00
|
|
|
.. doxygenfunction:: print(std::FILE *, string_view, const Args&...)
|
2015-12-18 18:16:40 +03:00
|
|
|
|
2016-04-29 16:40:31 +03:00
|
|
|
Date and time formatting
|
|
|
|
------------------------
|
|
|
|
|
2016-07-20 18:17:33 +03:00
|
|
|
The library supports `strftime
|
|
|
|
<http://en.cppreference.com/w/cpp/chrono/c/strftime>`_-like date and time
|
|
|
|
formatting::
|
2016-04-29 16:40:31 +03:00
|
|
|
|
|
|
|
#include "fmt/time.h"
|
|
|
|
|
|
|
|
std::time_t t = std::time(nullptr);
|
|
|
|
// Prints "The date is 2016-04-29." (with the current date)
|
|
|
|
fmt::print("The date is {:%Y-%m-%d}.", *std::localtime(&t));
|
|
|
|
|
|
|
|
The format string syntax is described in the documentation of
|
2016-04-29 17:02:37 +03:00
|
|
|
`strftime <http://en.cppreference.com/w/cpp/chrono/c/strftime>`_.
|
2016-04-29 16:40:31 +03:00
|
|
|
|
2018-01-20 21:07:29 +03:00
|
|
|
Formatting user-defined types
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
To make a user-defined type formattable, specialize the ``formatter<T>`` struct
|
|
|
|
template and implement ``parse`` and ``format`` methods::
|
|
|
|
|
2018-03-04 19:13:08 +03:00
|
|
|
struct point { double x, y; };
|
2018-01-20 21:07:29 +03:00
|
|
|
|
|
|
|
namespace fmt {
|
|
|
|
template <>
|
2018-03-04 19:13:08 +03:00
|
|
|
struct formatter<point> {
|
2018-01-20 21:07:29 +03:00
|
|
|
template <typename ParseContext>
|
2018-03-04 20:16:51 +03:00
|
|
|
constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }
|
2018-01-20 21:07:29 +03:00
|
|
|
|
|
|
|
template <typename FormatContext>
|
2018-03-04 19:13:08 +03:00
|
|
|
auto format(const point &p, FormatContext &ctx) {
|
|
|
|
return format_to(ctx.begin(), "({:.1f}, {:.1f})", p.x, p.y);
|
2018-01-20 21:07:29 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:13:08 +03:00
|
|
|
Then you can pass objects of type ``point`` to any formatting function::
|
2018-01-20 21:07:29 +03:00
|
|
|
|
2018-03-04 19:13:08 +03:00
|
|
|
point p = {1, 2};
|
|
|
|
std::string s = fmt::format("{}", p);
|
|
|
|
// s == "(1.0, 2.0)"
|
2018-01-20 21:07:29 +03:00
|
|
|
|
2018-03-04 19:13:08 +03:00
|
|
|
In the example above the ``formatter<point>::parse`` function ignores the
|
2018-01-20 21:07:29 +03:00
|
|
|
contents of the format string referred to by ``ctx.begin()`` so the object will
|
2018-02-12 00:43:16 +03:00
|
|
|
always be formatted in the same way. See ``formatter<tm>::parse`` in
|
2018-01-20 21:07:29 +03:00
|
|
|
:file:`fmt/time.h` for an advanced example of how to parse the format string and
|
|
|
|
customize the formatted output.
|
|
|
|
|
|
|
|
This section shows how to define a custom format function for a user-defined
|
|
|
|
type. The next section describes how to get ``fmt`` to use a conventional stream
|
|
|
|
output ``operator<<`` when one is defined for a user-defined type.
|
|
|
|
|
2016-05-07 19:09:33 +03:00
|
|
|
``std::ostream`` support
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
The header ``fmt/ostream.h`` provides ``std::ostream`` support including
|
|
|
|
formatting of user-defined types that have overloaded ``operator<<``::
|
|
|
|
|
|
|
|
#include "fmt/ostream.h"
|
|
|
|
|
2018-03-04 20:16:51 +03:00
|
|
|
class date {
|
2016-05-07 19:09:33 +03:00
|
|
|
int year_, month_, day_;
|
|
|
|
public:
|
2018-03-04 20:16:51 +03:00
|
|
|
date(int year, int month, int day): year_(year), month_(month), day_(day) {}
|
2016-05-07 19:09:33 +03:00
|
|
|
|
2018-03-04 20:16:51 +03:00
|
|
|
friend std::ostream &operator<<(std::ostream &os, const date &d) {
|
2016-05-07 19:09:33 +03:00
|
|
|
return os << d.year_ << '-' << d.month_ << '-' << d.day_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-04 20:16:51 +03:00
|
|
|
std::string s = fmt::format("The date is {}", date(2012, 12, 9));
|
2016-05-07 19:09:33 +03:00
|
|
|
// s == "The date is 2012-12-9"
|
|
|
|
|
2018-02-12 00:43:16 +03:00
|
|
|
.. doxygenfunction:: print(std::ostream&, string_view, const Args&...)
|
2016-05-07 19:09:33 +03:00
|
|
|
|
2016-04-20 17:16:52 +03:00
|
|
|
Argument formatters
|
|
|
|
-------------------
|
|
|
|
|
2016-04-29 16:40:31 +03:00
|
|
|
It is possible to change the way arguments are formatted by providing a
|
|
|
|
custom argument formatter class::
|
|
|
|
|
2018-03-04 09:12:23 +03:00
|
|
|
using arg_formatter =
|
|
|
|
fmt::arg_formatter<fmt::back_insert_range<fmt::internal::buffer>>;
|
|
|
|
|
2016-04-29 16:40:31 +03:00
|
|
|
// A custom argument formatter that formats negative integers as unsigned
|
|
|
|
// with the ``x`` format specifier.
|
2018-03-04 09:12:23 +03:00
|
|
|
class custom_arg_formatter : public arg_formatter {
|
2016-10-07 13:22:14 +03:00
|
|
|
public:
|
2018-03-04 17:40:43 +03:00
|
|
|
custom_arg_formatter(fmt::context &ctx, fmt::format_specs &spec)
|
2018-03-04 09:12:23 +03:00
|
|
|
: arg_formatter(ctx, spec) {}
|
|
|
|
|
|
|
|
using arg_formatter::operator();
|
2016-04-29 16:40:31 +03:00
|
|
|
|
2018-03-04 09:12:23 +03:00
|
|
|
void operator()(int value) {
|
2016-04-29 16:40:31 +03:00
|
|
|
if (spec().type() == 'x')
|
2018-03-04 09:12:23 +03:00
|
|
|
(*this)(static_cast<unsigned>(value)); // convert to unsigned and format
|
2016-04-29 16:40:31 +03:00
|
|
|
else
|
2018-03-04 09:12:23 +03:00
|
|
|
arg_formatter::operator()(value);
|
2016-04-29 16:40:31 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-04 09:12:23 +03:00
|
|
|
std::string custom_vformat(fmt::string_view format_str, fmt::format_args args) {
|
|
|
|
fmt::memory_buffer buffer;
|
2018-03-04 17:40:43 +03:00
|
|
|
// Pass custom argument formatter as a template arg to vformat_to.
|
|
|
|
fmt::vformat_to<custom_arg_formatter>(buffer, format_str, args);
|
2018-03-04 09:12:23 +03:00
|
|
|
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_args(args...));
|
2016-04-29 16:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string s = custom_format("{:x}", -42); // s == "ffffffd6"
|
|
|
|
|
2016-04-20 19:11:33 +03:00
|
|
|
.. doxygenclass:: fmt::ArgVisitor
|
|
|
|
:members:
|
|
|
|
|
2018-03-04 09:12:23 +03:00
|
|
|
.. doxygenclass:: fmt::arg_formatter_base
|
2016-04-20 17:16:52 +03:00
|
|
|
:members:
|
|
|
|
|
2018-03-04 09:12:23 +03:00
|
|
|
.. doxygenclass:: fmt::arg_formatter
|
2016-04-20 17:16:52 +03:00
|
|
|
:members:
|
|
|
|
|
2016-07-20 18:09:14 +03:00
|
|
|
Printf formatting
|
|
|
|
-----------------
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2016-07-20 18:09:14 +03:00
|
|
|
The header ``fmt/printf.h`` provides ``printf``-like formatting functionality.
|
2014-10-10 19:40:35 +04:00
|
|
|
The following functions use `printf format string syntax
|
|
|
|
<http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>`_ with
|
2016-07-20 18:09:14 +03:00
|
|
|
the POSIX extension for positional arguments. Unlike their standard
|
|
|
|
counterparts, the ``fmt`` functions are type-safe and throw an exception if an
|
|
|
|
argument type doesn't match its format specification.
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2018-02-12 00:43:16 +03:00
|
|
|
.. doxygenfunction:: printf(string_view, const Args&...)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2018-02-12 00:43:16 +03:00
|
|
|
.. doxygenfunction:: fprintf(std::FILE *, string_view, const Args&...)
|
2015-12-24 18:00:22 +03:00
|
|
|
|
2018-02-12 00:43:16 +03:00
|
|
|
.. doxygenfunction:: fprintf(std::ostream&, string_view, const Args&...)
|
2016-08-03 18:52:05 +03:00
|
|
|
|
2018-02-12 00:43:16 +03:00
|
|
|
.. doxygenfunction:: sprintf(string_view, const Args&...)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
|
|
|
Write API
|
|
|
|
=========
|
|
|
|
|
2016-05-10 17:29:31 +03:00
|
|
|
The write API provides classes for writing formatted data into character
|
|
|
|
streams. It is usually faster than the `format API`_ but, as IOStreams,
|
|
|
|
may result in larger compiled code size. The main writer class is
|
2018-03-04 20:55:17 +03:00
|
|
|
`~fmt::basic_memory_writer` which stores its output in a memory buffer and
|
2016-07-20 18:17:33 +03:00
|
|
|
provides direct access to it. It is possible to create custom writers that
|
2016-05-10 17:29:31 +03:00
|
|
|
store output elsewhere by subclassing `~fmt::BasicWriter`.
|
|
|
|
|
2014-10-10 19:40:35 +04:00
|
|
|
.. doxygenclass:: fmt::BasicWriter
|
|
|
|
:members:
|
|
|
|
|
2018-03-04 20:55:17 +03:00
|
|
|
.. doxygenclass:: fmt::basic_memory_writer
|
2014-10-10 19:40:35 +04:00
|
|
|
:members:
|
|
|
|
|
2015-03-02 05:10:09 +03:00
|
|
|
.. doxygenclass:: fmt::BasicArrayWriter
|
|
|
|
:members:
|
|
|
|
|
2016-07-14 17:41:00 +03:00
|
|
|
.. doxygenclass:: fmt::BasicStringWriter
|
|
|
|
:members:
|
|
|
|
|
2016-04-11 16:32:24 +03:00
|
|
|
.. doxygenfunction:: bin(int)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2016-04-11 16:32:24 +03:00
|
|
|
.. doxygenfunction:: oct(int)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2016-04-11 16:32:24 +03:00
|
|
|
.. doxygenfunction:: hex(int)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2016-04-11 16:32:24 +03:00
|
|
|
.. doxygenfunction:: hexu(int)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2015-05-20 04:04:32 +03:00
|
|
|
.. doxygenfunction:: pad(int, unsigned, Char)
|
2014-10-10 19:40:35 +04:00
|
|
|
|
|
|
|
Utilities
|
|
|
|
=========
|
|
|
|
|
2018-03-04 09:12:23 +03:00
|
|
|
.. doxygenfunction:: fmt::arg(string_view, const T&)
|
2015-06-10 04:32:59 +03:00
|
|
|
|
2015-10-13 01:35:22 +03:00
|
|
|
.. doxygenfunction:: operator""_a(const char *, std::size_t)
|
|
|
|
|
2018-03-04 09:12:23 +03:00
|
|
|
.. doxygenclass:: fmt::basic_format_args
|
2014-10-10 19:40:35 +04:00
|
|
|
:members:
|
|
|
|
|
2016-05-19 05:54:52 +03:00
|
|
|
.. doxygenfunction:: fmt::to_string(const T&)
|
|
|
|
|
2018-03-04 09:12:23 +03:00
|
|
|
.. doxygenclass:: fmt::basic_string_view
|
2015-06-26 19:09:23 +03:00
|
|
|
:members:
|
|
|
|
|
2018-03-04 20:55:17 +03:00
|
|
|
.. doxygenclass:: fmt::basic_memory_buffer
|
2015-03-20 16:46:39 +03:00
|
|
|
:protected-members:
|
2015-03-20 16:42:55 +03:00
|
|
|
:members:
|
|
|
|
|
2016-04-20 17:44:37 +03:00
|
|
|
System errors
|
2014-10-10 19:40:35 +04:00
|
|
|
=============
|
|
|
|
|
2018-02-12 00:43:16 +03:00
|
|
|
.. doxygenclass:: fmt::system_error
|
2014-10-10 19:40:35 +04:00
|
|
|
:members:
|
|
|
|
|
2016-05-12 06:36:22 +03:00
|
|
|
.. doxygenfunction:: fmt::format_system_error
|
|
|
|
|
2018-02-12 00:43:16 +03:00
|
|
|
.. doxygenclass:: fmt::windows_error
|
2014-10-10 19:40:35 +04:00
|
|
|
:members:
|
|
|
|
|
|
|
|
.. _formatstrings:
|
|
|
|
|
|
|
|
Custom allocators
|
|
|
|
=================
|
|
|
|
|
2018-03-04 20:55:17 +03:00
|
|
|
The {fmt} library supports custom dynamic memory allocators.
|
2014-10-10 19:40:35 +04:00
|
|
|
A custom allocator class can be specified as a template argument to
|
2018-03-04 20:55:17 +03:00
|
|
|
:class:`fmt::basic_memory_buffer`::
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2018-03-04 20:55:17 +03:00
|
|
|
using custom_memory_buffer =
|
2018-03-04 21:33:42 +03:00
|
|
|
fmt::basic_memory_buffer<char, fmt::inline_buffer_size, custom_allocator>;
|
2014-10-10 19:40:35 +04:00
|
|
|
|
|
|
|
It is also possible to write a formatting function that uses a custom
|
|
|
|
allocator::
|
|
|
|
|
2018-03-04 20:55:17 +03:00
|
|
|
using custom_string =
|
|
|
|
std::basic_string<char, std::char_traits<char>, custom_allocator>;
|
|
|
|
|
2018-03-04 21:33:42 +03:00
|
|
|
custom_string vformat(custom_allocator alloc, fmt::string_view format_str,
|
|
|
|
fmt::format_args args) {
|
2018-03-04 20:55:17 +03:00
|
|
|
custom_memory_buffer buf(alloc);
|
|
|
|
fmt::vformat_to(buf, format_str, args);
|
|
|
|
return custom_string(buf.data(), buf.size(), alloc);
|
|
|
|
}
|
2014-10-10 19:40:35 +04:00
|
|
|
|
2018-03-04 20:55:17 +03:00
|
|
|
template <typename ...Args>
|
|
|
|
inline custom_string format(custom_allocator alloc,
|
|
|
|
fmt::string_view format_str,
|
|
|
|
const Args & ... args) {
|
|
|
|
return vformat(alloc, format_str, fmt::make_args(args...));
|
2014-10-10 19:40:35 +04:00
|
|
|
}
|