blob: e540a55b601de9342c2750d6c836b88e1d5b21f3 [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 Zverovich58b6f8d2019-01-12 18:27:38 -080041class printf_precision_handler : public function<int> {
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"));
Glen Stark72d51e02016-06-08 01:23:32 +020047 return static_cast<int>(value);
48 }
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 Zverovich58b6f8d2019-01-12 18:27:38 -080058class is_zero_int : public function<bool> {
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 Zverovichd705d512016-12-29 09:07:39 -080075template <typename T, typename Context>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080076class arg_converter : public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +020077 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080078 typedef typename Context::char_type Char;
79
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080080 basic_format_arg<Context>& arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080081 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020082
Glen Stark72d51e02016-06-08 01:23:32 +020083 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080084 arg_converter(basic_format_arg<Context>& arg, Char type)
85 : arg_(arg), type_(type) {}
Glen Stark72d51e02016-06-08 01:23:32 +020086
Victor Zverovich751ff642016-11-19 08:40:24 -080087 void operator()(bool value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080088 if (type_ != 's') operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020089 }
90
Victor Zverovichc21c6b82019-03-16 12:58:18 -070091 template <typename U, FMT_ENABLE_IF(std::is_integral<U>::value)>
92 void operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020093 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080094 typedef typename std::conditional<std::is_same<T, void>::value, U, T>::type
95 TargetType;
Victor Zverovich5013c152018-02-10 06:52:46 -080096 if (const_check(sizeof(TargetType) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +020097 // Extra casts are used to silence warnings.
98 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -080099 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800100 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200101 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700102 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800103 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800104 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200105 }
106 } else {
107 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200108 // glibc's printf doesn't sign extend arguments of smaller types:
109 // std::printf("%lld", -42); // prints "4294967254"
110 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700111 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200112 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800113 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800114 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200115 }
116 }
117 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800118
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700119 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)>
120 void operator()(U) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200121};
122
Victor Zverovich751ff642016-11-19 08:40:24 -0800123// Converts an integer argument to T for printf, if T is an integral type.
124// If T is void, the argument is converted to corresponding signed or unsigned
125// type depending on the type specifier: 'd' and 'i' - signed, other -
126// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800127template <typename T, typename Context, typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800128void convert_arg(basic_format_arg<Context>& arg, Char type) {
Victor Zverovichf5480632018-10-05 07:15:41 -0700129 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800130}
131
Glen Stark72d51e02016-06-08 01:23:32 +0200132// Converts an integer argument to char for printf.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800133template <typename Context> class char_converter : public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200134 private:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800135 basic_format_arg<Context>& arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200136
Glen Stark72d51e02016-06-08 01:23:32 +0200137 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800138 explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200139
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700140 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
141 void operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200142 typedef typename Context::char_type Char;
143 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200144 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800145
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700146 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
147 void operator()(T) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200148};
149
150// Checks if an argument is a valid printf width specifier and sets
151// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000152template <typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800153class printf_width_handler : public function<unsigned> {
Glen Stark72d51e02016-06-08 01:23:32 +0200154 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000155 typedef basic_format_specs<Char> format_specs;
156
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800157 format_specs& spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200158
Glen Stark72d51e02016-06-08 01:23:32 +0200159 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800160 explicit printf_width_handler(format_specs& spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200161
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700162 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
163 unsigned operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800164 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200165 UnsignedType width = static_cast<UnsignedType>(value);
166 if (internal::is_negative(value)) {
167 spec_.align_ = ALIGN_LEFT;
168 width = 0 - width;
169 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700170 unsigned int_max = std::numeric_limits<int>::max();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800171 if (width > int_max) FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200172 return static_cast<unsigned>(width);
173 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800174
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700175 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
176 unsigned operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800177 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200178 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800179 }
Glen Stark72d51e02016-06-08 01:23:32 +0200180};
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800181
182template <typename Char, typename Context>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800183void printf(basic_buffer<Char>& buf, basic_string_view<Char> format,
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800184 basic_format_args<Context> args) {
185 Context(std::back_inserter(buf), format, args).format();
186}
Daniela Engertb0cde862019-01-01 11:45:56 +0100187
188template <typename OutputIt, typename Char, typename Context>
189internal::truncating_iterator<OutputIt> printf(
190 internal::truncating_iterator<OutputIt> it, basic_string_view<Char> format,
191 basic_format_args<Context> args) {
192 return Context(it, format, args).format();
193}
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700194} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200195
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800196using internal::printf; // For printing into memory_buffer.
197
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800198template <typename Range> class printf_arg_formatter;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800199
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800200template <typename OutputIt, typename Char,
201 typename ArgFormatter = printf_arg_formatter<
202 back_insert_range<internal::basic_buffer<Char>>>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800203class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800204
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700205/**
206 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800207 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700208 \endrst
209 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800210template <typename Range>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800211class printf_arg_formatter
212 : public internal::function<
213 typename internal::arg_formatter_base<Range>::iterator>,
214 public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200215 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800216 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800217 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800218 typedef internal::arg_formatter_base<Range> base;
Daniela Engertb0cde862019-01-01 11:45:56 +0100219 typedef basic_printf_context<iterator, char_type, printf_arg_formatter>
220 context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800221
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800222 context_type& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800223
Daniela Engert2570f1a2018-04-26 20:32:14 +0200224 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700225 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200226 this->write("(nil)");
227 }
228
Daniela Engert2570f1a2018-04-26 20:32:14 +0200229 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700230 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200231 this->write(L"(nil)");
232 }
233
Glen Stark72d51e02016-06-08 01:23:32 +0200234 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800235 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000236
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700237 /**
238 \rst
239 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500240 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700241 specifier information for standard argument types.
242 \endrst
243 */
Daniela Engertb0cde862019-01-01 11:45:56 +0100244 printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800245 : base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200246
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700247 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
248 iterator operator()(T value) {
Victor Zveroviche928b672018-07-04 11:36:38 -0700249 // MSVC2013 fails to compile separate overloads for bool and char_type so
250 // use std::is_same instead.
251 if (std::is_same<T, bool>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800252 format_specs& fmt_spec = *this->spec();
253 if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700254 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700255 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700256 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800257 format_specs& fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700258 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700259 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700260 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700261 fmt_spec.align_ = ALIGN_RIGHT;
262 return base::operator()(value);
263 } else {
264 return base::operator()(value);
265 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000266 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200267 }
268
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700269 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
270 iterator operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000271 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200272 }
273
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700274 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800275 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200276 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800277 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700278 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200279 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200280 else
281 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000282 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200283 }
284
Daniela Engert2570f1a2018-04-26 20:32:14 +0200285 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800286 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200287 if (value)
288 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700289 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200290 write_null_pointer(char_type());
291 else
292 this->write(L"(null)");
293 return this->out();
294 }
295
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700296 iterator operator()(basic_string_view<char_type> value) {
297 return base::operator()(value);
298 }
299
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800300 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700301
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700302 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800303 iterator operator()(const void* value) {
304 if (value) return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700305 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200306 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000307 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200308 }
309
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700310 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700311 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800312 handle.format(context_.parse_context(), context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000313 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700314 }
315};
316
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800317template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700318 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800319 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
320 return ctx.begin();
321 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700322
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800323 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800324 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700325 internal::format_value(internal::get_container(ctx.out()), value);
326 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200327 }
328};
329
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700330/** This template formats data and writes the output to a writer. */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800331template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800332class basic_printf_context {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700333 public:
334 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800335 typedef Char char_type;
Victor Zveroviche4572e52019-02-10 06:34:47 -0800336 typedef basic_format_arg<basic_printf_context> format_arg;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700337
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800338 template <typename T> struct formatter_type {
339 typedef printf_formatter<T> type;
340 };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700341
Glen Stark72d51e02016-06-08 01:23:32 +0200342 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800343 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700344
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800345 OutputIt out_;
346 basic_format_args<basic_printf_context> args_;
347 basic_parse_context<Char> parse_ctx_;
348
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800349 static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200350
351 // Returns the argument with specified index or, if arg_index is equal
352 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100353 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200354
355 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800356 unsigned parse_header(const Char*& it, const Char* end, format_specs& spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200357
358 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700359 /**
360 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800361 Constructs a ``printf_context`` object. References to the arguments and
362 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700363 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700364 \endrst
365 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800366 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800367 basic_format_args<basic_printf_context> args)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800368 : out_(out), args_(args), parse_ctx_(format_str) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700369
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800370 OutputIt out() { return out_; }
371 void advance_to(OutputIt it) { out_ = it; }
372
373 format_arg arg(unsigned id) const { return args_.get(id); }
374
375 basic_parse_context<Char>& parse_context() { return parse_ctx_; }
376
377 FMT_CONSTEXPR void on_error(const char* message) {
378 parse_ctx_.on_error(message);
379 }
Victor Zverovich9998f662016-11-06 16:11:24 -0800380
Victor Zverovichc0954452018-01-06 09:09:50 -0800381 /** Formats stored arguments and writes the output to the range. */
Daniela Engertb0cde862019-01-01 11:45:56 +0100382 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200383};
384
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800385template <typename OutputIt, typename Char, typename AF>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800386void basic_printf_context<OutputIt, Char, AF>::parse_flags(format_specs& spec,
387 const Char*& it,
388 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100389 for (; it != end; ++it) {
390 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800391 case '-':
392 spec.align_ = ALIGN_LEFT;
393 break;
394 case '+':
395 spec.flags |= SIGN_FLAG | PLUS_FLAG;
396 break;
397 case '0':
398 spec.fill_ = '0';
399 break;
400 case ' ':
401 spec.flags |= SIGN_FLAG;
402 break;
403 case '#':
404 spec.flags |= HASH_FLAG;
405 break;
406 default:
407 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200408 }
409 }
410}
411
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800412template <typename OutputIt, typename Char, typename AF>
413typename basic_printf_context<OutputIt, Char, AF>::format_arg
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800414basic_printf_context<OutputIt, Char, AF>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800415 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800416 arg_index = parse_ctx_.next_arg_id();
417 else
418 parse_ctx_.check_arg_id(--arg_index);
419 return internal::get_arg(*this, arg_index);
Glen Stark72d51e02016-06-08 01:23:32 +0200420}
421
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800422template <typename OutputIt, typename Char, typename AF>
423unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800424 const Char*& it, const Char* end, format_specs& spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200425 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800426 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200427 if (c >= '0' && c <= '9') {
428 // Parse an argument index (if followed by '$') or a width possibly
429 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700430 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100431 unsigned value = parse_nonnegative_int(it, end, eh);
432 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700433 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200434 arg_index = value;
435 } else {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800436 if (c == '0') spec.fill_ = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200437 if (value != 0) {
438 // Nonzero value means that we parsed width and don't need to
439 // parse it or flags again, so return now.
440 spec.width_ = value;
441 return arg_index;
442 }
443 }
444 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100445 parse_flags(spec, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200446 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100447 if (it != end) {
448 if (*it >= '0' && *it <= '9') {
449 internal::error_handler eh;
450 spec.width_ = parse_nonnegative_int(it, end, eh);
451 } else if (*it == '*') {
452 ++it;
453 spec.width_ = visit_format_arg(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800454 internal::printf_width_handler<char_type>(spec), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100455 }
Glen Stark72d51e02016-06-08 01:23:32 +0200456 }
457 return arg_index;
458}
459
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800460template <typename OutputIt, typename Char, typename AF>
Daniela Engertb0cde862019-01-01 11:45:56 +0100461OutputIt basic_printf_context<OutputIt, Char, AF>::format() {
462 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800463 const Char* start = parse_ctx_.begin();
464 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700465 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100466 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800467 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200468 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100469 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100470 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700471 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200472 continue;
473 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100474 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200475
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000476 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200477 spec.align_ = ALIGN_RIGHT;
478
479 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100480 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200481
482 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100483 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700484 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100485 c = it != end ? *it : 0;
486 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700487 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100488 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
489 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700490 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700491 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100492 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800493 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700494 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200495 }
496 }
497
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100498 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700499 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800500 spec.flags = static_cast<uint_least8_t>(
501 spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200502 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800503 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200504 spec.align_ = ALIGN_NUMERIC;
505 else
506 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
507 }
508
509 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100510 c = it != end ? *it++ : 0;
511 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800512 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100513 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200514 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100515 if (t == 'h') {
516 ++it;
517 t = it != end ? *it : 0;
518 convert_arg<signed char>(arg, t);
519 } else {
520 convert_arg<short>(arg, t);
521 }
Glen Stark72d51e02016-06-08 01:23:32 +0200522 break;
523 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100524 if (t == 'l') {
525 ++it;
526 t = it != end ? *it : 0;
527 convert_arg<long long>(arg, t);
528 } else {
529 convert_arg<long>(arg, t);
530 }
Glen Stark72d51e02016-06-08 01:23:32 +0200531 break;
532 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100533 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200534 break;
535 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100536 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200537 break;
538 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100539 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200540 break;
541 case 'L':
542 // printf produces garbage when 'L' is omitted for long double, no
543 // need to do the same.
544 break;
545 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700546 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100547 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200548 }
549
550 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800551 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700552 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800553 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200554 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700555 switch (spec.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800556 case 'i':
557 case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700558 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200559 break;
560 case 'c':
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800561 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
562 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200563 break;
564 }
565 }
566
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700567 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200568
569 // Format argument.
Daniela Engertb0cde862019-01-01 11:45:56 +0100570 visit_format_arg(AF(out, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200571 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100572 return std::copy(start, it, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200573}
Glen Stark72d51e02016-06-08 01:23:32 +0200574
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800575template <typename Buffer> struct basic_printf_context_t {
576 typedef basic_printf_context<std::back_insert_iterator<Buffer>,
577 typename Buffer::value_type>
578 type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800579};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800580
tnovotnye37d6a92018-11-22 22:57:07 +0100581typedef basic_printf_context_t<internal::buffer>::type printf_context;
582typedef basic_printf_context_t<internal::wbuffer>::type wprintf_context;
583
584typedef basic_format_args<printf_context> printf_args;
585typedef basic_format_args<wprintf_context> wprintf_args;
586
Daniela Engertb0cde862019-01-01 11:45:56 +0100587template <typename OutputIt, typename Char = typename OutputIt::value_type>
588struct basic_printf_n_context_t {
589 typedef fmt::internal::truncating_iterator<OutputIt> OutputIter;
590 typedef output_range<OutputIter, Char> Range;
591 typedef basic_printf_context<OutputIter, Char, printf_arg_formatter<Range>>
592 type;
593};
594
tnovotnye37d6a92018-11-22 22:57:07 +0100595/**
596 \rst
597 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800598 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100599 \endrst
600 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800601template <typename... Args>
602inline format_arg_store<printf_context, Args...> make_printf_args(
603 const Args&... args) {
604 return {args...};
605}
tnovotnye37d6a92018-11-22 22:57:07 +0100606
607/**
608 \rst
609 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800610 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100611 \endrst
612 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800613template <typename... Args>
614inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
615 const Args&... args) {
616 return {args...};
617}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800618
Victor Zverovich0a96c032018-10-25 07:20:02 -0700619template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800620inline std::basic_string<Char> vsprintf(
621 const S& format,
622 basic_format_args<
623 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
624 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200625 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700626 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500627 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700628}
629
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700630template <typename OutputIt, typename S, typename Char = FMT_CHAR(S),
631 FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value)>
632inline format_to_n_result<OutputIt> vsnprintf(
Daniela Engertb0cde862019-01-01 11:45:56 +0100633 OutputIt out, std::size_t n, const S& format,
634 basic_format_args<typename basic_printf_n_context_t<OutputIt, Char>::type>
635 args) {
636 typedef internal::truncating_iterator<OutputIt> It;
637 auto it = printf(It(out, n), to_string_view(format), args);
638 return {it.base(), it.count()};
639}
640
Glen Stark72d51e02016-06-08 01:23:32 +0200641/**
642 \rst
643 Formats arguments and returns the result as a string.
644
645 **Example**::
646
647 std::string message = fmt::sprintf("The answer is %d", 42);
648 \endrst
649*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700650template <typename S, typename... Args,
651 FMT_ENABLE_IF(internal::is_string<S>::value)>
652inline std::basic_string<FMT_CHAR(S)> sprintf(const S& format,
653 const Args&... args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700654 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200655 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100656 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800657 format_arg_store<context, Args...> as{args...};
658 return vsprintf(to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700659}
660
Daniela Engertb0cde862019-01-01 11:45:56 +0100661/**
662 \rst
663 Formats arguments for up to ``n`` characters stored through output iterator
664 ``out``. The function returns the updated iterator and the untruncated amount
665 of characters.
666
667 **Example**::
668 std::vector<char> out;
669
670 typedef fmt::format_to_n_result<
671 std::back_insert_iterator<std::vector<char>>> res;
672 res Res = fmt::snprintf(std::back_inserter(out), 5, "The answer is %d", 42);
673 \endrst
674*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700675template <typename OutputIt, typename S, typename... Args,
676 FMT_ENABLE_IF(internal::is_string<S>::value&&
677 internal::is_output_iterator<OutputIt>::value)>
678inline format_to_n_result<OutputIt> snprintf(OutputIt out, std::size_t n,
679 const S& format,
680 const Args&... args) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100681 internal::check_format_string<Args...>(format);
682 typedef FMT_CHAR(S) Char;
683 typedef typename basic_printf_n_context_t<OutputIt, Char>::type context;
684 format_arg_store<context, Args...> as{args...};
685 return vsnprintf(out, n, to_string_view(format),
686 basic_format_args<context>(as));
687}
688
Victor Zverovich0a96c032018-10-25 07:20:02 -0700689template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800690inline int vfprintf(
691 std::FILE* f, const S& format,
692 basic_format_args<
693 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
694 args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200695 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700696 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800697 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800698 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
699 ? -1
700 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800701}
Glen Stark72d51e02016-06-08 01:23:32 +0200702
703/**
704 \rst
705 Prints formatted data to the file *f*.
706
707 **Example**::
708
709 fmt::fprintf(stderr, "Don't %s!", "panic");
710 \endrst
711 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700712template <typename S, typename... Args,
713 FMT_ENABLE_IF(internal::is_string<S>::value)>
714inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700715 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200716 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100717 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800718 format_arg_store<context, Args...> as{args...};
719 return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700720}
721
Victor Zverovich0a96c032018-10-25 07:20:02 -0700722template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800723inline int vprintf(
724 const S& format,
725 basic_format_args<
726 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
727 args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700728 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200729}
730
Glen Stark72d51e02016-06-08 01:23:32 +0200731/**
732 \rst
733 Prints formatted data to ``stdout``.
734
735 **Example**::
736
737 fmt::printf("Elapsed time: %.2f seconds", 1.23);
738 \endrst
739 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700740template <typename S, typename... Args,
741 FMT_ENABLE_IF(internal::is_string<S>::value)>
742inline int printf(const S& format_str, const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200743 internal::check_format_string<Args...>(format_str);
744 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100745 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800746 format_arg_store<context, Args...> as{args...};
747 return vprintf(to_string_view(format_str), basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200748}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700749
Victor Zverovich0a96c032018-10-25 07:20:02 -0700750template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800751inline int vfprintf(
752 std::basic_ostream<Char>& os, const S& format,
753 basic_format_args<
754 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
755 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200756 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700757 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200758 internal::write(os, buffer);
759 return static_cast<int>(buffer.size());
760}
761
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700762/**
763 \rst
764 Prints formatted data to the stream *os*.
765
766 **Example**::
767
Victor Zverovich40232912018-03-04 09:55:17 -0800768 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700769 \endrst
770 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700771template <typename S, typename... Args,
772 FMT_ENABLE_IF(internal::is_string<S>::value)>
773inline int fprintf(std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str,
774 const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200775 internal::check_format_string<Args...>(format_str);
776 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100777 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800778 format_arg_store<context, Args...> as{args...};
Daniela Engert2c81c852018-10-08 20:14:39 +0200779 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200780 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200781}
Victor Zverovich838400d2018-05-12 08:33:51 -0700782FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200783
784#endif // FMT_PRINTF_H_