blob: 83f5fa3719aed0569a42ed6e8db75386f21f81b1 [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
43class PrecisionHandler : public ArgVisitor<PrecisionHandler, int> {
44 public:
45 void report_unhandled_arg() {
Victor Zverovich9bb213e2016-08-25 08:38:07 -070046 FMT_THROW(format_error("precision is not integer"));
Glen Stark72d51e02016-06-08 01:23:32 +020047 }
48
49 template <typename T>
50 int visit_any_int(T value) {
51 if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070052 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +020053 return static_cast<int>(value);
54 }
55};
56
57// IsZeroInt::visit(arg) returns true iff arg is a zero integer.
58class IsZeroInt : public ArgVisitor<IsZeroInt, bool> {
59 public:
60 template <typename T>
61 bool visit_any_int(T value) { return value == 0; }
62};
63
64template <typename T, typename U>
65struct is_same {
66 enum { value = 0 };
67};
68
69template <typename T>
70struct is_same<T, T> {
71 enum { value = 1 };
72};
73
Victor Zverovich751ff642016-11-19 08:40:24 -080074template <typename T>
75class ArgConverter {
Glen Stark72d51e02016-06-08 01:23:32 +020076 private:
77 internal::Arg &arg_;
78 wchar_t type_;
79
Glen Stark72d51e02016-06-08 01:23:32 +020080 public:
81 ArgConverter(internal::Arg &arg, wchar_t type)
82 : arg_(arg), type_(type) {}
83
Victor Zverovich751ff642016-11-19 08:40:24 -080084 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020085 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080086 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020087 }
88
89 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -080090 typename std::enable_if<std::is_integral<U>::value>::type
91 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020092 bool is_signed = type_ == 'd' || type_ == 'i';
93 using internal::Arg;
94 typedef typename internal::Conditional<
95 is_same<T, void>::value, U, T>::type TargetType;
96 if (sizeof(TargetType) <= sizeof(int)) {
97 // Extra casts are used to silence warnings.
98 if (is_signed) {
99 arg_.type = Arg::INT;
100 arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
101 } else {
102 arg_.type = Arg::UINT;
103 typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
104 arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
105 }
106 } else {
107 if (is_signed) {
108 arg_.type = Arg::LONG_LONG;
109 // glibc's printf doesn't sign extend arguments of smaller types:
110 // std::printf("%lld", -42); // prints "4294967254"
111 // but we don't have to do the same because it's a UB.
112 arg_.long_long_value = static_cast<LongLong>(value);
113 } else {
114 arg_.type = Arg::ULONG_LONG;
115 arg_.ulong_long_value =
116 static_cast<typename internal::MakeUnsigned<U>::Type>(value);
117 }
118 }
119 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800120
121 template <typename U>
122 typename std::enable_if<!std::is_integral<U>::value>::type
123 operator()(U value) {
124 // No coversion needed for non-integral types.
125 }
Glen Stark72d51e02016-06-08 01:23:32 +0200126};
127
Victor Zverovich751ff642016-11-19 08:40:24 -0800128// Converts an integer argument to T for printf, if T is an integral type.
129// If T is void, the argument is converted to corresponding signed or unsigned
130// type depending on the type specifier: 'd' and 'i' - signed, other -
131// unsigned).
132template <typename T>
133void convert_arg(format_arg &arg, wchar_t type) {
134 visit(ArgConverter<T>(arg, type), arg);
135}
136
Glen Stark72d51e02016-06-08 01:23:32 +0200137// Converts an integer argument to char for printf.
138class CharConverter : public ArgVisitor<CharConverter, void> {
139 private:
140 internal::Arg &arg_;
141
142 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
143
144 public:
145 explicit CharConverter(internal::Arg &arg) : arg_(arg) {}
146
147 template <typename T>
148 void visit_any_int(T value) {
149 arg_.type = internal::Arg::CHAR;
150 arg_.int_value = static_cast<char>(value);
151 }
152};
153
154// Checks if an argument is a valid printf width specifier and sets
155// left alignment if it is negative.
156class WidthHandler : public ArgVisitor<WidthHandler, unsigned> {
157 private:
158 FormatSpec &spec_;
159
160 FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler);
161
162 public:
163 explicit WidthHandler(FormatSpec &spec) : spec_(spec) {}
164
165 void report_unhandled_arg() {
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700166 FMT_THROW(format_error("width is not integer"));
Glen Stark72d51e02016-06-08 01:23:32 +0200167 }
168
169 template <typename T>
170 unsigned visit_any_int(T value) {
171 typedef typename internal::IntTraits<T>::MainType UnsignedType;
172 UnsignedType width = static_cast<UnsignedType>(value);
173 if (internal::is_negative(value)) {
174 spec_.align_ = ALIGN_LEFT;
175 width = 0 - width;
176 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700177 unsigned int_max = std::numeric_limits<int>::max();
178 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700179 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200180 return static_cast<unsigned>(width);
181 }
182};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700183} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200184
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700185/**
186 \rst
187 A ``printf`` argument formatter based on the `curiously recurring template
188 pattern <http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern>`_.
189
190 To use `~fmt::BasicPrintfArgFormatter` define a subclass that implements some
191 or all of the visit methods with the same signatures as the methods in
192 `~fmt::ArgVisitor`, for example, `~fmt::ArgVisitor::visit_int()`.
193 Pass the subclass as the *Impl* template parameter. When a formatting
194 function processes an argument, it will dispatch to a visit method
195 specific to the argument type. For example, if the argument type is
196 ``double`` then the `~fmt::ArgVisitor::visit_double()` method of a subclass
197 will be called. If the subclass doesn't contain a method with this signature,
198 then a corresponding method of `~fmt::BasicPrintfArgFormatter` or its
199 superclass will be called.
200 \endrst
201 */
Glen Stark72d51e02016-06-08 01:23:32 +0200202template <typename Impl, typename Char>
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700203class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200204 private:
205 void write_null_pointer() {
206 this->spec().type_ = 0;
207 this->write("(nil)");
208 }
209
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700210 typedef internal::ArgFormatterBase<Impl, Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200211
212 public:
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700213 /**
214 \rst
215 Constructs an argument formatter object.
216 *writer* is a reference to the output writer and *spec* contains format
217 specifier information for standard argument types.
218 \endrst
219 */
220 BasicPrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
221 : internal::ArgFormatterBase<Impl, Char>(writer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200222
Victor Zverovich95a53e12016-11-19 07:39:07 -0800223 using Base::operator();
224
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700225 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800226 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200227 FormatSpec &fmt_spec = this->spec();
228 if (fmt_spec.type_ != 's')
229 return this->visit_any_int(value);
230 fmt_spec.type_ = 0;
231 this->write(value);
232 }
233
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700234 /** Formats a character. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800235 void operator()(wchar_t value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200236 const FormatSpec &fmt_spec = this->spec();
237 BasicWriter<Char> &w = this->writer();
238 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
239 w.write_int(value, fmt_spec);
240 typedef typename BasicWriter<Char>::CharPtr CharPtr;
241 CharPtr out = CharPtr();
242 if (fmt_spec.width_ > 1) {
243 Char fill = ' ';
244 out = w.grow_buffer(fmt_spec.width_);
245 if (fmt_spec.align_ != ALIGN_LEFT) {
246 std::fill_n(out, fmt_spec.width_ - 1, fill);
247 out += fmt_spec.width_ - 1;
248 } else {
249 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
250 }
251 } else {
252 out = w.grow_buffer(1);
253 }
254 *out = static_cast<Char>(value);
255 }
256
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700257 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800258 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200259 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800260 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200261 else if (this->spec().type_ == 'p')
262 write_null_pointer();
263 else
264 this->write("(null)");
265 }
266
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700267 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800268 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200269 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800270 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200271 this->spec().type_ = 0;
272 write_null_pointer();
273 }
274
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700275 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800276 void operator()(internal::Arg::CustomValue c) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800277 const Char format_str[] = {'}', '\0'};
278 auto args = basic_format_args<basic_format_context<Char>>();
279 basic_format_context<Char> ctx(format_str, args);
280 c.format(&this->writer(), c.value, &ctx);
Glen Stark72d51e02016-06-08 01:23:32 +0200281 }
282};
283
284/** The default printf argument formatter. */
285template <typename Char>
286class PrintfArgFormatter
287 : public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> {
288 public:
289 /** Constructs an argument formatter object. */
290 PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
291 : BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
292};
293
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700294/** This template formats data and writes the output to a writer. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700295template <typename Char,
296 typename ArgFormatter = PrintfArgFormatter<Char> >
Victor Zverovich9998f662016-11-06 16:11:24 -0800297class printf_context :
298 private internal::format_context_base<
299 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700300 public:
301 /** The character type for the output. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700302 typedef Char char_type;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700303
Glen Stark72d51e02016-06-08 01:23:32 +0200304 private:
Victor Zverovich9998f662016-11-06 16:11:24 -0800305 typedef internal::format_context_base<Char, printf_context> Base;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700306
Glen Stark72d51e02016-06-08 01:23:32 +0200307 void parse_flags(FormatSpec &spec, const Char *&s);
308
309 // Returns the argument with specified index or, if arg_index is equal
310 // to the maximum unsigned value, the next argument.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700311 internal::Arg get_arg(
312 const Char *s,
Glen Stark72d51e02016-06-08 01:23:32 +0200313 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
314
315 // Parses argument index, flags and width and returns the argument index.
316 unsigned parse_header(const Char *&s, FormatSpec &spec);
317
318 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700319 /**
320 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800321 Constructs a ``printf_context`` object. References to the arguments and
322 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700323 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700324 \endrst
325 */
Victor Zverovich9998f662016-11-06 16:11:24 -0800326 explicit printf_context(BasicCStringRef<Char> format_str,
327 basic_format_args<printf_context> args)
328 : Base(format_str.c_str(), args) {}
329
Victor Zverovich355861f2016-07-20 08:26:14 -0700330 /** Formats stored arguments and writes the output to the writer. */
Victor Zverovich9998f662016-11-06 16:11:24 -0800331 FMT_API void format(BasicWriter<Char> &writer);
Glen Stark72d51e02016-06-08 01:23:32 +0200332};
333
334template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800335void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
Glen Stark72d51e02016-06-08 01:23:32 +0200336 for (;;) {
337 switch (*s++) {
338 case '-':
339 spec.align_ = ALIGN_LEFT;
340 break;
341 case '+':
342 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
343 break;
344 case '0':
345 spec.fill_ = '0';
346 break;
347 case ' ':
348 spec.flags_ |= SIGN_FLAG;
349 break;
350 case '#':
351 spec.flags_ |= HASH_FLAG;
352 break;
353 default:
354 --s;
355 return;
356 }
357 }
358}
359
360template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800361internal::Arg printf_context<Char, AF>::get_arg(const Char *s,
362 unsigned arg_index) {
Glen Stark72d51e02016-06-08 01:23:32 +0200363 (void)s;
364 const char *error = 0;
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700365 internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
Victor Zverovichdafbec72016-10-07 08:37:06 -0700366 this->next_arg(error) : Base::get_arg(arg_index - 1, error);
Glen Stark72d51e02016-06-08 01:23:32 +0200367 if (error)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700368 FMT_THROW(format_error(!*s ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200369 return arg;
370}
371
372template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800373unsigned printf_context<Char, AF>::parse_header(
Glen Stark72d51e02016-06-08 01:23:32 +0200374 const Char *&s, FormatSpec &spec) {
375 unsigned arg_index = std::numeric_limits<unsigned>::max();
376 Char c = *s;
377 if (c >= '0' && c <= '9') {
378 // Parse an argument index (if followed by '$') or a width possibly
379 // preceded with '0' flag(s).
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700380 unsigned value = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200381 if (*s == '$') { // value is an argument index
382 ++s;
383 arg_index = value;
384 } else {
385 if (c == '0')
386 spec.fill_ = '0';
387 if (value != 0) {
388 // Nonzero value means that we parsed width and don't need to
389 // parse it or flags again, so return now.
390 spec.width_ = value;
391 return arg_index;
392 }
393 }
394 }
395 parse_flags(spec, s);
396 // Parse width.
397 if (*s >= '0' && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700398 spec.width_ = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200399 } else if (*s == '*') {
400 ++s;
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800401 spec.width_ = visit(internal::WidthHandler(spec), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200402 }
403 return arg_index;
404}
405
406template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800407void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
408 const Char *start = this->ptr();
Glen Stark72d51e02016-06-08 01:23:32 +0200409 const Char *s = start;
410 while (*s) {
411 Char c = *s++;
412 if (c != '%') continue;
413 if (*s == c) {
Victor Zverovich2bba4202016-10-26 17:54:11 -0700414 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200415 start = ++s;
416 continue;
417 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700418 internal::write(writer, start, s - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200419
420 FormatSpec spec;
421 spec.align_ = ALIGN_RIGHT;
422
423 // Parse argument index, flags and width.
424 unsigned arg_index = parse_header(s, spec);
425
426 // Parse precision.
427 if (*s == '.') {
428 ++s;
429 if ('0' <= *s && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700430 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200431 } else if (*s == '*') {
432 ++s;
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800433 spec.precision_ = visit(internal::PrecisionHandler(), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200434 }
435 }
436
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700437 using internal::Arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200438 Arg arg = get_arg(s, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800439 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700440 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200441 if (spec.fill_ == '0') {
442 if (arg.type <= Arg::LAST_NUMERIC_TYPE)
443 spec.align_ = ALIGN_NUMERIC;
444 else
445 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
446 }
447
448 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800449 using internal::convert_arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200450 switch (*s++) {
451 case 'h':
452 if (*s == 'h')
Victor Zverovich751ff642016-11-19 08:40:24 -0800453 convert_arg<signed char>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200454 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800455 convert_arg<short>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200456 break;
457 case 'l':
458 if (*s == 'l')
Victor Zverovich751ff642016-11-19 08:40:24 -0800459 convert_arg<fmt::LongLong>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200460 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800461 convert_arg<long>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200462 break;
463 case 'j':
Victor Zverovich751ff642016-11-19 08:40:24 -0800464 convert_arg<intmax_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200465 break;
466 case 'z':
Victor Zverovich751ff642016-11-19 08:40:24 -0800467 convert_arg<std::size_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200468 break;
469 case 't':
Victor Zverovich751ff642016-11-19 08:40:24 -0800470 convert_arg<std::ptrdiff_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200471 break;
472 case 'L':
473 // printf produces garbage when 'L' is omitted for long double, no
474 // need to do the same.
475 break;
476 default:
477 --s;
Victor Zverovich751ff642016-11-19 08:40:24 -0800478 convert_arg<void>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200479 }
480
481 // Parse type.
482 if (!*s)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700483 FMT_THROW(format_error("invalid format string"));
Glen Stark72d51e02016-06-08 01:23:32 +0200484 spec.type_ = static_cast<char>(*s++);
485 if (arg.type <= Arg::LAST_INTEGER_TYPE) {
486 // Normalize type.
487 switch (spec.type_) {
488 case 'i': case 'u':
489 spec.type_ = 'd';
490 break;
491 case 'c':
492 // TODO: handle wchar_t
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800493 visit(internal::CharConverter(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200494 break;
495 }
496 }
497
498 start = s;
499
500 // Format argument.
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800501 visit(AF(writer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200502 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700503 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200504}
Glen Stark72d51e02016-06-08 01:23:32 +0200505
Victor Zverovich18dfa252016-10-21 06:46:21 -0700506// Formats a value.
507template <typename Char, typename T>
Victor Zverovichb656a1c2016-10-25 06:19:19 -0700508void format_value(BasicWriter<Char> &w, const T &value,
Victor Zverovich9998f662016-11-06 16:11:24 -0800509 printf_context<Char>& ctx) {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700510 internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
Victor Zverovich2bba4202016-10-26 17:54:11 -0700511 w << internal::format_value(buffer, value);
Victor Zverovich18dfa252016-10-21 06:46:21 -0700512}
513
Glen Stark72d51e02016-06-08 01:23:32 +0200514template <typename Char>
Victor Zverovich0028ce52016-08-26 17:23:13 -0700515void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800516 basic_format_args<printf_context<Char>> args) {
517 printf_context<Char>(format, args).format(w);
Glen Stark72d51e02016-06-08 01:23:32 +0200518}
519
Victor Zverovichdafbec72016-10-07 08:37:06 -0700520inline std::string vsprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800521 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700522 MemoryWriter w;
523 printf(w, format, args);
524 return w.str();
525}
526
Glen Stark72d51e02016-06-08 01:23:32 +0200527/**
528 \rst
529 Formats arguments and returns the result as a string.
530
531 **Example**::
532
533 std::string message = fmt::sprintf("The answer is %d", 42);
534 \endrst
535*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700536template <typename... Args>
537inline std::string sprintf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800538 return vsprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200539}
Glen Stark72d51e02016-06-08 01:23:32 +0200540
Victor Zverovichdafbec72016-10-07 08:37:06 -0700541inline std::wstring vsprintf(WCStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800542 basic_format_args<printf_context<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 Zverovichdafbec72016-10-07 08:37:06 -0700554FMT_API int vfprintf(std::FILE *f, CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800555 basic_format_args<printf_context<char>> args);
Glen Stark72d51e02016-06-08 01:23:32 +0200556
557/**
558 \rst
559 Prints formatted data to the file *f*.
560
561 **Example**::
562
563 fmt::fprintf(stderr, "Don't %s!", "panic");
564 \endrst
565 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700566template <typename... Args>
567inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800568 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700569 return vfprintf(f, format_str, vargs);
570}
571
Victor Zverovichdafbec72016-10-07 08:37:06 -0700572inline int vprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800573 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700574 return vfprintf(stdout, format, args);
575}
Glen Stark72d51e02016-06-08 01:23:32 +0200576
577/**
578 \rst
579 Prints formatted data to ``stdout``.
580
581 **Example**::
582
583 fmt::printf("Elapsed time: %.2f seconds", 1.23);
584 \endrst
585 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700586template <typename... Args>
587inline int printf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800588 return vprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200589}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700590
Victor Zverovichdafbec72016-10-07 08:37:06 -0700591inline int vfprintf(std::ostream &os, CStringRef format_str,
Victor Zverovich9998f662016-11-06 16:11:24 -0800592 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700593 MemoryWriter w;
594 printf(w, format_str, args);
595 internal::write(os, w);
596 return static_cast<int>(w.size());
597}
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 Zverovich85793a12016-11-06 19:27:14 -0800611 auto vargs = make_xformat_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_