blob: eefdb7bb980d37ccc599e439107078675d382464 [file] [log] [blame]
Victor Zverovichc0954452018-01-06 09:09:50 -08001// Formatting library for C++
2//
3// Copyright (c) 2012 - 2016, Victor Zverovich
4// All rights reserved.
5//
6// For the license information refer to format.h.
Glen Stark72d51e02016-06-08 01:23:32 +02007
8#ifndef FMT_PRINTF_H_
9#define FMT_PRINTF_H_
10
11#include <algorithm> // std::fill_n
12#include <limits> // std::numeric_limits
13
Victor Zverovich9dbb60c2016-08-03 08:52:05 -070014#include "fmt/ostream.h"
Glen Stark72d51e02016-06-08 01:23:32 +020015
16namespace fmt {
17namespace internal {
18
19// Checks if a value fits in int - used to avoid warnings about comparing
20// signed and unsigned integers.
21template <bool IsSigned>
22struct IntChecker {
23 template <typename T>
24 static bool fits_in_int(T value) {
25 unsigned max = std::numeric_limits<int>::max();
26 return value <= max;
27 }
28 static bool fits_in_int(bool) { return true; }
29};
30
31template <>
32struct IntChecker<true> {
33 template <typename T>
34 static bool fits_in_int(T value) {
35 return value >= std::numeric_limits<int>::min() &&
36 value <= std::numeric_limits<int>::max();
37 }
38 static bool fits_in_int(int) { return true; }
39};
40
Victor Zverovich0854f8c2016-12-11 13:22:45 -080041class PrintfPrecisionHandler {
Glen Stark72d51e02016-06-08 01:23:32 +020042 public:
Glen Stark72d51e02016-06-08 01:23:32 +020043 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080044 typename std::enable_if<std::is_integral<T>::value, int>::type
45 operator()(T value) {
Glen Stark72d51e02016-06-08 01:23:32 +020046 if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
Victor Zverovich9bb213e2016-08-25 08:38:07 -070047 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +020048 return static_cast<int>(value);
49 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -080050
51 template <typename T>
52 typename std::enable_if<!std::is_integral<T>::value, int>::type
53 operator()(T) {
54 FMT_THROW(format_error("precision is not integer"));
55 return 0;
56 }
Glen Stark72d51e02016-06-08 01:23:32 +020057};
58
Victor Zveroviche2dfd392016-11-19 09:29:09 -080059// An argument visitor that returns true iff arg is a zero integer.
60class IsZeroInt {
Glen Stark72d51e02016-06-08 01:23:32 +020061 public:
62 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -080063 typename std::enable_if<std::is_integral<T>::value, bool>::type
64 operator()(T value) { return value == 0; }
65
66 template <typename T>
67 typename std::enable_if<!std::is_integral<T>::value, bool>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -070068 operator()(T) { return false; }
Glen Stark72d51e02016-06-08 01:23:32 +020069};
70
Victor Zverovich0fbd8462017-09-04 11:41:15 -070071template <typename T>
72struct make_unsigned_or_bool : std::make_unsigned<T> {};
73
74template <>
75struct make_unsigned_or_bool<bool> {
76 using type = bool;
77};
78
Victor Zverovichd705d512016-12-29 09:07:39 -080079template <typename T, typename Context>
Victor Zverovich751ff642016-11-19 08:40:24 -080080class ArgConverter {
Glen Stark72d51e02016-06-08 01:23:32 +020081 private:
Victor Zverovichd705d512016-12-29 09:07:39 -080082 typedef typename Context::char_type Char;
83
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080084 basic_arg<Context> &arg_;
Victor Zverovichd705d512016-12-29 09:07:39 -080085 typename Context::char_type type_;
Glen Stark72d51e02016-06-08 01:23:32 +020086
Glen Stark72d51e02016-06-08 01:23:32 +020087 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -080088 ArgConverter(basic_arg<Context> &arg, Char type)
Glen Stark72d51e02016-06-08 01:23:32 +020089 : arg_(arg), type_(type) {}
90
Victor Zverovich751ff642016-11-19 08:40:24 -080091 void operator()(bool value) {
Glen Stark72d51e02016-06-08 01:23:32 +020092 if (type_ != 's')
Victor Zverovich751ff642016-11-19 08:40:24 -080093 operator()<bool>(value);
Glen Stark72d51e02016-06-08 01:23:32 +020094 }
95
96 template <typename U>
Victor Zverovich751ff642016-11-19 08:40:24 -080097 typename std::enable_if<std::is_integral<U>::value>::type
98 operator()(U value) {
Glen Stark72d51e02016-06-08 01:23:32 +020099 bool is_signed = type_ == 'd' || type_ == 'i';
Victor Zverovichc18a4042017-09-04 13:56:14 -0700100 typedef typename std::conditional<
Victor Zverovichf194a412017-08-27 09:16:50 -0700101 std::is_same<T, void>::value, U, T>::type TargetType;
Glen Stark72d51e02016-06-08 01:23:32 +0200102 if (sizeof(TargetType) <= sizeof(int)) {
103 // Extra casts are used to silence warnings.
104 if (is_signed) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800105 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800106 static_cast<int>(static_cast<TargetType>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200107 } else {
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700108 typedef typename make_unsigned_or_bool<TargetType>::type Unsigned;
Victor Zverovichd705d512016-12-29 09:07:39 -0800109 arg_ = internal::make_arg<Context>(
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800110 static_cast<unsigned>(static_cast<Unsigned>(value)));
Glen Stark72d51e02016-06-08 01:23:32 +0200111 }
112 } else {
113 if (is_signed) {
Glen Stark72d51e02016-06-08 01:23:32 +0200114 // glibc's printf doesn't sign extend arguments of smaller types:
115 // std::printf("%lld", -42); // prints "4294967254"
116 // but we don't have to do the same because it's a UB.
Victor Zverovich016aceb2017-08-26 09:09:43 -0700117 arg_ = internal::make_arg<Context>(static_cast<long long>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200118 } else {
Victor Zverovichd705d512016-12-29 09:07:39 -0800119 arg_ = internal::make_arg<Context>(
Victor Zverovich0fbd8462017-09-04 11:41:15 -0700120 static_cast<typename make_unsigned_or_bool<U>::type>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200121 }
122 }
123 }
Victor Zverovich751ff642016-11-19 08:40:24 -0800124
125 template <typename U>
Victor Zverovich77c892c2017-08-27 08:16:46 -0700126 typename std::enable_if<!std::is_integral<U>::value>::type operator()(U) {
Victor Zverovich751ff642016-11-19 08:40:24 -0800127 // No coversion needed for non-integral types.
128 }
Glen Stark72d51e02016-06-08 01:23:32 +0200129};
130
Victor Zverovich751ff642016-11-19 08:40:24 -0800131// Converts an integer argument to T for printf, if T is an integral type.
132// If T is void, the argument is converted to corresponding signed or unsigned
133// type depending on the type specifier: 'd' and 'i' - signed, other -
134// unsigned).
Victor Zverovichd705d512016-12-29 09:07:39 -0800135template <typename T, typename Context, typename Char>
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800136void convert_arg(basic_arg<Context> &arg, Char type) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800137 visit(ArgConverter<T, Context>(arg, type), arg);
Victor Zverovich751ff642016-11-19 08:40:24 -0800138}
139
Glen Stark72d51e02016-06-08 01:23:32 +0200140// Converts an integer argument to char for printf.
Victor Zverovichd705d512016-12-29 09:07:39 -0800141template <typename Context>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800142class CharConverter {
Glen Stark72d51e02016-06-08 01:23:32 +0200143 private:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800144 basic_arg<Context> &arg_;
Glen Stark72d51e02016-06-08 01:23:32 +0200145
146 FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
147
148 public:
Victor Zverovich7ae8bd72017-02-05 06:09:06 -0800149 explicit CharConverter(basic_arg<Context> &arg) : arg_(arg) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200150
151 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800152 typename std::enable_if<std::is_integral<T>::value>::type
153 operator()(T value) {
Victor Zverovichd705d512016-12-29 09:07:39 -0800154 arg_ = internal::make_arg<Context>(static_cast<char>(value));
Glen Stark72d51e02016-06-08 01:23:32 +0200155 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800156
157 template <typename T>
Victor Zverovichd705d512016-12-29 09:07:39 -0800158 typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800159 // No coversion needed for non-integral types.
160 }
Glen Stark72d51e02016-06-08 01:23:32 +0200161};
162
163// Checks if an argument is a valid printf width specifier and sets
164// left alignment if it is negative.
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000165template <typename Char>
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800166class PrintfWidthHandler {
Glen Stark72d51e02016-06-08 01:23:32 +0200167 private:
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000168 typedef basic_format_specs<Char> format_specs;
169
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000170 format_specs &spec_;
Glen Stark72d51e02016-06-08 01:23:32 +0200171
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800172 FMT_DISALLOW_COPY_AND_ASSIGN(PrintfWidthHandler);
Glen Stark72d51e02016-06-08 01:23:32 +0200173
174 public:
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000175 explicit PrintfWidthHandler(format_specs &spec) : spec_(spec) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200176
Glen Stark72d51e02016-06-08 01:23:32 +0200177 template <typename T>
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800178 typename std::enable_if<std::is_integral<T>::value, unsigned>::type
179 operator()(T value) {
Victor Zverovich6a2ff282017-02-19 06:46:51 -0800180 typedef typename internal::int_traits<T>::main_type UnsignedType;
Glen Stark72d51e02016-06-08 01:23:32 +0200181 UnsignedType width = static_cast<UnsignedType>(value);
182 if (internal::is_negative(value)) {
183 spec_.align_ = ALIGN_LEFT;
184 width = 0 - width;
185 }
Victor Zveroviche0d6f632016-06-15 06:29:47 -0700186 unsigned int_max = std::numeric_limits<int>::max();
187 if (width > int_max)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700188 FMT_THROW(format_error("number is too big"));
Glen Stark72d51e02016-06-08 01:23:32 +0200189 return static_cast<unsigned>(width);
190 }
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800191
192 template <typename T>
193 typename std::enable_if<!std::is_integral<T>::value, unsigned>::type
Victor Zverovich77c892c2017-08-27 08:16:46 -0700194 operator()(T) {
Victor Zveroviche2dfd392016-11-19 09:29:09 -0800195 FMT_THROW(format_error("width is not integer"));
196 return 0;
197 }
Glen Stark72d51e02016-06-08 01:23:32 +0200198};
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700199} // namespace internal
Glen Stark72d51e02016-06-08 01:23:32 +0200200
Victor Zverovichc0954452018-01-06 09:09:50 -0800201template <typename Range>
Victor Zverovich10e70a02017-12-02 09:44:48 -0800202class printf_arg_formatter;
203
Victor Zverovichc0954452018-01-06 09:09:50 -0800204template <typename Range, typename ArgFormatter = printf_arg_formatter<Range>>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800205class basic_printf_context;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800206
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 Zverovichc0954452018-01-06 09:09:50 -0800212template <typename Range>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800213class printf_arg_formatter : public internal::arg_formatter_base<Range> {
Glen Stark72d51e02016-06-08 01:23:32 +0200214 private:
Victor Zverovich217e7c72018-01-14 07:19:23 -0800215 basic_printf_context<Range> &context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800216
Glen Stark72d51e02016-06-08 01:23:32 +0200217 void write_null_pointer() {
218 this->spec().type_ = 0;
219 this->write("(nil)");
220 }
221
Victor Zverovichc0954452018-01-06 09:09:50 -0800222 using char_type = typename Range::value_type;
Victor Zverovich22994c62018-01-13 15:34:48 -0800223 using base = internal::arg_formatter_base<Range>;
Glen Stark72d51e02016-06-08 01:23:32 +0200224
225 public:
Victor Zverovich22994c62018-01-13 15:34:48 -0800226 using format_specs = typename base::format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000227
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700228 /**
229 \rst
230 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500231 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700232 specifier information for standard argument types.
233 \endrst
234 */
Victor Zverovich9a53a702018-01-14 12:25:03 -0800235 printf_arg_formatter(internal::basic_buffer<char_type> &buffer,
236 format_specs &spec, basic_printf_context<Range> &ctx)
Victor Zverovich22994c62018-01-13 15:34:48 -0800237 : base(buffer, spec), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200238
Victor Zverovichc0954452018-01-06 09:09:50 -0800239 using base::operator();
Victor Zverovich95a53e12016-11-19 07:39:07 -0800240
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700241 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800242 void operator()(bool value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000243 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200244 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800245 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200246 fmt_spec.type_ = 0;
247 this->write(value);
248 }
249
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700250 /** Formats a character. */
Victor Zverovichc0954452018-01-06 09:09:50 -0800251 void operator()(char_type value) {
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700252 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200253 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700254 return (*this)(static_cast<int>(value));
255 fmt_spec.flags_ = 0;
256 fmt_spec.align_ = ALIGN_RIGHT;
Victor Zverovichc0954452018-01-06 09:09:50 -0800257 base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200258 }
259
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700260 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800261 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200262 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800263 base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200264 else if (this->spec().type_ == 'p')
265 write_null_pointer();
266 else
267 this->write("(null)");
268 }
269
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700270 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800271 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200272 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800273 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200274 this->spec().type_ = 0;
275 write_null_pointer();
276 }
277
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700278 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovich217e7c72018-01-14 07:19:23 -0800279 void operator()(
280 typename basic_arg<basic_printf_context<Range>>::handle handle) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800281 handle.format(context_);
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700282 }
283};
284
Victor Zverovichc0954452018-01-06 09:09:50 -0800285template <typename T>
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700286struct printf_formatter {
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700287 template <typename ParseContext>
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800288 auto parse(ParseContext &ctx) { return ctx.begin(); }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700289
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800290 template <typename FormatContext>
291 auto format(const T &value, FormatContext &ctx) -> decltype(ctx.begin()) {
Victor Zverovich217e7c72018-01-14 07:19:23 -0800292 internal::format_value(ctx.range().container(), value);
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800293 return ctx.begin();
Glen Stark72d51e02016-06-08 01:23:32 +0200294 }
295};
296
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700297/** This template formats data and writes the output to a writer. */
Victor Zverovichc0954452018-01-06 09:09:50 -0800298template <typename Range, typename ArgFormatter>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800299class basic_printf_context :
300 private internal::context_base<
301 Range, basic_printf_context<Range, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700302 public:
303 /** The character type for the output. */
Victor Zverovichc0954452018-01-06 09:09:50 -0800304 using char_type = typename Range::value_type;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700305
306 template <typename T>
307 using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700308
Glen Stark72d51e02016-06-08 01:23:32 +0200309 private:
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800310 using base = internal::context_base<Range, basic_printf_context>;
311 using format_arg = typename base::format_arg;
Victor Zverovichc0954452018-01-06 09:09:50 -0800312 using format_specs = basic_format_specs<char_type>;
313 using iterator = internal::null_terminating_iterator<char_type>;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700314
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700315 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200316
317 // Returns the argument with specified index or, if arg_index is equal
318 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800319 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700320 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200321 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
322
323 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700324 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200325
326 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700327 /**
328 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800329 Constructs a ``printf_context`` object. References to the arguments and
330 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700331 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700332 \endrst
333 */
Victor Zverovich217e7c72018-01-14 07:19:23 -0800334 basic_printf_context(Range range, basic_string_view<char_type> format_str,
335 basic_format_args<basic_printf_context> args)
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800336 : base(range, format_str, args) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700337
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800338 using base::parse_context;
339 using base::range;
340 using base::begin;
341 using base::advance_to;
Victor Zverovich9998f662016-11-06 16:11:24 -0800342
Victor Zverovichc0954452018-01-06 09:09:50 -0800343 /** Formats stored arguments and writes the output to the range. */
344 FMT_API void format();
Glen Stark72d51e02016-06-08 01:23:32 +0200345};
346
Victor Zverovichc0954452018-01-06 09:09:50 -0800347template <typename Range, typename AF>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800348void basic_printf_context<Range, AF>::parse_flags(
349 format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200350 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700351 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200352 case '-':
353 spec.align_ = ALIGN_LEFT;
354 break;
355 case '+':
356 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
357 break;
358 case '0':
359 spec.fill_ = '0';
360 break;
361 case ' ':
362 spec.flags_ |= SIGN_FLAG;
363 break;
364 case '#':
365 spec.flags_ |= HASH_FLAG;
366 break;
367 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700368 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200369 return;
370 }
371 }
372}
373
Victor Zverovichc0954452018-01-06 09:09:50 -0800374template <typename Range, typename AF>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800375typename basic_printf_context<Range, AF>::format_arg
376 basic_printf_context<Range, AF>::get_arg(iterator it, unsigned arg_index) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700377 (void)it;
Victor Zverovichc523dd52017-11-19 07:36:01 -0800378 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zverovich67928ea2018-01-14 09:27:40 -0800379 return this->do_get_arg(this->parse_context().next_arg_id());
Victor Zverovich91ee9c92018-01-14 11:00:27 -0800380 return base::get_arg(arg_index - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200381}
382
Victor Zverovichc0954452018-01-06 09:09:50 -0800383template <typename Range, typename AF>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800384unsigned basic_printf_context<Range, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700385 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200386 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800387 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200388 if (c >= '0' && c <= '9') {
389 // Parse an argument index (if followed by '$') or a width possibly
390 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700391 internal::error_handler eh;
392 unsigned value = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700393 if (*it == '$') { // value is an argument index
394 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200395 arg_index = value;
396 } else {
397 if (c == '0')
398 spec.fill_ = '0';
399 if (value != 0) {
400 // Nonzero value means that we parsed width and don't need to
401 // parse it or flags again, so return now.
402 spec.width_ = value;
403 return arg_index;
404 }
405 }
406 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700407 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200408 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700409 if (*it >= '0' && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700410 internal::error_handler eh;
411 spec.width_ = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700412 } else if (*it == '*') {
413 ++it;
Victor Zverovichc0954452018-01-06 09:09:50 -0800414 spec.width_ =
415 visit(internal::PrintfWidthHandler<char_type>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200416 }
417 return arg_index;
418}
419
Victor Zverovichc0954452018-01-06 09:09:50 -0800420template <typename Range, typename AF>
Victor Zverovich217e7c72018-01-14 07:19:23 -0800421void basic_printf_context<Range, AF>::format() {
422 auto &buffer = this->range().container();
Victor Zverovich67928ea2018-01-14 09:27:40 -0800423 auto start = iterator(this->parse_context());
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700424 auto it = start;
425 using internal::pointer_from;
426 while (*it) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800427 char_type 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 Zverovich7cea1632017-12-09 06:19:15 -0800459 if (arg.is_arithmetic())
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 Zverovich217e7c72018-01-14 07:19:23 -0800510 visit(internal::CharConverter<basic_printf_context<Range, AF>>(arg),
511 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 Zverovich10e70a02017-12-02 09:44:48 -0800519 visit(AF(buffer, spec, *this), 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
Victor Zverovich217e7c72018-01-14 07:19:23 -0800524template <typename Char, typename Context>
Victor Zverovich9a53a702018-01-14 12:25:03 -0800525void printf(internal::basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovich217e7c72018-01-14 07:19:23 -0800526 basic_format_args<Context> args) {
527 Context(buf, format, args).format();
Glen Stark72d51e02016-06-08 01:23:32 +0200528}
529
Victor Zverovich217e7c72018-01-14 07:19:23 -0800530template <typename Buffer>
531using printf_context = basic_printf_context<internal::dynamic_range<Buffer>>;
532
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800533using printf_args = basic_format_args<printf_context<internal::buffer>>;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800534
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700535inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800536 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500537 printf(buffer, format, args);
538 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700539}
540
Glen Stark72d51e02016-06-08 01:23:32 +0200541/**
542 \rst
543 Formats arguments and returns the result as a string.
544
545 **Example**::
546
547 std::string message = fmt::sprintf("The answer is %d", 42);
548 \endrst
549*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700550template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700551inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800552 return vsprintf(format_str,
553 make_args<printf_context<internal::buffer>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200554}
Glen Stark72d51e02016-06-08 01:23:32 +0200555
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800556inline std::wstring vsprintf(
557 wstring_view format,
558 basic_format_args<printf_context<internal::wbuffer>> args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800559 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500560 printf(buffer, format, args);
561 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200562}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700563
564template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700565inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800566 auto vargs = make_args<printf_context<internal::wbuffer>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700567 return vsprintf(format_str, vargs);
568}
569
Victor Zverovich955062d2017-12-17 08:36:19 -0800570inline int vfprintf(std::FILE *f, string_view format, printf_args args) {
571 memory_buffer buffer;
572 printf(buffer, format, args);
573 std::size_t size = buffer.size();
574 return std::fwrite(
575 buffer.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
576}
Glen Stark72d51e02016-06-08 01:23:32 +0200577
578/**
579 \rst
580 Prints formatted data to the file *f*.
581
582 **Example**::
583
584 fmt::fprintf(stderr, "Don't %s!", "panic");
585 \endrst
586 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700587template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700588inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800589 auto vargs = make_args<printf_context<internal::buffer>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700590 return vfprintf(f, format_str, vargs);
591}
592
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700593inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700594 return vfprintf(stdout, format, args);
595}
Glen Stark72d51e02016-06-08 01:23:32 +0200596
597/**
598 \rst
599 Prints formatted data to ``stdout``.
600
601 **Example**::
602
603 fmt::printf("Elapsed time: %.2f seconds", 1.23);
604 \endrst
605 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700606template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700607inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800608 return vprintf(format_str,
609 make_args<printf_context<internal::buffer>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200610}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700611
Victor Zverovichc0954452018-01-06 09:09:50 -0800612inline int vfprintf(std::ostream &os, string_view format_str,
613 printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800614 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500615 printf(buffer, format_str, args);
616 internal::write(os, buffer);
617 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700618}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700619
620/**
621 \rst
622 Prints formatted data to the stream *os*.
623
624 **Example**::
625
626 fprintf(cerr, "Don't %s!", "panic");
627 \endrst
628 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700629template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700630inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700631 const Args & ... args) {
Victor Zverovichc2fecb92018-01-14 14:15:59 -0800632 auto vargs = make_args<printf_context<internal::buffer>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700633 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700634}
Glen Stark72d51e02016-06-08 01:23:32 +0200635} // namespace fmt
636
637#endif // FMT_PRINTF_H_