blob: a8533ef2d89ccb0e61cb3236f372f3e2c010cf8c [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 Zverovich0854f8c2016-12-11 13:22:45 -080043class PrintfPrecisionHandler {
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
Victor Zverovich77c892c2017-08-27 08:16:46 -070070 operator()(T) { return false; }
Glen Stark72d51e02016-06-08 01:23:32 +020071};
72
Victor Zverovich0fbd8462017-09-04 11:41:15 -070073template <typename T>
74struct make_unsigned_or_bool : std::make_unsigned<T> {};
75
76template <>
77struct make_unsigned_or_bool<bool> {
78 using type = bool;
79};
80
Victor Zverovichd705d512016-12-29 09:07:39 -080081template <typename T, typename Context>
Victor Zverovich751ff642016-11-19 08:40:24 -080082class ArgConverter {
Glen Stark72d51e02016-06-08 01:23:32 +020083 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080084 typedef typename Context::char_type Char;
85
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080086 basic_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080087 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020088
Glen Stark72d51e02016-06-08 01:23:32 +020089 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080090 ArgConverter(basic_arg<Context> &arg, Char 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';
Victor Zverovichc18a4042017-09-04 13:56:14 -0700102 typedef typename std::conditional<
Victor Zverovichf194a412017-08-27 09:16:50 -0700103 std::is_same<T, void>::value, U, T>::type TargetType;
Glen Stark72d51e02016-06-08 01:23:32 +0200104 if (sizeof(TargetType) <= sizeof(int)) {
105 // Extra casts are used to silence warnings.
106 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800107 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800108 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200109 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700110 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800111 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800112 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200113 }
114 } else {
115 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200116 // glibc's printf doesn't sign extend arguments of smaller types:
117 // std::printf("%lld", -42); // prints "4294967254"
118 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700119 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200120 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800121 arg_ = internal::make_arg<Context>(
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700122 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200123 }
124 }
125 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800126
127 template <typename U>
Victor Zverovich77c892c2017-08-27 08:16:46 -0700128 typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800129 // No coversion needed for non-integral types.
130 }
Glen Stark72d51e02016-06-08 01:23:32 +0200131};
132
Victor Zverovich751ff642016-11-19 08:40:24 -0800133// Converts an integer argument to T for printf, if T is an integral type.
134// If T is void, the argument is converted to corresponding signed or unsigned
135// type depending on the type specifier: 'd' and 'i' - signed, other -
136// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800137template <typename T, typename Context, typename Char>
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800138void convert_arg(basic_arg<Context> &arg, Char type) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800139 visit(ArgConverter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800140}
141
Glen Stark72d51e02016-06-08 01:23:32 +0200142// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800143template <typename Context>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800144class CharConverter {
Glen Stark72d51e02016-06-08 01:23:32 +0200145 private:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800146 basic_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200147
148 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
149
150 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800151 explicit CharConverter(basic_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200152
153 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800154 typename std::enable_if<std::is_integral<T>::value>::type
155 operator()(T value) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800156 arg_ = internal::make_arg<Context>(static_cast<char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200157 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800158
159 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800160 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800161 // No coversion needed for non-integral types.
162 }
Glen Stark72d51e02016-06-08 01:23:32 +0200163};
164
165// Checks if an argument is a valid printf width specifier and sets
166// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000167template <typename Char>
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800168class PrintfWidthHandler {
Glen Stark72d51e02016-06-08 01:23:32 +0200169 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000170 typedef basic_format_specs<Char> format_specs;
171
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000172 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200173
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800174 FMT_DISALLOW_COPY_AND_ASSIGN(PrintfWidthHandler);
Glen Stark72d51e02016-06-08 01:23:32 +0200175
176 public:
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000177 explicit PrintfWidthHandler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200178
Glen Stark72d51e02016-06-08 01:23:32 +0200179 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800180 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
181 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800182 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200183 UnsignedType width = static_cast<UnsignedType>(value);
184 if (internal::is_negative(value)) {
185 spec_.align_ = ALIGN_LEFT;
186 width = 0 - width;
187 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700188 unsigned int_max = std::numeric_limits<int>::max();
189 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700190 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200191 return static_cast<unsigned>(width);
192 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800193
194 template <typename T>
195 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -0700196 operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800197 FMT_THROW(format_error("width is not integer"));
198 return 0;
199 }
Glen Stark72d51e02016-06-08 01:23:32 +0200200};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700201} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200202
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700203/**
204 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800205 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700206 \endrst
207 */
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800208template <typename Char>
Victor Zverovichc333dca2017-02-19 08:41:38 -0800209class printf_arg_formatter : public internal::arg_formatter_base<Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200210 private:
211 void write_null_pointer() {
212 this->spec().type_ = 0;
213 this->write("(nil)");
214 }
215
Victor Zverovichc333dca2017-02-19 08:41:38 -0800216 typedef internal::arg_formatter_base<Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200217
218 public:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000219 typedef typename Base::format_specs format_specs;
220
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700221 /**
222 \rst
223 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500224 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700225 specifier information for standard argument types.
226 \endrst
227 */
Victor Zverovich4ec88602017-02-18 07:46:32 -0800228 printf_arg_formatter(basic_buffer<Char> &buffer, format_specs &spec)
Victor Zverovichc333dca2017-02-19 08:41:38 -0800229 : internal::arg_formatter_base<Char>(buffer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200230
Victor Zverovich95a53e12016-11-19 07:39:07 -0800231 using Base::operator();
232
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700233 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800234 void operator()(bool value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000235 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200236 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800237 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200238 fmt_spec.type_ = 0;
239 this->write(value);
240 }
241
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700242 /** Formats a character. */
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800243 void operator()(Char value) {
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700244 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200245 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700246 return (*this)(static_cast<int>(value));
247 fmt_spec.flags_ = 0;
248 fmt_spec.align_ = ALIGN_RIGHT;
249 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200250 }
251
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700252 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800253 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200254 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800255 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200256 else if (this->spec().type_ == 'p')
257 write_null_pointer();
258 else
259 this->write("(null)");
260 }
261
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700262 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800263 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200264 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800265 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200266 this->spec().type_ = 0;
267 write_null_pointer();
268 }
269
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700270 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800271 void operator()(internal::custom_value<Char> c) {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700272 const Char format_str[] = {'}', '\0'};
Victor Zverovich624c5862017-02-05 06:41:39 -0800273 auto args = basic_args<basic_context<Char>>();
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700274 basic_context<Char> ctx(basic_string_view<Char>(format_str), args);
275 c.format(this->writer().buffer(), c.value, &ctx);
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700276 }
277};
278
279template <typename Char,
280 typename ArgFormatter = printf_arg_formatter<Char> >
281class printf_context;
282
283template <typename T, typename Char = char>
284struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700285 template <typename ParseContext>
286 const Char *parse(ParseContext& ctx) { return ctx.begin(); }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700287
288 void format(basic_buffer<Char> &buf, const T &value, printf_context<Char> &) {
289 internal::format_value(buf, value);
Glen Stark72d51e02016-06-08 01:23:32 +0200290 }
291};
292
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700293/** This template formats data and writes the output to a writer. */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700294template <typename Char, typename ArgFormatter>
Victor Zverovich9998f662016-11-06 16:11:24 -0800295class printf_context :
Victor Zverovich624c5862017-02-05 06:41:39 -0800296 private internal::context_base<
Victor Zverovich9998f662016-11-06 16:11:24 -0800297 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700298 public:
299 /** The character type for the output. */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700300 using char_type = Char;
301
302 template <typename T>
303 using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700304
Glen Stark72d51e02016-06-08 01:23:32 +0200305 private:
Victor Zverovich624c5862017-02-05 06:41:39 -0800306 typedef internal::context_base<Char, printf_context> Base;
Victor Zverovichd705d512016-12-29 09:07:39 -0800307 typedef typename Base::format_arg format_arg;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000308 typedef basic_format_specs<Char> format_specs;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700309 typedef internal::null_terminating_iterator<Char> iterator;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700310
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700311 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200312
313 // Returns the argument with specified index or, if arg_index is equal
314 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800315 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700316 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200317 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
318
319 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700320 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200321
322 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700323 /**
324 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800325 Constructs a ``printf_context`` object. References to the arguments and
326 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700327 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700328 \endrst
329 */
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700330 printf_context(
331 basic_string_view<Char> format_str, basic_args<printf_context> args)
332 : Base(format_str, args) {}
333
334 using Base::get_parse_context;
Victor Zverovich9998f662016-11-06 16:11:24 -0800335
Victor Zverovichfefaf072017-02-14 16:29:47 -0500336 /** Formats stored arguments and writes the output to the buffer. */
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700337 FMT_API void format(basic_buffer<Char> &buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200338};
339
340template <typename Char, typename AF>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700341void printf_context<Char, AF>::parse_flags(format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200342 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700343 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200344 case '-':
345 spec.align_ = ALIGN_LEFT;
346 break;
347 case '+':
348 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
349 break;
350 case '0':
351 spec.fill_ = '0';
352 break;
353 case ' ':
354 spec.flags_ |= SIGN_FLAG;
355 break;
356 case '#':
357 spec.flags_ |= HASH_FLAG;
358 break;
359 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700360 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200361 return;
362 }
363 }
364}
365
366template <typename Char, typename AF>
Victor Zverovichd705d512016-12-29 09:07:39 -0800367typename printf_context<Char, AF>::format_arg printf_context<Char, AF>::get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700368 iterator it, unsigned arg_index) {
369 (void)it;
Glen Stark72d51e02016-06-08 01:23:32 +0200370 const char *error = 0;
Victor Zverovich1102d462017-07-30 08:37:26 -0700371 format_arg arg;
372 if (arg_index == std::numeric_limits<unsigned>::max()) {
373 arg_index = this->next_arg_index(error);
374 if (!error)
375 arg = this->do_get_arg(arg_index, error);
376 } else {
377 arg = Base::get_arg(arg_index - 1, error);
378 }
Glen Stark72d51e02016-06-08 01:23:32 +0200379 if (error)
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700380 FMT_THROW(format_error(!*it ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200381 return arg;
382}
383
384template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800385unsigned printf_context<Char, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700386 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200387 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700388 Char c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200389 if (c >= '0' && c <= '9') {
390 // Parse an argument index (if followed by '$') or a width possibly
391 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700392 internal::error_handler eh;
393 unsigned value = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700394 if (*it == '$') { // value is an argument index
395 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200396 arg_index = value;
397 } else {
398 if (c == '0')
399 spec.fill_ = '0';
400 if (value != 0) {
401 // Nonzero value means that we parsed width and don't need to
402 // parse it or flags again, so return now.
403 spec.width_ = value;
404 return arg_index;
405 }
406 }
407 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700408 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200409 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700410 if (*it >= '0' && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700411 internal::error_handler eh;
412 spec.width_ = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700413 } else if (*it == '*') {
414 ++it;
415 spec.width_ = visit(internal::PrintfWidthHandler<Char>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200416 }
417 return arg_index;
418}
419
420template <typename Char, typename AF>
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700421void printf_context<Char, AF>::format(basic_buffer<Char> &buffer) {
422 Base &base = *this;
423 auto start = iterator(base);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700424 auto it = start;
425 using internal::pointer_from;
426 while (*it) {
427 Char c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200428 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700429 if (*it == c) {
430 buffer.append(pointer_from(start), pointer_from(it));
431 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200432 continue;
433 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700434 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200435
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000436 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200437 spec.align_ = ALIGN_RIGHT;
438
439 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700440 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200441
442 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700443 if (*it == '.') {
444 ++it;
445 if ('0' <= *it && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700446 internal::error_handler eh;
447 spec.precision_ = static_cast<int>(parse_nonnegative_int(it, eh));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700448 } else if (*it == '*') {
449 ++it;
450 spec.precision_ =
451 visit(internal::PrintfPrecisionHandler(), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200452 }
453 }
454
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700455 format_arg arg = get_arg(it, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800456 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700457 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200458 if (spec.fill_ == '0') {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800459 if (arg.is_numeric())
Glen Stark72d51e02016-06-08 01:23:32 +0200460 spec.align_ = ALIGN_NUMERIC;
461 else
462 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
463 }
464
465 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800466 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700467 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200468 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700469 if (*it == 'h')
470 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200471 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700472 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200473 break;
474 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700475 if (*it == 'l')
Victor Zverovich016aceb2017-08-26 09:09:43 -0700476 convert_arg<long long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200477 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700478 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200479 break;
480 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700481 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200482 break;
483 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700484 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200485 break;
486 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700487 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200488 break;
489 case 'L':
490 // printf produces garbage when 'L' is omitted for long double, no
491 // need to do the same.
492 break;
493 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700494 --it;
495 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200496 }
497
498 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700499 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700500 FMT_THROW(format_error("invalid format string"));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700501 spec.type_ = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800502 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200503 // Normalize type.
504 switch (spec.type_) {
505 case 'i': case 'u':
506 spec.type_ = 'd';
507 break;
508 case 'c':
509 // TODO: handle wchar_t
Victor Zverovichd705d512016-12-29 09:07:39 -0800510 visit(internal::CharConverter<printf_context<Char, AF>>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200511 break;
512 }
513 }
514
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700515 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200516
517 // Format argument.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500518 visit(AF(buffer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200519 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700520 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200521}
Glen Stark72d51e02016-06-08 01:23:32 +0200522
523template <typename Char>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700524void printf(basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800525 basic_args<printf_context<Char>> args) {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700526 printf_context<Char>(format, args).format(buf);
Glen Stark72d51e02016-06-08 01:23:32 +0200527}
528
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800529typedef basic_args<printf_context<char>> printf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800530
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700531inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800532 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500533 printf(buffer, format, args);
534 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700535}
536
Glen Stark72d51e02016-06-08 01:23:32 +0200537/**
538 \rst
539 Formats arguments and returns the result as a string.
540
541 **Example**::
542
543 std::string message = fmt::sprintf("The answer is %d", 42);
544 \endrst
545*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700546template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700547inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800548 return vsprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200549}
Glen Stark72d51e02016-06-08 01:23:32 +0200550
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800551inline std::wstring vsprintf(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700552 wstring_view format, basic_args<printf_context<wchar_t>> args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800553 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500554 printf(buffer, format, args);
555 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200556}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700557
558template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700559inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800560 auto vargs = make_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700561 return vsprintf(format_str, vargs);
562}
563
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700564FMT_API int vfprintf(std::FILE *f, string_view format, printf_args args);
Glen Stark72d51e02016-06-08 01:23:32 +0200565
566/**
567 \rst
568 Prints formatted data to the file *f*.
569
570 **Example**::
571
572 fmt::fprintf(stderr, "Don't %s!", "panic");
573 \endrst
574 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700575template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700576inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800577 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700578 return vfprintf(f, format_str, vargs);
579}
580
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700581inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700582 return vfprintf(stdout, format, args);
583}
Glen Stark72d51e02016-06-08 01:23:32 +0200584
585/**
586 \rst
587 Prints formatted data to ``stdout``.
588
589 **Example**::
590
591 fmt::printf("Elapsed time: %.2f seconds", 1.23);
592 \endrst
593 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700594template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700595inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800596 return vprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200597}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700598
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700599inline int vfprintf(std::ostream &os, string_view format_str, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800600 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500601 printf(buffer, format_str, args);
602 internal::write(os, buffer);
603 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700604}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700605
606/**
607 \rst
608 Prints formatted data to the stream *os*.
609
610 **Example**::
611
612 fprintf(cerr, "Don't %s!", "panic");
613 \endrst
614 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700615template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700616inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700617 const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800618 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700619 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700620}
Glen Stark72d51e02016-06-08 01:23:32 +0200621} // namespace fmt
622
623#endif // FMT_PRINTF_H_