blob: 5a396964a2b362e213502c52a1dca431b20d587b [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:
218 public internal::function<void>, public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200219 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800220 typedef typename Range::value_type char_type;
Victor Zverovich418659a2018-03-03 14:04:59 -0800221 typedef decltype(internal::declval<Range>().begin()) iterator;
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800222 typedef internal::arg_formatter_base<Range> base;
223 typedef basic_printf_context<iterator, char_type> context_type;
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800224
225 context_type &context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800226
Daniela Engert2570f1a2018-04-26 20:32:14 +0200227 void write_null_pointer(char) {
Glen Stark72d51e02016-06-08 01:23:32 +0200228 this->spec().type_ = 0;
229 this->write("(nil)");
230 }
231
Daniela Engert2570f1a2018-04-26 20:32:14 +0200232 void write_null_pointer(wchar_t) {
233 this->spec().type_ = 0;
234 this->write(L"(nil)");
235 }
236
Glen Stark72d51e02016-06-08 01:23:32 +0200237 public:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800238 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000239
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700240 /**
241 \rst
242 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500243 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700244 specifier information for standard argument types.
245 \endrst
246 */
Victor Zverovich9a53a702018-01-14 12:25:03 -0800247 printf_arg_formatter(internal::basic_buffer<char_type> &buffer,
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800248 format_specs &spec, context_type &ctx)
249 : base(back_insert_range<internal::basic_buffer<char_type>>(buffer), spec),
250 context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200251
Victor Zverovichc0954452018-01-06 09:09:50 -0800252 using base::operator();
Victor Zverovich95a53e12016-11-19 07:39:07 -0800253
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700254 /** Formats an argument of type ``bool``. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000255 iterator operator()(bool value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000256 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200257 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800258 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200259 fmt_spec.type_ = 0;
260 this->write(value);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000261 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200262 }
263
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700264 /** Formats a character. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000265 iterator operator()(char_type value) {
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700266 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200267 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700268 return (*this)(static_cast<int>(value));
269 fmt_spec.flags_ = 0;
270 fmt_spec.align_ = ALIGN_RIGHT;
Victor Zverovich3cf05262018-03-30 08:20:12 -1000271 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200272 }
273
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700274 /** Formats a null-terminated C string. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000275 iterator operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200276 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800277 base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200278 else if (this->spec().type_ == 'p')
Daniela Engert2570f1a2018-04-26 20:32:14 +0200279 write_null_pointer(char_type());
Glen Stark72d51e02016-06-08 01:23:32 +0200280 else
281 this->write("(null)");
Victor Zverovich3cf05262018-03-30 08:20:12 -1000282 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200283 }
284
Daniela Engert2570f1a2018-04-26 20:32:14 +0200285 /** Formats a null-terminated wide C string. */
286 iterator operator()(const wchar_t *value) {
287 if (value)
288 base::operator()(value);
289 else if (this->spec().type_ == 'p')
290 write_null_pointer(char_type());
291 else
292 this->write(L"(null)");
293 return this->out();
294 }
295
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700296 /** Formats a pointer. */
Victor Zverovich3cf05262018-03-30 08:20:12 -1000297 iterator operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200298 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800299 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200300 this->spec().type_ = 0;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200301 write_null_pointer(char_type());
Victor Zverovich3cf05262018-03-30 08:20:12 -1000302 return this->out();
Glen Stark72d51e02016-06-08 01:23:32 +0200303 }
304
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700305 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich23759b22018-04-04 07:38:21 -0700306 iterator operator()(typename basic_format_arg<context_type>::handle handle) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800307 handle.format(context_);
Victor Zverovich3cf05262018-03-30 08:20:12 -1000308 return this->out();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700309 }
310};
311
Victor Zverovichc0954452018-01-06 09:09:50 -0800312template <typename T>
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700313struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700314 template <typename ParseContext>
Victor Zverovich1b452532018-02-28 05:09:24 -0800315 auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { return ctx.begin(); }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700316
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800317 template <typename FormatContext>
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700318 auto format(const T &value, FormatContext &ctx) -> decltype(ctx.out()) {
319 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 Zverovichbd8a7e72018-01-21 14:30:38 -0800325template <typename OutputIt, typename Char, typename ArgFormatter>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800326class basic_printf_context :
327 private internal::context_base<
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800328 OutputIt, basic_printf_context<OutputIt, Char, ArgFormatter>, Char> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700329 public:
330 /** The character type for the output. */
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800331 typedef Char char_type;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700332
333 template <typename T>
Victor Zverovich522de7b2018-02-11 08:32:02 -0800334 struct formatter_type { typedef printf_formatter<T> type; };
Victor Zverovich18dfa252016-10-21 06:46:21 -0700335
Glen Stark72d51e02016-06-08 01:23:32 +0200336 private:
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800337 typedef internal::context_base<OutputIt, basic_printf_context, Char> base;
338 typedef typename base::format_arg format_arg;
339 typedef basic_format_specs<char_type> format_specs;
340 typedef internal::null_terminating_iterator<char_type> iterator;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700341
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700342 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200343
344 // Returns the argument with specified index or, if arg_index is equal
345 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800346 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700347 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200348 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
349
350 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700351 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200352
353 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700354 /**
355 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800356 Constructs a ``printf_context`` object. References to the arguments and
357 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700358 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700359 \endrst
360 */
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800361 basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800362 basic_format_args<basic_printf_context> args)
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800363 : base(out, format_str, args) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700364
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800365 using base::parse_context;
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700366 using base::out;
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800367 using base::advance_to;
Victor Zverovich9998f662016-11-06 16:11:24 -0800368
Victor Zverovichc0954452018-01-06 09:09:50 -0800369 /** Formats stored arguments and writes the output to the range. */
Victor Zverovich08dff372018-01-28 20:50:43 -0800370 void format();
Glen Stark72d51e02016-06-08 01:23:32 +0200371};
372
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800373template <typename OutputIt, typename Char, typename AF>
374void basic_printf_context<OutputIt, Char, AF>::parse_flags(
Victor Zverovich217e7c72018-01-14 07:19:23 -0800375 format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200376 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700377 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200378 case '-':
379 spec.align_ = ALIGN_LEFT;
380 break;
381 case '+':
382 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
383 break;
384 case '0':
385 spec.fill_ = '0';
386 break;
387 case ' ':
388 spec.flags_ |= SIGN_FLAG;
389 break;
390 case '#':
391 spec.flags_ |= HASH_FLAG;
392 break;
393 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700394 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200395 return;
396 }
397 }
398}
399
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800400template <typename OutputIt, typename Char, typename AF>
401typename basic_printf_context<OutputIt, Char, AF>::format_arg
402 basic_printf_context<OutputIt, Char, AF>::get_arg(
403 iterator it, unsigned arg_index) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700404 (void)it;
Victor Zverovichc523dd52017-11-19 07:36:01 -0800405 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich67928ea2018-01-14 09:27:40 -0800406 return this->do_get_arg(this->parse_context().next_arg_id());
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800407 return base::get_arg(arg_index - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200408}
409
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800410template <typename OutputIt, typename Char, typename AF>
411unsigned basic_printf_context<OutputIt, Char, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700412 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200413 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800414 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200415 if (c >= '0' && c <= '9') {
416 // Parse an argument index (if followed by '$') or a width possibly
417 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700418 internal::error_handler eh;
419 unsigned value = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700420 if (*it == '$') { // value is an argument index
421 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200422 arg_index = value;
423 } else {
424 if (c == '0')
425 spec.fill_ = '0';
426 if (value != 0) {
427 // Nonzero value means that we parsed width and don't need to
428 // parse it or flags again, so return now.
429 spec.width_ = value;
430 return arg_index;
431 }
432 }
433 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700434 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200435 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700436 if (*it >= '0' && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700437 internal::error_handler eh;
438 spec.width_ = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700439 } else if (*it == '*') {
440 ++it;
Victor Zverovichc0954452018-01-06 09:09:50 -0800441 spec.width_ =
Victor Zverovich418659a2018-03-03 14:04:59 -0800442 visit(internal::printf_width_handler<char_type>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200443 }
444 return arg_index;
445}
446
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800447template <typename OutputIt, typename Char, typename AF>
448void basic_printf_context<OutputIt, Char, AF>::format() {
Victor Zverovichfa9066f2018-04-22 09:16:32 -0700449 auto &buffer = internal::get_container(this->out());
Victor Zverovich67928ea2018-01-14 09:27:40 -0800450 auto start = iterator(this->parse_context());
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700451 auto it = start;
452 using internal::pointer_from;
453 while (*it) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800454 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200455 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700456 if (*it == c) {
457 buffer.append(pointer_from(start), pointer_from(it));
458 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200459 continue;
460 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700461 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200462
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000463 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200464 spec.align_ = ALIGN_RIGHT;
465
466 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700467 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200468
469 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700470 if (*it == '.') {
471 ++it;
472 if ('0' <= *it && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700473 internal::error_handler eh;
474 spec.precision_ = static_cast<int>(parse_nonnegative_int(it, eh));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700475 } else if (*it == '*') {
476 ++it;
477 spec.precision_ =
Victor Zverovich418659a2018-03-03 14:04:59 -0800478 visit(internal::printf_precision_handler(), get_arg(it));
Victor Zverovich50605682018-01-27 17:56:19 -0800479 } else {
480 spec.precision_ = 0;
Glen Stark72d51e02016-06-08 01:23:32 +0200481 }
482 }
483
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700484 format_arg arg = get_arg(it, arg_index);
Victor Zverovich418659a2018-03-03 14:04:59 -0800485 if (spec.flag(HASH_FLAG) && visit(internal::is_zero_int(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700486 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200487 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800488 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200489 spec.align_ = ALIGN_NUMERIC;
490 else
491 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
492 }
493
494 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800495 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700496 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200497 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700498 if (*it == 'h')
499 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200500 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700501 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200502 break;
503 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700504 if (*it == 'l')
Victor Zverovich016aceb2017-08-26 09:09:43 -0700505 convert_arg<long long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200506 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700507 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200508 break;
509 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700510 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200511 break;
512 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700513 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200514 break;
515 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700516 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200517 break;
518 case 'L':
519 // printf produces garbage when 'L' is omitted for long double, no
520 // need to do the same.
521 break;
522 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700523 --it;
524 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200525 }
526
527 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700528 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700529 FMT_THROW(format_error("invalid format string"));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700530 spec.type_ = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800531 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200532 // Normalize type.
533 switch (spec.type_) {
534 case 'i': case 'u':
535 spec.type_ = 'd';
536 break;
537 case 'c':
Daniela Engert2570f1a2018-04-26 20:32:14 +0200538 // TODO: handle wchar_t better?
Victor Zverovich418659a2018-03-03 14:04:59 -0800539 visit(internal::char_converter<basic_printf_context>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200540 break;
541 }
542 }
543
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700544 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200545
546 // Format argument.
Victor Zverovich10e70a02017-12-02 09:44:48 -0800547 visit(AF(buffer, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200548 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700549 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200550}
Glen Stark72d51e02016-06-08 01:23:32 +0200551
Victor Zverovich217e7c72018-01-14 07:19:23 -0800552template <typename Char, typename Context>
Victor Zverovich9a53a702018-01-14 12:25:03 -0800553void printf(internal::basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800554 basic_format_args<Context> args) {
Victor Zverovichbd8a7e72018-01-21 14:30:38 -0800555 Context(std::back_inserter(buf), format, args).format();
Glen Stark72d51e02016-06-08 01:23:32 +0200556}
557
Victor Zverovich217e7c72018-01-14 07:19:23 -0800558template <typename Buffer>
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800559struct printf_context {
560 typedef basic_printf_context<
561 std::back_insert_iterator<Buffer>, typename Buffer::value_type> type;
562};
Victor Zverovich217e7c72018-01-14 07:19:23 -0800563
Germán Méndez Bravo7a41d612018-03-26 12:42:40 -0600564template <typename ...Args>
Victor Zverovich23759b22018-04-04 07:38:21 -0700565inline format_arg_store<printf_context<internal::buffer>::type, Args...>
566 make_printf_args(const Args & ... args) {
567 return format_arg_store<printf_context<internal::buffer>::type, Args...>(
568 args...);
Germán Méndez Bravo7a41d612018-03-26 12:42:40 -0600569}
Victor Zverovich418659a2018-03-03 14:04:59 -0800570typedef basic_format_args<printf_context<internal::buffer>::type> printf_args;
Daniela Engert2570f1a2018-04-26 20:32:14 +0200571typedef basic_format_args<printf_context<internal::wbuffer>::type> wprintf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800572
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700573inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800574 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500575 printf(buffer, format, args);
576 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700577}
578
Glen Stark72d51e02016-06-08 01:23:32 +0200579/**
580 \rst
581 Formats arguments and returns the result as a string.
582
583 **Example**::
584
585 std::string message = fmt::sprintf("The answer is %d", 42);
586 \endrst
587*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700588template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700589inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800590 return vsprintf(format_str,
Victor Zverovich7d286742018-04-08 07:21:26 -0700591 make_format_args<typename printf_context<internal::buffer>::type>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200592}
Glen Stark72d51e02016-06-08 01:23:32 +0200593
Daniela Engert2570f1a2018-04-26 20:32:14 +0200594inline std::wstring vsprintf(wstring_view format, wprintf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800595 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500596 printf(buffer, format, args);
597 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200598}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700599
600template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700601inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Daniela Engert2570f1a2018-04-26 20:32:14 +0200602 return vsprintf(format_str,
603 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
Victor Zverovich0028ce52016-08-26 17:23:13 -0700604}
605
Daniela Engert2570f1a2018-04-26 20:32:14 +0200606template <typename Char>
607inline int vfprintf(std::FILE *f, basic_string_view<Char> format,
608 basic_format_args<typename printf_context<
609 internal::basic_buffer<Char>>::type> args) {
610 basic_memory_buffer<Char> buffer;
Victor Zverovich955062d2017-12-17 08:36:19 -0800611 printf(buffer, format, args);
612 std::size_t size = buffer.size();
613 return std::fwrite(
Daniela Engert2570f1a2018-04-26 20:32:14 +0200614 buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast<int>(size);
Victor Zverovich955062d2017-12-17 08:36:19 -0800615}
Glen Stark72d51e02016-06-08 01:23:32 +0200616
617/**
618 \rst
619 Prints formatted data to the file *f*.
620
621 **Example**::
622
623 fmt::fprintf(stderr, "Don't %s!", "panic");
624 \endrst
625 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700626template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700627inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovich7d286742018-04-08 07:21:26 -0700628 auto vargs = make_format_args<
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800629 typename printf_context<internal::buffer>::type>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700630 return vfprintf(f, format_str, vargs);
631}
632
Daniela Engert2570f1a2018-04-26 20:32:14 +0200633template <typename... Args>
634inline int fprintf(std::FILE *f, wstring_view format_str,
635 const Args & ... args) {
636 return vfprintf(f, format_str,
637 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
638}
639
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700640inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700641 return vfprintf(stdout, format, args);
642}
Glen Stark72d51e02016-06-08 01:23:32 +0200643
Daniela Engert2570f1a2018-04-26 20:32:14 +0200644inline int vprintf(wstring_view format, wprintf_args args) {
645 return vfprintf(stdout, format, args);
646}
647
Glen Stark72d51e02016-06-08 01:23:32 +0200648/**
649 \rst
650 Prints formatted data to ``stdout``.
651
652 **Example**::
653
654 fmt::printf("Elapsed time: %.2f seconds", 1.23);
655 \endrst
656 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700657template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700658inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800659 return vprintf(format_str,
Victor Zverovich7d286742018-04-08 07:21:26 -0700660 make_format_args<typename printf_context<internal::buffer>::type>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200661}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700662
Daniela Engert2570f1a2018-04-26 20:32:14 +0200663template <typename... Args>
664inline int printf(wstring_view format_str, const Args & ... args) {
665 return vprintf(format_str,
666 make_format_args<typename printf_context<internal::wbuffer>::type>(args...));
667}
668
Victor Zverovichc0954452018-01-06 09:09:50 -0800669inline int vfprintf(std::ostream &os, string_view format_str,
670 printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800671 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500672 printf(buffer, format_str, args);
673 internal::write(os, buffer);
674 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700675}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700676
Daniela Engert2570f1a2018-04-26 20:32:14 +0200677inline int vfprintf(std::wostream &os, wstring_view format_str,
678 wprintf_args args) {
679 wmemory_buffer buffer;
680 printf(buffer, format_str, args);
681 internal::write(os, buffer);
682 return static_cast<int>(buffer.size());
683}
684
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700685/**
686 \rst
687 Prints formatted data to the stream *os*.
688
689 **Example**::
690
Victor Zverovich40232912018-03-04 09:55:17 -0800691 fmt::fprintf(cerr, "Don't %s!", "panic");
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700692 \endrst
693 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700694template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700695inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700696 const Args & ... args) {
Victor Zverovich7d286742018-04-08 07:21:26 -0700697 auto vargs = make_format_args<
Victor Zverovichaffb35c2018-02-11 09:23:47 -0800698 typename printf_context<internal::buffer>::type>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700699 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700700}
Daniela Engert2570f1a2018-04-26 20:32:14 +0200701
702template <typename... Args>
703inline int fprintf(std::wostream &os, wstring_view format_str,
704 const Args & ... args) {
705 auto vargs = make_format_args<
706 typename printf_context<internal::buffer>::type>(args...);
707 return vfprintf(os, format_str, vargs);
708}
Victor Zverovich838400d2018-05-12 08:33:51 -0700709FMT_END_NAMESPACE
Glen Stark72d51e02016-06-08 01:23:32 +0200710
711#endif // FMT_PRINTF_H_