blob: c3d8b5566b5b1c4e5f916305a1626b62034ed716 [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
19// Checks if a value fits in int - used to avoid warnings about comparing
20// signed and unsigned integers.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080021template <bool IsSigned> struct int_checker {
22 template <typename T> static bool fits_in_int(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020023 unsigned max = std::numeric_limits<int>::max();
24 return value <= max;
25 }
26 static bool fits_in_int(bool) { return true; }
27};
28
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080029template <> struct int_checker<true> {
30 template <typename T> static bool fits_in_int(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020031 return value >= std::numeric_limits<int>::min() &&
32 value <= std::numeric_limits<int>::max();
33 }
34 static bool fits_in_int(int) { return true; }
35};
36
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080037class printf_precision_handler : public function<int> {
Glen Stark72d51e02016-06-08 01:23:32 +020038 public:
Victor Zverovichc21c6b82019-03-16 12:58:18 -070039 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
40 int operator()(T value) {
Victor Zverovich418659a2018-03-03 14:04:59 -080041 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070042 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +020043 return static_cast<int>(value);
44 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080045
Victor Zverovichc21c6b82019-03-16 12:58:18 -070046 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
47 int operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -080048 FMT_THROW(format_error("precision is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +020049 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -080050 }
Glen Stark72d51e02016-06-08 01:23:32 +020051};
52
Victor Zveroviche2dfd392016-11-19 09:29:09 -080053// An argument visitor that returns true iff arg is a zero integer.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080054class is_zero_int : public function<bool> {
Glen Stark72d51e02016-06-08 01:23:32 +020055 public:
Victor Zverovichc21c6b82019-03-16 12:58:18 -070056 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
57 bool operator()(T value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080058 return value == 0;
59 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080060
Victor Zverovichc21c6b82019-03-16 12:58:18 -070061 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
62 bool operator()(T) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080063 return false;
64 }
Glen Stark72d51e02016-06-08 01:23:32 +020065};
66
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080067template <typename T> struct make_unsigned_or_bool : std::make_unsigned<T> {};
Victor Zverovich0fbd8462017-09-04 11:41:15 -070068
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080069template <> struct make_unsigned_or_bool<bool> { typedef bool type; };
Victor Zverovich0fbd8462017-09-04 11:41:15 -070070
Victor Zverovichd705d512016-12-29 09:07:39 -080071template <typename T, typename Context>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080072class arg_converter : public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +020073 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080074 typedef typename Context::char_type Char;
75
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080076 basic_format_arg<Context>& arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080077 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020078
Glen Stark72d51e02016-06-08 01:23:32 +020079 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080080 arg_converter(basic_format_arg<Context>& arg, Char type)
81 : arg_(arg), type_(type) {}
Glen Stark72d51e02016-06-08 01:23:32 +020082
Victor Zverovich751ff642016-11-19 08:40:24 -080083 void operator()(bool value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080084 if (type_ != 's') operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020085 }
86
Victor Zverovichc21c6b82019-03-16 12:58:18 -070087 template <typename U, FMT_ENABLE_IF(std::is_integral<U>::value)>
88 void operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020089 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080090 typedef typename std::conditional<std::is_same<T, void>::value, U, T>::type
91 TargetType;
Victor Zverovich5013c152018-02-10 06:52:46 -080092 if (const_check(sizeof(TargetType) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +020093 // Extra casts are used to silence warnings.
94 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -080095 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080096 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +020097 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -070098 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -080099 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800100 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200101 }
102 } else {
103 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200104 // glibc's printf doesn't sign extend arguments of smaller types:
105 // std::printf("%lld", -42); // prints "4294967254"
106 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700107 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200108 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800109 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800110 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200111 }
112 }
113 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800114
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700115 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)>
116 void operator()(U) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200117};
118
Victor Zverovich751ff642016-11-19 08:40:24 -0800119// Converts an integer argument to T for printf, if T is an integral type.
120// If T is void, the argument is converted to corresponding signed or unsigned
121// type depending on the type specifier: 'd' and 'i' - signed, other -
122// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800123template <typename T, typename Context, typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800124void convert_arg(basic_format_arg<Context>& arg, Char type) {
Victor Zverovichf5480632018-10-05 07:15:41 -0700125 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800126}
127
Glen Stark72d51e02016-06-08 01:23:32 +0200128// Converts an integer argument to char for printf.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800129template <typename Context> class char_converter : public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200130 private:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800131 basic_format_arg<Context>& arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200132
Glen Stark72d51e02016-06-08 01:23:32 +0200133 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800134 explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200135
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700136 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
137 void operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200138 typedef typename Context::char_type Char;
139 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200140 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800141
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700142 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
143 void operator()(T) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200144};
145
146// Checks if an argument is a valid printf width specifier and sets
147// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000148template <typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800149class printf_width_handler : public function<unsigned> {
Glen Stark72d51e02016-06-08 01:23:32 +0200150 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000151 typedef basic_format_specs<Char> format_specs;
152
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800153 format_specs& spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200154
Glen Stark72d51e02016-06-08 01:23:32 +0200155 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800156 explicit printf_width_handler(format_specs& spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200157
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700158 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
159 unsigned operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800160 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200161 UnsignedType width = static_cast<UnsignedType>(value);
162 if (internal::is_negative(value)) {
163 spec_.align_ = ALIGN_LEFT;
164 width = 0 - width;
165 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700166 unsigned int_max = std::numeric_limits<int>::max();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800167 if (width > int_max) FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200168 return static_cast<unsigned>(width);
169 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800170
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700171 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
172 unsigned operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800173 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200174 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800175 }
Glen Stark72d51e02016-06-08 01:23:32 +0200176};
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800177
178template <typename Char, typename Context>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800179void printf(basic_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 Zverovich58b6f8d2019-01-12 18:27:38 -0800196template <typename OutputIt, typename Char,
197 typename ArgFormatter = printf_arg_formatter<
198 back_insert_range<internal::basic_buffer<Char>>>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800199class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800200
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700201/**
202 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800203 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700204 \endrst
205 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800206template <typename Range>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800207class printf_arg_formatter
208 : public internal::function<
209 typename internal::arg_formatter_base<Range>::iterator>,
210 public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200211 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800212 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800213 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800214 typedef internal::arg_formatter_base<Range> base;
Daniela Engertb0cde862019-01-01 11:45:56 +0100215 typedef basic_printf_context<iterator, char_type, printf_arg_formatter>
216 context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800217
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800218 context_type& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800219
Daniela Engert2570f1a2018-04-26 20:32:14 +0200220 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700221 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200222 this->write("(nil)");
223 }
224
Daniela Engert2570f1a2018-04-26 20:32:14 +0200225 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700226 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200227 this->write(L"(nil)");
228 }
229
Glen Stark72d51e02016-06-08 01:23:32 +0200230 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800231 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000232
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700233 /**
234 \rst
235 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500236 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700237 specifier information for standard argument types.
238 \endrst
239 */
Daniela Engertb0cde862019-01-01 11:45:56 +0100240 printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800241 : base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200242
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700243 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
244 iterator operator()(T value) {
Victor Zveroviche928b672018-07-04 11:36:38 -0700245 // MSVC2013 fails to compile separate overloads for bool and char_type so
246 // use std::is_same instead.
247 if (std::is_same<T, bool>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800248 format_specs& fmt_spec = *this->spec();
249 if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700250 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700251 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700252 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800253 format_specs& fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700254 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700255 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700256 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700257 fmt_spec.align_ = ALIGN_RIGHT;
258 return base::operator()(value);
259 } else {
260 return base::operator()(value);
261 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000262 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200263 }
264
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700265 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
266 iterator operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000267 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200268 }
269
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700270 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800271 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200272 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800273 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700274 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200275 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200276 else
277 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000278 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200279 }
280
Daniela Engert2570f1a2018-04-26 20:32:14 +0200281 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800282 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200283 if (value)
284 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700285 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200286 write_null_pointer(char_type());
287 else
288 this->write(L"(null)");
289 return this->out();
290 }
291
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700292 iterator operator()(basic_string_view<char_type> value) {
293 return base::operator()(value);
294 }
295
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800296 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700297
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700298 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800299 iterator operator()(const void* value) {
300 if (value) return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700301 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200302 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000303 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200304 }
305
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700306 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700307 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800308 handle.format(context_.parse_context(), context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000309 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700310 }
311};
312
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800313template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700314 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800315 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
316 return ctx.begin();
317 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700318
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800319 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800320 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700321 internal::format_value(internal::get_container(ctx.out()), value);
322 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200323 }
324};
325
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700326/** This template formats data and writes the output to a writer. */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800327template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800328class basic_printf_context {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700329 public:
330 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800331 typedef Char char_type;
Victor Zveroviche4572e52019-02-10 06:34:47 -0800332 typedef basic_format_arg<basic_printf_context> format_arg;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700333
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800334 template <typename T> struct formatter_type {
335 typedef printf_formatter<T> type;
336 };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700337
Glen Stark72d51e02016-06-08 01:23:32 +0200338 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800339 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700340
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800341 OutputIt out_;
342 basic_format_args<basic_printf_context> args_;
343 basic_parse_context<Char> parse_ctx_;
344
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800345 static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200346
347 // Returns the argument with specified index or, if arg_index is equal
348 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100349 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200350
351 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800352 unsigned parse_header(const Char*& it, const Char* end, format_specs& spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200353
354 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700355 /**
356 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800357 Constructs a ``printf_context`` object. References to the arguments and
358 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700359 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700360 \endrst
361 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800362 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800363 basic_format_args<basic_printf_context> args)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800364 : out_(out), args_(args), parse_ctx_(format_str) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700365
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800366 OutputIt out() { return out_; }
367 void advance_to(OutputIt it) { out_ = it; }
368
369 format_arg arg(unsigned id) const { return args_.get(id); }
370
371 basic_parse_context<Char>& parse_context() { return parse_ctx_; }
372
373 FMT_CONSTEXPR void on_error(const char* message) {
374 parse_ctx_.on_error(message);
375 }
Victor Zverovich9998f662016-11-06 16:11:24 -0800376
Victor Zverovichc0954452018-01-06 09:09:50 -0800377 /** Formats stored arguments and writes the output to the range. */
Daniela Engertb0cde862019-01-01 11:45:56 +0100378 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200379};
380
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800381template <typename OutputIt, typename Char, typename AF>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800382void basic_printf_context<OutputIt, Char, AF>::parse_flags(format_specs& spec,
383 const Char*& it,
384 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100385 for (; it != end; ++it) {
386 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800387 case '-':
388 spec.align_ = ALIGN_LEFT;
389 break;
390 case '+':
391 spec.flags |= SIGN_FLAG | PLUS_FLAG;
392 break;
393 case '0':
394 spec.fill_ = '0';
395 break;
396 case ' ':
397 spec.flags |= SIGN_FLAG;
398 break;
399 case '#':
400 spec.flags |= HASH_FLAG;
401 break;
402 default:
403 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200404 }
405 }
406}
407
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800408template <typename OutputIt, typename Char, typename AF>
409typename basic_printf_context<OutputIt, Char, AF>::format_arg
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800410basic_printf_context<OutputIt, Char, AF>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800411 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800412 arg_index = parse_ctx_.next_arg_id();
413 else
414 parse_ctx_.check_arg_id(--arg_index);
415 return internal::get_arg(*this, arg_index);
Glen Stark72d51e02016-06-08 01:23:32 +0200416}
417
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800418template <typename OutputIt, typename Char, typename AF>
419unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800420 const Char*& it, const Char* end, format_specs& spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200421 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800422 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200423 if (c >= '0' && c <= '9') {
424 // Parse an argument index (if followed by '$') or a width possibly
425 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700426 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100427 unsigned value = parse_nonnegative_int(it, end, eh);
428 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700429 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200430 arg_index = value;
431 } else {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800432 if (c == '0') spec.fill_ = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200433 if (value != 0) {
434 // Nonzero value means that we parsed width and don't need to
435 // parse it or flags again, so return now.
436 spec.width_ = value;
437 return arg_index;
438 }
439 }
440 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100441 parse_flags(spec, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200442 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100443 if (it != end) {
444 if (*it >= '0' && *it <= '9') {
445 internal::error_handler eh;
446 spec.width_ = parse_nonnegative_int(it, end, eh);
447 } else if (*it == '*') {
448 ++it;
449 spec.width_ = visit_format_arg(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800450 internal::printf_width_handler<char_type>(spec), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100451 }
Glen Stark72d51e02016-06-08 01:23:32 +0200452 }
453 return arg_index;
454}
455
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800456template <typename OutputIt, typename Char, typename AF>
Daniela Engertb0cde862019-01-01 11:45:56 +0100457OutputIt basic_printf_context<OutputIt, Char, AF>::format() {
458 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800459 const Char* start = parse_ctx_.begin();
460 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700461 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100462 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800463 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200464 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100465 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100466 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700467 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200468 continue;
469 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100470 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200471
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000472 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200473 spec.align_ = ALIGN_RIGHT;
474
475 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100476 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200477
478 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100479 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700480 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100481 c = it != end ? *it : 0;
482 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700483 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100484 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
485 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700486 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700487 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100488 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800489 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700490 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200491 }
492 }
493
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100494 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700495 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800496 spec.flags = static_cast<uint_least8_t>(
497 spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200498 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800499 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200500 spec.align_ = ALIGN_NUMERIC;
501 else
502 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
503 }
504
505 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100506 c = it != end ? *it++ : 0;
507 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800508 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100509 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200510 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100511 if (t == 'h') {
512 ++it;
513 t = it != end ? *it : 0;
514 convert_arg<signed char>(arg, t);
515 } else {
516 convert_arg<short>(arg, t);
517 }
Glen Stark72d51e02016-06-08 01:23:32 +0200518 break;
519 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100520 if (t == 'l') {
521 ++it;
522 t = it != end ? *it : 0;
523 convert_arg<long long>(arg, t);
524 } else {
525 convert_arg<long>(arg, t);
526 }
Glen Stark72d51e02016-06-08 01:23:32 +0200527 break;
528 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100529 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200530 break;
531 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100532 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200533 break;
534 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100535 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200536 break;
537 case 'L':
538 // printf produces garbage when 'L' is omitted for long double, no
539 // need to do the same.
540 break;
541 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700542 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100543 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200544 }
545
546 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800547 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700548 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800549 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200550 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700551 switch (spec.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800552 case 'i':
553 case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700554 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200555 break;
556 case 'c':
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800557 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
558 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200559 break;
560 }
561 }
562
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700563 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200564
565 // Format argument.
Daniela Engertb0cde862019-01-01 11:45:56 +0100566 visit_format_arg(AF(out, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200567 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100568 return std::copy(start, it, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200569}
Glen Stark72d51e02016-06-08 01:23:32 +0200570
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800571template <typename Buffer> struct basic_printf_context_t {
572 typedef basic_printf_context<std::back_insert_iterator<Buffer>,
573 typename Buffer::value_type>
574 type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800575};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800576
tnovotnye37d6a92018-11-22 22:57:07 +0100577typedef basic_printf_context_t<internal::buffer>::type printf_context;
578typedef basic_printf_context_t<internal::wbuffer>::type wprintf_context;
579
580typedef basic_format_args<printf_context> printf_args;
581typedef basic_format_args<wprintf_context> wprintf_args;
582
Daniela Engertb0cde862019-01-01 11:45:56 +0100583template <typename OutputIt, typename Char = typename OutputIt::value_type>
584struct basic_printf_n_context_t {
585 typedef fmt::internal::truncating_iterator<OutputIt> OutputIter;
586 typedef output_range<OutputIter, Char> Range;
587 typedef basic_printf_context<OutputIter, Char, printf_arg_formatter<Range>>
588 type;
589};
590
tnovotnye37d6a92018-11-22 22:57:07 +0100591/**
592 \rst
593 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800594 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100595 \endrst
596 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800597template <typename... Args>
598inline format_arg_store<printf_context, Args...> make_printf_args(
599 const Args&... args) {
600 return {args...};
601}
tnovotnye37d6a92018-11-22 22:57:07 +0100602
603/**
604 \rst
605 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800606 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100607 \endrst
608 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800609template <typename... Args>
610inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
611 const Args&... args) {
612 return {args...};
613}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800614
Victor Zverovich0a96c032018-10-25 07:20:02 -0700615template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800616inline std::basic_string<Char> vsprintf(
617 const S& format,
618 basic_format_args<
619 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
620 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200621 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700622 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500623 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700624}
625
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700626template <typename OutputIt, typename S, typename Char = FMT_CHAR(S),
627 FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value)>
628inline format_to_n_result<OutputIt> vsnprintf(
Daniela Engertb0cde862019-01-01 11:45:56 +0100629 OutputIt out, std::size_t n, const S& format,
630 basic_format_args<typename basic_printf_n_context_t<OutputIt, Char>::type>
631 args) {
632 typedef internal::truncating_iterator<OutputIt> It;
633 auto it = printf(It(out, n), to_string_view(format), args);
634 return {it.base(), it.count()};
635}
636
Glen Stark72d51e02016-06-08 01:23:32 +0200637/**
638 \rst
639 Formats arguments and returns the result as a string.
640
641 **Example**::
642
643 std::string message = fmt::sprintf("The answer is %d", 42);
644 \endrst
645*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700646template <typename S, typename... Args,
647 FMT_ENABLE_IF(internal::is_string<S>::value)>
648inline std::basic_string<FMT_CHAR(S)> sprintf(const S& format,
649 const Args&... args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700650 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200651 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100652 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800653 format_arg_store<context, Args...> as{args...};
654 return vsprintf(to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700655}
656
Daniela Engertb0cde862019-01-01 11:45:56 +0100657/**
658 \rst
659 Formats arguments for up to ``n`` characters stored through output iterator
660 ``out``. The function returns the updated iterator and the untruncated amount
661 of characters.
662
663 **Example**::
664 std::vector<char> out;
665
666 typedef fmt::format_to_n_result<
667 std::back_insert_iterator<std::vector<char>>> res;
668 res Res = fmt::snprintf(std::back_inserter(out), 5, "The answer is %d", 42);
669 \endrst
670*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700671template <typename OutputIt, typename S, typename... Args,
672 FMT_ENABLE_IF(internal::is_string<S>::value&&
673 internal::is_output_iterator<OutputIt>::value)>
674inline format_to_n_result<OutputIt> snprintf(OutputIt out, std::size_t n,
675 const S& format,
676 const Args&... args) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100677 internal::check_format_string<Args...>(format);
678 typedef FMT_CHAR(S) Char;
679 typedef typename basic_printf_n_context_t<OutputIt, Char>::type context;
680 format_arg_store<context, Args...> as{args...};
681 return vsnprintf(out, n, to_string_view(format),
682 basic_format_args<context>(as));
683}
684
Victor Zverovich0a96c032018-10-25 07:20:02 -0700685template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800686inline int vfprintf(
687 std::FILE* f, const S& format,
688 basic_format_args<
689 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
690 args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200691 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700692 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800693 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800694 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
695 ? -1
696 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800697}
Glen Stark72d51e02016-06-08 01:23:32 +0200698
699/**
700 \rst
701 Prints formatted data to the file *f*.
702
703 **Example**::
704
705 fmt::fprintf(stderr, "Don't %s!", "panic");
706 \endrst
707 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700708template <typename S, typename... Args,
709 FMT_ENABLE_IF(internal::is_string<S>::value)>
710inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700711 internal::check_format_string<Args...>(format);
Daniela Engertf27defc2018-10-07 14:38:29 +0200712 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100713 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800714 format_arg_store<context, Args...> as{args...};
715 return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700716}
717
Victor Zverovich0a96c032018-10-25 07:20:02 -0700718template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800719inline int vprintf(
720 const S& format,
721 basic_format_args<
722 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
723 args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700724 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200725}
726
Glen Stark72d51e02016-06-08 01:23:32 +0200727/**
728 \rst
729 Prints formatted data to ``stdout``.
730
731 **Example**::
732
733 fmt::printf("Elapsed time: %.2f seconds", 1.23);
734 \endrst
735 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700736template <typename S, typename... Args,
737 FMT_ENABLE_IF(internal::is_string<S>::value)>
738inline int printf(const S& format_str, const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200739 internal::check_format_string<Args...>(format_str);
740 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100741 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800742 format_arg_store<context, Args...> as{args...};
743 return vprintf(to_string_view(format_str), basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200744}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700745
Victor Zverovich0a96c032018-10-25 07:20:02 -0700746template <typename S, typename Char = FMT_CHAR(S)>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800747inline int vfprintf(
748 std::basic_ostream<Char>& os, const S& format,
749 basic_format_args<
750 typename basic_printf_context_t<internal::basic_buffer<Char>>::type>
751 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200752 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700753 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200754 internal::write(os, buffer);
755 return static_cast<int>(buffer.size());
756}
757
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700758/**
759 \rst
760 Prints formatted data to the stream *os*.
761
762 **Example**::
763
Victor Zverovich40232912018-03-04 09:55:17 -0800764 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700765 \endrst
766 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700767template <typename S, typename... Args,
768 FMT_ENABLE_IF(internal::is_string<S>::value)>
769inline int fprintf(std::basic_ostream<FMT_CHAR(S)>& os, const S& format_str,
770 const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200771 internal::check_format_string<Args...>(format_str);
772 typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
tnovotnye37d6a92018-11-22 22:57:07 +0100773 typedef typename basic_printf_context_t<buffer>::type context;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800774 format_arg_store<context, Args...> as{args...};
Daniela Engert2c81c852018-10-08 20:14:39 +0200775 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200776 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200777}
Victor Zverovich838400d2018-05-12 08:33:51 -0700778FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200779
780#endif // FMT_PRINTF_H_