blob: 606cea4887e385e7220a87ff7e67d2dcc0bfd0c9 [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 Zverovich86686392018-12-13 07:50:50 -080019// An iterator that produces a null terminator on *end. This simplifies parsing
20// and allows comparing the performance of processing a null-terminated string
21// vs string_view.
22template <typename Char>
23class null_terminating_iterator {
24 public:
25 typedef std::ptrdiff_t difference_type;
26 typedef Char value_type;
27 typedef const Char* pointer;
28 typedef const Char& reference;
29 typedef std::random_access_iterator_tag iterator_category;
30
31 null_terminating_iterator() : ptr_(0), end_(0) {}
32
33 FMT_CONSTEXPR null_terminating_iterator(const Char *ptr, const Char *end)
34 : ptr_(ptr), end_(end) {}
35
36 template <typename Range>
37 FMT_CONSTEXPR explicit null_terminating_iterator(const Range &r)
38 : ptr_(r.begin()), end_(r.end()) {}
39
40 FMT_CONSTEXPR null_terminating_iterator &operator=(const Char *ptr) {
41 assert(ptr <= end_);
42 ptr_ = ptr;
43 return *this;
44 }
45
46 FMT_CONSTEXPR Char operator*() const {
47 return ptr_ != end_ ? *ptr_ : Char();
48 }
49
50 FMT_CONSTEXPR null_terminating_iterator operator++() {
51 ++ptr_;
52 return *this;
53 }
54
55 FMT_CONSTEXPR null_terminating_iterator operator++(int) {
56 null_terminating_iterator result(*this);
57 ++ptr_;
58 return result;
59 }
60
61 FMT_CONSTEXPR null_terminating_iterator operator--() {
62 --ptr_;
63 return *this;
64 }
65
66 FMT_CONSTEXPR null_terminating_iterator operator+(difference_type n) {
67 return null_terminating_iterator(ptr_ + n, end_);
68 }
69
70 FMT_CONSTEXPR null_terminating_iterator operator-(difference_type n) {
71 return null_terminating_iterator(ptr_ - n, end_);
72 }
73
74 FMT_CONSTEXPR null_terminating_iterator operator+=(difference_type n) {
75 ptr_ += n;
76 return *this;
77 }
78
79 FMT_CONSTEXPR difference_type operator-(
80 null_terminating_iterator other) const {
81 return ptr_ - other.ptr_;
82 }
83
84 FMT_CONSTEXPR bool operator!=(null_terminating_iterator other) const {
85 return ptr_ != other.ptr_;
86 }
87
88 bool operator>=(null_terminating_iterator other) const {
89 return ptr_ >= other.ptr_;
90 }
91
92 // This should be a friend specialization pointer_from<Char> but the latter
93 // doesn't compile by gcc 5.1 due to a compiler bug.
94 template <typename CharT>
95 friend FMT_CONSTEXPR_DECL const CharT *pointer_from(
96 null_terminating_iterator<CharT> it);
97
98 private:
99 const Char *ptr_;
100 const Char *end_;
101};
102
103template <typename T>
104FMT_CONSTEXPR const T *pointer_from(const T *p) { return p; }
105
106template <typename Char>
107FMT_CONSTEXPR const Char *pointer_from(null_terminating_iterator<Char> it) {
108 return it.ptr_;
109}
110
111// DEPRECATED: Parses the input as an unsigned integer. This function assumes
112// that the first character is a digit and presence of a non-digit character at
113// the end.
114// it: an iterator pointing to the beginning of the input range.
115template <typename Iterator, typename ErrorHandler>
116FMT_CONSTEXPR unsigned parse_nonnegative_int(Iterator &it, ErrorHandler &&eh) {
117 assert('0' <= *it && *it <= '9');
118 if (*it == '0') {
119 ++it;
120 return 0;
121 }
122 unsigned value = 0;
123 // Convert to unsigned to prevent a warning.
124 unsigned max_int = (std::numeric_limits<int>::max)();
125 unsigned big = max_int / 10;
126 do {
127 // Check for overflow.
128 if (value > big) {
129 value = max_int + 1;
130 break;
131 }
132 value = value * 10 + unsigned(*it - '0');
133 // Workaround for MSVC "setup_exception stack overflow" error:
134 auto next = it;
135 ++next;
136 it = next;
137 } while ('0' <= *it && *it <= '9');
138 if (value > max_int)
139 eh.on_error("number is too big");
140 return value;
141}
142
Glen Stark72d51e02016-06-08 01:23:32 +0200143// Checks if a value fits in int - used to avoid warnings about comparing
144// signed and unsigned integers.
145template <bool IsSigned>
Victor Zverovich418659a2018-03-03 14:04:59 -0800146struct int_checker {
Glen Stark72d51e02016-06-08 01:23:32 +0200147 template <typename T>
148 static bool fits_in_int(T value) {
149 unsigned max = std::numeric_limits<int>::max();
150 return value <= max;
151 }
152 static bool fits_in_int(bool) { return true; }
153};
154
155template <>
Victor Zverovich418659a2018-03-03 14:04:59 -0800156struct int_checker<true> {
Glen Stark72d51e02016-06-08 01:23:32 +0200157 template <typename T>
158 static bool fits_in_int(T value) {
159 return value >= std::numeric_limits<int>::min() &&
160 value <= std::numeric_limits<int>::max();
161 }
162 static bool fits_in_int(int) { return true; }
163};
164
Victor Zverovich418659a2018-03-03 14:04:59 -0800165class printf_precision_handler: public function<int> {
Glen Stark72d51e02016-06-08 01:23:32 +0200166 public:
Glen Stark72d51e02016-06-08 01:23:32 +0200167 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800168 typename std::enable_if<std::is_integral<T>::value, int>::type
169 operator()(T value) {
Victor Zverovich418659a2018-03-03 14:04:59 -0800170 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700171 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200172 return static_cast<int>(value);
173 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800174
175 template <typename T>
Victor Zverovich6322b472018-06-06 16:51:35 +0200176 typename std::enable_if<!std::is_integral<T>::value, int>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800177 FMT_THROW(format_error("precision 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};
181
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800182// An argument visitor that returns true iff arg is a zero integer.
Victor Zverovich418659a2018-03-03 14:04:59 -0800183class is_zero_int: public function<bool> {
Glen Stark72d51e02016-06-08 01:23:32 +0200184 public:
185 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800186 typename std::enable_if<std::is_integral<T>::value, bool>::type
187 operator()(T value) { return value == 0; }
188
189 template <typename T>
190 typename std::enable_if<!std::is_integral<T>::value, bool>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -0700191 operator()(T) { return false; }
Glen Stark72d51e02016-06-08 01:23:32 +0200192};
193
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700194template <typename T>
195struct make_unsigned_or_bool : std::make_unsigned<T> {};
196
197template <>
198struct make_unsigned_or_bool<bool> {
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800199 typedef bool type;
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700200};
201
Victor Zverovichd705d512016-12-29 09:07:39 -0800202template <typename T, typename Context>
Victor Zverovich418659a2018-03-03 14:04:59 -0800203class arg_converter: public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200204 private:
Victor Zverovichd705d512016-12-29 09:07:39 -0800205 typedef typename Context::char_type Char;
206
Victor Zverovich23759b22018-04-04 07:38:21 -0700207 basic_format_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -0800208 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +0200209
Glen Stark72d51e02016-06-08 01:23:32 +0200210 public:
Victor Zverovich23759b22018-04-04 07:38:21 -0700211 arg_converter(basic_format_arg<Context> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +0200212 : arg_(arg), type_(type) {}
213
Victor Zverovich751ff642016-11-19 08:40:24 -0800214 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200215 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -0800216 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200217 }
218
219 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -0800220 typename std::enable_if<std::is_integral<U>::value>::type
221 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200222 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovichc18a4042017-09-04 13:56:14 -0700223 typedef typename std::conditional<
Victor Zverovichf194a412017-08-27 09:16:50 -0700224 std::is_same<T, void>::value, U, T>::type TargetType;
Victor Zverovich5013c152018-02-10 06:52:46 -0800225 if (const_check(sizeof(TargetType) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +0200226 // Extra casts are used to silence warnings.
227 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800228 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800229 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200230 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700231 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800232 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800233 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200234 }
235 } else {
236 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200237 // glibc's printf doesn't sign extend arguments of smaller types:
238 // std::printf("%lld", -42); // prints "4294967254"
239 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700240 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200241 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800242 arg_ = internal::make_arg<Context>(
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700243 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200244 }
245 }
246 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800247
248 template <typename U>
Victor Zverovich77c892c2017-08-27 08:16:46 -0700249 typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800250 // No coversion needed for non-integral types.
251 }
Glen Stark72d51e02016-06-08 01:23:32 +0200252};
253
Victor Zverovich751ff642016-11-19 08:40:24 -0800254// Converts an integer argument to T for printf, if T is an integral type.
255// If T is void, the argument is converted to corresponding signed or unsigned
256// type depending on the type specifier: 'd' and 'i' - signed, other -
257// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800258template <typename T, typename Context, typename Char>
Victor Zverovich23759b22018-04-04 07:38:21 -0700259void convert_arg(basic_format_arg<Context> &arg, Char type) {
Victor Zverovichf5480632018-10-05 07:15:41 -0700260 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800261}
262
Glen Stark72d51e02016-06-08 01:23:32 +0200263// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800264template <typename Context>
Victor Zverovich418659a2018-03-03 14:04:59 -0800265class char_converter: public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200266 private:
Victor Zverovich23759b22018-04-04 07:38:21 -0700267 basic_format_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200268
Glen Stark72d51e02016-06-08 01:23:32 +0200269 public:
Victor Zverovich23759b22018-04-04 07:38:21 -0700270 explicit char_converter(basic_format_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200271
272 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800273 typename std::enable_if<std::is_integral<T>::value>::type
274 operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200275 typedef typename Context::char_type Char;
276 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200277 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800278
279 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800280 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800281 // No coversion needed for non-integral types.
282 }
Glen Stark72d51e02016-06-08 01:23:32 +0200283};
284
285// Checks if an argument is a valid printf width specifier and sets
286// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000287template <typename Char>
Victor Zverovich418659a2018-03-03 14:04:59 -0800288class printf_width_handler: public function<unsigned> {
Glen Stark72d51e02016-06-08 01:23:32 +0200289 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000290 typedef basic_format_specs<Char> format_specs;
291
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000292 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200293
Glen Stark72d51e02016-06-08 01:23:32 +0200294 public:
Victor Zverovich418659a2018-03-03 14:04:59 -0800295 explicit printf_width_handler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200296
Glen Stark72d51e02016-06-08 01:23:32 +0200297 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800298 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
299 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800300 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200301 UnsignedType width = static_cast<UnsignedType>(value);
302 if (internal::is_negative(value)) {
303 spec_.align_ = ALIGN_LEFT;
304 width = 0 - width;
305 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700306 unsigned int_max = std::numeric_limits<int>::max();
307 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700308 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200309 return static_cast<unsigned>(width);
310 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800311
312 template <typename T>
313 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -0700314 operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800315 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200316 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800317 }
Glen Stark72d51e02016-06-08 01:23:32 +0200318};
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800319
320template <typename Char, typename Context>
321void printf(basic_buffer<Char> &buf, basic_string_view<Char> format,
322 basic_format_args<Context> args) {
323 Context(std::back_inserter(buf), format, args).format();
324}
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700325} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200326
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800327using internal::printf; // For printing into memory_buffer.
328
Victor Zverovichc0954452018-01-06 09:09:50 -0800329template <typename Range>
Victor Zverovich10e70a02017-12-02 09:44:48 -0800330class printf_arg_formatter;
331
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800332template <
333 typename OutputIt, typename Char,
334 typename ArgFormatter =
335 printf_arg_formatter<back_insert_range<internal::basic_buffer<Char>>>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800336class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800337
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700338/**
339 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800340 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700341 \endrst
342 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800343template <typename Range>
Victor Zverovich418659a2018-03-03 14:04:59 -0800344class printf_arg_formatter:
Victor Zverovichedd5f142018-05-20 09:09:03 -0700345 public internal::function<
346 typename internal::arg_formatter_base<Range>::iterator>,
347 public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200348 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800349 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800350 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800351 typedef internal::arg_formatter_base<Range> base;
352 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800353
354 context_type &context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800355
Daniela Engert2570f1a2018-04-26 20:32:14 +0200356 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700357 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200358 this->write("(nil)");
359 }
360
Daniela Engert2570f1a2018-04-26 20:32:14 +0200361 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700362 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200363 this->write(L"(nil)");
364 }
365
Glen Stark72d51e02016-06-08 01:23:32 +0200366 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800367 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000368
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700369 /**
370 \rst
371 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500372 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700373 specifier information for standard argument types.
374 \endrst
375 */
Victor Zverovich9a53a702018-01-14 12:25:03 -0800376 printf_arg_formatter(internal::basic_buffer<char_type> &buffer,
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800377 format_specs &spec, context_type &ctx)
Victor Zverovichf2ee9882018-11-14 09:39:37 -0800378 : base(back_insert_range<internal::basic_buffer<char_type>>(buffer), &spec,
379 ctx.locale()),
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800380 context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200381
Victor Zveroviche928b672018-07-04 11:36:38 -0700382 template <typename T>
383 typename std::enable_if<std::is_integral<T>::value, iterator>::type
384 operator()(T value) {
385 // MSVC2013 fails to compile separate overloads for bool and char_type so
386 // use std::is_same instead.
387 if (std::is_same<T, bool>::value) {
Victor Zverovich7110b462018-09-12 07:34:22 -0700388 format_specs &fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700389 if (fmt_spec.type != 's')
Victor Zveroviche928b672018-07-04 11:36:38 -0700390 return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700391 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700392 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700393 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich7110b462018-09-12 07:34:22 -0700394 format_specs &fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700395 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700396 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700397 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700398 fmt_spec.align_ = ALIGN_RIGHT;
399 return base::operator()(value);
400 } else {
401 return base::operator()(value);
402 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000403 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200404 }
405
Victor Zveroviche928b672018-07-04 11:36:38 -0700406 template <typename T>
407 typename std::enable_if<std::is_floating_point<T>::value, iterator>::type
408 operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000409 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200410 }
411
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700412 /** Formats a null-terminated C string. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000413 iterator operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200414 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800415 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700416 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200417 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200418 else
419 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000420 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200421 }
422
Daniela Engert2570f1a2018-04-26 20:32:14 +0200423 /** Formats a null-terminated wide C string. */
424 iterator operator()(const wchar_t *value) {
425 if (value)
426 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700427 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200428 write_null_pointer(char_type());
429 else
430 this->write(L"(null)");
431 return this->out();
432 }
433
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700434 iterator operator()(basic_string_view<char_type> value) {
435 return base::operator()(value);
436 }
437
438 iterator operator()(monostate value) {
439 return base::operator()(value);
440 }
441
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700442 /** Formats a pointer. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000443 iterator operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200444 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800445 return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700446 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200447 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000448 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200449 }
450
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700451 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700452 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800453 handle.format(context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000454 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700455 }
456};
457
Victor Zverovichc0954452018-01-06 09:09:50 -0800458template <typename T>
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700459struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700460 template <typename ParseContext>
Victor Zverovich1b452532018-02-28 05:09:24 -0800461 auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { return ctx.begin(); }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700462
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800463 template <typename FormatContext>
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700464 auto format(const T &value, FormatContext &ctx) -> decltype(ctx.out()) {
465 internal::format_value(internal::get_container(ctx.out()), value);
466 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200467 }
468};
469
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700470/** This template formats data and writes the output to a writer. */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800471template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800472class basic_printf_context :
Victor Zverovicha11eb3a2018-08-22 09:07:17 -0700473 // Inherit publicly as a workaround for the icc bug
474 // https://software.intel.com/en-us/forums/intel-c-compiler/topic/783476.
475 public internal::context_base<
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800476 OutputIt, basic_printf_context<OutputIt, Char, ArgFormatter>, Char> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700477 public:
478 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800479 typedef Char char_type;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700480
481 template <typename T>
Victor Zverovich522de7b2018-02-11 08:32:02 -0800482 struct formatter_type { typedef printf_formatter<T> type; };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700483
Glen Stark72d51e02016-06-08 01:23:32 +0200484 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800485 typedef internal::context_base<OutputIt, basic_printf_context, Char> base;
486 typedef typename base::format_arg format_arg;
487 typedef basic_format_specs<char_type> format_specs;
488 typedef internal::null_terminating_iterator<char_type> iterator;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700489
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700490 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200491
492 // Returns the argument with specified index or, if arg_index is equal
493 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800494 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700495 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200496 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
497
498 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700499 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200500
501 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700502 /**
503 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800504 Constructs a ``printf_context`` object. References to the arguments and
505 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700506 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700507 \endrst
508 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800509 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800510 basic_format_args<basic_printf_context> args)
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800511 : base(out, format_str, args) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700512
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800513 using base::parse_context;
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700514 using base::out;
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800515 using base::advance_to;
Victor Zverovich9998f662016-11-06 16:11:24 -0800516
Victor Zverovichc0954452018-01-06 09:09:50 -0800517 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich08dff372018-01-28 20:50:43 -0800518 void format();
Glen Stark72d51e02016-06-08 01:23:32 +0200519};
520
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800521template <typename OutputIt, typename Char, typename AF>
522void basic_printf_context<OutputIt, Char, AF>::parse_flags(
Victor Zverovich217e7c72018-01-14 07:19:23 -0800523 format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200524 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700525 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200526 case '-':
527 spec.align_ = ALIGN_LEFT;
528 break;
529 case '+':
Victor Zverovich29246222018-10-17 09:15:29 -0700530 spec.flags |= SIGN_FLAG | PLUS_FLAG;
Glen Stark72d51e02016-06-08 01:23:32 +0200531 break;
532 case '0':
533 spec.fill_ = '0';
534 break;
535 case ' ':
Victor Zverovich29246222018-10-17 09:15:29 -0700536 spec.flags |= SIGN_FLAG;
Glen Stark72d51e02016-06-08 01:23:32 +0200537 break;
538 case '#':
Victor Zverovich29246222018-10-17 09:15:29 -0700539 spec.flags |= HASH_FLAG;
Glen Stark72d51e02016-06-08 01:23:32 +0200540 break;
541 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700542 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200543 return;
544 }
545 }
546}
547
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800548template <typename OutputIt, typename Char, typename AF>
549typename basic_printf_context<OutputIt, Char, AF>::format_arg
550 basic_printf_context<OutputIt, Char, AF>::get_arg(
551 iterator it, unsigned arg_index) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700552 (void)it;
Victor Zverovichc523dd52017-11-19 07:36:01 -0800553 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich67928ea2018-01-14 09:27:40 -0800554 return this->do_get_arg(this->parse_context().next_arg_id());
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800555 return base::get_arg(arg_index - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200556}
557
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800558template <typename OutputIt, typename Char, typename AF>
559unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700560 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200561 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800562 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200563 if (c >= '0' && c <= '9') {
564 // Parse an argument index (if followed by '$') or a width possibly
565 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700566 internal::error_handler eh;
567 unsigned value = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700568 if (*it == '$') { // value is an argument index
569 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200570 arg_index = value;
571 } else {
572 if (c == '0')
573 spec.fill_ = '0';
574 if (value != 0) {
575 // Nonzero value means that we parsed width and don't need to
576 // parse it or flags again, so return now.
577 spec.width_ = value;
578 return arg_index;
579 }
580 }
581 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700582 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200583 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700584 if (*it >= '0' && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700585 internal::error_handler eh;
586 spec.width_ = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700587 } else if (*it == '*') {
588 ++it;
Victor Zverovichf5480632018-10-05 07:15:41 -0700589 spec.width_ = visit_format_arg(
590 internal::printf_width_handler<char_type>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200591 }
592 return arg_index;
593}
594
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800595template <typename OutputIt, typename Char, typename AF>
596void basic_printf_context<OutputIt, Char, AF>::format() {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700597 auto &buffer = internal::get_container(this->out());
Victor Zverovich67928ea2018-01-14 09:27:40 -0800598 auto start = iterator(this->parse_context());
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700599 auto it = start;
600 using internal::pointer_from;
601 while (*it) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800602 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200603 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700604 if (*it == c) {
605 buffer.append(pointer_from(start), pointer_from(it));
606 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200607 continue;
608 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700609 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200610
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000611 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200612 spec.align_ = ALIGN_RIGHT;
613
614 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700615 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200616
617 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700618 if (*it == '.') {
619 ++it;
620 if ('0' <= *it && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700621 internal::error_handler eh;
Victor Zverovich29246222018-10-17 09:15:29 -0700622 spec.precision = static_cast<int>(parse_nonnegative_int(it, eh));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700623 } else if (*it == '*') {
624 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700625 spec.precision =
Victor Zverovichf5480632018-10-05 07:15:41 -0700626 visit_format_arg(internal::printf_precision_handler(), get_arg(it));
Victor Zverovich50605682018-01-27 17:56:19 -0800627 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700628 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200629 }
630 }
631
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700632 format_arg arg = get_arg(it, arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700633 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
634 spec.flags &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200635 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800636 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200637 spec.align_ = ALIGN_NUMERIC;
638 else
639 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
640 }
641
642 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800643 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700644 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200645 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700646 if (*it == 'h')
647 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200648 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700649 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200650 break;
651 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700652 if (*it == 'l')
Victor Zverovich016aceb2017-08-26 09:09:43 -0700653 convert_arg<long long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200654 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700655 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200656 break;
657 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700658 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200659 break;
660 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700661 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200662 break;
663 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700664 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200665 break;
666 case 'L':
667 // printf produces garbage when 'L' is omitted for long double, no
668 // need to do the same.
669 break;
670 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700671 --it;
672 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200673 }
674
675 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700676 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700677 FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700678 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800679 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200680 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700681 switch (spec.type) {
Glen Stark72d51e02016-06-08 01:23:32 +0200682 case 'i': case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700683 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200684 break;
685 case 'c':
Daniela Engert2570f1a2018-04-26 20:32:14 +0200686 // TODO: handle wchar_t better?
Victor Zverovichf5480632018-10-05 07:15:41 -0700687 visit_format_arg(
688 internal::char_converter<basic_printf_context>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200689 break;
690 }
691 }
692
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700693 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200694
695 // Format argument.
Victor Zverovichf5480632018-10-05 07:15:41 -0700696 visit_format_arg(AF(buffer, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200697 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700698 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200699}
Glen Stark72d51e02016-06-08 01:23:32 +0200700
Victor Zverovich217e7c72018-01-14 07:19:23 -0800701template <typename Buffer>
tnovotnye37d6a92018-11-22 22:57:07 +0100702struct basic_printf_context_t {
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800703 typedef basic_printf_context<
704 std::back_insert_iterator<Buffer>, typename Buffer::value_type> type;
705};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800706
tnovotnye37d6a92018-11-22 22:57:07 +0100707typedef basic_printf_context_t<internal::buffer>::type printf_context;
708typedef basic_printf_context_t<internal::wbuffer>::type wprintf_context;
709
710typedef basic_format_args<printf_context> printf_args;
711typedef basic_format_args<wprintf_context> wprintf_args;
712
713/**
714 \rst
715 Constructs an `~fmt::format_arg_store` object that contains references to
716 arguments and can be implicitly converted to `~fmt::printf_args`.
717 \endrst
718 */
719template<typename... Args>
720inline format_arg_store<printf_context, Args...>
721 make_printf_args(const Args &... args) { return {args...}; }
722
723/**
724 \rst
725 Constructs an `~fmt::format_arg_store` object that contains references to
726 arguments and can be implicitly converted to `~fmt::wprintf_args`.
727 \endrst
728 */
729template<typename... Args>
730inline format_arg_store<wprintf_context, Args...>
731 make_wprintf_args(const Args &... args) { return {args...}; }
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800732
Victor Zverovich0a96c032018-10-25 07:20:02 -0700733template <typename S, typename Char = FMT_CHAR(S)>
Daniela Engertf27defc2018-10-07 14:38:29 +0200734inline std::basic_string<Char>
Victor Zverovich0a96c032018-10-25 07:20:02 -0700735vsprintf(const S &format,
tnovotnye37d6a92018-11-22 22:57:07 +0100736 basic_format_args<typename basic_printf_context_t<
Daniela Engertf27defc2018-10-07 14:38:29 +0200737 internal::basic_buffer<Char>>::type> args) {
738 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700739 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500740 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700741}
742
Glen Stark72d51e02016-06-08 01:23:32 +0200743/**
744 \rst
745 Formats arguments and returns the result as a string.
746
747 **Example**::
748
749 std::string message = fmt::sprintf("The answer is %d", 42);
750 \endrst
751*/
Daniela Engertf27defc2018-10-07 14:38:29 +0200752template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800753inline FMT_ENABLE_IF_T(
754 internal::is_string<S>::value, std::basic_string<FMT_CHAR(S)>)
Victor Zverovich0a96c032018-10-25 07:20:02 -0700755 sprintf(const S &format, const Args & ... args) {
756 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200757 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100758 typedef typename basic_printf_context_t<buffer>::type context;
Daniela Engertf27defc2018-10-07 14:38:29 +0200759 format_arg_store<context, Args...> as{ args... };
Victor Zverovich0a96c032018-10-25 07:20:02 -0700760 return vsprintf(to_string_view(format),
Daniela Engertf27defc2018-10-07 14:38:29 +0200761 basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700762}
763
Victor Zverovich0a96c032018-10-25 07:20:02 -0700764template <typename S, typename Char = FMT_CHAR(S)>
765inline int vfprintf(std::FILE *f, const S &format,
tnovotnye37d6a92018-11-22 22:57:07 +0100766 basic_format_args<typename basic_printf_context_t<
Daniela Engert2570f1a2018-04-26 20:32:14 +0200767 internal::basic_buffer<Char>>::type> args) {
768 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700769 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800770 std::size_t size = buffer.size();
771 return std::fwrite(
Daniela Engert2570f1a2018-04-26 20:32:14 +0200772 buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800773}
Glen Stark72d51e02016-06-08 01:23:32 +0200774
775/**
776 \rst
777 Prints formatted data to the file *f*.
778
779 **Example**::
780
781 fmt::fprintf(stderr, "Don't %s!", "panic");
782 \endrst
783 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200784template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800785inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Victor Zverovich0a96c032018-10-25 07:20:02 -0700786 fprintf(std::FILE *f, const S &format, const Args & ... args) {
787 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200788 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100789 typedef typename basic_printf_context_t<buffer>::type context;
Daniela Engertf27defc2018-10-07 14:38:29 +0200790 format_arg_store<context, Args...> as{ args... };
Victor Zverovich0a96c032018-10-25 07:20:02 -0700791 return vfprintf(f, to_string_view(format),
Daniela Engertf27defc2018-10-07 14:38:29 +0200792 basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700793}
794
Victor Zverovich0a96c032018-10-25 07:20:02 -0700795template <typename S, typename Char = FMT_CHAR(S)>
796inline int vprintf(const S &format,
tnovotnye37d6a92018-11-22 22:57:07 +0100797 basic_format_args<typename basic_printf_context_t<
Daniela Engertf27defc2018-10-07 14:38:29 +0200798 internal::basic_buffer<Char>>::type> args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700799 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200800}
801
Glen Stark72d51e02016-06-08 01:23:32 +0200802/**
803 \rst
804 Prints formatted data to ``stdout``.
805
806 **Example**::
807
808 fmt::printf("Elapsed time: %.2f seconds", 1.23);
809 \endrst
810 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200811template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800812inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Daniela Engertf27defc2018-10-07 14:38:29 +0200813 printf(const S &format_str, const Args & ... args) {
814 internal::check_format_string<Args...>(format_str);
815 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100816 typedef typename basic_printf_context_t<buffer>::type context;
Daniela Engertf27defc2018-10-07 14:38:29 +0200817 format_arg_store<context, Args...> as{ args... };
Daniela Engert2c81c852018-10-08 20:14:39 +0200818 return vprintf(to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200819 basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200820}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700821
Victor Zverovich0a96c032018-10-25 07:20:02 -0700822template <typename S, typename Char = FMT_CHAR(S)>
Daniela Engertf27defc2018-10-07 14:38:29 +0200823inline int vfprintf(std::basic_ostream<Char> &os,
Victor Zverovich0a96c032018-10-25 07:20:02 -0700824 const S &format,
tnovotnye37d6a92018-11-22 22:57:07 +0100825 basic_format_args<typename basic_printf_context_t<
Daniela Engertf27defc2018-10-07 14:38:29 +0200826 internal::basic_buffer<Char>>::type> args) {
827 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700828 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200829 internal::write(os, buffer);
830 return static_cast<int>(buffer.size());
831}
832
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700833/**
834 \rst
835 Prints formatted data to the stream *os*.
836
837 **Example**::
838
Victor Zverovich40232912018-03-04 09:55:17 -0800839 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700840 \endrst
841 */
Daniela Engertf27defc2018-10-07 14:38:29 +0200842template <typename S, typename... Args>
Victor Zverovich7f7504b2018-12-12 18:21:13 -0800843inline FMT_ENABLE_IF_T(internal::is_string<S>::value, int)
Daniela Engertf27defc2018-10-07 14:38:29 +0200844 fprintf(std::basic_ostream<FMT_CHAR(S)> &os,
845 const S &format_str, const Args & ... args) {
846 internal::check_format_string<Args...>(format_str);
847 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100848 typedef typename basic_printf_context_t<buffer>::type context;
Daniela Engertf27defc2018-10-07 14:38:29 +0200849 format_arg_store<context, Args...> as{ args... };
Daniela Engert2c81c852018-10-08 20:14:39 +0200850 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200851 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200852}
Victor Zverovich838400d2018-05-12 08:33:51 -0700853FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200854
855#endif // FMT_PRINTF_H_