2014-10-10 15:40:35 +00:00
|
|
|
.. _string-formatting-api:
|
|
|
|
|
|
|
|
*************
|
|
|
|
API Reference
|
|
|
|
*************
|
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
All functions and classes provided by the fmt library reside in namespace
|
|
|
|
``fmt`` and macros have prefix ``FMT_``. For brevity the namespace is usually
|
|
|
|
omitted in examples.
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2015-12-18 15:16:40 +00:00
|
|
|
Format API
|
|
|
|
==========
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2018-02-11 21:43:16 +00:00
|
|
|
The following functions defined in ``fmt/core.h`` use :ref:`format string
|
2018-03-04 06:12:23 +00:00
|
|
|
syntax <syntax>` similar to that of Python's `str.format
|
|
|
|
<http://docs.python.org/3/library/stdtypes.html#str.format>`_.
|
2014-10-10 15:40:35 +00: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 17:43:54 +00:00
|
|
|
*args* is an argument list representing objects to be formatted.
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2016-05-10 14:29:31 +00:00
|
|
|
The `performance of the format API
|
|
|
|
<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 15:40:35 +00:00
|
|
|
.. _format:
|
|
|
|
|
2018-02-11 17:43:54 +00:00
|
|
|
.. doxygenfunction:: format(string_view, const Args&...)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2015-10-12 22:35:22 +00:00
|
|
|
.. doxygenfunction:: operator""_format(const char *, std::size_t)
|
|
|
|
|
2014-10-10 15:40:35 +00:00
|
|
|
.. _print:
|
|
|
|
|
2018-02-11 17:43:54 +00:00
|
|
|
.. doxygenfunction:: print(string_view, const Args&...)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2018-02-11 17:43:54 +00:00
|
|
|
.. doxygenfunction:: print(std::FILE *, string_view, const Args&...)
|
2015-12-18 15:16:40 +00:00
|
|
|
|
2016-04-29 13:40:31 +00:00
|
|
|
Date and time formatting
|
|
|
|
------------------------
|
|
|
|
|
2016-07-20 15:17:33 +00:00
|
|
|
The library supports `strftime
|
|
|
|
<http://en.cppreference.com/w/cpp/chrono/c/strftime>`_-like date and time
|
|
|
|
formatting::
|
2016-04-29 13:40:31 +00: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 14:02:37 +00:00
|
|
|
`strftime <http://en.cppreference.com/w/cpp/chrono/c/strftime>`_.
|
2016-04-29 13:40:31 +00:00
|
|
|
|
2018-01-20 18:07:29 +00:00
|
|
|
Formatting user-defined types
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
To make a user-defined type formattable, specialize the ``formatter<T>`` struct
|
|
|
|
template and implement ``parse`` and ``format`` methods::
|
|
|
|
|
|
|
|
struct MyStruct { double x, y; };
|
|
|
|
|
|
|
|
namespace fmt {
|
|
|
|
template <>
|
|
|
|
struct formatter<MyStruct> {
|
|
|
|
template <typename ParseContext>
|
|
|
|
auto parse(ParseContext &ctx) { return ctx.begin(); }
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
|
|
|
auto format(const MyStruct &s, FormatContext &ctx) {
|
2018-02-11 21:43:16 +00:00
|
|
|
return format_to(ctx.begin(), "[MyStruct: x={:.1f}, y={:.2f}]", s.x, s.y);
|
2018-01-20 18:07:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Then you can pass objects of type ``MyStruct`` to any formatting function::
|
|
|
|
|
|
|
|
MyStruct m = {1, 2};
|
|
|
|
std::string s = fmt::format("m={}", m);
|
|
|
|
// s == "m=[MyStruct: x=1.0, y=2.00]"
|
|
|
|
|
|
|
|
In the example above the ``formatter<MyStruct>::parse`` function ignores the
|
|
|
|
contents of the format string referred to by ``ctx.begin()`` so the object will
|
2018-02-11 21:43:16 +00:00
|
|
|
always be formatted in the same way. See ``formatter<tm>::parse`` in
|
2018-01-20 18:07:29 +00: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 16:09:33 +00: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"
|
|
|
|
|
|
|
|
class Date {
|
|
|
|
int year_, month_, day_;
|
|
|
|
public:
|
2016-07-20 15:17:33 +00:00
|
|
|
Date(int year, int month, int day): year_(year), month_(month), day_(day) {}
|
2016-05-07 16:09:33 +00:00
|
|
|
|
|
|
|
friend std::ostream &operator<<(std::ostream &os, const Date &d) {
|
|
|
|
return os << d.year_ << '-' << d.month_ << '-' << d.day_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string s = fmt::format("The date is {}", Date(2012, 12, 9));
|
|
|
|
// s == "The date is 2012-12-9"
|
|
|
|
|
2018-02-11 21:43:16 +00:00
|
|
|
.. doxygenfunction:: print(std::ostream&, string_view, const Args&...)
|
2016-05-07 16:09:33 +00:00
|
|
|
|
2016-04-20 14:16:52 +00:00
|
|
|
Argument formatters
|
|
|
|
-------------------
|
|
|
|
|
2016-04-29 13:40:31 +00:00
|
|
|
It is possible to change the way arguments are formatted by providing a
|
|
|
|
custom argument formatter class::
|
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
using arg_formatter =
|
|
|
|
fmt::arg_formatter<fmt::back_insert_range<fmt::internal::buffer>>;
|
|
|
|
|
2016-04-29 13:40:31 +00:00
|
|
|
// A custom argument formatter that formats negative integers as unsigned
|
|
|
|
// with the ``x`` format specifier.
|
2018-03-04 06:12:23 +00:00
|
|
|
class custom_arg_formatter : public arg_formatter {
|
2016-10-07 10:22:14 +00:00
|
|
|
public:
|
2018-03-04 06:12:23 +00:00
|
|
|
custom_arg_formatter(context_type &ctx, fmt::format_specs &spec)
|
|
|
|
: arg_formatter(ctx, spec) {}
|
|
|
|
|
|
|
|
using arg_formatter::operator();
|
2016-04-29 13:40:31 +00:00
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
void operator()(int value) {
|
2016-04-29 13:40:31 +00:00
|
|
|
if (spec().type() == 'x')
|
2018-03-04 06:12:23 +00:00
|
|
|
(*this)(static_cast<unsigned>(value)); // convert to unsigned and format
|
2016-04-29 13:40:31 +00:00
|
|
|
else
|
2018-03-04 06:12:23 +00:00
|
|
|
arg_formatter::operator()(value);
|
2016-04-29 13:40:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
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 do_vformat.
|
|
|
|
fmt::do_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_args(args...));
|
2016-04-29 13:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string s = custom_format("{:x}", -42); // s == "ffffffd6"
|
|
|
|
|
2016-04-20 16:11:33 +00:00
|
|
|
.. doxygenclass:: fmt::ArgVisitor
|
|
|
|
:members:
|
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
.. doxygenclass:: fmt::arg_formatter_base
|
2016-04-20 14:16:52 +00:00
|
|
|
:members:
|
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
.. doxygenclass:: fmt::arg_formatter
|
2016-04-20 14:16:52 +00:00
|
|
|
:members:
|
|
|
|
|
2016-07-20 15:09:14 +00:00
|
|
|
Printf formatting
|
|
|
|
-----------------
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2016-07-20 15:09:14 +00:00
|
|
|
The header ``fmt/printf.h`` provides ``printf``-like formatting functionality.
|
2014-10-10 15:40:35 +00:00
|
|
|
The following functions use `printf format string syntax
|
|
|
|
<http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>`_ with
|
2016-07-20 15:09:14 +00: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 15:40:35 +00:00
|
|
|
|
2018-02-11 21:43:16 +00:00
|
|
|
.. doxygenfunction:: printf(string_view, const Args&...)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2018-02-11 21:43:16 +00:00
|
|
|
.. doxygenfunction:: fprintf(std::FILE *, string_view, const Args&...)
|
2015-12-24 15:00:22 +00:00
|
|
|
|
2018-02-11 21:43:16 +00:00
|
|
|
.. doxygenfunction:: fprintf(std::ostream&, string_view, const Args&...)
|
2016-08-03 15:52:05 +00:00
|
|
|
|
2018-02-11 21:43:16 +00:00
|
|
|
.. doxygenfunction:: sprintf(string_view, const Args&...)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
|
|
|
Write API
|
|
|
|
=========
|
|
|
|
|
2016-05-10 14:29:31 +00: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
|
2016-07-20 15:17:33 +00:00
|
|
|
`~fmt::BasicMemoryWriter` which stores its output in a memory buffer and
|
|
|
|
provides direct access to it. It is possible to create custom writers that
|
2016-05-10 14:29:31 +00:00
|
|
|
store output elsewhere by subclassing `~fmt::BasicWriter`.
|
|
|
|
|
2014-10-10 15:40:35 +00:00
|
|
|
.. doxygenclass:: fmt::BasicWriter
|
|
|
|
:members:
|
|
|
|
|
|
|
|
.. doxygenclass:: fmt::BasicMemoryWriter
|
|
|
|
:members:
|
|
|
|
|
2015-03-02 02:10:09 +00:00
|
|
|
.. doxygenclass:: fmt::BasicArrayWriter
|
|
|
|
:members:
|
|
|
|
|
2016-07-14 14:41:00 +00:00
|
|
|
.. doxygenclass:: fmt::BasicStringWriter
|
|
|
|
:members:
|
|
|
|
|
2016-04-11 13:32:24 +00:00
|
|
|
.. doxygenfunction:: bin(int)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2016-04-11 13:32:24 +00:00
|
|
|
.. doxygenfunction:: oct(int)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2016-04-11 13:32:24 +00:00
|
|
|
.. doxygenfunction:: hex(int)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2016-04-11 13:32:24 +00:00
|
|
|
.. doxygenfunction:: hexu(int)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2015-05-20 01:04:32 +00:00
|
|
|
.. doxygenfunction:: pad(int, unsigned, Char)
|
2014-10-10 15:40:35 +00:00
|
|
|
|
|
|
|
Utilities
|
|
|
|
=========
|
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
.. doxygenfunction:: fmt::arg(string_view, const T&)
|
2015-06-10 01:32:59 +00:00
|
|
|
|
2015-10-12 22:35:22 +00:00
|
|
|
.. doxygenfunction:: operator""_a(const char *, std::size_t)
|
|
|
|
|
2015-06-10 01:32:59 +00:00
|
|
|
.. doxygendefine:: FMT_CAPTURE
|
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
.. doxygenclass:: fmt::basic_format_args
|
2014-10-10 15:40:35 +00:00
|
|
|
:members:
|
|
|
|
|
2016-05-19 02:54:52 +00:00
|
|
|
.. doxygenfunction:: fmt::to_string(const T&)
|
|
|
|
|
2018-03-04 06:12:23 +00:00
|
|
|
.. doxygenclass:: fmt::basic_string_view
|
2015-06-26 16:09:23 +00:00
|
|
|
:members:
|
|
|
|
|
2015-03-20 13:42:55 +00:00
|
|
|
.. doxygenclass:: fmt::Buffer
|
2015-03-20 13:46:39 +00:00
|
|
|
:protected-members:
|
2015-03-20 13:42:55 +00:00
|
|
|
:members:
|
|
|
|
|
2016-04-20 14:44:37 +00:00
|
|
|
System errors
|
2014-10-10 15:40:35 +00:00
|
|
|
=============
|
|
|
|
|
2018-02-11 21:43:16 +00:00
|
|
|
.. doxygenclass:: fmt::system_error
|
2014-10-10 15:40:35 +00:00
|
|
|
:members:
|
|
|
|
|
2016-05-12 03:36:22 +00:00
|
|
|
.. doxygenfunction:: fmt::format_system_error
|
|
|
|
|
2018-02-11 21:43:16 +00:00
|
|
|
.. doxygenclass:: fmt::windows_error
|
2014-10-10 15:40:35 +00:00
|
|
|
:members:
|
|
|
|
|
|
|
|
.. _formatstrings:
|
|
|
|
|
|
|
|
Custom allocators
|
|
|
|
=================
|
|
|
|
|
2016-04-27 15:35:59 +00:00
|
|
|
The fmt library supports custom dynamic memory allocators.
|
2014-10-10 15:40:35 +00:00
|
|
|
A custom allocator class can be specified as a template argument to
|
2014-12-18 16:36:53 +00:00
|
|
|
:class:`fmt::BasicMemoryWriter`::
|
2014-10-10 15:40:35 +00:00
|
|
|
|
|
|
|
typedef fmt::BasicMemoryWriter<char, CustomAllocator> CustomMemoryWriter;
|
|
|
|
|
|
|
|
It is also possible to write a formatting function that uses a custom
|
|
|
|
allocator::
|
|
|
|
|
2016-07-20 15:17:33 +00:00
|
|
|
typedef std::basic_string<char, std::char_traits<char>, CustomAllocator>
|
|
|
|
CustomString;
|
2014-10-10 15:40:35 +00:00
|
|
|
|
2015-06-26 16:09:23 +00:00
|
|
|
CustomString format(CustomAllocator alloc, fmt::CStringRef format_str,
|
2014-10-10 15:40:35 +00:00
|
|
|
fmt::ArgList args) {
|
|
|
|
CustomMemoryWriter writer(alloc);
|
|
|
|
writer.write(format_str, args);
|
|
|
|
return CustomString(writer.data(), writer.size(), alloc);
|
|
|
|
}
|
2015-06-26 16:09:23 +00:00
|
|
|
FMT_VARIADIC(CustomString, format, CustomAllocator, fmt::CStringRef)
|