blob: e9a81669f175b910adf9af54c3ee6b6a1ec4983a [file] [log] [blame]
Victor Zverovichc0954452018-01-06 09:09:50 -08001// Formatting library for C++
2//
3// Copyright (c) 2012 - 2016, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
Glen Stark72d51e02016-06-08 01:23:32 +02007
8#ifndef FMT_PRINTF_H_
9#define FMT_PRINTF_H_
10
11#include <algorithm> // std::fill_n
12#include <limits> // std::numeric_limits
13
Victor Zverovichf853d942018-01-20 10:28:10 -080014#include "ostream.h"
Glen Stark72d51e02016-06-08 01:23:32 +020015
Victor Zverovich838400d2018-05-12 08:33:51 -070016FMT_BEGIN_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +020017namespace internal {
18
19// Checks if a value fits in int - used to avoid warnings about comparing
20// signed and unsigned integers.
21template <bool IsSigned>
Victor Zverovich418659a2018-03-03 14:04:59 -080022struct int_checker {
Glen Stark72d51e02016-06-08 01:23:32 +020023 template <typename T>
24 static bool fits_in_int(T value) {
25 unsigned max = std::numeric_limits<int>::max();
26 return value <= max;
27 }
28 static bool fits_in_int(bool) { return true; }
29};
30
31template <>
Victor Zverovich418659a2018-03-03 14:04:59 -080032struct int_checker<true> {
Glen Stark72d51e02016-06-08 01:23:32 +020033 template <typename T>
34 static bool fits_in_int(T value) {
35 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 Zverovich418659a2018-03-03 14:04:59 -080041class printf_precision_handler: public function<int> {
Glen Stark72d51e02016-06-08 01:23:32 +020042 public:
Glen Stark72d51e02016-06-08 01:23:32 +020043 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080044 typename std::enable_if<std::is_integral<T>::value, int>::type
45 operator()(T value) {
Victor Zverovich418659a2018-03-03 14:04:59 -080046 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070047 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +020048 return static_cast<int>(value);
49 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080050
51 template <typename T>
52 typename std::enable_if<!std::is_integral<T>::value, int>::type
53 operator()(T) {
54 FMT_THROW(format_error("precision is not integer"));
55 return 0;
56 }
Glen Stark72d51e02016-06-08 01:23:32 +020057};
58
Victor Zveroviche2dfd392016-11-19 09:29:09 -080059// An argument visitor that returns true iff arg is a zero integer.
Victor Zverovich418659a2018-03-03 14:04:59 -080060class is_zero_int: public function<bool> {
Glen Stark72d51e02016-06-08 01:23:32 +020061 public:
62 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080063 typename std::enable_if<std::is_integral<T>::value, bool>::type
64 operator()(T value) { return value == 0; }
65
66 template <typename T>
67 typename std::enable_if<!std::is_integral<T>::value, bool>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -070068 operator()(T) { return false; }
Glen Stark72d51e02016-06-08 01:23:32 +020069};
70
Victor Zverovich0fbd8462017-09-04 11:41:15 -070071template <typename T>
72struct make_unsigned_or_bool : std::make_unsigned<T> {};
73
74template <>
75struct make_unsigned_or_bool<bool> {
Victor Zverovichaffb35c2018-02-11 09:23:47 -080076 typedef bool type;
Victor Zverovich0fbd8462017-09-04 11:41:15 -070077};
78
Victor Zverovichd705d512016-12-29 09:07:39 -080079template <typename T, typename Context>
Victor Zverovich418659a2018-03-03 14:04:59 -080080class arg_converter: public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +020081 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080082 typedef typename Context::char_type Char;
83
Victor Zverovich23759b22018-04-04 07:38:21 -070084 basic_format_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080085 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020086
Glen Stark72d51e02016-06-08 01:23:32 +020087 public:
Victor Zverovich23759b22018-04-04 07:38:21 -070088 arg_converter(basic_format_arg<Context> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +020089 : arg_(arg), type_(type) {}
90
Victor Zverovich751ff642016-11-19 08:40:24 -080091 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020092 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080093 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020094 }
95
96 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -080097 typename std::enable_if<std::is_integral<U>::value>::type
98 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020099 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovichc18a4042017-09-04 13:56:14 -0700100 typedef typename std::conditional<
Victor Zverovichf194a412017-08-27 09:16:50 -0700101 std::is_same<T, void>::value, U, T>::type TargetType;
Victor Zverovich5013c152018-02-10 06:52:46 -0800102 if (const_check(sizeof(TargetType) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +0200103 // Extra casts are used to silence warnings.
104 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800105 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800106 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200107 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700108 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800109 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800110 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200111 }
112 } else {
113 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200114 // glibc's printf doesn't sign extend arguments of smaller types:
115 // std::printf("%lld", -42); // prints "4294967254"
116 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700117 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200118 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800119 arg_ = internal::make_arg<Context>(
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700120 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200121 }
122 }
123 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800124
125 template <typename U>
Victor Zverovich77c892c2017-08-27 08:16:46 -0700126 typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800127 // No coversion needed for non-integral types.
128 }
Glen Stark72d51e02016-06-08 01:23:32 +0200129};
130
Victor Zverovich751ff642016-11-19 08:40:24 -0800131// Converts an integer argument to T for printf, if T is an integral type.
132// If T is void, the argument is converted to corresponding signed or unsigned
133// type depending on the type specifier: 'd' and 'i' - signed, other -
134// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800135template <typename T, typename Context, typename Char>
Victor Zverovich23759b22018-04-04 07:38:21 -0700136void convert_arg(basic_format_arg<Context> &arg, Char type) {
Victor Zverovich418659a2018-03-03 14:04:59 -0800137 visit(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800138}
139
Glen Stark72d51e02016-06-08 01:23:32 +0200140// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800141template <typename Context>
Victor Zverovich418659a2018-03-03 14:04:59 -0800142class char_converter: public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200143 private:
Victor Zverovich23759b22018-04-04 07:38:21 -0700144 basic_format_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200145
Victor Zverovich418659a2018-03-03 14:04:59 -0800146 FMT_DISALLOW_COPY_AND_ASSIGN(char_converter);
Glen Stark72d51e02016-06-08 01:23:32 +0200147
148 public:
Victor Zverovich23759b22018-04-04 07:38:21 -0700149 explicit char_converter(basic_format_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200150
151 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800152 typename std::enable_if<std::is_integral<T>::value>::type
153 operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200154 typedef typename Context::char_type Char;
155 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200156 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800157
158 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800159 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800160 // No coversion needed for non-integral types.
161 }
Glen Stark72d51e02016-06-08 01:23:32 +0200162};
163
164// Checks if an argument is a valid printf width specifier and sets
165// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000166template <typename Char>
Victor Zverovich418659a2018-03-03 14:04:59 -0800167class printf_width_handler: public function<unsigned> {
Glen Stark72d51e02016-06-08 01:23:32 +0200168 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000169 typedef basic_format_specs<Char> format_specs;
170
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000171 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200172
Victor Zverovich418659a2018-03-03 14:04:59 -0800173 FMT_DISALLOW_COPY_AND_ASSIGN(printf_width_handler);
Glen Stark72d51e02016-06-08 01:23:32 +0200174
175 public:
Victor Zverovich418659a2018-03-03 14:04:59 -0800176 explicit printf_width_handler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200177
Glen Stark72d51e02016-06-08 01:23:32 +0200178 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800179 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
180 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800181 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200182 UnsignedType width = static_cast<UnsignedType>(value);
183 if (internal::is_negative(value)) {
184 spec_.align_ = ALIGN_LEFT;
185 width = 0 - width;
186 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700187 unsigned int_max = std::numeric_limits<int>::max();
188 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700189 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200190 return static_cast<unsigned>(width);
191 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800192
193 template <typename T>
194 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -0700195 operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800196 FMT_THROW(format_error("width is not integer"));
197 return 0;
198 }
Glen Stark72d51e02016-06-08 01:23:32 +0200199};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700200} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200201
Victor Zverovichc0954452018-01-06 09:09:50 -0800202template <typename Range>
Victor Zverovich10e70a02017-12-02 09:44:48 -0800203class printf_arg_formatter;
204
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800205template <
206 typename OutputIt, typename Char,
207 typename ArgFormatter =
208 printf_arg_formatter<back_insert_range<internal::basic_buffer<Char>>>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800209class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800210
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700211/**
212 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800213 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700214 \endrst
215 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800216template <typename Range>
Victor Zverovich418659a2018-03-03 14:04:59 -0800217class printf_arg_formatter:
Victor Zverovichedd5f142018-05-20 09:09:03 -0700218 public internal::function<
219 typename internal::arg_formatter_base<Range>::iterator>,
220 public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200221 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800222 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800223 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800224 typedef internal::arg_formatter_base<Range> base;
225 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800226
227 context_type &context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800228
Daniela Engert2570f1a2018-04-26 20:32:14 +0200229 void write_null_pointer(char) {
Glen Stark72d51e02016-06-08 01:23:32 +0200230 this->spec().type_ = 0;
231 this->write("(nil)");
232 }
233
Daniela Engert2570f1a2018-04-26 20:32:14 +0200234 void write_null_pointer(wchar_t) {
235 this->spec().type_ = 0;
236 this->write(L"(nil)");
237 }
238
Glen Stark72d51e02016-06-08 01:23:32 +0200239 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800240 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000241
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700242 /**
243 \rst
244 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500245 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700246 specifier information for standard argument types.
247 \endrst
248 */
Victor Zverovich9a53a702018-01-14 12:25:03 -0800249 printf_arg_formatter(internal::basic_buffer<char_type> &buffer,
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800250 format_specs &spec, context_type &ctx)
251 : base(back_insert_range<internal::basic_buffer<char_type>>(buffer), spec),
252 context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200253
Victor Zverovichc0954452018-01-06 09:09:50 -0800254 using base::operator();
Victor Zverovich95a53e12016-11-19 07:39:07 -0800255
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700256 /** Formats an argument of type ``bool``. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000257 iterator operator()(bool value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000258 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200259 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800260 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200261 fmt_spec.type_ = 0;
262 this->write(value);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000263 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200264 }
265
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700266 /** Formats a character. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000267 iterator operator()(char_type value) {
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700268 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200269 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700270 return (*this)(static_cast<int>(value));
271 fmt_spec.flags_ = 0;
272 fmt_spec.align_ = ALIGN_RIGHT;
Victor Zverovich3cf05262018-03-30 08:20:12 -1000273 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200274 }
275
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700276 /** Formats a null-terminated C string. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000277 iterator operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200278 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800279 base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200280 else if (this->spec().type_ == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200281 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200282 else
283 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000284 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200285 }
286
Daniela Engert2570f1a2018-04-26 20:32:14 +0200287 /** Formats a null-terminated wide C string. */
288 iterator operator()(const wchar_t *value) {
289 if (value)
290 base::operator()(value);
291 else if (this->spec().type_ == 'p')
292 write_null_pointer(char_type());
293 else
294 this->write(L"(null)");
295 return this->out();
296 }
297
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700298 /** Formats a pointer. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000299 iterator operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200300 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800301 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200302 this->spec().type_ = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200303 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000304 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200305 }
306
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700307 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700308 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800309 handle.format(context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000310 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700311 }
312};
313
Victor Zverovichc0954452018-01-06 09:09:50 -0800314template <typename T>
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700315struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700316 template <typename ParseContext>
Victor Zverovich1b452532018-02-28 05:09:24 -0800317 auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { return ctx.begin(); }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700318
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800319 template <typename FormatContext>
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700320 auto format(const T &value, FormatContext &ctx) -> decltype(ctx.out()) {
321 internal::format_value(internal::get_container(ctx.out()), value);
322 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200323 }
324};
325
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700326/** This template formats data and writes the output to a writer. */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800327template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800328class basic_printf_context :
329 private internal::context_base<
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800330 OutputIt, basic_printf_context<OutputIt, Char, ArgFormatter>, Char> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700331 public:
332 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800333 typedef Char char_type;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700334
335 template <typename T>
Victor Zverovich522de7b2018-02-11 08:32:02 -0800336 struct formatter_type { typedef printf_formatter<T> type; };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700337
Glen Stark72d51e02016-06-08 01:23:32 +0200338 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800339 typedef internal::context_base<OutputIt, basic_printf_context, Char> base;
340 typedef typename base::format_arg format_arg;
341 typedef basic_format_specs<char_type> format_specs;
342 typedef internal::null_terminating_iterator<char_type> iterator;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700343
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700344 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200345
346 // Returns the argument with specified index or, if arg_index is equal
347 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800348 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700349 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200350 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
351
352 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700353 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200354
355 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700356 /**
357 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800358 Constructs a ``printf_context`` object. References to the arguments and
359 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700360 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700361 \endrst
362 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800363 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800364 basic_format_args<basic_printf_context> args)
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800365 : base(out, format_str, args) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700366
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800367 using base::parse_context;
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700368 using base::out;
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800369 using base::advance_to;
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 Zverovich08dff372018-01-28 20:50:43 -0800372 void format();
Glen Stark72d51e02016-06-08 01:23:32 +0200373};
374
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800375template <typename OutputIt, typename Char, typename AF>
376void basic_printf_context<OutputIt, Char, AF>::parse_flags(
Victor Zverovich217e7c72018-01-14 07:19:23 -0800377 format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200378 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700379 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200380 case '-':
381 spec.align_ = ALIGN_LEFT;
382 break;
383 case '+':
384 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
385 break;
386 case '0':
387 spec.fill_ = '0';
388 break;
389 case ' ':
390 spec.flags_ |= SIGN_FLAG;
391 break;
392 case '#':
393 spec.flags_ |= HASH_FLAG;
394 break;
395 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700396 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200397 return;
398 }
399 }
400}
401
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800402template <typename OutputIt, typename Char, typename AF>
403typename basic_printf_context<OutputIt, Char, AF>::format_arg
404 basic_printf_context<OutputIt, Char, AF>::get_arg(
405 iterator it, unsigned arg_index) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700406 (void)it;
Victor Zverovichc523dd52017-11-19 07:36:01 -0800407 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich67928ea2018-01-14 09:27:40 -0800408 return this->do_get_arg(this->parse_context().next_arg_id());
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800409 return base::get_arg(arg_index - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200410}
411
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800412template <typename OutputIt, typename Char, typename AF>
413unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700414 iterator &it, 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;
421 unsigned value = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700422 if (*it == '$') { // value is an argument index
423 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200424 arg_index = value;
425 } else {
426 if (c == '0')
427 spec.fill_ = '0';
428 if (value != 0) {
429 // Nonzero value means that we parsed width and don't need to
430 // parse it or flags again, so return now.
431 spec.width_ = value;
432 return arg_index;
433 }
434 }
435 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700436 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200437 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700438 if (*it >= '0' && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700439 internal::error_handler eh;
440 spec.width_ = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700441 } else if (*it == '*') {
442 ++it;
Victor Zverovichc0954452018-01-06 09:09:50 -0800443 spec.width_ =
Victor Zverovich418659a2018-03-03 14:04:59 -0800444 visit(internal::printf_width_handler<char_type>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200445 }
446 return arg_index;
447}
448
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800449template <typename OutputIt, typename Char, typename AF>
450void basic_printf_context<OutputIt, Char, AF>::format() {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700451 auto &buffer = internal::get_container(this->out());
Victor Zverovich67928ea2018-01-14 09:27:40 -0800452 auto start = iterator(this->parse_context());
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700453 auto it = start;
454 using internal::pointer_from;
455 while (*it) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800456 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200457 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700458 if (*it == c) {
459 buffer.append(pointer_from(start), pointer_from(it));
460 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200461 continue;
462 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700463 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200464
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000465 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200466 spec.align_ = ALIGN_RIGHT;
467
468 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700469 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200470
471 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700472 if (*it == '.') {
473 ++it;
474 if ('0' <= *it && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700475 internal::error_handler eh;
476 spec.precision_ = static_cast<int>(parse_nonnegative_int(it, eh));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700477 } else if (*it == '*') {
478 ++it;
479 spec.precision_ =
Victor Zverovich418659a2018-03-03 14:04:59 -0800480 visit(internal::printf_precision_handler(), get_arg(it));
Victor Zverovich50605682018-01-27 17:56:19 -0800481 } else {
482 spec.precision_ = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200483 }
484 }
485
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700486 format_arg arg = get_arg(it, arg_index);
Victor Zverovich418659a2018-03-03 14:04:59 -0800487 if (spec.flag(HASH_FLAG) && visit(internal::is_zero_int(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700488 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200489 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800490 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200491 spec.align_ = ALIGN_NUMERIC;
492 else
493 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
494 }
495
496 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800497 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700498 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200499 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700500 if (*it == 'h')
501 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200502 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700503 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200504 break;
505 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700506 if (*it == 'l')
Victor Zverovich016aceb2017-08-26 09:09:43 -0700507 convert_arg<long long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200508 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700509 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200510 break;
511 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700512 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200513 break;
514 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700515 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200516 break;
517 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700518 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200519 break;
520 case 'L':
521 // printf produces garbage when 'L' is omitted for long double, no
522 // need to do the same.
523 break;
524 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700525 --it;
526 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200527 }
528
529 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700530 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700531 FMT_THROW(format_error("invalid format string"));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700532 spec.type_ = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800533 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200534 // Normalize type.
535 switch (spec.type_) {
536 case 'i': case 'u':
537 spec.type_ = 'd';
538 break;
539 case 'c':
Daniela Engert2570f1a2018-04-26 20:32:14 +0200540 // TODO: handle wchar_t better?
Victor Zverovich418659a2018-03-03 14:04:59 -0800541 visit(internal::char_converter<basic_printf_context>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200542 break;
543 }
544 }
545
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700546 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200547
548 // Format argument.
Victor Zverovich10e70a02017-12-02 09:44:48 -0800549 visit(AF(buffer, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200550 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700551 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200552}
Glen Stark72d51e02016-06-08 01:23:32 +0200553
Victor Zverovich217e7c72018-01-14 07:19:23 -0800554template <typename Char, typename Context>
Victor Zverovich9a53a702018-01-14 12:25:03 -0800555void printf(internal::basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800556 basic_format_args<Context> args) {
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800557 Context(std::back_inserter(buf), format, args).format();
Glen Stark72d51e02016-06-08 01:23:32 +0200558}
559
Victor Zverovich217e7c72018-01-14 07:19:23 -0800560template <typename Buffer>
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800561struct printf_context {
562 typedef basic_printf_context<
563 std::back_insert_iterator<Buffer>, typename Buffer::value_type> type;
564};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800565
Germán Méndez Bravo7a41d612018-03-26 12:42:40 -0600566template <typename ...Args>
Victor Zverovich23759b22018-04-04 07:38:21 -0700567inline format_arg_store<printf_context<internal::buffer>::type, Args...>
568 make_printf_args(const Args & ... args) {
569 return format_arg_store<printf_context<internal::buffer>::type, Args...>(
570 args...);
Germán Méndez Bravo7a41d612018-03-26 12:42:40 -0600571}
Victor Zverovich418659a2018-03-03 14:04:59 -0800572typedef basic_format_args<printf_context<internal::buffer>::type> printf_args;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200573typedef basic_format_args<printf_context<internal::wbuffer>::type> wprintf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800574
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700575inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800576 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500577 printf(buffer, format, args);
578 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700579}
580
Glen Stark72d51e02016-06-08 01:23:32 +0200581/**
582 \rst
583 Formats arguments and returns the result as a string.
584
585 **Example**::
586
587 std::string message = fmt::sprintf("The answer is %d", 42);
588 \endrst
589*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700590template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700591inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800592 return vsprintf(format_str,
Victor Zverovich7d286742018-04-08 07:21:26 -0700593 make_format_args<typename printf_context<internal::buffer>::type>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200594}
Glen Stark72d51e02016-06-08 01:23:32 +0200595
Daniela Engert2570f1a2018-04-26 20:32:14 +0200596inline std::wstring vsprintf(wstring_view format, wprintf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800597 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500598 printf(buffer, format, args);
599 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200600}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700601
602template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700603inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200604 return vsprintf(format_str,
605 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700606}
607
Daniela Engert2570f1a2018-04-26 20:32:14 +0200608template <typename Char>
609inline int vfprintf(std::FILE *f, basic_string_view<Char> format,
610 basic_format_args<typename printf_context<
611 internal::basic_buffer<Char>>::type> args) {
612 basic_memory_buffer<Char> buffer;
Victor Zverovich955062d2017-12-17 08:36:19 -0800613 printf(buffer, format, args);
614 std::size_t size = buffer.size();
615 return std::fwrite(
Daniela Engert2570f1a2018-04-26 20:32:14 +0200616 buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800617}
Glen Stark72d51e02016-06-08 01:23:32 +0200618
619/**
620 \rst
621 Prints formatted data to the file *f*.
622
623 **Example**::
624
625 fmt::fprintf(stderr, "Don't %s!", "panic");
626 \endrst
627 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700628template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700629inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovich7d286742018-04-08 07:21:26 -0700630 auto vargs = make_format_args<
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800631 typename printf_context<internal::buffer>::type>(args...);
Victor Zverovichedd5f142018-05-20 09:09:03 -0700632 return vfprintf<char>(f, format_str, vargs);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700633}
634
Daniela Engert2570f1a2018-04-26 20:32:14 +0200635template <typename... Args>
636inline int fprintf(std::FILE *f, wstring_view format_str,
637 const Args & ... args) {
638 return vfprintf(f, format_str,
639 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
640}
641
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700642inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700643 return vfprintf(stdout, format, args);
644}
Glen Stark72d51e02016-06-08 01:23:32 +0200645
Daniela Engert2570f1a2018-04-26 20:32:14 +0200646inline int vprintf(wstring_view format, wprintf_args args) {
647 return vfprintf(stdout, format, args);
648}
649
Glen Stark72d51e02016-06-08 01:23:32 +0200650/**
651 \rst
652 Prints formatted data to ``stdout``.
653
654 **Example**::
655
656 fmt::printf("Elapsed time: %.2f seconds", 1.23);
657 \endrst
658 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700659template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700660inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800661 return vprintf(format_str,
Victor Zverovich7d286742018-04-08 07:21:26 -0700662 make_format_args<typename printf_context<internal::buffer>::type>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200663}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700664
Daniela Engert2570f1a2018-04-26 20:32:14 +0200665template <typename... Args>
666inline int printf(wstring_view format_str, const Args & ... args) {
667 return vprintf(format_str,
668 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
669}
670
Victor Zverovichc0954452018-01-06 09:09:50 -0800671inline int vfprintf(std::ostream &os, string_view format_str,
672 printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800673 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500674 printf(buffer, format_str, args);
675 internal::write(os, buffer);
676 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700677}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700678
Daniela Engert2570f1a2018-04-26 20:32:14 +0200679inline int vfprintf(std::wostream &os, wstring_view format_str,
680 wprintf_args args) {
681 wmemory_buffer buffer;
682 printf(buffer, format_str, args);
683 internal::write(os, buffer);
684 return static_cast<int>(buffer.size());
685}
686
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700687/**
688 \rst
689 Prints formatted data to the stream *os*.
690
691 **Example**::
692
Victor Zverovich40232912018-03-04 09:55:17 -0800693 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700694 \endrst
695 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700696template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700697inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700698 const Args & ... args) {
Victor Zverovich7d286742018-04-08 07:21:26 -0700699 auto vargs = make_format_args<
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800700 typename printf_context<internal::buffer>::type>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700701 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700702}
Daniela Engert2570f1a2018-04-26 20:32:14 +0200703
704template <typename... Args>
705inline int fprintf(std::wostream &os, wstring_view format_str,
706 const Args & ... args) {
707 auto vargs = make_format_args<
708 typename printf_context<internal::buffer>::type>(args...);
709 return vfprintf(os, format_str, vargs);
710}
Victor Zverovich838400d2018-05-12 08:33:51 -0700711FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200712
713#endif // FMT_PRINTF_H_