blob: 0b77ae3451385c0f0ffb1df7c7406e42f46f48fe [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';
Glen Stark72d51e02016-06-08 01:23:32 +0200104 typedef typename internal::Conditional<
105 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 {
Glen Stark72d51e02016-06-08 01:23:32 +0200113 typedef typename internal::MakeUnsigned<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 Zverovichd705d512016-12-29 09:07:39 -0800122 arg_ = internal::make_arg<Context>(static_cast<LongLong>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200123 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800124 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800125 static_cast<typename internal::MakeUnsigned<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) {
Glen Stark72d51e02016-06-08 01:23:32 +0200186 typedef typename internal::IntTraits<T>::MainType UnsignedType;
187 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 Zverovich28429702016-11-20 09:36:27 -0800213class PrintfArgFormatter : public internal::ArgFormatterBase<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 Zverovich28429702016-11-20 09:36:27 -0800220 typedef internal::ArgFormatterBase<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 Zverovichfefaf072017-02-14 16:29:47 -0500232 PrintfArgFormatter(basic_buffer<Char> &buffer, format_specs &spec)
233 : internal::ArgFormatterBase<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 Zverovich422236a2016-12-28 07:55:33 -0800288 void operator()(internal::CustomValue<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,
298 typename ArgFormatter = PrintfArgFormatter<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 Zverovichdafbec72016-10-07 08:37:06 -0700310
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000311 void parse_flags(format_specs &spec, const Char *&s);
Glen Stark72d51e02016-06-08 01:23:32 +0200312
313 // Returns the argument with specified index or, if arg_index is equal
314 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800315 format_arg get_arg(
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700316 const Char *s,
Glen Stark72d51e02016-06-08 01:23:32 +0200317 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
318
319 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000320 unsigned parse_header(const Char *&s, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200321
322 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700323 /**
324 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800325 Constructs a ``printf_context`` object. References to the arguments and
326 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700327 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700328 \endrst
329 */
Victor Zverovich9998f662016-11-06 16:11:24 -0800330 explicit printf_context(BasicCStringRef<Char> format_str,
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800331 basic_args<printf_context> args)
Victor Zverovich9998f662016-11-06 16:11:24 -0800332 : Base(format_str.c_str(), args) {}
333
Victor Zverovichfefaf072017-02-14 16:29:47 -0500334 /** Formats stored arguments and writes the output to the buffer. */
335 FMT_API void format(basic_buffer<Char> &buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200336};
337
338template <typename Char, typename AF>
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000339void printf_context<Char, AF>::parse_flags(format_specs &spec, const Char *&s) {
Glen Stark72d51e02016-06-08 01:23:32 +0200340 for (;;) {
341 switch (*s++) {
342 case '-':
343 spec.align_ = ALIGN_LEFT;
344 break;
345 case '+':
346 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
347 break;
348 case '0':
349 spec.fill_ = '0';
350 break;
351 case ' ':
352 spec.flags_ |= SIGN_FLAG;
353 break;
354 case '#':
355 spec.flags_ |= HASH_FLAG;
356 break;
357 default:
358 --s;
359 return;
360 }
361 }
362}
363
364template <typename Char, typename AF>
Victor Zverovichd705d512016-12-29 09:07:39 -0800365typename printf_context<Char, AF>::format_arg printf_context<Char, AF>::get_arg(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800366 const Char *s, unsigned arg_index) {
Glen Stark72d51e02016-06-08 01:23:32 +0200367 (void)s;
368 const char *error = 0;
Victor Zverovichd705d512016-12-29 09:07:39 -0800369 format_arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
Victor Zverovichdafbec72016-10-07 08:37:06 -0700370 this->next_arg(error) : Base::get_arg(arg_index - 1, error);
Glen Stark72d51e02016-06-08 01:23:32 +0200371 if (error)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700372 FMT_THROW(format_error(!*s ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200373 return arg;
374}
375
376template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800377unsigned printf_context<Char, AF>::parse_header(
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000378 const Char *&s, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200379 unsigned arg_index = std::numeric_limits<unsigned>::max();
380 Char c = *s;
381 if (c >= '0' && c <= '9') {
382 // Parse an argument index (if followed by '$') or a width possibly
383 // preceded with '0' flag(s).
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700384 unsigned value = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200385 if (*s == '$') { // value is an argument index
386 ++s;
387 arg_index = value;
388 } else {
389 if (c == '0')
390 spec.fill_ = '0';
391 if (value != 0) {
392 // Nonzero value means that we parsed width and don't need to
393 // parse it or flags again, so return now.
394 spec.width_ = value;
395 return arg_index;
396 }
397 }
398 }
399 parse_flags(spec, s);
400 // Parse width.
401 if (*s >= '0' && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700402 spec.width_ = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200403 } else if (*s == '*') {
404 ++s;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000405 spec.width_ = visit(internal::PrintfWidthHandler<Char>(spec), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200406 }
407 return arg_index;
408}
409
410template <typename Char, typename AF>
Victor Zverovichfefaf072017-02-14 16:29:47 -0500411void printf_context<Char, AF>::format(basic_buffer<Char> &buffer) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800412 const Char *start = this->ptr();
Glen Stark72d51e02016-06-08 01:23:32 +0200413 const Char *s = start;
414 while (*s) {
415 Char c = *s++;
416 if (c != '%') continue;
417 if (*s == c) {
Victor Zverovichfefaf072017-02-14 16:29:47 -0500418 buffer.append(start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200419 start = ++s;
420 continue;
421 }
Victor Zverovichfefaf072017-02-14 16:29:47 -0500422 buffer.append(start, s - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200423
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000424 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200425 spec.align_ = ALIGN_RIGHT;
426
427 // Parse argument index, flags and width.
428 unsigned arg_index = parse_header(s, spec);
429
430 // Parse precision.
431 if (*s == '.') {
432 ++s;
433 if ('0' <= *s && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700434 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200435 } else if (*s == '*') {
436 ++s;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800437 spec.precision_ = visit(internal::PrintfPrecisionHandler(), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200438 }
439 }
440
Victor Zverovichd705d512016-12-29 09:07:39 -0800441 format_arg arg = get_arg(s, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800442 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700443 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200444 if (spec.fill_ == '0') {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800445 if (arg.is_numeric())
Glen Stark72d51e02016-06-08 01:23:32 +0200446 spec.align_ = ALIGN_NUMERIC;
447 else
448 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
449 }
450
451 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800452 using internal::convert_arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200453 switch (*s++) {
454 case 'h':
455 if (*s == 'h')
Victor Zverovich751ff642016-11-19 08:40:24 -0800456 convert_arg<signed char>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200457 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800458 convert_arg<short>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200459 break;
460 case 'l':
461 if (*s == 'l')
Victor Zverovich751ff642016-11-19 08:40:24 -0800462 convert_arg<fmt::LongLong>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200463 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800464 convert_arg<long>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200465 break;
466 case 'j':
Victor Zverovich751ff642016-11-19 08:40:24 -0800467 convert_arg<intmax_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200468 break;
469 case 'z':
Victor Zverovich751ff642016-11-19 08:40:24 -0800470 convert_arg<std::size_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200471 break;
472 case 't':
Victor Zverovich751ff642016-11-19 08:40:24 -0800473 convert_arg<std::ptrdiff_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200474 break;
475 case 'L':
476 // printf produces garbage when 'L' is omitted for long double, no
477 // need to do the same.
478 break;
479 default:
480 --s;
Victor Zverovich751ff642016-11-19 08:40:24 -0800481 convert_arg<void>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200482 }
483
484 // Parse type.
485 if (!*s)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700486 FMT_THROW(format_error("invalid format string"));
Glen Stark72d51e02016-06-08 01:23:32 +0200487 spec.type_ = static_cast<char>(*s++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800488 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200489 // Normalize type.
490 switch (spec.type_) {
491 case 'i': case 'u':
492 spec.type_ = 'd';
493 break;
494 case 'c':
495 // TODO: handle wchar_t
Victor Zverovichd705d512016-12-29 09:07:39 -0800496 visit(internal::CharConverter<printf_context<Char, AF>>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200497 break;
498 }
499 }
500
501 start = s;
502
503 // Format argument.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500504 visit(AF(buffer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200505 }
Victor Zverovichfefaf072017-02-14 16:29:47 -0500506 buffer.append(start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200507}
Glen Stark72d51e02016-06-08 01:23:32 +0200508
Victor Zverovich18dfa252016-10-21 06:46:21 -0700509// Formats a value.
510template <typename Char, typename T>
Victor Zverovichfefaf072017-02-14 16:29:47 -0500511void format_value(basic_buffer<Char> &buf, const T &value,
Victor Zverovich9998f662016-11-06 16:11:24 -0800512 printf_context<Char>& ctx) {
Victor Zverovichfefaf072017-02-14 16:29:47 -0500513 internal::format_value(buf, value);
Victor Zverovich18dfa252016-10-21 06:46:21 -0700514}
515
Glen Stark72d51e02016-06-08 01:23:32 +0200516template <typename Char>
Victor Zverovichfefaf072017-02-14 16:29:47 -0500517void printf(basic_buffer<Char> &buf, BasicCStringRef<Char> format,
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800518 basic_args<printf_context<Char>> args) {
Victor Zverovichfefaf072017-02-14 16:29:47 -0500519 printf_context<Char>(format, args).format(buf);
Glen Stark72d51e02016-06-08 01:23:32 +0200520}
521
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800522typedef basic_args<printf_context<char>> printf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800523
524inline std::string vsprintf(CStringRef format, printf_args args) {
Victor Zverovichfefaf072017-02-14 16:29:47 -0500525 internal::MemoryBuffer<char> buffer;
526 printf(buffer, format, args);
527 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700528}
529
Glen Stark72d51e02016-06-08 01:23:32 +0200530/**
531 \rst
532 Formats arguments and returns the result as a string.
533
534 **Example**::
535
536 std::string message = fmt::sprintf("The answer is %d", 42);
537 \endrst
538*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700539template <typename... Args>
540inline std::string sprintf(CStringRef format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800541 return vsprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200542}
Glen Stark72d51e02016-06-08 01:23:32 +0200543
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800544inline std::wstring vsprintf(
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800545 WCStringRef format, basic_args<printf_context<wchar_t>> args) {
Victor Zverovichfefaf072017-02-14 16:29:47 -0500546 internal::MemoryBuffer<wchar_t> buffer;
547 printf(buffer, format, args);
548 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200549}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700550
551template <typename... Args>
552inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800553 auto vargs = make_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700554 return vsprintf(format_str, vargs);
555}
556
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800557FMT_API int vfprintf(std::FILE *f, CStringRef format, printf_args args);
Glen Stark72d51e02016-06-08 01:23:32 +0200558
559/**
560 \rst
561 Prints formatted data to the file *f*.
562
563 **Example**::
564
565 fmt::fprintf(stderr, "Don't %s!", "panic");
566 \endrst
567 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700568template <typename... Args>
569inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800570 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700571 return vfprintf(f, format_str, vargs);
572}
573
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800574inline int vprintf(CStringRef format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700575 return vfprintf(stdout, format, args);
576}
Glen Stark72d51e02016-06-08 01:23:32 +0200577
578/**
579 \rst
580 Prints formatted data to ``stdout``.
581
582 **Example**::
583
584 fmt::printf("Elapsed time: %.2f seconds", 1.23);
585 \endrst
586 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700587template <typename... Args>
588inline int printf(CStringRef format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800589 return vprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200590}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700591
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800592inline int vfprintf(std::ostream &os, CStringRef format_str, printf_args args) {
Victor Zverovichfefaf072017-02-14 16:29:47 -0500593 internal::MemoryBuffer<char> buffer;
594 printf(buffer, format_str, args);
595 internal::write(os, buffer);
596 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700597}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700598
599/**
600 \rst
601 Prints formatted data to the stream *os*.
602
603 **Example**::
604
605 fprintf(cerr, "Don't %s!", "panic");
606 \endrst
607 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700608template <typename... Args>
609inline int fprintf(std::ostream &os, CStringRef format_str,
610 const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800611 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700612 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700613}
Glen Stark72d51e02016-06-08 01:23:32 +0200614} // namespace fmt
615
616#endif // FMT_PRINTF_H_