blob: 0d01227d4d358bd1231b007eb76fd4845ce07806 [file] [log] [blame]
Victor Zverovichf1cd5412016-04-24 10:46:21 -07001{fmt}
2=====
vitautec27d5d2012-12-07 08:26:46 -08003
Victor Zverovich5a6a5852016-04-27 08:05:40 -07004.. image:: https://travis-ci.org/fmtlib/fmt.png?branch=master
5 :target: https://travis-ci.org/fmtlib/fmt
Victor Zverovich817d98a2014-04-14 23:19:38 -07006
Victor Zverovichf4b5aea2016-04-27 08:13:56 -07007.. image:: https://ci.appveyor.com/api/projects/status/ehjkiefde6gucy1v
8 :target: https://ci.appveyor.com/project/vitaut/fmt
Victor Zverovichd22d11b2019-05-10 09:54:42 -07009
Paul Dreik840a8172019-09-19 06:47:11 +020010.. image:: https://oss-fuzz-build-logs.storage.googleapis.com/badges/libfmt.svg
11 :alt: fmt is continuously fuzzed att oss-fuzz
Paul Dreik63a9f3f2019-11-23 08:33:51 +010012 :target: https://bugs.chromium.org/p/oss-fuzz/issues/list?colspec=ID%20Type%20Component%20Status%20Proj%20Reported%20Owner%20Summary&q=proj%3Dlibfmt&can=1
Paul Dreik840a8172019-09-19 06:47:11 +020013
Victor Zverovich4afb39b2019-11-26 08:33:16 -080014.. image:: https://img.shields.io/badge/stackoverflow-fmt-blue.svg
15 :alt: Ask questions at StackOverflow with the tag fmt
16 :target: http://stackoverflow.com/questions/tagged/fmt
17
Victor Zverovich82222212018-02-10 07:45:32 -080018**{fmt}** is an open-source formatting library for C++.
Victor Zverovich1428b342019-03-16 06:30:50 -070019It can be used as a safe and fast alternative to (s)printf and iostreams.
Victor Zverovich70a23172012-12-07 14:18:37 -080020
Victor Zverovich91bb3aa2019-05-17 15:42:00 -070021`Documentation <https://fmt.dev/latest/>`__
vitauteb502692015-05-22 06:50:46 -070022
Victor Zverovichccc318e2019-05-10 10:04:44 -070023Q&A: ask questions on `StackOverflow with the tag fmt <http://stackoverflow.com/questions/tagged/fmt>`_.
Victor Zverovich4c8efd62019-05-10 09:56:40 -070024
Victor Zverovich70a23172012-12-07 14:18:37 -080025Features
26--------
27
Victor Zverovich91bb3aa2019-05-17 15:42:00 -070028* Replacement-based `format API <https://fmt.dev/dev/api.html>`_ with
Victor Zverovich84e520b2018-03-21 06:16:13 -070029 positional arguments for localization.
Victor Zverovich91bb3aa2019-05-17 15:42:00 -070030* `Format string syntax <https://fmt.dev/dev/syntax.html>`_ similar to the one
Paul Dreikd2c92762019-09-29 08:55:09 +020031 of `str.format <https://docs.python.org/3/library/stdtypes.html#str.format>`_
Victor Zverovich84e520b2018-03-21 06:16:13 -070032 in Python.
Victor Zverovich86a7d852015-02-26 09:05:20 -080033* Safe `printf implementation
Victor Zverovich91bb3aa2019-05-17 15:42:00 -070034 <https://fmt.dev/latest/api.html#printf-formatting>`_ including
Victor Zveroviche613b3c2017-11-10 07:24:16 -080035 the POSIX extension for positional arguments.
Victor Zverovich4fb73d12019-07-21 15:28:48 +020036* Implementation of `C++20 std::format <https://fmt.dev/Text%20Formatting.html>`__.
Victor Zverovich58e53b42012-12-07 14:32:48 -080037* Support for user-defined types.
Victor Zverovichf2301702019-03-10 12:13:14 -070038* High performance: faster than common standard library implementations of
39 `printf <http://en.cppreference.com/w/cpp/io/c/fprintf>`_ and
Victor Zverovich1428b342019-03-16 06:30:50 -070040 iostreams. See `Speed tests`_ and `Fast integer to string conversion in C++
Victor Zverovichd1369d42014-02-13 09:53:01 -080041 <http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html>`_.
Victor Zverovich0ea70de2018-03-21 09:25:28 -070042* Small code size both in terms of source code (the minimum configuration
43 consists of just three header files, ``core.h``, ``format.h`` and
44 ``format-inl.h``) and compiled code. See `Compile time and code bloat`_.
Victor Zverovich7c697bf2012-12-11 16:30:27 -080045* Reliability: the library has an extensive set of `unit tests
Victor Zverovich4b8f8fa2019-09-25 07:14:45 -070046 <https://github.com/fmtlib/fmt/tree/master/test>`_ and is continuously fuzzed.
Victor Zveroviche613b3c2017-11-10 07:24:16 -080047* Safety: the library is fully type safe, errors in format strings can be
48 reported at compile time, automatic memory management prevents buffer overflow
49 errors.
Victor Zverovich7c697bf2012-12-11 16:30:27 -080050* Ease of use: small self-contained code base, no external dependencies,
Victor Zverovich972ffd32019-09-06 13:13:30 -070051 permissive MIT `license
Victor Zveroviche817bb92016-04-27 08:23:03 -070052 <https://github.com/fmtlib/fmt/blob/master/LICENSE.rst>`_
Victor Zverovich91bb3aa2019-05-17 15:42:00 -070053* `Portability <https://fmt.dev/latest/index.html#portability>`_ with
Victor Zveroviche613b3c2017-11-10 07:24:16 -080054 consistent output across platforms and support for older compilers.
Victor Zverovich2d481582013-09-03 09:06:49 -070055* Clean warning-free codebase even on high warning levels
Victor Zveroviche613b3c2017-11-10 07:24:16 -080056 (``-Wall -Wextra -pedantic``).
Victor Zverovichbc6c1c12013-09-04 22:03:37 -070057* Support for wide strings.
Victor Zverovichbc33d172015-02-19 08:40:40 -080058* Optional header-only configuration enabled with the ``FMT_HEADER_ONLY`` macro.
Victor Zverovich70a23172012-12-07 14:18:37 -080059
Victor Zverovich91bb3aa2019-05-17 15:42:00 -070060See the `documentation <https://fmt.dev/latest/>`_ for more details.
Victor Zverovichc7354b62013-01-21 10:11:59 -080061
Victor Zverovich58e53b42012-12-07 14:32:48 -080062Examples
63--------
Victor Zverovich70a23172012-12-07 14:18:37 -080064
Victor Zverovichfdd01492019-03-16 07:27:53 -070065Print ``Hello, world!`` to ``stdout``:
Victor Zverovichf02e4102014-11-07 07:04:03 -080066
67.. code:: c++
Victor Zverovich58e53b42012-12-07 14:32:48 -080068
Victor Zverovich9b392a62019-03-16 07:59:58 -070069 fmt::print("Hello, {}!", "world"); // Python-like format string syntax
70 fmt::printf("Hello, %s!", "world"); // printf format string syntax
Victor Zverovichc50f94a2012-12-07 13:20:50 -080071
Victor Zverovich0fa65cf2019-03-16 07:36:27 -070072Format a string and use positional arguments:
73
74.. code:: c++
75
76 std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy");
Victor Zverovich9b392a62019-03-16 07:59:58 -070077 // s == "I'd rather be happy than right."
Victor Zverovich0fa65cf2019-03-16 07:36:27 -070078
Victor Zverovichfdd01492019-03-16 07:27:53 -070079Check a format string at compile time:
Victor Zverovichdba1ccc2017-11-10 07:38:51 -080080
81.. code:: c++
82
83 // test.cc
Victor Zverovich8fd7e302018-03-09 15:44:46 -080084 #include <fmt/format.h>
Victor Zverovich6793ffc2019-11-20 09:33:59 -080085 std::string s = format(FMT_STRING("{2}"), 42);
Victor Zverovichdba1ccc2017-11-10 07:38:51 -080086
87.. code::
88
Victor Zverovich434eb912018-07-04 18:07:17 -070089 $ c++ -Iinclude -std=c++14 test.cc
Victor Zverovichdba1ccc2017-11-10 07:38:51 -080090 ...
Victor Zverovichfbca8302018-09-12 12:44:04 -070091 test.cc:4:17: note: in instantiation of function template specialization 'fmt::v5::format<S, int>' requested here
Victor Zverovich6793ffc2019-11-20 09:33:59 -080092 std::string s = format(FMT_STRING("{2}"), 42);
Victor Zverovich434eb912018-07-04 18:07:17 -070093 ^
94 include/fmt/core.h:778:19: note: non-constexpr function 'on_error' cannot be used in a constant expression
Victor Zverovich8fd7e302018-03-09 15:44:46 -080095 ErrorHandler::on_error(message);
96 ^
Victor Zverovich434eb912018-07-04 18:07:17 -070097 include/fmt/format.h:2226:16: note: in call to '&checker.context_->on_error(&"argument index out of range"[0])'
Victor Zverovich8fd7e302018-03-09 15:44:46 -080098 context_.on_error("argument index out of range");
99 ^
Victor Zverovichdba1ccc2017-11-10 07:38:51 -0800100
Victor Zverovichfdd01492019-03-16 07:27:53 -0700101Use {fmt} as a safe portable replacement for ``itoa``
Victor Zverovichdf1ba522018-04-22 17:04:28 -0700102(`godbolt <https://godbolt.org/g/NXmpU4>`_):
Victor Zverovich1a236c82013-09-06 15:12:46 -0700103
Victor Zverovichf02e4102014-11-07 07:04:03 -0800104.. code:: c++
Victor Zverovich1a236c82013-09-06 15:12:46 -0700105
Victor Zverovichdf1ba522018-04-22 17:04:28 -0700106 fmt::memory_buffer buf;
107 format_to(buf, "{}", 42); // replaces itoa(42, buffer, 10)
108 format_to(buf, "{:x}", 42); // replaces itoa(42, buffer, 16)
Victor Zverovich9b392a62019-03-16 07:59:58 -0700109 // access the string with to_string(buf) or buf.data()
Victor Zverovich1a236c82013-09-06 15:12:46 -0700110
Victor Zverovichec645ca2019-03-16 10:47:50 -0700111Format objects of user-defined types via a simple `extension API
Victor Zverovich91bb3aa2019-05-17 15:42:00 -0700112<https://fmt.dev/latest/api.html#formatting-user-defined-types>`_:
Victor Zverovicha8a536b2012-12-13 08:12:09 -0800113
Victor Zverovichf02e4102014-11-07 07:04:03 -0800114.. code:: c++
Victor Zverovich8e237302012-12-09 19:19:47 -0800115
Victor Zverovicha7e356c2018-09-14 07:52:30 -0700116 #include "fmt/format.h"
Victor Zverovichc110c6e2016-07-16 08:44:27 -0700117
Victor Zverovicha7e356c2018-09-14 07:52:30 -0700118 struct date {
119 int year, month, day;
120 };
Victor Zverovich8e237302012-12-09 19:19:47 -0800121
Victor Zverovicha7e356c2018-09-14 07:52:30 -0700122 template <>
123 struct fmt::formatter<date> {
Victor Zverovich6793ffc2019-11-20 09:33:59 -0800124 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
Victor Zverovicha7e356c2018-09-14 07:52:30 -0700125
126 template <typename FormatContext>
Victor Zverovich6793ffc2019-11-20 09:33:59 -0800127 auto format(const date& d, FormatContext& ctx) {
Victor Zverovich0c7f5c32018-12-02 08:22:51 -0800128 return format_to(ctx.out(), "{}-{}-{}", d.year, d.month, d.day);
Victor Zverovich8e237302012-12-09 19:19:47 -0800129 }
130 };
131
Victor Zverovicha7e356c2018-09-14 07:52:30 -0700132 std::string s = fmt::format("The date is {}", date{2012, 12, 9});
Victor Zverovich8e237302012-12-09 19:19:47 -0800133 // s == "The date is 2012-12-9"
134
Victor Zverovichfdd01492019-03-16 07:27:53 -0700135Create your own functions similar to `format
Victor Zverovich91bb3aa2019-05-17 15:42:00 -0700136<https://fmt.dev/latest/api.html#format>`_ and
137`print <https://fmt.dev/latest/api.html#print>`_
Victor Zverovichd180c252018-04-15 06:19:38 -0700138which take arbitrary arguments (`godbolt <https://godbolt.org/g/MHjHVf>`_):
Victor Zverovicha8a536b2012-12-13 08:12:09 -0800139
Victor Zverovichf02e4102014-11-07 07:04:03 -0800140.. code:: c++
Victor Zverovich87b5ebf2012-12-11 13:54:53 -0800141
Victor Zverovichf6d481f2014-06-29 14:22:53 -0700142 // Prints formatted error message.
Victor Zverovich6793ffc2019-11-20 09:33:59 -0800143 void vreport_error(const char* format, fmt::format_args args) {
Victor Zverovichb827a972014-07-01 16:59:25 -0700144 fmt::print("Error: ");
Victor Zverovichc69e3082017-10-20 18:00:31 -0700145 fmt::vprint(format, args);
Victor Zverovich87b5ebf2012-12-11 13:54:53 -0800146 }
Victor Zverovichc69e3082017-10-20 18:00:31 -0700147 template <typename... Args>
Victor Zverovich6793ffc2019-11-20 09:33:59 -0800148 void report_error(const char* format, const Args & ... args) {
Victor Zverovich111fa582018-04-08 09:03:37 -0700149 vreport_error(format, fmt::make_format_args(args...));
Victor Zverovichc69e3082017-10-20 18:00:31 -0700150 }
Victor Zverovich87b5ebf2012-12-11 13:54:53 -0800151
Victor Zverovichf6d481f2014-06-29 14:22:53 -0700152 report_error("file not found: {}", path);
Victor Zverovich9d590572014-06-29 11:51:27 -0700153
Victor Zverovichc69e3082017-10-20 18:00:31 -0700154Note that ``vreport_error`` is not parameterized on argument types which can
Victor Zverovich9b392a62019-03-16 07:59:58 -0700155improve compile times and reduce code size compared to a fully parameterized
Victor Zverovichb10ccb82018-12-05 06:42:33 -0800156version.
Victor Zverovich87b5ebf2012-12-11 13:54:53 -0800157
Victor Zverovichfdd01492019-03-16 07:27:53 -0700158Benchmarks
159----------
160
161Speed tests
162~~~~~~~~~~~
163
164================= ============= ===========
165Library Method Run Time, s
166================= ============= ===========
Victor Zverovichf4fcc5f2019-11-20 09:31:11 -0800167libc printf 1.03
168libc++ std::ostream 2.98
169{fmt} 4de41a fmt::print 0.76
170Boost Format 1.67 boost::format 7.24
Victor Zverovichfdd01492019-03-16 07:27:53 -0700171Folly Format folly::format 2.23
172================= ============= ===========
173
Victor Zverovichf4fcc5f2019-11-20 09:31:11 -0800174{fmt} is the fastest of the benchmarked methods, ~35% faster than ``printf``.
Victor Zverovichfdd01492019-03-16 07:27:53 -0700175
176The above results were generated by building ``tinyformat_test.cpp`` on macOS
17710.14.3 with ``clang++ -O3 -DSPEED_TEST -DHAVE_FORMAT``, and taking the best of
178three runs. In the test, the format string ``"%0.10f:%04d:%+g:%s:%p:%c:%%\n"``
179or equivalent is filled 2,000,000 times with output sent to ``/dev/null``; for
180further details refer to the `source
181<https://github.com/fmtlib/format-benchmark/blob/master/tinyformat_test.cpp>`_.
182
Victor Zverovich0a66e4c2019-03-24 11:37:07 -0700183{fmt} is 10x faster than ``std::ostringstream`` and ``sprintf`` on floating-point
184formatting (`dtoa-benchmark <https://github.com/fmtlib/dtoa-benchmark>`_)
185and as fast as `double-conversion <https://github.com/google/double-conversion>`_:
186
Victor Zverovichef7369c2019-11-28 08:09:46 -0800187.. image:: https://user-images.githubusercontent.com/576385/69767160-cdaca400-112f-11ea-9fc5-347c9f83caad.png
Victor Zverovich91bb3aa2019-05-17 15:42:00 -0700188 :target: https://fmt.dev/unknown_mac64_clang10.0.html
Victor Zverovich0a66e4c2019-03-24 11:37:07 -0700189
Victor Zverovichfdd01492019-03-16 07:27:53 -0700190Compile time and code bloat
191~~~~~~~~~~~~~~~~~~~~~~~~~~~
192
193The script `bloat-test.py
194<https://github.com/fmtlib/format-benchmark/blob/master/bloat-test.py>`_
195from `format-benchmark <https://github.com/fmtlib/format-benchmark>`_
196tests compile time and code bloat for nontrivial projects.
197It generates 100 translation units and uses ``printf()`` or its alternative
198five times in each to simulate a medium sized project. The resulting
199executable size and compile time (Apple LLVM version 8.1.0 (clang-802.0.42),
200macOS Sierra, best of three) is shown in the following tables.
201
202**Optimized build (-O3)**
203
204============= =============== ==================== ==================
205Method Compile Time, s Executable size, KiB Stripped size, KiB
206============= =============== ==================== ==================
207printf 2.6 29 26
208printf+string 16.4 29 26
209iostreams 31.1 59 55
210{fmt} 19.0 37 34
Victor Zverovichfdd01492019-03-16 07:27:53 -0700211Boost Format 91.9 226 203
212Folly Format 115.7 101 88
213============= =============== ==================== ==================
214
215As you can see, {fmt} has 60% less overhead in terms of resulting binary code
216size compared to iostreams and comes pretty close to ``printf``. Boost Format
217and Folly Format have the largest overheads.
218
219``printf+string`` is the same as ``printf`` but with extra ``<string>``
220include to measure the overhead of the latter.
221
222**Non-optimized build**
223
224============= =============== ==================== ==================
225Method Compile Time, s Executable size, KiB Stripped size, KiB
226============= =============== ==================== ==================
227printf 2.2 33 30
228printf+string 16.0 33 30
229iostreams 28.3 56 52
230{fmt} 18.2 59 50
Victor Zverovichfdd01492019-03-16 07:27:53 -0700231Boost Format 54.1 365 303
232Folly Format 79.9 445 430
233============= =============== ==================== ==================
234
235``libc``, ``lib(std)c++`` and ``libfmt`` are all linked as shared libraries to
Victor Zverovich6793ffc2019-11-20 09:33:59 -0800236compare formatting function overhead only. Boost Format is a
237header-only library so it doesn't provide any linkage options.
Victor Zverovichfdd01492019-03-16 07:27:53 -0700238
239Running the tests
240~~~~~~~~~~~~~~~~~
241
242Please refer to `Building the library`__ for the instructions on how to build
243the library and run the unit tests.
244
Victor Zverovich91bb3aa2019-05-17 15:42:00 -0700245__ https://fmt.dev/latest/usage.html#building-the-library
Victor Zverovichfdd01492019-03-16 07:27:53 -0700246
247Benchmarks reside in a separate repository,
248`format-benchmarks <https://github.com/fmtlib/format-benchmark>`_,
249so to run the benchmarks you first need to clone this repository and
250generate Makefiles with CMake::
251
252 $ git clone --recursive https://github.com/fmtlib/format-benchmark.git
253 $ cd format-benchmark
254 $ cmake .
255
256Then you can run the speed test::
257
258 $ make speed-test
259
260or the bloat test::
261
262 $ make bloat-test
263
Victor Zverovichaffa7942014-01-30 11:16:27 -0800264Projects using this library
265---------------------------
266
Victor Zverovicha0844952018-11-28 17:28:27 -0800267* `0 A.D. <http://play0ad.com/>`_: A free, open-source, cross-platform real-time
268 strategy game
Victor Zverovich38a74062015-02-06 13:46:19 -0800269
Victor Zverovich73ec6502014-10-12 14:18:17 -0700270* `AMPL/MP <https://github.com/ampl/mp>`_:
271 An open-source library for mathematical programming
Victor Zverovich34cf54c2018-02-24 09:37:17 +0000272
Victor Zverovicha0844952018-11-28 17:28:27 -0800273* `AvioBook <https://www.aviobook.aero/en>`_: A comprehensive aircraft
274 operations suite
Victor Zverovich29352af2018-12-01 08:19:20 -0800275
276* `Celestia <https://celestia.space/>`_: Real-time 3D visualization of space
Victor Zverovichaffa7942014-01-30 11:16:27 -0800277
Victor Zverovicha0844952018-11-28 17:28:27 -0800278* `Ceph <https://ceph.com/>`_: A scalable distributed storage system
279
Victor Zverovich58a8f2f2019-09-06 06:26:09 -0700280* `ccache <https://ccache.dev/>`_: A compiler cache
281
Victor Zverovicha0844952018-11-28 17:28:27 -0800282* `CUAUV <http://cuauv.org/>`_: Cornell University's autonomous underwater
283 vehicle
Victor Zverovich2bf59a92016-08-10 08:29:44 -0700284
vitaut7e94fcb2015-12-03 08:09:53 -0800285* `HarpyWar/pvpgn <https://github.com/pvpgn/pvpgn-server>`_:
Victor Zverovich8dbfd722014-06-28 19:17:13 -0700286 Player vs Player Gaming Network with tweaks
287
vitaut7e94fcb2015-12-03 08:09:53 -0800288* `KBEngine <http://kbengine.org/>`_: An open-source MMOG server engine
Victor Zverovich2938a922014-09-13 08:44:49 -0700289
Victor Zverovich5174b8c2016-03-10 10:52:21 -0800290* `Keypirinha <http://keypirinha.com/>`_: A semantic launcher for Windows
Victor Zverovich090de292016-03-10 10:51:52 -0800291
Victor Zverovicha09f7482018-01-27 17:47:41 -0800292* `Kodi <https://kodi.tv/>`_ (formerly xbmc): Home theater software
293
Victor Zverovich73ec6502014-10-12 14:18:17 -0700294* `Lifeline <https://github.com/peter-clark/lifeline>`_: A 2D game
295
Victor Zverovichc57029c2018-02-01 16:44:27 -0800296* `Drake <http://drake.mit.edu/>`_: A planning, control, and analysis toolbox
297 for nonlinear dynamical systems (MIT)
298
299* `Envoy <https://lyft.github.io/envoy/>`_: C++ L7 proxy and communication bus
300 (Lyft)
301
Victor Zverovicha9f810c2018-02-02 19:17:19 -0800302* `FiveM <https://fivem.net/>`_: a modification framework for GTA V
303
Billy Donahue1763d0e2019-04-12 12:47:21 -0400304* `MongoDB <https://mongodb.com/>`_: Distributed document database
305
Victor Zverovichc57029c2018-02-01 16:44:27 -0800306* `MongoDB Smasher <https://github.com/duckie/mongo_smasher>`_: A small tool to
307 generate randomized datasets
Victor Zverovich6e820842016-03-01 14:28:52 -0800308
Victor Zverovich1a7d0ba2018-02-02 19:10:25 -0800309* `OpenSpace <http://openspaceproject.com/>`_: An open-source astrovisualization
310 framework
311
Victor Zverovich81dc04d2015-02-07 08:31:17 -0800312* `PenUltima Online (POL) <http://www.polserver.com/>`_:
313 An MMO server, compatible with most Ultima Online clients
314
Victor Zverovich1a7d0ba2018-02-02 19:10:25 -0800315* `quasardb <https://www.quasardb.net/>`_: A distributed, high-performance,
316 associative database
Victor Zverovich001054c2015-04-27 07:02:15 -0700317
Victor Zverovich820c03a2014-12-02 06:39:16 -0800318* `readpe <https://bitbucket.org/sys_dev/readpe>`_: Read Portable Executable
319
Victor Zverovicha0844952018-11-28 17:28:27 -0800320* `redis-cerberus <https://github.com/HunanTV/redis-cerberus>`_: A Redis cluster
321 proxy
Victor Zverovich42d8f5b2015-09-16 21:33:51 -0700322
Victor Zverovichb10ccb82018-12-05 06:42:33 -0800323* `rpclib <http://rpclib.net/>`_: A modern C++ msgpack-RPC server and client
324 library
325
vitaut7e94fcb2015-12-03 08:09:53 -0800326* `Saddy <https://github.com/mamontov-cpp/saddy-graphics-engine-2d>`_:
Victor Zverovich38a74062015-02-06 13:46:19 -0800327 Small crossplatform 2D graphic engine
328
Victor Zverovicha23fbbb2014-12-02 12:54:12 -0800329* `Salesforce Analytics Cloud <http://www.salesforce.com/analytics-cloud/overview/>`_:
330 Business intelligence software
331
Victor Zverovicha0844952018-11-28 17:28:27 -0800332* `Scylla <http://www.scylladb.com/>`_: A Cassandra-compatible NoSQL data store
333 that can handle 1 million transactions per second on a single server
Victor Zverovich2bf59a92016-08-10 08:29:44 -0700334
Victor Zverovicha0844952018-11-28 17:28:27 -0800335* `Seastar <http://www.seastar-project.org/>`_: An advanced, open-source C++
336 framework for high-performance server applications on modern hardware
Victor Zverovich2bf59a92016-08-10 08:29:44 -0700337
Victor Zverovich6e3d7d92014-12-08 06:15:31 -0800338* `spdlog <https://github.com/gabime/spdlog>`_: Super fast C++ logging library
339
Victor Zverovicha12f4962015-09-16 21:45:27 -0700340* `Stellar <https://www.stellar.org/>`_: Financial platform
341
vitaut32fbc082015-08-08 18:58:25 -0700342* `Touch Surgery <https://www.touchsurgery.com/>`_: Surgery simulator
343
Victor Zverovicha0844952018-11-28 17:28:27 -0800344* `TrinityCore <https://github.com/TrinityCore/TrinityCore>`_: Open-source
345 MMORPG framework
Victor Zverovichef5972e2015-03-17 19:43:47 -0700346
Victor Zverovich2ed412f2019-09-06 13:03:14 -0700347`More... <https://github.com/search?q=fmtlib&type=Code>`_
Victor Zverovichd930f692015-02-06 14:03:44 -0800348
Victor Zverovich9d590572014-06-29 11:51:27 -0700349If you are aware of other projects using this library, please let me know
Victor Zverovich73ec6502014-10-12 14:18:17 -0700350by `email <mailto:victor.zverovich@gmail.com>`_ or by submitting an
Victor Zveroviche817bb92016-04-27 08:23:03 -0700351`issue <https://github.com/fmtlib/fmt/issues>`_.
Victor Zverovichaffa7942014-01-30 11:16:27 -0800352
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800353Motivation
354----------
355
356So why yet another formatting library?
357
358There are plenty of methods for doing this task, from standard ones like
Victor Zverovich1428b342019-03-16 06:30:50 -0700359the printf family of function and iostreams to Boost Format and FastFormat
Victor Zverovich5e5506f2019-03-12 06:08:18 -0700360libraries. The reason for creating a new library is that every existing
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800361solution that I found either had serious issues or didn't provide
362all the features I needed.
363
Victor Zverovicha7f68dc2019-03-16 06:46:19 -0700364printf
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800365~~~~~~
366
Victor Zverovicha7f68dc2019-03-16 06:46:19 -0700367The good thing about ``printf`` is that it is pretty fast and readily available
Victor Zveroviche1ba41f2014-05-14 19:17:20 -0700368being a part of the C standard library. The main drawback is that it
Victor Zverovichef392742019-03-16 06:47:18 -0700369doesn't support user-defined types. ``printf`` also has safety issues although
Victor Zverovicha7f68dc2019-03-16 06:46:19 -0700370they are somewhat mitigated with `__attribute__ ((format (printf, ...))
Victor Zverovich9cbb25e2014-11-05 07:36:55 -0800371<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ in GCC.
Victor Zverovichcb458292012-12-09 10:09:15 -0800372There is a POSIX extension that adds positional arguments required for
vitaut7e94fcb2015-12-03 08:09:53 -0800373`i18n <https://en.wikipedia.org/wiki/Internationalization_and_localization>`_
Victor Zverovicha7f68dc2019-03-16 06:46:19 -0700374to ``printf`` but it is not a part of C99 and may not be available on some
Victor Zverovichcb458292012-12-09 10:09:15 -0800375platforms.
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800376
Victor Zverovich1428b342019-03-16 06:30:50 -0700377iostreams
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800378~~~~~~~~~
379
Victor Zverovich1428b342019-03-16 06:30:50 -0700380The main issue with iostreams is best illustrated with an example:
Victor Zverovicha8a536b2012-12-13 08:12:09 -0800381
Victor Zverovichf02e4102014-11-07 07:04:03 -0800382.. code:: c++
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800383
384 std::cout << std::setprecision(2) << std::fixed << 1.23456 << "\n";
385
Victor Zverovicha8a536b2012-12-13 08:12:09 -0800386which is a lot of typing compared to printf:
387
Victor Zverovichf02e4102014-11-07 07:04:03 -0800388.. code:: c++
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800389
390 printf("%.2f\n", 1.23456);
391
Victor Zverovich1428b342019-03-16 06:30:50 -0700392Matthew Wilson, the author of FastFormat, called this "chevron hell". iostreams
393don't support positional arguments by design.
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800394
Victor Zverovich1428b342019-03-16 06:30:50 -0700395The good part is that iostreams support user-defined types and are safe although
Victor Zverovicha7f68dc2019-03-16 06:46:19 -0700396error handling is awkward.
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800397
Victor Zverovicha7f68dc2019-03-16 06:46:19 -0700398Boost Format
399~~~~~~~~~~~~
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800400
Victor Zverovicha7f68dc2019-03-16 06:46:19 -0700401This is a very powerful library which supports both ``printf``-like format
402strings and positional arguments. Its main drawback is performance. According to
403various benchmarks it is much slower than other methods considered here. Boost
404Format also has excessive build times and severe code bloat issues (see
405`Benchmarks`_).
Victor Zverovich3e4d8472012-12-07 15:33:27 -0800406
407FastFormat
408~~~~~~~~~~
409
410This is an interesting library which is fast, safe and has positional
411arguments. However it has significant limitations, citing its author:
412
413 Three features that have no hope of being accommodated within the
414 current design are:
415
416 * Leading zeros (or any other non-space padding)
417 * Octal/hexadecimal encoding
418 * Runtime width/alignment specification
419
420It is also quite big and has a heavy dependency, STLSoft, which might be
421too restrictive for using it in some projects.
422
Victor Zverovich3cea8692012-12-24 21:31:05 -0800423Boost Spirit.Karma
424~~~~~~~~~~~~~~~~~~
425
Victor Zverovicha7f68dc2019-03-16 06:46:19 -0700426This is not really a formatting library but I decided to include it here for
427completeness. As iostreams, it suffers from the problem of mixing verbatim text
428with arguments. The library is pretty fast, but slower on integer formatting
429than ``fmt::format_int`` on Karma's own benchmark,
Victor Zverovichd1369d42014-02-13 09:53:01 -0800430see `Fast integer to string conversion in C++
Victor Zverovich9cbb25e2014-11-05 07:36:55 -0800431<http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html>`_.
Victor Zverovich3cea8692012-12-24 21:31:05 -0800432
Victor Zverovich8bbb0b42018-08-15 06:34:34 -0700433FAQ
434---
435
436Q: how can I capture formatting arguments and format them later?
437
438A: use ``std::tuple``:
439
440.. code:: c++
441
442 template <typename... Args>
443 auto capture(const Args&... args) {
444 return std::make_tuple(args...);
445 }
446
447 auto print_message = [](const auto&... args) {
448 fmt::print(args...);
449 };
450
451 // Capture and store arguments:
452 auto args = capture("{} {}", 42, "foo");
453 // Do formatting:
454 std::apply(print_message, args);
455
Victor Zverovichfaccb4c2012-12-12 07:44:41 -0800456License
457-------
458
Victor Zverovich972ffd32019-09-06 13:13:30 -0700459{fmt} is distributed under the MIT `license
Victor Zveroviche817bb92016-04-27 08:23:03 -0700460<https://github.com/fmtlib/fmt/blob/master/LICENSE.rst>`_.
Victor Zveroviche7c7b122013-01-14 06:43:31 -0800461
Victor Zverovich3541e3a2013-01-14 06:47:10 -0800462The `Format String Syntax
Victor Zverovich91bb3aa2019-05-17 15:42:00 -0700463<https://fmt.dev/latest/syntax.html>`_
Victor Zveroviche7c7b122013-01-14 06:43:31 -0800464section in the documentation is based on the one from Python `string module
vitaut7e94fcb2015-12-03 08:09:53 -0800465documentation <https://docs.python.org/3/library/string.html#module-string>`_
Victor Zveroviche7c7b122013-01-14 06:43:31 -0800466adapted for the current library. For this reason the documentation is
467distributed under the Python Software Foundation license available in
vitaut1c72e702015-05-07 06:47:22 -0700468`doc/python-license.txt
Victor Zverovichf9ee5f42016-04-27 08:28:24 -0700469<https://raw.github.com/fmtlib/fmt/master/doc/python-license.txt>`_.
Victor Zverovich4cddbfa2016-04-25 06:36:26 -0700470It only applies if you distribute the documentation of fmt.
Victor Zveroviche7c7b122013-01-14 06:43:31 -0800471
Victor Zverovichc50f94a2012-12-07 13:20:50 -0800472Acknowledgments
473---------------
474
Victor Zverovichfdd01492019-03-16 07:27:53 -0700475The {fmt} library is maintained by Victor Zverovich (`vitaut
Victor Zverovich38b603a2018-01-20 17:12:43 -0800476<https://github.com/vitaut>`_) and Jonathan MΓΌller (`foonathan
477<https://github.com/foonathan>`_) with contributions from many other people.
478See `Contributors <https://github.com/fmtlib/fmt/graphs/contributors>`_ and
479`Releases <https://github.com/fmtlib/fmt/releases>`_ for some of the names.
480Let us know if your contribution is not listed or mentioned incorrectly and
481we'll make it right.
482
Victor Zverovichc50f94a2012-12-07 13:20:50 -0800483The benchmark section of this readme file and the performance tests are taken
Victor Zverovich9cbb25e2014-11-05 07:36:55 -0800484from the excellent `tinyformat <https://github.com/c42f/tinyformat>`_ library
Victor Zverovichcb458292012-12-09 10:09:15 -0800485written by Chris Foster. Boost Format library is acknowledged transitively
486since it had some influence on tinyformat.
487Some ideas used in the implementation are borrowed from `Loki
Victor Zverovich9cbb25e2014-11-05 07:36:55 -0800488<http://loki-lib.sourceforge.net/>`_ SafeFormat and `Diagnostic API
489<http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html>`_ in
490`Clang <http://clang.llvm.org/>`_.
Victor Zveroviche7c7b122013-01-14 06:43:31 -0800491Format string syntax and the documentation are based on Python's `str.format
Jack Andersen05710132019-09-30 16:43:04 -1000492<https://docs.python.org/3/library/stdtypes.html#str.format>`_.
Victor Zverovich9cbb25e2014-11-05 07:36:55 -0800493Thanks `Doug Turnbull <https://github.com/softwaredoug>`_ for his valuable
Victor Zverovich4c304472013-11-14 08:57:19 -0800494comments and contribution to the design of the type-safe API and
Victor Zverovich9cbb25e2014-11-05 07:36:55 -0800495`Gregory Czajkowski <https://github.com/gcflymoto>`_ for implementing binary
496formatting. Thanks `Ruslan Baratov <https://github.com/ruslo>`_ for comprehensive
497`comparison of integer formatting algorithms <https://github.com/ruslo/int-dec-format-tests>`_
498and useful comments regarding performance, `Boris Kaul <https://github.com/localvoid>`_ for
499`C++ counting digits benchmark <https://github.com/localvoid/cxx-benchmark-count-digits>`_.
Victor Zverovicheb6baf02015-03-15 06:39:03 -0700500Thanks to `CarterLi <https://github.com/CarterLi>`_ for contributing various
Victor Zverovich6e820842016-03-01 14:28:52 -0800501improvements to the code.