blob: c8ae99b28c79ea23ec0ab6107a74ea304bc9640d [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 Zveroviche2dfd392016-11-19 09:29:09 -080043class PrecisionHandler {
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 Zverovich751ff642016-11-19 08:40:24 -080083template <typename T>
84class ArgConverter {
Glen Stark72d51e02016-06-08 01:23:32 +020085 private:
86 internal::Arg &arg_;
87 wchar_t type_;
88
Glen Stark72d51e02016-06-08 01:23:32 +020089 public:
90 ArgConverter(internal::Arg &arg, wchar_t type)
91 : 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';
102 using internal::Arg;
103 typedef typename internal::Conditional<
104 is_same<T, void>::value, U, T>::type TargetType;
105 if (sizeof(TargetType) <= sizeof(int)) {
106 // Extra casts are used to silence warnings.
107 if (is_signed) {
108 arg_.type = Arg::INT;
109 arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
110 } else {
111 arg_.type = Arg::UINT;
112 typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
113 arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
114 }
115 } else {
116 if (is_signed) {
117 arg_.type = Arg::LONG_LONG;
118 // glibc's printf doesn't sign extend arguments of smaller types:
119 // std::printf("%lld", -42); // prints "4294967254"
120 // but we don't have to do the same because it's a UB.
121 arg_.long_long_value = static_cast<LongLong>(value);
122 } else {
123 arg_.type = Arg::ULONG_LONG;
124 arg_.ulong_long_value =
125 static_cast<typename internal::MakeUnsigned<U>::Type>(value);
126 }
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).
141template <typename T>
142void convert_arg(format_arg &arg, wchar_t type) {
143 visit(ArgConverter<T>(arg, type), arg);
144}
145
Glen Stark72d51e02016-06-08 01:23:32 +0200146// Converts an integer argument to char for printf.
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800147class CharConverter {
Glen Stark72d51e02016-06-08 01:23:32 +0200148 private:
149 internal::Arg &arg_;
150
151 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
152
153 public:
154 explicit CharConverter(internal::Arg &arg) : arg_(arg) {}
155
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) {
Glen Stark72d51e02016-06-08 01:23:32 +0200159 arg_.type = internal::Arg::CHAR;
160 arg_.int_value = static_cast<char>(value);
161 }
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 Zveroviche2dfd392016-11-19 09:29:09 -0800172class WidthHandler {
Glen Stark72d51e02016-06-08 01:23:32 +0200173 private:
174 FormatSpec &spec_;
175
176 FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler);
177
178 public:
179 explicit WidthHandler(FormatSpec &spec) : spec_(spec) {}
180
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
207 A ``printf`` argument formatter based on the `curiously recurring template
208 pattern <http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern>`_.
209
210 To use `~fmt::BasicPrintfArgFormatter` define a subclass that implements some
211 or all of the visit methods with the same signatures as the methods in
212 `~fmt::ArgVisitor`, for example, `~fmt::ArgVisitor::visit_int()`.
213 Pass the subclass as the *Impl* template parameter. When a formatting
214 function processes an argument, it will dispatch to a visit method
215 specific to the argument type. For example, if the argument type is
216 ``double`` then the `~fmt::ArgVisitor::visit_double()` method of a subclass
217 will be called. If the subclass doesn't contain a method with this signature,
218 then a corresponding method of `~fmt::BasicPrintfArgFormatter` or its
219 superclass will be called.
220 \endrst
221 */
Glen Stark72d51e02016-06-08 01:23:32 +0200222template <typename Impl, typename Char>
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700223class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200224 private:
225 void write_null_pointer() {
226 this->spec().type_ = 0;
227 this->write("(nil)");
228 }
229
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700230 typedef internal::ArgFormatterBase<Impl, Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200231
232 public:
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700233 /**
234 \rst
235 Constructs an argument formatter object.
236 *writer* is a reference to the output writer and *spec* contains format
237 specifier information for standard argument types.
238 \endrst
239 */
240 BasicPrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
241 : internal::ArgFormatterBase<Impl, Char>(writer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200242
Victor Zverovich95a53e12016-11-19 07:39:07 -0800243 using Base::operator();
244
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700245 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800246 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200247 FormatSpec &fmt_spec = this->spec();
248 if (fmt_spec.type_ != 's')
249 return this->visit_any_int(value);
250 fmt_spec.type_ = 0;
251 this->write(value);
252 }
253
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700254 /** Formats a character. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800255 void operator()(wchar_t value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200256 const FormatSpec &fmt_spec = this->spec();
257 BasicWriter<Char> &w = this->writer();
258 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
259 w.write_int(value, fmt_spec);
260 typedef typename BasicWriter<Char>::CharPtr CharPtr;
261 CharPtr out = CharPtr();
262 if (fmt_spec.width_ > 1) {
263 Char fill = ' ';
264 out = w.grow_buffer(fmt_spec.width_);
265 if (fmt_spec.align_ != ALIGN_LEFT) {
266 std::fill_n(out, fmt_spec.width_ - 1, fill);
267 out += fmt_spec.width_ - 1;
268 } else {
269 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
270 }
271 } else {
272 out = w.grow_buffer(1);
273 }
274 *out = static_cast<Char>(value);
275 }
276
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700277 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800278 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200279 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800280 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200281 else if (this->spec().type_ == 'p')
282 write_null_pointer();
283 else
284 this->write("(null)");
285 }
286
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700287 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800288 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200289 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800290 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200291 this->spec().type_ = 0;
292 write_null_pointer();
293 }
294
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700295 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800296 void operator()(internal::Arg::CustomValue c) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800297 const Char format_str[] = {'}', '\0'};
298 auto args = basic_format_args<basic_format_context<Char>>();
299 basic_format_context<Char> ctx(format_str, args);
300 c.format(&this->writer(), c.value, &ctx);
Glen Stark72d51e02016-06-08 01:23:32 +0200301 }
302};
303
304/** The default printf argument formatter. */
305template <typename Char>
306class PrintfArgFormatter
307 : public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> {
308 public:
309 /** Constructs an argument formatter object. */
310 PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
311 : BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
312};
313
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700314/** This template formats data and writes the output to a writer. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700315template <typename Char,
316 typename ArgFormatter = PrintfArgFormatter<Char> >
Victor Zverovich9998f662016-11-06 16:11:24 -0800317class printf_context :
318 private internal::format_context_base<
319 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700320 public:
321 /** The character type for the output. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700322 typedef Char char_type;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700323
Glen Stark72d51e02016-06-08 01:23:32 +0200324 private:
Victor Zverovich9998f662016-11-06 16:11:24 -0800325 typedef internal::format_context_base<Char, printf_context> Base;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700326
Glen Stark72d51e02016-06-08 01:23:32 +0200327 void parse_flags(FormatSpec &spec, const Char *&s);
328
329 // Returns the argument with specified index or, if arg_index is equal
330 // to the maximum unsigned value, the next argument.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700331 internal::Arg get_arg(
332 const Char *s,
Glen Stark72d51e02016-06-08 01:23:32 +0200333 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
334
335 // Parses argument index, flags and width and returns the argument index.
336 unsigned parse_header(const Char *&s, FormatSpec &spec);
337
338 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700339 /**
340 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800341 Constructs a ``printf_context`` object. References to the arguments and
342 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700343 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700344 \endrst
345 */
Victor Zverovich9998f662016-11-06 16:11:24 -0800346 explicit printf_context(BasicCStringRef<Char> format_str,
347 basic_format_args<printf_context> args)
348 : Base(format_str.c_str(), args) {}
349
Victor Zverovich355861f2016-07-20 08:26:14 -0700350 /** Formats stored arguments and writes the output to the writer. */
Victor Zverovich9998f662016-11-06 16:11:24 -0800351 FMT_API void format(BasicWriter<Char> &writer);
Glen Stark72d51e02016-06-08 01:23:32 +0200352};
353
354template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800355void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
Glen Stark72d51e02016-06-08 01:23:32 +0200356 for (;;) {
357 switch (*s++) {
358 case '-':
359 spec.align_ = ALIGN_LEFT;
360 break;
361 case '+':
362 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
363 break;
364 case '0':
365 spec.fill_ = '0';
366 break;
367 case ' ':
368 spec.flags_ |= SIGN_FLAG;
369 break;
370 case '#':
371 spec.flags_ |= HASH_FLAG;
372 break;
373 default:
374 --s;
375 return;
376 }
377 }
378}
379
380template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800381internal::Arg printf_context<Char, AF>::get_arg(const Char *s,
382 unsigned arg_index) {
Glen Stark72d51e02016-06-08 01:23:32 +0200383 (void)s;
384 const char *error = 0;
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700385 internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
Victor Zverovichdafbec72016-10-07 08:37:06 -0700386 this->next_arg(error) : Base::get_arg(arg_index - 1, error);
Glen Stark72d51e02016-06-08 01:23:32 +0200387 if (error)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700388 FMT_THROW(format_error(!*s ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200389 return arg;
390}
391
392template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800393unsigned printf_context<Char, AF>::parse_header(
Glen Stark72d51e02016-06-08 01:23:32 +0200394 const Char *&s, FormatSpec &spec) {
395 unsigned arg_index = std::numeric_limits<unsigned>::max();
396 Char c = *s;
397 if (c >= '0' && c <= '9') {
398 // Parse an argument index (if followed by '$') or a width possibly
399 // preceded with '0' flag(s).
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700400 unsigned value = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200401 if (*s == '$') { // value is an argument index
402 ++s;
403 arg_index = value;
404 } else {
405 if (c == '0')
406 spec.fill_ = '0';
407 if (value != 0) {
408 // Nonzero value means that we parsed width and don't need to
409 // parse it or flags again, so return now.
410 spec.width_ = value;
411 return arg_index;
412 }
413 }
414 }
415 parse_flags(spec, s);
416 // Parse width.
417 if (*s >= '0' && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700418 spec.width_ = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200419 } else if (*s == '*') {
420 ++s;
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800421 spec.width_ = visit(internal::WidthHandler(spec), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200422 }
423 return arg_index;
424}
425
426template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800427void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
428 const Char *start = this->ptr();
Glen Stark72d51e02016-06-08 01:23:32 +0200429 const Char *s = start;
430 while (*s) {
431 Char c = *s++;
432 if (c != '%') continue;
433 if (*s == c) {
Victor Zverovich2bba4202016-10-26 17:54:11 -0700434 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200435 start = ++s;
436 continue;
437 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700438 internal::write(writer, start, s - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200439
440 FormatSpec spec;
441 spec.align_ = ALIGN_RIGHT;
442
443 // Parse argument index, flags and width.
444 unsigned arg_index = parse_header(s, spec);
445
446 // Parse precision.
447 if (*s == '.') {
448 ++s;
449 if ('0' <= *s && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700450 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200451 } else if (*s == '*') {
452 ++s;
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800453 spec.precision_ = visit(internal::PrecisionHandler(), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200454 }
455 }
456
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700457 using internal::Arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200458 Arg arg = get_arg(s, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800459 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700460 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200461 if (spec.fill_ == '0') {
462 if (arg.type <= Arg::LAST_NUMERIC_TYPE)
463 spec.align_ = ALIGN_NUMERIC;
464 else
465 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
466 }
467
468 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800469 using internal::convert_arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200470 switch (*s++) {
471 case 'h':
472 if (*s == 'h')
Victor Zverovich751ff642016-11-19 08:40:24 -0800473 convert_arg<signed char>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200474 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800475 convert_arg<short>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200476 break;
477 case 'l':
478 if (*s == 'l')
Victor Zverovich751ff642016-11-19 08:40:24 -0800479 convert_arg<fmt::LongLong>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200480 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800481 convert_arg<long>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200482 break;
483 case 'j':
Victor Zverovich751ff642016-11-19 08:40:24 -0800484 convert_arg<intmax_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200485 break;
486 case 'z':
Victor Zverovich751ff642016-11-19 08:40:24 -0800487 convert_arg<std::size_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200488 break;
489 case 't':
Victor Zverovich751ff642016-11-19 08:40:24 -0800490 convert_arg<std::ptrdiff_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200491 break;
492 case 'L':
493 // printf produces garbage when 'L' is omitted for long double, no
494 // need to do the same.
495 break;
496 default:
497 --s;
Victor Zverovich751ff642016-11-19 08:40:24 -0800498 convert_arg<void>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200499 }
500
501 // Parse type.
502 if (!*s)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700503 FMT_THROW(format_error("invalid format string"));
Glen Stark72d51e02016-06-08 01:23:32 +0200504 spec.type_ = static_cast<char>(*s++);
505 if (arg.type <= Arg::LAST_INTEGER_TYPE) {
506 // Normalize type.
507 switch (spec.type_) {
508 case 'i': case 'u':
509 spec.type_ = 'd';
510 break;
511 case 'c':
512 // TODO: handle wchar_t
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800513 visit(internal::CharConverter(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200514 break;
515 }
516 }
517
518 start = s;
519
520 // Format argument.
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800521 visit(AF(writer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200522 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700523 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200524}
Glen Stark72d51e02016-06-08 01:23:32 +0200525
Victor Zverovich18dfa252016-10-21 06:46:21 -0700526// Formats a value.
527template <typename Char, typename T>
Victor Zverovichb656a1c2016-10-25 06:19:19 -0700528void format_value(BasicWriter<Char> &w, const T &value,
Victor Zverovich9998f662016-11-06 16:11:24 -0800529 printf_context<Char>& ctx) {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700530 internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
Victor Zverovich2bba4202016-10-26 17:54:11 -0700531 w << internal::format_value(buffer, value);
Victor Zverovich18dfa252016-10-21 06:46:21 -0700532}
533
Glen Stark72d51e02016-06-08 01:23:32 +0200534template <typename Char>
Victor Zverovich0028ce52016-08-26 17:23:13 -0700535void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800536 basic_format_args<printf_context<Char>> args) {
537 printf_context<Char>(format, args).format(w);
Glen Stark72d51e02016-06-08 01:23:32 +0200538}
539
Victor Zverovichdafbec72016-10-07 08:37:06 -0700540inline std::string vsprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800541 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700542 MemoryWriter w;
543 printf(w, format, args);
544 return w.str();
545}
546
Glen Stark72d51e02016-06-08 01:23:32 +0200547/**
548 \rst
549 Formats arguments and returns the result as a string.
550
551 **Example**::
552
553 std::string message = fmt::sprintf("The answer is %d", 42);
554 \endrst
555*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700556template <typename... Args>
557inline std::string sprintf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800558 return vsprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200559}
Glen Stark72d51e02016-06-08 01:23:32 +0200560
Victor Zverovichdafbec72016-10-07 08:37:06 -0700561inline std::wstring vsprintf(WCStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800562 basic_format_args<printf_context<wchar_t>> args) {
Glen Stark72d51e02016-06-08 01:23:32 +0200563 WMemoryWriter w;
564 printf(w, format, args);
565 return w.str();
566}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700567
568template <typename... Args>
569inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800570 auto vargs = make_xformat_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700571 return vsprintf(format_str, vargs);
572}
573
Victor Zverovichdafbec72016-10-07 08:37:06 -0700574FMT_API int vfprintf(std::FILE *f, CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800575 basic_format_args<printf_context<char>> args);
Glen Stark72d51e02016-06-08 01:23:32 +0200576
577/**
578 \rst
579 Prints formatted data to the file *f*.
580
581 **Example**::
582
583 fmt::fprintf(stderr, "Don't %s!", "panic");
584 \endrst
585 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700586template <typename... Args>
587inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800588 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700589 return vfprintf(f, format_str, vargs);
590}
591
Victor Zverovichdafbec72016-10-07 08:37:06 -0700592inline int vprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800593 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700594 return vfprintf(stdout, format, args);
595}
Glen Stark72d51e02016-06-08 01:23:32 +0200596
597/**
598 \rst
599 Prints formatted data to ``stdout``.
600
601 **Example**::
602
603 fmt::printf("Elapsed time: %.2f seconds", 1.23);
604 \endrst
605 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700606template <typename... Args>
607inline int printf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800608 return vprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200609}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700610
Victor Zverovichdafbec72016-10-07 08:37:06 -0700611inline int vfprintf(std::ostream &os, CStringRef format_str,
Victor Zverovich9998f662016-11-06 16:11:24 -0800612 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700613 MemoryWriter w;
614 printf(w, format_str, args);
615 internal::write(os, w);
616 return static_cast<int>(w.size());
617}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700618
619/**
620 \rst
621 Prints formatted data to the stream *os*.
622
623 **Example**::
624
625 fprintf(cerr, "Don't %s!", "panic");
626 \endrst
627 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700628template <typename... Args>
629inline int fprintf(std::ostream &os, CStringRef format_str,
630 const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800631 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700632 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700633}
Glen Stark72d51e02016-06-08 01:23:32 +0200634} // namespace fmt
635
636#endif // FMT_PRINTF_H_