blob: 47d868e60cd5c12ba90fb4a9842ab1f37a55ce7f [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 Zverovich0854f8c2016-12-11 13:22:45 -080083template <typename T, typename Char>
Victor Zverovich751ff642016-11-19 08:40:24 -080084class ArgConverter {
Glen Stark72d51e02016-06-08 01:23:32 +020085 private:
Victor Zverovich0854f8c2016-12-11 13:22:45 -080086 basic_format_arg<Char> &arg_;
87 Char type_;
Glen Stark72d51e02016-06-08 01:23:32 +020088
Glen Stark72d51e02016-06-08 01:23:32 +020089 public:
Victor Zverovich0854f8c2016-12-11 13:22:45 -080090 ArgConverter(basic_format_arg<Char> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +020091 : arg_(arg), type_(type) {}
92
Victor Zverovich751ff642016-11-19 08:40:24 -080093 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020094 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080095 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020096 }
97
98 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -080099 typename std::enable_if<std::is_integral<U>::value>::type
100 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200101 bool is_signed = type_ == 'd' || type_ == 'i';
Glen Stark72d51e02016-06-08 01:23:32 +0200102 typedef typename internal::Conditional<
103 is_same<T, void>::value, U, T>::type TargetType;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800104 typedef basic_format_context<Char> format_context;
Glen Stark72d51e02016-06-08 01:23:32 +0200105 if (sizeof(TargetType) <= sizeof(int)) {
106 // Extra casts are used to silence warnings.
107 if (is_signed) {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800108 arg_ = internal::MakeArg<format_context>(
109 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200110 } else {
Glen Stark72d51e02016-06-08 01:23:32 +0200111 typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800112 arg_ = internal::MakeArg<format_context>(
113 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200114 }
115 } else {
116 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200117 // glibc's printf doesn't sign extend arguments of smaller types:
118 // std::printf("%lld", -42); // prints "4294967254"
119 // but we don't have to do the same because it's a UB.
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800120 arg_ = internal::MakeArg<format_context>(
121 static_cast<LongLong>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200122 } else {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800123 arg_ = internal::MakeArg<format_context>(
124 static_cast<typename internal::MakeUnsigned<U>::Type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200125 }
126 }
127 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800128
129 template <typename U>
130 typename std::enable_if<!std::is_integral<U>::value>::type
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800131 operator()(U value) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800132 // No coversion needed for non-integral types.
133 }
Glen Stark72d51e02016-06-08 01:23:32 +0200134};
135
Victor Zverovich751ff642016-11-19 08:40:24 -0800136// Converts an integer argument to T for printf, if T is an integral type.
137// If T is void, the argument is converted to corresponding signed or unsigned
138// type depending on the type specifier: 'd' and 'i' - signed, other -
139// unsigned).
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800140template <typename T, typename Char>
141void convert_arg(basic_format_arg<Char> &arg, Char type) {
142 visit(ArgConverter<T, Char>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800143}
144
Glen Stark72d51e02016-06-08 01:23:32 +0200145// Converts an integer argument to char for printf.
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800146template <typename Char>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800147class CharConverter {
Glen Stark72d51e02016-06-08 01:23:32 +0200148 private:
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800149 basic_format_arg<Char> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200150
151 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
152
153 public:
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800154 explicit CharConverter(basic_format_arg<Char> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200155
156 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800157 typename std::enable_if<std::is_integral<T>::value>::type
158 operator()(T value) {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800159 arg_ =
160 internal::MakeArg<basic_format_context<Char>>(static_cast<char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200161 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800162
163 template <typename T>
164 typename std::enable_if<!std::is_integral<T>::value>::type
165 operator()(T value) {
166 // No coversion needed for non-integral types.
167 }
Glen Stark72d51e02016-06-08 01:23:32 +0200168};
169
170// Checks if an argument is a valid printf width specifier and sets
171// left alignment if it is negative.
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800172class PrintfWidthHandler {
Glen Stark72d51e02016-06-08 01:23:32 +0200173 private:
174 FormatSpec &spec_;
175
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800176 FMT_DISALLOW_COPY_AND_ASSIGN(PrintfWidthHandler);
Glen Stark72d51e02016-06-08 01:23:32 +0200177
178 public:
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800179 explicit PrintfWidthHandler(FormatSpec &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200180
Glen Stark72d51e02016-06-08 01:23:32 +0200181 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800182 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
183 operator()(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200184 typedef typename internal::IntTraits<T>::MainType UnsignedType;
185 UnsignedType width = static_cast<UnsignedType>(value);
186 if (internal::is_negative(value)) {
187 spec_.align_ = ALIGN_LEFT;
188 width = 0 - width;
189 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700190 unsigned int_max = std::numeric_limits<int>::max();
191 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700192 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200193 return static_cast<unsigned>(width);
194 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800195
196 template <typename T>
197 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
198 operator()(T value) {
199 FMT_THROW(format_error("width is not integer"));
200 return 0;
201 }
Glen Stark72d51e02016-06-08 01:23:32 +0200202};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700203} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200204
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700205/**
206 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800207 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700208 \endrst
209 */
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800210template <typename Char>
Victor Zverovich28429702016-11-20 09:36:27 -0800211class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200212 private:
213 void write_null_pointer() {
214 this->spec().type_ = 0;
215 this->write("(nil)");
216 }
217
Victor Zverovich28429702016-11-20 09:36:27 -0800218 typedef internal::ArgFormatterBase<Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200219
220 public:
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700221 /**
222 \rst
223 Constructs an argument formatter object.
224 *writer* is a reference to the output writer and *spec* contains format
225 specifier information for standard argument types.
226 \endrst
227 */
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800228 PrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
Victor Zverovich28429702016-11-20 09:36:27 -0800229 : internal::ArgFormatterBase<Char>(writer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200230
Victor Zverovich95a53e12016-11-19 07:39:07 -0800231 using Base::operator();
232
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700233 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800234 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200235 FormatSpec &fmt_spec = this->spec();
236 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800237 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200238 fmt_spec.type_ = 0;
239 this->write(value);
240 }
241
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700242 /** Formats a character. */
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800243 void operator()(Char value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200244 const FormatSpec &fmt_spec = this->spec();
245 BasicWriter<Char> &w = this->writer();
246 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
247 w.write_int(value, fmt_spec);
248 typedef typename BasicWriter<Char>::CharPtr CharPtr;
249 CharPtr out = CharPtr();
250 if (fmt_spec.width_ > 1) {
251 Char fill = ' ';
252 out = w.grow_buffer(fmt_spec.width_);
253 if (fmt_spec.align_ != ALIGN_LEFT) {
254 std::fill_n(out, fmt_spec.width_ - 1, fill);
255 out += fmt_spec.width_ - 1;
256 } else {
257 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
258 }
259 } else {
260 out = w.grow_buffer(1);
261 }
262 *out = static_cast<Char>(value);
263 }
264
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700265 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800266 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200267 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800268 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200269 else if (this->spec().type_ == 'p')
270 write_null_pointer();
271 else
272 this->write("(null)");
273 }
274
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700275 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800276 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200277 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800278 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200279 this->spec().type_ = 0;
280 write_null_pointer();
281 }
282
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700283 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich6cba8fe2016-12-15 07:51:40 -0800284 void operator()(internal::CustomValue c) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800285 const Char format_str[] = {'}', '\0'};
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800286 auto args = basic_format_args<basic_format_context<Char>, Char>();
Victor Zverovich9998f662016-11-06 16:11:24 -0800287 basic_format_context<Char> ctx(format_str, args);
288 c.format(&this->writer(), c.value, &ctx);
Glen Stark72d51e02016-06-08 01:23:32 +0200289 }
290};
291
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700292/** This template formats data and writes the output to a writer. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700293template <typename Char,
294 typename ArgFormatter = PrintfArgFormatter<Char> >
Victor Zverovich9998f662016-11-06 16:11:24 -0800295class printf_context :
296 private internal::format_context_base<
297 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700298 public:
299 /** The character type for the output. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700300 typedef Char char_type;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700301
Glen Stark72d51e02016-06-08 01:23:32 +0200302 private:
Victor Zverovich9998f662016-11-06 16:11:24 -0800303 typedef internal::format_context_base<Char, printf_context> Base;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700304
Glen Stark72d51e02016-06-08 01:23:32 +0200305 void parse_flags(FormatSpec &spec, const Char *&s);
306
307 // Returns the argument with specified index or, if arg_index is equal
308 // to the maximum unsigned value, the next argument.
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800309 basic_format_arg<Char> get_arg(
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700310 const Char *s,
Glen Stark72d51e02016-06-08 01:23:32 +0200311 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
312
313 // Parses argument index, flags and width and returns the argument index.
314 unsigned parse_header(const Char *&s, FormatSpec &spec);
315
316 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700317 /**
318 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800319 Constructs a ``printf_context`` object. References to the arguments and
320 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700321 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700322 \endrst
323 */
Victor Zverovich9998f662016-11-06 16:11:24 -0800324 explicit printf_context(BasicCStringRef<Char> format_str,
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800325 basic_format_args<printf_context, Char> args)
Victor Zverovich9998f662016-11-06 16:11:24 -0800326 : Base(format_str.c_str(), args) {}
327
Victor Zverovich355861f2016-07-20 08:26:14 -0700328 /** Formats stored arguments and writes the output to the writer. */
Victor Zverovich9998f662016-11-06 16:11:24 -0800329 FMT_API void format(BasicWriter<Char> &writer);
Glen Stark72d51e02016-06-08 01:23:32 +0200330};
331
332template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800333void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
Glen Stark72d51e02016-06-08 01:23:32 +0200334 for (;;) {
335 switch (*s++) {
336 case '-':
337 spec.align_ = ALIGN_LEFT;
338 break;
339 case '+':
340 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
341 break;
342 case '0':
343 spec.fill_ = '0';
344 break;
345 case ' ':
346 spec.flags_ |= SIGN_FLAG;
347 break;
348 case '#':
349 spec.flags_ |= HASH_FLAG;
350 break;
351 default:
352 --s;
353 return;
354 }
355 }
356}
357
358template <typename Char, typename AF>
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800359basic_format_arg<Char> printf_context<Char, AF>::get_arg(
360 const Char *s, unsigned arg_index) {
Glen Stark72d51e02016-06-08 01:23:32 +0200361 (void)s;
362 const char *error = 0;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800363 basic_format_arg<Char> arg =
364 arg_index == std::numeric_limits<unsigned>::max() ?
Victor Zverovichdafbec72016-10-07 08:37:06 -0700365 this->next_arg(error) : Base::get_arg(arg_index - 1, error);
Glen Stark72d51e02016-06-08 01:23:32 +0200366 if (error)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700367 FMT_THROW(format_error(!*s ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200368 return arg;
369}
370
371template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800372unsigned printf_context<Char, AF>::parse_header(
Glen Stark72d51e02016-06-08 01:23:32 +0200373 const Char *&s, FormatSpec &spec) {
374 unsigned arg_index = std::numeric_limits<unsigned>::max();
375 Char c = *s;
376 if (c >= '0' && c <= '9') {
377 // Parse an argument index (if followed by '$') or a width possibly
378 // preceded with '0' flag(s).
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700379 unsigned value = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200380 if (*s == '$') { // value is an argument index
381 ++s;
382 arg_index = value;
383 } else {
384 if (c == '0')
385 spec.fill_ = '0';
386 if (value != 0) {
387 // Nonzero value means that we parsed width and don't need to
388 // parse it or flags again, so return now.
389 spec.width_ = value;
390 return arg_index;
391 }
392 }
393 }
394 parse_flags(spec, s);
395 // Parse width.
396 if (*s >= '0' && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700397 spec.width_ = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200398 } else if (*s == '*') {
399 ++s;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800400 spec.width_ = visit(internal::PrintfWidthHandler(spec), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200401 }
402 return arg_index;
403}
404
405template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800406void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
407 const Char *start = this->ptr();
Glen Stark72d51e02016-06-08 01:23:32 +0200408 const Char *s = start;
409 while (*s) {
410 Char c = *s++;
411 if (c != '%') continue;
412 if (*s == c) {
Victor Zverovich2bba4202016-10-26 17:54:11 -0700413 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200414 start = ++s;
415 continue;
416 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700417 internal::write(writer, start, s - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200418
419 FormatSpec spec;
420 spec.align_ = ALIGN_RIGHT;
421
422 // Parse argument index, flags and width.
423 unsigned arg_index = parse_header(s, spec);
424
425 // Parse precision.
426 if (*s == '.') {
427 ++s;
428 if ('0' <= *s && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700429 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200430 } else if (*s == '*') {
431 ++s;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800432 spec.precision_ = visit(internal::PrintfPrecisionHandler(), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200433 }
434 }
435
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800436 basic_format_arg<Char> arg = get_arg(s, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800437 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700438 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200439 if (spec.fill_ == '0') {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800440 if (arg.is_numeric())
Glen Stark72d51e02016-06-08 01:23:32 +0200441 spec.align_ = ALIGN_NUMERIC;
442 else
443 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
444 }
445
446 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800447 using internal::convert_arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200448 switch (*s++) {
449 case 'h':
450 if (*s == 'h')
Victor Zverovich751ff642016-11-19 08:40:24 -0800451 convert_arg<signed char>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200452 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800453 convert_arg<short>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200454 break;
455 case 'l':
456 if (*s == 'l')
Victor Zverovich751ff642016-11-19 08:40:24 -0800457 convert_arg<fmt::LongLong>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200458 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800459 convert_arg<long>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200460 break;
461 case 'j':
Victor Zverovich751ff642016-11-19 08:40:24 -0800462 convert_arg<intmax_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200463 break;
464 case 'z':
Victor Zverovich751ff642016-11-19 08:40:24 -0800465 convert_arg<std::size_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200466 break;
467 case 't':
Victor Zverovich751ff642016-11-19 08:40:24 -0800468 convert_arg<std::ptrdiff_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200469 break;
470 case 'L':
471 // printf produces garbage when 'L' is omitted for long double, no
472 // need to do the same.
473 break;
474 default:
475 --s;
Victor Zverovich751ff642016-11-19 08:40:24 -0800476 convert_arg<void>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200477 }
478
479 // Parse type.
480 if (!*s)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700481 FMT_THROW(format_error("invalid format string"));
Glen Stark72d51e02016-06-08 01:23:32 +0200482 spec.type_ = static_cast<char>(*s++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800483 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200484 // Normalize type.
485 switch (spec.type_) {
486 case 'i': case 'u':
487 spec.type_ = 'd';
488 break;
489 case 'c':
490 // TODO: handle wchar_t
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800491 visit(internal::CharConverter<Char>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200492 break;
493 }
494 }
495
496 start = s;
497
498 // Format argument.
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800499 visit(AF(writer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200500 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700501 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200502}
Glen Stark72d51e02016-06-08 01:23:32 +0200503
Victor Zverovich18dfa252016-10-21 06:46:21 -0700504// Formats a value.
505template <typename Char, typename T>
Victor Zverovichb656a1c2016-10-25 06:19:19 -0700506void format_value(BasicWriter<Char> &w, const T &value,
Victor Zverovich9998f662016-11-06 16:11:24 -0800507 printf_context<Char>& ctx) {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700508 internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
Victor Zverovich2bba4202016-10-26 17:54:11 -0700509 w << internal::format_value(buffer, value);
Victor Zverovich18dfa252016-10-21 06:46:21 -0700510}
511
Glen Stark72d51e02016-06-08 01:23:32 +0200512template <typename Char>
Victor Zverovich0028ce52016-08-26 17:23:13 -0700513void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800514 basic_format_args<printf_context<Char>, Char> args) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800515 printf_context<Char>(format, args).format(w);
Glen Stark72d51e02016-06-08 01:23:32 +0200516}
517
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800518typedef basic_format_args<printf_context<char>, char> printf_args;
519
520inline std::string vsprintf(CStringRef format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700521 MemoryWriter w;
522 printf(w, format, args);
523 return w.str();
524}
525
Glen Stark72d51e02016-06-08 01:23:32 +0200526/**
527 \rst
528 Formats arguments and returns the result as a string.
529
530 **Example**::
531
532 std::string message = fmt::sprintf("The answer is %d", 42);
533 \endrst
534*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700535template <typename... Args>
536inline std::string sprintf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800537 return vsprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200538}
Glen Stark72d51e02016-06-08 01:23:32 +0200539
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800540inline std::wstring vsprintf(
541 WCStringRef format,
542 basic_format_args<printf_context<wchar_t>, wchar_t> args) {
Glen Stark72d51e02016-06-08 01:23:32 +0200543 WMemoryWriter w;
544 printf(w, format, args);
545 return w.str();
546}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700547
548template <typename... Args>
549inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800550 auto vargs = make_xformat_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700551 return vsprintf(format_str, vargs);
552}
553
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800554FMT_API int vfprintf(std::FILE *f, CStringRef format, printf_args args);
Glen Stark72d51e02016-06-08 01:23:32 +0200555
556/**
557 \rst
558 Prints formatted data to the file *f*.
559
560 **Example**::
561
562 fmt::fprintf(stderr, "Don't %s!", "panic");
563 \endrst
564 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700565template <typename... Args>
566inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800567 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700568 return vfprintf(f, format_str, vargs);
569}
570
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800571inline int vprintf(CStringRef format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700572 return vfprintf(stdout, format, args);
573}
Glen Stark72d51e02016-06-08 01:23:32 +0200574
575/**
576 \rst
577 Prints formatted data to ``stdout``.
578
579 **Example**::
580
581 fmt::printf("Elapsed time: %.2f seconds", 1.23);
582 \endrst
583 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700584template <typename... Args>
585inline int printf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800586 return vprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200587}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700588
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800589inline int vfprintf(std::ostream &os, CStringRef format_str, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700590 MemoryWriter w;
591 printf(w, format_str, args);
592 internal::write(os, w);
593 return static_cast<int>(w.size());
594}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700595
596/**
597 \rst
598 Prints formatted data to the stream *os*.
599
600 **Example**::
601
602 fprintf(cerr, "Don't %s!", "panic");
603 \endrst
604 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700605template <typename... Args>
606inline int fprintf(std::ostream &os, CStringRef format_str,
607 const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800608 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700609 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700610}
Glen Stark72d51e02016-06-08 01:23:32 +0200611} // namespace fmt
612
613#endif // FMT_PRINTF_H_