blob: ba600ca4d0dd4bd3e164b24cc13a80b761212c02 [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>
Victor Zverovich6322b472018-06-06 16:51:35 +020052 typename std::enable_if<!std::is_integral<T>::value, int>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -080053 FMT_THROW(format_error("precision is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +020054 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -080055 }
Glen Stark72d51e02016-06-08 01:23:32 +020056};
57
Victor Zveroviche2dfd392016-11-19 09:29:09 -080058// An argument visitor that returns true iff arg is a zero integer.
Victor Zverovich418659a2018-03-03 14:04:59 -080059class is_zero_int: public function<bool> {
Glen Stark72d51e02016-06-08 01:23:32 +020060 public:
61 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080062 typename std::enable_if<std::is_integral<T>::value, bool>::type
63 operator()(T value) { return value == 0; }
64
65 template <typename T>
66 typename std::enable_if<!std::is_integral<T>::value, bool>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -070067 operator()(T) { return false; }
Glen Stark72d51e02016-06-08 01:23:32 +020068};
69
Victor Zverovich0fbd8462017-09-04 11:41:15 -070070template <typename T>
71struct make_unsigned_or_bool : std::make_unsigned<T> {};
72
73template <>
74struct make_unsigned_or_bool<bool> {
Victor Zverovichaffb35c2018-02-11 09:23:47 -080075 typedef bool type;
Victor Zverovich0fbd8462017-09-04 11:41:15 -070076};
77
Victor Zverovichd705d512016-12-29 09:07:39 -080078template <typename T, typename Context>
Victor Zverovich418659a2018-03-03 14:04:59 -080079class arg_converter: public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +020080 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080081 typedef typename Context::char_type Char;
82
Victor Zverovich23759b22018-04-04 07:38:21 -070083 basic_format_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080084 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020085
Glen Stark72d51e02016-06-08 01:23:32 +020086 public:
Victor Zverovich23759b22018-04-04 07:38:21 -070087 arg_converter(basic_format_arg<Context> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +020088 : arg_(arg), type_(type) {}
89
Victor Zverovich751ff642016-11-19 08:40:24 -080090 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020091 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080092 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020093 }
94
95 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -080096 typename std::enable_if<std::is_integral<U>::value>::type
97 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020098 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovichc18a4042017-09-04 13:56:14 -070099 typedef typename std::conditional<
Victor Zverovichf194a412017-08-27 09:16:50 -0700100 std::is_same<T, void>::value, U, T>::type TargetType;
Victor Zverovich5013c152018-02-10 06:52:46 -0800101 if (const_check(sizeof(TargetType) <= sizeof(int))) {
Glen Stark72d51e02016-06-08 01:23:32 +0200102 // Extra casts are used to silence warnings.
103 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800104 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800105 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200106 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700107 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800108 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800109 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200110 }
111 } else {
112 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200113 // glibc's printf doesn't sign extend arguments of smaller types:
114 // std::printf("%lld", -42); // prints "4294967254"
115 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700116 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200117 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800118 arg_ = internal::make_arg<Context>(
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700119 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200120 }
121 }
122 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800123
124 template <typename U>
Victor Zverovich77c892c2017-08-27 08:16:46 -0700125 typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800126 // No coversion needed for non-integral types.
127 }
Glen Stark72d51e02016-06-08 01:23:32 +0200128};
129
Victor Zverovich751ff642016-11-19 08:40:24 -0800130// Converts an integer argument to T for printf, if T is an integral type.
131// If T is void, the argument is converted to corresponding signed or unsigned
132// type depending on the type specifier: 'd' and 'i' - signed, other -
133// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800134template <typename T, typename Context, typename Char>
Victor Zverovich23759b22018-04-04 07:38:21 -0700135void convert_arg(basic_format_arg<Context> &arg, Char type) {
Victor Zverovich418659a2018-03-03 14:04:59 -0800136 visit(arg_converter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800137}
138
Glen Stark72d51e02016-06-08 01:23:32 +0200139// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800140template <typename Context>
Victor Zverovich418659a2018-03-03 14:04:59 -0800141class char_converter: public function<void> {
Glen Stark72d51e02016-06-08 01:23:32 +0200142 private:
Victor Zverovich23759b22018-04-04 07:38:21 -0700143 basic_format_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200144
Victor Zverovich418659a2018-03-03 14:04:59 -0800145 FMT_DISALLOW_COPY_AND_ASSIGN(char_converter);
Glen Stark72d51e02016-06-08 01:23:32 +0200146
147 public:
Victor Zverovich23759b22018-04-04 07:38:21 -0700148 explicit char_converter(basic_format_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200149
150 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800151 typename std::enable_if<std::is_integral<T>::value>::type
152 operator()(T value) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200153 typedef typename Context::char_type Char;
154 arg_ = internal::make_arg<Context>(static_cast<Char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200155 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800156
157 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800158 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800159 // No coversion needed for non-integral types.
160 }
Glen Stark72d51e02016-06-08 01:23:32 +0200161};
162
163// Checks if an argument is a valid printf width specifier and sets
164// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000165template <typename Char>
Victor Zverovich418659a2018-03-03 14:04:59 -0800166class printf_width_handler: public function<unsigned> {
Glen Stark72d51e02016-06-08 01:23:32 +0200167 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000168 typedef basic_format_specs<Char> format_specs;
169
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000170 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200171
Victor Zverovich418659a2018-03-03 14:04:59 -0800172 FMT_DISALLOW_COPY_AND_ASSIGN(printf_width_handler);
Glen Stark72d51e02016-06-08 01:23:32 +0200173
174 public:
Victor Zverovich418659a2018-03-03 14:04:59 -0800175 explicit printf_width_handler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200176
Glen Stark72d51e02016-06-08 01:23:32 +0200177 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800178 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
179 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800180 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200181 UnsignedType width = static_cast<UnsignedType>(value);
182 if (internal::is_negative(value)) {
183 spec_.align_ = ALIGN_LEFT;
184 width = 0 - width;
185 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700186 unsigned int_max = std::numeric_limits<int>::max();
187 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700188 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200189 return static_cast<unsigned>(width);
190 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800191
192 template <typename T>
193 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -0700194 operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800195 FMT_THROW(format_error("width is not integer"));
Victor Zverovich6322b472018-06-06 16:51:35 +0200196 return 0;
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800197 }
Glen Stark72d51e02016-06-08 01:23:32 +0200198};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700199} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200200
Victor Zverovichc0954452018-01-06 09:09:50 -0800201template <typename Range>
Victor Zverovich10e70a02017-12-02 09:44:48 -0800202class printf_arg_formatter;
203
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800204template <
205 typename OutputIt, typename Char,
206 typename ArgFormatter =
207 printf_arg_formatter<back_insert_range<internal::basic_buffer<Char>>>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800208class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800209
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700210/**
211 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800212 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700213 \endrst
214 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800215template <typename Range>
Victor Zverovich418659a2018-03-03 14:04:59 -0800216class printf_arg_formatter:
Victor Zverovichedd5f142018-05-20 09:09:03 -0700217 public internal::function<
218 typename internal::arg_formatter_base<Range>::iterator>,
219 public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200220 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800221 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800222 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800223 typedef internal::arg_formatter_base<Range> base;
224 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800225
226 context_type &context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800227
Daniela Engert2570f1a2018-04-26 20:32:14 +0200228 void write_null_pointer(char) {
Glen Stark72d51e02016-06-08 01:23:32 +0200229 this->spec().type_ = 0;
230 this->write("(nil)");
231 }
232
Daniela Engert2570f1a2018-04-26 20:32:14 +0200233 void write_null_pointer(wchar_t) {
234 this->spec().type_ = 0;
235 this->write(L"(nil)");
236 }
237
Glen Stark72d51e02016-06-08 01:23:32 +0200238 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800239 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000240
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700241 /**
242 \rst
243 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500244 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700245 specifier information for standard argument types.
246 \endrst
247 */
Victor Zverovich9a53a702018-01-14 12:25:03 -0800248 printf_arg_formatter(internal::basic_buffer<char_type> &buffer,
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800249 format_specs &spec, context_type &ctx)
250 : base(back_insert_range<internal::basic_buffer<char_type>>(buffer), spec),
251 context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200252
Victor Zverovichc0954452018-01-06 09:09:50 -0800253 using base::operator();
Victor Zverovich95a53e12016-11-19 07:39:07 -0800254
Victor Zveroviche928b672018-07-04 11:36:38 -0700255 template <typename T>
256 typename std::enable_if<std::is_integral<T>::value, iterator>::type
257 operator()(T value) {
258 // MSVC2013 fails to compile separate overloads for bool and char_type so
259 // use std::is_same instead.
260 if (std::is_same<T, bool>::value) {
261 format_specs &fmt_spec = this->spec();
262 if (fmt_spec.type_ != 's')
263 return base::operator()(value ? 1 : 0);
264 fmt_spec.type_ = 0;
265 this->write(value);
266 } else if (std::is_same<T, char_type>::value) {
267 format_specs &fmt_spec = this->spec();
268 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
269 return (*this)(static_cast<int>(value));
270 fmt_spec.flags_ = 0;
271 fmt_spec.align_ = ALIGN_RIGHT;
272 return base::operator()(value);
273 } else {
274 return base::operator()(value);
275 }
Victor Zverovich3cf05262018-03-30 08:20:12 -1000276 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200277 }
278
Victor Zveroviche928b672018-07-04 11:36:38 -0700279 template <typename T>
280 typename std::enable_if<std::is_floating_point<T>::value, iterator>::type
281 operator()(T value) {
Victor Zverovich3cf05262018-03-30 08:20:12 -1000282 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200283 }
284
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700285 /** Formats a null-terminated C string. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000286 iterator operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200287 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800288 base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200289 else if (this->spec().type_ == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200290 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200291 else
292 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000293 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200294 }
295
Daniela Engert2570f1a2018-04-26 20:32:14 +0200296 /** Formats a null-terminated wide C string. */
297 iterator operator()(const wchar_t *value) {
298 if (value)
299 base::operator()(value);
300 else if (this->spec().type_ == 'p')
301 write_null_pointer(char_type());
302 else
303 this->write(L"(null)");
304 return this->out();
305 }
306
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700307 /** Formats a pointer. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000308 iterator operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200309 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800310 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200311 this->spec().type_ = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200312 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000313 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200314 }
315
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700316 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700317 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800318 handle.format(context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000319 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700320 }
321};
322
Victor Zverovichc0954452018-01-06 09:09:50 -0800323template <typename T>
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700324struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700325 template <typename ParseContext>
Victor Zverovich1b452532018-02-28 05:09:24 -0800326 auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { return ctx.begin(); }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700327
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800328 template <typename FormatContext>
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700329 auto format(const T &value, FormatContext &ctx) -> decltype(ctx.out()) {
330 internal::format_value(internal::get_container(ctx.out()), value);
331 return ctx.out();
Glen Stark72d51e02016-06-08 01:23:32 +0200332 }
333};
334
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700335/** This template formats data and writes the output to a writer. */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800336template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800337class basic_printf_context :
338 private internal::context_base<
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800339 OutputIt, basic_printf_context<OutputIt, Char, ArgFormatter>, Char> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700340 public:
341 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800342 typedef Char char_type;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700343
344 template <typename T>
Victor Zverovich522de7b2018-02-11 08:32:02 -0800345 struct formatter_type { typedef printf_formatter<T> type; };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700346
Glen Stark72d51e02016-06-08 01:23:32 +0200347 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800348 typedef internal::context_base<OutputIt, basic_printf_context, Char> base;
349 typedef typename base::format_arg format_arg;
350 typedef basic_format_specs<char_type> format_specs;
351 typedef internal::null_terminating_iterator<char_type> iterator;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700352
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700353 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200354
355 // Returns the argument with specified index or, if arg_index is equal
356 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800357 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700358 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200359 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
360
361 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700362 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200363
364 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700365 /**
366 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800367 Constructs a ``printf_context`` object. References to the arguments and
368 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700369 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700370 \endrst
371 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800372 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800373 basic_format_args<basic_printf_context> args)
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800374 : base(out, format_str, args) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700375
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800376 using base::parse_context;
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700377 using base::out;
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800378 using base::advance_to;
Victor Zverovich9998f662016-11-06 16:11:24 -0800379
Victor Zverovichc0954452018-01-06 09:09:50 -0800380 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich08dff372018-01-28 20:50:43 -0800381 void format();
Glen Stark72d51e02016-06-08 01:23:32 +0200382};
383
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800384template <typename OutputIt, typename Char, typename AF>
385void basic_printf_context<OutputIt, Char, AF>::parse_flags(
Victor Zverovich217e7c72018-01-14 07:19:23 -0800386 format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200387 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700388 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200389 case '-':
390 spec.align_ = ALIGN_LEFT;
391 break;
392 case '+':
393 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
394 break;
395 case '0':
396 spec.fill_ = '0';
397 break;
398 case ' ':
399 spec.flags_ |= SIGN_FLAG;
400 break;
401 case '#':
402 spec.flags_ |= HASH_FLAG;
403 break;
404 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700405 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200406 return;
407 }
408 }
409}
410
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800411template <typename OutputIt, typename Char, typename AF>
412typename basic_printf_context<OutputIt, Char, AF>::format_arg
413 basic_printf_context<OutputIt, Char, AF>::get_arg(
414 iterator it, unsigned arg_index) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700415 (void)it;
Victor Zverovichc523dd52017-11-19 07:36:01 -0800416 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich67928ea2018-01-14 09:27:40 -0800417 return this->do_get_arg(this->parse_context().next_arg_id());
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800418 return base::get_arg(arg_index - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200419}
420
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800421template <typename OutputIt, typename Char, typename AF>
422unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700423 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200424 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800425 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200426 if (c >= '0' && c <= '9') {
427 // Parse an argument index (if followed by '$') or a width possibly
428 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700429 internal::error_handler eh;
430 unsigned value = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700431 if (*it == '$') { // value is an argument index
432 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200433 arg_index = value;
434 } else {
435 if (c == '0')
436 spec.fill_ = '0';
437 if (value != 0) {
438 // Nonzero value means that we parsed width and don't need to
439 // parse it or flags again, so return now.
440 spec.width_ = value;
441 return arg_index;
442 }
443 }
444 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700445 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200446 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700447 if (*it >= '0' && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700448 internal::error_handler eh;
449 spec.width_ = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700450 } else if (*it == '*') {
451 ++it;
Victor Zverovichc0954452018-01-06 09:09:50 -0800452 spec.width_ =
Victor Zverovich418659a2018-03-03 14:04:59 -0800453 visit(internal::printf_width_handler<char_type>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200454 }
455 return arg_index;
456}
457
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800458template <typename OutputIt, typename Char, typename AF>
459void basic_printf_context<OutputIt, Char, AF>::format() {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700460 auto &buffer = internal::get_container(this->out());
Victor Zverovich67928ea2018-01-14 09:27:40 -0800461 auto start = iterator(this->parse_context());
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700462 auto it = start;
463 using internal::pointer_from;
464 while (*it) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800465 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200466 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700467 if (*it == c) {
468 buffer.append(pointer_from(start), pointer_from(it));
469 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200470 continue;
471 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700472 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200473
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000474 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200475 spec.align_ = ALIGN_RIGHT;
476
477 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700478 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200479
480 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700481 if (*it == '.') {
482 ++it;
483 if ('0' <= *it && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700484 internal::error_handler eh;
485 spec.precision_ = static_cast<int>(parse_nonnegative_int(it, eh));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700486 } else if (*it == '*') {
487 ++it;
488 spec.precision_ =
Victor Zverovich418659a2018-03-03 14:04:59 -0800489 visit(internal::printf_precision_handler(), get_arg(it));
Victor Zverovich50605682018-01-27 17:56:19 -0800490 } else {
491 spec.precision_ = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200492 }
493 }
494
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700495 format_arg arg = get_arg(it, arg_index);
Victor Zverovich418659a2018-03-03 14:04:59 -0800496 if (spec.flag(HASH_FLAG) && visit(internal::is_zero_int(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700497 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200498 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800499 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200500 spec.align_ = ALIGN_NUMERIC;
501 else
502 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
503 }
504
505 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800506 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700507 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200508 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700509 if (*it == 'h')
510 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200511 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700512 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200513 break;
514 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700515 if (*it == 'l')
Victor Zverovich016aceb2017-08-26 09:09:43 -0700516 convert_arg<long long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200517 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700518 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200519 break;
520 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700521 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200522 break;
523 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700524 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200525 break;
526 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700527 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200528 break;
529 case 'L':
530 // printf produces garbage when 'L' is omitted for long double, no
531 // need to do the same.
532 break;
533 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700534 --it;
535 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200536 }
537
538 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700539 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700540 FMT_THROW(format_error("invalid format string"));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700541 spec.type_ = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800542 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200543 // Normalize type.
544 switch (spec.type_) {
545 case 'i': case 'u':
546 spec.type_ = 'd';
547 break;
548 case 'c':
Daniela Engert2570f1a2018-04-26 20:32:14 +0200549 // TODO: handle wchar_t better?
Victor Zverovich418659a2018-03-03 14:04:59 -0800550 visit(internal::char_converter<basic_printf_context>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200551 break;
552 }
553 }
554
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700555 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200556
557 // Format argument.
Victor Zverovich10e70a02017-12-02 09:44:48 -0800558 visit(AF(buffer, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200559 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700560 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200561}
Glen Stark72d51e02016-06-08 01:23:32 +0200562
Victor Zverovich217e7c72018-01-14 07:19:23 -0800563template <typename Char, typename Context>
Victor Zverovich9a53a702018-01-14 12:25:03 -0800564void printf(internal::basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800565 basic_format_args<Context> args) {
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800566 Context(std::back_inserter(buf), format, args).format();
Glen Stark72d51e02016-06-08 01:23:32 +0200567}
568
Victor Zverovich217e7c72018-01-14 07:19:23 -0800569template <typename Buffer>
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800570struct printf_context {
571 typedef basic_printf_context<
572 std::back_insert_iterator<Buffer>, typename Buffer::value_type> type;
573};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800574
Germán Méndez Bravo7a41d612018-03-26 12:42:40 -0600575template <typename ...Args>
Victor Zverovich23759b22018-04-04 07:38:21 -0700576inline format_arg_store<printf_context<internal::buffer>::type, Args...>
577 make_printf_args(const Args & ... args) {
578 return format_arg_store<printf_context<internal::buffer>::type, Args...>(
579 args...);
Germán Méndez Bravo7a41d612018-03-26 12:42:40 -0600580}
Victor Zverovich418659a2018-03-03 14:04:59 -0800581typedef basic_format_args<printf_context<internal::buffer>::type> printf_args;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200582typedef basic_format_args<printf_context<internal::wbuffer>::type> wprintf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800583
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700584inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800585 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500586 printf(buffer, format, args);
587 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700588}
589
Glen Stark72d51e02016-06-08 01:23:32 +0200590/**
591 \rst
592 Formats arguments and returns the result as a string.
593
594 **Example**::
595
596 std::string message = fmt::sprintf("The answer is %d", 42);
597 \endrst
598*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700599template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700600inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800601 return vsprintf(format_str,
Victor Zverovich7d286742018-04-08 07:21:26 -0700602 make_format_args<typename printf_context<internal::buffer>::type>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200603}
Glen Stark72d51e02016-06-08 01:23:32 +0200604
Daniela Engert2570f1a2018-04-26 20:32:14 +0200605inline std::wstring vsprintf(wstring_view format, wprintf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800606 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500607 printf(buffer, format, args);
608 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200609}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700610
611template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700612inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200613 return vsprintf(format_str,
614 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700615}
616
Daniela Engert2570f1a2018-04-26 20:32:14 +0200617template <typename Char>
618inline int vfprintf(std::FILE *f, basic_string_view<Char> format,
619 basic_format_args<typename printf_context<
620 internal::basic_buffer<Char>>::type> args) {
621 basic_memory_buffer<Char> buffer;
Victor Zverovich955062d2017-12-17 08:36:19 -0800622 printf(buffer, format, args);
623 std::size_t size = buffer.size();
624 return std::fwrite(
Daniela Engert2570f1a2018-04-26 20:32:14 +0200625 buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800626}
Glen Stark72d51e02016-06-08 01:23:32 +0200627
628/**
629 \rst
630 Prints formatted data to the file *f*.
631
632 **Example**::
633
634 fmt::fprintf(stderr, "Don't %s!", "panic");
635 \endrst
636 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700637template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700638inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovich7d286742018-04-08 07:21:26 -0700639 auto vargs = make_format_args<
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800640 typename printf_context<internal::buffer>::type>(args...);
Victor Zverovichedd5f142018-05-20 09:09:03 -0700641 return vfprintf<char>(f, format_str, vargs);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700642}
643
Daniela Engert2570f1a2018-04-26 20:32:14 +0200644template <typename... Args>
645inline int fprintf(std::FILE *f, wstring_view format_str,
646 const Args & ... args) {
647 return vfprintf(f, format_str,
648 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
649}
650
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700651inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700652 return vfprintf(stdout, format, args);
653}
Glen Stark72d51e02016-06-08 01:23:32 +0200654
Daniela Engert2570f1a2018-04-26 20:32:14 +0200655inline int vprintf(wstring_view format, wprintf_args args) {
656 return vfprintf(stdout, format, args);
657}
658
Glen Stark72d51e02016-06-08 01:23:32 +0200659/**
660 \rst
661 Prints formatted data to ``stdout``.
662
663 **Example**::
664
665 fmt::printf("Elapsed time: %.2f seconds", 1.23);
666 \endrst
667 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700668template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700669inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800670 return vprintf(format_str,
Victor Zverovich7d286742018-04-08 07:21:26 -0700671 make_format_args<typename printf_context<internal::buffer>::type>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200672}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700673
Daniela Engert2570f1a2018-04-26 20:32:14 +0200674template <typename... Args>
675inline int printf(wstring_view format_str, const Args & ... args) {
676 return vprintf(format_str,
677 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
678}
679
Victor Zverovichc0954452018-01-06 09:09:50 -0800680inline int vfprintf(std::ostream &os, string_view format_str,
681 printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800682 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500683 printf(buffer, format_str, args);
684 internal::write(os, buffer);
685 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700686}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700687
Daniela Engert2570f1a2018-04-26 20:32:14 +0200688inline int vfprintf(std::wostream &os, wstring_view format_str,
689 wprintf_args args) {
690 wmemory_buffer buffer;
691 printf(buffer, format_str, args);
692 internal::write(os, buffer);
693 return static_cast<int>(buffer.size());
694}
695
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700696/**
697 \rst
698 Prints formatted data to the stream *os*.
699
700 **Example**::
701
Victor Zverovich40232912018-03-04 09:55:17 -0800702 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700703 \endrst
704 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700705template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700706inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700707 const Args & ... args) {
Victor Zverovich7d286742018-04-08 07:21:26 -0700708 auto vargs = make_format_args<
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800709 typename printf_context<internal::buffer>::type>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700710 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700711}
Daniela Engert2570f1a2018-04-26 20:32:14 +0200712
713template <typename... Args>
714inline int fprintf(std::wostream &os, wstring_view format_str,
715 const Args & ... args) {
716 auto vargs = make_format_args<
717 typename printf_context<internal::buffer>::type>(args...);
718 return vfprintf(os, format_str, vargs);
719}
Victor Zverovich838400d2018-05-12 08:33:51 -0700720FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200721
722#endif // FMT_PRINTF_H_