blob: 64f79654d86044a8f98af8ac6823864a92492124 [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 Zverovich10e70a02017-12-02 09:44:48 -0800205class printf_context;
206
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>
213class printf_arg_formatter :
214 public internal::arg_formatter_base<typename Range::value_type> {
Glen Stark72d51e02016-06-08 01:23:32 +0200215 private:
Victor Zverovichc0954452018-01-06 09:09:50 -0800216 printf_context<Range>& context_;
Victor Zverovich10e70a02017-12-02 09:44:48 -0800217
Glen Stark72d51e02016-06-08 01:23:32 +0200218 void write_null_pointer() {
219 this->spec().type_ = 0;
220 this->write("(nil)");
221 }
222
Victor Zverovichc0954452018-01-06 09:09:50 -0800223 using char_type = typename Range::value_type;
224 using base = internal::arg_formatter_base<char_type>;
Glen Stark72d51e02016-06-08 01:23:32 +0200225
226 public:
Victor Zverovichc0954452018-01-06 09:09:50 -0800227 typedef typename base::format_specs format_specs;
Victor Zverovichbf0f1072017-01-28 13:17:47 +0000228
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700229 /**
230 \rst
231 Constructs an argument formatter object.
Victor Zverovichfefaf072017-02-14 16:29:47 -0500232 *buffer* is a reference to the output buffer and *spec* contains format
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700233 specifier information for standard argument types.
234 \endrst
235 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800236 printf_arg_formatter(basic_buffer<char_type> &buffer, format_specs &spec,
237 printf_context<Range> &ctx)
238 : internal::arg_formatter_base<char_type>(buffer, spec), context_(ctx) {}
Glen Stark72d51e02016-06-08 01:23:32 +0200239
Victor Zverovichc0954452018-01-06 09:09:50 -0800240 using base::operator();
Victor Zverovich95a53e12016-11-19 07:39:07 -0800241
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700242 /** Formats an argument of type ``bool``. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800243 void operator()(bool value) {
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000244 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200245 if (fmt_spec.type_ != 's')
Victor Zverovichd58cc8a2016-11-20 07:42:38 -0800246 return (*this)(value ? 1 : 0);
Glen Stark72d51e02016-06-08 01:23:32 +0200247 fmt_spec.type_ = 0;
248 this->write(value);
249 }
250
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700251 /** Formats a character. */
Victor Zverovichc0954452018-01-06 09:09:50 -0800252 void operator()(char_type value) {
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700253 format_specs &fmt_spec = this->spec();
Glen Stark72d51e02016-06-08 01:23:32 +0200254 if (fmt_spec.type_ && fmt_spec.type_ != 'c')
Victor Zverovichaf00e4f2017-09-04 12:28:53 -0700255 return (*this)(static_cast<int>(value));
256 fmt_spec.flags_ = 0;
257 fmt_spec.align_ = ALIGN_RIGHT;
Victor Zverovichc0954452018-01-06 09:09:50 -0800258 base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200259 }
260
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700261 /** Formats a null-terminated C string. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800262 void operator()(const char *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200263 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800264 base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200265 else if (this->spec().type_ == 'p')
266 write_null_pointer();
267 else
268 this->write("(null)");
269 }
270
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700271 /** Formats a pointer. */
Victor Zverovich95a53e12016-11-19 07:39:07 -0800272 void operator()(const void *value) {
Glen Stark72d51e02016-06-08 01:23:32 +0200273 if (value)
Victor Zverovichc0954452018-01-06 09:09:50 -0800274 return base::operator()(value);
Glen Stark72d51e02016-06-08 01:23:32 +0200275 this->spec().type_ = 0;
276 write_null_pointer();
277 }
278
Victor Zverovich6ee9f2e2016-07-21 06:59:28 -0700279 /** Formats an argument of a custom (user-defined) type. */
Victor Zverovichc0954452018-01-06 09:09:50 -0800280 void operator()(typename basic_arg<printf_context<Range>>::handle handle) {
281 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 Zverovichc0954452018-01-06 09:09:50 -0800288 auto parse(ParseContext& ctx) { return ctx.begin(); }
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700289
Victor Zverovichc0954452018-01-06 09:09:50 -0800290 template <typename Range>
291 void format(const T &value, printf_context<Range> &ctx) {
292 internal::format_value(ctx.range(), value);
Glen Stark72d51e02016-06-08 01:23:32 +0200293 }
294};
295
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700296/** This template formats data and writes the output to a writer. */
Victor Zverovichc0954452018-01-06 09:09:50 -0800297template <typename Range, typename ArgFormatter>
Victor Zverovich9998f662016-11-06 16:11:24 -0800298class printf_context :
Victor Zverovichc0954452018-01-06 09:09:50 -0800299 private internal::context_base<Range, printf_context<Range, ArgFormatter>> {
Victor Zverovich18dfa252016-10-21 06:46:21 -0700300 public:
301 /** The character type for the output. */
Victor Zverovichc0954452018-01-06 09:09:50 -0800302 using char_type = typename Range::value_type;
Victor Zverovich5e0562a2017-08-13 13:09:02 -0700303
304 template <typename T>
305 using formatter_type = printf_formatter<T>;
Victor Zverovich18dfa252016-10-21 06:46:21 -0700306
Glen Stark72d51e02016-06-08 01:23:32 +0200307 private:
Victor Zverovichc0954452018-01-06 09:09:50 -0800308 typedef internal::context_base<Range, printf_context> Base;
309 using format_arg = typename Base::format_arg;
310 using format_specs = basic_format_specs<char_type>;
311 using iterator = internal::null_terminating_iterator<char_type>;
Victor Zverovichdafbec72016-10-07 08:37:06 -0700312
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700313 void parse_flags(format_specs &spec, iterator &it);
Glen Stark72d51e02016-06-08 01:23:32 +0200314
315 // Returns the argument with specified index or, if arg_index is equal
316 // to the maximum unsigned value, the next argument.
Victor Zverovichd705d512016-12-29 09:07:39 -0800317 format_arg get_arg(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700318 iterator it,
Glen Stark72d51e02016-06-08 01:23:32 +0200319 unsigned arg_index = (std::numeric_limits<unsigned>::max)());
320
321 // Parses argument index, flags and width and returns the argument index.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700322 unsigned parse_header(iterator &it, format_specs &spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200323
324 public:
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700325 /**
326 \rst
Victor Zverovich9998f662016-11-06 16:11:24 -0800327 Constructs a ``printf_context`` object. References to the arguments and
328 the writer are stored in the context object so make sure they have
Victor Zverovichab054532016-07-20 08:21:13 -0700329 appropriate lifetimes.
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700330 \endrst
331 */
Victor Zverovichc0954452018-01-06 09:09:50 -0800332 printf_context(Range &range, basic_string_view<char_type> format_str,
Victor Zverovich81bd9e82017-12-03 07:32:04 -0800333 basic_format_args<printf_context> args)
Victor Zverovichc0954452018-01-06 09:09:50 -0800334 : Base(range, format_str, args) {}
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700335
Victor Zverovich44e18652017-11-19 08:49:58 -0800336 using Base::parse_context;
Victor Zverovichc0954452018-01-06 09:09:50 -0800337 using Base::range;
Victor Zverovich9998f662016-11-06 16:11:24 -0800338
Victor Zverovichc0954452018-01-06 09:09:50 -0800339 /** Formats stored arguments and writes the output to the range. */
340 FMT_API void format();
Glen Stark72d51e02016-06-08 01:23:32 +0200341};
342
Victor Zverovichc0954452018-01-06 09:09:50 -0800343template <typename Range, typename AF>
344void printf_context<Range, AF>::parse_flags(format_specs &spec, iterator &it) {
Glen Stark72d51e02016-06-08 01:23:32 +0200345 for (;;) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700346 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200347 case '-':
348 spec.align_ = ALIGN_LEFT;
349 break;
350 case '+':
351 spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
352 break;
353 case '0':
354 spec.fill_ = '0';
355 break;
356 case ' ':
357 spec.flags_ |= SIGN_FLAG;
358 break;
359 case '#':
360 spec.flags_ |= HASH_FLAG;
361 break;
362 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700363 --it;
Glen Stark72d51e02016-06-08 01:23:32 +0200364 return;
365 }
366 }
367}
368
Victor Zverovichc0954452018-01-06 09:09:50 -0800369template <typename Range, typename AF>
370typename printf_context<Range, AF>::format_arg
371 printf_context<Range, AF>::get_arg(iterator it, unsigned arg_index) {
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700372 (void)it;
Victor Zverovichc523dd52017-11-19 07:36:01 -0800373 if (arg_index == std::numeric_limits<unsigned>::max())
Victor Zveroviche0243002017-11-26 09:29:55 -0800374 return this->do_get_arg(this->next_arg_id());
Victor Zverovichc523dd52017-11-19 07:36:01 -0800375 return Base::get_arg(arg_index - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200376}
377
Victor Zverovichc0954452018-01-06 09:09:50 -0800378template <typename Range, typename AF>
379unsigned printf_context<Range, AF>::parse_header(
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700380 iterator &it, format_specs &spec) {
Glen Stark72d51e02016-06-08 01:23:32 +0200381 unsigned arg_index = std::numeric_limits<unsigned>::max();
Victor Zverovichc0954452018-01-06 09:09:50 -0800382 char_type c = *it;
Glen Stark72d51e02016-06-08 01:23:32 +0200383 if (c >= '0' && c <= '9') {
384 // Parse an argument index (if followed by '$') or a width possibly
385 // preceded with '0' flag(s).
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700386 internal::error_handler eh;
387 unsigned value = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700388 if (*it == '$') { // value is an argument index
389 ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200390 arg_index = value;
391 } else {
392 if (c == '0')
393 spec.fill_ = '0';
394 if (value != 0) {
395 // Nonzero value means that we parsed width and don't need to
396 // parse it or flags again, so return now.
397 spec.width_ = value;
398 return arg_index;
399 }
400 }
401 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700402 parse_flags(spec, it);
Glen Stark72d51e02016-06-08 01:23:32 +0200403 // Parse width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700404 if (*it >= '0' && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700405 internal::error_handler eh;
406 spec.width_ = parse_nonnegative_int(it, eh);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700407 } else if (*it == '*') {
408 ++it;
Victor Zverovichc0954452018-01-06 09:09:50 -0800409 spec.width_ =
410 visit(internal::PrintfWidthHandler<char_type>(spec), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200411 }
412 return arg_index;
413}
414
Victor Zverovichc0954452018-01-06 09:09:50 -0800415template <typename Range, typename AF>
416void printf_context<Range, AF>::format() {
417 Range &buffer = this->range();
Victor Zverovich8cbf5442017-09-17 08:32:57 -0700418 Base &base = *this;
419 auto start = iterator(base);
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700420 auto it = start;
421 using internal::pointer_from;
422 while (*it) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800423 char_type c = *it++;
Glen Stark72d51e02016-06-08 01:23:32 +0200424 if (c != '%') continue;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700425 if (*it == c) {
426 buffer.append(pointer_from(start), pointer_from(it));
427 start = ++it;
Glen Stark72d51e02016-06-08 01:23:32 +0200428 continue;
429 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700430 buffer.append(pointer_from(start), pointer_from(it) - 1);
Glen Stark72d51e02016-06-08 01:23:32 +0200431
Victor Zverovich296e9ca2017-01-28 12:51:35 +0000432 format_specs spec;
Glen Stark72d51e02016-06-08 01:23:32 +0200433 spec.align_ = ALIGN_RIGHT;
434
435 // Parse argument index, flags and width.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700436 unsigned arg_index = parse_header(it, spec);
Glen Stark72d51e02016-06-08 01:23:32 +0200437
438 // Parse precision.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700439 if (*it == '.') {
440 ++it;
441 if ('0' <= *it && *it <= '9') {
Victor Zverovich932ab2b2017-10-21 08:37:52 -0700442 internal::error_handler eh;
443 spec.precision_ = static_cast<int>(parse_nonnegative_int(it, eh));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700444 } else if (*it == '*') {
445 ++it;
446 spec.precision_ =
447 visit(internal::PrintfPrecisionHandler(), get_arg(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200448 }
449 }
450
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700451 format_arg arg = get_arg(it, arg_index);
Victor Zverovichc9dc41a2016-11-19 07:59:54 -0800452 if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
Victor Zverovichd4ddaaf2016-07-20 08:09:14 -0700453 spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
Glen Stark72d51e02016-06-08 01:23:32 +0200454 if (spec.fill_ == '0') {
Victor Zverovich7cea1632017-12-09 06:19:15 -0800455 if (arg.is_arithmetic())
Glen Stark72d51e02016-06-08 01:23:32 +0200456 spec.align_ = ALIGN_NUMERIC;
457 else
458 spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
459 }
460
461 // Parse length and convert the argument to the required type.
Victor Zverovich751ff642016-11-19 08:40:24 -0800462 using internal::convert_arg;
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700463 switch (*it++) {
Glen Stark72d51e02016-06-08 01:23:32 +0200464 case 'h':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700465 if (*it == 'h')
466 convert_arg<signed char>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200467 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700468 convert_arg<short>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200469 break;
470 case 'l':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700471 if (*it == 'l')
Victor Zverovich016aceb2017-08-26 09:09:43 -0700472 convert_arg<long long>(arg, *++it);
Glen Stark72d51e02016-06-08 01:23:32 +0200473 else
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700474 convert_arg<long>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200475 break;
476 case 'j':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700477 convert_arg<intmax_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200478 break;
479 case 'z':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700480 convert_arg<std::size_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200481 break;
482 case 't':
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700483 convert_arg<std::ptrdiff_t>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200484 break;
485 case 'L':
486 // printf produces garbage when 'L' is omitted for long double, no
487 // need to do the same.
488 break;
489 default:
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700490 --it;
491 convert_arg<void>(arg, *it);
Glen Stark72d51e02016-06-08 01:23:32 +0200492 }
493
494 // Parse type.
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700495 if (!*it)
Victor Zverovich9bb213e2016-08-25 08:38:07 -0700496 FMT_THROW(format_error("invalid format string"));
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700497 spec.type_ = static_cast<char>(*it++);
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800498 if (arg.is_integral()) {
Glen Stark72d51e02016-06-08 01:23:32 +0200499 // Normalize type.
500 switch (spec.type_) {
501 case 'i': case 'u':
502 spec.type_ = 'd';
503 break;
504 case 'c':
505 // TODO: handle wchar_t
Victor Zverovichc0954452018-01-06 09:09:50 -0800506 visit(internal::CharConverter<printf_context<Range, AF>>(arg), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200507 break;
508 }
509 }
510
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700511 start = it;
Glen Stark72d51e02016-06-08 01:23:32 +0200512
513 // Format argument.
Victor Zverovich10e70a02017-12-02 09:44:48 -0800514 visit(AF(buffer, spec, *this), arg);
Glen Stark72d51e02016-06-08 01:23:32 +0200515 }
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700516 buffer.append(pointer_from(start), pointer_from(it));
Glen Stark72d51e02016-06-08 01:23:32 +0200517}
Glen Stark72d51e02016-06-08 01:23:32 +0200518
519template <typename Char>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700520void printf(basic_buffer<Char> &buf, basic_string_view<Char> format,
Victor Zverovichc0954452018-01-06 09:09:50 -0800521 basic_format_args<printf_context<basic_buffer<Char>>> args) {
522 printf_context<basic_buffer<Char>>(buf, format, args).format();
Glen Stark72d51e02016-06-08 01:23:32 +0200523}
524
Victor Zverovichc0954452018-01-06 09:09:50 -0800525typedef basic_format_args<printf_context<buffer>> printf_args;
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800526
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700527inline std::string vsprintf(string_view format, printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800528 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500529 printf(buffer, format, args);
530 return to_string(buffer);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700531}
532
Glen Stark72d51e02016-06-08 01:23:32 +0200533/**
534 \rst
535 Formats arguments and returns the result as a string.
536
537 **Example**::
538
539 std::string message = fmt::sprintf("The answer is %d", 42);
540 \endrst
541*/
Victor Zverovich0028ce52016-08-26 17:23:13 -0700542template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700543inline std::string sprintf(string_view format_str, const Args & ... args) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800544 return vsprintf(format_str, make_args<printf_context<buffer>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200545}
Glen Stark72d51e02016-06-08 01:23:32 +0200546
Victor Zverovich0854f8c2016-12-11 13:22:45 -0800547inline std::wstring vsprintf(
Victor Zverovichc0954452018-01-06 09:09:50 -0800548 wstring_view format, basic_format_args<printf_context<wbuffer>> args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800549 wmemory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500550 printf(buffer, format, args);
551 return to_string(buffer);
Glen Stark72d51e02016-06-08 01:23:32 +0200552}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700553
554template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700555inline std::wstring sprintf(wstring_view format_str, const Args & ... args) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800556 auto vargs = make_args<printf_context<wbuffer>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700557 return vsprintf(format_str, vargs);
558}
559
Victor Zverovich955062d2017-12-17 08:36:19 -0800560inline int vfprintf(std::FILE *f, string_view format, printf_args args) {
561 memory_buffer buffer;
562 printf(buffer, format, args);
563 std::size_t size = buffer.size();
564 return std::fwrite(
565 buffer.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
566}
Glen Stark72d51e02016-06-08 01:23:32 +0200567
568/**
569 \rst
570 Prints formatted data to the file *f*.
571
572 **Example**::
573
574 fmt::fprintf(stderr, "Don't %s!", "panic");
575 \endrst
576 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700577template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700578inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800579 auto vargs = make_args<printf_context<buffer>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700580 return vfprintf(f, format_str, vargs);
581}
582
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700583inline int vprintf(string_view format, printf_args args) {
Victor Zverovich0028ce52016-08-26 17:23:13 -0700584 return vfprintf(stdout, format, args);
585}
Glen Stark72d51e02016-06-08 01:23:32 +0200586
587/**
588 \rst
589 Prints formatted data to ``stdout``.
590
591 **Example**::
592
593 fmt::printf("Elapsed time: %.2f seconds", 1.23);
594 \endrst
595 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700596template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700597inline int printf(string_view format_str, const Args & ... args) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800598 return vprintf(format_str, make_args<printf_context<buffer>>(args...));
Glen Stark72d51e02016-06-08 01:23:32 +0200599}
Victor Zverovich0028ce52016-08-26 17:23:13 -0700600
Victor Zverovichc0954452018-01-06 09:09:50 -0800601inline int vfprintf(std::ostream &os, string_view format_str,
602 printf_args args) {
Victor Zverovicheedfd072017-02-18 09:13:12 -0800603 memory_buffer buffer;
Victor Zverovichfefaf072017-02-14 16:29:47 -0500604 printf(buffer, format_str, args);
605 internal::write(os, buffer);
606 return static_cast<int>(buffer.size());
Victor Zverovich0028ce52016-08-26 17:23:13 -0700607}
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700608
609/**
610 \rst
611 Prints formatted data to the stream *os*.
612
613 **Example**::
614
615 fprintf(cerr, "Don't %s!", "panic");
616 \endrst
617 */
Victor Zverovich0028ce52016-08-26 17:23:13 -0700618template <typename... Args>
Victor Zverovich2f4f49f2017-07-18 19:40:48 -0700619inline int fprintf(std::ostream &os, string_view format_str,
Victor Zverovich0028ce52016-08-26 17:23:13 -0700620 const Args & ... args) {
Victor Zverovichc0954452018-01-06 09:09:50 -0800621 auto vargs = make_args<printf_context<buffer>>(args...);
Victor Zverovich0028ce52016-08-26 17:23:13 -0700622 return vfprintf(os, format_str, vargs);
Victor Zverovich9dbb60c2016-08-03 08:52:05 -0700623}
Glen Stark72d51e02016-06-08 01:23:32 +0200624} // namespace fmt
625
626#endif // FMT_PRINTF_H_