blob: 7894d7f36a548bfa2ba6e423ccc3b5c247c9170e [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) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800122 // No coversion needed for non-integral types.
123 }
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) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800152 // No coversion needed for non-integral types.
153 }
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}
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700195} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200196
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800197using internal::printf; // For printing into memory_buffer.
198
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800199template <typename Range> class printf_arg_formatter;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800200
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800201template <typename OutputIt, typename Char,
202 typename ArgFormatter = printf_arg_formatter<
203 back_insert_range<internal::basic_buffer<Char>>>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800204class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800205
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700206/**
207 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800208 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700209 \endrst
210 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800211template <typename Range>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800212class printf_arg_formatter
213 : public internal::function<
214 typename internal::arg_formatter_base<Range>::iterator>,
215 public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200216 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800217 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800218 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800219 typedef internal::arg_formatter_base<Range> base;
220 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800221
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800222 context_type& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800223
Daniela Engert2570f1a2018-04-26 20:32:14 +0200224 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700225 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200226 this->write("(nil)");
227 }
228
Daniela Engert2570f1a2018-04-26 20:32:14 +0200229 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700230 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200231 this->write(L"(nil)");
232 }
233
Glen Stark72d51e02016-06-08 01:23:32 +0200234 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800235 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000236
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700237 /**
238 \rst
239 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500240 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700241 specifier information for standard argument types.
242 \endrst
243 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800244 printf_arg_formatter(internal::basic_buffer<char_type>& buffer,
245 format_specs& spec, context_type& ctx)
246 : base(back_insert_range<internal::basic_buffer<char_type>>(buffer),
247 &spec, ctx.locale()),
248 context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200249
Victor Zveroviche928b672018-07-04 11:36:38 -0700250 template <typename T>
251 typename std::enable_if<std::is_integral<T>::value, iterator>::type
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800252 operator()(T value) {
Victor Zveroviche928b672018-07-04 11:36:38 -0700253 // MSVC2013 fails to compile separate overloads for bool and char_type so
254 // use std::is_same instead.
255 if (std::is_same<T, bool>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800256 format_specs& fmt_spec = *this->spec();
257 if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700258 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700259 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700260 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800261 format_specs& fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700262 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700263 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700264 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700265 fmt_spec.align_ = ALIGN_RIGHT;
266 return base::operator()(value);
267 } else {
268 return base::operator()(value);
269 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000270 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200271 }
272
Victor Zveroviche928b672018-07-04 11:36:38 -0700273 template <typename T>
274 typename std::enable_if<std::is_floating_point<T>::value, iterator>::type
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800275 operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000276 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200277 }
278
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700279 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800280 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200281 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800282 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700283 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200284 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200285 else
286 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000287 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200288 }
289
Daniela Engert2570f1a2018-04-26 20:32:14 +0200290 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800291 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200292 if (value)
293 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700294 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200295 write_null_pointer(char_type());
296 else
297 this->write(L"(null)");
298 return this->out();
299 }
300
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700301 iterator operator()(basic_string_view<char_type> value) {
302 return base::operator()(value);
303 }
304
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800305 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700306
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700307 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800308 iterator operator()(const void* value) {
309 if (value) return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700310 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200311 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000312 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200313 }
314
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700315 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700316 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800317 handle.format(context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000318 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700319 }
320};
321
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800322template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700323 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800324 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
325 return ctx.begin();
326 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700327
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800328 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800329 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700330 internal::format_value(internal::get_container(ctx.out()), value);
331 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200332 }
333};
334
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700335/** This template formats data and writes the output to a writer. */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800336template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800337class basic_printf_context :
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800338 // Inherit publicly as a workaround for the icc bug
339 // https://software.intel.com/en-us/forums/intel-c-compiler/topic/783476.
340 public internal::context_base<
341 OutputIt, basic_printf_context<OutputIt, Char, ArgFormatter>, Char> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700342 public:
343 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800344 typedef Char char_type;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700345
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800346 template <typename T> struct formatter_type {
347 typedef printf_formatter<T> type;
348 };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700349
Glen Stark72d51e02016-06-08 01:23:32 +0200350 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800351 typedef internal::context_base<OutputIt, basic_printf_context, Char> base;
352 typedef typename base::format_arg format_arg;
353 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700354
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800355 static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200356
357 // Returns the argument with specified index or, if arg_index is equal
358 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100359 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200360
361 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800362 unsigned parse_header(const Char*& it, const Char* end, format_specs& spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200363
364 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700365 /**
366 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800367 Constructs a ``printf_context`` object. References to the arguments and
368 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700369 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700370 \endrst
371 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800372 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800373 basic_format_args<basic_printf_context> args)
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800374 : base(out, format_str, args) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700375
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800376 using base::advance_to;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800377 using base::out;
378 using base::parse_context;
Victor Zverovich9998f662016-11-06 16:11:24 -0800379
Victor Zverovichc0954452018-01-06 09:09:50 -0800380 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich08dff372018-01-28 20:50:43 -0800381 void format();
Glen Stark72d51e02016-06-08 01:23:32 +0200382};
383
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800384template <typename OutputIt, typename Char, typename AF>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800385void basic_printf_context<OutputIt, Char, AF>::parse_flags(format_specs& spec,
386 const Char*& it,
387 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100388 for (; it != end; ++it) {
389 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800390 case '-':
391 spec.align_ = ALIGN_LEFT;
392 break;
393 case '+':
394 spec.flags |= SIGN_FLAG | PLUS_FLAG;
395 break;
396 case '0':
397 spec.fill_ = '0';
398 break;
399 case ' ':
400 spec.flags |= SIGN_FLAG;
401 break;
402 case '#':
403 spec.flags |= HASH_FLAG;
404 break;
405 default:
406 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200407 }
408 }
409}
410
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800411template <typename OutputIt, typename Char, typename AF>
412typename basic_printf_context<OutputIt, Char, AF>::format_arg
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800413basic_printf_context<OutputIt, Char, AF>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800414 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich67928ea2018-01-14 09:27:40 -0800415 return this->do_get_arg(this->parse_context().next_arg_id());
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800416 return base::get_arg(arg_index - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200417}
418
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800419template <typename OutputIt, typename Char, typename AF>
420unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800421 const Char*& it, const Char* end, format_specs& spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200422 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800423 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200424 if (c >= '0' && c <= '9') {
425 // Parse an argument index (if followed by '$') or a width possibly
426 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700427 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100428 unsigned value = parse_nonnegative_int(it, end, eh);
429 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700430 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200431 arg_index = value;
432 } else {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800433 if (c == '0') spec.fill_ = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200434 if (value != 0) {
435 // Nonzero value means that we parsed width and don't need to
436 // parse it or flags again, so return now.
437 spec.width_ = value;
438 return arg_index;
439 }
440 }
441 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100442 parse_flags(spec, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200443 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100444 if (it != end) {
445 if (*it >= '0' && *it <= '9') {
446 internal::error_handler eh;
447 spec.width_ = parse_nonnegative_int(it, end, eh);
448 } else if (*it == '*') {
449 ++it;
450 spec.width_ = visit_format_arg(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800451 internal::printf_width_handler<char_type>(spec), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100452 }
Glen Stark72d51e02016-06-08 01:23:32 +0200453 }
454 return arg_index;
455}
456
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800457template <typename OutputIt, typename Char, typename AF>
458void basic_printf_context<OutputIt, Char, AF>::format() {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800459 auto& buffer = internal::get_container(this->out());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100460 const auto range = this->parse_context();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800461 const Char* end = range.end();
462 const Char* start = range.begin();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700463 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100464 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800465 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200466 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100467 if (it != end && *it == c) {
468 buffer.append(start, it);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700469 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200470 continue;
471 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100472 buffer.append(start, it - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200473
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000474 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200475 spec.align_ = ALIGN_RIGHT;
476
477 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100478 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200479
480 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100481 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700482 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100483 c = it != end ? *it : 0;
484 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700485 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100486 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
487 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700488 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700489 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100490 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800491 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700492 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200493 }
494 }
495
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100496 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700497 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800498 spec.flags = static_cast<uint_least8_t>(
499 spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200500 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800501 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200502 spec.align_ = ALIGN_NUMERIC;
503 else
504 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
505 }
506
507 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100508 c = it != end ? *it++ : 0;
509 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800510 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100511 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200512 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100513 if (t == 'h') {
514 ++it;
515 t = it != end ? *it : 0;
516 convert_arg<signed char>(arg, t);
517 } else {
518 convert_arg<short>(arg, t);
519 }
Glen Stark72d51e02016-06-08 01:23:32 +0200520 break;
521 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100522 if (t == 'l') {
523 ++it;
524 t = it != end ? *it : 0;
525 convert_arg<long long>(arg, t);
526 } else {
527 convert_arg<long>(arg, t);
528 }
Glen Stark72d51e02016-06-08 01:23:32 +0200529 break;
530 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100531 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200532 break;
533 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100534 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200535 break;
536 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100537 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200538 break;
539 case 'L':
540 // printf produces garbage when 'L' is omitted for long double, no
541 // need to do the same.
542 break;
543 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700544 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100545 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200546 }
547
548 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800549 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700550 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800551 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200552 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700553 switch (spec.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800554 case 'i':
555 case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700556 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200557 break;
558 case 'c':
Daniela Engert2570f1a2018-04-26 20:32:14 +0200559 // TODO: handle wchar_t better?
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800560 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
561 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200562 break;
563 }
564 }
565
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700566 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200567
568 // Format argument.
Victor Zverovichf5480632018-10-05 07:15:41 -0700569 visit_format_arg(AF(buffer, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200570 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100571 buffer.append(start, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200572}
Glen Stark72d51e02016-06-08 01:23:32 +0200573
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800574template <typename Buffer> struct basic_printf_context_t {
575 typedef basic_printf_context<std::back_insert_iterator<Buffer>,
576 typename Buffer::value_type>
577 type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800578};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800579
tnovotnye37d6a92018-11-22 22:57:07 +0100580typedef basic_printf_context_t<internal::buffer>::type printf_context;
581typedef basic_printf_context_t<internal::wbuffer>::type wprintf_context;
582
583typedef basic_format_args<printf_context> printf_args;
584typedef basic_format_args<wprintf_context> wprintf_args;
585
586/**
587 \rst
588 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800589 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100590 \endrst
591 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800592template <typename... Args>
593inline format_arg_store<printf_context, Args...> make_printf_args(
594 const Args&... args) {
595 return {args...};
596}
tnovotnye37d6a92018-11-22 22:57:07 +0100597
598/**
599 \rst
600 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800601 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100602 \endrst
603 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800604template <typename... Args>
605inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
606 const Args&... args) {
607 return {args...};
608}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800609
Victor Zverovich0a96c032018-10-25 07:20:02 -0700610template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800611inline std::basic_string<Char> vsprintf(
612 const S& format,
613 basic_format_args<
614 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
615 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200616 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700617 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500618 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700619}
620
Glen Stark72d51e02016-06-08 01:23:32 +0200621/**
622 \rst
623 Formats arguments and returns the result as a string.
624
625 **Example**::
626
627 std::string message = fmt::sprintf("The answer is %d", 42);
628 \endrst
629*/
Daniela Engertf27defc2018-10-07 14:38:29 +0200630template <typename S, typename... Args>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800631inline FMT_ENABLE_IF_T(internal::is_string<S>::value,
632 std::basic_string<FMT_CHAR(S)>)
633 sprintf(const S& format, const Args&... args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700634 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200635 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100636 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800637 format_arg_store<context, Args...> as{args...};
638 return vsprintf(to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700639}
640
Victor Zverovich0a96c032018-10-25 07:20:02 -0700641template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800642inline int vfprintf(
643 std::FILE* f, const S& format,
644 basic_format_args<
645 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
646 args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200647 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700648 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800649 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800650 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
651 ? -1
652 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800653}
Glen Stark72d51e02016-06-08 01:23:32 +0200654
655/**
656 \rst
657 Prints formatted data to the file *f*.
658
659 **Example**::
660
661 fmt::fprintf(stderr, "Don't %s!", "panic");
662 \endrst
663 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200664template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800665inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800666 fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700667 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200668 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100669 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800670 format_arg_store<context, Args...> as{args...};
671 return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700672}
673
Victor Zverovich0a96c032018-10-25 07:20:02 -0700674template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800675inline int vprintf(
676 const S& format,
677 basic_format_args<
678 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
679 args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700680 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200681}
682
Glen Stark72d51e02016-06-08 01:23:32 +0200683/**
684 \rst
685 Prints formatted data to ``stdout``.
686
687 **Example**::
688
689 fmt::printf("Elapsed time: %.2f seconds", 1.23);
690 \endrst
691 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200692template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800693inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800694 printf(const S& format_str, const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200695 internal::check_format_string<Args...>(format_str);
696 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100697 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800698 format_arg_store<context, Args...> as{args...};
699 return vprintf(to_string_view(format_str), basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200700}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700701
Victor Zverovich0a96c032018-10-25 07:20:02 -0700702template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800703inline int vfprintf(
704 std::basic_ostream<Char>& os, const S& format,
705 basic_format_args<
706 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
707 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200708 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700709 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200710 internal::write(os, buffer);
711 return static_cast<int>(buffer.size());
712}
713
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700714/**
715 \rst
716 Prints formatted data to the stream *os*.
717
718 **Example**::
719
Victor Zverovich40232912018-03-04 09:55:17 -0800720 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700721 \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::basic_ostream<FMT_CHAR(S)>& os, const S& format_str,
726 const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200727 internal::check_format_string<Args...>(format_str);
728 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100729 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800730 format_arg_store<context, Args...> as{args...};
Daniela Engert2c81c852018-10-08 20:14:39 +0200731 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200732 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200733}
Victor Zverovich838400d2018-05-12 08:33:51 -0700734FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200735
736#endif // FMT_PRINTF_H_