| 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 | |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 43 | class PrintfPrecisionHandler { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 44 | public: |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 45 | template <typename T> |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 46 | typename std::enable_if<std::is_integral<T>::value, int>::type |
| 47 | operator()(T value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 48 | if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value)) |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 49 | FMT_THROW(format_error("number is too big")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 50 | return static_cast<int>(value); |
| 51 | } |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 52 | |
| 53 | template <typename T> |
| 54 | typename std::enable_if<!std::is_integral<T>::value, int>::type |
| 55 | operator()(T) { |
| 56 | FMT_THROW(format_error("precision is not integer")); |
| 57 | return 0; |
| 58 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 61 | // An argument visitor that returns true iff arg is a zero integer. |
| 62 | class IsZeroInt { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 63 | public: |
| 64 | template <typename T> |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 65 | typename std::enable_if<std::is_integral<T>::value, bool>::type |
| 66 | operator()(T value) { return value == 0; } |
| 67 | |
| 68 | template <typename T> |
| 69 | typename std::enable_if<!std::is_integral<T>::value, bool>::type |
| 70 | operator()(T value) { return false; } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | template <typename T, typename U> |
| 74 | struct is_same { |
| 75 | enum { value = 0 }; |
| 76 | }; |
| 77 | |
| 78 | template <typename T> |
| 79 | struct is_same<T, T> { |
| 80 | enum { value = 1 }; |
| 81 | }; |
| 82 | |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 83 | template <typename T, typename Context> |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 84 | class ArgConverter { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 85 | private: |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 86 | typedef typename Context::char_type Char; |
| 87 | |
| Victor Zverovich | 7ae8bd7 | 2017-02-05 06:09:06 -0800 | [diff] [blame] | 88 | basic_arg<Context> &arg_; |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 89 | typename Context::char_type type_; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 90 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 91 | public: |
| Victor Zverovich | 7ae8bd7 | 2017-02-05 06:09:06 -0800 | [diff] [blame] | 92 | ArgConverter(basic_arg<Context> &arg, Char type) |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 93 | : arg_(arg), type_(type) {} |
| 94 | |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 95 | void operator()(bool value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 96 | if (type_ != 's') |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 97 | operator()<bool>(value); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | template <typename U> |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 101 | typename std::enable_if<std::is_integral<U>::value>::type |
| 102 | operator()(U value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 103 | bool is_signed = type_ == 'd' || type_ == 'i'; |
| Victor Zverovich | 6a2ff28 | 2017-02-19 06:46:51 -0800 | [diff] [blame] | 104 | typedef typename internal::conditional< |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 105 | is_same<T, void>::value, U, T>::type TargetType; |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 106 | typedef basic_context<Char> context; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 107 | if (sizeof(TargetType) <= sizeof(int)) { |
| 108 | // Extra casts are used to silence warnings. |
| 109 | if (is_signed) { |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 110 | arg_ = internal::make_arg<Context>( |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 111 | static_cast<int>(static_cast<TargetType>(value))); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 112 | } else { |
| Victor Zverovich | 6a2ff28 | 2017-02-19 06:46:51 -0800 | [diff] [blame] | 113 | typedef typename internal::make_unsigned<TargetType>::type Unsigned; |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 114 | arg_ = internal::make_arg<Context>( |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 115 | static_cast<unsigned>(static_cast<Unsigned>(value))); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 116 | } |
| 117 | } else { |
| 118 | if (is_signed) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 119 | // glibc's printf doesn't sign extend arguments of smaller types: |
| 120 | // std::printf("%lld", -42); // prints "4294967254" |
| 121 | // but we don't have to do the same because it's a UB. |
| Victor Zverovich | 50e7167 | 2017-02-18 06:52:52 -0800 | [diff] [blame] | 122 | arg_ = internal::make_arg<Context>(static_cast<long_long>(value)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 123 | } else { |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 124 | arg_ = internal::make_arg<Context>( |
| Victor Zverovich | 6a2ff28 | 2017-02-19 06:46:51 -0800 | [diff] [blame] | 125 | static_cast<typename internal::make_unsigned<U>::type>(value)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 129 | |
| 130 | template <typename U> |
| 131 | typename std::enable_if<!std::is_integral<U>::value>::type |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 132 | operator()(U value) { |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 133 | // No coversion needed for non-integral types. |
| 134 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 137 | // Converts an integer argument to T for printf, if T is an integral type. |
| 138 | // If T is void, the argument is converted to corresponding signed or unsigned |
| 139 | // type depending on the type specifier: 'd' and 'i' - signed, other - |
| 140 | // unsigned). |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 141 | template <typename T, typename Context, typename Char> |
| Victor Zverovich | 7ae8bd7 | 2017-02-05 06:09:06 -0800 | [diff] [blame] | 142 | void convert_arg(basic_arg<Context> &arg, Char type) { |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 143 | visit(ArgConverter<T, Context>(arg, type), arg); |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 146 | // Converts an integer argument to char for printf. |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 147 | template <typename Context> |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 148 | class CharConverter { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 149 | private: |
| Victor Zverovich | 7ae8bd7 | 2017-02-05 06:09:06 -0800 | [diff] [blame] | 150 | basic_arg<Context> &arg_; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 151 | |
| 152 | FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter); |
| 153 | |
| 154 | public: |
| Victor Zverovich | 7ae8bd7 | 2017-02-05 06:09:06 -0800 | [diff] [blame] | 155 | explicit CharConverter(basic_arg<Context> &arg) : arg_(arg) {} |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 156 | |
| 157 | template <typename T> |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 158 | typename std::enable_if<std::is_integral<T>::value>::type |
| 159 | operator()(T value) { |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 160 | arg_ = internal::make_arg<Context>(static_cast<char>(value)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 161 | } |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 162 | |
| 163 | template <typename T> |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 164 | typename std::enable_if<!std::is_integral<T>::value>::type operator()(T) { |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 165 | // No coversion needed for non-integral types. |
| 166 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | // Checks if an argument is a valid printf width specifier and sets |
| 170 | // left alignment if it is negative. |
| Victor Zverovich | bf0f107 | 2017-01-28 13:17:47 +0000 | [diff] [blame] | 171 | template <typename Char> |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 172 | class PrintfWidthHandler { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 173 | private: |
| Victor Zverovich | bf0f107 | 2017-01-28 13:17:47 +0000 | [diff] [blame] | 174 | typedef basic_format_specs<Char> format_specs; |
| 175 | |
| Victor Zverovich | 296e9ca | 2017-01-28 12:51:35 +0000 | [diff] [blame] | 176 | format_specs &spec_; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 177 | |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 178 | FMT_DISALLOW_COPY_AND_ASSIGN(PrintfWidthHandler); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 179 | |
| 180 | public: |
| Victor Zverovich | 296e9ca | 2017-01-28 12:51:35 +0000 | [diff] [blame] | 181 | explicit PrintfWidthHandler(format_specs &spec) : spec_(spec) {} |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 182 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 183 | template <typename T> |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 184 | typename std::enable_if<std::is_integral<T>::value, unsigned>::type |
| 185 | operator()(T value) { |
| Victor Zverovich | 6a2ff28 | 2017-02-19 06:46:51 -0800 | [diff] [blame] | 186 | typedef typename internal::int_traits<T>::main_type UnsignedType; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 187 | UnsignedType width = static_cast<UnsignedType>(value); |
| 188 | if (internal::is_negative(value)) { |
| 189 | spec_.align_ = ALIGN_LEFT; |
| 190 | width = 0 - width; |
| 191 | } |
| Victor Zverovich | e0d6f63 | 2016-06-15 06:29:47 -0700 | [diff] [blame] | 192 | unsigned int_max = std::numeric_limits<int>::max(); |
| 193 | if (width > int_max) |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 194 | FMT_THROW(format_error("number is too big")); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 195 | return static_cast<unsigned>(width); |
| 196 | } |
| Victor Zverovich | e2dfd39 | 2016-11-19 09:29:09 -0800 | [diff] [blame] | 197 | |
| 198 | template <typename T> |
| 199 | typename std::enable_if<!std::is_integral<T>::value, unsigned>::type |
| 200 | operator()(T value) { |
| 201 | FMT_THROW(format_error("width is not integer")); |
| 202 | return 0; |
| 203 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 204 | }; |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 205 | } // namespace internal |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 206 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 207 | /** |
| 208 | \rst |
| Victor Zverovich | d58cc8a | 2016-11-20 07:42:38 -0800 | [diff] [blame] | 209 | The ``printf`` argument formatter. |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 210 | \endrst |
| 211 | */ |
| Victor Zverovich | d58cc8a | 2016-11-20 07:42:38 -0800 | [diff] [blame] | 212 | template <typename Char> |
| Victor Zverovich | c333dca | 2017-02-19 08:41:38 -0800 | [diff] [blame] | 213 | class printf_arg_formatter : public internal::arg_formatter_base<Char> { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 214 | private: |
| 215 | void write_null_pointer() { |
| 216 | this->spec().type_ = 0; |
| 217 | this->write("(nil)"); |
| 218 | } |
| 219 | |
| Victor Zverovich | c333dca | 2017-02-19 08:41:38 -0800 | [diff] [blame] | 220 | typedef internal::arg_formatter_base<Char> Base; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 221 | |
| 222 | public: |
| Victor Zverovich | bf0f107 | 2017-01-28 13:17:47 +0000 | [diff] [blame] | 223 | typedef typename Base::format_specs format_specs; |
| 224 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 225 | /** |
| 226 | \rst |
| 227 | Constructs an argument formatter object. |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 228 | *buffer* is a reference to the output buffer and *spec* contains format |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 229 | specifier information for standard argument types. |
| 230 | \endrst |
| 231 | */ |
| Victor Zverovich | 4ec8860 | 2017-02-18 07:46:32 -0800 | [diff] [blame] | 232 | printf_arg_formatter(basic_buffer<Char> &buffer, format_specs &spec) |
| Victor Zverovich | c333dca | 2017-02-19 08:41:38 -0800 | [diff] [blame] | 233 | : internal::arg_formatter_base<Char>(buffer, spec) {} |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 234 | |
| Victor Zverovich | 95a53e1 | 2016-11-19 07:39:07 -0800 | [diff] [blame] | 235 | using Base::operator(); |
| 236 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 237 | /** Formats an argument of type ``bool``. */ |
| Victor Zverovich | 95a53e1 | 2016-11-19 07:39:07 -0800 | [diff] [blame] | 238 | void operator()(bool value) { |
| Victor Zverovich | 296e9ca | 2017-01-28 12:51:35 +0000 | [diff] [blame] | 239 | format_specs &fmt_spec = this->spec(); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 240 | if (fmt_spec.type_ != 's') |
| Victor Zverovich | d58cc8a | 2016-11-20 07:42:38 -0800 | [diff] [blame] | 241 | return (*this)(value ? 1 : 0); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 242 | fmt_spec.type_ = 0; |
| 243 | this->write(value); |
| 244 | } |
| 245 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 246 | /** Formats a character. */ |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 247 | void operator()(Char value) { |
| Victor Zverovich | 296e9ca | 2017-01-28 12:51:35 +0000 | [diff] [blame] | 248 | const format_specs &fmt_spec = this->spec(); |
| Victor Zverovich | 8428621 | 2016-12-30 12:11:27 -0800 | [diff] [blame] | 249 | basic_writer<Char> &w = this->writer(); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 250 | if (fmt_spec.type_ && fmt_spec.type_ != 'c') |
| 251 | w.write_int(value, fmt_spec); |
| Victor Zverovich | 8428621 | 2016-12-30 12:11:27 -0800 | [diff] [blame] | 252 | typedef typename basic_writer<Char>::CharPtr CharPtr; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 253 | CharPtr out = CharPtr(); |
| 254 | if (fmt_spec.width_ > 1) { |
| 255 | Char fill = ' '; |
| 256 | out = w.grow_buffer(fmt_spec.width_); |
| 257 | if (fmt_spec.align_ != ALIGN_LEFT) { |
| 258 | std::fill_n(out, fmt_spec.width_ - 1, fill); |
| 259 | out += fmt_spec.width_ - 1; |
| 260 | } else { |
| 261 | std::fill_n(out + 1, fmt_spec.width_ - 1, fill); |
| 262 | } |
| 263 | } else { |
| 264 | out = w.grow_buffer(1); |
| 265 | } |
| 266 | *out = static_cast<Char>(value); |
| 267 | } |
| 268 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 269 | /** Formats a null-terminated C string. */ |
| Victor Zverovich | 95a53e1 | 2016-11-19 07:39:07 -0800 | [diff] [blame] | 270 | void operator()(const char *value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 271 | if (value) |
| Victor Zverovich | 95a53e1 | 2016-11-19 07:39:07 -0800 | [diff] [blame] | 272 | Base::operator()(value); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 273 | else if (this->spec().type_ == 'p') |
| 274 | write_null_pointer(); |
| 275 | else |
| 276 | this->write("(null)"); |
| 277 | } |
| 278 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 279 | /** Formats a pointer. */ |
| Victor Zverovich | 95a53e1 | 2016-11-19 07:39:07 -0800 | [diff] [blame] | 280 | void operator()(const void *value) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 281 | if (value) |
| Victor Zverovich | 95a53e1 | 2016-11-19 07:39:07 -0800 | [diff] [blame] | 282 | return Base::operator()(value); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 283 | this->spec().type_ = 0; |
| 284 | write_null_pointer(); |
| 285 | } |
| 286 | |
| Victor Zverovich | 6ee9f2e | 2016-07-21 06:59:28 -0700 | [diff] [blame] | 287 | /** Formats an argument of a custom (user-defined) type. */ |
| Victor Zverovich | 6a2ff28 | 2017-02-19 06:46:51 -0800 | [diff] [blame] | 288 | void operator()(internal::custom_value<Char> c) { |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 289 | const Char format_str[] = {'}', '\0'}; |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 290 | auto args = basic_args<basic_context<Char>>(); |
| 291 | basic_context<Char> ctx(format_str, args); |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 292 | c.format(this->writer().buffer(), c.value, &ctx); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 293 | } |
| 294 | }; |
| 295 | |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 296 | /** This template formats data and writes the output to a writer. */ |
| Victor Zverovich | be61320 | 2016-10-22 08:19:19 -0700 | [diff] [blame] | 297 | template <typename Char, |
| Victor Zverovich | 4ec8860 | 2017-02-18 07:46:32 -0800 | [diff] [blame] | 298 | typename ArgFormatter = printf_arg_formatter<Char> > |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 299 | class printf_context : |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 300 | private internal::context_base< |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 301 | Char, printf_context<Char, ArgFormatter>> { |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame] | 302 | public: |
| 303 | /** The character type for the output. */ |
| Victor Zverovich | be61320 | 2016-10-22 08:19:19 -0700 | [diff] [blame] | 304 | typedef Char char_type; |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame] | 305 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 306 | private: |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 307 | typedef internal::context_base<Char, printf_context> Base; |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 308 | typedef typename Base::format_arg format_arg; |
| Victor Zverovich | bf0f107 | 2017-01-28 13:17:47 +0000 | [diff] [blame] | 309 | typedef basic_format_specs<Char> format_specs; |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 310 | typedef typename Base::iterator iterator; |
| Victor Zverovich | dafbec7 | 2016-10-07 08:37:06 -0700 | [diff] [blame] | 311 | |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 312 | void parse_flags(format_specs &spec, iterator &it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 313 | |
| 314 | // Returns the argument with specified index or, if arg_index is equal |
| 315 | // to the maximum unsigned value, the next argument. |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 316 | format_arg get_arg( |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 317 | iterator it, |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 318 | unsigned arg_index = (std::numeric_limits<unsigned>::max)()); |
| 319 | |
| 320 | // Parses argument index, flags and width and returns the argument index. |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 321 | unsigned parse_header(iterator &it, format_specs &spec); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 322 | |
| 323 | public: |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 324 | /** |
| 325 | \rst |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 326 | Constructs a ``printf_context`` object. References to the arguments and |
| 327 | the writer are stored in the context object so make sure they have |
| Victor Zverovich | ab05453 | 2016-07-20 08:21:13 -0700 | [diff] [blame] | 328 | appropriate lifetimes. |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 329 | \endrst |
| 330 | */ |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 331 | explicit printf_context(basic_string_view<Char> format_str, |
| Victor Zverovich | 7ae8bd7 | 2017-02-05 06:09:06 -0800 | [diff] [blame] | 332 | basic_args<printf_context> args) |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 333 | : Base(format_str, args) {} |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 334 | |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 335 | /** Formats stored arguments and writes the output to the buffer. */ |
| 336 | FMT_API void format(basic_buffer<Char> &buffer); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | template <typename Char, typename AF> |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 340 | void printf_context<Char, AF>::parse_flags(format_specs &spec, iterator &it) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 341 | for (;;) { |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 342 | switch (*it++) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 343 | case '-': |
| 344 | spec.align_ = ALIGN_LEFT; |
| 345 | break; |
| 346 | case '+': |
| 347 | spec.flags_ |= SIGN_FLAG | PLUS_FLAG; |
| 348 | break; |
| 349 | case '0': |
| 350 | spec.fill_ = '0'; |
| 351 | break; |
| 352 | case ' ': |
| 353 | spec.flags_ |= SIGN_FLAG; |
| 354 | break; |
| 355 | case '#': |
| 356 | spec.flags_ |= HASH_FLAG; |
| 357 | break; |
| 358 | default: |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 359 | --it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 360 | return; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | template <typename Char, typename AF> |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 366 | typename printf_context<Char, AF>::format_arg printf_context<Char, AF>::get_arg( |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 367 | iterator it, unsigned arg_index) { |
| 368 | (void)it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 369 | const char *error = 0; |
| Victor Zverovich | 1102d46 | 2017-07-30 08:37:26 -0700 | [diff] [blame^] | 370 | format_arg arg; |
| 371 | if (arg_index == std::numeric_limits<unsigned>::max()) { |
| 372 | arg_index = this->next_arg_index(error); |
| 373 | if (!error) |
| 374 | arg = this->do_get_arg(arg_index, error); |
| 375 | } else { |
| 376 | arg = Base::get_arg(arg_index - 1, error); |
| 377 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 378 | if (error) |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 379 | FMT_THROW(format_error(!*it ? "invalid format string" : error)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 380 | return arg; |
| 381 | } |
| 382 | |
| 383 | template <typename Char, typename AF> |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 384 | unsigned printf_context<Char, AF>::parse_header( |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 385 | iterator &it, format_specs &spec) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 386 | unsigned arg_index = std::numeric_limits<unsigned>::max(); |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 387 | Char c = *it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 388 | if (c >= '0' && c <= '9') { |
| 389 | // Parse an argument index (if followed by '$') or a width possibly |
| 390 | // preceded with '0' flag(s). |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 391 | unsigned value = internal::parse_nonnegative_int(it); |
| 392 | if (*it == '$') { // value is an argument index |
| 393 | ++it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 394 | arg_index = value; |
| 395 | } else { |
| 396 | if (c == '0') |
| 397 | spec.fill_ = '0'; |
| 398 | if (value != 0) { |
| 399 | // Nonzero value means that we parsed width and don't need to |
| 400 | // parse it or flags again, so return now. |
| 401 | spec.width_ = value; |
| 402 | return arg_index; |
| 403 | } |
| 404 | } |
| 405 | } |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 406 | parse_flags(spec, it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 407 | // Parse width. |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 408 | if (*it >= '0' && *it <= '9') { |
| 409 | spec.width_ = internal::parse_nonnegative_int(it); |
| 410 | } else if (*it == '*') { |
| 411 | ++it; |
| 412 | spec.width_ = visit(internal::PrintfWidthHandler<Char>(spec), get_arg(it)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 413 | } |
| 414 | return arg_index; |
| 415 | } |
| 416 | |
| 417 | template <typename Char, typename AF> |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 418 | void printf_context<Char, AF>::format(basic_buffer<Char> &buffer) { |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 419 | auto start = this->pos(); |
| 420 | auto it = start; |
| 421 | using internal::pointer_from; |
| 422 | while (*it) { |
| 423 | Char c = *it++; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 424 | if (c != '%') continue; |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 425 | if (*it == c) { |
| 426 | buffer.append(pointer_from(start), pointer_from(it)); |
| 427 | start = ++it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 428 | continue; |
| 429 | } |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 430 | buffer.append(pointer_from(start), pointer_from(it) - 1); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 431 | |
| Victor Zverovich | 296e9ca | 2017-01-28 12:51:35 +0000 | [diff] [blame] | 432 | format_specs spec; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 433 | spec.align_ = ALIGN_RIGHT; |
| 434 | |
| 435 | // Parse argument index, flags and width. |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 436 | unsigned arg_index = parse_header(it, spec); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 437 | |
| 438 | // Parse precision. |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 439 | if (*it == '.') { |
| 440 | ++it; |
| 441 | if ('0' <= *it && *it <= '9') { |
| 442 | spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(it)); |
| 443 | } else if (*it == '*') { |
| 444 | ++it; |
| 445 | spec.precision_ = |
| 446 | visit(internal::PrintfPrecisionHandler(), get_arg(it)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 450 | format_arg arg = get_arg(it, arg_index); |
| Victor Zverovich | c9dc41a | 2016-11-19 07:59:54 -0800 | [diff] [blame] | 451 | if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg)) |
| Victor Zverovich | d4ddaaf | 2016-07-20 08:09:14 -0700 | [diff] [blame] | 452 | spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 453 | if (spec.fill_ == '0') { |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 454 | if (arg.is_numeric()) |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 455 | spec.align_ = ALIGN_NUMERIC; |
| 456 | else |
| 457 | spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. |
| 458 | } |
| 459 | |
| 460 | // Parse length and convert the argument to the required type. |
| Victor Zverovich | 751ff64 | 2016-11-19 08:40:24 -0800 | [diff] [blame] | 461 | using internal::convert_arg; |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 462 | switch (*it++) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 463 | case 'h': |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 464 | if (*it == 'h') |
| 465 | convert_arg<signed char>(arg, *++it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 466 | else |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 467 | convert_arg<short>(arg, *it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 468 | break; |
| 469 | case 'l': |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 470 | if (*it == 'l') |
| 471 | convert_arg<fmt::long_long>(arg, *++it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 472 | else |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 473 | convert_arg<long>(arg, *it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 474 | break; |
| 475 | case 'j': |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 476 | convert_arg<intmax_t>(arg, *it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 477 | break; |
| 478 | case 'z': |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 479 | convert_arg<std::size_t>(arg, *it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 480 | break; |
| 481 | case 't': |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 482 | convert_arg<std::ptrdiff_t>(arg, *it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 483 | break; |
| 484 | case 'L': |
| 485 | // printf produces garbage when 'L' is omitted for long double, no |
| 486 | // need to do the same. |
| 487 | break; |
| 488 | default: |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 489 | --it; |
| 490 | convert_arg<void>(arg, *it); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Parse type. |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 494 | if (!*it) |
| Victor Zverovich | 9bb213e | 2016-08-25 08:38:07 -0700 | [diff] [blame] | 495 | FMT_THROW(format_error("invalid format string")); |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 496 | spec.type_ = static_cast<char>(*it++); |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 497 | if (arg.is_integral()) { |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 498 | // Normalize type. |
| 499 | switch (spec.type_) { |
| 500 | case 'i': case 'u': |
| 501 | spec.type_ = 'd'; |
| 502 | break; |
| 503 | case 'c': |
| 504 | // TODO: handle wchar_t |
| Victor Zverovich | d705d51 | 2016-12-29 09:07:39 -0800 | [diff] [blame] | 505 | visit(internal::CharConverter<printf_context<Char, AF>>(arg), arg); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | } |
| 509 | |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 510 | start = it; |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 511 | |
| 512 | // Format argument. |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 513 | visit(AF(buffer, spec), arg); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 514 | } |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 515 | buffer.append(pointer_from(start), pointer_from(it)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 516 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 517 | |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame] | 518 | // Formats a value. |
| 519 | template <typename Char, typename T> |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 520 | void format_value(basic_buffer<Char> &buf, const T &value, |
| Victor Zverovich | 9998f66 | 2016-11-06 16:11:24 -0800 | [diff] [blame] | 521 | printf_context<Char>& ctx) { |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 522 | internal::format_value(buf, value); |
| Victor Zverovich | 18dfa25 | 2016-10-21 06:46:21 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 525 | template <typename Char> |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 526 | void printf(basic_buffer<Char> &buf, basic_string_view<Char> format, |
| Victor Zverovich | 7ae8bd7 | 2017-02-05 06:09:06 -0800 | [diff] [blame] | 527 | basic_args<printf_context<Char>> args) { |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 528 | printf_context<Char>(format, args).format(buf); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| Victor Zverovich | 7ae8bd7 | 2017-02-05 06:09:06 -0800 | [diff] [blame] | 531 | typedef basic_args<printf_context<char>> printf_args; |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 532 | |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 533 | inline std::string vsprintf(string_view format, printf_args args) { |
| Victor Zverovich | eedfd07 | 2017-02-18 09:13:12 -0800 | [diff] [blame] | 534 | memory_buffer buffer; |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 535 | printf(buffer, format, args); |
| 536 | return to_string(buffer); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 539 | /** |
| 540 | \rst |
| 541 | Formats arguments and returns the result as a string. |
| 542 | |
| 543 | **Example**:: |
| 544 | |
| 545 | std::string message = fmt::sprintf("The answer is %d", 42); |
| 546 | \endrst |
| 547 | */ |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 548 | template <typename... Args> |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 549 | inline std::string sprintf(string_view format_str, const Args & ... args) { |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 550 | return vsprintf(format_str, make_args<printf_context<char>>(args...)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 551 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 552 | |
| Victor Zverovich | 0854f8c | 2016-12-11 13:22:45 -0800 | [diff] [blame] | 553 | inline std::wstring vsprintf( |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 554 | wstring_view format, basic_args<printf_context<wchar_t>> args) { |
| Victor Zverovich | eedfd07 | 2017-02-18 09:13:12 -0800 | [diff] [blame] | 555 | wmemory_buffer buffer; |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 556 | printf(buffer, format, args); |
| 557 | return to_string(buffer); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 558 | } |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 559 | |
| 560 | template <typename... Args> |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 561 | inline std::wstring sprintf(wstring_view format_str, const Args & ... args) { |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 562 | auto vargs = make_args<printf_context<wchar_t>>(args...); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 563 | return vsprintf(format_str, vargs); |
| 564 | } |
| 565 | |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 566 | FMT_API int vfprintf(std::FILE *f, string_view format, printf_args args); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 567 | |
| 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 Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 577 | template <typename... Args> |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 578 | inline int fprintf(std::FILE *f, string_view format_str, const Args & ... args) { |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 579 | auto vargs = make_args<printf_context<char>>(args...); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 580 | return vfprintf(f, format_str, vargs); |
| 581 | } |
| 582 | |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 583 | inline int vprintf(string_view format, printf_args args) { |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 584 | return vfprintf(stdout, format, args); |
| 585 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 586 | |
| 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 Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 596 | template <typename... Args> |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 597 | inline int printf(string_view format_str, const Args & ... args) { |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 598 | return vprintf(format_str, make_args<printf_context<char>>(args...)); |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 599 | } |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 600 | |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 601 | inline int vfprintf(std::ostream &os, string_view format_str, printf_args args) { |
| Victor Zverovich | eedfd07 | 2017-02-18 09:13:12 -0800 | [diff] [blame] | 602 | memory_buffer buffer; |
| Victor Zverovich | fefaf07 | 2017-02-14 16:29:47 -0500 | [diff] [blame] | 603 | printf(buffer, format_str, args); |
| 604 | internal::write(os, buffer); |
| 605 | return static_cast<int>(buffer.size()); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 606 | } |
| Victor Zverovich | 9dbb60c | 2016-08-03 08:52:05 -0700 | [diff] [blame] | 607 | |
| 608 | /** |
| 609 | \rst |
| 610 | Prints formatted data to the stream *os*. |
| 611 | |
| 612 | **Example**:: |
| 613 | |
| 614 | fprintf(cerr, "Don't %s!", "panic"); |
| 615 | \endrst |
| 616 | */ |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 617 | template <typename... Args> |
| Victor Zverovich | 2f4f49f | 2017-07-18 19:40:48 -0700 | [diff] [blame] | 618 | inline int fprintf(std::ostream &os, string_view format_str, |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 619 | const Args & ... args) { |
| Victor Zverovich | 624c586 | 2017-02-05 06:41:39 -0800 | [diff] [blame] | 620 | auto vargs = make_args<printf_context<char>>(args...); |
| Victor Zverovich | 0028ce5 | 2016-08-26 17:23:13 -0700 | [diff] [blame] | 621 | return vfprintf(os, format_str, vargs); |
| Victor Zverovich | 9dbb60c | 2016-08-03 08:52:05 -0700 | [diff] [blame] | 622 | } |
| Glen Stark | 72d51e0 | 2016-06-08 01:23:32 +0200 | [diff] [blame] | 623 | } // namespace fmt |
| 624 | |
| 625 | #endif // FMT_PRINTF_H_ |