blob: 29f36323a55c9d30d38b4409381ea30b695a93c5 [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"));
Victor Zverovichbd516e32019-04-21 07:39:41 -070047 return (std::max)(static_cast<int>(value), 0);
Glen Stark72d51e02016-06-08 01:23:32 +020048 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080049
Victor Zverovichc21c6b82019-03-16 12:58:18 -070050 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
51 int operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -080052 FMT_THROW(format_error("precision is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +020053 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -080054 }
Glen Stark72d51e02016-06-08 01:23:32 +020055};
56
Victor Zveroviche2dfd392016-11-19 09:29:09 -080057// An argument visitor that returns true iff arg is a zero integer.
Victor 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 Zverovich28083952019-04-07 10:05:49 -0700183void printf(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 Zverovich397e8dd2019-04-15 11:39:19 -0700200template <typename OutputIt, typename Char> class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800201
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700202/**
203 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800204 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700205 \endrst
206 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800207template <typename Range>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800208class printf_arg_formatter
209 : public internal::function<
210 typename internal::arg_formatter_base<Range>::iterator>,
211 public internal::arg_formatter_base<Range> {
ToolsDevler2b415b72019-04-11 11:05:55 +0200212 public:
Victor Zverovich8302c2f2019-05-30 08:34:17 -0700213 typedef decltype(std::declval<Range>().begin()) iterator;
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700214
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 Zverovichaffb35c2018-02-11 09:23:47 -0800217 typedef internal::arg_formatter_base<Range> base;
ToolsDevler2b415b72019-04-11 11:05:55 +0200218 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800219
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800220 context_type& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800221
Daniela Engert2570f1a2018-04-26 20:32:14 +0200222 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700223 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200224 this->write("(nil)");
225 }
226
Daniela Engert2570f1a2018-04-26 20:32:14 +0200227 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700228 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200229 this->write(L"(nil)");
230 }
231
Glen Stark72d51e02016-06-08 01:23:32 +0200232 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800233 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000234
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700235 /**
236 \rst
237 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500238 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700239 specifier information for standard argument types.
240 \endrst
241 */
Daniela Engertb0cde862019-01-01 11:45:56 +0100242 printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800243 : base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200244
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700245 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
246 iterator operator()(T value) {
Victor Zveroviche928b672018-07-04 11:36:38 -0700247 // MSVC2013 fails to compile separate overloads for bool and char_type so
248 // use std::is_same instead.
249 if (std::is_same<T, bool>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800250 format_specs& fmt_spec = *this->spec();
251 if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700252 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700253 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700254 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800255 format_specs& fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700256 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700257 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700258 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700259 fmt_spec.align_ = ALIGN_RIGHT;
260 return base::operator()(value);
261 } else {
262 return base::operator()(value);
263 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000264 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200265 }
266
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700267 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
268 iterator operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000269 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200270 }
271
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700272 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800273 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200274 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800275 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700276 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200277 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200278 else
279 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000280 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200281 }
282
Daniela Engert2570f1a2018-04-26 20:32:14 +0200283 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800284 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200285 if (value)
286 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700287 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200288 write_null_pointer(char_type());
289 else
290 this->write(L"(null)");
291 return this->out();
292 }
293
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700294 iterator operator()(basic_string_view<char_type> value) {
295 return base::operator()(value);
296 }
297
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800298 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700299
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700300 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800301 iterator operator()(const void* value) {
302 if (value) return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700303 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200304 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000305 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200306 }
307
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700308 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700309 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800310 handle.format(context_.parse_context(), context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000311 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700312 }
313};
314
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800315template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700316 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800317 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
318 return ctx.begin();
319 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700320
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800321 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800322 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700323 internal::format_value(internal::get_container(ctx.out()), value);
324 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200325 }
326};
327
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700328/** This template formats data and writes the output to a writer. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700329template <typename OutputIt, typename Char> class basic_printf_context {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700330 public:
331 /** The character type for the output. */
Victor Zverovichec665102019-06-02 16:04:17 -0700332 using char_type = Char;
333 using format_arg = basic_format_arg<basic_printf_context>;
334 template <typename T> using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700335
Glen Stark72d51e02016-06-08 01:23:32 +0200336 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800337 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700338
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800339 OutputIt out_;
340 basic_format_args<basic_printf_context> args_;
341 basic_parse_context<Char> parse_ctx_;
342
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800343 static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200344
345 // Returns the argument with specified index or, if arg_index is equal
346 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100347 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200348
349 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800350 unsigned parse_header(const Char*& it, const Char* end, format_specs& spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200351
352 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700353 /**
354 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800355 Constructs a ``printf_context`` object. References to the arguments and
356 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700357 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700358 \endrst
359 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800360 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800361 basic_format_args<basic_printf_context> args)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800362 : out_(out), args_(args), parse_ctx_(format_str) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700363
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800364 OutputIt out() { return out_; }
365 void advance_to(OutputIt it) { out_ = it; }
366
367 format_arg arg(unsigned id) const { return args_.get(id); }
368
369 basic_parse_context<Char>& parse_context() { return parse_ctx_; }
370
371 FMT_CONSTEXPR void on_error(const char* message) {
372 parse_ctx_.on_error(message);
373 }
Victor Zverovich9998f662016-11-06 16:11:24 -0800374
Victor Zverovichc0954452018-01-06 09:09:50 -0800375 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700376 template <typename ArgFormatter =
377 printf_arg_formatter<back_insert_range<internal::buffer<Char>>>>
Daniela Engertb0cde862019-01-01 11:45:56 +0100378 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200379};
380
ToolsDevler2b415b72019-04-11 11:05:55 +0200381template <typename OutputIt, typename Char>
382void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& spec,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700383 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
ToolsDevler2b415b72019-04-11 11:05:55 +0200408template <typename OutputIt, typename Char>
409typename basic_printf_context<OutputIt, Char>::format_arg
410basic_printf_context<OutputIt, Char>::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
ToolsDevler2b415b72019-04-11 11:05:55 +0200418template <typename OutputIt, typename Char>
419unsigned basic_printf_context<OutputIt, Char>::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
ToolsDevler2b415b72019-04-11 11:05:55 +0200456template <typename OutputIt, typename Char>
457template <typename ArgFormatter>
458OutputIt basic_printf_context<OutputIt, Char>::format() {
Daniela Engertb0cde862019-01-01 11:45:56 +0100459 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800460 const Char* start = parse_ctx_.begin();
461 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700462 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100463 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800464 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200465 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100466 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100467 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700468 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200469 continue;
470 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100471 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200472
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000473 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200474 spec.align_ = ALIGN_RIGHT;
475
476 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100477 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200478
479 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100480 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700481 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100482 c = it != end ? *it : 0;
483 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700484 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100485 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
486 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700487 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700488 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100489 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800490 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700491 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200492 }
493 }
494
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100495 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700496 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800497 spec.flags = static_cast<uint_least8_t>(
498 spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200499 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800500 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200501 spec.align_ = ALIGN_NUMERIC;
502 else
503 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
504 }
505
506 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100507 c = it != end ? *it++ : 0;
508 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800509 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100510 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200511 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100512 if (t == 'h') {
513 ++it;
514 t = it != end ? *it : 0;
515 convert_arg<signed char>(arg, t);
516 } else {
517 convert_arg<short>(arg, t);
518 }
Glen Stark72d51e02016-06-08 01:23:32 +0200519 break;
520 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100521 if (t == 'l') {
522 ++it;
523 t = it != end ? *it : 0;
524 convert_arg<long long>(arg, t);
525 } else {
526 convert_arg<long>(arg, t);
527 }
Glen Stark72d51e02016-06-08 01:23:32 +0200528 break;
529 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100530 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200531 break;
532 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100533 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200534 break;
535 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100536 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200537 break;
538 case 'L':
539 // printf produces garbage when 'L' is omitted for long double, no
540 // need to do the same.
541 break;
542 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700543 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100544 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200545 }
546
547 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800548 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700549 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800550 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200551 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700552 switch (spec.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800553 case 'i':
554 case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700555 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200556 break;
557 case 'c':
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800558 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
559 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200560 break;
561 }
562 }
563
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700564 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200565
566 // Format argument.
ToolsDevler2b415b72019-04-11 11:05:55 +0200567 visit_format_arg(ArgFormatter(out, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200568 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100569 return std::copy(start, it, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200570}
Glen Stark72d51e02016-06-08 01:23:32 +0200571
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800572template <typename Buffer> struct basic_printf_context_t {
573 typedef basic_printf_context<std::back_insert_iterator<Buffer>,
574 typename Buffer::value_type>
575 type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800576};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800577
Victor Zverovich28083952019-04-07 10:05:49 -0700578typedef basic_printf_context_t<internal::buffer<char>>::type printf_context;
579typedef basic_printf_context_t<internal::buffer<wchar_t>>::type wprintf_context;
tnovotnye37d6a92018-11-22 22:57:07 +0100580
581typedef basic_format_args<printf_context> printf_args;
582typedef basic_format_args<wprintf_context> wprintf_args;
583
584/**
585 \rst
586 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800587 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100588 \endrst
589 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800590template <typename... Args>
591inline format_arg_store<printf_context, Args...> make_printf_args(
592 const Args&... args) {
593 return {args...};
594}
tnovotnye37d6a92018-11-22 22:57:07 +0100595
596/**
597 \rst
598 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800599 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100600 \endrst
601 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800602template <typename... Args>
603inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
604 const Args&... args) {
605 return {args...};
606}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800607
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700608template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800609inline std::basic_string<Char> vsprintf(
610 const S& format,
611 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700612 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800613 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200614 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700615 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500616 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700617}
618
Glen Stark72d51e02016-06-08 01:23:32 +0200619/**
620 \rst
621 Formats arguments and returns the result as a string.
622
623 **Example**::
624
625 std::string message = fmt::sprintf("The answer is %d", 42);
626 \endrst
627*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700628template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700629 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
630inline std::basic_string<Char> sprintf(const S& format, const Args&... args) {
631 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800632 format_arg_store<context, Args...> as{args...};
633 return vsprintf(to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700634}
635
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700636template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800637inline int vfprintf(
638 std::FILE* f, const S& format,
639 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700640 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800641 args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200642 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700643 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800644 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800645 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
646 ? -1
647 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800648}
Glen Stark72d51e02016-06-08 01:23:32 +0200649
650/**
651 \rst
652 Prints formatted data to the file *f*.
653
654 **Example**::
655
656 fmt::fprintf(stderr, "Don't %s!", "panic");
657 \endrst
658 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700659template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700660 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700661inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700662 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800663 format_arg_store<context, Args...> as{args...};
664 return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700665}
666
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700667template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800668inline int vprintf(
669 const S& format,
670 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700671 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800672 args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700673 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200674}
675
Glen Stark72d51e02016-06-08 01:23:32 +0200676/**
677 \rst
678 Prints formatted data to ``stdout``.
679
680 **Example**::
681
682 fmt::printf("Elapsed time: %.2f seconds", 1.23);
683 \endrst
684 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700685template <typename S, typename... Args,
686 FMT_ENABLE_IF(internal::is_string<S>::value)>
687inline int printf(const S& format_str, const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200688 internal::check_format_string<Args...>(format_str);
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700689 using buffer = internal::buffer<char_t<S>>;
690 using context = typename basic_printf_context_t<buffer>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800691 format_arg_store<context, Args...> as{args...};
692 return vprintf(to_string_view(format_str), basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200693}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700694
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700695template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800696inline int vfprintf(
697 std::basic_ostream<Char>& os, const S& format,
698 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700699 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800700 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200701 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700702 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200703 internal::write(os, buffer);
704 return static_cast<int>(buffer.size());
705}
706
ToolsDevler2b415b72019-04-11 11:05:55 +0200707/** Formats arguments and writes the output to the range. */
708template <typename ArgFormatter, typename Char,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700709 typename Context =
710 basic_printf_context<typename ArgFormatter::iterator, Char>>
ToolsDevler2b415b72019-04-11 11:05:55 +0200711typename ArgFormatter::iterator vprintf(internal::buffer<Char>& out,
712 basic_string_view<Char> format_str,
713 basic_format_args<Context> args) {
714 typename ArgFormatter::iterator iter(out);
715 Context(iter, format_str, args).template format<ArgFormatter>();
716 return iter;
717}
718
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700719/**
720 \rst
721 Prints formatted data to the stream *os*.
722
723 **Example**::
724
Victor Zverovich40232912018-03-04 09:55:17 -0800725 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700726 \endrst
727 */
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700728template <typename S, typename... Args, typename Char = char_t<S>>
729inline int fprintf(std::basic_ostream<Char>& os, const S& format_str,
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700730 const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200731 internal::check_format_string<Args...>(format_str);
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700732 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800733 format_arg_store<context, Args...> as{args...};
Daniela Engert2c81c852018-10-08 20:14:39 +0200734 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200735 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200736}
Victor Zverovich838400d2018-05-12 08:33:51 -0700737FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200738
739#endif // FMT_PRINTF_H_