blob: 272da034a7a0e154685a931e820821ac91221409 [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
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 Zverovichd705d512016-12-29 09:07:39 -080083template <typename T, typename Context>
Victor Zverovich751ff642016-11-19 08:40:24 -080084class ArgConverter {
Glen Stark72d51e02016-06-08 01:23:32 +020085 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080086 typedef typename Context::char_type Char;
87
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080088 basic_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080089 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020090
Glen Stark72d51e02016-06-08 01:23:32 +020091 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080092 ArgConverter(basic_arg<Context> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +020093 : arg_(arg), type_(type) {}
94
Victor Zverovich751ff642016-11-19 08:40:24 -080095 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020096 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080097 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020098 }
99
100 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -0800101 typename std::enable_if<std::is_integral<U>::value>::type
102 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200103 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800104 typedef typename internal::conditional<
Glen Stark72d51e02016-06-08 01:23:32 +0200105 is_same<T, void>::value, U, T>::type TargetType;
Victor Zverovich624c5862017-02-05 06:41:39 -0800106 typedef basic_context<Char> context;
Glen Stark72d51e02016-06-08 01:23:32 +0200107 if (sizeof(TargetType) <= sizeof(int)) {
108 // Extra casts are used to silence warnings.
109 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800110 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800111 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200112 } else {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800113 typedef typename internal::make_unsigned<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800114 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800115 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200116 }
117 } else {
118 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200119 // glibc's printf doesn't sign extend arguments of smaller types:
120 // std::printf("%lld", -42); // prints "4294967254"
121 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700122 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200123 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800124 arg_ = internal::make_arg<Context>(
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800125 static_cast<typename internal::make_unsigned<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200126 }
127 }
128 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800129
130 template <typename U>
131 typename std::enable_if<!std::is_integral<U>::value>::type
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800132 operator()(U value) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800133 // No coversion needed for non-integral types.
134 }
Glen Stark72d51e02016-06-08 01:23:32 +0200135};
136
Victor Zverovich751ff642016-11-19 08:40:24 -0800137// Converts an integer argument to T for printf, if T is an integral type.
138// If T is void, the argument is converted to corresponding signed or unsigned
139// type depending on the type specifier: 'd' and 'i' - signed, other -
140// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800141template <typename T, typename Context, typename Char>
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800142void convert_arg(basic_arg<Context> &arg, Char type) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800143 visit(ArgConverter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800144}
145
Glen Stark72d51e02016-06-08 01:23:32 +0200146// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800147template <typename Context>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800148class CharConverter {
Glen Stark72d51e02016-06-08 01:23:32 +0200149 private:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800150 basic_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200151
152 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
153
154 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800155 explicit CharConverter(basic_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200156
157 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800158 typename std::enable_if<std::is_integral<T>::value>::type
159 operator()(T value) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800160 arg_ = internal::make_arg<Context>(static_cast<char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200161 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800162
163 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800164 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800165 // 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 Zverovichbf0f1072017-01-28 13:17:47 +0000171template <typename Char>
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800172class PrintfWidthHandler {
Glen Stark72d51e02016-06-08 01:23:32 +0200173 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000174 typedef basic_format_specs<Char> format_specs;
175
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000176 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200177
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800178 FMT_DISALLOW_COPY_AND_ASSIGN(PrintfWidthHandler);
Glen Stark72d51e02016-06-08 01:23:32 +0200179
180 public:
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000181 explicit PrintfWidthHandler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200182
Glen Stark72d51e02016-06-08 01:23:32 +0200183 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800184 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
185 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800186 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200187 UnsignedType width = static_cast<UnsignedType>(value);
188 if (internal::is_negative(value)) {
189 spec_.align_ = ALIGN_LEFT;
190 width = 0 - width;
191 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700192 unsigned int_max = std::numeric_limits<int>::max();
193 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700194 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200195 return static_cast<unsigned>(width);
196 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800197
198 template <typename T>
199 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
200 operator()(T value) {
201 FMT_THROW(format_error("width is not integer"));
202 return 0;
203 }
Glen Stark72d51e02016-06-08 01:23:32 +0200204};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700205} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200206
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700207/**
208 \rst
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800209 The ``printf`` argument formatter.
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700210 \endrst
211 */
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800212template <typename Char>
Victor Zverovichc333dca2017-02-19 08:41:38 -0800213class printf_arg_formatter : public internal::arg_formatter_base<Char> {
Glen Stark72d51e02016-06-08 01:23:32 +0200214 private:
215 void write_null_pointer() {
216 this->spec().type_ = 0;
217 this->write("(nil)");
218 }
219
Victor Zverovichc333dca2017-02-19 08:41:38 -0800220 typedef internal::arg_formatter_base<Char> Base;
Glen Stark72d51e02016-06-08 01:23:32 +0200221
222 public:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000223 typedef typename Base::format_specs format_specs;
224
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700225 /**
226 \rst
227 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500228 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700229 specifier information for standard argument types.
230 \endrst
231 */
Victor Zverovich4ec88602017-02-18 07:46:32 -0800232 printf_arg_formatter(basic_buffer<Char> &buffer, format_specs &spec)
Victor Zverovichc333dca2017-02-19 08:41:38 -0800233 : internal::arg_formatter_base<Char>(buffer, spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200234
Victor Zverovich95a53e12016-11-19 07:39:07 -0800235 using Base::operator();
236
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700237 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800238 void operator()(bool value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000239 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200240 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800241 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200242 fmt_spec.type_ = 0;
243 this->write(value);
244 }
245
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700246 /** Formats a character. */
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800247 void operator()(Char value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000248 const format_specs &fmt_spec = this->spec();
Victor Zverovich84286212016-12-30 12:11:27 -0800249 basic_writer<Char> &w = this->writer();
Glen Stark72d51e02016-06-08 01:23:32 +0200250 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
251 w.write_int(value, fmt_spec);
Victor Zverovich84286212016-12-30 12:11:27 -0800252 typedef typename basic_writer<Char>::CharPtr CharPtr;
Glen Stark72d51e02016-06-08 01:23:32 +0200253 CharPtr out = CharPtr();
254 if (fmt_spec.width_ > 1) {
255 Char fill = ' ';
256 out = w.grow_buffer(fmt_spec.width_);
257 if (fmt_spec.align_ != ALIGN_LEFT) {
258 std::fill_n(out, fmt_spec.width_ - 1, fill);
259 out += fmt_spec.width_ - 1;
260 } else {
261 std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
262 }
263 } else {
264 out = w.grow_buffer(1);
265 }
266 *out = static_cast<Char>(value);
267 }
268
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700269 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800270 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200271 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800272 Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200273 else if (this->spec().type_ == 'p')
274 write_null_pointer();
275 else
276 this->write("(null)");
277 }
278
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700279 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800280 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200281 if (value)
Victor Zverovich95a53e12016-11-19 07:39:07 -0800282 return Base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200283 this->spec().type_ = 0;
284 write_null_pointer();
285 }
286
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700287 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800288 void operator()(internal::custom_value<Char> c) {
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700289 const Char format_str_data[] = {'}', '\0'};
290 basic_string_view<Char> format_str = format_str_data;
Victor Zverovich624c5862017-02-05 06:41:39 -0800291 auto args = basic_args<basic_context<Char>>();
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700292 basic_context<Char> ctx(args);
293 c.format(this->writer().buffer(), c.value, format_str, &ctx);
294 }
295};
296
297template <typename Char,
298 typename ArgFormatter = printf_arg_formatter<Char> >
299class printf_context;
300
301template <typename T, typename Char = char>
302struct printf_formatter {
303 const Char *parse(basic_string_view<Char> s) {
304 return s.data();
305 }
306
307 void format(basic_buffer<Char> &buf, const T &value, printf_context<Char> &) {
308 internal::format_value(buf, value);
Glen Stark72d51e02016-06-08 01:23:32 +0200309 }
310};
311
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700312/** This template formats data and writes the output to a writer. */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700313template <typename Char, typename ArgFormatter>
Victor Zverovich9998f662016-11-06 16:11:24 -0800314class printf_context :
Victor Zverovich624c5862017-02-05 06:41:39 -0800315 private internal::context_base<
Victor Zverovich9998f662016-11-06 16:11:24 -0800316 Char, printf_context<Char, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700317 public:
318 /** The character type for the output. */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700319 using char_type = Char;
320
321 template <typename T>
322 using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700323
Glen Stark72d51e02016-06-08 01:23:32 +0200324 private:
Victor Zverovich624c5862017-02-05 06:41:39 -0800325 typedef internal::context_base<Char, printf_context> Base;
Victor Zverovichd705d512016-12-29 09:07:39 -0800326 typedef typename Base::format_arg format_arg;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000327 typedef basic_format_specs<Char> format_specs;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700328 typedef internal::null_terminating_iterator<Char> iterator;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700329
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700330 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200331
332 // Returns the argument with specified index or, if arg_index is equal
333 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800334 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700335 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200336 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
337
338 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700339 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200340
341 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700342 /**
343 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800344 Constructs a ``printf_context`` object. References to the arguments and
345 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700346 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700347 \endrst
348 */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700349 explicit printf_context(basic_args<printf_context> args): Base(args) {}
Victor Zverovich9998f662016-11-06 16:11:24 -0800350
Victor Zverovichfefaf072017-02-14 16:29:47 -0500351 /** Formats stored arguments and writes the output to the buffer. */
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700352 FMT_API void format(
353 basic_string_view<Char> format_str, basic_buffer<Char> &buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200354};
355
356template <typename Char, typename AF>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700357void printf_context<Char, AF>::parse_flags(format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200358 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700359 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200360 case '-':
361 spec.align_ = ALIGN_LEFT;
362 break;
363 case '+':
364 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
365 break;
366 case '0':
367 spec.fill_ = '0';
368 break;
369 case ' ':
370 spec.flags_ |= SIGN_FLAG;
371 break;
372 case '#':
373 spec.flags_ |= HASH_FLAG;
374 break;
375 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700376 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200377 return;
378 }
379 }
380}
381
382template <typename Char, typename AF>
Victor Zverovichd705d512016-12-29 09:07:39 -0800383typename printf_context<Char, AF>::format_arg printf_context<Char, AF>::get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700384 iterator it, unsigned arg_index) {
385 (void)it;
Glen Stark72d51e02016-06-08 01:23:32 +0200386 const char *error = 0;
Victor Zverovich1102d462017-07-30 08:37:26 -0700387 format_arg arg;
388 if (arg_index == std::numeric_limits<unsigned>::max()) {
389 arg_index = this->next_arg_index(error);
390 if (!error)
391 arg = this->do_get_arg(arg_index, error);
392 } else {
393 arg = Base::get_arg(arg_index - 1, error);
394 }
Glen Stark72d51e02016-06-08 01:23:32 +0200395 if (error)
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700396 FMT_THROW(format_error(!*it ? "invalid format string" : error));
Glen Stark72d51e02016-06-08 01:23:32 +0200397 return arg;
398}
399
400template <typename Char, typename AF>
Victor Zverovich9998f662016-11-06 16:11:24 -0800401unsigned printf_context<Char, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700402 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200403 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700404 Char c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200405 if (c >= '0' && c <= '9') {
406 // Parse an argument index (if followed by '$') or a width possibly
407 // preceded with '0' flag(s).
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700408 unsigned value = internal::parse_nonnegative_int(it);
409 if (*it == '$') { // value is an argument index
410 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200411 arg_index = value;
412 } else {
413 if (c == '0')
414 spec.fill_ = '0';
415 if (value != 0) {
416 // Nonzero value means that we parsed width and don't need to
417 // parse it or flags again, so return now.
418 spec.width_ = value;
419 return arg_index;
420 }
421 }
422 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700423 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200424 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700425 if (*it >= '0' && *it <= '9') {
426 spec.width_ = internal::parse_nonnegative_int(it);
427 } else if (*it == '*') {
428 ++it;
429 spec.width_ = visit(internal::PrintfWidthHandler<Char>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200430 }
431 return arg_index;
432}
433
434template <typename Char, typename AF>
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700435void printf_context<Char, AF>::format(
436 basic_string_view<Char> format_str, basic_buffer<Char> &buffer) {
437 auto start = iterator(format_str);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700438 auto it = start;
439 using internal::pointer_from;
440 while (*it) {
441 Char c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200442 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700443 if (*it == c) {
444 buffer.append(pointer_from(start), pointer_from(it));
445 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200446 continue;
447 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700448 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200449
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000450 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200451 spec.align_ = ALIGN_RIGHT;
452
453 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700454 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200455
456 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700457 if (*it == '.') {
458 ++it;
459 if ('0' <= *it && *it <= '9') {
460 spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(it));
461 } else if (*it == '*') {
462 ++it;
463 spec.precision_ =
464 visit(internal::PrintfPrecisionHandler(), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200465 }
466 }
467
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700468 format_arg arg = get_arg(it, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800469 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700470 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200471 if (spec.fill_ == '0') {
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800472 if (arg.is_numeric())
Glen Stark72d51e02016-06-08 01:23:32 +0200473 spec.align_ = ALIGN_NUMERIC;
474 else
475 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
476 }
477
478 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800479 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700480 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200481 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700482 if (*it == 'h')
483 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200484 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700485 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200486 break;
487 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700488 if (*it == 'l')
Victor Zverovich016aceb2017-08-26 09:09:43 -0700489 convert_arg<long long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200490 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700491 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200492 break;
493 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700494 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200495 break;
496 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700497 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200498 break;
499 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700500 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200501 break;
502 case 'L':
503 // printf produces garbage when 'L' is omitted for long double, no
504 // need to do the same.
505 break;
506 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700507 --it;
508 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200509 }
510
511 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700512 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700513 FMT_THROW(format_error("invalid format string"));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700514 spec.type_ = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800515 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200516 // Normalize type.
517 switch (spec.type_) {
518 case 'i': case 'u':
519 spec.type_ = 'd';
520 break;
521 case 'c':
522 // TODO: handle wchar_t
Victor Zverovichd705d512016-12-29 09:07:39 -0800523 visit(internal::CharConverter<printf_context<Char, AF>>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200524 break;
525 }
526 }
527
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700528 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200529
530 // Format argument.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500531 visit(AF(buffer, spec), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200532 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700533 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200534}
Glen Stark72d51e02016-06-08 01:23:32 +0200535
536template <typename Char>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700537void printf(basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800538 basic_args<printf_context<Char>> args) {
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700539 printf_context<Char>(args).format(format, buf);
Glen Stark72d51e02016-06-08 01:23:32 +0200540}
541
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800542typedef basic_args<printf_context<char>> printf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800543
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700544inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800545 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500546 printf(buffer, format, args);
547 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700548}
549
Glen Stark72d51e02016-06-08 01:23:32 +0200550/**
551 \rst
552 Formats arguments and returns the result as a string.
553
554 **Example**::
555
556 std::string message = fmt::sprintf("The answer is %d", 42);
557 \endrst
558*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700559template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700560inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800561 return vsprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200562}
Glen Stark72d51e02016-06-08 01:23:32 +0200563
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800564inline std::wstring vsprintf(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700565 wstring_view format, basic_args<printf_context<wchar_t>> args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800566 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500567 printf(buffer, format, args);
568 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200569}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700570
571template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700572inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800573 auto vargs = make_args<printf_context<wchar_t>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700574 return vsprintf(format_str, vargs);
575}
576
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700577FMT_API int vfprintf(std::FILE *f, string_view format, printf_args args);
Glen Stark72d51e02016-06-08 01:23:32 +0200578
579/**
580 \rst
581 Prints formatted data to the file *f*.
582
583 **Example**::
584
585 fmt::fprintf(stderr, "Don't %s!", "panic");
586 \endrst
587 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700588template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700589inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800590 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700591 return vfprintf(f, format_str, vargs);
592}
593
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700594inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700595 return vfprintf(stdout, format, args);
596}
Glen Stark72d51e02016-06-08 01:23:32 +0200597
598/**
599 \rst
600 Prints formatted data to ``stdout``.
601
602 **Example**::
603
604 fmt::printf("Elapsed time: %.2f seconds", 1.23);
605 \endrst
606 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700607template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700608inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800609 return vprintf(format_str, make_args<printf_context<char>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200610}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700611
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700612inline int vfprintf(std::ostream &os, string_view format_str, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800613 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500614 printf(buffer, format_str, args);
615 internal::write(os, buffer);
616 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700617}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700618
619/**
620 \rst
621 Prints formatted data to the stream *os*.
622
623 **Example**::
624
625 fprintf(cerr, "Don't %s!", "panic");
626 \endrst
627 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700628template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700629inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700630 const Args & ... args) {
Victor Zverovich624c5862017-02-05 06:41:39 -0800631 auto vargs = make_args<printf_context<char>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700632 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700633}
Glen Stark72d51e02016-06-08 01:23:32 +0200634} // namespace fmt
635
636#endif // FMT_PRINTF_H_