blob: 407d23a83d0bec68a1cbcfa1199f40309cac1ad1 [file] [log] [blame]
Victor Zverovichc0954452018-01-06 09:09:50 -08001// Formatting library for C++
2//
3// Copyright (c) 2012 - 2016, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
Glen Stark72d51e02016-06-08 01:23:32 +02007
8#ifndef FMT_PRINTF_H_
9#define FMT_PRINTF_H_
10
11#include <algorithm> // std::fill_n
12#include <limits> // std::numeric_limits
13
Victor Zverovichf853d942018-01-20 10:28:10 -080014#include "ostream.h"
Glen Stark72d51e02016-06-08 01:23:32 +020015
Victor Zverovich838400d2018-05-12 08:33:51 -070016FMT_BEGIN_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +020017namespace internal {
18
19// Checks if a value fits in int - used to avoid warnings about comparing
20// signed and unsigned integers.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080021template <bool IsSigned> struct int_checker {
22 template <typename T> static bool fits_in_int(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020023 unsigned max = std::numeric_limits<int>::max();
24 return value <= max;
25 }
26 static bool fits_in_int(bool) { return true; }
27};
28
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080029template <> struct int_checker<true> {
30 template <typename T> static bool fits_in_int(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020031 return value >= std::numeric_limits<int>::min() &&
32 value <= std::numeric_limits<int>::max();
33 }
34 static bool fits_in_int(int) { return true; }
35};
36
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080037class printf_precision_handler : public function<int> {
Glen Stark72d51e02016-06-08 01:23:32 +020038 public:
Glen Stark72d51e02016-06-08 01:23:32 +020039 template <typename T>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080040 typename std::enable_if<std::is_integral<T>::value, int>::type operator()(
41 T value) {
Victor Zverovich418659a2018-03-03 14:04:59 -080042 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070043 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +020044 return static_cast<int>(value);
45 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080046
47 template <typename T>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080048 typename std::enable_if<!std::is_integral<T>::value, int>::type operator()(
49 T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -080050 FMT_THROW(format_error("precision is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +020051 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -080052 }
Glen Stark72d51e02016-06-08 01:23:32 +020053};
54
Victor Zveroviche2dfd392016-11-19 09:29:09 -080055// An argument visitor that returns true iff arg is a zero integer.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080056class is_zero_int : public function<bool> {
Glen Stark72d51e02016-06-08 01:23:32 +020057 public:
58 template <typename T>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080059 typename std::enable_if<std::is_integral<T>::value, bool>::type operator()(
60 T value) {
61 return value == 0;
62 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080063
64 template <typename T>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080065 typename std::enable_if<!std::is_integral<T>::value, bool>::type operator()(
66 T) {
67 return false;
68 }
Glen Stark72d51e02016-06-08 01:23:32 +020069};
70
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080071template <typename T> struct make_unsigned_or_bool : std::make_unsigned<T> {};
Victor Zverovich0fbd8462017-09-04 11:41:15 -070072
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080073template <> struct make_unsigned_or_bool<bool> { typedef bool type; };
Victor Zverovich0fbd8462017-09-04 11:41:15 -070074
Victor Zverovichd705d512016-12-29 09:07:39 -080075template <typename T, typename Context>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080076class arg_converter : public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +020077 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080078 typedef typename Context::char_type Char;
79
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080080 basic_format_arg<Context>& arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080081 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020082
Glen Stark72d51e02016-06-08 01:23:32 +020083 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080084 arg_converter(basic_format_arg<Context>& arg, Char type)
85 : arg_(arg), type_(type) {}
Glen Stark72d51e02016-06-08 01:23:32 +020086
Victor Zverovich751ff642016-11-19 08:40:24 -080087 void operator()(bool value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080088 if (type_ != 's') operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020089 }
90
91 template <typename U>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080092 typename std::enable_if<std::is_integral<U>::value>::type operator()(
93 U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020094 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080095 typedef typename std::conditional<std::is_same<T, void>::value, U, T>::type
96 TargetType;
Victor Zverovich5013c152018-02-10 06:52:46 -080097 if (const_check(sizeof(TargetType) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +020098 // Extra casts are used to silence warnings.
99 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800100 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800101 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200102 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700103 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800104 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800105 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200106 }
107 } else {
108 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200109 // glibc's printf doesn't sign extend arguments of smaller types:
110 // std::printf("%lld", -42); // prints "4294967254"
111 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700112 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200113 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800114 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800115 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200116 }
117 }
118 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800119
120 template <typename U>
Victor Zverovich77c892c2017-08-27 08:16:46 -0700121 typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) {
Bruce Mitchener45230532019-03-10 08:06:30 +0700122 // No conversion needed for non-integral types.
Victor Zverovich751ff642016-11-19 08:40:24 -0800123 }
Glen Stark72d51e02016-06-08 01:23:32 +0200124};
125
Victor Zverovich751ff642016-11-19 08:40:24 -0800126// Converts an integer argument to T for printf, if T is an integral type.
127// If T is void, the argument is converted to corresponding signed or unsigned
128// type depending on the type specifier: 'd' and 'i' - signed, other -
129// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800130template <typename T, typename Context, typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800131void convert_arg(basic_format_arg<Context>& arg, Char type) {
Victor Zverovichf5480632018-10-05 07:15:41 -0700132 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800133}
134
Glen Stark72d51e02016-06-08 01:23:32 +0200135// Converts an integer argument to char for printf.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800136template <typename Context> class char_converter : public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200137 private:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800138 basic_format_arg<Context>& arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200139
Glen Stark72d51e02016-06-08 01:23:32 +0200140 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800141 explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200142
143 template <typename T>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800144 typename std::enable_if<std::is_integral<T>::value>::type operator()(
145 T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200146 typedef typename Context::char_type Char;
147 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200148 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800149
150 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800151 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Bruce Mitchener45230532019-03-10 08:06:30 +0700152 // No conversion needed for non-integral types.
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800153 }
Glen Stark72d51e02016-06-08 01:23:32 +0200154};
155
156// Checks if an argument is a valid printf width specifier and sets
157// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000158template <typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800159class printf_width_handler : public function<unsigned> {
Glen Stark72d51e02016-06-08 01:23:32 +0200160 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000161 typedef basic_format_specs<Char> format_specs;
162
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800163 format_specs& spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200164
Glen Stark72d51e02016-06-08 01:23:32 +0200165 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800166 explicit printf_width_handler(format_specs& spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200167
Glen Stark72d51e02016-06-08 01:23:32 +0200168 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800169 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800170 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800171 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200172 UnsignedType width = static_cast<UnsignedType>(value);
173 if (internal::is_negative(value)) {
174 spec_.align_ = ALIGN_LEFT;
175 width = 0 - width;
176 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700177 unsigned int_max = std::numeric_limits<int>::max();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800178 if (width > int_max) FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200179 return static_cast<unsigned>(width);
180 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800181
182 template <typename T>
183 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800184 operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800185 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200186 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800187 }
Glen Stark72d51e02016-06-08 01:23:32 +0200188};
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800189
190template <typename Char, typename Context>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800191void printf(basic_buffer<Char>& buf, basic_string_view<Char> format,
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800192 basic_format_args<Context> args) {
193 Context(std::back_inserter(buf), format, args).format();
194}
Daniela Engertb0cde862019-01-01 11:45:56 +0100195
196template <typename OutputIt, typename Char, typename Context>
197internal::truncating_iterator<OutputIt> printf(
198 internal::truncating_iterator<OutputIt> it, basic_string_view<Char> format,
199 basic_format_args<Context> args) {
200 return Context(it, format, args).format();
201}
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700202} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200203
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800204using internal::printf; // For printing into memory_buffer.
205
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800206template <typename Range> class printf_arg_formatter;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800207
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800208template <typename OutputIt, typename Char,
209 typename ArgFormatter = printf_arg_formatter<
210 back_insert_range<internal::basic_buffer<Char>>>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800211class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800212
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700213/**
214 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800215 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700216 \endrst
217 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800218template <typename Range>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800219class printf_arg_formatter
220 : public internal::function<
221 typename internal::arg_formatter_base<Range>::iterator>,
222 public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200223 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800224 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800225 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800226 typedef internal::arg_formatter_base<Range> base;
Daniela Engertb0cde862019-01-01 11:45:56 +0100227 typedef basic_printf_context<iterator, char_type, printf_arg_formatter>
228 context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800229
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800230 context_type& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800231
Daniela Engert2570f1a2018-04-26 20:32:14 +0200232 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700233 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200234 this->write("(nil)");
235 }
236
Daniela Engert2570f1a2018-04-26 20:32:14 +0200237 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700238 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200239 this->write(L"(nil)");
240 }
241
Glen Stark72d51e02016-06-08 01:23:32 +0200242 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800243 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000244
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700245 /**
246 \rst
247 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500248 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700249 specifier information for standard argument types.
250 \endrst
251 */
Daniela Engertb0cde862019-01-01 11:45:56 +0100252 printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800253 : base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200254
Victor Zveroviche928b672018-07-04 11:36:38 -0700255 template <typename T>
256 typename std::enable_if<std::is_integral<T>::value, iterator>::type
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800257 operator()(T value) {
Victor Zveroviche928b672018-07-04 11:36:38 -0700258 // MSVC2013 fails to compile separate overloads for bool and char_type so
259 // use std::is_same instead.
260 if (std::is_same<T, bool>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800261 format_specs& fmt_spec = *this->spec();
262 if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700263 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700264 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700265 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800266 format_specs& fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700267 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700268 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700269 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700270 fmt_spec.align_ = ALIGN_RIGHT;
271 return base::operator()(value);
272 } else {
273 return base::operator()(value);
274 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000275 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200276 }
277
Victor Zveroviche928b672018-07-04 11:36:38 -0700278 template <typename T>
279 typename std::enable_if<std::is_floating_point<T>::value, iterator>::type
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800280 operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000281 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200282 }
283
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700284 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800285 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200286 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800287 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700288 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200289 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200290 else
291 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000292 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200293 }
294
Daniela Engert2570f1a2018-04-26 20:32:14 +0200295 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800296 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200297 if (value)
298 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700299 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200300 write_null_pointer(char_type());
301 else
302 this->write(L"(null)");
303 return this->out();
304 }
305
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700306 iterator operator()(basic_string_view<char_type> value) {
307 return base::operator()(value);
308 }
309
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800310 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700311
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700312 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800313 iterator operator()(const void* value) {
314 if (value) return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700315 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200316 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000317 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200318 }
319
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700320 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700321 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800322 handle.format(context_.parse_context(), context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000323 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700324 }
325};
326
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800327template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700328 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800329 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
330 return ctx.begin();
331 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700332
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800333 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800334 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700335 internal::format_value(internal::get_container(ctx.out()), value);
336 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200337 }
338};
339
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700340/** This template formats data and writes the output to a writer. */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800341template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800342class basic_printf_context {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700343 public:
344 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800345 typedef Char char_type;
Victor Zveroviche4572e52019-02-10 06:34:47 -0800346 typedef basic_format_arg<basic_printf_context> format_arg;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700347
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800348 template <typename T> struct formatter_type {
349 typedef printf_formatter<T> type;
350 };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700351
Glen Stark72d51e02016-06-08 01:23:32 +0200352 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800353 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700354
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800355 OutputIt out_;
356 basic_format_args<basic_printf_context> args_;
357 basic_parse_context<Char> parse_ctx_;
358
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800359 static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200360
361 // Returns the argument with specified index or, if arg_index is equal
362 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100363 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200364
365 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800366 unsigned parse_header(const Char*& it, const Char* end, format_specs& spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200367
368 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700369 /**
370 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800371 Constructs a ``printf_context`` object. References to the arguments and
372 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700373 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700374 \endrst
375 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800376 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800377 basic_format_args<basic_printf_context> args)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800378 : out_(out), args_(args), parse_ctx_(format_str) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700379
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800380 OutputIt out() { return out_; }
381 void advance_to(OutputIt it) { out_ = it; }
382
383 format_arg arg(unsigned id) const { return args_.get(id); }
384
385 basic_parse_context<Char>& parse_context() { return parse_ctx_; }
386
387 FMT_CONSTEXPR void on_error(const char* message) {
388 parse_ctx_.on_error(message);
389 }
Victor Zverovich9998f662016-11-06 16:11:24 -0800390
Victor Zverovichc0954452018-01-06 09:09:50 -0800391 /** Formats stored arguments and writes the output to the range. */
Daniela Engertb0cde862019-01-01 11:45:56 +0100392 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200393};
394
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800395template <typename OutputIt, typename Char, typename AF>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800396void basic_printf_context<OutputIt, Char, AF>::parse_flags(format_specs& spec,
397 const Char*& it,
398 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100399 for (; it != end; ++it) {
400 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800401 case '-':
402 spec.align_ = ALIGN_LEFT;
403 break;
404 case '+':
405 spec.flags |= SIGN_FLAG | PLUS_FLAG;
406 break;
407 case '0':
408 spec.fill_ = '0';
409 break;
410 case ' ':
411 spec.flags |= SIGN_FLAG;
412 break;
413 case '#':
414 spec.flags |= HASH_FLAG;
415 break;
416 default:
417 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200418 }
419 }
420}
421
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800422template <typename OutputIt, typename Char, typename AF>
423typename basic_printf_context<OutputIt, Char, AF>::format_arg
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800424basic_printf_context<OutputIt, Char, AF>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800425 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800426 arg_index = parse_ctx_.next_arg_id();
427 else
428 parse_ctx_.check_arg_id(--arg_index);
429 return internal::get_arg(*this, arg_index);
Glen Stark72d51e02016-06-08 01:23:32 +0200430}
431
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800432template <typename OutputIt, typename Char, typename AF>
433unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800434 const Char*& it, const Char* end, format_specs& spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200435 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800436 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200437 if (c >= '0' && c <= '9') {
438 // Parse an argument index (if followed by '$') or a width possibly
439 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700440 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100441 unsigned value = parse_nonnegative_int(it, end, eh);
442 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700443 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200444 arg_index = value;
445 } else {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800446 if (c == '0') spec.fill_ = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200447 if (value != 0) {
448 // Nonzero value means that we parsed width and don't need to
449 // parse it or flags again, so return now.
450 spec.width_ = value;
451 return arg_index;
452 }
453 }
454 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100455 parse_flags(spec, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200456 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100457 if (it != end) {
458 if (*it >= '0' && *it <= '9') {
459 internal::error_handler eh;
460 spec.width_ = parse_nonnegative_int(it, end, eh);
461 } else if (*it == '*') {
462 ++it;
463 spec.width_ = visit_format_arg(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800464 internal::printf_width_handler<char_type>(spec), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100465 }
Glen Stark72d51e02016-06-08 01:23:32 +0200466 }
467 return arg_index;
468}
469
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800470template <typename OutputIt, typename Char, typename AF>
Daniela Engertb0cde862019-01-01 11:45:56 +0100471OutputIt basic_printf_context<OutputIt, Char, AF>::format() {
472 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800473 const Char* start = parse_ctx_.begin();
474 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700475 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100476 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800477 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200478 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100479 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100480 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700481 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200482 continue;
483 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100484 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200485
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000486 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200487 spec.align_ = ALIGN_RIGHT;
488
489 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100490 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200491
492 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100493 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700494 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100495 c = it != end ? *it : 0;
496 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700497 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100498 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
499 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700500 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700501 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100502 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800503 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700504 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200505 }
506 }
507
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100508 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700509 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800510 spec.flags = static_cast<uint_least8_t>(
511 spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200512 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800513 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200514 spec.align_ = ALIGN_NUMERIC;
515 else
516 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
517 }
518
519 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100520 c = it != end ? *it++ : 0;
521 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800522 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100523 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200524 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100525 if (t == 'h') {
526 ++it;
527 t = it != end ? *it : 0;
528 convert_arg<signed char>(arg, t);
529 } else {
530 convert_arg<short>(arg, t);
531 }
Glen Stark72d51e02016-06-08 01:23:32 +0200532 break;
533 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100534 if (t == 'l') {
535 ++it;
536 t = it != end ? *it : 0;
537 convert_arg<long long>(arg, t);
538 } else {
539 convert_arg<long>(arg, t);
540 }
Glen Stark72d51e02016-06-08 01:23:32 +0200541 break;
542 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100543 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200544 break;
545 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100546 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200547 break;
548 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100549 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200550 break;
551 case 'L':
552 // printf produces garbage when 'L' is omitted for long double, no
553 // need to do the same.
554 break;
555 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700556 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100557 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200558 }
559
560 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800561 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700562 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800563 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200564 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700565 switch (spec.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800566 case 'i':
567 case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700568 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200569 break;
570 case 'c':
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800571 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
572 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200573 break;
574 }
575 }
576
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700577 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200578
579 // Format argument.
Daniela Engertb0cde862019-01-01 11:45:56 +0100580 visit_format_arg(AF(out, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200581 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100582 return std::copy(start, it, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200583}
Glen Stark72d51e02016-06-08 01:23:32 +0200584
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800585template <typename Buffer> struct basic_printf_context_t {
586 typedef basic_printf_context<std::back_insert_iterator<Buffer>,
587 typename Buffer::value_type>
588 type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800589};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800590
tnovotnye37d6a92018-11-22 22:57:07 +0100591typedef basic_printf_context_t<internal::buffer>::type printf_context;
592typedef basic_printf_context_t<internal::wbuffer>::type wprintf_context;
593
594typedef basic_format_args<printf_context> printf_args;
595typedef basic_format_args<wprintf_context> wprintf_args;
596
Daniela Engertb0cde862019-01-01 11:45:56 +0100597template <typename OutputIt, typename Char = typename OutputIt::value_type>
598struct basic_printf_n_context_t {
599 typedef fmt::internal::truncating_iterator<OutputIt> OutputIter;
600 typedef output_range<OutputIter, Char> Range;
601 typedef basic_printf_context<OutputIter, Char, printf_arg_formatter<Range>>
602 type;
603};
604
tnovotnye37d6a92018-11-22 22:57:07 +0100605/**
606 \rst
607 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800608 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100609 \endrst
610 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800611template <typename... Args>
612inline format_arg_store<printf_context, Args...> make_printf_args(
613 const Args&... args) {
614 return {args...};
615}
tnovotnye37d6a92018-11-22 22:57:07 +0100616
617/**
618 \rst
619 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800620 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100621 \endrst
622 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800623template <typename... Args>
624inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
625 const Args&... args) {
626 return {args...};
627}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800628
Victor Zverovich0a96c032018-10-25 07:20:02 -0700629template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800630inline std::basic_string<Char> vsprintf(
631 const S& format,
632 basic_format_args<
633 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
634 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200635 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700636 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500637 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700638}
639
Daniela Engertb0cde862019-01-01 11:45:56 +0100640template <typename OutputIt, typename S, typename Char = FMT_CHAR(S)>
641inline typename std::enable_if<internal::is_output_iterator<OutputIt>::value,
642 format_to_n_result<OutputIt>>::type
643vsnprintf(
644 OutputIt out, std::size_t n, const S& format,
645 basic_format_args<typename basic_printf_n_context_t<OutputIt, Char>::type>
646 args) {
647 typedef internal::truncating_iterator<OutputIt> It;
648 auto it = printf(It(out, n), to_string_view(format), args);
649 return {it.base(), it.count()};
650}
651
Glen Stark72d51e02016-06-08 01:23:32 +0200652/**
653 \rst
654 Formats arguments and returns the result as a string.
655
656 **Example**::
657
658 std::string message = fmt::sprintf("The answer is %d", 42);
659 \endrst
660*/
Daniela Engertf27defc2018-10-07 14:38:29 +0200661template <typename S, typename... Args>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800662inline FMT_ENABLE_IF_T(internal::is_string<S>::value,
663 std::basic_string<FMT_CHAR(S)>)
664 sprintf(const S& format, const Args&... args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700665 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200666 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100667 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800668 format_arg_store<context, Args...> as{args...};
669 return vsprintf(to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700670}
671
Daniela Engertb0cde862019-01-01 11:45:56 +0100672/**
673 \rst
674 Formats arguments for up to ``n`` characters stored through output iterator
675 ``out``. The function returns the updated iterator and the untruncated amount
676 of characters.
677
678 **Example**::
679 std::vector<char> out;
680
681 typedef fmt::format_to_n_result<
682 std::back_insert_iterator<std::vector<char>>> res;
683 res Res = fmt::snprintf(std::back_inserter(out), 5, "The answer is %d", 42);
684 \endrst
685*/
686template <typename OutputIt, typename S, typename... Args>
687inline FMT_ENABLE_IF_T(internal::is_string<S>::value&&
688 internal::is_output_iterator<OutputIt>::value,
689 format_to_n_result<OutputIt>)
690 snprintf(OutputIt out, std::size_t n, const S& format,
691 const Args&... args) {
692 internal::check_format_string<Args...>(format);
693 typedef FMT_CHAR(S) Char;
694 typedef typename basic_printf_n_context_t<OutputIt, Char>::type context;
695 format_arg_store<context, Args...> as{args...};
696 return vsnprintf(out, n, to_string_view(format),
697 basic_format_args<context>(as));
698}
699
Victor Zverovich0a96c032018-10-25 07:20:02 -0700700template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800701inline int vfprintf(
702 std::FILE* f, const S& format,
703 basic_format_args<
704 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
705 args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200706 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700707 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800708 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800709 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
710 ? -1
711 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800712}
Glen Stark72d51e02016-06-08 01:23:32 +0200713
714/**
715 \rst
716 Prints formatted data to the file *f*.
717
718 **Example**::
719
720 fmt::fprintf(stderr, "Don't %s!", "panic");
721 \endrst
722 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200723template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800724inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800725 fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700726 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200727 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100728 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800729 format_arg_store<context, Args...> as{args...};
730 return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700731}
732
Victor Zverovich0a96c032018-10-25 07:20:02 -0700733template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800734inline int vprintf(
735 const S& format,
736 basic_format_args<
737 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
738 args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700739 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200740}
741
Glen Stark72d51e02016-06-08 01:23:32 +0200742/**
743 \rst
744 Prints formatted data to ``stdout``.
745
746 **Example**::
747
748 fmt::printf("Elapsed time: %.2f seconds", 1.23);
749 \endrst
750 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200751template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800752inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800753 printf(const S& format_str, const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200754 internal::check_format_string<Args...>(format_str);
755 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100756 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800757 format_arg_store<context, Args...> as{args...};
758 return vprintf(to_string_view(format_str), basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200759}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700760
Victor Zverovich0a96c032018-10-25 07:20:02 -0700761template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800762inline int vfprintf(
763 std::basic_ostream<Char>& os, const S& format,
764 basic_format_args<
765 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
766 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200767 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700768 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200769 internal::write(os, buffer);
770 return static_cast<int>(buffer.size());
771}
772
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700773/**
774 \rst
775 Prints formatted data to the stream *os*.
776
777 **Example**::
778
Victor Zverovich40232912018-03-04 09:55:17 -0800779 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700780 \endrst
781 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200782template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800783inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800784 fprintf(std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str,
785 const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200786 internal::check_format_string<Args...>(format_str);
787 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100788 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800789 format_arg_store<context, Args...> as{args...};
Daniela Engert2c81c852018-10-08 20:14:39 +0200790 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200791 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200792}
Victor Zverovich838400d2018-05-12 08:33:51 -0700793FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200794
795#endif // FMT_PRINTF_H_