blob: 42728b788fb351289064b6c705cf6310bd95f770 [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 Zverovich6ee9f2e2016-07-21 06:59:28 -0700213 /** Formats an argument of type ``bool``. */
Glen Stark72d51e02016-06-08 01:23:32 +0200214 void visit_bool(bool value) {
215 FormatSpec &fmt_spec = this->spec();
216 if (fmt_spec.type_ != 's')
217 return this->visit_any_int(value);
218 fmt_spec.type_ = 0;
219 this->write(value);
220 }
221
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700222 /** Formats a character. */
Glen Stark72d51e02016-06-08 01:23:32 +0200223 void visit_char(int value) {
224 const FormatSpec &fmt_spec = this->spec();
225 BasicWriter<Char> &w = this->writer();
226 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
227 w.write_int(value, fmt_spec);
228 typedef typename BasicWriter<Char>::CharPtr CharPtr;
229 CharPtr out = CharPtr();
230 if (fmt_spec.width_ > 1) {
231 Char fill = ' ';
232 out = w.grow_buffer(fmt_spec.width_);
233 if (fmt_spec.align_ != ALIGN_LEFT) {
234 std::fill_n(out, fmt_spec.width_ - 1, fill);
235 out += fmt_spec.width_ - 1;
236 } else {
237 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
238 }
239 } else {
240 out = w.grow_buffer(1);
241 }
242 *out = static_cast<Char>(value);
243 }
244
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700245 /** Formats a null-terminated C string. */
Glen Stark72d51e02016-06-08 01:23:32 +0200246 void visit_cstring(const char *value) {
247 if (value)
248 Base::visit_cstring(value);
249 else if (this->spec().type_ == 'p')
250 write_null_pointer();
251 else
252 this->write("(null)");
253 }
254
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700255 /** Formats a pointer. */
Glen Stark72d51e02016-06-08 01:23:32 +0200256 void visit_pointer(const void *value) {
257 if (value)
258 return Base::visit_pointer(value);
259 this->spec().type_ = 0;
260 write_null_pointer();
261 }
262
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700263 /** Formats an argument of a custom (user-defined) type. */
264 void visit_custom(internal::Arg::CustomValue c) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800265 const Char format_str[] = {'}', '\0'};
266 auto args = basic_format_args<basic_format_context<Char>>();
267 basic_format_context<Char> ctx(format_str, args);
268 c.format(&this->writer(), c.value, &ctx);
Glen Stark72d51e02016-06-08 01:23:32 +0200269 }
270};
271
272/** The default printf argument formatter. */
273template <typename Char>
274class PrintfArgFormatter
275 : public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> {
276 public:
277 /** Constructs an argument formatter object. */
278 PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
279 : BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
280};
281
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700282/** This template formats data and writes the output to a writer. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700283template <typename Char,
284 typename ArgFormatter = PrintfArgFormatter<Char> >
Victor Zverovich9998f662016-11-06 16:11:24 -0800285class printf_context :
286 private internal::format_context_base<
287 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700288 public:
289 /** The character type for the output. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700290 typedef Char char_type;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700291
Glen Stark72d51e02016-06-08 01:23:32 +0200292 private:
Victor Zverovich9998f662016-11-06 16:11:24 -0800293 typedef internal::format_context_base<Char, printf_context> Base;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700294
Glen Stark72d51e02016-06-08 01:23:32 +0200295 void parse_flags(FormatSpec &spec, const Char *&s);
296
297 // Returns the argument with specified index or, if arg_index is equal
298 // to the maximum unsigned value, the next argument.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700299 internal::Arg get_arg(
300 const Char *s,
Glen Stark72d51e02016-06-08 01:23:32 +0200301 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
302
303 // Parses argument index, flags and width and returns the argument index.
304 unsigned parse_header(const Char *&s, FormatSpec &spec);
305
306 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700307 /**
308 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800309 Constructs a ``printf_context`` object. References to the arguments and
310 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700311 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700312 \endrst
313 */
Victor Zverovich9998f662016-11-06 16:11:24 -0800314 explicit printf_context(BasicCStringRef<Char> format_str,
315 basic_format_args<printf_context> args)
316 : Base(format_str.c_str(), args) {}
317
Victor Zverovich355861f2016-07-20 08:26:14 -0700318 /** Formats stored arguments and writes the output to the writer. */
Victor Zverovich9998f662016-11-06 16:11:24 -0800319 FMT_API void format(BasicWriter<Char> &writer);
Glen Stark72d51e02016-06-08 01:23:32 +0200320};
321
322template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800323void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
Glen Stark72d51e02016-06-08 01:23:32 +0200324 for (;;) {
325 switch (*s++) {
326 case '-':
327 spec.align_ = ALIGN_LEFT;
328 break;
329 case '+':
330 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
331 break;
332 case '0':
333 spec.fill_ = '0';
334 break;
335 case ' ':
336 spec.flags_ |= SIGN_FLAG;
337 break;
338 case '#':
339 spec.flags_ |= HASH_FLAG;
340 break;
341 default:
342 --s;
343 return;
344 }
345 }
346}
347
348template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800349internal::Arg printf_context<Char, AF>::get_arg(const Char *s,
350 unsigned arg_index) {
Glen Stark72d51e02016-06-08 01:23:32 +0200351 (void)s;
352 const char *error = 0;
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700353 internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
Victor Zverovichdafbec72016-10-07 08:37:06 -0700354 this->next_arg(error) : Base::get_arg(arg_index - 1, error);
Glen Stark72d51e02016-06-08 01:23:32 +0200355 if (error)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700356 FMT_THROW(format_error(!*s ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200357 return arg;
358}
359
360template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800361unsigned printf_context<Char, AF>::parse_header(
Glen Stark72d51e02016-06-08 01:23:32 +0200362 const Char *&s, FormatSpec &spec) {
363 unsigned arg_index = std::numeric_limits<unsigned>::max();
364 Char c = *s;
365 if (c >= '0' && c <= '9') {
366 // Parse an argument index (if followed by '$') or a width possibly
367 // preceded with '0' flag(s).
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700368 unsigned value = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200369 if (*s == '$') { // value is an argument index
370 ++s;
371 arg_index = value;
372 } else {
373 if (c == '0')
374 spec.fill_ = '0';
375 if (value != 0) {
376 // Nonzero value means that we parsed width and don't need to
377 // parse it or flags again, so return now.
378 spec.width_ = value;
379 return arg_index;
380 }
381 }
382 }
383 parse_flags(spec, s);
384 // Parse width.
385 if (*s >= '0' && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700386 spec.width_ = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200387 } else if (*s == '*') {
388 ++s;
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700389 spec.width_ = internal::WidthHandler(spec).visit(get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200390 }
391 return arg_index;
392}
393
394template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800395void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
396 const Char *start = this->ptr();
Glen Stark72d51e02016-06-08 01:23:32 +0200397 const Char *s = start;
398 while (*s) {
399 Char c = *s++;
400 if (c != '%') continue;
401 if (*s == c) {
Victor Zverovich2bba4202016-10-26 17:54:11 -0700402 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200403 start = ++s;
404 continue;
405 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700406 internal::write(writer, start, s - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200407
408 FormatSpec spec;
409 spec.align_ = ALIGN_RIGHT;
410
411 // Parse argument index, flags and width.
412 unsigned arg_index = parse_header(s, spec);
413
414 // Parse precision.
415 if (*s == '.') {
416 ++s;
417 if ('0' <= *s && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700418 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200419 } else if (*s == '*') {
420 ++s;
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700421 spec.precision_ = internal::PrecisionHandler().visit(get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200422 }
423 }
424
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700425 using internal::Arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200426 Arg arg = get_arg(s, arg_index);
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700427 if (spec.flag(HASH_FLAG) && internal::IsZeroInt().visit(arg))
428 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200429 if (spec.fill_ == '0') {
430 if (arg.type <= Arg::LAST_NUMERIC_TYPE)
431 spec.align_ = ALIGN_NUMERIC;
432 else
433 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
434 }
435
436 // Parse length and convert the argument to the required type.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700437 using internal::ArgConverter;
Glen Stark72d51e02016-06-08 01:23:32 +0200438 switch (*s++) {
439 case 'h':
440 if (*s == 'h')
441 ArgConverter<signed char>(arg, *++s).visit(arg);
442 else
443 ArgConverter<short>(arg, *s).visit(arg);
444 break;
445 case 'l':
446 if (*s == 'l')
447 ArgConverter<fmt::LongLong>(arg, *++s).visit(arg);
448 else
449 ArgConverter<long>(arg, *s).visit(arg);
450 break;
451 case 'j':
452 ArgConverter<intmax_t>(arg, *s).visit(arg);
453 break;
454 case 'z':
455 ArgConverter<std::size_t>(arg, *s).visit(arg);
456 break;
457 case 't':
458 ArgConverter<std::ptrdiff_t>(arg, *s).visit(arg);
459 break;
460 case 'L':
461 // printf produces garbage when 'L' is omitted for long double, no
462 // need to do the same.
463 break;
464 default:
465 --s;
466 ArgConverter<void>(arg, *s).visit(arg);
467 }
468
469 // Parse type.
470 if (!*s)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700471 FMT_THROW(format_error("invalid format string"));
Glen Stark72d51e02016-06-08 01:23:32 +0200472 spec.type_ = static_cast<char>(*s++);
473 if (arg.type <= Arg::LAST_INTEGER_TYPE) {
474 // Normalize type.
475 switch (spec.type_) {
476 case 'i': case 'u':
477 spec.type_ = 'd';
478 break;
479 case 'c':
480 // TODO: handle wchar_t
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700481 internal::CharConverter(arg).visit(arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200482 break;
483 }
484 }
485
486 start = s;
487
488 // Format argument.
Victor Zverovich2bba4202016-10-26 17:54:11 -0700489 AF(writer, spec).visit(arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200490 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700491 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200492}
Glen Stark72d51e02016-06-08 01:23:32 +0200493
Victor Zverovich18dfa252016-10-21 06:46:21 -0700494// Formats a value.
495template <typename Char, typename T>
Victor Zverovichb656a1c2016-10-25 06:19:19 -0700496void format_value(BasicWriter<Char> &w, const T &value,
Victor Zverovich9998f662016-11-06 16:11:24 -0800497 printf_context<Char>& ctx) {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700498 internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
Victor Zverovich2bba4202016-10-26 17:54:11 -0700499 w << internal::format_value(buffer, value);
Victor Zverovich18dfa252016-10-21 06:46:21 -0700500}
501
Glen Stark72d51e02016-06-08 01:23:32 +0200502template <typename Char>
Victor Zverovich0028ce52016-08-26 17:23:13 -0700503void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800504 basic_format_args<printf_context<Char>> args) {
505 printf_context<Char>(format, args).format(w);
Glen Stark72d51e02016-06-08 01:23:32 +0200506}
507
Victor Zverovichdafbec72016-10-07 08:37:06 -0700508inline std::string vsprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800509 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700510 MemoryWriter w;
511 printf(w, format, args);
512 return w.str();
513}
514
Glen Stark72d51e02016-06-08 01:23:32 +0200515/**
516 \rst
517 Formats arguments and returns the result as a string.
518
519 **Example**::
520
521 std::string message = fmt::sprintf("The answer is %d", 42);
522 \endrst
523*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700524template <typename... Args>
525inline std::string sprintf(CStringRef format_str, const Args & ... args) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800526 return vsprintf(format_str, make_format_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200527}
Glen Stark72d51e02016-06-08 01:23:32 +0200528
Victor Zverovichdafbec72016-10-07 08:37:06 -0700529inline std::wstring vsprintf(WCStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800530 basic_format_args<printf_context<wchar_t>> args) {
Glen Stark72d51e02016-06-08 01:23:32 +0200531 WMemoryWriter w;
532 printf(w, format, args);
533 return w.str();
534}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700535
536template <typename... Args>
537inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800538 auto vargs = make_format_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700539 return vsprintf(format_str, vargs);
540}
541
Victor Zverovichdafbec72016-10-07 08:37:06 -0700542FMT_API int vfprintf(std::FILE *f, CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800543 basic_format_args<printf_context<char>> args);
Glen Stark72d51e02016-06-08 01:23:32 +0200544
545/**
546 \rst
547 Prints formatted data to the file *f*.
548
549 **Example**::
550
551 fmt::fprintf(stderr, "Don't %s!", "panic");
552 \endrst
553 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700554template <typename... Args>
555inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800556 auto vargs = make_format_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700557 return vfprintf(f, format_str, vargs);
558}
559
Victor Zverovichdafbec72016-10-07 08:37:06 -0700560inline int vprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800561 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700562 return vfprintf(stdout, format, args);
563}
Glen Stark72d51e02016-06-08 01:23:32 +0200564
565/**
566 \rst
567 Prints formatted data to ``stdout``.
568
569 **Example**::
570
571 fmt::printf("Elapsed time: %.2f seconds", 1.23);
572 \endrst
573 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700574template <typename... Args>
575inline int printf(CStringRef format_str, const Args & ... args) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800576 return vprintf(format_str, make_format_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200577}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700578
Victor Zverovichdafbec72016-10-07 08:37:06 -0700579inline int vfprintf(std::ostream &os, CStringRef format_str,
Victor Zverovich9998f662016-11-06 16:11:24 -0800580 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700581 MemoryWriter w;
582 printf(w, format_str, args);
583 internal::write(os, w);
584 return static_cast<int>(w.size());
585}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700586
587/**
588 \rst
589 Prints formatted data to the stream *os*.
590
591 **Example**::
592
593 fprintf(cerr, "Don't %s!", "panic");
594 \endrst
595 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700596template <typename... Args>
597inline int fprintf(std::ostream &os, CStringRef format_str,
598 const Args & ... args) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800599 auto vargs = make_format_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700600 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700601}
Glen Stark72d51e02016-06-08 01:23:32 +0200602} // namespace fmt
603
604#endif // FMT_PRINTF_H_