1 // Formatting library for C++ - legacy printf implementation 2 // 3 // Copyright (c) 2012 - 2016, Victor Zverovich 4 // All rights reserved. 5 // 6 // For the license information refer to format.h. 7 8 #ifndef FMT_PRINTF_H_ 9 #define FMT_PRINTF_H_ 10 11 #include <algorithm> // std::max 12 #include <limits> // std::numeric_limits 13 14 #include "format.h" 15 16 FMT_BEGIN_NAMESPACE 17 FMT_BEGIN_EXPORT 18 19 template <typename T> struct printf_formatter { 20 printf_formatter() = delete; 21 }; 22 23 template <typename Char> class basic_printf_context { 24 private: 25 detail::buffer_appender<Char> out_; 26 basic_format_args<basic_printf_context> args_; 27 28 static_assert(std::is_same<Char, char>::value || 29 std::is_same<Char, wchar_t>::value, 30 "Unsupported code unit type."); 31 32 public: 33 using char_type = Char; 34 using parse_context_type = basic_format_parse_context<Char>; 35 template <typename T> using formatter_type = printf_formatter<T>; 36 37 /** 38 \rst 39 Constructs a ``printf_context`` object. References to the arguments are 40 stored in the context object so make sure they have appropriate lifetimes. 41 \endrst 42 */ basic_printf_context(detail::buffer_appender<Char> out,basic_format_args<basic_printf_context> args)43 basic_printf_context(detail::buffer_appender<Char> out, 44 basic_format_args<basic_printf_context> args) 45 : out_(out), args_(args) {} 46 47 auto out() -> detail::buffer_appender<Char> { return out_; } advance_to(detail::buffer_appender<Char>)48 void advance_to(detail::buffer_appender<Char>) {} 49 50 auto locale() -> detail::locale_ref { return {}; } 51 52 auto arg(int id) const -> basic_format_arg<basic_printf_context> { 53 return args_.get(id); 54 } 55 on_error(const char * message)56 FMT_CONSTEXPR void on_error(const char* message) { 57 detail::error_handler().on_error(message); 58 } 59 }; 60 61 namespace detail { 62 63 // Checks if a value fits in int - used to avoid warnings about comparing 64 // signed and unsigned integers. 65 template <bool IsSigned> struct int_checker { 66 template <typename T> static auto fits_in_int(T value) -> bool { 67 unsigned max = max_value<int>(); 68 return value <= max; 69 } 70 static auto fits_in_int(bool) -> bool { return true; } 71 }; 72 73 template <> struct int_checker<true> { 74 template <typename T> static auto fits_in_int(T value) -> bool { 75 return value >= (std::numeric_limits<int>::min)() && 76 value <= max_value<int>(); 77 } 78 static auto fits_in_int(int) -> bool { return true; } 79 }; 80 81 struct printf_precision_handler { 82 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> 83 auto operator()(T value) -> int { 84 if (!int_checker<std::numeric_limits<T>::is_signed>::fits_in_int(value)) 85 throw_format_error("number is too big"); 86 return (std::max)(static_cast<int>(value), 0); 87 } 88 89 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> 90 auto operator()(T) -> int { 91 throw_format_error("precision is not integer"); 92 return 0; 93 } 94 }; 95 96 // An argument visitor that returns true iff arg is a zero integer. 97 struct is_zero_int { 98 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> 99 auto operator()(T value) -> bool { 100 return value == 0; 101 } 102 103 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> 104 auto operator()(T) -> bool { 105 return false; 106 } 107 }; 108 109 template <typename T> struct make_unsigned_or_bool : std::make_unsigned<T> {}; 110 111 template <> struct make_unsigned_or_bool<bool> { 112 using type = bool; 113 }; 114 115 template <typename T, typename Context> class arg_converter { 116 private: 117 using char_type = typename Context::char_type; 118 119 basic_format_arg<Context>& arg_; 120 char_type type_; 121 122 public: 123 arg_converter(basic_format_arg<Context>& arg, char_type type) 124 : arg_(arg), type_(type) {} 125 126 void operator()(bool value) { 127 if (type_ != 's') operator()<bool>(value); 128 } 129 130 template <typename U, FMT_ENABLE_IF(std::is_integral<U>::value)> 131 void operator()(U value) { 132 bool is_signed = type_ == 'd' || type_ == 'i'; 133 using target_type = conditional_t<std::is_same<T, void>::value, U, T>; 134 if (const_check(sizeof(target_type) <= sizeof(int))) { 135 // Extra casts are used to silence warnings. 136 if (is_signed) { 137 auto n = static_cast<int>(static_cast<target_type>(value)); 138 arg_ = detail::make_arg<Context>(n); 139 } else { 140 using unsigned_type = typename make_unsigned_or_bool<target_type>::type; 141 auto n = static_cast<unsigned>(static_cast<unsigned_type>(value)); 142 arg_ = detail::make_arg<Context>(n); 143 } 144 } else { 145 if (is_signed) { 146 // glibc's printf doesn't sign extend arguments of smaller types: 147 // std::printf("%lld", -42); // prints "4294967254" 148 // but we don't have to do the same because it's a UB. 149 auto n = static_cast<long long>(value); 150 arg_ = detail::make_arg<Context>(n); 151 } else { 152 auto n = static_cast<typename make_unsigned_or_bool<U>::type>(value); 153 arg_ = detail::make_arg<Context>(n); 154 } 155 } 156 } 157 158 template <typename U, FMT_ENABLE_IF(!std::is_integral<U>::value)> 159 void operator()(U) {} // No conversion needed for non-integral types. 160 }; 161 162 // Converts an integer argument to T for printf, if T is an integral type. 163 // If T is void, the argument is converted to corresponding signed or unsigned 164 // type depending on the type specifier: 'd' and 'i' - signed, other - 165 // unsigned). 166 template <typename T, typename Context, typename Char> 167 void convert_arg(basic_format_arg<Context>& arg, Char type) { 168 visit_format_arg(arg_converter<T, Context>(arg, type), arg); 169 } 170 171 // Converts an integer argument to char for printf. 172 template <typename Context> class char_converter { 173 private: 174 basic_format_arg<Context>& arg_; 175 176 public: 177 explicit char_converter(basic_format_arg<Context>& arg) : arg_(arg) {} 178 179 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> 180 void operator()(T value) { 181 auto c = static_cast<typename Context::char_type>(value); 182 arg_ = detail::make_arg<Context>(c); 183 } 184 185 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> 186 void operator()(T) {} // No conversion needed for non-integral types. 187 }; 188 189 // An argument visitor that return a pointer to a C string if argument is a 190 // string or null otherwise. 191 template <typename Char> struct get_cstring { 192 template <typename T> auto operator()(T) -> const Char* { return nullptr; } 193 auto operator()(const Char* s) -> const Char* { return s; } 194 }; 195 196 // Checks if an argument is a valid printf width specifier and sets 197 // left alignment if it is negative. 198 template <typename Char> class printf_width_handler { 199 private: 200 format_specs<Char>& specs_; 201 202 public: 203 explicit printf_width_handler(format_specs<Char>& specs) : specs_(specs) {} 204 205 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)> 206 auto operator()(T value) -> unsigned { 207 auto width = static_cast<uint32_or_64_or_128_t<T>>(value); 208 if (detail::is_negative(value)) { 209 specs_.align = align::left; 210 width = 0 - width; 211 } 212 unsigned int_max = max_value<int>(); 213 if (width > int_max) throw_format_error("number is too big"); 214 return static_cast<unsigned>(width); 215 } 216 217 template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)> 218 auto operator()(T) -> unsigned { 219 throw_format_error("width is not integer"); 220 return 0; 221 } 222 }; 223 224 // Workaround for a bug with the XL compiler when initializing 225 // printf_arg_formatter's base class. 226 template <typename Char> 227 auto make_arg_formatter(buffer_appender<Char> iter, format_specs<Char>& s) 228 -> arg_formatter<Char> { 229 return {iter, s, locale_ref()}; 230 } 231 232 // The ``printf`` argument formatter. 233 template <typename Char> 234 class printf_arg_formatter : public arg_formatter<Char> { 235 private: 236 using base = arg_formatter<Char>; 237 using context_type = basic_printf_context<Char>; 238 239 context_type& context_; 240 241 void write_null_pointer(bool is_string = false) { 242 auto s = this->specs; 243 s.type = presentation_type::none; 244 write_bytes(this->out, is_string ? "(null)" : "(nil)", s); 245 } 246 247 public: 248 printf_arg_formatter(buffer_appender<Char> iter, format_specs<Char>& s, 249 context_type& ctx) 250 : base(make_arg_formatter(iter, s)), context_(ctx) {} 251 252 void operator()(monostate value) { base::operator()(value); } 253 254 template <typename T, FMT_ENABLE_IF(detail::is_integral<T>::value)> 255 void operator()(T value) { 256 // MSVC2013 fails to compile separate overloads for bool and Char so use 257 // std::is_same instead. 258 if (!std::is_same<T, Char>::value) { 259 base::operator()(value); 260 return; 261 } 262 format_specs<Char> fmt_specs = this->specs; 263 if (fmt_specs.type != presentation_type::none && 264 fmt_specs.type != presentation_type::chr) { 265 return (*this)(static_cast<int>(value)); 266 } 267 fmt_specs.sign = sign::none; 268 fmt_specs.alt = false; 269 fmt_specs.fill[0] = ' '; // Ignore '0' flag for char types. 270 // align::numeric needs to be overwritten here since the '0' flag is 271 // ignored for non-numeric types 272 if (fmt_specs.align == align::none || fmt_specs.align == align::numeric) 273 fmt_specs.align = align::right; 274 write<Char>(this->out, static_cast<Char>(value), fmt_specs); 275 } 276 277 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)> 278 void operator()(T value) { 279 base::operator()(value); 280 } 281 282 /** Formats a null-terminated C string. */ 283 void operator()(const char* value) { 284 if (value) 285 base::operator()(value); 286 else 287 write_null_pointer(this->specs.type != presentation_type::pointer); 288 } 289 290 /** Formats a null-terminated wide C string. */ 291 void operator()(const wchar_t* value) { 292 if (value) 293 base::operator()(value); 294 else 295 write_null_pointer(this->specs.type != presentation_type::pointer); 296 } 297 298 void operator()(basic_string_view<Char> value) { base::operator()(value); } 299 300 /** Formats a pointer. */ 301 void operator()(const void* value) { 302 if (value) 303 base::operator()(value); 304 else 305 write_null_pointer(); 306 } 307 308 /** Formats an argument of a custom (user-defined) type. */ 309 void operator()(typename basic_format_arg<context_type>::handle handle) { 310 auto parse_ctx = basic_format_parse_context<Char>({}); 311 handle.format(parse_ctx, context_); 312 } 313 }; 314 315 template <typename Char> 316 void parse_flags(format_specs<Char>& specs, const Char*& it, const Char* end) { 317 for (; it != end; ++it) { 318 switch (*it) { 319 case '-': 320 specs.align = align::left; 321 break; 322 case '+': 323 specs.sign = sign::plus; 324 break; 325 case '0': 326 specs.fill[0] = '0'; 327 break; 328 case ' ': 329 if (specs.sign != sign::plus) specs.sign = sign::space; 330 break; 331 case '#': 332 specs.alt = true; 333 break; 334 default: 335 return; 336 } 337 } 338 } 339 340 template <typename Char, typename GetArg> 341 auto parse_header(const Char*& it, const Char* end, format_specs<Char>& specs, 342 GetArg get_arg) -> int { 343 int arg_index = -1; 344 Char c = *it; 345 if (c >= '0' && c <= '9') { 346 // Parse an argument index (if followed by '$') or a width possibly 347 // preceded with '0' flag(s). 348 int value = parse_nonnegative_int(it, end, -1); 349 if (it != end && *it == '$') { // value is an argument index 350 ++it; 351 arg_index = value != -1 ? value : max_value<int>(); 352 } else { 353 if (c == '0') specs.fill[0] = '0'; 354 if (value != 0) { 355 // Nonzero value means that we parsed width and don't need to 356 // parse it or flags again, so return now. 357 if (value == -1) throw_format_error("number is too big"); 358 specs.width = value; 359 return arg_index; 360 } 361 } 362 } 363 parse_flags(specs, it, end); 364 // Parse width. 365 if (it != end) { 366 if (*it >= '0' && *it <= '9') { 367 specs.width = parse_nonnegative_int(it, end, -1); 368 if (specs.width == -1) throw_format_error("number is too big"); 369 } else if (*it == '*') { 370 ++it; 371 specs.width = static_cast<int>(visit_format_arg( 372 detail::printf_width_handler<Char>(specs), get_arg(-1))); 373 } 374 } 375 return arg_index; 376 } 377 378 inline auto parse_printf_presentation_type(char c, type t) 379 -> presentation_type { 380 using pt = presentation_type; 381 constexpr auto integral_set = sint_set | uint_set | bool_set | char_set; 382 switch (c) { 383 case 'd': 384 return in(t, integral_set) ? pt::dec : pt::none; 385 case 'o': 386 return in(t, integral_set) ? pt::oct : pt::none; 387 case 'x': 388 return in(t, integral_set) ? pt::hex_lower : pt::none; 389 case 'X': 390 return in(t, integral_set) ? pt::hex_upper : pt::none; 391 case 'a': 392 return in(t, float_set) ? pt::hexfloat_lower : pt::none; 393 case 'A': 394 return in(t, float_set) ? pt::hexfloat_upper : pt::none; 395 case 'e': 396 return in(t, float_set) ? pt::exp_lower : pt::none; 397 case 'E': 398 return in(t, float_set) ? pt::exp_upper : pt::none; 399 case 'f': 400 return in(t, float_set) ? pt::fixed_lower : pt::none; 401 case 'F': 402 return in(t, float_set) ? pt::fixed_upper : pt::none; 403 case 'g': 404 return in(t, float_set) ? pt::general_lower : pt::none; 405 case 'G': 406 return in(t, float_set) ? pt::general_upper : pt::none; 407 case 'c': 408 return in(t, integral_set) ? pt::chr : pt::none; 409 case 's': 410 return in(t, string_set | cstring_set) ? pt::string : pt::none; 411 case 'p': 412 return in(t, pointer_set | cstring_set) ? pt::pointer : pt::none; 413 default: 414 return pt::none; 415 } 416 } 417 418 template <typename Char, typename Context> 419 void vprintf(buffer<Char>& buf, basic_string_view<Char> format, 420 basic_format_args<Context> args) { 421 using iterator = buffer_appender<Char>; 422 auto out = iterator(buf); 423 auto context = basic_printf_context<Char>(out, args); 424 auto parse_ctx = basic_format_parse_context<Char>(format); 425 426 // Returns the argument with specified index or, if arg_index is -1, the next 427 // argument. 428 auto get_arg = [&](int arg_index) { 429 if (arg_index < 0) 430 arg_index = parse_ctx.next_arg_id(); 431 else 432 parse_ctx.check_arg_id(--arg_index); 433 return detail::get_arg(context, arg_index); 434 }; 435 436 const Char* start = parse_ctx.begin(); 437 const Char* end = parse_ctx.end(); 438 auto it = start; 439 while (it != end) { 440 if (!find<false, Char>(it, end, '%', it)) { 441 it = end; // find leaves it == nullptr if it doesn't find '%'. 442 break; 443 } 444 Char c = *it++; 445 if (it != end && *it == c) { 446 write(out, basic_string_view<Char>(start, to_unsigned(it - start))); 447 start = ++it; 448 continue; 449 } 450 write(out, basic_string_view<Char>(start, to_unsigned(it - 1 - start))); 451 452 auto specs = format_specs<Char>(); 453 specs.align = align::right; 454 455 // Parse argument index, flags and width. 456 int arg_index = parse_header(it, end, specs, get_arg); 457 if (arg_index == 0) throw_format_error("argument not found"); 458 459 // Parse precision. 460 if (it != end && *it == '.') { 461 ++it; 462 c = it != end ? *it : 0; 463 if ('0' <= c && c <= '9') { 464 specs.precision = parse_nonnegative_int(it, end, 0); 465 } else if (c == '*') { 466 ++it; 467 specs.precision = static_cast<int>( 468 visit_format_arg(printf_precision_handler(), get_arg(-1))); 469 } else { 470 specs.precision = 0; 471 } 472 } 473 474 auto arg = get_arg(arg_index); 475 // For d, i, o, u, x, and X conversion specifiers, if a precision is 476 // specified, the '0' flag is ignored 477 if (specs.precision >= 0 && arg.is_integral()) { 478 // Ignore '0' for non-numeric types or if '-' present. 479 specs.fill[0] = ' '; 480 } 481 if (specs.precision >= 0 && arg.type() == type::cstring_type) { 482 auto str = visit_format_arg(get_cstring<Char>(), arg); 483 auto str_end = str + specs.precision; 484 auto nul = std::find(str, str_end, Char()); 485 auto sv = basic_string_view<Char>( 486 str, to_unsigned(nul != str_end ? nul - str : specs.precision)); 487 arg = make_arg<basic_printf_context<Char>>(sv); 488 } 489 if (specs.alt && visit_format_arg(is_zero_int(), arg)) specs.alt = false; 490 if (specs.fill[0] == '0') { 491 if (arg.is_arithmetic() && specs.align != align::left) 492 specs.align = align::numeric; 493 else 494 specs.fill[0] = ' '; // Ignore '0' flag for non-numeric types or if '-' 495 // flag is also present. 496 } 497 498 // Parse length and convert the argument to the required type. 499 c = it != end ? *it++ : 0; 500 Char t = it != end ? *it : 0; 501 switch (c) { 502 case 'h': 503 if (t == 'h') { 504 ++it; 505 t = it != end ? *it : 0; 506 convert_arg<signed char>(arg, t); 507 } else { 508 convert_arg<short>(arg, t); 509 } 510 break; 511 case 'l': 512 if (t == 'l') { 513 ++it; 514 t = it != end ? *it : 0; 515 convert_arg<long long>(arg, t); 516 } else { 517 convert_arg<long>(arg, t); 518 } 519 break; 520 case 'j': 521 convert_arg<intmax_t>(arg, t); 522 break; 523 case 'z': 524 convert_arg<size_t>(arg, t); 525 break; 526 case 't': 527 convert_arg<std::ptrdiff_t>(arg, t); 528 break; 529 case 'L': 530 // printf produces garbage when 'L' is omitted for long double, no 531 // need to do the same. 532 break; 533 default: 534 --it; 535 convert_arg<void>(arg, c); 536 } 537 538 // Parse type. 539 if (it == end) throw_format_error("invalid format string"); 540 char type = static_cast<char>(*it++); 541 if (arg.is_integral()) { 542 // Normalize type. 543 switch (type) { 544 case 'i': 545 case 'u': 546 type = 'd'; 547 break; 548 case 'c': 549 visit_format_arg(char_converter<basic_printf_context<Char>>(arg), arg); 550 break; 551 } 552 } 553 specs.type = parse_printf_presentation_type(type, arg.type()); 554 if (specs.type == presentation_type::none) 555 throw_format_error("invalid format specifier"); 556 557 start = it; 558 559 // Format argument. 560 visit_format_arg(printf_arg_formatter<Char>(out, specs, context), arg); 561 } 562 write(out, basic_string_view<Char>(start, to_unsigned(it - start))); 563 } 564 } // namespace detail 565 566 using printf_context = basic_printf_context<char>; 567 using wprintf_context = basic_printf_context<wchar_t>; 568 569 using printf_args = basic_format_args<printf_context>; 570 using wprintf_args = basic_format_args<wprintf_context>; 571 572 /** 573 \rst 574 Constructs an `~fmt::format_arg_store` object that contains references to 575 arguments and can be implicitly converted to `~fmt::printf_args`. 576 \endrst 577 */ 578 template <typename... T> 579 inline auto make_printf_args(const T&... args) 580 -> format_arg_store<printf_context, T...> { 581 return {args...}; 582 } 583 584 // DEPRECATED! 585 template <typename... T> 586 inline auto make_wprintf_args(const T&... args) 587 -> format_arg_store<wprintf_context, T...> { 588 return {args...}; 589 } 590 591 template <typename Char> 592 inline auto vsprintf( 593 basic_string_view<Char> fmt, 594 basic_format_args<basic_printf_context<type_identity_t<Char>>> args) 595 -> std::basic_string<Char> { 596 auto buf = basic_memory_buffer<Char>(); 597 detail::vprintf(buf, fmt, args); 598 return to_string(buf); 599 } 600 601 /** 602 \rst 603 Formats arguments and returns the result as a string. 604 605 **Example**:: 606 607 std::string message = fmt::sprintf("The answer is %d", 42); 608 \endrst 609 */ 610 template <typename S, typename... T, 611 typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>> 612 inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string<Char> { 613 return vsprintf(detail::to_string_view(fmt), 614 fmt::make_format_args<basic_printf_context<Char>>(args...)); 615 } 616 617 template <typename Char> 618 inline auto vfprintf( 619 std::FILE* f, basic_string_view<Char> fmt, 620 basic_format_args<basic_printf_context<type_identity_t<Char>>> args) 621 -> int { 622 auto buf = basic_memory_buffer<Char>(); 623 detail::vprintf(buf, fmt, args); 624 size_t size = buf.size(); 625 return std::fwrite(buf.data(), sizeof(Char), size, f) < size 626 ? -1 627 : static_cast<int>(size); 628 } 629 630 /** 631 \rst 632 Prints formatted data to the file *f*. 633 634 **Example**:: 635 636 fmt::fprintf(stderr, "Don't %s!", "panic"); 637 \endrst 638 */ 639 template <typename S, typename... T, typename Char = char_t<S>> 640 inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int { 641 return vfprintf(f, detail::to_string_view(fmt), 642 fmt::make_format_args<basic_printf_context<Char>>(args...)); 643 } 644 645 template <typename Char> 646 FMT_DEPRECATED inline auto vprintf( 647 basic_string_view<Char> fmt, 648 basic_format_args<basic_printf_context<type_identity_t<Char>>> args) 649 -> int { 650 return vfprintf(stdout, fmt, args); 651 } 652 653 /** 654 \rst 655 Prints formatted data to ``stdout``. 656 657 **Example**:: 658 659 fmt::printf("Elapsed time: %.2f seconds", 1.23); 660 \endrst 661 */ 662 template <typename... T> 663 inline auto printf(string_view fmt, const T&... args) -> int { 664 return vfprintf(stdout, fmt, make_printf_args(args...)); 665 } 666 template <typename... T> 667 FMT_DEPRECATED inline auto printf(basic_string_view<wchar_t> fmt, 668 const T&... args) -> int { 669 return vfprintf(stdout, fmt, make_wprintf_args(args...)); 670 } 671 672 FMT_END_EXPORT 673 FMT_END_NAMESPACE 674 675 #endif // FMT_PRINTF_H_ 676