blob: 1c58df2b6a08d3f5b14fbd2f522b0956e0da7acc [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 Zverovichd705d512016-12-29 09:07:39 -080073template <typename T, typename Context>
Victor Zverovich751ff642016-11-19 08:40:24 -080074class ArgConverter {
Glen Stark72d51e02016-06-08 01:23:32 +020075 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080076 typedef typename Context::char_type Char;
77
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080078 basic_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080079 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020080
Glen Stark72d51e02016-06-08 01:23:32 +020081 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080082 ArgConverter(basic_arg<Context> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +020083 : arg_(arg), type_(type) {}
84
Victor Zverovich751ff642016-11-19 08:40:24 -080085 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020086 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080087 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020088 }
89
90 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -080091 typename std::enable_if<std::is_integral<U>::value>::type
92 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020093 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovich6a2ff282017-02-19 06:46:51 -080094 typedef typename internal::conditional<
Victor Zverovichf194a412017-08-27 09:16:50 -070095 std::is_same<T, void>::value, U, T>::type TargetType;
Glen Stark72d51e02016-06-08 01:23:32 +020096 if (sizeof(TargetType) <= sizeof(int)) {
97 // Extra casts are used to silence warnings.
98 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -080099 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800100 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200101 } else {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800102 typedef typename internal::make_unsigned<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800103 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800104 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200105 }
106 } else {
107 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200108 // glibc's printf doesn't sign extend arguments of smaller types:
109 // std::printf("%lld", -42); // prints "4294967254"
110 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700111 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200112 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800113 arg_ = internal::make_arg<Context>(
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800114 static_cast<typename internal::make_unsigned<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200115 }
116 }
117 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800118
119 template <typename U>
Victor Zverovich77c892c2017-08-27 08:16:46 -0700120 typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800121 // No coversion needed for non-integral types.
122 }
Glen Stark72d51e02016-06-08 01:23:32 +0200123};
124
Victor Zverovich751ff642016-11-19 08:40:24 -0800125// Converts an integer argument to T for printf, if T is an integral type.
126// If T is void, the argument is converted to corresponding signed or unsigned
127// type depending on the type specifier: 'd' and 'i' - signed, other -
128// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800129template <typename T, typename Context, typename Char>
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800130void convert_arg(basic_arg<Context> &arg, Char type) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800131 visit(ArgConverter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800132}
133
Glen Stark72d51e02016-06-08 01:23:32 +0200134// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800135template <typename Context>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800136class CharConverter {
Glen Stark72d51e02016-06-08 01:23:32 +0200137 private:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800138 basic_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200139
140 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
141
142 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800143 explicit CharConverter(basic_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200144
145 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800146 typename std::enable_if<std::is_integral<T>::value>::type
147 operator()(T value) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800148 arg_ = internal::make_arg<Context>(static_cast<char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200149 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800150
151 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800152 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800153 // No coversion needed for non-integral types.
154 }
Glen Stark72d51e02016-06-08 01:23:32 +0200155};
156
157// Checks if an argument is a valid printf width specifier and sets
158// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000159template <typename Char>
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800160class PrintfWidthHandler {
Glen Stark72d51e02016-06-08 01:23:32 +0200161 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000162 typedef basic_format_specs<Char> format_specs;
163
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000164 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200165
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800166 FMT_DISALLOW_COPY_AND_ASSIGN(PrintfWidthHandler);
Glen Stark72d51e02016-06-08 01:23:32 +0200167
168 public:
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000169 explicit PrintfWidthHandler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200170
Glen Stark72d51e02016-06-08 01:23:32 +0200171 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800172 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
173 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800174 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200175 UnsignedType width = static_cast<UnsignedType>(value);
176 if (internal::is_negative(value)) {
177 spec_.align_ = ALIGN_LEFT;
178 width = 0 - width;
179 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700180 unsigned int_max = std::numeric_limits<int>::max();
181 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700182 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200183 return static_cast<unsigned>(width);
184 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800185
186 template <typename T>
187 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -0700188 operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800189 FMT_THROW(format_error("width is not integer"));
190 return 0;
191 }
Glen Stark72d51e02016-06-08 01:23:32 +0200192};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700193} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200194
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700195/**
196 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800197 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700198 \endrst
199 */
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800200template <typename Char>
Victor Zverovichc333dca2017-02-19 08:41:38 -0800201class printf_arg_formatter : public internal::arg_formatter_base<Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200202 private:
203 void write_null_pointer() {
204 this->spec().type_ = 0;
205 this->write("(nil)");
206 }
207
Victor Zverovichc333dca2017-02-19 08:41:38 -0800208 typedef internal::arg_formatter_base<Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200209
210 public:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000211 typedef typename Base::format_specs format_specs;
212
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700213 /**
214 \rst
215 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500216 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700217 specifier information for standard argument types.
218 \endrst
219 */
Victor Zverovich4ec88602017-02-18 07:46:32 -0800220 printf_arg_formatter(basic_buffer<Char> &buffer, format_specs &spec)
Victor Zverovichc333dca2017-02-19 08:41:38 -0800221 : internal::arg_formatter_base<Char>(buffer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200222
Victor Zverovich95a53e12016-11-19 07:39:07 -0800223 using Base::operator();
224
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700225 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800226 void operator()(bool value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000227 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200228 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800229 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200230 fmt_spec.type_ = 0;
231 this->write(value);
232 }
233
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700234 /** Formats a character. */
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800235 void operator()(Char value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000236 const format_specs &fmt_spec = this->spec();
Victor Zverovich84286212016-12-30 12:11:27 -0800237 basic_writer<Char> &w = this->writer();
Glen Stark72d51e02016-06-08 01:23:32 +0200238 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
239 w.write_int(value, fmt_spec);
Victor Zverovich84286212016-12-30 12:11:27 -0800240 typedef typename basic_writer<Char>::CharPtr CharPtr;
Glen Stark72d51e02016-06-08 01:23:32 +0200241 CharPtr out = CharPtr();
242 if (fmt_spec.width_ > 1) {
243 Char fill = ' ';
244 out = w.grow_buffer(fmt_spec.width_);
245 if (fmt_spec.align_ != ALIGN_LEFT) {
246 std::fill_n(out, fmt_spec.width_ - 1, fill);
247 out += fmt_spec.width_ - 1;
248 } else {
249 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
250 }
251 } else {
252 out = w.grow_buffer(1);
253 }
254 *out = static_cast<Char>(value);
255 }
256
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700257 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800258 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200259 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800260 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200261 else if (this->spec().type_ == 'p')
262 write_null_pointer();
263 else
264 this->write("(null)");
265 }
266
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700267 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800268 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200269 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800270 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200271 this->spec().type_ = 0;
272 write_null_pointer();
273 }
274
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700275 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800276 void operator()(internal::custom_value<Char> c) {
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700277 const Char format_str_data[] = {'}', '\0'};
278 basic_string_view<Char> format_str = format_str_data;
Victor Zverovich624c5862017-02-05 06:41:39 -0800279 auto args = basic_args<basic_context<Char>>();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700280 basic_context<Char> ctx(args);
281 c.format(this->writer().buffer(), c.value, format_str, &ctx);
282 }
283};
284
285template <typename Char,
286 typename ArgFormatter = printf_arg_formatter<Char> >
287class printf_context;
288
289template <typename T, typename Char = char>
290struct printf_formatter {
291 const Char *parse(basic_string_view<Char> s) {
292 return s.data();
293 }
294
295 void format(basic_buffer<Char> &buf, const T &value, printf_context<Char> &) {
296 internal::format_value(buf, value);
Glen Stark72d51e02016-06-08 01:23:32 +0200297 }
298};
299
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700300/** This template formats data and writes the output to a writer. */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700301template <typename Char, typename ArgFormatter>
Victor Zverovich9998f662016-11-06 16:11:24 -0800302class printf_context :
Victor Zverovich624c5862017-02-05 06:41:39 -0800303 private internal::context_base<
Victor Zverovich9998f662016-11-06 16:11:24 -0800304 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700305 public:
306 /** The character type for the output. */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700307 using char_type = Char;
308
309 template <typename T>
310 using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700311
Glen Stark72d51e02016-06-08 01:23:32 +0200312 private:
Victor Zverovich624c5862017-02-05 06:41:39 -0800313 typedef internal::context_base<Char, printf_context> Base;
Victor Zverovichd705d512016-12-29 09:07:39 -0800314 typedef typename Base::format_arg format_arg;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000315 typedef basic_format_specs<Char> format_specs;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700316 typedef internal::null_terminating_iterator<Char> iterator;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700317
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700318 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200319
320 // Returns the argument with specified index or, if arg_index is equal
321 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800322 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700323 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200324 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
325
326 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700327 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200328
329 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700330 /**
331 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800332 Constructs a ``printf_context`` object. References to the arguments and
333 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700334 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700335 \endrst
336 */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700337 explicit printf_context(basic_args<printf_context> args): Base(args) {}
Victor Zverovich9998f662016-11-06 16:11:24 -0800338
Victor Zverovichfefaf072017-02-14 16:29:47 -0500339 /** Formats stored arguments and writes the output to the buffer. */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700340 FMT_API void format(
341 basic_string_view<Char> format_str, basic_buffer<Char> &buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200342};
343
344template <typename Char, typename AF>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700345void printf_context<Char, AF>::parse_flags(format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200346 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700347 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200348 case '-':
349 spec.align_ = ALIGN_LEFT;
350 break;
351 case '+':
352 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
353 break;
354 case '0':
355 spec.fill_ = '0';
356 break;
357 case ' ':
358 spec.flags_ |= SIGN_FLAG;
359 break;
360 case '#':
361 spec.flags_ |= HASH_FLAG;
362 break;
363 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700364 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200365 return;
366 }
367 }
368}
369
370template <typename Char, typename AF>
Victor Zverovichd705d512016-12-29 09:07:39 -0800371typename printf_context<Char, AF>::format_arg printf_context<Char, AF>::get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700372 iterator it, unsigned arg_index) {
373 (void)it;
Glen Stark72d51e02016-06-08 01:23:32 +0200374 const char *error = 0;
Victor Zverovich1102d462017-07-30 08:37:26 -0700375 format_arg arg;
376 if (arg_index == std::numeric_limits<unsigned>::max()) {
377 arg_index = this->next_arg_index(error);
378 if (!error)
379 arg = this->do_get_arg(arg_index, error);
380 } else {
381 arg = Base::get_arg(arg_index - 1, error);
382 }
Glen Stark72d51e02016-06-08 01:23:32 +0200383 if (error)
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700384 FMT_THROW(format_error(!*it ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200385 return arg;
386}
387
388template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800389unsigned printf_context<Char, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700390 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200391 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700392 Char c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200393 if (c >= '0' && c <= '9') {
394 // Parse an argument index (if followed by '$') or a width possibly
395 // preceded with '0' flag(s).
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700396 unsigned value = internal::parse_nonnegative_int(it);
397 if (*it == '$') { // value is an argument index
398 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200399 arg_index = value;
400 } else {
401 if (c == '0')
402 spec.fill_ = '0';
403 if (value != 0) {
404 // Nonzero value means that we parsed width and don't need to
405 // parse it or flags again, so return now.
406 spec.width_ = value;
407 return arg_index;
408 }
409 }
410 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700411 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200412 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700413 if (*it >= '0' && *it <= '9') {
414 spec.width_ = internal::parse_nonnegative_int(it);
415 } else if (*it == '*') {
416 ++it;
417 spec.width_ = visit(internal::PrintfWidthHandler<Char>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200418 }
419 return arg_index;
420}
421
422template <typename Char, typename AF>
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700423void printf_context<Char, AF>::format(
424 basic_string_view<Char> format_str, basic_buffer<Char> &buffer) {
425 auto start = iterator(format_str);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700426 auto it = start;
427 using internal::pointer_from;
428 while (*it) {
429 Char c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200430 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700431 if (*it == c) {
432 buffer.append(pointer_from(start), pointer_from(it));
433 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200434 continue;
435 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700436 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200437
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000438 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200439 spec.align_ = ALIGN_RIGHT;
440
441 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700442 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200443
444 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700445 if (*it == '.') {
446 ++it;
447 if ('0' <= *it && *it <= '9') {
448 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(it));
449 } else if (*it == '*') {
450 ++it;
451 spec.precision_ =
452 visit(internal::PrintfPrecisionHandler(), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200453 }
454 }
455
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700456 format_arg arg = get_arg(it, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800457 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700458 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200459 if (spec.fill_ == '0') {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800460 if (arg.is_numeric())
Glen Stark72d51e02016-06-08 01:23:32 +0200461 spec.align_ = ALIGN_NUMERIC;
462 else
463 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
464 }
465
466 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800467 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700468 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200469 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700470 if (*it == 'h')
471 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200472 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700473 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200474 break;
475 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700476 if (*it == 'l')
Victor Zverovich016aceb2017-08-26 09:09:43 -0700477 convert_arg<long long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200478 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700479 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200480 break;
481 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700482 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200483 break;
484 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700485 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200486 break;
487 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700488 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200489 break;
490 case 'L':
491 // printf produces garbage when 'L' is omitted for long double, no
492 // need to do the same.
493 break;
494 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700495 --it;
496 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200497 }
498
499 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700500 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700501 FMT_THROW(format_error("invalid format string"));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700502 spec.type_ = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800503 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200504 // Normalize type.
505 switch (spec.type_) {
506 case 'i': case 'u':
507 spec.type_ = 'd';
508 break;
509 case 'c':
510 // TODO: handle wchar_t
Victor Zverovichd705d512016-12-29 09:07:39 -0800511 visit(internal::CharConverter<printf_context<Char, AF>>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200512 break;
513 }
514 }
515
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700516 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200517
518 // Format argument.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500519 visit(AF(buffer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200520 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700521 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200522}
Glen Stark72d51e02016-06-08 01:23:32 +0200523
524template <typename Char>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700525void printf(basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800526 basic_args<printf_context<Char>> args) {
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700527 printf_context<Char>(args).format(format, buf);
Glen Stark72d51e02016-06-08 01:23:32 +0200528}
529
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800530typedef basic_args<printf_context<char>> printf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800531
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700532inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800533 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500534 printf(buffer, format, args);
535 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700536}
537
Glen Stark72d51e02016-06-08 01:23:32 +0200538/**
539 \rst
540 Formats arguments and returns the result as a string.
541
542 **Example**::
543
544 std::string message = fmt::sprintf("The answer is %d", 42);
545 \endrst
546*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700547template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700548inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800549 return vsprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200550}
Glen Stark72d51e02016-06-08 01:23:32 +0200551
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800552inline std::wstring vsprintf(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700553 wstring_view format, basic_args<printf_context<wchar_t>> args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800554 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500555 printf(buffer, format, args);
556 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200557}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700558
559template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700560inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800561 auto vargs = make_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700562 return vsprintf(format_str, vargs);
563}
564
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700565FMT_API int vfprintf(std::FILE *f, string_view format, printf_args args);
Glen Stark72d51e02016-06-08 01:23:32 +0200566
567/**
568 \rst
569 Prints formatted data to the file *f*.
570
571 **Example**::
572
573 fmt::fprintf(stderr, "Don't %s!", "panic");
574 \endrst
575 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700576template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700577inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800578 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700579 return vfprintf(f, format_str, vargs);
580}
581
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700582inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700583 return vfprintf(stdout, format, args);
584}
Glen Stark72d51e02016-06-08 01:23:32 +0200585
586/**
587 \rst
588 Prints formatted data to ``stdout``.
589
590 **Example**::
591
592 fmt::printf("Elapsed time: %.2f seconds", 1.23);
593 \endrst
594 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700595template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700596inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800597 return vprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200598}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700599
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700600inline int vfprintf(std::ostream &os, string_view format_str, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800601 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500602 printf(buffer, format_str, args);
603 internal::write(os, buffer);
604 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700605}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700606
607/**
608 \rst
609 Prints formatted data to the stream *os*.
610
611 **Example**::
612
613 fprintf(cerr, "Don't %s!", "panic");
614 \endrst
615 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700616template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700617inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700618 const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800619 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700620 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700621}
Glen Stark72d51e02016-06-08 01:23:32 +0200622} // namespace fmt
623
624#endif // FMT_PRINTF_H_