blob: c3a48ed4794e9b0935b61b25e7d1032855697a18 [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 Zverovichc264e642019-06-04 18:50:30 -070094 using target_type = conditional_t<std::is_same<T, void>::value, U, T>;
95 if (const_check(sizeof(target_type) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +020096 // Extra casts are used to silence warnings.
97 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -080098 arg_ = internal::make_arg<Context>(
Victor Zverovichc264e642019-06-04 18:50:30 -070099 static_cast<int>(static_cast<target_type>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200100 } else {
Victor Zverovichc264e642019-06-04 18:50:30 -0700101 typedef typename make_unsigned_or_bool<target_type>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800102 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800103 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200104 }
105 } else {
106 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200107 // glibc's printf doesn't sign extend arguments of smaller types:
108 // std::printf("%lld", -42); // prints "4294967254"
109 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700110 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200111 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800112 arg_ = internal::make_arg<Context>(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800113 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200114 }
115 }
116 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800117
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700118 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)>
119 void operator()(U) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200120};
121
Victor Zverovich751ff642016-11-19 08:40:24 -0800122// Converts an integer argument to T for printf, if T is an integral type.
123// If T is void, the argument is converted to corresponding signed or unsigned
124// type depending on the type specifier: 'd' and 'i' - signed, other -
125// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800126template <typename T, typename Context, typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800127void convert_arg(basic_format_arg<Context>& arg, Char type) {
Victor Zverovichf5480632018-10-05 07:15:41 -0700128 visit_format_arg(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800129}
130
Glen Stark72d51e02016-06-08 01:23:32 +0200131// Converts an integer argument to char for printf.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800132template <typename Context> class char_converter : public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200133 private:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800134 basic_format_arg<Context>& arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200135
Glen Stark72d51e02016-06-08 01:23:32 +0200136 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800137 explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200138
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700139 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
140 void operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200141 typedef typename Context::char_type Char;
142 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200143 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800144
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700145 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
146 void operator()(T) {} // No conversion needed for non-integral types.
Glen Stark72d51e02016-06-08 01:23:32 +0200147};
148
149// Checks if an argument is a valid printf width specifier and sets
150// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000151template <typename Char>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800152class printf_width_handler : public function<unsigned> {
Glen Stark72d51e02016-06-08 01:23:32 +0200153 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000154 typedef basic_format_specs<Char> format_specs;
155
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800156 format_specs& spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200157
Glen Stark72d51e02016-06-08 01:23:32 +0200158 public:
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800159 explicit printf_width_handler(format_specs& spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200160
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700161 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
162 unsigned operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800163 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200164 UnsignedType width = static_cast<UnsignedType>(value);
165 if (internal::is_negative(value)) {
166 spec_.align_ = ALIGN_LEFT;
167 width = 0 - width;
168 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700169 unsigned int_max = std::numeric_limits<int>::max();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800170 if (width > int_max) FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200171 return static_cast<unsigned>(width);
172 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800173
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700174 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
175 unsigned operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800176 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200177 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800178 }
Glen Stark72d51e02016-06-08 01:23:32 +0200179};
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800180
181template <typename Char, typename Context>
Victor Zverovich28083952019-04-07 10:05:49 -0700182void printf(buffer<Char>& buf, basic_string_view<Char> format,
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800183 basic_format_args<Context> args) {
184 Context(std::back_inserter(buf), format, args).format();
185}
Daniela Engertb0cde862019-01-01 11:45:56 +0100186
187template <typename OutputIt, typename Char, typename Context>
188internal::truncating_iterator<OutputIt> printf(
189 internal::truncating_iterator<OutputIt> it, basic_string_view<Char> format,
190 basic_format_args<Context> args) {
191 return Context(it, format, args).format();
192}
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700193} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200194
Victor Zverovich8c2e15a2018-12-12 16:07:54 -0800195using internal::printf; // For printing into memory_buffer.
196
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800197template <typename Range> class printf_arg_formatter;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800198
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700199template <typename OutputIt, typename Char> class 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 Zverovich9d7b64a2019-06-11 22:11:47 -0700207class printf_arg_formatter : public internal::arg_formatter_base<Range> {
ToolsDevler2b415b72019-04-11 11:05:55 +0200208 public:
Victor Zverovich8302c2f2019-05-30 08:34:17 -0700209 typedef decltype(std::declval<Range>().begin()) iterator;
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700210
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 Zverovichaffb35c2018-02-11 09:23:47 -0800213 typedef internal::arg_formatter_base<Range> base;
ToolsDevler2b415b72019-04-11 11:05:55 +0200214 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800215
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800216 context_type& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800217
Daniela Engert2570f1a2018-04-26 20:32:14 +0200218 void write_null_pointer(char) {
Victor Zverovich29246222018-10-17 09:15:29 -0700219 this->spec()->type = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200220 this->write("(nil)");
221 }
222
Daniela Engert2570f1a2018-04-26 20:32:14 +0200223 void write_null_pointer(wchar_t) {
Victor Zverovich29246222018-10-17 09:15:29 -0700224 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200225 this->write(L"(nil)");
226 }
227
Glen Stark72d51e02016-06-08 01:23:32 +0200228 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800229 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000230
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700231 /**
232 \rst
233 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500234 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700235 specifier information for standard argument types.
236 \endrst
237 */
Daniela Engertb0cde862019-01-01 11:45:56 +0100238 printf_arg_formatter(iterator iter, format_specs& spec, context_type& ctx)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800239 : base(Range(iter), &spec, internal::locale_ref()), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200240
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700241 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
242 iterator operator()(T value) {
Victor Zveroviche928b672018-07-04 11:36:38 -0700243 // MSVC2013 fails to compile separate overloads for bool and char_type so
244 // use std::is_same instead.
245 if (std::is_same<T, bool>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800246 format_specs& fmt_spec = *this->spec();
247 if (fmt_spec.type != 's') return base::operator()(value ? 1 : 0);
Victor Zverovich29246222018-10-17 09:15:29 -0700248 fmt_spec.type = 0;
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700249 this->write(value != 0);
Victor Zveroviche928b672018-07-04 11:36:38 -0700250 } else if (std::is_same<T, char_type>::value) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800251 format_specs& fmt_spec = *this->spec();
Victor Zverovich29246222018-10-17 09:15:29 -0700252 if (fmt_spec.type && fmt_spec.type != 'c')
Victor Zveroviche928b672018-07-04 11:36:38 -0700253 return (*this)(static_cast<int>(value));
Victor Zverovich29246222018-10-17 09:15:29 -0700254 fmt_spec.flags = 0;
Victor Zveroviche928b672018-07-04 11:36:38 -0700255 fmt_spec.align_ = ALIGN_RIGHT;
256 return base::operator()(value);
257 } else {
258 return base::operator()(value);
259 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000260 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200261 }
262
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700263 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
264 iterator operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000265 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200266 }
267
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700268 /** Formats a null-terminated C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800269 iterator operator()(const char* value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200270 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800271 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700272 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200273 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200274 else
275 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000276 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200277 }
278
Daniela Engert2570f1a2018-04-26 20:32:14 +0200279 /** Formats a null-terminated wide C string. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800280 iterator operator()(const wchar_t* value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200281 if (value)
282 base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700283 else if (this->spec()->type == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200284 write_null_pointer(char_type());
285 else
286 this->write(L"(null)");
287 return this->out();
288 }
289
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700290 iterator operator()(basic_string_view<char_type> value) {
291 return base::operator()(value);
292 }
293
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800294 iterator operator()(monostate value) { return base::operator()(value); }
Victor Zverovich479ee2a2018-07-04 13:17:03 -0700295
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700296 /** Formats a pointer. */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800297 iterator operator()(const void* value) {
298 if (value) return base::operator()(value);
Victor Zverovich29246222018-10-17 09:15:29 -0700299 this->spec()->type = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200300 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000301 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200302 }
303
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700304 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700305 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800306 handle.format(context_.parse_context(), context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000307 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700308 }
309};
310
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800311template <typename T> struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700312 template <typename ParseContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800313 auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
314 return ctx.begin();
315 }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700316
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800317 template <typename FormatContext>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800318 auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700319 internal::format_value(internal::get_container(ctx.out()), value);
320 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200321 }
322};
323
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700324/** This template formats data and writes the output to a writer. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700325template <typename OutputIt, typename Char> class basic_printf_context {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700326 public:
327 /** The character type for the output. */
Victor Zverovichec665102019-06-02 16:04:17 -0700328 using char_type = Char;
329 using format_arg = basic_format_arg<basic_printf_context>;
330 template <typename T> using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700331
Glen Stark72d51e02016-06-08 01:23:32 +0200332 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800333 typedef basic_format_specs<char_type> format_specs;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700334
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800335 OutputIt out_;
336 basic_format_args<basic_printf_context> args_;
337 basic_parse_context<Char> parse_ctx_;
338
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800339 static void parse_flags(format_specs& spec, const Char*& it, const Char* end);
Glen Stark72d51e02016-06-08 01:23:32 +0200340
341 // Returns the argument with specified index or, if arg_index is equal
342 // to the maximum unsigned value, the next argument.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100343 format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
Glen Stark72d51e02016-06-08 01:23:32 +0200344
345 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800346 unsigned parse_header(const Char*& it, const Char* end, format_specs& spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200347
348 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700349 /**
350 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800351 Constructs a ``printf_context`` object. References to the arguments and
352 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700353 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700354 \endrst
355 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800356 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800357 basic_format_args<basic_printf_context> args)
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800358 : out_(out), args_(args), parse_ctx_(format_str) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700359
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800360 OutputIt out() { return out_; }
361 void advance_to(OutputIt it) { out_ = it; }
362
363 format_arg arg(unsigned id) const { return args_.get(id); }
364
365 basic_parse_context<Char>& parse_context() { return parse_ctx_; }
366
367 FMT_CONSTEXPR void on_error(const char* message) {
368 parse_ctx_.on_error(message);
369 }
Victor Zverovich9998f662016-11-06 16:11:24 -0800370
Victor Zverovichc0954452018-01-06 09:09:50 -0800371 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700372 template <typename ArgFormatter =
373 printf_arg_formatter<back_insert_range<internal::buffer<Char>>>>
Daniela Engertb0cde862019-01-01 11:45:56 +0100374 OutputIt format();
Glen Stark72d51e02016-06-08 01:23:32 +0200375};
376
ToolsDevler2b415b72019-04-11 11:05:55 +0200377template <typename OutputIt, typename Char>
378void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& spec,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700379 const Char*& it,
380 const Char* end) {
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100381 for (; it != end; ++it) {
382 switch (*it) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800383 case '-':
384 spec.align_ = ALIGN_LEFT;
385 break;
386 case '+':
387 spec.flags |= SIGN_FLAG | PLUS_FLAG;
388 break;
389 case '0':
390 spec.fill_ = '0';
391 break;
392 case ' ':
393 spec.flags |= SIGN_FLAG;
394 break;
395 case '#':
396 spec.flags |= HASH_FLAG;
397 break;
398 default:
399 return;
Glen Stark72d51e02016-06-08 01:23:32 +0200400 }
401 }
402}
403
ToolsDevler2b415b72019-04-11 11:05:55 +0200404template <typename OutputIt, typename Char>
405typename basic_printf_context<OutputIt, Char>::format_arg
406basic_printf_context<OutputIt, Char>::get_arg(unsigned arg_index) {
Victor Zverovichc523dd52017-11-19 07:36:01 -0800407 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800408 arg_index = parse_ctx_.next_arg_id();
409 else
410 parse_ctx_.check_arg_id(--arg_index);
411 return internal::get_arg(*this, arg_index);
Glen Stark72d51e02016-06-08 01:23:32 +0200412}
413
ToolsDevler2b415b72019-04-11 11:05:55 +0200414template <typename OutputIt, typename Char>
415unsigned basic_printf_context<OutputIt, Char>::parse_header(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800416 const Char*& it, const Char* end, format_specs& spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200417 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800418 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200419 if (c >= '0' && c <= '9') {
420 // Parse an argument index (if followed by '$') or a width possibly
421 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700422 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100423 unsigned value = parse_nonnegative_int(it, end, eh);
424 if (it != end && *it == '$') { // value is an argument index
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700425 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200426 arg_index = value;
427 } else {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800428 if (c == '0') spec.fill_ = '0';
Glen Stark72d51e02016-06-08 01:23:32 +0200429 if (value != 0) {
430 // Nonzero value means that we parsed width and don't need to
431 // parse it or flags again, so return now.
432 spec.width_ = value;
433 return arg_index;
434 }
435 }
436 }
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100437 parse_flags(spec, it, end);
Glen Stark72d51e02016-06-08 01:23:32 +0200438 // Parse width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100439 if (it != end) {
440 if (*it >= '0' && *it <= '9') {
441 internal::error_handler eh;
442 spec.width_ = parse_nonnegative_int(it, end, eh);
443 } else if (*it == '*') {
444 ++it;
445 spec.width_ = visit_format_arg(
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800446 internal::printf_width_handler<char_type>(spec), get_arg());
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100447 }
Glen Stark72d51e02016-06-08 01:23:32 +0200448 }
449 return arg_index;
450}
451
ToolsDevler2b415b72019-04-11 11:05:55 +0200452template <typename OutputIt, typename Char>
453template <typename ArgFormatter>
454OutputIt basic_printf_context<OutputIt, Char>::format() {
Daniela Engertb0cde862019-01-01 11:45:56 +0100455 auto out = this->out();
Victor Zverovich442fa1b2019-02-09 19:34:42 -0800456 const Char* start = parse_ctx_.begin();
457 const Char* end = parse_ctx_.end();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700458 auto it = start;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100459 while (it != end) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800460 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200461 if (c != '%') continue;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100462 if (it != end && *it == c) {
Daniela Engertb0cde862019-01-01 11:45:56 +0100463 out = std::copy(start, it, out);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700464 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200465 continue;
466 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100467 out = std::copy(start, it - 1, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200468
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000469 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200470 spec.align_ = ALIGN_RIGHT;
471
472 // Parse argument index, flags and width.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100473 unsigned arg_index = parse_header(it, end, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200474
475 // Parse precision.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100476 if (it != end && *it == '.') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700477 ++it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100478 c = it != end ? *it : 0;
479 if ('0' <= c && c <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700480 internal::error_handler eh;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100481 spec.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
482 } else if (c == '*') {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700483 ++it;
Victor Zverovich29246222018-10-17 09:15:29 -0700484 spec.precision =
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100485 visit_format_arg(internal::printf_precision_handler(), get_arg());
Victor Zverovich50605682018-01-27 17:56:19 -0800486 } else {
Victor Zverovich29246222018-10-17 09:15:29 -0700487 spec.precision = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200488 }
489 }
490
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100491 format_arg arg = get_arg(arg_index);
Victor Zverovich29246222018-10-17 09:15:29 -0700492 if (spec.has(HASH_FLAG) && visit_format_arg(internal::is_zero_int(), arg))
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800493 spec.flags = static_cast<uint_least8_t>(
494 spec.flags & (~internal::to_unsigned<int>(HASH_FLAG)));
Glen Stark72d51e02016-06-08 01:23:32 +0200495 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800496 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200497 spec.align_ = ALIGN_NUMERIC;
498 else
499 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
500 }
501
502 // Parse length and convert the argument to the required type.
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100503 c = it != end ? *it++ : 0;
504 char_type t = it != end ? *it : 0;
Victor Zverovich751ff642016-11-19 08:40:24 -0800505 using internal::convert_arg;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100506 switch (c) {
Glen Stark72d51e02016-06-08 01:23:32 +0200507 case 'h':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100508 if (t == 'h') {
509 ++it;
510 t = it != end ? *it : 0;
511 convert_arg<signed char>(arg, t);
512 } else {
513 convert_arg<short>(arg, t);
514 }
Glen Stark72d51e02016-06-08 01:23:32 +0200515 break;
516 case 'l':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100517 if (t == 'l') {
518 ++it;
519 t = it != end ? *it : 0;
520 convert_arg<long long>(arg, t);
521 } else {
522 convert_arg<long>(arg, t);
523 }
Glen Stark72d51e02016-06-08 01:23:32 +0200524 break;
525 case 'j':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100526 convert_arg<intmax_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200527 break;
528 case 'z':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100529 convert_arg<std::size_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200530 break;
531 case 't':
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100532 convert_arg<std::ptrdiff_t>(arg, t);
Glen Stark72d51e02016-06-08 01:23:32 +0200533 break;
534 case 'L':
535 // printf produces garbage when 'L' is omitted for long double, no
536 // need to do the same.
537 break;
538 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700539 --it;
Daniela Engertf5cc77c2018-12-31 16:21:45 +0100540 convert_arg<void>(arg, c);
Glen Stark72d51e02016-06-08 01:23:32 +0200541 }
542
543 // Parse type.
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800544 if (it == end) FMT_THROW(format_error("invalid format string"));
Victor Zverovich29246222018-10-17 09:15:29 -0700545 spec.type = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800546 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200547 // Normalize type.
Victor Zverovich29246222018-10-17 09:15:29 -0700548 switch (spec.type) {
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800549 case 'i':
550 case 'u':
Victor Zverovich29246222018-10-17 09:15:29 -0700551 spec.type = 'd';
Glen Stark72d51e02016-06-08 01:23:32 +0200552 break;
553 case 'c':
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800554 visit_format_arg(internal::char_converter<basic_printf_context>(arg),
555 arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200556 break;
557 }
558 }
559
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700560 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200561
562 // Format argument.
ToolsDevler2b415b72019-04-11 11:05:55 +0200563 visit_format_arg(ArgFormatter(out, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200564 }
Daniela Engertb0cde862019-01-01 11:45:56 +0100565 return std::copy(start, it, out);
Glen Stark72d51e02016-06-08 01:23:32 +0200566}
Glen Stark72d51e02016-06-08 01:23:32 +0200567
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800568template <typename Buffer> struct basic_printf_context_t {
569 typedef basic_printf_context<std::back_insert_iterator<Buffer>,
570 typename Buffer::value_type>
571 type;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800572};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800573
Victor Zverovich28083952019-04-07 10:05:49 -0700574typedef basic_printf_context_t<internal::buffer<char>>::type printf_context;
575typedef basic_printf_context_t<internal::buffer<wchar_t>>::type wprintf_context;
tnovotnye37d6a92018-11-22 22:57:07 +0100576
577typedef basic_format_args<printf_context> printf_args;
578typedef basic_format_args<wprintf_context> wprintf_args;
579
580/**
581 \rst
582 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800583 arguments and can be implicitly converted to `~fmt::printf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100584 \endrst
585 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800586template <typename... Args>
587inline format_arg_store<printf_context, Args...> make_printf_args(
588 const Args&... args) {
589 return {args...};
590}
tnovotnye37d6a92018-11-22 22:57:07 +0100591
592/**
593 \rst
594 Constructs an `~fmt::format_arg_store` object that contains references to
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800595 arguments and can be implicitly converted to `~fmt::wprintf_args`.
tnovotnye37d6a92018-11-22 22:57:07 +0100596 \endrst
597 */
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800598template <typename... Args>
599inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
600 const Args&... args) {
601 return {args...};
602}
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800603
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700604template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800605inline std::basic_string<Char> vsprintf(
606 const S& format,
607 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700608 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800609 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200610 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700611 printf(buffer, to_string_view(format), args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500612 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700613}
614
Glen Stark72d51e02016-06-08 01:23:32 +0200615/**
616 \rst
617 Formats arguments and returns the result as a string.
618
619 **Example**::
620
621 std::string message = fmt::sprintf("The answer is %d", 42);
622 \endrst
623*/
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700624template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700625 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
626inline std::basic_string<Char> sprintf(const S& format, const Args&... args) {
627 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800628 format_arg_store<context, Args...> as{args...};
629 return vsprintf(to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700630}
631
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700632template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800633inline int vfprintf(
634 std::FILE* f, const S& format,
635 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700636 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800637 args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200638 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700639 printf(buffer, to_string_view(format), args);
Victor Zverovich955062d2017-12-17 08:36:19 -0800640 std::size_t size = buffer.size();
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800641 return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
642 ? -1
643 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800644}
Glen Stark72d51e02016-06-08 01:23:32 +0200645
646/**
647 \rst
648 Prints formatted data to the file *f*.
649
650 **Example**::
651
652 fmt::fprintf(stderr, "Don't %s!", "panic");
653 \endrst
654 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700655template <typename S, typename... Args,
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700656 typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700657inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700658 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800659 format_arg_store<context, Args...> as{args...};
660 return vfprintf(f, to_string_view(format), basic_format_args<context>(as));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700661}
662
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700663template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800664inline int vprintf(
665 const S& format,
666 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700667 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800668 args) {
Victor Zverovich0a96c032018-10-25 07:20:02 -0700669 return vfprintf(stdout, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200670}
671
Glen Stark72d51e02016-06-08 01:23:32 +0200672/**
673 \rst
674 Prints formatted data to ``stdout``.
675
676 **Example**::
677
678 fmt::printf("Elapsed time: %.2f seconds", 1.23);
679 \endrst
680 */
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700681template <typename S, typename... Args,
682 FMT_ENABLE_IF(internal::is_string<S>::value)>
683inline int printf(const S& format_str, const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200684 internal::check_format_string<Args...>(format_str);
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700685 using buffer = internal::buffer<char_t<S>>;
686 using context = typename basic_printf_context_t<buffer>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800687 format_arg_store<context, Args...> as{args...};
688 return vprintf(to_string_view(format_str), basic_format_args<context>(as));
Glen Stark72d51e02016-06-08 01:23:32 +0200689}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700690
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700691template <typename S, typename Char = char_t<S>>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800692inline int vfprintf(
693 std::basic_ostream<Char>& os, const S& format,
694 basic_format_args<
Victor Zverovich28083952019-04-07 10:05:49 -0700695 typename basic_printf_context_t<internal::buffer<Char>>::type>
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800696 args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200697 basic_memory_buffer<Char> buffer;
Victor Zverovich0a96c032018-10-25 07:20:02 -0700698 printf(buffer, to_string_view(format), args);
Daniela Engert2570f1a2018-04-26 20:32:14 +0200699 internal::write(os, buffer);
700 return static_cast<int>(buffer.size());
701}
702
ToolsDevler2b415b72019-04-11 11:05:55 +0200703/** Formats arguments and writes the output to the range. */
704template <typename ArgFormatter, typename Char,
Victor Zverovich397e8dd2019-04-15 11:39:19 -0700705 typename Context =
706 basic_printf_context<typename ArgFormatter::iterator, Char>>
ToolsDevler2b415b72019-04-11 11:05:55 +0200707typename ArgFormatter::iterator vprintf(internal::buffer<Char>& out,
708 basic_string_view<Char> format_str,
709 basic_format_args<Context> args) {
710 typename ArgFormatter::iterator iter(out);
711 Context(iter, format_str, args).template format<ArgFormatter>();
712 return iter;
713}
714
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700715/**
716 \rst
717 Prints formatted data to the stream *os*.
718
719 **Example**::
720
Victor Zverovich40232912018-03-04 09:55:17 -0800721 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700722 \endrst
723 */
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700724template <typename S, typename... Args, typename Char = char_t<S>>
725inline int fprintf(std::basic_ostream<Char>& os, const S& format_str,
Victor Zverovichc21c6b82019-03-16 12:58:18 -0700726 const Args&... args) {
Daniela Engertf27defc2018-10-07 14:38:29 +0200727 internal::check_format_string<Args...>(format_str);
Victor Zverovich4d4b8c22019-06-01 12:32:24 -0700728 using context = typename basic_printf_context_t<internal::buffer<Char>>::type;
Victor Zverovich58b6f8d2019-01-12 18:27:38 -0800729 format_arg_store<context, Args...> as{args...};
Daniela Engert2c81c852018-10-08 20:14:39 +0200730 return vfprintf(os, to_string_view(format_str),
Daniela Engertf27defc2018-10-07 14:38:29 +0200731 basic_format_args<context>(as));
Daniela Engert2570f1a2018-04-26 20:32:14 +0200732}
Victor Zverovich838400d2018-05-12 08:33:51 -0700733FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200734
735#endif // FMT_PRINTF_H_