blob: fbca84ad99b9ff6fcf212ca6dee836078901b243 [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
Victor Zverovich294fd7d2019-03-17 14:49:19 -070019// A helper function to suppress bogus "conditional expression is constant"
20// warnings.
21template <typename T> inline T const_check(T value) { return value; }
22
Glen Stark72d51e02016-06-08 01:23:32 +020023// Checks if a value fits in int - used to avoid warnings about comparing
24// signed and unsigned integers.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080025template <bool IsSigned> struct int_checker {
26 template <typename T> static bool fits_in_int(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020027 unsigned max = std::numeric_limits<int>::max();
28 return value <= max;
29 }
30 static bool fits_in_int(bool) { return true; }
31};
32
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080033template <> struct int_checker<true> {
34 template <typename T> static bool fits_in_int(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020035 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 Zverovich874d6722019-06-13 20:16:06 -070041class printf_precision_handler {
Glen Stark72d51e02016-06-08 01:23:32 +020042 public:
Victor Zverovichc21c6b82019-03-16 12:58:18 -070043 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
44 int operator()(T value) {
Victor Zverovich418659a2018-03-03 14:04:59 -080045 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070046 FMT_THROW(format_error("number is too big"));
Victor Zverovichbd516e32019-04-21 07:39:41 -070047 return (std::max)(static_cast<int>(value), 0);
Glen Stark72d51e02016-06-08 01:23:32 +020048 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080049
Victor Zverovichc21c6b82019-03-16 12:58:18 -070050 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
51 int operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -080052 FMT_THROW(format_error("precision is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +020053 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -080054 }
Glen Stark72d51e02016-06-08 01:23:32 +020055};
56
Victor Zveroviche2dfd392016-11-19 09:29:09 -080057// An argument visitor that returns true iff arg is a zero integer.
Victor Zverovich874d6722019-06-13 20:16:06 -070058class is_zero_int {
Glen Stark72d51e02016-06-08 01:23:32 +020059 public:
Victor Zverovichc21c6b82019-03-16 12:58:18 -070060 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
61 bool operator()(T value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080062 return value == 0;
63 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080064
Victor Zverovichc21c6b82019-03-16 12:58:18 -070065 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
66 bool operator()(T) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080067 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 Zverovich874d6722019-06-13 20:16:06 -070075template <typename T, typename Context> class arg_converter {
Glen Stark72d51e02016-06-08 01:23:32 +020076 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080077 typedef typename Context::char_type Char;
78
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080079 basic_format_arg<Context>& arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080080 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020081
Glen Stark72d51e02016-06-08 01:23:32 +020082 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080083 arg_converter(basic_format_arg<Context>& arg, Char type)
84 : arg_(arg), type_(type) {}
Glen Stark72d51e02016-06-08 01:23:32 +020085
Victor Zverovich751ff642016-11-19 08:40:24 -080086 void operator()(bool value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080087 if (type_ != 's') operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020088 }
89
Victor Zverovichc21c6b82019-03-16 12:58:18 -070090 template <typename U, FMT_ENABLE_IF(std::is_integral<U>::value)>
91 void operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020092 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovichc264e642019-06-04 18:50:30 -070093 using target_type = conditional_t<std::is_same<T, void>::value, U, T>;
94 if (const_check(sizeof(target_type) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +020095 // Extra casts are used to silence warnings.
96 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -080097 arg_ = internal::make_arg<Context>(
Victor Zverovichc264e642019-06-04 18:50:30 -070098 static_cast<int>(static_cast<target_type>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +020099 } else {
Victor Zverovichc264e642019-06-04 18:50:30 -0700100 typedef typename make_unsigned_or_bool<target_type>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800101 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800102 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200103 }
104 } else {
105 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200106 // glibc's printf doesn't sign extend arguments of smaller types:
107 // std::printf("%lld", -42); // prints "4294967254"
108 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700109 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200110 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800111 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800112 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200113 }
114 }
115 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800116
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700117 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)>
118 void operator()(U) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200119};
120
Victor Zverovich751ff642016-11-19 08:40:24 -0800121// Converts an integer argument to T for printf, if T is an integral type.
122// If T is void, the argument is converted to corresponding signed or unsigned
123// type depending on the type specifier: 'd' and 'i' - signed, other -
124// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800125template <typename T, typename Context, typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800126void convert_arg(basic_format_arg<Context>& arg, Char type) {
Victor Zverovichf5480632018-10-05 07:15:41 -0700127 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800128}
129
Glen Stark72d51e02016-06-08 01:23:32 +0200130// Converts an integer argument to char for printf.
Victor Zverovich874d6722019-06-13 20:16:06 -0700131template <typename Context> class char_converter {
Glen Stark72d51e02016-06-08 01:23:32 +0200132 private:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800133 basic_format_arg<Context>& arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200134
Glen Stark72d51e02016-06-08 01:23:32 +0200135 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800136 explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200137
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700138 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
139 void operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200140 typedef typename Context::char_type Char;
141 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200142 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800143
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700144 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
145 void operator()(T) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200146};
147
148// Checks if an argument is a valid printf width specifier and sets
149// left alignment if it is negative.
Victor Zverovich874d6722019-06-13 20:16:06 -0700150template <typename Char> class printf_width_handler {
Glen Stark72d51e02016-06-08 01:23:32 +0200151 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000152 typedef basic_format_specs<Char> format_specs;
153
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800154 format_specs& spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200155
Glen Stark72d51e02016-06-08 01:23:32 +0200156 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800157 explicit printf_width_handler(format_specs& spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200158
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700159 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
160 unsigned operator()(T value) {
Victor Zverovichc286ffc2019-07-03 16:01:21 -0700161 auto width = static_cast<uint32_or_64_t<T>>(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200162 if (internal::is_negative(value)) {
163 spec_.align_ = ALIGN_LEFT;
164 width = 0 - width;
165 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700166 unsigned int_max = std::numeric_limits<int>::max();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800167 if (width > int_max) FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200168 return static_cast<unsigned>(width);
169 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800170
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700171 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
172 unsigned operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800173 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200174 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800175 }
Glen Stark72d51e02016-06-08 01:23:32 +0200176};
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800177
178template <typename Char, typename Context>
Victor Zverovich28083952019-04-07 10:05:49 -0700179void printf(buffer<Char>& buf, basic_string_view<Char> format,
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800180 basic_format_args<Context> args) {
181 Context(std::back_inserter(buf), format, args).format();
182}
Daniela Engertb0cde862019-01-01 11:45:56 +0100183
184template <typename OutputIt, typename Char, typename Context>
185internal::truncating_iterator<OutputIt> printf(
186 internal::truncating_iterator<OutputIt> it, basic_string_view<Char> format,
187 basic_format_args<Context> args) {
188 return Context(it, format, args).format();
189}
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700190} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200191
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800192using internal::printf; // For printing into memory_buffer.
193
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800194template <typename Range> class printf_arg_formatter;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800195
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700196template <typename OutputIt, typename Char> class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800197
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700198/**
199 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800200 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700201 \endrst
202 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800203template <typename Range>
Victor Zverovich9d7b64a2019-06-11 22:11:47 -0700204class printf_arg_formatter : public internal::arg_formatter_base<Range> {
ToolsDevler2b415b72019-04-11 11:05:55 +0200205 public:
Victor Zverovich8302c2f2019-05-30 08:34:17 -0700206 typedef decltype(std::declval<Range>().begin()) iterator;
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700207
Glen Stark72d51e02016-06-08 01:23:32 +0200208 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800209 typedef typename Range::value_type char_type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800210 typedef internal::arg_formatter_base<Range> base;
ToolsDevler2b415b72019-04-11 11:05:55 +0200211 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800212
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800213 context_type& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800214
Daniela Engert2570f1a2018-04-26 20:32:14 +0200215 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700216 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200217 this->write("(nil)");
218 }
219
Daniela Engert2570f1a2018-04-26 20:32:14 +0200220 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700221 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200222 this->write(L"(nil)");
223 }
224
Glen Stark72d51e02016-06-08 01:23:32 +0200225 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800226 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000227
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700228 /**
229 \rst
230 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500231 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700232 specifier information for standard argument types.
233 \endrst
234 */
Daniela Engertb0cde862019-01-01 11:45:56 +0100235 printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800236 : base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200237
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700238 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
239 iterator operator()(T value) {
Victor Zveroviche928b672018-07-04 11:36:38 -0700240 // MSVC2013 fails to compile separate overloads for bool and char_type so
241 // use std::is_same instead.
242 if (std::is_same<T, bool>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800243 format_specs& fmt_spec = *this->spec();
244 if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700245 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700246 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700247 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800248 format_specs& fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700249 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700250 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700251 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700252 fmt_spec.align_ = ALIGN_RIGHT;
253 return base::operator()(value);
254 } else {
255 return base::operator()(value);
256 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000257 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200258 }
259
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700260 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
261 iterator operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000262 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200263 }
264
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700265 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800266 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200267 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800268 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700269 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200270 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200271 else
272 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000273 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200274 }
275
Daniela Engert2570f1a2018-04-26 20:32:14 +0200276 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800277 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200278 if (value)
279 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700280 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200281 write_null_pointer(char_type());
282 else
283 this->write(L"(null)");
284 return this->out();
285 }
286
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700287 iterator operator()(basic_string_view<char_type> value) {
288 return base::operator()(value);
289 }
290
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800291 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700292
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700293 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800294 iterator operator()(const void* value) {
295 if (value) return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700296 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200297 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000298 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200299 }
300
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700301 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700302 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800303 handle.format(context_.parse_context(), context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000304 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700305 }
306};
307
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800308template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700309 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800310 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
311 return ctx.begin();
312 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700313
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800314 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800315 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700316 internal::format_value(internal::get_container(ctx.out()), value);
317 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200318 }
319};
320
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700321/** This template formats data and writes the output to a writer. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700322template <typename OutputIt, typename Char> class basic_printf_context {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700323 public:
324 /** The character type for the output. */
Victor Zverovichec665102019-06-02 16:04:17 -0700325 using char_type = Char;
326 using format_arg = basic_format_arg<basic_printf_context>;
327 template <typename T> using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700328
Glen Stark72d51e02016-06-08 01:23:32 +0200329 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800330 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700331
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800332 OutputIt out_;
333 basic_format_args<basic_printf_context> args_;
334 basic_parse_context<Char> parse_ctx_;
335
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800336 static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200337
338 // Returns the argument with specified index or, if arg_index is equal
339 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100340 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200341
342 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800343 unsigned parse_header(const Char*& it, const Char* end, format_specs& spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200344
345 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700346 /**
347 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800348 Constructs a ``printf_context`` object. References to the arguments and
349 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700350 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700351 \endrst
352 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800353 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800354 basic_format_args<basic_printf_context> args)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800355 : out_(out), args_(args), parse_ctx_(format_str) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700356
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800357 OutputIt out() { return out_; }
358 void advance_to(OutputIt it) { out_ = it; }
359
360 format_arg arg(unsigned id) const { return args_.get(id); }
361
362 basic_parse_context<Char>& parse_context() { return parse_ctx_; }
363
364 FMT_CONSTEXPR void on_error(const char* message) {
365 parse_ctx_.on_error(message);
366 }
Victor Zverovich9998f662016-11-06 16:11:24 -0800367
Victor Zverovichc0954452018-01-06 09:09:50 -0800368 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700369 template <typename ArgFormatter =
Victor Zverovichf13906f2019-06-19 13:51:36 -0700370 printf_arg_formatter<internal::buffer_range<Char>>>
Daniela Engertb0cde862019-01-01 11:45:56 +0100371 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200372};
373
ToolsDevler2b415b72019-04-11 11:05:55 +0200374template <typename OutputIt, typename Char>
375void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& spec,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700376 const Char*& it,
377 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100378 for (; it != end; ++it) {
379 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800380 case '-':
381 spec.align_ = ALIGN_LEFT;
382 break;
383 case '+':
384 spec.flags |= SIGN_FLAG | PLUS_FLAG;
385 break;
386 case '0':
387 spec.fill_ = '0';
388 break;
389 case ' ':
390 spec.flags |= SIGN_FLAG;
391 break;
392 case '#':
393 spec.flags |= HASH_FLAG;
394 break;
395 default:
396 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200397 }
398 }
399}
400
ToolsDevler2b415b72019-04-11 11:05:55 +0200401template <typename OutputIt, typename Char>
402typename basic_printf_context<OutputIt, Char>::format_arg
403basic_printf_context<OutputIt, Char>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800404 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800405 arg_index = parse_ctx_.next_arg_id();
406 else
407 parse_ctx_.check_arg_id(--arg_index);
408 return internal::get_arg(*this, arg_index);
Glen Stark72d51e02016-06-08 01:23:32 +0200409}
410
ToolsDevler2b415b72019-04-11 11:05:55 +0200411template <typename OutputIt, typename Char>
412unsigned basic_printf_context<OutputIt, Char>::parse_header(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800413 const Char*& it, const Char* end, format_specs& spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200414 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800415 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200416 if (c >= '0' && c <= '9') {
417 // Parse an argument index (if followed by '$') or a width possibly
418 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700419 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100420 unsigned value = parse_nonnegative_int(it, end, eh);
421 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700422 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200423 arg_index = value;
424 } else {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800425 if (c == '0') spec.fill_ = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200426 if (value != 0) {
427 // Nonzero value means that we parsed width and don't need to
428 // parse it or flags again, so return now.
429 spec.width_ = value;
430 return arg_index;
431 }
432 }
433 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100434 parse_flags(spec, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200435 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100436 if (it != end) {
437 if (*it >= '0' && *it <= '9') {
438 internal::error_handler eh;
439 spec.width_ = parse_nonnegative_int(it, end, eh);
440 } else if (*it == '*') {
441 ++it;
442 spec.width_ = visit_format_arg(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800443 internal::printf_width_handler<char_type>(spec), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100444 }
Glen Stark72d51e02016-06-08 01:23:32 +0200445 }
446 return arg_index;
447}
448
ToolsDevler2b415b72019-04-11 11:05:55 +0200449template <typename OutputIt, typename Char>
450template <typename ArgFormatter>
451OutputIt basic_printf_context<OutputIt, Char>::format() {
Daniela Engertb0cde862019-01-01 11:45:56 +0100452 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800453 const Char* start = parse_ctx_.begin();
454 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700455 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100456 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800457 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200458 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100459 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100460 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700461 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200462 continue;
463 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100464 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200465
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000466 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200467 spec.align_ = ALIGN_RIGHT;
468
469 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100470 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200471
472 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100473 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700474 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100475 c = it != end ? *it : 0;
476 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700477 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100478 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
479 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700480 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700481 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100482 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800483 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700484 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200485 }
486 }
487
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100488 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700489 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800490 spec.flags = static_cast<uint_least8_t>(
491 spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200492 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800493 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200494 spec.align_ = ALIGN_NUMERIC;
495 else
496 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
497 }
498
499 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100500 c = it != end ? *it++ : 0;
501 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800502 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100503 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200504 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100505 if (t == 'h') {
506 ++it;
507 t = it != end ? *it : 0;
508 convert_arg<signed char>(arg, t);
509 } else {
510 convert_arg<short>(arg, t);
511 }
Glen Stark72d51e02016-06-08 01:23:32 +0200512 break;
513 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100514 if (t == 'l') {
515 ++it;
516 t = it != end ? *it : 0;
517 convert_arg<long long>(arg, t);
518 } else {
519 convert_arg<long>(arg, t);
520 }
Glen Stark72d51e02016-06-08 01:23:32 +0200521 break;
522 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100523 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200524 break;
525 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100526 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200527 break;
528 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100529 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200530 break;
531 case 'L':
532 // printf produces garbage when 'L' is omitted for long double, no
533 // need to do the same.
534 break;
535 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700536 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100537 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200538 }
539
540 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800541 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700542 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800543 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200544 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700545 switch (spec.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800546 case 'i':
547 case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700548 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200549 break;
550 case 'c':
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800551 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
552 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200553 break;
554 }
555 }
556
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700557 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200558
559 // Format argument.
ToolsDevler2b415b72019-04-11 11:05:55 +0200560 visit_format_arg(ArgFormatter(out, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200561 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100562 return std::copy(start, it, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200563}
Glen Stark72d51e02016-06-08 01:23:32 +0200564
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800565template <typename Buffer> struct basic_printf_context_t {
566 typedef basic_printf_context<std::back_insert_iterator<Buffer>,
567 typename Buffer::value_type>
568 type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800569};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800570
Victor Zverovich28083952019-04-07 10:05:49 -0700571typedef basic_printf_context_t<internal::buffer<char>>::type printf_context;
572typedef basic_printf_context_t<internal::buffer<wchar_t>>::type wprintf_context;
tnovotnye37d6a92018-11-22 22:57:07 +0100573
574typedef basic_format_args<printf_context> printf_args;
575typedef basic_format_args<wprintf_context> wprintf_args;
576
577/**
578 \rst
579 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800580 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100581 \endrst
582 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800583template <typename... Args>
584inline format_arg_store<printf_context, Args...> make_printf_args(
585 const Args&... args) {
586 return {args...};
587}
tnovotnye37d6a92018-11-22 22:57:07 +0100588
589/**
590 \rst
591 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800592 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100593 \endrst
594 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800595template <typename... Args>
596inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
597 const Args&... args) {
598 return {args...};
599}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800600
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700601template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800602inline std::basic_string<Char> vsprintf(
603 const S& format,
604 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700605 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800606 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200607 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700608 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500609 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700610}
611
Glen Stark72d51e02016-06-08 01:23:32 +0200612/**
613 \rst
614 Formats arguments and returns the result as a string.
615
616 **Example**::
617
618 std::string message = fmt::sprintf("The answer is %d", 42);
619 \endrst
620*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700621template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700622 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
623inline std::basic_string<Char> sprintf(const S& format, const Args&... args) {
624 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800625 format_arg_store<context, Args...> as{args...};
626 return vsprintf(to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700627}
628
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700629template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800630inline int vfprintf(
631 std::FILE* f, const S& format,
632 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700633 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800634 args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200635 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700636 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800637 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800638 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
639 ? -1
640 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800641}
Glen Stark72d51e02016-06-08 01:23:32 +0200642
643/**
644 \rst
645 Prints formatted data to the file *f*.
646
647 **Example**::
648
649 fmt::fprintf(stderr, "Don't %s!", "panic");
650 \endrst
651 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700652template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700653 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700654inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700655 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800656 format_arg_store<context, Args...> as{args...};
657 return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700658}
659
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700660template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800661inline int vprintf(
662 const S& format,
663 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700664 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800665 args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700666 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200667}
668
Glen Stark72d51e02016-06-08 01:23:32 +0200669/**
670 \rst
671 Prints formatted data to ``stdout``.
672
673 **Example**::
674
675 fmt::printf("Elapsed time: %.2f seconds", 1.23);
676 \endrst
677 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700678template <typename S, typename... Args,
679 FMT_ENABLE_IF(internal::is_string<S>::value)>
680inline int printf(const S& format_str, const Args&... args) {
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700681 using buffer = internal::buffer<char_t<S>>;
682 using context = typename basic_printf_context_t<buffer>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800683 format_arg_store<context, Args...> as{args...};
684 return vprintf(to_string_view(format_str), basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200685}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700686
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700687template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800688inline int vfprintf(
689 std::basic_ostream<Char>& os, const S& format,
690 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700691 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800692 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200693 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700694 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200695 internal::write(os, buffer);
696 return static_cast<int>(buffer.size());
697}
698
ToolsDevler2b415b72019-04-11 11:05:55 +0200699/** Formats arguments and writes the output to the range. */
700template <typename ArgFormatter, typename Char,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700701 typename Context =
702 basic_printf_context<typename ArgFormatter::iterator, Char>>
ToolsDevler2b415b72019-04-11 11:05:55 +0200703typename ArgFormatter::iterator vprintf(internal::buffer<Char>& out,
704 basic_string_view<Char> format_str,
705 basic_format_args<Context> args) {
706 typename ArgFormatter::iterator iter(out);
707 Context(iter, format_str, args).template format<ArgFormatter>();
708 return iter;
709}
710
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700711/**
712 \rst
713 Prints formatted data to the stream *os*.
714
715 **Example**::
716
Victor Zverovich40232912018-03-04 09:55:17 -0800717 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700718 \endrst
719 */
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700720template <typename S, typename... Args, typename Char = char_t<S>>
721inline int fprintf(std::basic_ostream<Char>& os, const S& format_str,
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700722 const Args&... args) {
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700723 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800724 format_arg_store<context, Args...> as{args...};
Daniela Engert2c81c852018-10-08 20:14:39 +0200725 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200726 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200727}
Victor Zverovich838400d2018-05-12 08:33:51 -0700728FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200729
730#endif // FMT_PRINTF_H_