blob: d52f849c7600582ad453fd6e9c1e2edf374bd22b [file] [log] [blame]
Victor Zverovichc0954452018-01-06 09:09:50 -08001// Formatting library for C++
2//
3// Copyright (c) 2012 - 2016, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
Glen Stark72d51e02016-06-08 01:23:32 +02007
8#ifndef FMT_PRINTF_H_
9#define FMT_PRINTF_H_
10
11#include <algorithm> // std::fill_n
12#include <limits> // std::numeric_limits
13
Victor Zverovichf853d942018-01-20 10:28:10 -080014#include "ostream.h"
Glen Stark72d51e02016-06-08 01:23:32 +020015
Victor Zverovich838400d2018-05-12 08:33:51 -070016FMT_BEGIN_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +020017namespace internal {
18
Victor Zverovich294fd7d2019-03-17 14:49:19 -070019// A helper function to suppress bogus "conditional expression is constant"
20// warnings.
21template <typename T> inline T const_check(T value) { return value; }
22
Glen Stark72d51e02016-06-08 01:23:32 +020023// Checks if a value fits in int - used to avoid warnings about comparing
24// signed and unsigned integers.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080025template <bool IsSigned> struct int_checker {
26 template <typename T> static bool fits_in_int(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020027 unsigned max = std::numeric_limits<int>::max();
28 return value <= max;
29 }
30 static bool fits_in_int(bool) { return true; }
31};
32
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080033template <> struct int_checker<true> {
34 template <typename T> static bool fits_in_int(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020035 return value >= std::numeric_limits<int>::min() &&
36 value <= std::numeric_limits<int>::max();
37 }
38 static bool fits_in_int(int) { return true; }
39};
40
Victor Zverovich874d6722019-06-13 20:16:06 -070041class printf_precision_handler {
Glen Stark72d51e02016-06-08 01:23:32 +020042 public:
Victor Zverovichc21c6b82019-03-16 12:58:18 -070043 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
44 int operator()(T value) {
Victor Zverovich418659a2018-03-03 14:04:59 -080045 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070046 FMT_THROW(format_error("number is too big"));
Victor Zverovichbd516e32019-04-21 07:39:41 -070047 return (std::max)(static_cast<int>(value), 0);
Glen Stark72d51e02016-06-08 01:23:32 +020048 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080049
Victor Zverovichc21c6b82019-03-16 12:58:18 -070050 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
51 int operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -080052 FMT_THROW(format_error("precision is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +020053 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -080054 }
Glen Stark72d51e02016-06-08 01:23:32 +020055};
56
Victor Zveroviche2dfd392016-11-19 09:29:09 -080057// An argument visitor that returns true iff arg is a zero integer.
Victor Zverovich874d6722019-06-13 20:16:06 -070058class is_zero_int {
Glen Stark72d51e02016-06-08 01:23:32 +020059 public:
Victor Zverovichc21c6b82019-03-16 12:58:18 -070060 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
61 bool operator()(T value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080062 return value == 0;
63 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080064
Victor Zverovichc21c6b82019-03-16 12:58:18 -070065 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
66 bool operator()(T) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080067 return false;
68 }
Glen Stark72d51e02016-06-08 01:23:32 +020069};
70
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080071template <typename T> struct make_unsigned_or_bool : std::make_unsigned<T> {};
Victor Zverovich0fbd8462017-09-04 11:41:15 -070072
Victor 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 Zverovich874d6722019-06-13 20:16:06 -070075template <typename T, typename Context> class arg_converter {
Glen Stark72d51e02016-06-08 01:23:32 +020076 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080077 typedef typename Context::char_type Char;
78
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080079 basic_format_arg<Context>& arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080080 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020081
Glen Stark72d51e02016-06-08 01:23:32 +020082 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080083 arg_converter(basic_format_arg<Context>& arg, Char type)
84 : arg_(arg), type_(type) {}
Glen Stark72d51e02016-06-08 01:23:32 +020085
Victor Zverovich751ff642016-11-19 08:40:24 -080086 void operator()(bool value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -080087 if (type_ != 's') operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020088 }
89
Victor Zverovichc21c6b82019-03-16 12:58:18 -070090 template <typename U, FMT_ENABLE_IF(std::is_integral<U>::value)>
91 void operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020092 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovichc264e642019-06-04 18:50:30 -070093 using target_type = conditional_t<std::is_same<T, void>::value, U, T>;
94 if (const_check(sizeof(target_type) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +020095 // Extra casts are used to silence warnings.
96 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -080097 arg_ = internal::make_arg<Context>(
Victor Zverovichc264e642019-06-04 18:50:30 -070098 static_cast<int>(static_cast<target_type>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +020099 } else {
Victor Zverovichc264e642019-06-04 18:50:30 -0700100 typedef typename make_unsigned_or_bool<target_type>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800101 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800102 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200103 }
104 } else {
105 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200106 // glibc's printf doesn't sign extend arguments of smaller types:
107 // std::printf("%lld", -42); // prints "4294967254"
108 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700109 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200110 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800111 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800112 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200113 }
114 }
115 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800116
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700117 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)>
118 void operator()(U) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200119};
120
Victor Zverovich751ff642016-11-19 08:40:24 -0800121// Converts an integer argument to T for printf, if T is an integral type.
122// If T is void, the argument is converted to corresponding signed or unsigned
123// type depending on the type specifier: 'd' and 'i' - signed, other -
124// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800125template <typename T, typename Context, typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800126void convert_arg(basic_format_arg<Context>& arg, Char type) {
Victor Zverovichf5480632018-10-05 07:15:41 -0700127 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800128}
129
Glen Stark72d51e02016-06-08 01:23:32 +0200130// Converts an integer argument to char for printf.
Victor Zverovich874d6722019-06-13 20:16:06 -0700131template <typename Context> class char_converter {
Glen Stark72d51e02016-06-08 01:23:32 +0200132 private:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800133 basic_format_arg<Context>& arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200134
Glen Stark72d51e02016-06-08 01:23:32 +0200135 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800136 explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200137
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700138 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
139 void operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200140 typedef typename Context::char_type Char;
141 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200142 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800143
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700144 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
145 void operator()(T) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200146};
147
148// Checks if an argument is a valid printf width specifier and sets
149// left alignment if it is negative.
Victor Zverovich874d6722019-06-13 20:16:06 -0700150template <typename Char> class printf_width_handler {
Glen Stark72d51e02016-06-08 01:23:32 +0200151 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000152 typedef basic_format_specs<Char> format_specs;
153
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800154 format_specs& spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200155
Glen Stark72d51e02016-06-08 01:23:32 +0200156 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800157 explicit printf_width_handler(format_specs& spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200158
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700159 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
160 unsigned operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800161 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200162 UnsignedType width = static_cast<UnsignedType>(value);
163 if (internal::is_negative(value)) {
164 spec_.align_ = ALIGN_LEFT;
165 width = 0 - width;
166 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700167 unsigned int_max = std::numeric_limits<int>::max();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800168 if (width > int_max) FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200169 return static_cast<unsigned>(width);
170 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800171
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700172 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
173 unsigned operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800174 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200175 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800176 }
Glen Stark72d51e02016-06-08 01:23:32 +0200177};
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800178
179template <typename Char, typename Context>
Victor Zverovich28083952019-04-07 10:05:49 -0700180void printf(buffer<Char>& buf, basic_string_view<Char> format,
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800181 basic_format_args<Context> args) {
182 Context(std::back_inserter(buf), format, args).format();
183}
Daniela Engertb0cde862019-01-01 11:45:56 +0100184
185template <typename OutputIt, typename Char, typename Context>
186internal::truncating_iterator<OutputIt> printf(
187 internal::truncating_iterator<OutputIt> it, basic_string_view<Char> format,
188 basic_format_args<Context> args) {
189 return Context(it, format, args).format();
190}
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700191} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200192
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800193using internal::printf; // For printing into memory_buffer.
194
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800195template <typename Range> class printf_arg_formatter;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800196
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700197template <typename OutputIt, typename Char> class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800198
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700199/**
200 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800201 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700202 \endrst
203 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800204template <typename Range>
Victor Zverovich9d7b64a2019-06-11 22:11:47 -0700205class printf_arg_formatter : public internal::arg_formatter_base<Range> {
ToolsDevler2b415b72019-04-11 11:05:55 +0200206 public:
Victor Zverovich8302c2f2019-05-30 08:34:17 -0700207 typedef decltype(std::declval<Range>().begin()) iterator;
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700208
Glen Stark72d51e02016-06-08 01:23:32 +0200209 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800210 typedef typename Range::value_type char_type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800211 typedef internal::arg_formatter_base<Range> base;
ToolsDevler2b415b72019-04-11 11:05:55 +0200212 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800213
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800214 context_type& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800215
Daniela Engert2570f1a2018-04-26 20:32:14 +0200216 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700217 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200218 this->write("(nil)");
219 }
220
Daniela Engert2570f1a2018-04-26 20:32:14 +0200221 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700222 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200223 this->write(L"(nil)");
224 }
225
Glen Stark72d51e02016-06-08 01:23:32 +0200226 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800227 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000228
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700229 /**
230 \rst
231 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500232 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700233 specifier information for standard argument types.
234 \endrst
235 */
Daniela Engertb0cde862019-01-01 11:45:56 +0100236 printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800237 : base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200238
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700239 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
240 iterator operator()(T value) {
Victor Zveroviche928b672018-07-04 11:36:38 -0700241 // MSVC2013 fails to compile separate overloads for bool and char_type so
242 // use std::is_same instead.
243 if (std::is_same<T, bool>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800244 format_specs& fmt_spec = *this->spec();
245 if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700246 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700247 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700248 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800249 format_specs& fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700250 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700251 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700252 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700253 fmt_spec.align_ = ALIGN_RIGHT;
254 return base::operator()(value);
255 } else {
256 return base::operator()(value);
257 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000258 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200259 }
260
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700261 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
262 iterator operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000263 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200264 }
265
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700266 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800267 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200268 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800269 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700270 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200271 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200272 else
273 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000274 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200275 }
276
Daniela Engert2570f1a2018-04-26 20:32:14 +0200277 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800278 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200279 if (value)
280 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700281 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200282 write_null_pointer(char_type());
283 else
284 this->write(L"(null)");
285 return this->out();
286 }
287
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700288 iterator operator()(basic_string_view<char_type> value) {
289 return base::operator()(value);
290 }
291
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800292 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700293
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700294 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800295 iterator operator()(const void* value) {
296 if (value) return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700297 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200298 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000299 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200300 }
301
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700302 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700303 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800304 handle.format(context_.parse_context(), context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000305 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700306 }
307};
308
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800309template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700310 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800311 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
312 return ctx.begin();
313 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700314
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800315 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800316 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700317 internal::format_value(internal::get_container(ctx.out()), value);
318 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200319 }
320};
321
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700322/** This template formats data and writes the output to a writer. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700323template <typename OutputIt, typename Char> class basic_printf_context {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700324 public:
325 /** The character type for the output. */
Victor Zverovichec665102019-06-02 16:04:17 -0700326 using char_type = Char;
327 using format_arg = basic_format_arg<basic_printf_context>;
328 template <typename T> using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700329
Glen Stark72d51e02016-06-08 01:23:32 +0200330 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800331 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700332
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800333 OutputIt out_;
334 basic_format_args<basic_printf_context> args_;
335 basic_parse_context<Char> parse_ctx_;
336
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800337 static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200338
339 // Returns the argument with specified index or, if arg_index is equal
340 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100341 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200342
343 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800344 unsigned parse_header(const Char*& it, const Char* end, format_specs& spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200345
346 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700347 /**
348 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800349 Constructs a ``printf_context`` object. References to the arguments and
350 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700351 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700352 \endrst
353 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800354 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800355 basic_format_args<basic_printf_context> args)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800356 : out_(out), args_(args), parse_ctx_(format_str) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700357
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800358 OutputIt out() { return out_; }
359 void advance_to(OutputIt it) { out_ = it; }
360
361 format_arg arg(unsigned id) const { return args_.get(id); }
362
363 basic_parse_context<Char>& parse_context() { return parse_ctx_; }
364
365 FMT_CONSTEXPR void on_error(const char* message) {
366 parse_ctx_.on_error(message);
367 }
Victor Zverovich9998f662016-11-06 16:11:24 -0800368
Victor Zverovichc0954452018-01-06 09:09:50 -0800369 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700370 template <typename ArgFormatter =
371 printf_arg_formatter<back_insert_range<internal::buffer<Char>>>>
Daniela Engertb0cde862019-01-01 11:45:56 +0100372 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200373};
374
ToolsDevler2b415b72019-04-11 11:05:55 +0200375template <typename OutputIt, typename Char>
376void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& spec,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700377 const Char*& it,
378 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100379 for (; it != end; ++it) {
380 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800381 case '-':
382 spec.align_ = ALIGN_LEFT;
383 break;
384 case '+':
385 spec.flags |= SIGN_FLAG | PLUS_FLAG;
386 break;
387 case '0':
388 spec.fill_ = '0';
389 break;
390 case ' ':
391 spec.flags |= SIGN_FLAG;
392 break;
393 case '#':
394 spec.flags |= HASH_FLAG;
395 break;
396 default:
397 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200398 }
399 }
400}
401
ToolsDevler2b415b72019-04-11 11:05:55 +0200402template <typename OutputIt, typename Char>
403typename basic_printf_context<OutputIt, Char>::format_arg
404basic_printf_context<OutputIt, Char>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800405 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800406 arg_index = parse_ctx_.next_arg_id();
407 else
408 parse_ctx_.check_arg_id(--arg_index);
409 return internal::get_arg(*this, arg_index);
Glen Stark72d51e02016-06-08 01:23:32 +0200410}
411
ToolsDevler2b415b72019-04-11 11:05:55 +0200412template <typename OutputIt, typename Char>
413unsigned basic_printf_context<OutputIt, Char>::parse_header(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800414 const Char*& it, const Char* end, format_specs& spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200415 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800416 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200417 if (c >= '0' && c <= '9') {
418 // Parse an argument index (if followed by '$') or a width possibly
419 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700420 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100421 unsigned value = parse_nonnegative_int(it, end, eh);
422 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700423 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200424 arg_index = value;
425 } else {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800426 if (c == '0') spec.fill_ = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200427 if (value != 0) {
428 // Nonzero value means that we parsed width and don't need to
429 // parse it or flags again, so return now.
430 spec.width_ = value;
431 return arg_index;
432 }
433 }
434 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100435 parse_flags(spec, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200436 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100437 if (it != end) {
438 if (*it >= '0' && *it <= '9') {
439 internal::error_handler eh;
440 spec.width_ = parse_nonnegative_int(it, end, eh);
441 } else if (*it == '*') {
442 ++it;
443 spec.width_ = visit_format_arg(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800444 internal::printf_width_handler<char_type>(spec), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100445 }
Glen Stark72d51e02016-06-08 01:23:32 +0200446 }
447 return arg_index;
448}
449
ToolsDevler2b415b72019-04-11 11:05:55 +0200450template <typename OutputIt, typename Char>
451template <typename ArgFormatter>
452OutputIt basic_printf_context<OutputIt, Char>::format() {
Daniela Engertb0cde862019-01-01 11:45:56 +0100453 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800454 const Char* start = parse_ctx_.begin();
455 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700456 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100457 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800458 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200459 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100460 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100461 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700462 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200463 continue;
464 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100465 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200466
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000467 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200468 spec.align_ = ALIGN_RIGHT;
469
470 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100471 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200472
473 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100474 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700475 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100476 c = it != end ? *it : 0;
477 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700478 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100479 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
480 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700481 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700482 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100483 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800484 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700485 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200486 }
487 }
488
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100489 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700490 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800491 spec.flags = static_cast<uint_least8_t>(
492 spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200493 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800494 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200495 spec.align_ = ALIGN_NUMERIC;
496 else
497 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
498 }
499
500 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100501 c = it != end ? *it++ : 0;
502 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800503 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100504 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200505 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100506 if (t == 'h') {
507 ++it;
508 t = it != end ? *it : 0;
509 convert_arg<signed char>(arg, t);
510 } else {
511 convert_arg<short>(arg, t);
512 }
Glen Stark72d51e02016-06-08 01:23:32 +0200513 break;
514 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100515 if (t == 'l') {
516 ++it;
517 t = it != end ? *it : 0;
518 convert_arg<long long>(arg, t);
519 } else {
520 convert_arg<long>(arg, t);
521 }
Glen Stark72d51e02016-06-08 01:23:32 +0200522 break;
523 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100524 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200525 break;
526 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100527 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200528 break;
529 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100530 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200531 break;
532 case 'L':
533 // printf produces garbage when 'L' is omitted for long double, no
534 // need to do the same.
535 break;
536 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700537 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100538 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200539 }
540
541 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800542 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700543 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800544 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200545 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700546 switch (spec.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800547 case 'i':
548 case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700549 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200550 break;
551 case 'c':
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800552 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
553 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200554 break;
555 }
556 }
557
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700558 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200559
560 // Format argument.
ToolsDevler2b415b72019-04-11 11:05:55 +0200561 visit_format_arg(ArgFormatter(out, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200562 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100563 return std::copy(start, it, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200564}
Glen Stark72d51e02016-06-08 01:23:32 +0200565
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800566template <typename Buffer> struct basic_printf_context_t {
567 typedef basic_printf_context<std::back_insert_iterator<Buffer>,
568 typename Buffer::value_type>
569 type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800570};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800571
Victor Zverovich28083952019-04-07 10:05:49 -0700572typedef basic_printf_context_t<internal::buffer<char>>::type printf_context;
573typedef basic_printf_context_t<internal::buffer<wchar_t>>::type wprintf_context;
tnovotnye37d6a92018-11-22 22:57:07 +0100574
575typedef basic_format_args<printf_context> printf_args;
576typedef basic_format_args<wprintf_context> wprintf_args;
577
578/**
579 \rst
580 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800581 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100582 \endrst
583 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800584template <typename... Args>
585inline format_arg_store<printf_context, Args...> make_printf_args(
586 const Args&... args) {
587 return {args...};
588}
tnovotnye37d6a92018-11-22 22:57:07 +0100589
590/**
591 \rst
592 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800593 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100594 \endrst
595 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800596template <typename... Args>
597inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
598 const Args&... args) {
599 return {args...};
600}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800601
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700602template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800603inline std::basic_string<Char> vsprintf(
604 const S& format,
605 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700606 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800607 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200608 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700609 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500610 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700611}
612
Glen Stark72d51e02016-06-08 01:23:32 +0200613/**
614 \rst
615 Formats arguments and returns the result as a string.
616
617 **Example**::
618
619 std::string message = fmt::sprintf("The answer is %d", 42);
620 \endrst
621*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700622template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700623 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
624inline std::basic_string<Char> sprintf(const S& format, const Args&... args) {
625 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800626 format_arg_store<context, Args...> as{args...};
627 return vsprintf(to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700628}
629
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700630template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800631inline int vfprintf(
632 std::FILE* f, const S& format,
633 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700634 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800635 args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200636 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700637 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800638 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800639 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
640 ? -1
641 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800642}
Glen Stark72d51e02016-06-08 01:23:32 +0200643
644/**
645 \rst
646 Prints formatted data to the file *f*.
647
648 **Example**::
649
650 fmt::fprintf(stderr, "Don't %s!", "panic");
651 \endrst
652 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700653template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700654 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700655inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700656 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800657 format_arg_store<context, Args...> as{args...};
658 return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700659}
660
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700661template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800662inline int vprintf(
663 const S& format,
664 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700665 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800666 args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700667 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200668}
669
Glen Stark72d51e02016-06-08 01:23:32 +0200670/**
671 \rst
672 Prints formatted data to ``stdout``.
673
674 **Example**::
675
676 fmt::printf("Elapsed time: %.2f seconds", 1.23);
677 \endrst
678 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700679template <typename S, typename... Args,
680 FMT_ENABLE_IF(internal::is_string<S>::value)>
681inline int printf(const S& format_str, const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200682 internal::check_format_string<Args...>(format_str);
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700683 using buffer = internal::buffer<char_t<S>>;
684 using context = typename basic_printf_context_t<buffer>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800685 format_arg_store<context, Args...> as{args...};
686 return vprintf(to_string_view(format_str), basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200687}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700688
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700689template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800690inline int vfprintf(
691 std::basic_ostream<Char>& os, const S& format,
692 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700693 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800694 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200695 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700696 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200697 internal::write(os, buffer);
698 return static_cast<int>(buffer.size());
699}
700
ToolsDevler2b415b72019-04-11 11:05:55 +0200701/** Formats arguments and writes the output to the range. */
702template <typename ArgFormatter, typename Char,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700703 typename Context =
704 basic_printf_context<typename ArgFormatter::iterator, Char>>
ToolsDevler2b415b72019-04-11 11:05:55 +0200705typename ArgFormatter::iterator vprintf(internal::buffer<Char>& out,
706 basic_string_view<Char> format_str,
707 basic_format_args<Context> args) {
708 typename ArgFormatter::iterator iter(out);
709 Context(iter, format_str, args).template format<ArgFormatter>();
710 return iter;
711}
712
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700713/**
714 \rst
715 Prints formatted data to the stream *os*.
716
717 **Example**::
718
Victor Zverovich40232912018-03-04 09:55:17 -0800719 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700720 \endrst
721 */
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700722template <typename S, typename... Args, typename Char = char_t<S>>
723inline int fprintf(std::basic_ostream<Char>& os, const S& format_str,
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700724 const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200725 internal::check_format_string<Args...>(format_str);
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700726 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800727 format_arg_store<context, Args...> as{args...};
Daniela Engert2c81c852018-10-08 20:14:39 +0200728 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200729 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200730}
Victor Zverovich838400d2018-05-12 08:33:51 -0700731FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200732
733#endif // FMT_PRINTF_H_