blob: 6cfc4dfc06dcaa703d75f8c44336819077aeb2ab [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.
21template <bool IsSigned>
Victor Zverovich418659a2018-03-03 14:04:59 -080022struct int_checker {
Glen Stark72d51e02016-06-08 01:23:32 +020023 template <typename T>
24 static bool fits_in_int(T value) {
25 unsigned max = std::numeric_limits<int>::max();
26 return value <= max;
27 }
28 static bool fits_in_int(bool) { return true; }
29};
30
31template <>
Victor Zverovich418659a2018-03-03 14:04:59 -080032struct int_checker<true> {
Glen Stark72d51e02016-06-08 01:23:32 +020033 template <typename T>
34 static bool fits_in_int(T value) {
35 return value >= std::numeric_limits<int>::min() &&
36 value <= std::numeric_limits<int>::max();
37 }
38 static bool fits_in_int(int) { return true; }
39};
40
Victor Zverovich418659a2018-03-03 14:04:59 -080041class printf_precision_handler: public function<int> {
Glen Stark72d51e02016-06-08 01:23:32 +020042 public:
Glen Stark72d51e02016-06-08 01:23:32 +020043 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080044 typename std::enable_if<std::is_integral<T>::value, int>::type
45 operator()(T value) {
Victor Zverovich418659a2018-03-03 14:04:59 -080046 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070047 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +020048 return static_cast<int>(value);
49 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080050
51 template <typename T>
Victor Zverovich6322b472018-06-06 16:51:35 +020052 typename std::enable_if<!std::is_integral<T>::value, int>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -080053 FMT_THROW(format_error("precision is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +020054 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -080055 }
Glen Stark72d51e02016-06-08 01:23:32 +020056};
57
Victor Zveroviche2dfd392016-11-19 09:29:09 -080058// An argument visitor that returns true iff arg is a zero integer.
Victor Zverovich418659a2018-03-03 14:04:59 -080059class is_zero_int: public function<bool> {
Glen Stark72d51e02016-06-08 01:23:32 +020060 public:
61 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080062 typename std::enable_if<std::is_integral<T>::value, bool>::type
63 operator()(T value) { return value == 0; }
64
65 template <typename T>
66 typename std::enable_if<!std::is_integral<T>::value, bool>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -070067 operator()(T) { return false; }
Glen Stark72d51e02016-06-08 01:23:32 +020068};
69
Victor Zverovich0fbd8462017-09-04 11:41:15 -070070template <typename T>
71struct make_unsigned_or_bool : std::make_unsigned<T> {};
72
73template <>
74struct make_unsigned_or_bool<bool> {
Victor Zverovichaffb35c2018-02-11 09:23:47 -080075 typedef bool type;
Victor Zverovich0fbd8462017-09-04 11:41:15 -070076};
77
Victor Zverovichd705d512016-12-29 09:07:39 -080078template <typename T, typename Context>
Victor Zverovich418659a2018-03-03 14:04:59 -080079class arg_converter: public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +020080 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080081 typedef typename Context::char_type Char;
82
Victor Zverovich23759b22018-04-04 07:38:21 -070083 basic_format_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080084 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020085
Glen Stark72d51e02016-06-08 01:23:32 +020086 public:
Victor Zverovich23759b22018-04-04 07:38:21 -070087 arg_converter(basic_format_arg<Context> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +020088 : arg_(arg), type_(type) {}
89
Victor Zverovich751ff642016-11-19 08:40:24 -080090 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020091 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080092 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020093 }
94
95 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -080096 typename std::enable_if<std::is_integral<U>::value>::type
97 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020098 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovichc18a4042017-09-04 13:56:14 -070099 typedef typename std::conditional<
Victor Zverovichf194a412017-08-27 09:16:50 -0700100 std::is_same<T, void>::value, U, T>::type TargetType;
Victor Zverovich5013c152018-02-10 06:52:46 -0800101 if (const_check(sizeof(TargetType) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +0200102 // Extra casts are used to silence warnings.
103 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800104 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800105 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200106 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700107 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800108 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800109 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200110 }
111 } else {
112 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200113 // glibc's printf doesn't sign extend arguments of smaller types:
114 // std::printf("%lld", -42); // prints "4294967254"
115 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700116 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200117 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800118 arg_ = internal::make_arg<Context>(
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700119 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200120 }
121 }
122 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800123
124 template <typename U>
Victor Zverovich77c892c2017-08-27 08:16:46 -0700125 typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800126 // No coversion needed for non-integral types.
127 }
Glen Stark72d51e02016-06-08 01:23:32 +0200128};
129
Victor Zverovich751ff642016-11-19 08:40:24 -0800130// Converts an integer argument to T for printf, if T is an integral type.
131// If T is void, the argument is converted to corresponding signed or unsigned
132// type depending on the type specifier: 'd' and 'i' - signed, other -
133// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800134template <typename T, typename Context, typename Char>
Victor Zverovich23759b22018-04-04 07:38:21 -0700135void convert_arg(basic_format_arg<Context> &arg, Char type) {
Victor Zverovichf5480632018-10-05 07:15:41 -0700136 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800137}
138
Glen Stark72d51e02016-06-08 01:23:32 +0200139// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800140template <typename Context>
Victor Zverovich418659a2018-03-03 14:04:59 -0800141class char_converter: public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200142 private:
Victor Zverovich23759b22018-04-04 07:38:21 -0700143 basic_format_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200144
Glen Stark72d51e02016-06-08 01:23:32 +0200145 public:
Victor Zverovich23759b22018-04-04 07:38:21 -0700146 explicit char_converter(basic_format_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200147
148 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800149 typename std::enable_if<std::is_integral<T>::value>::type
150 operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200151 typedef typename Context::char_type Char;
152 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200153 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800154
155 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800156 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800157 // No coversion needed for non-integral types.
158 }
Glen Stark72d51e02016-06-08 01:23:32 +0200159};
160
161// Checks if an argument is a valid printf width specifier and sets
162// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000163template <typename Char>
Victor Zverovich418659a2018-03-03 14:04:59 -0800164class printf_width_handler: public function<unsigned> {
Glen Stark72d51e02016-06-08 01:23:32 +0200165 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000166 typedef basic_format_specs<Char> format_specs;
167
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000168 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200169
Glen Stark72d51e02016-06-08 01:23:32 +0200170 public:
Victor Zverovich418659a2018-03-03 14:04:59 -0800171 explicit printf_width_handler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200172
Glen Stark72d51e02016-06-08 01:23:32 +0200173 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800174 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
175 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800176 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200177 UnsignedType width = static_cast<UnsignedType>(value);
178 if (internal::is_negative(value)) {
179 spec_.align_ = ALIGN_LEFT;
180 width = 0 - width;
181 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700182 unsigned int_max = std::numeric_limits<int>::max();
183 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700184 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200185 return static_cast<unsigned>(width);
186 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800187
188 template <typename T>
189 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -0700190 operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800191 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200192 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800193 }
Glen Stark72d51e02016-06-08 01:23:32 +0200194};
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800195
196template <typename Char, typename Context>
197void printf(basic_buffer<Char> &buf, basic_string_view<Char> format,
198 basic_format_args<Context> args) {
199 Context(std::back_inserter(buf), format, args).format();
200}
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700201} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200202
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800203using internal::printf; // For printing into memory_buffer.
204
Victor Zverovichc0954452018-01-06 09:09:50 -0800205template <typename Range>
Victor Zverovich10e70a02017-12-02 09:44:48 -0800206class printf_arg_formatter;
207
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800208template <
209 typename OutputIt, typename Char,
210 typename ArgFormatter =
211 printf_arg_formatter<back_insert_range<internal::basic_buffer<Char>>>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800212class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800213
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700214/**
215 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800216 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700217 \endrst
218 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800219template <typename Range>
Victor Zverovich418659a2018-03-03 14:04:59 -0800220class printf_arg_formatter:
Victor Zverovichedd5f142018-05-20 09:09:03 -0700221 public internal::function<
222 typename internal::arg_formatter_base<Range>::iterator>,
223 public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200224 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800225 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800226 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800227 typedef internal::arg_formatter_base<Range> base;
228 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800229
230 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 */
Victor Zverovich9a53a702018-01-14 12:25:03 -0800252 printf_arg_formatter(internal::basic_buffer<char_type> &buffer,
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800253 format_specs &spec, context_type &ctx)
Victor Zverovichf2ee9882018-11-14 09:39:37 -0800254 : base(back_insert_range<internal::basic_buffer<char_type>>(buffer), &spec,
255 ctx.locale()),
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800256 context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200257
Victor Zveroviche928b672018-07-04 11:36:38 -0700258 template <typename T>
259 typename std::enable_if<std::is_integral<T>::value, iterator>::type
260 operator()(T value) {
261 // MSVC2013 fails to compile separate overloads for bool and char_type so
262 // use std::is_same instead.
263 if (std::is_same<T, bool>::value) {
Victor Zverovich7110b462018-09-12 07:34:22 -0700264 format_specs &fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700265 if (fmt_spec.type != 's')
Victor Zveroviche928b672018-07-04 11:36:38 -0700266 return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700267 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700268 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700269 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich7110b462018-09-12 07:34:22 -0700270 format_specs &fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700271 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700272 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700273 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700274 fmt_spec.align_ = ALIGN_RIGHT;
275 return base::operator()(value);
276 } else {
277 return base::operator()(value);
278 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000279 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200280 }
281
Victor Zveroviche928b672018-07-04 11:36:38 -0700282 template <typename T>
283 typename std::enable_if<std::is_floating_point<T>::value, iterator>::type
284 operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000285 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200286 }
287
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700288 /** Formats a null-terminated C string. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000289 iterator operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200290 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800291 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700292 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200293 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200294 else
295 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000296 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200297 }
298
Daniela Engert2570f1a2018-04-26 20:32:14 +0200299 /** Formats a null-terminated wide C string. */
300 iterator operator()(const wchar_t *value) {
301 if (value)
302 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700303 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200304 write_null_pointer(char_type());
305 else
306 this->write(L"(null)");
307 return this->out();
308 }
309
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700310 iterator operator()(basic_string_view<char_type> value) {
311 return base::operator()(value);
312 }
313
314 iterator operator()(monostate value) {
315 return base::operator()(value);
316 }
317
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700318 /** Formats a pointer. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000319 iterator operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200320 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800321 return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700322 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200323 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000324 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200325 }
326
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700327 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700328 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800329 handle.format(context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000330 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700331 }
332};
333
Victor Zverovichc0954452018-01-06 09:09:50 -0800334template <typename T>
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700335struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700336 template <typename ParseContext>
Victor Zverovich1b452532018-02-28 05:09:24 -0800337 auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { return ctx.begin(); }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700338
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800339 template <typename FormatContext>
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700340 auto format(const T &value, FormatContext &ctx) -> decltype(ctx.out()) {
341 internal::format_value(internal::get_container(ctx.out()), value);
342 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200343 }
344};
345
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700346/** This template formats data and writes the output to a writer. */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800347template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800348class basic_printf_context :
Victor Zverovicha11eb3a2018-08-22 09:07:17 -0700349 // Inherit publicly as a workaround for the icc bug
350 // https://software.intel.com/en-us/forums/intel-c-compiler/topic/783476.
351 public internal::context_base<
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800352 OutputIt, basic_printf_context<OutputIt, Char, ArgFormatter>, Char> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700353 public:
354 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800355 typedef Char char_type;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700356
357 template <typename T>
Victor Zverovich522de7b2018-02-11 08:32:02 -0800358 struct formatter_type { typedef printf_formatter<T> type; };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700359
Glen Stark72d51e02016-06-08 01:23:32 +0200360 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800361 typedef internal::context_base<OutputIt, basic_printf_context, Char> base;
362 typedef typename base::format_arg format_arg;
363 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700364
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100365 static void parse_flags(format_specs &spec, const Char *&it, const Char *end);
Glen Stark72d51e02016-06-08 01:23:32 +0200366
367 // Returns the argument with specified index or, if arg_index is equal
368 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100369 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200370
371 // Parses argument index, flags and width and returns the argument index.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100372 unsigned parse_header(const Char *&it, const Char *end, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200373
374 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700375 /**
376 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800377 Constructs a ``printf_context`` object. References to the arguments and
378 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700379 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700380 \endrst
381 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800382 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800383 basic_format_args<basic_printf_context> args)
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800384 : base(out, format_str, args) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700385
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800386 using base::parse_context;
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700387 using base::out;
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800388 using base::advance_to;
Victor Zverovich9998f662016-11-06 16:11:24 -0800389
Victor Zverovichc0954452018-01-06 09:09:50 -0800390 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich08dff372018-01-28 20:50:43 -0800391 void format();
Glen Stark72d51e02016-06-08 01:23:32 +0200392};
393
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800394template <typename OutputIt, typename Char, typename AF>
395void basic_printf_context<OutputIt, Char, AF>::parse_flags(
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100396 format_specs &spec, const Char *&it, const Char *end) {
397 for (; it != end; ++it) {
398 switch (*it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200399 case '-':
400 spec.align_ = ALIGN_LEFT;
401 break;
402 case '+':
Victor Zverovich29246222018-10-17 09:15:29 -0700403 spec.flags |= SIGN_FLAG | PLUS_FLAG;
Glen Stark72d51e02016-06-08 01:23:32 +0200404 break;
405 case '0':
406 spec.fill_ = '0';
407 break;
408 case ' ':
Victor Zverovich29246222018-10-17 09:15:29 -0700409 spec.flags |= SIGN_FLAG;
Glen Stark72d51e02016-06-08 01:23:32 +0200410 break;
411 case '#':
Victor Zverovich29246222018-10-17 09:15:29 -0700412 spec.flags |= HASH_FLAG;
Glen Stark72d51e02016-06-08 01:23:32 +0200413 break;
414 default:
Glen Stark72d51e02016-06-08 01:23:32 +0200415 return;
416 }
417 }
418}
419
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800420template <typename OutputIt, typename Char, typename AF>
421typename basic_printf_context<OutputIt, Char, AF>::format_arg
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100422 basic_printf_context<OutputIt, Char, AF>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800423 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich67928ea2018-01-14 09:27:40 -0800424 return this->do_get_arg(this->parse_context().next_arg_id());
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800425 return base::get_arg(arg_index - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200426}
427
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800428template <typename OutputIt, typename Char, typename AF>
429unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100430 const Char *&it, const Char *end, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200431 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800432 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200433 if (c >= '0' && c <= '9') {
434 // Parse an argument index (if followed by '$') or a width possibly
435 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700436 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100437 unsigned value = parse_nonnegative_int(it, end, eh);
438 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700439 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200440 arg_index = value;
441 } else {
442 if (c == '0')
443 spec.fill_ = '0';
444 if (value != 0) {
445 // Nonzero value means that we parsed width and don't need to
446 // parse it or flags again, so return now.
447 spec.width_ = value;
448 return arg_index;
449 }
450 }
451 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100452 parse_flags(spec, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200453 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100454 if (it != end) {
455 if (*it >= '0' && *it <= '9') {
456 internal::error_handler eh;
457 spec.width_ = parse_nonnegative_int(it, end, eh);
458 } else if (*it == '*') {
459 ++it;
460 spec.width_ = visit_format_arg(
461 internal::printf_width_handler<char_type>(spec), get_arg());
462 }
Glen Stark72d51e02016-06-08 01:23:32 +0200463 }
464 return arg_index;
465}
466
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800467template <typename OutputIt, typename Char, typename AF>
468void basic_printf_context<OutputIt, Char, AF>::format() {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700469 auto &buffer = internal::get_container(this->out());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100470 const auto range = this->parse_context();
471 const Char * end = range.end();
472 const Char * start = range.begin();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700473 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100474 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800475 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200476 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100477 if (it != end && *it == c) {
478 buffer.append(start, it);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700479 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200480 continue;
481 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100482 buffer.append(start, it - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200483
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000484 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200485 spec.align_ = ALIGN_RIGHT;
486
487 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100488 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200489
490 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100491 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700492 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100493 c = it != end ? *it : 0;
494 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700495 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100496 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
497 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700498 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700499 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100500 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800501 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700502 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200503 }
504 }
505
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100506 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700507 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Sign Bit0bbdca52018-12-20 09:24:19 -0500508 spec.flags = static_cast<uint_least8_t>(spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200509 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800510 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200511 spec.align_ = ALIGN_NUMERIC;
512 else
513 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
514 }
515
516 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100517 c = it != end ? *it++ : 0;
518 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800519 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100520 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200521 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100522 if (t == 'h') {
523 ++it;
524 t = it != end ? *it : 0;
525 convert_arg<signed char>(arg, t);
526 } else {
527 convert_arg<short>(arg, t);
528 }
Glen Stark72d51e02016-06-08 01:23:32 +0200529 break;
530 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100531 if (t == 'l') {
532 ++it;
533 t = it != end ? *it : 0;
534 convert_arg<long long>(arg, t);
535 } else {
536 convert_arg<long>(arg, t);
537 }
Glen Stark72d51e02016-06-08 01:23:32 +0200538 break;
539 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100540 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200541 break;
542 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100543 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200544 break;
545 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100546 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200547 break;
548 case 'L':
549 // printf produces garbage when 'L' is omitted for long double, no
550 // need to do the same.
551 break;
552 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700553 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100554 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200555 }
556
557 // Parse type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100558 if (it == end)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700559 FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700560 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800561 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200562 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700563 switch (spec.type) {
Glen Stark72d51e02016-06-08 01:23:32 +0200564 case 'i': case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700565 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200566 break;
567 case 'c':
Daniela Engert2570f1a2018-04-26 20:32:14 +0200568 // TODO: handle wchar_t better?
Victor Zverovichf5480632018-10-05 07:15:41 -0700569 visit_format_arg(
570 internal::char_converter<basic_printf_context>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200571 break;
572 }
573 }
574
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700575 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200576
577 // Format argument.
Victor Zverovichf5480632018-10-05 07:15:41 -0700578 visit_format_arg(AF(buffer, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200579 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100580 buffer.append(start, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200581}
Glen Stark72d51e02016-06-08 01:23:32 +0200582
Victor Zverovich217e7c72018-01-14 07:19:23 -0800583template <typename Buffer>
tnovotnye37d6a92018-11-22 22:57:07 +0100584struct basic_printf_context_t {
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800585 typedef basic_printf_context<
586 std::back_insert_iterator<Buffer>, typename Buffer::value_type> type;
587};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800588
tnovotnye37d6a92018-11-22 22:57:07 +0100589typedef basic_printf_context_t<internal::buffer>::type printf_context;
590typedef basic_printf_context_t<internal::wbuffer>::type wprintf_context;
591
592typedef basic_format_args<printf_context> printf_args;
593typedef basic_format_args<wprintf_context> wprintf_args;
594
595/**
596 \rst
597 Constructs an `~fmt::format_arg_store` object that contains references to
598 arguments and can be implicitly converted to `~fmt::printf_args`.
599 \endrst
600 */
601template<typename... Args>
602inline format_arg_store<printf_context, Args...>
603 make_printf_args(const Args &... args) { return {args...}; }
604
605/**
606 \rst
607 Constructs an `~fmt::format_arg_store` object that contains references to
608 arguments and can be implicitly converted to `~fmt::wprintf_args`.
609 \endrst
610 */
611template<typename... Args>
612inline format_arg_store<wprintf_context, Args...>
613 make_wprintf_args(const Args &... args) { return {args...}; }
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800614
Victor Zverovich0a96c032018-10-25 07:20:02 -0700615template <typename S, typename Char = FMT_CHAR(S)>
Daniela Engertf27defc2018-10-07 14:38:29 +0200616inline std::basic_string<Char>
Victor Zverovich0a96c032018-10-25 07:20:02 -0700617vsprintf(const S &format,
tnovotnye37d6a92018-11-22 22:57:07 +0100618 basic_format_args<typename basic_printf_context_t<
Daniela Engertf27defc2018-10-07 14:38:29 +0200619 internal::basic_buffer<Char>>::type> args) {
620 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700621 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500622 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700623}
624
Glen Stark72d51e02016-06-08 01:23:32 +0200625/**
626 \rst
627 Formats arguments and returns the result as a string.
628
629 **Example**::
630
631 std::string message = fmt::sprintf("The answer is %d", 42);
632 \endrst
633*/
Daniela Engertf27defc2018-10-07 14:38:29 +0200634template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800635inline FMT_ENABLE_IF_T(
636 internal::is_string<S>::value, std::basic_string<FMT_CHAR(S)>)
Victor Zverovich0a96c032018-10-25 07:20:02 -0700637 sprintf(const S &format, const Args & ... args) {
638 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200639 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100640 typedef typename basic_printf_context_t<buffer>::type context;
Daniela Engertf27defc2018-10-07 14:38:29 +0200641 format_arg_store<context, Args...> as{ args... };
Victor Zverovich0a96c032018-10-25 07:20:02 -0700642 return vsprintf(to_string_view(format),
Daniela Engertf27defc2018-10-07 14:38:29 +0200643 basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700644}
645
Victor Zverovich0a96c032018-10-25 07:20:02 -0700646template <typename S, typename Char = FMT_CHAR(S)>
647inline int vfprintf(std::FILE *f, const S &format,
tnovotnye37d6a92018-11-22 22:57:07 +0100648 basic_format_args<typename basic_printf_context_t<
Daniela Engert2570f1a2018-04-26 20:32:14 +0200649 internal::basic_buffer<Char>>::type> args) {
650 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700651 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800652 std::size_t size = buffer.size();
653 return std::fwrite(
Daniela Engert2570f1a2018-04-26 20:32:14 +0200654 buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800655}
Glen Stark72d51e02016-06-08 01:23:32 +0200656
657/**
658 \rst
659 Prints formatted data to the file *f*.
660
661 **Example**::
662
663 fmt::fprintf(stderr, "Don't %s!", "panic");
664 \endrst
665 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200666template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800667inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Victor Zverovich0a96c032018-10-25 07:20:02 -0700668 fprintf(std::FILE *f, const S &format, const Args & ... args) {
669 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200670 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100671 typedef typename basic_printf_context_t<buffer>::type context;
Daniela Engertf27defc2018-10-07 14:38:29 +0200672 format_arg_store<context, Args...> as{ args... };
Victor Zverovich0a96c032018-10-25 07:20:02 -0700673 return vfprintf(f, to_string_view(format),
Daniela Engertf27defc2018-10-07 14:38:29 +0200674 basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700675}
676
Victor Zverovich0a96c032018-10-25 07:20:02 -0700677template <typename S, typename Char = FMT_CHAR(S)>
678inline int vprintf(const S &format,
tnovotnye37d6a92018-11-22 22:57:07 +0100679 basic_format_args<typename basic_printf_context_t<
Daniela Engertf27defc2018-10-07 14:38:29 +0200680 internal::basic_buffer<Char>>::type> args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700681 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200682}
683
Glen Stark72d51e02016-06-08 01:23:32 +0200684/**
685 \rst
686 Prints formatted data to ``stdout``.
687
688 **Example**::
689
690 fmt::printf("Elapsed time: %.2f seconds", 1.23);
691 \endrst
692 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200693template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800694inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Daniela Engertf27defc2018-10-07 14:38:29 +0200695 printf(const S &format_str, const Args & ... args) {
696 internal::check_format_string<Args...>(format_str);
697 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100698 typedef typename basic_printf_context_t<buffer>::type context;
Daniela Engertf27defc2018-10-07 14:38:29 +0200699 format_arg_store<context, Args...> as{ args... };
Daniela Engert2c81c852018-10-08 20:14:39 +0200700 return vprintf(to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200701 basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200702}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700703
Victor Zverovich0a96c032018-10-25 07:20:02 -0700704template <typename S, typename Char = FMT_CHAR(S)>
Daniela Engertf27defc2018-10-07 14:38:29 +0200705inline int vfprintf(std::basic_ostream<Char> &os,
Victor Zverovich0a96c032018-10-25 07:20:02 -0700706 const S &format,
tnovotnye37d6a92018-11-22 22:57:07 +0100707 basic_format_args<typename basic_printf_context_t<
Daniela Engertf27defc2018-10-07 14:38:29 +0200708 internal::basic_buffer<Char>>::type> args) {
709 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700710 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200711 internal::write(os, buffer);
712 return static_cast<int>(buffer.size());
713}
714
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700715/**
716 \rst
717 Prints formatted data to the stream *os*.
718
719 **Example**::
720
Victor Zverovich40232912018-03-04 09:55:17 -0800721 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700722 \endrst
723 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200724template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800725inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Daniela Engertf27defc2018-10-07 14:38:29 +0200726 fprintf(std::basic_ostream<FMT_CHAR(S)> &os,
727 const S &format_str, const Args & ... args) {
728 internal::check_format_string<Args...>(format_str);
729 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100730 typedef typename basic_printf_context_t<buffer>::type context;
Daniela Engertf27defc2018-10-07 14:38:29 +0200731 format_arg_store<context, Args...> as{ args... };
Daniela Engert2c81c852018-10-08 20:14:39 +0200732 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200733 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200734}
Victor Zverovich838400d2018-05-12 08:33:51 -0700735FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200736
737#endif // FMT_PRINTF_H_