blob: c803aa952bf78de96b97895a2fecb27a7746e10c [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 Zverovichf6f04152019-07-07 16:43:38 -070073template <> struct make_unsigned_or_bool<bool> { using type = bool; };
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 Zverovichf6f04152019-07-07 16:43:38 -070077 using char_type = typename Context::char_type;
Victor Zverovichd705d512016-12-29 09:07:39 -080078
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080079 basic_format_arg<Context>& arg_;
Victor Zverovichf6f04152019-07-07 16:43:38 -070080 char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020081
Glen Stark72d51e02016-06-08 01:23:32 +020082 public:
Victor Zverovichf6f04152019-07-07 16:43:38 -070083 arg_converter(basic_format_arg<Context>& arg, char_type type)
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080084 : 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 Zverovichf6f04152019-07-07 16:43:38 -0700100 using unsigned_type = typename make_unsigned_or_bool<target_type>::type;
Victor Zverovichd705d512016-12-29 09:07:39 -0800101 arg_ = internal::make_arg<Context>(
Victor Zverovichf6f04152019-07-07 16:43:38 -0700102 static_cast<unsigned>(static_cast<unsigned_type>(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) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700140 arg_ = internal::make_arg<Context>(
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700141 static_cast<typename Context::char_type>(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 Zverovichf6f04152019-07-07 16:43:38 -0700152 using format_specs = basic_format_specs<Char>;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000153
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700154 format_specs& specs_;
Glen Stark72d51e02016-06-08 01:23:32 +0200155
Glen Stark72d51e02016-06-08 01:23:32 +0200156 public:
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700157 explicit printf_width_handler(format_specs& specs) : specs_(specs) {}
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)) {
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700163 specs_.align = align::left;
Glen Stark72d51e02016-06-08 01:23:32 +0200164 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 Zverovichf6f04152019-07-07 16:43:38 -0700206 using iterator = typename Range::iterator;
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700207
Glen Stark72d51e02016-06-08 01:23:32 +0200208 private:
Victor Zverovichf6f04152019-07-07 16:43:38 -0700209 using char_type = typename Range::value_type;
210 using base = internal::arg_formatter_base<Range>;
211 using context_type = basic_printf_context<iterator, char_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 Zverovich5488d0b2019-07-07 06:39:20 -0700216 this->specs()->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 Zverovich5488d0b2019-07-07 06:39:20 -0700221 this->specs()->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 Zverovichf6f04152019-07-07 16:43:38 -0700226 using format_specs = typename base::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 Zverovich5488d0b2019-07-07 06:39:20 -0700231 *buffer* is a reference to the output buffer and *specs* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700232 specifier information for standard argument types.
233 \endrst
234 */
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700235 printf_arg_formatter(iterator iter, format_specs& specs, context_type& ctx)
236 : base(Range(iter), &specs, 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 Zverovich5488d0b2019-07-07 06:39:20 -0700243 format_specs& fmt_specs = *this->specs();
244 if (fmt_specs.type != 's') return base::operator()(value ? 1 : 0);
245 fmt_specs.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 Zverovich5488d0b2019-07-07 06:39:20 -0700248 format_specs& fmt_specs = *this->specs();
249 if (fmt_specs.type && fmt_specs.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700250 return (*this)(static_cast<int>(value));
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700251 fmt_specs.sign = sign::none;
252 fmt_specs.alt = false;
253 fmt_specs.align = align::right;
Victor Zveroviche928b672018-07-04 11:36:38 -0700254 return base::operator()(value);
255 } else {
256 return base::operator()(value);
257 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000258 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200259 }
260
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700261 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
262 iterator operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000263 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200264 }
265
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700266 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800267 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200268 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800269 base::operator()(value);
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700270 else if (this->specs()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200271 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200272 else
273 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000274 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200275 }
276
Daniela Engert2570f1a2018-04-26 20:32:14 +0200277 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800278 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200279 if (value)
280 base::operator()(value);
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700281 else if (this->specs()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200282 write_null_pointer(char_type());
283 else
284 this->write(L"(null)");
285 return this->out();
286 }
287
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700288 iterator operator()(basic_string_view<char_type> value) {
289 return base::operator()(value);
290 }
291
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800292 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700293
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700294 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800295 iterator operator()(const void* value) {
296 if (value) return base::operator()(value);
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700297 this->specs()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200298 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000299 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200300 }
301
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700302 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700303 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800304 handle.format(context_.parse_context(), context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000305 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700306 }
307};
308
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800309template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700310 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800311 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
312 return ctx.begin();
313 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700314
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800315 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800316 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700317 internal::format_value(internal::get_container(ctx.out()), value);
318 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200319 }
320};
321
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700322/** This template formats data and writes the output to a writer. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700323template <typename OutputIt, typename Char> class basic_printf_context {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700324 public:
325 /** The character type for the output. */
Victor Zverovichec665102019-06-02 16:04:17 -0700326 using char_type = Char;
327 using format_arg = basic_format_arg<basic_printf_context>;
328 template <typename T> using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700329
Glen Stark72d51e02016-06-08 01:23:32 +0200330 private:
Victor Zverovichf6f04152019-07-07 16:43:38 -0700331 using format_specs = basic_format_specs<char_type>;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700332
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800333 OutputIt out_;
334 basic_format_args<basic_printf_context> args_;
335 basic_parse_context<Char> parse_ctx_;
336
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700337 static void parse_flags(format_specs& specs, const Char*& it,
338 const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200339
340 // Returns the argument with specified index or, if arg_index is equal
341 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100342 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200343
344 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700345 unsigned parse_header(const Char*& it, const Char* end, format_specs& specs);
Glen Stark72d51e02016-06-08 01:23:32 +0200346
347 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700348 /**
349 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800350 Constructs a ``printf_context`` object. References to the arguments and
351 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700352 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700353 \endrst
354 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800355 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800356 basic_format_args<basic_printf_context> args)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800357 : out_(out), args_(args), parse_ctx_(format_str) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700358
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800359 OutputIt out() { return out_; }
360 void advance_to(OutputIt it) { out_ = it; }
361
362 format_arg arg(unsigned id) const { return args_.get(id); }
363
364 basic_parse_context<Char>& parse_context() { return parse_ctx_; }
365
366 FMT_CONSTEXPR void on_error(const char* message) {
367 parse_ctx_.on_error(message);
368 }
Victor Zverovich9998f662016-11-06 16:11:24 -0800369
Victor Zverovichc0954452018-01-06 09:09:50 -0800370 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700371 template <typename ArgFormatter =
Victor Zverovichf13906f2019-06-19 13:51:36 -0700372 printf_arg_formatter<internal::buffer_range<Char>>>
Daniela Engertb0cde862019-01-01 11:45:56 +0100373 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200374};
375
ToolsDevler2b415b72019-04-11 11:05:55 +0200376template <typename OutputIt, typename Char>
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700377void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& specs,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700378 const Char*& it,
379 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100380 for (; it != end; ++it) {
381 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800382 case '-':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700383 specs.align = align::left;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800384 break;
385 case '+':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700386 specs.sign = sign::plus;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800387 break;
388 case '0':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700389 specs.fill[0] = '0';
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800390 break;
391 case ' ':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700392 specs.sign = sign::space;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800393 break;
394 case '#':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700395 specs.alt = true;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800396 break;
397 default:
398 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200399 }
400 }
401}
402
ToolsDevler2b415b72019-04-11 11:05:55 +0200403template <typename OutputIt, typename Char>
404typename basic_printf_context<OutputIt, Char>::format_arg
405basic_printf_context<OutputIt, Char>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800406 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800407 arg_index = parse_ctx_.next_arg_id();
408 else
409 parse_ctx_.check_arg_id(--arg_index);
410 return internal::get_arg(*this, arg_index);
Glen Stark72d51e02016-06-08 01:23:32 +0200411}
412
ToolsDevler2b415b72019-04-11 11:05:55 +0200413template <typename OutputIt, typename Char>
414unsigned basic_printf_context<OutputIt, Char>::parse_header(
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700415 const Char*& it, const Char* end, format_specs& specs) {
Glen Stark72d51e02016-06-08 01:23:32 +0200416 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800417 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200418 if (c >= '0' && c <= '9') {
419 // Parse an argument index (if followed by '$') or a width possibly
420 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700421 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100422 unsigned value = parse_nonnegative_int(it, end, eh);
423 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700424 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200425 arg_index = value;
426 } else {
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700427 if (c == '0') specs.fill[0] = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200428 if (value != 0) {
429 // Nonzero value means that we parsed width and don't need to
430 // parse it or flags again, so return now.
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700431 specs.width = value;
Glen Stark72d51e02016-06-08 01:23:32 +0200432 return arg_index;
433 }
434 }
435 }
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700436 parse_flags(specs, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200437 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100438 if (it != end) {
439 if (*it >= '0' && *it <= '9') {
440 internal::error_handler eh;
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700441 specs.width = parse_nonnegative_int(it, end, eh);
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100442 } else if (*it == '*') {
443 ++it;
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700444 specs.width = visit_format_arg(
445 internal::printf_width_handler<char_type>(specs), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100446 }
Glen Stark72d51e02016-06-08 01:23:32 +0200447 }
448 return arg_index;
449}
450
ToolsDevler2b415b72019-04-11 11:05:55 +0200451template <typename OutputIt, typename Char>
452template <typename ArgFormatter>
453OutputIt basic_printf_context<OutputIt, Char>::format() {
Daniela Engertb0cde862019-01-01 11:45:56 +0100454 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800455 const Char* start = parse_ctx_.begin();
456 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700457 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100458 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800459 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200460 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100461 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100462 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700463 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200464 continue;
465 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100466 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200467
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700468 format_specs specs;
469 specs.align = align::right;
Glen Stark72d51e02016-06-08 01:23:32 +0200470
471 // Parse argument index, flags and width.
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700472 unsigned arg_index = parse_header(it, end, specs);
Glen Stark72d51e02016-06-08 01:23:32 +0200473
474 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100475 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700476 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100477 c = it != end ? *it : 0;
478 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700479 internal::error_handler eh;
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700480 specs.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100481 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700482 ++it;
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700483 specs.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100484 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800485 } else {
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700486 specs.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200487 }
488 }
489
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100490 format_arg arg = get_arg(arg_index);
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700491 if (specs.alt && visit_format_arg(internal::is_zero_int(), arg))
492 specs.alt = false;
493 if (specs.fill[0] == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800494 if (arg.is_arithmetic())
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700495 specs.align = align::numeric;
Glen Stark72d51e02016-06-08 01:23:32 +0200496 else
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700497 specs.fill[0] = ' '; // Ignore '0' flag for non-numeric types.
Glen Stark72d51e02016-06-08 01:23:32 +0200498 }
499
500 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100501 c = it != end ? *it++ : 0;
502 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800503 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100504 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200505 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100506 if (t == 'h') {
507 ++it;
508 t = it != end ? *it : 0;
509 convert_arg<signed char>(arg, t);
510 } else {
511 convert_arg<short>(arg, t);
512 }
Glen Stark72d51e02016-06-08 01:23:32 +0200513 break;
514 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100515 if (t == 'l') {
516 ++it;
517 t = it != end ? *it : 0;
518 convert_arg<long long>(arg, t);
519 } else {
520 convert_arg<long>(arg, t);
521 }
Glen Stark72d51e02016-06-08 01:23:32 +0200522 break;
523 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100524 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200525 break;
526 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100527 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200528 break;
529 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100530 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200531 break;
532 case 'L':
533 // printf produces garbage when 'L' is omitted for long double, no
534 // need to do the same.
535 break;
536 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700537 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100538 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200539 }
540
541 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800542 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700543 specs.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800544 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200545 // Normalize type.
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700546 switch (specs.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800547 case 'i':
548 case 'u':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700549 specs.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200550 break;
551 case 'c':
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800552 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
553 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200554 break;
555 }
556 }
557
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700558 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200559
560 // Format argument.
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700561 visit_format_arg(ArgFormatter(out, specs, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200562 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100563 return std::copy(start, it, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200564}
Glen Stark72d51e02016-06-08 01:23:32 +0200565
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700566template <typename Char>
567using basic_printf_context_t =
568 basic_printf_context<std::back_insert_iterator<internal::buffer<Char>>,
569 Char>;
Victor Zverovich217e7c72018-01-14 07:19:23 -0800570
Victor Zverovichf6f04152019-07-07 16:43:38 -0700571using printf_context = basic_printf_context_t<char>;
572using wprintf_context = basic_printf_context_t<wchar_t>;
tnovotnye37d6a92018-11-22 22:57:07 +0100573
Victor Zverovichf6f04152019-07-07 16:43:38 -0700574using printf_args = basic_format_args<printf_context>;
575using wprintf_args = basic_format_args<wprintf_context>;
tnovotnye37d6a92018-11-22 22:57:07 +0100576
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(
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700603 const S& format, basic_format_args<basic_printf_context_t<Char>> args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200604 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700605 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500606 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700607}
608
Glen Stark72d51e02016-06-08 01:23:32 +0200609/**
610 \rst
611 Formats arguments and returns the result as a string.
612
613 **Example**::
614
615 std::string message = fmt::sprintf("The answer is %d", 42);
616 \endrst
617*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700618template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700619 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
620inline std::basic_string<Char> sprintf(const S& format, const Args&... args) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700621 using context = basic_printf_context_t<Char>;
622 return vsprintf(to_string_view(format), {make_format_args<context>(args...)});
Victor Zverovich0028ce52016-08-26 17:23:13 -0700623}
624
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700625template <typename S, typename Char = char_t<S>>
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700626inline int vfprintf(std::FILE* f, const S& format,
627 basic_format_args<basic_printf_context_t<Char>> args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200628 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700629 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800630 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800631 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
632 ? -1
633 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800634}
Glen Stark72d51e02016-06-08 01:23:32 +0200635
636/**
637 \rst
638 Prints formatted data to the file *f*.
639
640 **Example**::
641
642 fmt::fprintf(stderr, "Don't %s!", "panic");
643 \endrst
644 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700645template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700646 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700647inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700648 using context = basic_printf_context_t<Char>;
649 return vfprintf(f, to_string_view(format),
650 {make_format_args<context>(args...)});
Victor Zverovich0028ce52016-08-26 17:23:13 -0700651}
652
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700653template <typename S, typename Char = char_t<S>>
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700654inline int vprintf(const S& format,
655 basic_format_args<basic_printf_context_t<Char>> args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700656 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200657}
658
Glen Stark72d51e02016-06-08 01:23:32 +0200659/**
660 \rst
661 Prints formatted data to ``stdout``.
662
663 **Example**::
664
665 fmt::printf("Elapsed time: %.2f seconds", 1.23);
666 \endrst
667 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700668template <typename S, typename... Args,
669 FMT_ENABLE_IF(internal::is_string<S>::value)>
670inline int printf(const S& format_str, const Args&... args) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700671 using context = basic_printf_context_t<char_t<S>>;
672 return vprintf(to_string_view(format_str),
673 {make_format_args<context>(args...)});
Glen Stark72d51e02016-06-08 01:23:32 +0200674}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700675
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700676template <typename S, typename Char = char_t<S>>
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700677inline int vfprintf(std::basic_ostream<Char>& os, const S& format,
678 basic_format_args<basic_printf_context_t<Char>> args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200679 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700680 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200681 internal::write(os, buffer);
682 return static_cast<int>(buffer.size());
683}
684
ToolsDevler2b415b72019-04-11 11:05:55 +0200685/** Formats arguments and writes the output to the range. */
686template <typename ArgFormatter, typename Char,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700687 typename Context =
688 basic_printf_context<typename ArgFormatter::iterator, Char>>
ToolsDevler2b415b72019-04-11 11:05:55 +0200689typename ArgFormatter::iterator vprintf(internal::buffer<Char>& out,
690 basic_string_view<Char> format_str,
691 basic_format_args<Context> args) {
692 typename ArgFormatter::iterator iter(out);
693 Context(iter, format_str, args).template format<ArgFormatter>();
694 return iter;
695}
696
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700697/**
698 \rst
699 Prints formatted data to the stream *os*.
700
701 **Example**::
702
Victor Zverovich40232912018-03-04 09:55:17 -0800703 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700704 \endrst
705 */
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700706template <typename S, typename... Args, typename Char = char_t<S>>
707inline int fprintf(std::basic_ostream<Char>& os, const S& format_str,
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700708 const Args&... args) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700709 using context = basic_printf_context_t<Char>;
Daniela Engert2c81c852018-10-08 20:14:39 +0200710 return vfprintf(os, to_string_view(format_str),
Victor Zverovichf6f04152019-07-07 16:43:38 -0700711 {make_format_args<context>(args...)});
Daniela Engert2570f1a2018-04-26 20:32:14 +0200712}
Victor Zverovich838400d2018-05-12 08:33:51 -0700713FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200714
715#endif // FMT_PRINTF_H_