blob: c44ac414b7088774a66725a7d4f1d53d25cd45aa [file] [log] [blame]
Glen Stark72d51e02016-06-08 01:23:32 +02001/*
2 Formatting library for C++
3
4 Copyright (c) 2012 - 2016, Victor Zverovich
5 All rights reserved.
6
7 For the license information refer to format.h.
8 */
9
10#ifndef FMT_PRINTF_H_
11#define FMT_PRINTF_H_
12
13#include <algorithm> // std::fill_n
14#include <limits> // std::numeric_limits
15
Victor Zverovich9dbb60c2016-08-03 08:52:05 -070016#include "fmt/ostream.h"
Glen Stark72d51e02016-06-08 01:23:32 +020017
18namespace fmt {
19namespace internal {
20
21// Checks if a value fits in int - used to avoid warnings about comparing
22// signed and unsigned integers.
23template <bool IsSigned>
24struct IntChecker {
25 template <typename T>
26 static bool fits_in_int(T value) {
27 unsigned max = std::numeric_limits<int>::max();
28 return value <= max;
29 }
30 static bool fits_in_int(bool) { return true; }
31};
32
33template <>
34struct IntChecker<true> {
35 template <typename T>
36 static bool fits_in_int(T value) {
37 return value >= std::numeric_limits<int>::min() &&
38 value <= std::numeric_limits<int>::max();
39 }
40 static bool fits_in_int(int) { return true; }
41};
42
Victor Zverovich0854f8c2016-12-11 13:22:45 -080043class PrintfPrecisionHandler {
Glen Stark72d51e02016-06-08 01:23:32 +020044 public:
Glen Stark72d51e02016-06-08 01:23:32 +020045 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080046 typename std::enable_if<std::is_integral<T>::value, int>::type
47 operator()(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020048 if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070049 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +020050 return static_cast<int>(value);
51 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080052
53 template <typename T>
54 typename std::enable_if<!std::is_integral<T>::value, int>::type
55 operator()(T) {
56 FMT_THROW(format_error("precision is not integer"));
57 return 0;
58 }
Glen Stark72d51e02016-06-08 01:23:32 +020059};
60
Victor Zveroviche2dfd392016-11-19 09:29:09 -080061// An argument visitor that returns true iff arg is a zero integer.
62class IsZeroInt {
Glen Stark72d51e02016-06-08 01:23:32 +020063 public:
64 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080065 typename std::enable_if<std::is_integral<T>::value, bool>::type
66 operator()(T value) { return value == 0; }
67
68 template <typename T>
69 typename std::enable_if<!std::is_integral<T>::value, bool>::type
70 operator()(T value) { return false; }
Glen Stark72d51e02016-06-08 01:23:32 +020071};
72
73template <typename T, typename U>
74struct is_same {
75 enum { value = 0 };
76};
77
78template <typename T>
79struct is_same<T, T> {
80 enum { value = 1 };
81};
82
Victor Zverovichd705d512016-12-29 09:07:39 -080083template <typename T, typename Context>
Victor Zverovich751ff642016-11-19 08:40:24 -080084class ArgConverter {
Glen Stark72d51e02016-06-08 01:23:32 +020085 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080086 typedef typename Context::char_type Char;
87
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080088 basic_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080089 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020090
Glen Stark72d51e02016-06-08 01:23:32 +020091 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080092 ArgConverter(basic_arg<Context> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +020093 : arg_(arg), type_(type) {}
94
Victor Zverovich751ff642016-11-19 08:40:24 -080095 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020096 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080097 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020098 }
99
100 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -0800101 typename std::enable_if<std::is_integral<U>::value>::type
102 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200103 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800104 typedef typename internal::conditional<
Glen Stark72d51e02016-06-08 01:23:32 +0200105 is_same<T, void>::value, U, T>::type TargetType;
Victor Zverovich624c5862017-02-05 06:41:39 -0800106 typedef basic_context<Char> context;
Glen Stark72d51e02016-06-08 01:23:32 +0200107 if (sizeof(TargetType) <= sizeof(int)) {
108 // Extra casts are used to silence warnings.
109 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800110 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800111 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200112 } else {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800113 typedef typename internal::make_unsigned<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800114 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800115 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200116 }
117 } else {
118 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200119 // glibc's printf doesn't sign extend arguments of smaller types:
120 // std::printf("%lld", -42); // prints "4294967254"
121 // but we don't have to do the same because it's a UB.
Victor Zverovich50e71672017-02-18 06:52:52 -0800122 arg_ = internal::make_arg<Context>(static_cast<long_long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200123 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800124 arg_ = internal::make_arg<Context>(
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800125 static_cast<typename internal::make_unsigned<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200126 }
127 }
128 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800129
130 template <typename U>
131 typename std::enable_if<!std::is_integral<U>::value>::type
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800132 operator()(U value) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800133 // No coversion needed for non-integral types.
134 }
Glen Stark72d51e02016-06-08 01:23:32 +0200135};
136
Victor Zverovich751ff642016-11-19 08:40:24 -0800137// Converts an integer argument to T for printf, if T is an integral type.
138// If T is void, the argument is converted to corresponding signed or unsigned
139// type depending on the type specifier: 'd' and 'i' - signed, other -
140// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800141template <typename T, typename Context, typename Char>
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800142void convert_arg(basic_arg<Context> &arg, Char type) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800143 visit(ArgConverter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800144}
145
Glen Stark72d51e02016-06-08 01:23:32 +0200146// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800147template <typename Context>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800148class CharConverter {
Glen Stark72d51e02016-06-08 01:23:32 +0200149 private:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800150 basic_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200151
152 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
153
154 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800155 explicit CharConverter(basic_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200156
157 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800158 typename std::enable_if<std::is_integral<T>::value>::type
159 operator()(T value) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800160 arg_ = internal::make_arg<Context>(static_cast<char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200161 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800162
163 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800164 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800165 // No coversion needed for non-integral types.
166 }
Glen Stark72d51e02016-06-08 01:23:32 +0200167};
168
169// Checks if an argument is a valid printf width specifier and sets
170// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000171template <typename Char>
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800172class PrintfWidthHandler {
Glen Stark72d51e02016-06-08 01:23:32 +0200173 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000174 typedef basic_format_specs<Char> format_specs;
175
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000176 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200177
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800178 FMT_DISALLOW_COPY_AND_ASSIGN(PrintfWidthHandler);
Glen Stark72d51e02016-06-08 01:23:32 +0200179
180 public:
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000181 explicit PrintfWidthHandler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200182
Glen Stark72d51e02016-06-08 01:23:32 +0200183 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800184 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
185 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800186 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200187 UnsignedType width = static_cast<UnsignedType>(value);
188 if (internal::is_negative(value)) {
189 spec_.align_ = ALIGN_LEFT;
190 width = 0 - width;
191 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700192 unsigned int_max = std::numeric_limits<int>::max();
193 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700194 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200195 return static_cast<unsigned>(width);
196 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800197
198 template <typename T>
199 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
200 operator()(T value) {
201 FMT_THROW(format_error("width is not integer"));
202 return 0;
203 }
Glen Stark72d51e02016-06-08 01:23:32 +0200204};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700205} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200206
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700207/**
208 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800209 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700210 \endrst
211 */
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800212template <typename Char>
Victor Zverovichc333dca2017-02-19 08:41:38 -0800213class printf_arg_formatter : public internal::arg_formatter_base<Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200214 private:
215 void write_null_pointer() {
216 this->spec().type_ = 0;
217 this->write("(nil)");
218 }
219
Victor Zverovichc333dca2017-02-19 08:41:38 -0800220 typedef internal::arg_formatter_base<Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200221
222 public:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000223 typedef typename Base::format_specs format_specs;
224
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700225 /**
226 \rst
227 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500228 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700229 specifier information for standard argument types.
230 \endrst
231 */
Victor Zverovich4ec88602017-02-18 07:46:32 -0800232 printf_arg_formatter(basic_buffer<Char> &buffer, format_specs &spec)
Victor Zverovichc333dca2017-02-19 08:41:38 -0800233 : internal::arg_formatter_base<Char>(buffer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200234
Victor Zverovich95a53e12016-11-19 07:39:07 -0800235 using Base::operator();
236
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700237 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800238 void operator()(bool value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000239 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200240 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800241 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200242 fmt_spec.type_ = 0;
243 this->write(value);
244 }
245
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700246 /** Formats a character. */
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800247 void operator()(Char value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000248 const format_specs &fmt_spec = this->spec();
Victor Zverovich84286212016-12-30 12:11:27 -0800249 basic_writer<Char> &w = this->writer();
Glen Stark72d51e02016-06-08 01:23:32 +0200250 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
251 w.write_int(value, fmt_spec);
Victor Zverovich84286212016-12-30 12:11:27 -0800252 typedef typename basic_writer<Char>::CharPtr CharPtr;
Glen Stark72d51e02016-06-08 01:23:32 +0200253 CharPtr out = CharPtr();
254 if (fmt_spec.width_ > 1) {
255 Char fill = ' ';
256 out = w.grow_buffer(fmt_spec.width_);
257 if (fmt_spec.align_ != ALIGN_LEFT) {
258 std::fill_n(out, fmt_spec.width_ - 1, fill);
259 out += fmt_spec.width_ - 1;
260 } else {
261 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
262 }
263 } else {
264 out = w.grow_buffer(1);
265 }
266 *out = static_cast<Char>(value);
267 }
268
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700269 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800270 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200271 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800272 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200273 else if (this->spec().type_ == 'p')
274 write_null_pointer();
275 else
276 this->write("(null)");
277 }
278
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700279 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800280 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200281 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800282 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200283 this->spec().type_ = 0;
284 write_null_pointer();
285 }
286
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700287 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800288 void operator()(internal::custom_value<Char> c) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800289 const Char format_str[] = {'}', '\0'};
Victor Zverovich624c5862017-02-05 06:41:39 -0800290 auto args = basic_args<basic_context<Char>>();
291 basic_context<Char> ctx(format_str, args);
Victor Zverovichfefaf072017-02-14 16:29:47 -0500292 c.format(this->writer().buffer(), c.value, &ctx);
Glen Stark72d51e02016-06-08 01:23:32 +0200293 }
294};
295
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700296/** This template formats data and writes the output to a writer. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700297template <typename Char,
Victor Zverovich4ec88602017-02-18 07:46:32 -0800298 typename ArgFormatter = printf_arg_formatter<Char> >
Victor Zverovich9998f662016-11-06 16:11:24 -0800299class printf_context :
Victor Zverovich624c5862017-02-05 06:41:39 -0800300 private internal::context_base<
Victor Zverovich9998f662016-11-06 16:11:24 -0800301 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700302 public:
303 /** The character type for the output. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700304 typedef Char char_type;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700305
Glen Stark72d51e02016-06-08 01:23:32 +0200306 private:
Victor Zverovich624c5862017-02-05 06:41:39 -0800307 typedef internal::context_base<Char, printf_context> Base;
Victor Zverovichd705d512016-12-29 09:07:39 -0800308 typedef typename Base::format_arg format_arg;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000309 typedef basic_format_specs<Char> format_specs;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700310 typedef typename Base::iterator iterator;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700311
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700312 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200313
314 // Returns the argument with specified index or, if arg_index is equal
315 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800316 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700317 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200318 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
319
320 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700321 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200322
323 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700324 /**
325 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800326 Constructs a ``printf_context`` object. References to the arguments and
327 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700328 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700329 \endrst
330 */
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700331 explicit printf_context(basic_string_view<Char> format_str,
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800332 basic_args<printf_context> args)
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700333 : Base(format_str, args) {}
Victor Zverovich9998f662016-11-06 16:11:24 -0800334
Victor Zverovichfefaf072017-02-14 16:29:47 -0500335 /** Formats stored arguments and writes the output to the buffer. */
336 FMT_API void format(basic_buffer<Char> &buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200337};
338
339template <typename Char, typename AF>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700340void printf_context<Char, AF>::parse_flags(format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200341 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700342 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200343 case '-':
344 spec.align_ = ALIGN_LEFT;
345 break;
346 case '+':
347 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
348 break;
349 case '0':
350 spec.fill_ = '0';
351 break;
352 case ' ':
353 spec.flags_ |= SIGN_FLAG;
354 break;
355 case '#':
356 spec.flags_ |= HASH_FLAG;
357 break;
358 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700359 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200360 return;
361 }
362 }
363}
364
365template <typename Char, typename AF>
Victor Zverovichd705d512016-12-29 09:07:39 -0800366typename printf_context<Char, AF>::format_arg printf_context<Char, AF>::get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700367 iterator it, unsigned arg_index) {
368 (void)it;
Glen Stark72d51e02016-06-08 01:23:32 +0200369 const char *error = 0;
Victor Zverovich1102d462017-07-30 08:37:26 -0700370 format_arg arg;
371 if (arg_index == std::numeric_limits<unsigned>::max()) {
372 arg_index = this->next_arg_index(error);
373 if (!error)
374 arg = this->do_get_arg(arg_index, error);
375 } else {
376 arg = Base::get_arg(arg_index - 1, error);
377 }
Glen Stark72d51e02016-06-08 01:23:32 +0200378 if (error)
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700379 FMT_THROW(format_error(!*it ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200380 return arg;
381}
382
383template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800384unsigned printf_context<Char, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700385 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200386 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700387 Char c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200388 if (c >= '0' && c <= '9') {
389 // Parse an argument index (if followed by '$') or a width possibly
390 // preceded with '0' flag(s).
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700391 unsigned value = internal::parse_nonnegative_int(it);
392 if (*it == '$') { // value is an argument index
393 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200394 arg_index = value;
395 } else {
396 if (c == '0')
397 spec.fill_ = '0';
398 if (value != 0) {
399 // Nonzero value means that we parsed width and don't need to
400 // parse it or flags again, so return now.
401 spec.width_ = value;
402 return arg_index;
403 }
404 }
405 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700406 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200407 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700408 if (*it >= '0' && *it <= '9') {
409 spec.width_ = internal::parse_nonnegative_int(it);
410 } else if (*it == '*') {
411 ++it;
412 spec.width_ = visit(internal::PrintfWidthHandler<Char>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200413 }
414 return arg_index;
415}
416
417template <typename Char, typename AF>
Victor Zverovichfefaf072017-02-14 16:29:47 -0500418void printf_context<Char, AF>::format(basic_buffer<Char> &buffer) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700419 auto start = this->pos();
420 auto it = start;
421 using internal::pointer_from;
422 while (*it) {
423 Char c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200424 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700425 if (*it == c) {
426 buffer.append(pointer_from(start), pointer_from(it));
427 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200428 continue;
429 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700430 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200431
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000432 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200433 spec.align_ = ALIGN_RIGHT;
434
435 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700436 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200437
438 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700439 if (*it == '.') {
440 ++it;
441 if ('0' <= *it && *it <= '9') {
442 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(it));
443 } else if (*it == '*') {
444 ++it;
445 spec.precision_ =
446 visit(internal::PrintfPrecisionHandler(), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200447 }
448 }
449
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700450 format_arg arg = get_arg(it, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800451 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700452 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200453 if (spec.fill_ == '0') {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800454 if (arg.is_numeric())
Glen Stark72d51e02016-06-08 01:23:32 +0200455 spec.align_ = ALIGN_NUMERIC;
456 else
457 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
458 }
459
460 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800461 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700462 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200463 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700464 if (*it == 'h')
465 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200466 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700467 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200468 break;
469 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700470 if (*it == 'l')
471 convert_arg<fmt::long_long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200472 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700473 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200474 break;
475 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700476 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200477 break;
478 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700479 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200480 break;
481 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700482 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200483 break;
484 case 'L':
485 // printf produces garbage when 'L' is omitted for long double, no
486 // need to do the same.
487 break;
488 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700489 --it;
490 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200491 }
492
493 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700494 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700495 FMT_THROW(format_error("invalid format string"));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700496 spec.type_ = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800497 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200498 // Normalize type.
499 switch (spec.type_) {
500 case 'i': case 'u':
501 spec.type_ = 'd';
502 break;
503 case 'c':
504 // TODO: handle wchar_t
Victor Zverovichd705d512016-12-29 09:07:39 -0800505 visit(internal::CharConverter<printf_context<Char, AF>>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200506 break;
507 }
508 }
509
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700510 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200511
512 // Format argument.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500513 visit(AF(buffer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200514 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700515 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200516}
Glen Stark72d51e02016-06-08 01:23:32 +0200517
Victor Zverovich18dfa252016-10-21 06:46:21 -0700518// Formats a value.
519template <typename Char, typename T>
Victor Zverovichfefaf072017-02-14 16:29:47 -0500520void format_value(basic_buffer<Char> &buf, const T &value,
Victor Zverovich9998f662016-11-06 16:11:24 -0800521 printf_context<Char>& ctx) {
Victor Zverovichfefaf072017-02-14 16:29:47 -0500522 internal::format_value(buf, value);
Victor Zverovich18dfa252016-10-21 06:46:21 -0700523}
524
Glen Stark72d51e02016-06-08 01:23:32 +0200525template <typename Char>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700526void printf(basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800527 basic_args<printf_context<Char>> args) {
Victor Zverovichfefaf072017-02-14 16:29:47 -0500528 printf_context<Char>(format, args).format(buf);
Glen Stark72d51e02016-06-08 01:23:32 +0200529}
530
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800531typedef basic_args<printf_context<char>> printf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800532
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700533inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800534 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500535 printf(buffer, format, args);
536 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700537}
538
Glen Stark72d51e02016-06-08 01:23:32 +0200539/**
540 \rst
541 Formats arguments and returns the result as a string.
542
543 **Example**::
544
545 std::string message = fmt::sprintf("The answer is %d", 42);
546 \endrst
547*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700548template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700549inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800550 return vsprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200551}
Glen Stark72d51e02016-06-08 01:23:32 +0200552
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800553inline std::wstring vsprintf(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700554 wstring_view format, basic_args<printf_context<wchar_t>> args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800555 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500556 printf(buffer, format, args);
557 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200558}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700559
560template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700561inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800562 auto vargs = make_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700563 return vsprintf(format_str, vargs);
564}
565
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700566FMT_API int vfprintf(std::FILE *f, string_view format, printf_args args);
Glen Stark72d51e02016-06-08 01:23:32 +0200567
568/**
569 \rst
570 Prints formatted data to the file *f*.
571
572 **Example**::
573
574 fmt::fprintf(stderr, "Don't %s!", "panic");
575 \endrst
576 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700577template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700578inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800579 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700580 return vfprintf(f, format_str, vargs);
581}
582
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700583inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700584 return vfprintf(stdout, format, args);
585}
Glen Stark72d51e02016-06-08 01:23:32 +0200586
587/**
588 \rst
589 Prints formatted data to ``stdout``.
590
591 **Example**::
592
593 fmt::printf("Elapsed time: %.2f seconds", 1.23);
594 \endrst
595 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700596template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700597inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800598 return vprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200599}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700600
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700601inline int vfprintf(std::ostream &os, string_view format_str, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800602 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500603 printf(buffer, format_str, args);
604 internal::write(os, buffer);
605 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700606}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700607
608/**
609 \rst
610 Prints formatted data to the stream *os*.
611
612 **Example**::
613
614 fprintf(cerr, "Don't %s!", "panic");
615 \endrst
616 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700617template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700618inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700619 const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800620 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700621 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700622}
Glen Stark72d51e02016-06-08 01:23:32 +0200623} // namespace fmt
624
625#endif // FMT_PRINTF_H_