blob: 386acf98e14d246fd3e20017bf3373040cc6096a [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:
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -080086 format_arg &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +020087 wchar_t type_;
88
Glen Stark72d51e02016-06-08 01:23:32 +020089 public:
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -080090 ArgConverter(format_arg &arg, wchar_t type)
Glen Stark72d51e02016-06-08 01:23:32 +020091 : arg_(arg), type_(type) {}
92
Victor Zverovich751ff642016-11-19 08:40:24 -080093 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020094 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080095 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020096 }
97
98 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -080099 typename std::enable_if<std::is_integral<U>::value>::type
100 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200101 bool is_signed = type_ == 'd' || type_ == 'i';
Glen Stark72d51e02016-06-08 01:23:32 +0200102 typedef typename internal::Conditional<
103 is_same<T, void>::value, U, T>::type TargetType;
104 if (sizeof(TargetType) <= sizeof(int)) {
105 // Extra casts are used to silence warnings.
106 if (is_signed) {
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800107 arg_.type = format_arg::INT;
Glen Stark72d51e02016-06-08 01:23:32 +0200108 arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
109 } else {
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800110 arg_.type = format_arg::UINT;
Glen Stark72d51e02016-06-08 01:23:32 +0200111 typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
112 arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
113 }
114 } else {
115 if (is_signed) {
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800116 arg_.type = format_arg::LONG_LONG;
Glen Stark72d51e02016-06-08 01:23:32 +0200117 // glibc's printf doesn't sign extend arguments of smaller types:
118 // std::printf("%lld", -42); // prints "4294967254"
119 // but we don't have to do the same because it's a UB.
120 arg_.long_long_value = static_cast<LongLong>(value);
121 } else {
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800122 arg_.type = format_arg::ULONG_LONG;
Glen Stark72d51e02016-06-08 01:23:32 +0200123 arg_.ulong_long_value =
124 static_cast<typename internal::MakeUnsigned<U>::Type>(value);
125 }
126 }
127 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800128
129 template <typename U>
130 typename std::enable_if<!std::is_integral<U>::value>::type
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800131 operator()(U value) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800132 // No coversion needed for non-integral types.
133 }
Glen Stark72d51e02016-06-08 01:23:32 +0200134};
135
Victor Zverovich751ff642016-11-19 08:40:24 -0800136// Converts an integer argument to T for printf, if T is an integral type.
137// If T is void, the argument is converted to corresponding signed or unsigned
138// type depending on the type specifier: 'd' and 'i' - signed, other -
139// unsigned).
140template <typename T>
141void convert_arg(format_arg &arg, wchar_t type) {
142 visit(ArgConverter<T>(arg, type), arg);
143}
144
Glen Stark72d51e02016-06-08 01:23:32 +0200145// Converts an integer argument to char for printf.
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800146class CharConverter {
Glen Stark72d51e02016-06-08 01:23:32 +0200147 private:
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800148 format_arg &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200149
150 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
151
152 public:
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800153 explicit CharConverter(format_arg &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200154
155 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800156 typename std::enable_if<std::is_integral<T>::value>::type
157 operator()(T value) {
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800158 arg_.type = format_arg::CHAR;
Glen Stark72d51e02016-06-08 01:23:32 +0200159 arg_.int_value = static_cast<char>(value);
160 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800161
162 template <typename T>
163 typename std::enable_if<!std::is_integral<T>::value>::type
164 operator()(T value) {
165 // No coversion needed for non-integral types.
166 }
Glen Stark72d51e02016-06-08 01:23:32 +0200167};
168
169// Checks if an argument is a valid printf width specifier and sets
170// left alignment if it is negative.
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800171class WidthHandler {
Glen Stark72d51e02016-06-08 01:23:32 +0200172 private:
173 FormatSpec &spec_;
174
175 FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler);
176
177 public:
178 explicit WidthHandler(FormatSpec &spec) : spec_(spec) {}
179
Glen Stark72d51e02016-06-08 01:23:32 +0200180 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800181 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
182 operator()(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200183 typedef typename internal::IntTraits<T>::MainType UnsignedType;
184 UnsignedType width = static_cast<UnsignedType>(value);
185 if (internal::is_negative(value)) {
186 spec_.align_ = ALIGN_LEFT;
187 width = 0 - width;
188 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700189 unsigned int_max = std::numeric_limits<int>::max();
190 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700191 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200192 return static_cast<unsigned>(width);
193 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800194
195 template <typename T>
196 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
197 operator()(T value) {
198 FMT_THROW(format_error("width is not integer"));
199 return 0;
200 }
Glen Stark72d51e02016-06-08 01:23:32 +0200201};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700202} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200203
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700204/**
205 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800206 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700207 \endrst
208 */
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800209template <typename Char>
Victor Zverovich28429702016-11-20 09:36:27 -0800210class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200211 private:
212 void write_null_pointer() {
213 this->spec().type_ = 0;
214 this->write("(nil)");
215 }
216
Victor Zverovich28429702016-11-20 09:36:27 -0800217 typedef internal::ArgFormatterBase<Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200218
219 public:
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700220 /**
221 \rst
222 Constructs an argument formatter object.
223 *writer* is a reference to the output writer and *spec* contains format
224 specifier information for standard argument types.
225 \endrst
226 */
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800227 PrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
Victor Zverovich28429702016-11-20 09:36:27 -0800228 : internal::ArgFormatterBase<Char>(writer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200229
Victor Zverovich95a53e12016-11-19 07:39:07 -0800230 using Base::operator();
231
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700232 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800233 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200234 FormatSpec &fmt_spec = this->spec();
235 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800236 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200237 fmt_spec.type_ = 0;
238 this->write(value);
239 }
240
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700241 /** Formats a character. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800242 void operator()(wchar_t value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200243 const FormatSpec &fmt_spec = this->spec();
244 BasicWriter<Char> &w = this->writer();
245 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
246 w.write_int(value, fmt_spec);
247 typedef typename BasicWriter<Char>::CharPtr CharPtr;
248 CharPtr out = CharPtr();
249 if (fmt_spec.width_ > 1) {
250 Char fill = ' ';
251 out = w.grow_buffer(fmt_spec.width_);
252 if (fmt_spec.align_ != ALIGN_LEFT) {
253 std::fill_n(out, fmt_spec.width_ - 1, fill);
254 out += fmt_spec.width_ - 1;
255 } else {
256 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
257 }
258 } else {
259 out = w.grow_buffer(1);
260 }
261 *out = static_cast<Char>(value);
262 }
263
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700264 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800265 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200266 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800267 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200268 else if (this->spec().type_ == 'p')
269 write_null_pointer();
270 else
271 this->write("(null)");
272 }
273
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700274 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800275 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200276 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800277 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200278 this->spec().type_ = 0;
279 write_null_pointer();
280 }
281
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700282 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800283 void operator()(format_arg::CustomValue c) {
Victor Zverovich9998f662016-11-06 16:11:24 -0800284 const Char format_str[] = {'}', '\0'};
285 auto args = basic_format_args<basic_format_context<Char>>();
286 basic_format_context<Char> ctx(format_str, args);
287 c.format(&this->writer(), c.value, &ctx);
Glen Stark72d51e02016-06-08 01:23:32 +0200288 }
289};
290
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700291/** This template formats data and writes the output to a writer. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700292template <typename Char,
293 typename ArgFormatter = PrintfArgFormatter<Char> >
Victor Zverovich9998f662016-11-06 16:11:24 -0800294class printf_context :
295 private internal::format_context_base<
296 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700297 public:
298 /** The character type for the output. */
Victor Zverovichbe613202016-10-22 08:19:19 -0700299 typedef Char char_type;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700300
Glen Stark72d51e02016-06-08 01:23:32 +0200301 private:
Victor Zverovich9998f662016-11-06 16:11:24 -0800302 typedef internal::format_context_base<Char, printf_context> Base;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700303
Glen Stark72d51e02016-06-08 01:23:32 +0200304 void parse_flags(FormatSpec &spec, const Char *&s);
305
306 // Returns the argument with specified index or, if arg_index is equal
307 // to the maximum unsigned value, the next argument.
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800308 format_arg get_arg(
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700309 const Char *s,
Glen Stark72d51e02016-06-08 01:23:32 +0200310 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
311
312 // Parses argument index, flags and width and returns the argument index.
313 unsigned parse_header(const Char *&s, FormatSpec &spec);
314
315 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700316 /**
317 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800318 Constructs a ``printf_context`` object. References to the arguments and
319 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700320 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700321 \endrst
322 */
Victor Zverovich9998f662016-11-06 16:11:24 -0800323 explicit printf_context(BasicCStringRef<Char> format_str,
324 basic_format_args<printf_context> args)
325 : Base(format_str.c_str(), args) {}
326
Victor Zverovich355861f2016-07-20 08:26:14 -0700327 /** Formats stored arguments and writes the output to the writer. */
Victor Zverovich9998f662016-11-06 16:11:24 -0800328 FMT_API void format(BasicWriter<Char> &writer);
Glen Stark72d51e02016-06-08 01:23:32 +0200329};
330
331template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800332void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
Glen Stark72d51e02016-06-08 01:23:32 +0200333 for (;;) {
334 switch (*s++) {
335 case '-':
336 spec.align_ = ALIGN_LEFT;
337 break;
338 case '+':
339 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
340 break;
341 case '0':
342 spec.fill_ = '0';
343 break;
344 case ' ':
345 spec.flags_ |= SIGN_FLAG;
346 break;
347 case '#':
348 spec.flags_ |= HASH_FLAG;
349 break;
350 default:
351 --s;
352 return;
353 }
354 }
355}
356
357template <typename Char, typename AF>
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800358format_arg printf_context<Char, AF>::get_arg(const Char *s,
359 unsigned arg_index) {
Glen Stark72d51e02016-06-08 01:23:32 +0200360 (void)s;
361 const char *error = 0;
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800362 format_arg arg = arg_index == std::numeric_limits<unsigned>::max() ?
Victor Zverovichdafbec72016-10-07 08:37:06 -0700363 this->next_arg(error) : Base::get_arg(arg_index - 1, error);
Glen Stark72d51e02016-06-08 01:23:32 +0200364 if (error)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700365 FMT_THROW(format_error(!*s ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200366 return arg;
367}
368
369template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800370unsigned printf_context<Char, AF>::parse_header(
Glen Stark72d51e02016-06-08 01:23:32 +0200371 const Char *&s, FormatSpec &spec) {
372 unsigned arg_index = std::numeric_limits<unsigned>::max();
373 Char c = *s;
374 if (c >= '0' && c <= '9') {
375 // Parse an argument index (if followed by '$') or a width possibly
376 // preceded with '0' flag(s).
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700377 unsigned value = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200378 if (*s == '$') { // value is an argument index
379 ++s;
380 arg_index = value;
381 } else {
382 if (c == '0')
383 spec.fill_ = '0';
384 if (value != 0) {
385 // Nonzero value means that we parsed width and don't need to
386 // parse it or flags again, so return now.
387 spec.width_ = value;
388 return arg_index;
389 }
390 }
391 }
392 parse_flags(spec, s);
393 // Parse width.
394 if (*s >= '0' && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700395 spec.width_ = internal::parse_nonnegative_int(s);
Glen Stark72d51e02016-06-08 01:23:32 +0200396 } else if (*s == '*') {
397 ++s;
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800398 spec.width_ = visit(internal::WidthHandler(spec), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200399 }
400 return arg_index;
401}
402
403template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800404void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
405 const Char *start = this->ptr();
Glen Stark72d51e02016-06-08 01:23:32 +0200406 const Char *s = start;
407 while (*s) {
408 Char c = *s++;
409 if (c != '%') continue;
410 if (*s == c) {
Victor Zverovich2bba4202016-10-26 17:54:11 -0700411 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200412 start = ++s;
413 continue;
414 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700415 internal::write(writer, start, s - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200416
417 FormatSpec spec;
418 spec.align_ = ALIGN_RIGHT;
419
420 // Parse argument index, flags and width.
421 unsigned arg_index = parse_header(s, spec);
422
423 // Parse precision.
424 if (*s == '.') {
425 ++s;
426 if ('0' <= *s && *s <= '9') {
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700427 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200428 } else if (*s == '*') {
429 ++s;
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800430 spec.precision_ = visit(internal::PrecisionHandler(), get_arg(s));
Glen Stark72d51e02016-06-08 01:23:32 +0200431 }
432 }
433
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800434 format_arg arg = get_arg(s, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800435 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700436 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200437 if (spec.fill_ == '0') {
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800438 if (arg.type <= format_arg::LAST_NUMERIC_TYPE)
Glen Stark72d51e02016-06-08 01:23:32 +0200439 spec.align_ = ALIGN_NUMERIC;
440 else
441 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
442 }
443
444 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800445 using internal::convert_arg;
Glen Stark72d51e02016-06-08 01:23:32 +0200446 switch (*s++) {
447 case 'h':
448 if (*s == 'h')
Victor Zverovich751ff642016-11-19 08:40:24 -0800449 convert_arg<signed char>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200450 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800451 convert_arg<short>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200452 break;
453 case 'l':
454 if (*s == 'l')
Victor Zverovich751ff642016-11-19 08:40:24 -0800455 convert_arg<fmt::LongLong>(arg, *++s);
Glen Stark72d51e02016-06-08 01:23:32 +0200456 else
Victor Zverovich751ff642016-11-19 08:40:24 -0800457 convert_arg<long>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200458 break;
459 case 'j':
Victor Zverovich751ff642016-11-19 08:40:24 -0800460 convert_arg<intmax_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200461 break;
462 case 'z':
Victor Zverovich751ff642016-11-19 08:40:24 -0800463 convert_arg<std::size_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200464 break;
465 case 't':
Victor Zverovich751ff642016-11-19 08:40:24 -0800466 convert_arg<std::ptrdiff_t>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200467 break;
468 case 'L':
469 // printf produces garbage when 'L' is omitted for long double, no
470 // need to do the same.
471 break;
472 default:
473 --s;
Victor Zverovich751ff642016-11-19 08:40:24 -0800474 convert_arg<void>(arg, *s);
Glen Stark72d51e02016-06-08 01:23:32 +0200475 }
476
477 // Parse type.
478 if (!*s)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700479 FMT_THROW(format_error("invalid format string"));
Glen Stark72d51e02016-06-08 01:23:32 +0200480 spec.type_ = static_cast<char>(*s++);
Victor Zverovich9cf6c8f2016-11-26 10:21:31 -0800481 if (arg.type <= format_arg::LAST_INTEGER_TYPE) {
Glen Stark72d51e02016-06-08 01:23:32 +0200482 // Normalize type.
483 switch (spec.type_) {
484 case 'i': case 'u':
485 spec.type_ = 'd';
486 break;
487 case 'c':
488 // TODO: handle wchar_t
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800489 visit(internal::CharConverter(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200490 break;
491 }
492 }
493
494 start = s;
495
496 // Format argument.
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800497 visit(AF(writer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200498 }
Victor Zverovich2bba4202016-10-26 17:54:11 -0700499 internal::write(writer, start, s);
Glen Stark72d51e02016-06-08 01:23:32 +0200500}
Glen Stark72d51e02016-06-08 01:23:32 +0200501
Victor Zverovich18dfa252016-10-21 06:46:21 -0700502// Formats a value.
503template <typename Char, typename T>
Victor Zverovichb656a1c2016-10-25 06:19:19 -0700504void format_value(BasicWriter<Char> &w, const T &value,
Victor Zverovich9998f662016-11-06 16:11:24 -0800505 printf_context<Char>& ctx) {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700506 internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
Victor Zverovich2bba4202016-10-26 17:54:11 -0700507 w << internal::format_value(buffer, value);
Victor Zverovich18dfa252016-10-21 06:46:21 -0700508}
509
Glen Stark72d51e02016-06-08 01:23:32 +0200510template <typename Char>
Victor Zverovich0028ce52016-08-26 17:23:13 -0700511void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800512 basic_format_args<printf_context<Char>> args) {
513 printf_context<Char>(format, args).format(w);
Glen Stark72d51e02016-06-08 01:23:32 +0200514}
515
Victor Zverovichdafbec72016-10-07 08:37:06 -0700516inline std::string vsprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800517 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700518 MemoryWriter w;
519 printf(w, format, args);
520 return w.str();
521}
522
Glen Stark72d51e02016-06-08 01:23:32 +0200523/**
524 \rst
525 Formats arguments and returns the result as a string.
526
527 **Example**::
528
529 std::string message = fmt::sprintf("The answer is %d", 42);
530 \endrst
531*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700532template <typename... Args>
533inline std::string sprintf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800534 return vsprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200535}
Glen Stark72d51e02016-06-08 01:23:32 +0200536
Victor Zverovichdafbec72016-10-07 08:37:06 -0700537inline std::wstring vsprintf(WCStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800538 basic_format_args<printf_context<wchar_t>> args) {
Glen Stark72d51e02016-06-08 01:23:32 +0200539 WMemoryWriter w;
540 printf(w, format, args);
541 return w.str();
542}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700543
544template <typename... Args>
545inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800546 auto vargs = make_xformat_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700547 return vsprintf(format_str, vargs);
548}
549
Victor Zverovichdafbec72016-10-07 08:37:06 -0700550FMT_API int vfprintf(std::FILE *f, CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800551 basic_format_args<printf_context<char>> args);
Glen Stark72d51e02016-06-08 01:23:32 +0200552
553/**
554 \rst
555 Prints formatted data to the file *f*.
556
557 **Example**::
558
559 fmt::fprintf(stderr, "Don't %s!", "panic");
560 \endrst
561 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700562template <typename... Args>
563inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800564 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700565 return vfprintf(f, format_str, vargs);
566}
567
Victor Zverovichdafbec72016-10-07 08:37:06 -0700568inline int vprintf(CStringRef format,
Victor Zverovich9998f662016-11-06 16:11:24 -0800569 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700570 return vfprintf(stdout, format, args);
571}
Glen Stark72d51e02016-06-08 01:23:32 +0200572
573/**
574 \rst
575 Prints formatted data to ``stdout``.
576
577 **Example**::
578
579 fmt::printf("Elapsed time: %.2f seconds", 1.23);
580 \endrst
581 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700582template <typename... Args>
583inline int printf(CStringRef format_str, const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800584 return vprintf(format_str, make_xformat_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200585}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700586
Victor Zverovichdafbec72016-10-07 08:37:06 -0700587inline int vfprintf(std::ostream &os, CStringRef format_str,
Victor Zverovich9998f662016-11-06 16:11:24 -0800588 basic_format_args<printf_context<char>> args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700589 MemoryWriter w;
590 printf(w, format_str, args);
591 internal::write(os, w);
592 return static_cast<int>(w.size());
593}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700594
595/**
596 \rst
597 Prints formatted data to the stream *os*.
598
599 **Example**::
600
601 fprintf(cerr, "Don't %s!", "panic");
602 \endrst
603 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700604template <typename... Args>
605inline int fprintf(std::ostream &os, CStringRef format_str,
606 const Args & ... args) {
Victor Zverovich85793a12016-11-06 19:27:14 -0800607 auto vargs = make_xformat_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700608 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700609}
Glen Stark72d51e02016-06-08 01:23:32 +0200610} // namespace fmt
611
612#endif // FMT_PRINTF_H_