blob: a0c2d14a593d455015c022c4af2c2e2ce8616fd6 [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 Zverovich3f75e2b2019-08-28 06:18:34 -0700371 template <typename ArgFormatter = printf_arg_formatter<buffer_range<Char>>>
Daniela Engertb0cde862019-01-01 11:45:56 +0100372 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200373};
374
ToolsDevler2b415b72019-04-11 11:05:55 +0200375template <typename OutputIt, typename Char>
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700376void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& specs,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700377 const Char*& it,
378 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100379 for (; it != end; ++it) {
380 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800381 case '-':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700382 specs.align = align::left;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800383 break;
384 case '+':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700385 specs.sign = sign::plus;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800386 break;
387 case '0':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700388 specs.fill[0] = '0';
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800389 break;
390 case ' ':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700391 specs.sign = sign::space;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800392 break;
393 case '#':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700394 specs.alt = true;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800395 break;
396 default:
397 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200398 }
399 }
400}
401
ToolsDevler2b415b72019-04-11 11:05:55 +0200402template <typename OutputIt, typename Char>
403typename basic_printf_context<OutputIt, Char>::format_arg
404basic_printf_context<OutputIt, Char>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800405 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800406 arg_index = parse_ctx_.next_arg_id();
407 else
408 parse_ctx_.check_arg_id(--arg_index);
409 return internal::get_arg(*this, arg_index);
Glen Stark72d51e02016-06-08 01:23:32 +0200410}
411
ToolsDevler2b415b72019-04-11 11:05:55 +0200412template <typename OutputIt, typename Char>
413unsigned basic_printf_context<OutputIt, Char>::parse_header(
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700414 const Char*& it, const Char* end, format_specs& specs) {
Glen Stark72d51e02016-06-08 01:23:32 +0200415 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800416 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200417 if (c >= '0' && c <= '9') {
418 // Parse an argument index (if followed by '$') or a width possibly
419 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700420 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100421 unsigned value = parse_nonnegative_int(it, end, eh);
422 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700423 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200424 arg_index = value;
425 } else {
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700426 if (c == '0') specs.fill[0] = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200427 if (value != 0) {
428 // Nonzero value means that we parsed width and don't need to
429 // parse it or flags again, so return now.
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700430 specs.width = value;
Glen Stark72d51e02016-06-08 01:23:32 +0200431 return arg_index;
432 }
433 }
434 }
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700435 parse_flags(specs, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200436 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100437 if (it != end) {
438 if (*it >= '0' && *it <= '9') {
439 internal::error_handler eh;
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700440 specs.width = parse_nonnegative_int(it, end, eh);
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100441 } else if (*it == '*') {
442 ++it;
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700443 specs.width = visit_format_arg(
444 internal::printf_width_handler<char_type>(specs), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100445 }
Glen Stark72d51e02016-06-08 01:23:32 +0200446 }
447 return arg_index;
448}
449
ToolsDevler2b415b72019-04-11 11:05:55 +0200450template <typename OutputIt, typename Char>
451template <typename ArgFormatter>
452OutputIt basic_printf_context<OutputIt, Char>::format() {
Daniela Engertb0cde862019-01-01 11:45:56 +0100453 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800454 const Char* start = parse_ctx_.begin();
455 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700456 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100457 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800458 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200459 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100460 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100461 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700462 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200463 continue;
464 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100465 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200466
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700467 format_specs specs;
468 specs.align = align::right;
Glen Stark72d51e02016-06-08 01:23:32 +0200469
470 // Parse argument index, flags and width.
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700471 unsigned arg_index = parse_header(it, end, specs);
Glen Stark72d51e02016-06-08 01:23:32 +0200472
473 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100474 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700475 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100476 c = it != end ? *it : 0;
477 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700478 internal::error_handler eh;
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700479 specs.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100480 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700481 ++it;
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700482 specs.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100483 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800484 } else {
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700485 specs.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200486 }
487 }
488
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100489 format_arg arg = get_arg(arg_index);
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700490 if (specs.alt && visit_format_arg(internal::is_zero_int(), arg))
491 specs.alt = false;
492 if (specs.fill[0] == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800493 if (arg.is_arithmetic())
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700494 specs.align = align::numeric;
Glen Stark72d51e02016-06-08 01:23:32 +0200495 else
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700496 specs.fill[0] = ' '; // Ignore '0' flag for non-numeric types.
Glen Stark72d51e02016-06-08 01:23:32 +0200497 }
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 Zverovich5488d0b2019-07-07 06:39:20 -0700542 specs.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 Zverovich5488d0b2019-07-07 06:39:20 -0700545 switch (specs.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800546 case 'i':
547 case 'u':
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700548 specs.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.
Victor Zverovich5488d0b2019-07-07 06:39:20 -0700560 visit_format_arg(ArgFormatter(out, specs, *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 Zverovichdd8cc8b2019-07-09 11:50:16 -0700565template <typename Char>
566using basic_printf_context_t =
567 basic_printf_context<std::back_insert_iterator<internal::buffer<Char>>,
568 Char>;
Victor Zverovich217e7c72018-01-14 07:19:23 -0800569
Victor Zverovichf6f04152019-07-07 16:43:38 -0700570using printf_context = basic_printf_context_t<char>;
571using wprintf_context = basic_printf_context_t<wchar_t>;
tnovotnye37d6a92018-11-22 22:57:07 +0100572
Victor Zverovichf6f04152019-07-07 16:43:38 -0700573using printf_args = basic_format_args<printf_context>;
574using wprintf_args = basic_format_args<wprintf_context>;
tnovotnye37d6a92018-11-22 22:57:07 +0100575
576/**
577 \rst
578 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800579 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100580 \endrst
581 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800582template <typename... Args>
583inline format_arg_store<printf_context, Args...> make_printf_args(
584 const Args&... args) {
585 return {args...};
586}
tnovotnye37d6a92018-11-22 22:57:07 +0100587
588/**
589 \rst
590 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800591 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100592 \endrst
593 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800594template <typename... Args>
595inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
596 const Args&... args) {
597 return {args...};
598}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800599
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700600template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800601inline std::basic_string<Char> vsprintf(
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700602 const S& format, basic_format_args<basic_printf_context_t<Char>> args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200603 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700604 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500605 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700606}
607
Glen Stark72d51e02016-06-08 01:23:32 +0200608/**
609 \rst
610 Formats arguments and returns the result as a string.
611
612 **Example**::
613
614 std::string message = fmt::sprintf("The answer is %d", 42);
615 \endrst
616*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700617template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700618 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
619inline std::basic_string<Char> sprintf(const S& format, const Args&... args) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700620 using context = basic_printf_context_t<Char>;
621 return vsprintf(to_string_view(format), {make_format_args<context>(args...)});
Victor Zverovich0028ce52016-08-26 17:23:13 -0700622}
623
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700624template <typename S, typename Char = char_t<S>>
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700625inline int vfprintf(std::FILE* f, const S& format,
626 basic_format_args<basic_printf_context_t<Char>> args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200627 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700628 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800629 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800630 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
631 ? -1
632 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800633}
Glen Stark72d51e02016-06-08 01:23:32 +0200634
635/**
636 \rst
637 Prints formatted data to the file *f*.
638
639 **Example**::
640
641 fmt::fprintf(stderr, "Don't %s!", "panic");
642 \endrst
643 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700644template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700645 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700646inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700647 using context = basic_printf_context_t<Char>;
648 return vfprintf(f, to_string_view(format),
649 {make_format_args<context>(args...)});
Victor Zverovich0028ce52016-08-26 17:23:13 -0700650}
651
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700652template <typename S, typename Char = char_t<S>>
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700653inline int vprintf(const S& format,
654 basic_format_args<basic_printf_context_t<Char>> args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700655 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200656}
657
Glen Stark72d51e02016-06-08 01:23:32 +0200658/**
659 \rst
660 Prints formatted data to ``stdout``.
661
662 **Example**::
663
664 fmt::printf("Elapsed time: %.2f seconds", 1.23);
665 \endrst
666 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700667template <typename S, typename... Args,
668 FMT_ENABLE_IF(internal::is_string<S>::value)>
669inline int printf(const S& format_str, const Args&... args) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700670 using context = basic_printf_context_t<char_t<S>>;
671 return vprintf(to_string_view(format_str),
672 {make_format_args<context>(args...)});
Glen Stark72d51e02016-06-08 01:23:32 +0200673}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700674
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700675template <typename S, typename Char = char_t<S>>
Victor Zverovichdd8cc8b2019-07-09 11:50:16 -0700676inline int vfprintf(std::basic_ostream<Char>& os, const S& format,
677 basic_format_args<basic_printf_context_t<Char>> args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200678 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700679 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200680 internal::write(os, buffer);
681 return static_cast<int>(buffer.size());
682}
683
ToolsDevler2b415b72019-04-11 11:05:55 +0200684/** Formats arguments and writes the output to the range. */
685template <typename ArgFormatter, typename Char,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700686 typename Context =
687 basic_printf_context<typename ArgFormatter::iterator, Char>>
ToolsDevler2b415b72019-04-11 11:05:55 +0200688typename ArgFormatter::iterator vprintf(internal::buffer<Char>& out,
689 basic_string_view<Char> format_str,
690 basic_format_args<Context> args) {
691 typename ArgFormatter::iterator iter(out);
692 Context(iter, format_str, args).template format<ArgFormatter>();
693 return iter;
694}
695
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700696/**
697 \rst
698 Prints formatted data to the stream *os*.
699
700 **Example**::
701
Victor Zverovich40232912018-03-04 09:55:17 -0800702 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700703 \endrst
704 */
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700705template <typename S, typename... Args, typename Char = char_t<S>>
706inline int fprintf(std::basic_ostream<Char>& os, const S& format_str,
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700707 const Args&... args) {
Victor Zverovichf6f04152019-07-07 16:43:38 -0700708 using context = basic_printf_context_t<Char>;
Daniela Engert2c81c852018-10-08 20:14:39 +0200709 return vfprintf(os, to_string_view(format_str),
Victor Zverovichf6f04152019-07-07 16:43:38 -0700710 {make_format_args<context>(args...)});
Daniela Engert2570f1a2018-04-26 20:32:14 +0200711}
Victor Zverovich838400d2018-05-12 08:33:51 -0700712FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200713
714#endif // FMT_PRINTF_H_