blob: 8f9278fef50d23aedbd7f20173e4e34ae5340fd5 [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
74// An argument visitor that converts an integer argument to T for printf,
75// if T is an integral type. If T is void, the argument is converted to
76// corresponding signed or unsigned type depending on the type specifier:
77// 'd' and 'i' - signed, other - unsigned)
78template <typename T = void>
79class ArgConverter : public ArgVisitor<ArgConverter<T>, void> {
80 private:
81 internal::Arg &arg_;
82 wchar_t type_;
83
84 FMT_DISALLOW_COPY_AND_ASSIGN(ArgConverter);
85
86 public:
87 ArgConverter(internal::Arg &arg, wchar_t type)
88 : arg_(arg), type_(type) {}
89
90 void visit_bool(bool value) {
91 if (type_ != 's')
92 visit_any_int(value);
93 }
94
95 template <typename U>
96 void visit_any_int(U value) {
97 bool is_signed = type_ == 'd' || type_ == 'i';
98 using internal::Arg;
99 typedef typename internal::Conditional<
100 is_same<T, void>::value, U, T>::type TargetType;
101 if (sizeof(TargetType) <= sizeof(int)) {
102 // Extra casts are used to silence warnings.
103 if (is_signed) {
104 arg_.type = Arg::INT;
105 arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
106 } else {
107 arg_.type = Arg::UINT;
108 typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
109 arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
110 }
111 } else {
112 if (is_signed) {
113 arg_.type = Arg::LONG_LONG;
114 // glibc's printf doesn't sign extend arguments of smaller types:
115 // std::printf("%lld", -42); // prints "4294967254"
116 // but we don't have to do the same because it's a UB.
117 arg_.long_long_value = static_cast<LongLong>(value);
118 } else {
119 arg_.type = Arg::ULONG_LONG;
120 arg_.ulong_long_value =
121 static_cast<typename internal::MakeUnsigned<U>::Type>(value);
122 }
123 }
124 }
125};
126
127// Converts an integer argument to char for printf.
128class CharConverter : public ArgVisitor<CharConverter, void> {
129 private:
130 internal::Arg &arg_;
131
132 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
133
134 public:
135 explicit CharConverter(internal::Arg &arg) : arg_(arg) {}
136
137 template <typename T>
138 void visit_any_int(T value) {
139 arg_.type = internal::Arg::CHAR;
140 arg_.int_value = static_cast<char>(value);
141 }
142};
143
144// Checks if an argument is a valid printf width specifier and sets
145// left alignment if it is negative.
146class WidthHandler : public ArgVisitor<WidthHandler, unsigned> {
147 private:
148 FormatSpec &spec_;
149
150 FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler);
151
152 public:
153 explicit WidthHandler(FormatSpec &spec) : spec_(spec) {}
154
155 void report_unhandled_arg() {
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700156 FMT_THROW(format_error("width is not integer"));
Glen Stark72d51e02016-06-08 01:23:32 +0200157 }
158
159 template <typename T>
160 unsigned visit_any_int(T value) {
161 typedef typename internal::IntTraits<T>::MainType UnsignedType;
162 UnsignedType width = static_cast<UnsignedType>(value);
163 if (internal::is_negative(value)) {
164 spec_.align_ = ALIGN_LEFT;
165 width = 0 - width;
166 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700167 unsigned int_max = std::numeric_limits<int>::max();
168 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700169 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200170 return static_cast<unsigned>(width);
171 }
172};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700173} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200174
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700175/**
176 \rst
177 A ``printf`` argument formatter based on the `curiously recurring template
178 pattern <http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern>`_.
179
180 To use `~fmt::BasicPrintfArgFormatter` define a subclass that implements some
181 or all of the visit methods with the same signatures as the methods in
182 `~fmt::ArgVisitor`, for example, `~fmt::ArgVisitor::visit_int()`.
183 Pass the subclass as the *Impl* template parameter. When a formatting
184 function processes an argument, it will dispatch to a visit method
185 specific to the argument type. For example, if the argument type is
186 ``double`` then the `~fmt::ArgVisitor::visit_double()` method of a subclass
187 will be called. If the subclass doesn't contain a method with this signature,
188 then a corresponding method of `~fmt::BasicPrintfArgFormatter` or its
189 superclass will be called.
190 \endrst
191 */
Glen Stark72d51e02016-06-08 01:23:32 +0200192template <typename Impl, typename Char>
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700193class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200194 private:
195 void write_null_pointer() {
196 this->spec().type_ = 0;
197 this->write("(nil)");
198 }
199
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700200 typedef internal::ArgFormatterBase<Impl, Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200201
202 public:
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700203 /**
204 \rst
205 Constructs an argument formatter object.
206 *writer* is a reference to the output writer and *spec* contains format
207 specifier information for standard argument types.
208 \endrst
209 */
210 BasicPrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
211 : internal::ArgFormatterBase<Impl, Char>(writer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200212
Victor Zverovich95a53e12016-11-19 07:39:07 -0800213 using Base::operator();
214
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700215 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800216 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200217 FormatSpec &fmt_spec = this->spec();
218 if (fmt_spec.type_ != 's')
219 return this->visit_any_int(value);
220 fmt_spec.type_ = 0;
221 this->write(value);
222 }
223
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700224 /** Formats a character. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800225 void operator()(wchar_t value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200226 const FormatSpec &fmt_spec = this->spec();
227 BasicWriter<Char> &w = this->writer();
228 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
229 w.write_int(value, fmt_spec);
230 typedef typename BasicWriter<Char>::CharPtr CharPtr;
231 CharPtr out = CharPtr();
232 if (fmt_spec.width_ > 1) {
233 Char fill = ' ';
234 out = w.grow_buffer(fmt_spec.width_);
235 if (fmt_spec.align_ != ALIGN_LEFT) {
236 std::fill_n(out, fmt_spec.width_ - 1, fill);
237 out += fmt_spec.width_ - 1;
238 } else {
239 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
240 }
241 } else {
242 out = w.grow_buffer(1);
243 }
244 *out = static_cast<Char>(value);
245 }
246
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700247 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800248 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200249 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800250 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200251 else if (this->spec().type_ == 'p')
252 write_null_pointer();
253 else
254 this->write("(null)");
255 }
256
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700257 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800258 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200259 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800260 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200261 this->spec().type_ = 0;
262 write_null_pointer();
263 }
264
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700265 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800266 void operator()(internal::Arg::CustomValue c) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800267 const Char format_str[] = {'}', '\0'};
268 auto args = basic_format_args<basic_format_context<Char>>();
269 basic_format_context<Char> ctx(format_str, args);
270 c.format(&this->writer(), c.value, &ctx);
Glen Stark72d51e02016-06-08 01:23:32 +0200271 }
272};
273
274/** The default printf argument formatter. */
275template <typename Char>
276class PrintfArgFormatter
277 : public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> {
278 public:
279 /** Constructs an argument formatter object. */
280 PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
281 : BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
282};
283
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700284/** This template formats data and writes the output to a writer. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700285template <typename Char,
286 typename ArgFormatter = PrintfArgFormatter<Char> >
Victor Zverovich9998f662016-11-06 16:11:24 -0800287class printf_context :
288 private internal::format_context_base<
289 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700290 public:
291 /** The character type for the output. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700292 typedef Char char_type;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700293
Glen Stark72d51e02016-06-08 01:23:32 +0200294 private:
Victor Zverovich9998f662016-11-06 16:11:24 -0800295 typedef internal::format_context_base<Char, printf_context> Base;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700296
Glen Stark72d51e02016-06-08 01:23:32 +0200297 void parse_flags(FormatSpec &spec, const Char *&s);
298
299 // Returns the argument with specified index or, if arg_index is equal
300 // to the maximum unsigned value, the next argument.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700301 internal::Arg get_arg(
302 const Char *s,
Glen Stark72d51e02016-06-08 01:23:32 +0200303 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
304
305 // Parses argument index, flags and width and returns the argument index.
306 unsigned parse_header(const Char *&s, FormatSpec &spec);
307
308 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700309 /**
310 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800311 Constructs a ``printf_context`` object. References to the arguments and
312 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700313 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700314 \endrst
315 */
Victor Zverovich9998f662016-11-06 16:11:24 -0800316 explicit printf_context(BasicCStringRef<Char> format_str,
317 basic_format_args<printf_context> args)
318 : Base(format_str.c_str(), args) {}
319
Victor Zverovich355861f2016-07-20 08:26:14 -0700320 /** Formats stored arguments and writes the output to the writer. */
Victor Zverovich9998f662016-11-06 16:11:24 -0800321 FMT_API void format(BasicWriter<Char> &writer);
Glen Stark72d51e02016-06-08 01:23:32 +0200322};
323
324template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800325void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
Glen Stark72d51e02016-06-08 01:23:32 +0200326 for (;;) {
327 switch (*s++) {
328 case '-':
329 spec.align_ = ALIGN_LEFT;
330 break;
331 case '+':
332 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
333 break;
334 case '0':
335 spec.fill_ = '0';
336 break;
337 case ' ':
338 spec.flags_ |= SIGN_FLAG;
339 break;
340 case '#':
341 spec.flags_ |= HASH_FLAG;
342 break;
343 default:
344 --s;
345 return;
346 }
347 }
348}
349
350template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800351internal::Arg printf_context<Char, AF>::get_arg(const Char *s,
352 unsigned arg_index) {
Glen Stark72d51e02016-06-08 01:23:32 +0200353 (void)s;
354 const char *error = 0;
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700355 internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
Victor Zverovichdafbec72016-10-07 08:37:06 -0700356 this->next_arg(error) : Base::get_arg(arg_index - 1, error);
Glen Stark72d51e02016-06-08 01:23:32 +0200357 if (error)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700358 FMT_THROW(format_error(!*s ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200359 return arg;
360}
361
362template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800363unsigned printf_context<Char, AF>::parse_header(
Glen Stark72d51e02016-06-08 01:23:32 +0200364 const Char *&s, FormatSpec &spec) {
365 unsigned arg_index = std::numeric_limits<unsigned>::max();
366 Char c = *s;
367 if (c >= '0' && c <= '9') {
368 // Parse an argument index (if followed by '$') or a width possibly
369 // preceded with '0' flag(s).
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700370 unsigned value = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200371 if (*s == '$') { // value is an argument index
372 ++s;
373 arg_index = value;
374 } else {
375 if (c == '0')
376 spec.fill_ = '0';
377 if (value != 0) {
378 // Nonzero value means that we parsed width and don't need to
379 // parse it or flags again, so return now.
380 spec.width_ = value;
381 return arg_index;
382 }
383 }
384 }
385 parse_flags(spec, s);
386 // Parse width.
387 if (*s >= '0' && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700388 spec.width_ = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200389 } else if (*s == '*') {
390 ++s;
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700391 spec.width_ = internal::WidthHandler(spec).visit(get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200392 }
393 return arg_index;
394}
395
396template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800397void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
398 const Char *start = this->ptr();
Glen Stark72d51e02016-06-08 01:23:32 +0200399 const Char *s = start;
400 while (*s) {
401 Char c = *s++;
402 if (c != '%') continue;
403 if (*s == c) {
Victor Zverovich2bba4202016-10-26 17:54:11 -0700404 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200405 start = ++s;
406 continue;
407 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700408 internal::write(writer, start, s - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200409
410 FormatSpec spec;
411 spec.align_ = ALIGN_RIGHT;
412
413 // Parse argument index, flags and width.
414 unsigned arg_index = parse_header(s, spec);
415
416 // Parse precision.
417 if (*s == '.') {
418 ++s;
419 if ('0' <= *s && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700420 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200421 } else if (*s == '*') {
422 ++s;
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700423 spec.precision_ = internal::PrecisionHandler().visit(get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200424 }
425 }
426
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700427 using internal::Arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200428 Arg arg = get_arg(s, arg_index);
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700429 if (spec.flag(HASH_FLAG) && internal::IsZeroInt().visit(arg))
430 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200431 if (spec.fill_ == '0') {
432 if (arg.type <= Arg::LAST_NUMERIC_TYPE)
433 spec.align_ = ALIGN_NUMERIC;
434 else
435 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
436 }
437
438 // Parse length and convert the argument to the required type.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700439 using internal::ArgConverter;
Glen Stark72d51e02016-06-08 01:23:32 +0200440 switch (*s++) {
441 case 'h':
442 if (*s == 'h')
443 ArgConverter<signed char>(arg, *++s).visit(arg);
444 else
445 ArgConverter<short>(arg, *s).visit(arg);
446 break;
447 case 'l':
448 if (*s == 'l')
449 ArgConverter<fmt::LongLong>(arg, *++s).visit(arg);
450 else
451 ArgConverter<long>(arg, *s).visit(arg);
452 break;
453 case 'j':
454 ArgConverter<intmax_t>(arg, *s).visit(arg);
455 break;
456 case 'z':
457 ArgConverter<std::size_t>(arg, *s).visit(arg);
458 break;
459 case 't':
460 ArgConverter<std::ptrdiff_t>(arg, *s).visit(arg);
461 break;
462 case 'L':
463 // printf produces garbage when 'L' is omitted for long double, no
464 // need to do the same.
465 break;
466 default:
467 --s;
468 ArgConverter<void>(arg, *s).visit(arg);
469 }
470
471 // Parse type.
472 if (!*s)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700473 FMT_THROW(format_error("invalid format string"));
Glen Stark72d51e02016-06-08 01:23:32 +0200474 spec.type_ = static_cast<char>(*s++);
475 if (arg.type <= Arg::LAST_INTEGER_TYPE) {
476 // Normalize type.
477 switch (spec.type_) {
478 case 'i': case 'u':
479 spec.type_ = 'd';
480 break;
481 case 'c':
482 // TODO: handle wchar_t
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700483 internal::CharConverter(arg).visit(arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200484 break;
485 }
486 }
487
488 start = s;
489
490 // Format argument.
Victor Zverovich2bba4202016-10-26 17:54:11 -0700491 AF(writer, spec).visit(arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200492 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700493 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200494}
Glen Stark72d51e02016-06-08 01:23:32 +0200495
Victor Zverovich18dfa252016-10-21 06:46:21 -0700496// Formats a value.
497template <typename Char, typename T>
Victor Zverovichb656a1c2016-10-25 06:19:19 -0700498void format_value(BasicWriter<Char> &w, const T &value,
Victor Zverovich9998f662016-11-06 16:11:24 -0800499 printf_context<Char>& ctx) {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700500 internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
Victor Zverovich2bba4202016-10-26 17:54:11 -0700501 w << internal::format_value(buffer, value);
Victor Zverovich18dfa252016-10-21 06:46:21 -0700502}
503
Glen Stark72d51e02016-06-08 01:23:32 +0200504template <typename Char>
Victor Zverovich0028ce52016-08-26 17:23:13 -0700505void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800506 basic_format_args<printf_context<Char>> args) {
507 printf_context<Char>(format, args).format(w);
Glen Stark72d51e02016-06-08 01:23:32 +0200508}
509
Victor Zverovichdafbec72016-10-07 08:37:06 -0700510inline std::string vsprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800511 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700512 MemoryWriter w;
513 printf(w, format, args);
514 return w.str();
515}
516
Glen Stark72d51e02016-06-08 01:23:32 +0200517/**
518 \rst
519 Formats arguments and returns the result as a string.
520
521 **Example**::
522
523 std::string message = fmt::sprintf("The answer is %d", 42);
524 \endrst
525*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700526template <typename... Args>
527inline std::string sprintf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800528 return vsprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200529}
Glen Stark72d51e02016-06-08 01:23:32 +0200530
Victor Zverovichdafbec72016-10-07 08:37:06 -0700531inline std::wstring vsprintf(WCStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800532 basic_format_args<printf_context<wchar_t>> args) {
Glen Stark72d51e02016-06-08 01:23:32 +0200533 WMemoryWriter w;
534 printf(w, format, args);
535 return w.str();
536}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700537
538template <typename... Args>
539inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800540 auto vargs = make_xformat_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700541 return vsprintf(format_str, vargs);
542}
543
Victor Zverovichdafbec72016-10-07 08:37:06 -0700544FMT_API int vfprintf(std::FILE *f, CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800545 basic_format_args<printf_context<char>> args);
Glen Stark72d51e02016-06-08 01:23:32 +0200546
547/**
548 \rst
549 Prints formatted data to the file *f*.
550
551 **Example**::
552
553 fmt::fprintf(stderr, "Don't %s!", "panic");
554 \endrst
555 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700556template <typename... Args>
557inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800558 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700559 return vfprintf(f, format_str, vargs);
560}
561
Victor Zverovichdafbec72016-10-07 08:37:06 -0700562inline int vprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800563 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700564 return vfprintf(stdout, format, args);
565}
Glen Stark72d51e02016-06-08 01:23:32 +0200566
567/**
568 \rst
569 Prints formatted data to ``stdout``.
570
571 **Example**::
572
573 fmt::printf("Elapsed time: %.2f seconds", 1.23);
574 \endrst
575 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700576template <typename... Args>
577inline int printf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800578 return vprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200579}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700580
Victor Zverovichdafbec72016-10-07 08:37:06 -0700581inline int vfprintf(std::ostream &os, CStringRef format_str,
Victor Zverovich9998f662016-11-06 16:11:24 -0800582 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700583 MemoryWriter w;
584 printf(w, format_str, args);
585 internal::write(os, w);
586 return static_cast<int>(w.size());
587}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700588
589/**
590 \rst
591 Prints formatted data to the stream *os*.
592
593 **Example**::
594
595 fprintf(cerr, "Don't %s!", "panic");
596 \endrst
597 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700598template <typename... Args>
599inline int fprintf(std::ostream &os, CStringRef format_str,
600 const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800601 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700602 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700603}
Glen Stark72d51e02016-06-08 01:23:32 +0200604} // namespace fmt
605
606#endif // FMT_PRINTF_H_