| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 1 | /* |
| 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 Zverovich | 9dbb60c | 2016-08-03 08:52:05 -0700 | [diff] [blame] | 16 | #include "fmt/ostream.h" |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 17 | |
| 18 | namespace fmt { |
| 19 | namespace internal { |
| 20 | |
| 21 | // Checks if a value fits in int - used to avoid warnings about comparing |
| 22 | // signed and unsigned integers. |
| 23 | template <bool IsSigned> |
| 24 | struct 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 | |
| 33 | template <> |
| 34 | struct 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 | |
| 43 | class PrecisionHandler : public ArgVisitor<PrecisionHandler, int> { |
| 44 | public: |
| 45 | void report_unhandled_arg() { |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 46 | FMT_THROW(format_error("precision is not integer")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | template <typename T> |
| 50 | int visit_any_int(T value) { |
| 51 | if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value)) |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 52 | FMT_THROW(format_error("number is too big")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 53 | return static_cast<int>(value); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | // IsZeroInt::visit(arg) returns true iff arg is a zero integer. |
| 58 | class IsZeroInt : public ArgVisitor<IsZeroInt, bool> { |
| 59 | public: |
| 60 | template <typename T> |
| 61 | bool visit_any_int(T value) { return value == 0; } |
| 62 | }; |
| 63 | |
| 64 | template <typename T, typename U> |
| 65 | struct is_same { |
| 66 | enum { value = 0 }; |
| 67 | }; |
| 68 | |
| 69 | template <typename T> |
| 70 | struct is_same<T, T> { |
| 71 | enum { value = 1 }; |
| 72 | }; |
| 73 | |
| 74 | // An argument visitor that converts an integer argument to T for printf, |
| 75 | // if T is an integral type. If T is void, the argument is converted to |
| 76 | // corresponding signed or unsigned type depending on the type specifier: |
| 77 | // 'd' and 'i' - signed, other - unsigned) |
| 78 | template <typename T = void> |
| 79 | class ArgConverter : public ArgVisitor<ArgConverter<T>, void> { |
| 80 | private: |
| 81 | internal::Arg &arg_; |
| 82 | wchar_t type_; |
| 83 | |
| 84 | FMT_DISALLOW_COPY_AND_ASSIGN(ArgConverter); |
| 85 | |
| 86 | public: |
| 87 | ArgConverter(internal::Arg &arg, wchar_t type) |
| 88 | : arg_(arg), type_(type) {} |
| 89 | |
| 90 | void visit_bool(bool value) { |
| 91 | if (type_ != 's') |
| 92 | visit_any_int(value); |
| 93 | } |
| 94 | |
| 95 | template <typename U> |
| 96 | void visit_any_int(U value) { |
| 97 | bool is_signed = type_ == 'd' || type_ == 'i'; |
| 98 | using internal::Arg; |
| 99 | typedef typename internal::Conditional< |
| 100 | is_same<T, void>::value, U, T>::type TargetType; |
| 101 | if (sizeof(TargetType) <= sizeof(int)) { |
| 102 | // Extra casts are used to silence warnings. |
| 103 | if (is_signed) { |
| 104 | arg_.type = Arg::INT; |
| 105 | arg_.int_value = static_cast<int>(static_cast<TargetType>(value)); |
| 106 | } else { |
| 107 | arg_.type = Arg::UINT; |
| 108 | typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned; |
| 109 | arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value)); |
| 110 | } |
| 111 | } else { |
| 112 | if (is_signed) { |
| 113 | arg_.type = Arg::LONG_LONG; |
| 114 | // 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. |
| 117 | arg_.long_long_value = static_cast<LongLong>(value); |
| 118 | } else { |
| 119 | arg_.type = Arg::ULONG_LONG; |
| 120 | arg_.ulong_long_value = |
| 121 | static_cast<typename internal::MakeUnsigned<U>::Type>(value); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | // Converts an integer argument to char for printf. |
| 128 | class CharConverter : public ArgVisitor<CharConverter, void> { |
| 129 | private: |
| 130 | internal::Arg &arg_; |
| 131 | |
| 132 | FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter); |
| 133 | |
| 134 | public: |
| 135 | explicit CharConverter(internal::Arg &arg) : arg_(arg) {} |
| 136 | |
| 137 | template <typename T> |
| 138 | void visit_any_int(T value) { |
| 139 | arg_.type = internal::Arg::CHAR; |
| 140 | arg_.int_value = static_cast<char>(value); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | // Checks if an argument is a valid printf width specifier and sets |
| 145 | // left alignment if it is negative. |
| 146 | class WidthHandler : public ArgVisitor<WidthHandler, unsigned> { |
| 147 | private: |
| 148 | FormatSpec &spec_; |
| 149 | |
| 150 | FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler); |
| 151 | |
| 152 | public: |
| 153 | explicit WidthHandler(FormatSpec &spec) : spec_(spec) {} |
| 154 | |
| 155 | void report_unhandled_arg() { |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 156 | FMT_THROW(format_error("width is not integer")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | template <typename T> |
| 160 | unsigned visit_any_int(T value) { |
| 161 | typedef typename internal::IntTraits<T>::MainType UnsignedType; |
| 162 | UnsignedType width = static_cast<UnsignedType>(value); |
| 163 | if (internal::is_negative(value)) { |
| 164 | spec_.align_ = ALIGN_LEFT; |
| 165 | width = 0 - width; |
| 166 | } |
| Victor Zverovich | e0d6f63 | 2016-06-15 06:29:47 -0700 | [diff] [blame] | 167 | unsigned int_max = std::numeric_limits<int>::max(); |
| 168 | if (width > int_max) |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 169 | FMT_THROW(format_error("number is too big")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 170 | return static_cast<unsigned>(width); |
| 171 | } |
| 172 | }; |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 173 | } // namespace internal |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 174 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 175 | /** |
| 176 | \rst |
| 177 | A ``printf`` argument formatter based on the `curiously recurring template |
| 178 | pattern <http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern>`_. |
| 179 | |
| 180 | To use `~fmt::BasicPrintfArgFormatter` define a subclass that implements some |
| 181 | or all of the visit methods with the same signatures as the methods in |
| 182 | `~fmt::ArgVisitor`, for example, `~fmt::ArgVisitor::visit_int()`. |
| 183 | Pass the subclass as the *Impl* template parameter. When a formatting |
| 184 | function processes an argument, it will dispatch to a visit method |
| 185 | specific to the argument type. For example, if the argument type is |
| 186 | ``double`` then the `~fmt::ArgVisitor::visit_double()` method of a subclass |
| 187 | will be called. If the subclass doesn't contain a method with this signature, |
| 188 | then a corresponding method of `~fmt::BasicPrintfArgFormatter` or its |
| 189 | superclass will be called. |
| 190 | \endrst |
| 191 | */ |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 192 | template <typename Impl, typename Char> |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 193 | class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 194 | private: |
| 195 | void write_null_pointer() { |
| 196 | this->spec().type_ = 0; |
| 197 | this->write("(nil)"); |
| 198 | } |
| 199 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 200 | typedef internal::ArgFormatterBase<Impl, Char> Base; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 201 | |
| 202 | public: |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 203 | /** |
| 204 | \rst |
| 205 | Constructs an argument formatter object. |
| 206 | *writer* is a reference to the output writer and *spec* contains format |
| 207 | specifier information for standard argument types. |
| 208 | \endrst |
| 209 | */ |
| 210 | BasicPrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec) |
| 211 | : internal::ArgFormatterBase<Impl, Char>(writer, spec) {} |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 212 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 213 | /** Formats an argument of type ``bool``. */ |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 214 | void visit_bool(bool value) { |
| 215 | FormatSpec &fmt_spec = this->spec(); |
| 216 | if (fmt_spec.type_ != 's') |
| 217 | return this->visit_any_int(value); |
| 218 | fmt_spec.type_ = 0; |
| 219 | this->write(value); |
| 220 | } |
| 221 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 222 | /** Formats a character. */ |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 223 | void visit_char(int value) { |
| 224 | const FormatSpec &fmt_spec = this->spec(); |
| 225 | BasicWriter<Char> &w = this->writer(); |
| 226 | if (fmt_spec.type_ && fmt_spec.type_ != 'c') |
| 227 | w.write_int(value, fmt_spec); |
| 228 | typedef typename BasicWriter<Char>::CharPtr CharPtr; |
| 229 | CharPtr out = CharPtr(); |
| 230 | if (fmt_spec.width_ > 1) { |
| 231 | Char fill = ' '; |
| 232 | out = w.grow_buffer(fmt_spec.width_); |
| 233 | if (fmt_spec.align_ != ALIGN_LEFT) { |
| 234 | std::fill_n(out, fmt_spec.width_ - 1, fill); |
| 235 | out += fmt_spec.width_ - 1; |
| 236 | } else { |
| 237 | std::fill_n(out + 1, fmt_spec.width_ - 1, fill); |
| 238 | } |
| 239 | } else { |
| 240 | out = w.grow_buffer(1); |
| 241 | } |
| 242 | *out = static_cast<Char>(value); |
| 243 | } |
| 244 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 245 | /** Formats a null-terminated C string. */ |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 246 | void visit_cstring(const char *value) { |
| 247 | if (value) |
| 248 | Base::visit_cstring(value); |
| 249 | else if (this->spec().type_ == 'p') |
| 250 | write_null_pointer(); |
| 251 | else |
| 252 | this->write("(null)"); |
| 253 | } |
| 254 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 255 | /** Formats a pointer. */ |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 256 | void visit_pointer(const void *value) { |
| 257 | if (value) |
| 258 | return Base::visit_pointer(value); |
| 259 | this->spec().type_ = 0; |
| 260 | write_null_pointer(); |
| 261 | } |
| 262 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 263 | /** Formats an argument of a custom (user-defined) type. */ |
| 264 | void visit_custom(internal::Arg::CustomValue c) { |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 265 | BasicFormatter<Char> formatter(basic_format_args<BasicFormatter<Char>>(), |
| 266 | this->writer()); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 267 | const Char format_str[] = {'}', 0}; |
| 268 | const Char *format = format_str; |
| 269 | c.format(&formatter, c.value, &format); |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | /** The default printf argument formatter. */ |
| 274 | template <typename Char> |
| 275 | class PrintfArgFormatter |
| 276 | : public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> { |
| 277 | public: |
| 278 | /** Constructs an argument formatter object. */ |
| 279 | PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s) |
| 280 | : BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {} |
| 281 | }; |
| 282 | |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 283 | /** This template formats data and writes the output to a writer. */ |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame^] | 284 | template <typename CharType, |
| 285 | typename ArgFormatter = PrintfArgFormatter<CharType> > |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 286 | class PrintfFormatter : |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame^] | 287 | private internal::FormatterBase<PrintfFormatter<CharType, ArgFormatter>> { |
| 288 | public: |
| 289 | /** The character type for the output. */ |
| 290 | typedef CharType Char; |
| 291 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 292 | private: |
| Victor Zverovich | ab05453 | 2016-07-20 08:21:13 -0700 | [diff] [blame] | 293 | BasicWriter<Char> &writer_; |
| 294 | |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 295 | typedef internal::FormatterBase<PrintfFormatter> Base; |
| 296 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 297 | void parse_flags(FormatSpec &spec, const Char *&s); |
| 298 | |
| 299 | // Returns the argument with specified index or, if arg_index is equal |
| 300 | // to the maximum unsigned value, the next argument. |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 301 | internal::Arg get_arg( |
| 302 | const Char *s, |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 303 | unsigned arg_index = (std::numeric_limits<unsigned>::max)()); |
| 304 | |
| 305 | // Parses argument index, flags and width and returns the argument index. |
| 306 | unsigned parse_header(const Char *&s, FormatSpec &spec); |
| 307 | |
| 308 | public: |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 309 | /** |
| 310 | \rst |
| Victor Zverovich | ab05453 | 2016-07-20 08:21:13 -0700 | [diff] [blame] | 311 | Constructs a ``PrintfFormatter`` object. References to the arguments and |
| 312 | the writer are stored in the formatter object so make sure they have |
| 313 | appropriate lifetimes. |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 314 | \endrst |
| 315 | */ |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 316 | explicit PrintfFormatter(basic_format_args<PrintfFormatter> args, |
| 317 | BasicWriter<Char> &w) |
| 318 | : Base(args), writer_(w) {} |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 319 | |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame^] | 320 | BasicWriter<Char> &writer() { return writer_; } |
| 321 | |
| Victor Zverovich | 355861f | 2016-07-20 08:26:14 -0700 | [diff] [blame] | 322 | /** Formats stored arguments and writes the output to the writer. */ |
| Victor Zverovich | ab05453 | 2016-07-20 08:21:13 -0700 | [diff] [blame] | 323 | FMT_API void format(BasicCStringRef<Char> format_str); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | template <typename Char, typename AF> |
| 327 | void PrintfFormatter<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) { |
| 328 | for (;;) { |
| 329 | switch (*s++) { |
| 330 | case '-': |
| 331 | spec.align_ = ALIGN_LEFT; |
| 332 | break; |
| 333 | case '+': |
| 334 | spec.flags_ |= SIGN_FLAG | PLUS_FLAG; |
| 335 | break; |
| 336 | case '0': |
| 337 | spec.fill_ = '0'; |
| 338 | break; |
| 339 | case ' ': |
| 340 | spec.flags_ |= SIGN_FLAG; |
| 341 | break; |
| 342 | case '#': |
| 343 | spec.flags_ |= HASH_FLAG; |
| 344 | break; |
| 345 | default: |
| 346 | --s; |
| 347 | return; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | template <typename Char, typename AF> |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 353 | internal::Arg PrintfFormatter<Char, AF>::get_arg(const Char *s, |
| 354 | unsigned arg_index) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 355 | (void)s; |
| 356 | const char *error = 0; |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 357 | internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() ? |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 358 | this->next_arg(error) : Base::get_arg(arg_index - 1, error); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 359 | if (error) |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 360 | FMT_THROW(format_error(!*s ? "invalid format string" : error)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 361 | return arg; |
| 362 | } |
| 363 | |
| 364 | template <typename Char, typename AF> |
| 365 | unsigned PrintfFormatter<Char, AF>::parse_header( |
| 366 | const Char *&s, FormatSpec &spec) { |
| 367 | unsigned arg_index = std::numeric_limits<unsigned>::max(); |
| 368 | Char c = *s; |
| 369 | if (c >= '0' && c <= '9') { |
| 370 | // Parse an argument index (if followed by '$') or a width possibly |
| 371 | // preceded with '0' flag(s). |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 372 | unsigned value = internal::parse_nonnegative_int(s); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 373 | if (*s == '$') { // value is an argument index |
| 374 | ++s; |
| 375 | arg_index = value; |
| 376 | } else { |
| 377 | if (c == '0') |
| 378 | spec.fill_ = '0'; |
| 379 | if (value != 0) { |
| 380 | // Nonzero value means that we parsed width and don't need to |
| 381 | // parse it or flags again, so return now. |
| 382 | spec.width_ = value; |
| 383 | return arg_index; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | parse_flags(spec, s); |
| 388 | // Parse width. |
| 389 | if (*s >= '0' && *s <= '9') { |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 390 | spec.width_ = internal::parse_nonnegative_int(s); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 391 | } else if (*s == '*') { |
| 392 | ++s; |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 393 | spec.width_ = internal::WidthHandler(spec).visit(get_arg(s)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 394 | } |
| 395 | return arg_index; |
| 396 | } |
| 397 | |
| 398 | template <typename Char, typename AF> |
| Victor Zverovich | ab05453 | 2016-07-20 08:21:13 -0700 | [diff] [blame] | 399 | void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 400 | const Char *start = format_str.c_str(); |
| 401 | const Char *s = start; |
| 402 | while (*s) { |
| 403 | Char c = *s++; |
| 404 | if (c != '%') continue; |
| 405 | if (*s == c) { |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 406 | this->write(writer_, start, s); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 407 | start = ++s; |
| 408 | continue; |
| 409 | } |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 410 | this->write(writer_, start, s - 1); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 411 | |
| 412 | FormatSpec spec; |
| 413 | spec.align_ = ALIGN_RIGHT; |
| 414 | |
| 415 | // Parse argument index, flags and width. |
| 416 | unsigned arg_index = parse_header(s, spec); |
| 417 | |
| 418 | // Parse precision. |
| 419 | if (*s == '.') { |
| 420 | ++s; |
| 421 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 422 | spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 423 | } else if (*s == '*') { |
| 424 | ++s; |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 425 | spec.precision_ = internal::PrecisionHandler().visit(get_arg(s)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 429 | using internal::Arg; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 430 | Arg arg = get_arg(s, arg_index); |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 431 | if (spec.flag(HASH_FLAG) && internal::IsZeroInt().visit(arg)) |
| 432 | spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 433 | if (spec.fill_ == '0') { |
| 434 | if (arg.type <= Arg::LAST_NUMERIC_TYPE) |
| 435 | spec.align_ = ALIGN_NUMERIC; |
| 436 | else |
| 437 | spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. |
| 438 | } |
| 439 | |
| 440 | // Parse length and convert the argument to the required type. |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 441 | using internal::ArgConverter; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 442 | switch (*s++) { |
| 443 | case 'h': |
| 444 | if (*s == 'h') |
| 445 | ArgConverter<signed char>(arg, *++s).visit(arg); |
| 446 | else |
| 447 | ArgConverter<short>(arg, *s).visit(arg); |
| 448 | break; |
| 449 | case 'l': |
| 450 | if (*s == 'l') |
| 451 | ArgConverter<fmt::LongLong>(arg, *++s).visit(arg); |
| 452 | else |
| 453 | ArgConverter<long>(arg, *s).visit(arg); |
| 454 | break; |
| 455 | case 'j': |
| 456 | ArgConverter<intmax_t>(arg, *s).visit(arg); |
| 457 | break; |
| 458 | case 'z': |
| 459 | ArgConverter<std::size_t>(arg, *s).visit(arg); |
| 460 | break; |
| 461 | case 't': |
| 462 | ArgConverter<std::ptrdiff_t>(arg, *s).visit(arg); |
| 463 | break; |
| 464 | case 'L': |
| 465 | // printf produces garbage when 'L' is omitted for long double, no |
| 466 | // need to do the same. |
| 467 | break; |
| 468 | default: |
| 469 | --s; |
| 470 | ArgConverter<void>(arg, *s).visit(arg); |
| 471 | } |
| 472 | |
| 473 | // Parse type. |
| 474 | if (!*s) |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 475 | FMT_THROW(format_error("invalid format string")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 476 | spec.type_ = static_cast<char>(*s++); |
| 477 | if (arg.type <= Arg::LAST_INTEGER_TYPE) { |
| 478 | // Normalize type. |
| 479 | switch (spec.type_) { |
| 480 | case 'i': case 'u': |
| 481 | spec.type_ = 'd'; |
| 482 | break; |
| 483 | case 'c': |
| 484 | // TODO: handle wchar_t |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 485 | internal::CharConverter(arg).visit(arg); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | start = s; |
| 491 | |
| 492 | // Format argument. |
| Victor Zverovich | ab05453 | 2016-07-20 08:21:13 -0700 | [diff] [blame] | 493 | AF(writer_, spec).visit(arg); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 494 | } |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 495 | this->write(writer_, start, s); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 496 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 497 | |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame^] | 498 | // Formats a value. |
| 499 | template <typename Char, typename T> |
| 500 | void format_value(PrintfFormatter<Char> &f, const Char *&, const T &value) { |
| 501 | internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; |
| 502 | f.writer() << internal::format_value(buffer, value); |
| 503 | } |
| 504 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 505 | template <typename Char> |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 506 | void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 507 | basic_format_args<PrintfFormatter<Char>> args) { |
| Victor Zverovich | ab05453 | 2016-07-20 08:21:13 -0700 | [diff] [blame] | 508 | PrintfFormatter<Char>(args, w).format(format); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 511 | inline std::string vsprintf(CStringRef format, |
| 512 | basic_format_args<PrintfFormatter<char>> args) { |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 513 | MemoryWriter w; |
| 514 | printf(w, format, args); |
| 515 | return w.str(); |
| 516 | } |
| 517 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 518 | /** |
| 519 | \rst |
| 520 | Formats arguments and returns the result as a string. |
| 521 | |
| 522 | **Example**:: |
| 523 | |
| 524 | std::string message = fmt::sprintf("The answer is %d", 42); |
| 525 | \endrst |
| 526 | */ |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 527 | template <typename... Args> |
| 528 | inline std::string sprintf(CStringRef format_str, const Args & ... args) { |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame^] | 529 | return vsprintf(format_str, make_format_args<PrintfFormatter<char>>(args...)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 530 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 531 | |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 532 | inline std::wstring vsprintf(WCStringRef format, |
| 533 | basic_format_args<PrintfFormatter<wchar_t>> args) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 534 | WMemoryWriter w; |
| 535 | printf(w, format, args); |
| 536 | return w.str(); |
| 537 | } |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 538 | |
| 539 | template <typename... Args> |
| 540 | inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) { |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame^] | 541 | auto vargs = make_format_args<PrintfFormatter<wchar_t>>(args...); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 542 | return vsprintf(format_str, vargs); |
| 543 | } |
| 544 | |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 545 | FMT_API int vfprintf(std::FILE *f, CStringRef format, |
| 546 | basic_format_args<PrintfFormatter<char>> args); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 547 | |
| 548 | /** |
| 549 | \rst |
| 550 | Prints formatted data to the file *f*. |
| 551 | |
| 552 | **Example**:: |
| 553 | |
| 554 | fmt::fprintf(stderr, "Don't %s!", "panic"); |
| 555 | \endrst |
| 556 | */ |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 557 | template <typename... Args> |
| 558 | inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) { |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 559 | auto vargs = make_format_args<PrintfFormatter<char>>(args...); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 560 | return vfprintf(f, format_str, vargs); |
| 561 | } |
| 562 | |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 563 | inline int vprintf(CStringRef format, |
| 564 | basic_format_args<PrintfFormatter<char>> args) { |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 565 | return vfprintf(stdout, format, args); |
| 566 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 567 | |
| 568 | /** |
| 569 | \rst |
| 570 | Prints formatted data to ``stdout``. |
| 571 | |
| 572 | **Example**:: |
| 573 | |
| 574 | fmt::printf("Elapsed time: %.2f seconds", 1.23); |
| 575 | \endrst |
| 576 | */ |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 577 | template <typename... Args> |
| 578 | inline int printf(CStringRef format_str, const Args & ... args) { |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame^] | 579 | return vprintf(format_str, make_format_args<PrintfFormatter<char>>(args...)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 580 | } |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 581 | |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 582 | inline int vfprintf(std::ostream &os, CStringRef format_str, |
| 583 | basic_format_args<PrintfFormatter<char>> args) { |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 584 | MemoryWriter w; |
| 585 | printf(w, format_str, args); |
| 586 | internal::write(os, w); |
| 587 | return static_cast<int>(w.size()); |
| 588 | } |
| Victor Zverovich | 9dbb60c | 2016-08-03 08:52:05 -0700 | [diff] [blame] | 589 | |
| 590 | /** |
| 591 | \rst |
| 592 | Prints formatted data to the stream *os*. |
| 593 | |
| 594 | **Example**:: |
| 595 | |
| 596 | fprintf(cerr, "Don't %s!", "panic"); |
| 597 | \endrst |
| 598 | */ |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 599 | template <typename... Args> |
| 600 | inline int fprintf(std::ostream &os, CStringRef format_str, |
| 601 | const Args & ... args) { |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame^] | 602 | auto vargs = make_format_args<PrintfFormatter<char>>(args...); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 603 | return vfprintf(os, format_str, vargs); |
| Victor Zverovich | 9dbb60c | 2016-08-03 08:52:05 -0700 | [diff] [blame] | 604 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 605 | } // namespace fmt |
| 606 | |
| 607 | #endif // FMT_PRINTF_H_ |