| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 1 | /* |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 2 | Formatting library for C++ |
| Victor Zverovich | faccb4c | 2012-12-12 07:44:41 -0800 | [diff] [blame] | 3 | |
| Victor Zverovich | e3a44c1 | 2014-07-09 06:56:36 -0700 | [diff] [blame] | 4 | Copyright (c) 2012 - 2014, Victor Zverovich |
| Victor Zverovich | faccb4c | 2012-12-12 07:44:41 -0800 | [diff] [blame] | 5 | All rights reserved. |
| 6 | |
| 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are met: |
| 9 | |
| 10 | 1. Redistributions of source code must retain the above copyright notice, this |
| 11 | list of conditions and the following disclaimer. |
| 12 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | this list of conditions and the following disclaimer in the documentation |
| 14 | and/or other materials provided with the distribution. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 20 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 26 | */ |
| 27 | |
| Victor Zverovich | fbfedcf | 2013-01-14 15:16:20 -0800 | [diff] [blame] | 28 | #include "format.h" |
| 29 | |
| Victor Zverovich | 859a497 | 2014-04-30 06:55:21 -0700 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
| Victor Zverovich | 72f896d | 2012-12-12 09:17:28 -0800 | [diff] [blame] | 32 | #include <cctype> |
| Victor Zverovich | 5d15bdd | 2014-07-01 16:23:50 -0700 | [diff] [blame] | 33 | #include <cerrno> |
| Victor Zverovich | f28645f | 2014-04-24 12:37:06 -0700 | [diff] [blame] | 34 | #include <climits> |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 35 | #include <cmath> |
| Victor Zverovich | a684d0c | 2013-12-27 08:00:10 -0800 | [diff] [blame] | 36 | #include <cstdarg> |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 37 | |
| Victor Zverovich | 859a497 | 2014-04-30 06:55:21 -0700 | [diff] [blame] | 38 | #ifdef _WIN32 |
| Constantine Tarasenkov | 6aace69 | 2014-06-11 02:38:57 +0400 | [diff] [blame] | 39 | # ifdef __MINGW32__ |
| 40 | # include <cstring> |
| 41 | # endif |
| Victor Zverovich | 859a497 | 2014-04-30 06:55:21 -0700 | [diff] [blame] | 42 | # include <windows.h> |
| 43 | #endif |
| 44 | |
| Victor Zverovich | 6e5551e | 2014-07-02 06:33:25 -0700 | [diff] [blame] | 45 | using fmt::internal::Arg; |
| Victor Zverovich | 447e02c | 2014-02-15 10:48:34 -0800 | [diff] [blame] | 46 | |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 47 | // Check if exceptions are disabled. |
| 48 | #if __GNUC__ && !__EXCEPTIONS |
| 49 | # define FMT_EXCEPTIONS 0 |
| 50 | #endif |
| 51 | #if _MSC_VER && !_HAS_EXCEPTIONS |
| 52 | # define FMT_EXCEPTIONS 0 |
| 53 | #endif |
| 54 | #ifndef FMT_EXCEPTIONS |
| 55 | # define FMT_EXCEPTIONS 1 |
| 56 | #endif |
| 57 | |
| 58 | #if FMT_EXCEPTIONS |
| 59 | # define FMT_TRY try |
| 60 | # define FMT_CATCH(x) catch (x) |
| 61 | #else |
| 62 | # define FMT_TRY if (true) |
| 63 | # define FMT_CATCH(x) if (false) |
| 64 | #endif |
| 65 | |
| 66 | #ifndef FMT_THROW |
| 67 | # if FMT_EXCEPTIONS |
| 68 | # define FMT_THROW(x) throw x |
| 69 | # else |
| 70 | # define FMT_THROW(x) assert(false) |
| 71 | # endif |
| 72 | #endif |
| 73 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 74 | #ifdef FMT_HEADER_ONLY |
| 75 | # define FMT_FUNC inline |
| Victor Zverovich | c2a6903 | 2014-11-28 15:30:03 -0800 | [diff] [blame] | 76 | #else |
| 77 | # define FMT_FUNC |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 78 | #endif |
| 79 | |
| jdale88 | a9862fd | 2014-03-11 18:56:24 +0000 | [diff] [blame] | 80 | #if _MSC_VER |
| 81 | # pragma warning(push) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 82 | # pragma warning(disable: 4127) // conditional expression is constant |
| Daniel.Perry | bd0067e | 2014-11-25 18:01:09 -0500 | [diff] [blame] | 83 | # pragma warning(disable: 4702) // unreachable code |
| jdale88 | a9862fd | 2014-03-11 18:56:24 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 86 | namespace { |
| 87 | |
| 88 | #ifndef _MSC_VER |
| Victor Zverovich | b9a568b | 2014-09-19 07:51:42 -0700 | [diff] [blame] | 89 | # define FMT_SNPRINTF snprintf |
| Victor Zverovich | a684d0c | 2013-12-27 08:00:10 -0800 | [diff] [blame] | 90 | #else // _MSC_VER |
| Victor Zverovich | 406c612 | 2014-08-19 08:47:38 -0700 | [diff] [blame] | 91 | inline int fmt_snprintf(char *buffer, size_t size, const char *format, ...) { |
| Victor Zverovich | a684d0c | 2013-12-27 08:00:10 -0800 | [diff] [blame] | 92 | va_list args; |
| 93 | va_start(args, format); |
| 94 | int result = vsnprintf_s(buffer, size, _TRUNCATE, format, args); |
| 95 | va_end(args); |
| 96 | return result; |
| 97 | } |
| Victor Zverovich | b9a568b | 2014-09-19 07:51:42 -0700 | [diff] [blame] | 98 | # define FMT_SNPRINTF fmt_snprintf |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 99 | #endif // _MSC_VER |
| Victor Zverovich | 43fe100 | 2014-02-19 14:20:26 -0800 | [diff] [blame] | 100 | |
| Victor Zverovich | adce024 | 2014-08-17 07:53:55 -0700 | [diff] [blame] | 101 | // Checks if a value fits in int - used to avoid warnings about comparing |
| 102 | // signed and unsigned integers. |
| 103 | template <bool IsSigned> |
| 104 | struct IntChecker { |
| 105 | template <typename T> |
| 106 | static bool fits_in_int(T value) { |
| 107 | unsigned max = INT_MAX; |
| 108 | return value <= max; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | template <> |
| 113 | struct IntChecker<true> { |
| 114 | template <typename T> |
| 115 | static bool fits_in_int(T value) { |
| 116 | return value >= INT_MIN && value <= INT_MAX; |
| 117 | } |
| 118 | }; |
| 119 | |
| Victor Zverovich | 43fe100 | 2014-02-19 14:20:26 -0800 | [diff] [blame] | 120 | const char RESET_COLOR[] = "\x1b[0m"; |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 121 | |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 122 | typedef void (*FormatFunc)(fmt::Writer &, int, fmt::StringRef); |
| 123 | |
| Victor Zverovich | f2c9df8 | 2014-09-05 08:44:41 -0700 | [diff] [blame] | 124 | // Portable thread-safe version of strerror. |
| 125 | // Sets buffer to point to a string describing the error code. |
| 126 | // This can be either a pointer to a string stored in buffer, |
| 127 | // or a pointer to some static immutable string. |
| 128 | // Returns one of the following values: |
| 129 | // 0 - success |
| 130 | // ERANGE - buffer is not large enough to store the error message |
| 131 | // other - failure |
| 132 | // Buffer should be at least of size 1. |
| 133 | int safe_strerror( |
| 134 | int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true) { |
| 135 | assert(buffer != 0 && buffer_size != 0); |
| 136 | int result = 0; |
| Victor Zverovich | bdeffc3 | 2015-02-05 07:04:22 -0800 | [diff] [blame^] | 137 | #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || __ANDROID__ |
| 138 | // XSI-compliant version of strerror_r. |
| 139 | result = strerror_r(error_code, buffer, buffer_size); |
| 140 | if (result != 0) |
| 141 | result = errno; |
| 142 | #elif _GNU_SOURCE |
| 143 | // GNU-specific version of strerror_r. |
| Victor Zverovich | f2c9df8 | 2014-09-05 08:44:41 -0700 | [diff] [blame] | 144 | char *message = strerror_r(error_code, buffer, buffer_size); |
| 145 | // If the buffer is full then the message is probably truncated. |
| 146 | if (message == buffer && strlen(buffer) == buffer_size - 1) |
| 147 | result = ERANGE; |
| 148 | buffer = message; |
| 149 | #elif __MINGW32__ |
| 150 | errno = 0; |
| 151 | (void)buffer_size; |
| 152 | buffer = strerror(error_code); |
| 153 | result = errno; |
| 154 | #elif _WIN32 |
| 155 | result = strerror_s(buffer, buffer_size, error_code); |
| 156 | // If the buffer is full then the message is probably truncated. |
| 157 | if (result == 0 && std::strlen(buffer) == buffer_size - 1) |
| 158 | result = ERANGE; |
| 159 | #else |
| 160 | result = strerror_r(error_code, buffer, buffer_size); |
| 161 | if (result == -1) |
| 162 | result = errno; // glibc versions before 2.13 return result in errno. |
| 163 | #endif |
| 164 | return result; |
| 165 | } |
| 166 | |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 167 | void format_error_code(fmt::Writer &out, int error_code, |
| 168 | fmt::StringRef message) FMT_NOEXCEPT(true) { |
| 169 | // Report error code making sure that the output fits into |
| 170 | // INLINE_BUFFER_SIZE to avoid dynamic memory allocation and potential |
| 171 | // bad_alloc. |
| 172 | out.clear(); |
| 173 | static const char SEP[] = ": "; |
| Victor Zverovich | dff2137 | 2014-12-16 07:01:01 -0800 | [diff] [blame] | 174 | static const char ERR[] = "error "; |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 175 | fmt::internal::IntTraits<int>::MainType ec_value = error_code; |
| Victor Zverovich | dff2137 | 2014-12-16 07:01:01 -0800 | [diff] [blame] | 176 | // Subtract 2 to account for terminating null characters in SEP and ERR. |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 177 | std::size_t error_code_size = |
| Victor Zverovich | dff2137 | 2014-12-16 07:01:01 -0800 | [diff] [blame] | 178 | sizeof(SEP) + sizeof(ERR) + fmt::internal::count_digits(ec_value) - 2; |
| Victor Zverovich | 88e0db8 | 2014-09-05 08:04:26 -0700 | [diff] [blame] | 179 | if (message.size() <= fmt::internal::INLINE_BUFFER_SIZE - error_code_size) |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 180 | out << message << SEP; |
| Victor Zverovich | dff2137 | 2014-12-16 07:01:01 -0800 | [diff] [blame] | 181 | out << ERR << error_code; |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 182 | assert(out.size() <= fmt::internal::INLINE_BUFFER_SIZE); |
| 183 | } |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 184 | |
| Victor Zverovich | d8b9f41 | 2014-07-29 06:38:05 -0700 | [diff] [blame] | 185 | void report_error(FormatFunc func, |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 186 | int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) { |
| Victor Zverovich | d1ded56 | 2014-09-29 08:48:16 -0700 | [diff] [blame] | 187 | fmt::MemoryWriter full_message; |
| Victor Zverovich | 88e0db8 | 2014-09-05 08:04:26 -0700 | [diff] [blame] | 188 | func(full_message, error_code, message); |
| 189 | // Use Writer::data instead of Writer::c_str to avoid potential memory |
| 190 | // allocation. |
| 191 | std::fwrite(full_message.data(), full_message.size(), 1, stderr); |
| 192 | std::fputc('\n', stderr); |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 193 | } |
| Victor Zverovich | d29e505 | 2014-06-30 07:12:09 -0700 | [diff] [blame] | 194 | |
| Victor Zverovich | e4c4e4e | 2014-07-30 06:51:35 -0700 | [diff] [blame] | 195 | // IsZeroInt::visit(arg) returns true iff arg is a zero integer. |
| 196 | class IsZeroInt : public fmt::internal::ArgVisitor<IsZeroInt, bool> { |
| 197 | public: |
| 198 | template <typename T> |
| 199 | bool visit_any_int(T value) { return value == 0; } |
| 200 | }; |
| Victor Zverovich | 6e5551e | 2014-07-02 06:33:25 -0700 | [diff] [blame] | 201 | |
| 202 | // Parses an unsigned integer advancing s to the end of the parsed input. |
| 203 | // This function assumes that the first character of s is a digit. |
| 204 | template <typename Char> |
| Victor Zverovich | 526b7fc | 2014-08-28 06:42:59 -0700 | [diff] [blame] | 205 | int parse_nonnegative_int(const Char *&s) { |
| Victor Zverovich | 6e5551e | 2014-07-02 06:33:25 -0700 | [diff] [blame] | 206 | assert('0' <= *s && *s <= '9'); |
| 207 | unsigned value = 0; |
| 208 | do { |
| 209 | unsigned new_value = value * 10 + (*s++ - '0'); |
| 210 | // Check if value wrapped around. |
| Victor Zverovich | 526b7fc | 2014-08-28 06:42:59 -0700 | [diff] [blame] | 211 | if (new_value < value) { |
| 212 | value = UINT_MAX; |
| 213 | break; |
| 214 | } |
| 215 | value = new_value; |
| Victor Zverovich | 6e5551e | 2014-07-02 06:33:25 -0700 | [diff] [blame] | 216 | } while ('0' <= *s && *s <= '9'); |
| Victor Zverovich | 526b7fc | 2014-08-28 06:42:59 -0700 | [diff] [blame] | 217 | if (value > INT_MAX) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 218 | FMT_THROW(fmt::FormatError("number is too big")); |
| Victor Zverovich | 6e5551e | 2014-07-02 06:33:25 -0700 | [diff] [blame] | 219 | return value; |
| 220 | } |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 221 | |
| Victor Zverovich | 5debb2a | 2014-08-29 08:16:10 -0700 | [diff] [blame] | 222 | inline void require_numeric_argument(const Arg &arg, char spec) { |
| 223 | if (arg.type > Arg::LAST_NUMERIC_TYPE) { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 224 | std::string message = |
| 225 | fmt::format("format specifier '{}' requires numeric argument", spec); |
| 226 | FMT_THROW(fmt::FormatError(message)); |
| Victor Zverovich | 5debb2a | 2014-08-29 08:16:10 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 230 | template <typename Char> |
| Victor Zverovich | 3947a7a | 2014-08-29 06:57:53 -0700 | [diff] [blame] | 231 | void check_sign(const Char *&s, const Arg &arg) { |
| 232 | char sign = static_cast<char>(*s); |
| Victor Zverovich | 5debb2a | 2014-08-29 08:16:10 -0700 | [diff] [blame] | 233 | require_numeric_argument(arg, sign); |
| Victor Zverovich | 3947a7a | 2014-08-29 06:57:53 -0700 | [diff] [blame] | 234 | if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 235 | FMT_THROW(fmt::FormatError(fmt::format( |
| 236 | "format specifier '{}' requires signed argument", sign))); |
| Victor Zverovich | 3947a7a | 2014-08-29 06:57:53 -0700 | [diff] [blame] | 237 | } |
| 238 | ++s; |
| 239 | } |
| 240 | |
| Victor Zverovich | e3a2ac8 | 2014-07-14 08:04:17 -0700 | [diff] [blame] | 241 | // Checks if an argument is a valid printf width specifier and sets |
| 242 | // left alignment if it is negative. |
| Victor Zverovich | e4c4e4e | 2014-07-30 06:51:35 -0700 | [diff] [blame] | 243 | class WidthHandler : public fmt::internal::ArgVisitor<WidthHandler, unsigned> { |
| Victor Zverovich | 591ad0a | 2014-07-14 06:55:29 -0700 | [diff] [blame] | 244 | private: |
| 245 | fmt::FormatSpec &spec_; |
| Victor Zverovich | e3a2ac8 | 2014-07-14 08:04:17 -0700 | [diff] [blame] | 246 | |
| Victor Zverovich | 6f3c095 | 2014-12-03 06:16:52 -0800 | [diff] [blame] | 247 | FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler); |
| 248 | |
| Victor Zverovich | 591ad0a | 2014-07-14 06:55:29 -0700 | [diff] [blame] | 249 | public: |
| 250 | explicit WidthHandler(fmt::FormatSpec &spec) : spec_(spec) {} |
| Victor Zverovich | e3a2ac8 | 2014-07-14 08:04:17 -0700 | [diff] [blame] | 251 | |
| Victor Zverovich | 9d74f95 | 2014-07-16 07:27:54 -0700 | [diff] [blame] | 252 | unsigned visit_unhandled_arg() { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 253 | FMT_THROW(fmt::FormatError("width is not integer")); |
| Victor Zverovich | d707adc | 2014-10-13 06:59:18 -0700 | [diff] [blame] | 254 | return 0; |
| Victor Zverovich | 591ad0a | 2014-07-14 06:55:29 -0700 | [diff] [blame] | 255 | } |
| Victor Zverovich | e3a2ac8 | 2014-07-14 08:04:17 -0700 | [diff] [blame] | 256 | |
| Victor Zverovich | 9d74f95 | 2014-07-16 07:27:54 -0700 | [diff] [blame] | 257 | template <typename T> |
| 258 | unsigned visit_any_int(T value) { |
| 259 | typedef typename fmt::internal::IntTraits<T>::MainType UnsignedType; |
| 260 | UnsignedType width = value; |
| 261 | if (fmt::internal::is_negative(value)) { |
| Victor Zverovich | 591ad0a | 2014-07-14 06:55:29 -0700 | [diff] [blame] | 262 | spec_.align_ = fmt::ALIGN_LEFT; |
| 263 | width = 0 - width; |
| 264 | } |
| Victor Zverovich | 9d74f95 | 2014-07-16 07:27:54 -0700 | [diff] [blame] | 265 | if (width > INT_MAX) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 266 | FMT_THROW(fmt::FormatError("number is too big")); |
| Victor Zverovich | 9d74f95 | 2014-07-16 07:27:54 -0700 | [diff] [blame] | 267 | return static_cast<unsigned>(width); |
| Victor Zverovich | 591ad0a | 2014-07-14 06:55:29 -0700 | [diff] [blame] | 268 | } |
| Victor Zverovich | 591ad0a | 2014-07-14 06:55:29 -0700 | [diff] [blame] | 269 | }; |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 270 | |
| Victor Zverovich | e4c4e4e | 2014-07-30 06:51:35 -0700 | [diff] [blame] | 271 | class PrecisionHandler : |
| 272 | public fmt::internal::ArgVisitor<PrecisionHandler, int> { |
| 273 | public: |
| 274 | unsigned visit_unhandled_arg() { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 275 | FMT_THROW(fmt::FormatError("precision is not integer")); |
| Victor Zverovich | 755ecb0 | 2014-10-13 08:39:38 -0700 | [diff] [blame] | 276 | return 0; |
| Victor Zverovich | e4c4e4e | 2014-07-30 06:51:35 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | template <typename T> |
| 280 | int visit_any_int(T value) { |
| Victor Zverovich | adce024 | 2014-08-17 07:53:55 -0700 | [diff] [blame] | 281 | if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value)) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 282 | FMT_THROW(fmt::FormatError("number is too big")); |
| Victor Zverovich | e4c4e4e | 2014-07-30 06:51:35 -0700 | [diff] [blame] | 283 | return static_cast<int>(value); |
| 284 | } |
| 285 | }; |
| 286 | |
| Victor Zverovich | 32344d9 | 2014-08-28 08:11:21 -0700 | [diff] [blame] | 287 | // Converts an integer argument to an integral type T for printf. |
| Victor Zverovich | eeca223 | 2014-07-30 07:37:16 -0700 | [diff] [blame] | 288 | template <typename T> |
| 289 | class ArgConverter : public fmt::internal::ArgVisitor<ArgConverter<T>, void> { |
| 290 | private: |
| 291 | fmt::internal::Arg &arg_; |
| Victor Zverovich | e22d657 | 2014-08-08 06:51:09 -0700 | [diff] [blame] | 292 | wchar_t type_; |
| Victor Zverovich | eeca223 | 2014-07-30 07:37:16 -0700 | [diff] [blame] | 293 | |
| Victor Zverovich | 6f3c095 | 2014-12-03 06:16:52 -0800 | [diff] [blame] | 294 | FMT_DISALLOW_COPY_AND_ASSIGN(ArgConverter); |
| 295 | |
| Victor Zverovich | eeca223 | 2014-07-30 07:37:16 -0700 | [diff] [blame] | 296 | public: |
| Victor Zverovich | e22d657 | 2014-08-08 06:51:09 -0700 | [diff] [blame] | 297 | ArgConverter(fmt::internal::Arg &arg, wchar_t type) |
| 298 | : arg_(arg), type_(type) {} |
| Victor Zverovich | eeca223 | 2014-07-30 07:37:16 -0700 | [diff] [blame] | 299 | |
| 300 | template <typename U> |
| 301 | void visit_any_int(U value) { |
| Victor Zverovich | d81fafc | 2014-07-31 07:43:14 -0700 | [diff] [blame] | 302 | bool is_signed = type_ == 'd' || type_ == 'i'; |
| 303 | using fmt::internal::Arg; |
| 304 | if (sizeof(T) <= sizeof(int)) { |
| Victor Zverovich | 32344d9 | 2014-08-28 08:11:21 -0700 | [diff] [blame] | 305 | // Extra casts are used to silence warnings. |
| Victor Zverovich | d81fafc | 2014-07-31 07:43:14 -0700 | [diff] [blame] | 306 | if (is_signed) { |
| 307 | arg_.type = Arg::INT; |
| Victor Zverovich | bb01633 | 2014-08-15 09:03:59 -0700 | [diff] [blame] | 308 | arg_.int_value = static_cast<int>(static_cast<T>(value)); |
| Victor Zverovich | d81fafc | 2014-07-31 07:43:14 -0700 | [diff] [blame] | 309 | } else { |
| 310 | arg_.type = Arg::UINT; |
| Victor Zverovich | 186734c | 2014-08-18 07:03:12 -0700 | [diff] [blame] | 311 | arg_.uint_value = static_cast<unsigned>( |
| 312 | static_cast<typename fmt::internal::MakeUnsigned<T>::Type>(value)); |
| Victor Zverovich | d81fafc | 2014-07-31 07:43:14 -0700 | [diff] [blame] | 313 | } |
| Victor Zverovich | eeca223 | 2014-07-30 07:37:16 -0700 | [diff] [blame] | 314 | } else { |
| Victor Zverovich | d81fafc | 2014-07-31 07:43:14 -0700 | [diff] [blame] | 315 | if (is_signed) { |
| 316 | arg_.type = Arg::LONG_LONG; |
| Victor Zverovich | a259c94 | 2014-08-01 07:15:27 -0700 | [diff] [blame] | 317 | arg_.long_long_value = |
| 318 | static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value); |
| Victor Zverovich | d81fafc | 2014-07-31 07:43:14 -0700 | [diff] [blame] | 319 | } else { |
| 320 | arg_.type = Arg::ULONG_LONG; |
| 321 | arg_.ulong_long_value = |
| Victor Zverovich | a259c94 | 2014-08-01 07:15:27 -0700 | [diff] [blame] | 322 | static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value); |
| Victor Zverovich | d81fafc | 2014-07-31 07:43:14 -0700 | [diff] [blame] | 323 | } |
| Victor Zverovich | eeca223 | 2014-07-30 07:37:16 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | }; |
| 327 | |
| Victor Zverovich | 32344d9 | 2014-08-28 08:11:21 -0700 | [diff] [blame] | 328 | // Converts an integer argument to char for printf. |
| Victor Zverovich | c4a4a05 | 2014-08-19 08:14:21 -0700 | [diff] [blame] | 329 | class CharConverter : public fmt::internal::ArgVisitor<CharConverter, void> { |
| 330 | private: |
| 331 | fmt::internal::Arg &arg_; |
| 332 | |
| Victor Zverovich | 6f3c095 | 2014-12-03 06:16:52 -0800 | [diff] [blame] | 333 | FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter); |
| 334 | |
| Victor Zverovich | c4a4a05 | 2014-08-19 08:14:21 -0700 | [diff] [blame] | 335 | public: |
| 336 | explicit CharConverter(fmt::internal::Arg &arg) : arg_(arg) {} |
| 337 | |
| 338 | template <typename T> |
| 339 | void visit_any_int(T value) { |
| 340 | arg_.type = Arg::CHAR; |
| 341 | arg_.int_value = static_cast<char>(value); |
| 342 | } |
| 343 | }; |
| 344 | |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 345 | // This function template is used to prevent compile errors when handling |
| 346 | // incompatible string arguments, e.g. handling a wide string in a narrow |
| 347 | // string formatter. |
| 348 | template <typename Char> |
| 349 | Arg::StringValue<Char> ignore_incompatible_str(Arg::StringValue<wchar_t>); |
| 350 | |
| 351 | template <> |
| 352 | inline Arg::StringValue<char> ignore_incompatible_str( |
| 353 | Arg::StringValue<wchar_t>) { return Arg::StringValue<char>(); } |
| 354 | |
| 355 | template <> |
| 356 | inline Arg::StringValue<wchar_t> ignore_incompatible_str( |
| 357 | Arg::StringValue<wchar_t> s) { return s; } |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 358 | } // namespace |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 359 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 360 | FMT_FUNC void fmt::SystemError::init( |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 361 | int err_code, StringRef format_str, ArgList args) { |
| 362 | error_code_ = err_code; |
| Victor Zverovich | d1ded56 | 2014-09-29 08:48:16 -0700 | [diff] [blame] | 363 | MemoryWriter w; |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 364 | internal::format_system_error(w, err_code, format(format_str, args)); |
| Victor Zverovich | 5320103 | 2014-06-30 14:26:29 -0700 | [diff] [blame] | 365 | std::runtime_error &base = *this; |
| 366 | base = std::runtime_error(w.str()); |
| 367 | } |
| 368 | |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 369 | template <typename T> |
| Victor Zverovich | b498ba0 | 2014-07-26 08:03:03 -0700 | [diff] [blame] | 370 | int fmt::internal::CharTraits<char>::format_float( |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 371 | char *buffer, std::size_t size, const char *format, |
| 372 | unsigned width, int precision, T value) { |
| 373 | if (width == 0) { |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 374 | return precision < 0 ? |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 375 | FMT_SNPRINTF(buffer, size, format, value) : |
| 376 | FMT_SNPRINTF(buffer, size, format, precision, value); |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 377 | } |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 378 | return precision < 0 ? |
| 379 | FMT_SNPRINTF(buffer, size, format, width, value) : |
| 380 | FMT_SNPRINTF(buffer, size, format, width, precision, value); |
| 381 | } |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 382 | |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 383 | template <typename T> |
| Victor Zverovich | b498ba0 | 2014-07-26 08:03:03 -0700 | [diff] [blame] | 384 | int fmt::internal::CharTraits<wchar_t>::format_float( |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 385 | wchar_t *buffer, std::size_t size, const wchar_t *format, |
| 386 | unsigned width, int precision, T value) { |
| 387 | if (width == 0) { |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 388 | return precision < 0 ? |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 389 | swprintf(buffer, size, format, value) : |
| 390 | swprintf(buffer, size, format, precision, value); |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 391 | } |
| Victor Zverovich | b605b39 | 2013-09-09 22:21:40 -0700 | [diff] [blame] | 392 | return precision < 0 ? |
| 393 | swprintf(buffer, size, format, width, value) : |
| 394 | swprintf(buffer, size, format, width, precision, value); |
| Victor Zverovich | 65d47e5 | 2013-09-09 06:51:03 -0700 | [diff] [blame] | 395 | } |
| Victor Zverovich | e8ba960 | 2012-12-12 09:29:50 -0800 | [diff] [blame] | 396 | |
| Victor Zverovich | 311251e | 2014-11-29 06:58:00 -0800 | [diff] [blame] | 397 | template <typename T> |
| 398 | const char fmt::internal::BasicData<T>::DIGITS[] = |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame] | 399 | "0001020304050607080910111213141516171819" |
| 400 | "2021222324252627282930313233343536373839" |
| 401 | "4041424344454647484950515253545556575859" |
| 402 | "6061626364656667686970717273747576777879" |
| 403 | "8081828384858687888990919293949596979899"; |
| Victor Zverovich | e9b2191 | 2014-02-19 12:43:55 -0800 | [diff] [blame] | 404 | |
| Victor Zverovich | f1d8516 | 2014-02-19 13:02:22 -0800 | [diff] [blame] | 405 | #define FMT_POWERS_OF_10(factor) \ |
| 406 | factor * 10, \ |
| 407 | factor * 100, \ |
| 408 | factor * 1000, \ |
| 409 | factor * 10000, \ |
| 410 | factor * 100000, \ |
| 411 | factor * 1000000, \ |
| 412 | factor * 10000000, \ |
| 413 | factor * 100000000, \ |
| 414 | factor * 1000000000 |
| Victor Zverovich | e9b2191 | 2014-02-19 12:43:55 -0800 | [diff] [blame] | 415 | |
| Victor Zverovich | 311251e | 2014-11-29 06:58:00 -0800 | [diff] [blame] | 416 | template <typename T> |
| 417 | const uint32_t fmt::internal::BasicData<T>::POWERS_OF_10_32[] = { |
| 418 | 0, FMT_POWERS_OF_10(1) |
| 419 | }; |
| 420 | |
| 421 | template <typename T> |
| 422 | const uint64_t fmt::internal::BasicData<T>::POWERS_OF_10_64[] = { |
| Victor Zverovich | 6f0387f | 2014-02-14 10:36:17 -0800 | [diff] [blame] | 423 | 0, |
| Victor Zverovich | f1d8516 | 2014-02-19 13:02:22 -0800 | [diff] [blame] | 424 | FMT_POWERS_OF_10(1), |
| Victor Zverovich | ecd2b80 | 2014-12-17 06:42:26 -0800 | [diff] [blame] | 425 | FMT_POWERS_OF_10(fmt::ULongLong(1000000000)), |
| Victor Zverovich | 406c612 | 2014-08-19 08:47:38 -0700 | [diff] [blame] | 426 | // Multiply several constants instead of using a single long long constant |
| Victor Zverovich | f1d8516 | 2014-02-19 13:02:22 -0800 | [diff] [blame] | 427 | // to avoid warnings about C++98 not supporting long long. |
| Victor Zverovich | ecd2b80 | 2014-12-17 06:42:26 -0800 | [diff] [blame] | 428 | fmt::ULongLong(1000000000) * fmt::ULongLong(1000000000) * 10 |
| Victor Zverovich | 6f0387f | 2014-02-14 10:36:17 -0800 | [diff] [blame] | 429 | }; |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 430 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 431 | FMT_FUNC void fmt::internal::report_unknown_type(char code, const char *type) { |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 432 | if (std::isprint(static_cast<unsigned char>(code))) { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 433 | FMT_THROW(fmt::FormatError( |
| 434 | fmt::format("unknown format code '{}' for {}", code, type))); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 435 | } |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 436 | FMT_THROW(fmt::FormatError( |
| Victor Zverovich | e63a0ff | 2014-06-30 06:43:53 -0700 | [diff] [blame] | 437 | fmt::format("unknown format code '\\x{:02x}' for {}", |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 438 | static_cast<unsigned>(code), type))); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 439 | } |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 440 | |
| Victor Zverovich | da9aeab | 2014-04-30 07:23:43 -0700 | [diff] [blame] | 441 | #ifdef _WIN32 |
| 442 | |
| Victor Zverovich | b49a1b4 | 2014-12-09 06:32:07 -0800 | [diff] [blame] | 443 | FMT_FUNC fmt::internal::UTF8ToUTF16::UTF8ToUTF16(fmt::StringRef s) { |
| Victor Zverovich | da9aeab | 2014-04-30 07:23:43 -0700 | [diff] [blame] | 444 | int length = MultiByteToWideChar( |
| 445 | CP_UTF8, MB_ERR_INVALID_CHARS, s.c_str(), -1, 0, 0); |
| Victor Zverovich | dff2137 | 2014-12-16 07:01:01 -0800 | [diff] [blame] | 446 | static const char ERROR_MSG[] = "cannot convert string from UTF-8 to UTF-16"; |
| Victor Zverovich | da9aeab | 2014-04-30 07:23:43 -0700 | [diff] [blame] | 447 | if (length == 0) |
| Victor Zverovich | dff2137 | 2014-12-16 07:01:01 -0800 | [diff] [blame] | 448 | FMT_THROW(WindowsError(GetLastError(), ERROR_MSG)); |
| Victor Zverovich | da9aeab | 2014-04-30 07:23:43 -0700 | [diff] [blame] | 449 | buffer_.resize(length); |
| 450 | length = MultiByteToWideChar( |
| 451 | CP_UTF8, MB_ERR_INVALID_CHARS, s.c_str(), -1, &buffer_[0], length); |
| 452 | if (length == 0) |
| Victor Zverovich | dff2137 | 2014-12-16 07:01:01 -0800 | [diff] [blame] | 453 | FMT_THROW(WindowsError(GetLastError(), ERROR_MSG)); |
| Victor Zverovich | da9aeab | 2014-04-30 07:23:43 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| Victor Zverovich | b49a1b4 | 2014-12-09 06:32:07 -0800 | [diff] [blame] | 456 | FMT_FUNC fmt::internal::UTF16ToUTF8::UTF16ToUTF8(fmt::WStringRef s) { |
| Victor Zverovich | 5d4803a | 2014-07-27 12:53:42 -0700 | [diff] [blame] | 457 | if (int error_code = convert(s)) { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 458 | FMT_THROW(WindowsError(error_code, |
| 459 | "cannot convert string from UTF-16 to UTF-8")); |
| Victor Zverovich | da9aeab | 2014-04-30 07:23:43 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| Victor Zverovich | b49a1b4 | 2014-12-09 06:32:07 -0800 | [diff] [blame] | 463 | FMT_FUNC int fmt::internal::UTF16ToUTF8::convert(fmt::WStringRef s) { |
| Victor Zverovich | da9aeab | 2014-04-30 07:23:43 -0700 | [diff] [blame] | 464 | int length = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, 0, 0, 0, 0); |
| 465 | if (length == 0) |
| 466 | return GetLastError(); |
| 467 | buffer_.resize(length); |
| 468 | length = WideCharToMultiByte( |
| 469 | CP_UTF8, 0, s.c_str(), -1, &buffer_[0], length, 0, 0); |
| 470 | if (length == 0) |
| 471 | return GetLastError(); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| Victor Zverovich | b49a1b4 | 2014-12-09 06:32:07 -0800 | [diff] [blame] | 475 | FMT_FUNC void fmt::WindowsError::init( |
| Victor Zverovich | 1d46404 | 2014-09-21 08:08:52 -0700 | [diff] [blame] | 476 | int error_code, StringRef format_str, ArgList args) { |
| Victor Zverovich | 5320103 | 2014-06-30 14:26:29 -0700 | [diff] [blame] | 477 | error_code_ = error_code; |
| Victor Zverovich | 8c4db50 | 2014-09-29 09:17:02 -0700 | [diff] [blame] | 478 | MemoryWriter w; |
| Victor Zverovich | 75b5eb4 | 2014-07-27 15:09:05 -0700 | [diff] [blame] | 479 | internal::format_windows_error(w, error_code, format(format_str, args)); |
| Victor Zverovich | 5320103 | 2014-06-30 14:26:29 -0700 | [diff] [blame] | 480 | std::runtime_error &base = *this; |
| 481 | base = std::runtime_error(w.str()); |
| 482 | } |
| 483 | |
| Victor Zverovich | da9aeab | 2014-04-30 07:23:43 -0700 | [diff] [blame] | 484 | #endif |
| 485 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 486 | FMT_FUNC void fmt::internal::format_system_error( |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 487 | fmt::Writer &out, int error_code, |
| 488 | fmt::StringRef message) FMT_NOEXCEPT(true) { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 489 | FMT_TRY { |
| Victor Zverovich | d1ded56 | 2014-09-29 08:48:16 -0700 | [diff] [blame] | 490 | MemoryBuffer<char, INLINE_BUFFER_SIZE> buffer; |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 491 | buffer.resize(INLINE_BUFFER_SIZE); |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 492 | for (;;) { |
| Victor Zverovich | aca4937 | 2014-11-24 07:38:26 -0800 | [diff] [blame] | 493 | char *system_message = &buffer[0]; |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 494 | int result = safe_strerror(error_code, system_message, buffer.size()); |
| 495 | if (result == 0) { |
| 496 | out << message << ": " << system_message; |
| 497 | return; |
| 498 | } |
| 499 | if (result != ERANGE) |
| 500 | break; // Can't get error message, report error code instead. |
| 501 | buffer.resize(buffer.size() * 2); |
| Victor Zverovich | 53b4c31 | 2014-04-30 15:00:41 -0700 | [diff] [blame] | 502 | } |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 503 | } FMT_CATCH(...) {} |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 504 | format_error_code(out, error_code, message); |
| Victor Zverovich | 53b4c31 | 2014-04-30 15:00:41 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | #ifdef _WIN32 |
| Victor Zverovich | b49a1b4 | 2014-12-09 06:32:07 -0800 | [diff] [blame] | 508 | FMT_FUNC void fmt::internal::format_windows_error( |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 509 | fmt::Writer &out, int error_code, |
| 510 | fmt::StringRef message) FMT_NOEXCEPT(true) { |
| Victor Zverovich | 53b4c31 | 2014-04-30 15:00:41 -0700 | [diff] [blame] | 511 | class String { |
| 512 | private: |
| 513 | LPWSTR str_; |
| 514 | |
| 515 | public: |
| 516 | String() : str_() {} |
| 517 | ~String() { LocalFree(str_); } |
| 518 | LPWSTR *ptr() { return &str_; } |
| 519 | LPCWSTR c_str() const { return str_; } |
| 520 | }; |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 521 | FMT_TRY { |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 522 | String system_message; |
| 523 | if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 524 | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, |
| 525 | error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 526 | reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) { |
| 527 | UTF16ToUTF8 utf8_message; |
| 528 | if (utf8_message.convert(system_message.c_str()) == ERROR_SUCCESS) { |
| 529 | out << message << ": " << utf8_message; |
| 530 | return; |
| 531 | } |
| Victor Zverovich | 53b4c31 | 2014-04-30 15:00:41 -0700 | [diff] [blame] | 532 | } |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 533 | } FMT_CATCH(...) {} |
| Victor Zverovich | 22f75d8 | 2014-09-03 08:03:05 -0700 | [diff] [blame] | 534 | format_error_code(out, error_code, message); |
| Victor Zverovich | 53b4c31 | 2014-04-30 15:00:41 -0700 | [diff] [blame] | 535 | } |
| 536 | #endif |
| 537 | |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 538 | // An argument formatter. |
| 539 | template <typename Char> |
| 540 | class fmt::internal::ArgFormatter : |
| 541 | public fmt::internal::ArgVisitor<fmt::internal::ArgFormatter<Char>, void> { |
| 542 | private: |
| Victor Zverovich | 4edc88f | 2014-07-16 08:38:15 -0700 | [diff] [blame] | 543 | fmt::BasicFormatter<Char> &formatter_; |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 544 | fmt::BasicWriter<Char> &writer_; |
| 545 | fmt::FormatSpec &spec_; |
| 546 | const Char *format_; |
| 547 | |
| Victor Zverovich | 6f3c095 | 2014-12-03 06:16:52 -0800 | [diff] [blame] | 548 | FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatter); |
| 549 | |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 550 | public: |
| Victor Zverovich | 2a1c0c9 | 2014-07-26 09:45:03 -0700 | [diff] [blame] | 551 | ArgFormatter( |
| 552 | fmt::BasicFormatter<Char> &f,fmt::FormatSpec &s, const Char *fmt) |
| Victor Zverovich | 4edc88f | 2014-07-16 08:38:15 -0700 | [diff] [blame] | 553 | : formatter_(f), writer_(f.writer()), spec_(s), format_(fmt) {} |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 554 | |
| Victor Zverovich | 9d74f95 | 2014-07-16 07:27:54 -0700 | [diff] [blame] | 555 | template <typename T> |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 556 | void visit_any_int(T value) { writer_.write_int(value, spec_); } |
| Victor Zverovich | 9d74f95 | 2014-07-16 07:27:54 -0700 | [diff] [blame] | 557 | |
| 558 | template <typename T> |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 559 | void visit_any_double(T value) { writer_.write_double(value, spec_); } |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 560 | |
| 561 | void visit_char(int value) { |
| Victor Zverovich | a7d94f0 | 2014-07-22 12:37:10 -0700 | [diff] [blame] | 562 | if (spec_.type_ && spec_.type_ != 'c') { |
| Victor Zverovich | d699c2a | 2014-07-25 08:24:27 -0700 | [diff] [blame] | 563 | spec_.flags_ |= CHAR_FLAG; |
| 564 | writer_.write_int(value, spec_); |
| 565 | return; |
| Victor Zverovich | a7d94f0 | 2014-07-22 12:37:10 -0700 | [diff] [blame] | 566 | } |
| 567 | if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 568 | FMT_THROW(FormatError("invalid format specifier for char")); |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 569 | typedef typename fmt::BasicWriter<Char>::CharPtr CharPtr; |
| Victor Zverovich | 43aebf5 | 2015-01-08 07:56:08 -0800 | [diff] [blame] | 570 | Char fill = static_cast<Char>(spec_.fill()); |
| 571 | if (spec_.precision_ == 0) { |
| 572 | std::fill_n(writer_.grow_buffer(spec_.width_), spec_.width_, fill); |
| 573 | return; |
| 574 | } |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 575 | CharPtr out = CharPtr(); |
| 576 | if (spec_.width_ > 1) { |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 577 | out = writer_.grow_buffer(spec_.width_); |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 578 | if (spec_.align_ == fmt::ALIGN_RIGHT) { |
| 579 | std::fill_n(out, spec_.width_ - 1, fill); |
| 580 | out += spec_.width_ - 1; |
| 581 | } else if (spec_.align_ == fmt::ALIGN_CENTER) { |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 582 | out = writer_.fill_padding(out, spec_.width_, 1, fill); |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 583 | } else { |
| 584 | std::fill_n(out + 1, spec_.width_ - 1, fill); |
| 585 | } |
| 586 | } else { |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 587 | out = writer_.grow_buffer(1); |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 588 | } |
| 589 | *out = static_cast<Char>(value); |
| 590 | } |
| 591 | |
| 592 | void visit_string(Arg::StringValue<char> value) { |
| 593 | writer_.write_str(value, spec_); |
| 594 | } |
| 595 | void visit_wstring(Arg::StringValue<wchar_t> value) { |
| 596 | writer_.write_str(ignore_incompatible_str<Char>(value), spec_); |
| 597 | } |
| 598 | |
| 599 | void visit_pointer(const void *value) { |
| 600 | if (spec_.type_ && spec_.type_ != 'p') |
| Victor Zverovich | f634ccb | 2014-07-26 08:42:19 -0700 | [diff] [blame] | 601 | fmt::internal::report_unknown_type(spec_.type_, "pointer"); |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 602 | spec_.flags_ = fmt::HASH_FLAG; |
| 603 | spec_.type_ = 'x'; |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 604 | writer_.write_int(reinterpret_cast<uintptr_t>(value), spec_); |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void visit_custom(Arg::CustomValue c) { |
| Victor Zverovich | 271fa8c | 2014-08-29 07:23:54 -0700 | [diff] [blame] | 608 | c.format(&formatter_, c.value, &format_); |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 609 | } |
| 610 | }; |
| 611 | |
| Victor Zverovich | d1ded56 | 2014-09-29 08:48:16 -0700 | [diff] [blame] | 612 | template <typename Char> |
| Victor Zverovich | 2a1c0c9 | 2014-07-26 09:45:03 -0700 | [diff] [blame] | 613 | template <typename StrChar> |
| Victor Zverovich | d1ded56 | 2014-09-29 08:48:16 -0700 | [diff] [blame] | 614 | void fmt::BasicWriter<Char>::write_str( |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 615 | const Arg::StringValue<StrChar> &s, const FormatSpec &spec) { |
| Victor Zverovich | 2a1c0c9 | 2014-07-26 09:45:03 -0700 | [diff] [blame] | 616 | // Check if StrChar is convertible to Char. |
| 617 | internal::CharTraits<Char>::convert(StrChar()); |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 618 | if (spec.type_ && spec.type_ != 's') |
| Victor Zverovich | f634ccb | 2014-07-26 08:42:19 -0700 | [diff] [blame] | 619 | internal::report_unknown_type(spec.type_, "string"); |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 620 | const StrChar *str_value = s.value; |
| 621 | std::size_t str_size = s.size; |
| 622 | if (str_size == 0) { |
| 623 | if (!str_value) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 624 | FMT_THROW(FormatError("string pointer is null")); |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 625 | if (*str_value) |
| 626 | str_size = std::char_traits<StrChar>::length(str_value); |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 627 | } |
| Victor Zverovich | 43aebf5 | 2015-01-08 07:56:08 -0800 | [diff] [blame] | 628 | if (spec.precision_ >= 0 && spec.precision_ < str_size) |
| 629 | str_size = spec.precision_; |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 630 | write_str(str_value, str_size, spec); |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | template <typename Char> |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 634 | inline Arg fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) { |
| Victor Zverovich | 9646e38 | 2014-08-27 09:13:42 -0700 | [diff] [blame] | 635 | const char *error = 0; |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 636 | Arg arg = *s < '0' || *s > '9' ? |
| Victor Zverovich | c57d7a5 | 2014-08-28 08:05:47 -0700 | [diff] [blame] | 637 | next_arg(error) : get_arg(parse_nonnegative_int(s), error); |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 638 | if (error) { |
| 639 | FMT_THROW(FormatError( |
| 640 | *s != '}' && *s != ':' ? "invalid format string" : error)); |
| 641 | } |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 642 | return arg; |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 645 | FMT_FUNC Arg fmt::internal::FormatterBase::do_get_arg( |
| Victor Zverovich | 605d260 | 2014-08-29 07:45:55 -0700 | [diff] [blame] | 646 | unsigned arg_index, const char *&error) { |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 647 | Arg arg = args_[arg_index]; |
| 648 | if (arg.type == Arg::NONE) |
| 649 | error = "argument index out of range"; |
| 650 | return arg; |
| Victor Zverovich | 4edc88f | 2014-07-16 08:38:15 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 653 | inline Arg fmt::internal::FormatterBase::next_arg(const char *&error) { |
| Victor Zverovich | 605d260 | 2014-08-29 07:45:55 -0700 | [diff] [blame] | 654 | if (next_arg_index_ >= 0) |
| 655 | return do_get_arg(next_arg_index_++, error); |
| 656 | error = "cannot switch from manual to automatic argument indexing"; |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 657 | return Arg(); |
| Victor Zverovich | 605d260 | 2014-08-29 07:45:55 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 660 | inline Arg fmt::internal::FormatterBase::get_arg( |
| Victor Zverovich | c57d7a5 | 2014-08-28 08:05:47 -0700 | [diff] [blame] | 661 | unsigned arg_index, const char *&error) { |
| Victor Zverovich | 605d260 | 2014-08-29 07:45:55 -0700 | [diff] [blame] | 662 | if (next_arg_index_ <= 0) { |
| 663 | next_arg_index_ = -1; |
| 664 | return do_get_arg(arg_index, error); |
| Victor Zverovich | 4edc88f | 2014-07-16 08:38:15 -0700 | [diff] [blame] | 665 | } |
| Victor Zverovich | 605d260 | 2014-08-29 07:45:55 -0700 | [diff] [blame] | 666 | error = "cannot switch from automatic to manual argument indexing"; |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 667 | return Arg(); |
| Victor Zverovich | 4edc88f | 2014-07-16 08:38:15 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 670 | template <typename Char> |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 671 | void fmt::internal::PrintfFormatter<Char>::parse_flags( |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 672 | FormatSpec &spec, const Char *&s) { |
| Victor Zverovich | bf790d2 | 2014-06-07 07:31:25 -0700 | [diff] [blame] | 673 | for (;;) { |
| Victor Zverovich | 1b80148 | 2014-06-07 08:57:55 -0700 | [diff] [blame] | 674 | switch (*s++) { |
| Victor Zverovich | bf790d2 | 2014-06-07 07:31:25 -0700 | [diff] [blame] | 675 | case '-': |
| Victor Zverovich | bf790d2 | 2014-06-07 07:31:25 -0700 | [diff] [blame] | 676 | spec.align_ = ALIGN_LEFT; |
| 677 | break; |
| 678 | case '+': |
| Victor Zverovich | bf790d2 | 2014-06-07 07:31:25 -0700 | [diff] [blame] | 679 | spec.flags_ |= SIGN_FLAG | PLUS_FLAG; |
| 680 | break; |
| 681 | case '0': |
| 682 | spec.fill_ = '0'; |
| Victor Zverovich | 1b80148 | 2014-06-07 08:57:55 -0700 | [diff] [blame] | 683 | break; |
| Victor Zverovich | bf790d2 | 2014-06-07 07:31:25 -0700 | [diff] [blame] | 684 | case ' ': |
| Victor Zverovich | 1b80148 | 2014-06-07 08:57:55 -0700 | [diff] [blame] | 685 | spec.flags_ |= SIGN_FLAG; |
| 686 | break; |
| Victor Zverovich | bf790d2 | 2014-06-07 07:31:25 -0700 | [diff] [blame] | 687 | case '#': |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 688 | spec.flags_ |= HASH_FLAG; |
| Victor Zverovich | bf790d2 | 2014-06-07 07:31:25 -0700 | [diff] [blame] | 689 | break; |
| 690 | default: |
| Victor Zverovich | 1b80148 | 2014-06-07 08:57:55 -0700 | [diff] [blame] | 691 | --s; |
| Victor Zverovich | bf790d2 | 2014-06-07 07:31:25 -0700 | [diff] [blame] | 692 | return; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 697 | template <typename Char> |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 698 | Arg fmt::internal::PrintfFormatter<Char>::get_arg( |
| Victor Zverovich | c57d7a5 | 2014-08-28 08:05:47 -0700 | [diff] [blame] | 699 | const Char *s, unsigned arg_index) { |
| 700 | const char *error = 0; |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 701 | Arg arg = arg_index == UINT_MAX ? |
| Victor Zverovich | c57d7a5 | 2014-08-28 08:05:47 -0700 | [diff] [blame] | 702 | next_arg(error) : FormatterBase::get_arg(arg_index - 1, error); |
| 703 | if (error) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 704 | FMT_THROW(FormatError(!*s ? "invalid format string" : error)); |
| Victor Zverovich | beb00ed | 2014-09-23 07:59:43 -0700 | [diff] [blame] | 705 | return arg; |
| Victor Zverovich | c57d7a5 | 2014-08-28 08:05:47 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | template <typename Char> |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 709 | unsigned fmt::internal::PrintfFormatter<Char>::parse_header( |
| Victor Zverovich | f1dfd59 | 2014-07-16 08:49:23 -0700 | [diff] [blame] | 710 | const Char *&s, FormatSpec &spec) { |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 711 | unsigned arg_index = UINT_MAX; |
| 712 | Char c = *s; |
| 713 | if (c >= '0' && c <= '9') { |
| 714 | // Parse an argument index (if followed by '$') or a width possibly |
| Victor Zverovich | 879838a | 2014-06-20 07:34:02 -0700 | [diff] [blame] | 715 | // preceded with '0' flag(s). |
| Victor Zverovich | 526b7fc | 2014-08-28 06:42:59 -0700 | [diff] [blame] | 716 | unsigned value = parse_nonnegative_int(s); |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 717 | if (*s == '$') { // value is an argument index |
| 718 | ++s; |
| 719 | arg_index = value; |
| 720 | } else { |
| 721 | if (c == '0') |
| 722 | spec.fill_ = '0'; |
| 723 | if (value != 0) { |
| 724 | // Nonzero value means that we parsed width and don't need to |
| 725 | // parse it or flags again, so return now. |
| 726 | spec.width_ = value; |
| 727 | return arg_index; |
| 728 | } |
| 729 | } |
| 730 | } |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 731 | parse_flags(spec, s); |
| Victor Zverovich | 4099a12 | 2014-06-23 08:10:50 -0700 | [diff] [blame] | 732 | // Parse width. |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 733 | if (*s >= '0' && *s <= '9') { |
| Victor Zverovich | 526b7fc | 2014-08-28 06:42:59 -0700 | [diff] [blame] | 734 | spec.width_ = parse_nonnegative_int(s); |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 735 | } else if (*s == '*') { |
| 736 | ++s; |
| Victor Zverovich | 56fc525 | 2014-08-28 07:48:55 -0700 | [diff] [blame] | 737 | spec.width_ = WidthHandler(spec).visit(get_arg(s)); |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 738 | } |
| 739 | return arg_index; |
| 740 | } |
| 741 | |
| Victor Zverovich | 1f19b98 | 2014-06-16 07:49:30 -0700 | [diff] [blame] | 742 | template <typename Char> |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 743 | void fmt::internal::PrintfFormatter<Char>::format( |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 744 | BasicWriter<Char> &writer, BasicStringRef<Char> format_str, |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 745 | const ArgList &args) { |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 746 | const Char *start = format_str.c_str(); |
| Victor Zverovich | 605d260 | 2014-08-29 07:45:55 -0700 | [diff] [blame] | 747 | set_args(args); |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 748 | const Char *s = start; |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 749 | while (*s) { |
| Victor Zverovich | 0fc7316 | 2013-09-07 12:52:52 -0700 | [diff] [blame] | 750 | Char c = *s++; |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 751 | if (c != '%') continue; |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 752 | if (*s == c) { |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 753 | write(writer, start, s); |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 754 | start = ++s; |
| 755 | continue; |
| 756 | } |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 757 | write(writer, start, s - 1); |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 758 | |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 759 | FormatSpec spec; |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 760 | spec.align_ = ALIGN_RIGHT; |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 761 | |
| Victor Zverovich | 1a75ed0 | 2014-06-23 07:16:46 -0700 | [diff] [blame] | 762 | // Parse argument index, flags and width. |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 763 | unsigned arg_index = parse_header(s, spec); |
| Victor Zverovich | 1a75ed0 | 2014-06-23 07:16:46 -0700 | [diff] [blame] | 764 | |
| 765 | // Parse precision. |
| 766 | if (*s == '.') { |
| 767 | ++s; |
| 768 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | 526b7fc | 2014-08-28 06:42:59 -0700 | [diff] [blame] | 769 | spec.precision_ = parse_nonnegative_int(s); |
| Victor Zverovich | 1a75ed0 | 2014-06-23 07:16:46 -0700 | [diff] [blame] | 770 | } else if (*s == '*') { |
| 771 | ++s; |
| Victor Zverovich | 56fc525 | 2014-08-28 07:48:55 -0700 | [diff] [blame] | 772 | spec.precision_ = PrecisionHandler().visit(get_arg(s)); |
| Victor Zverovich | 1a75ed0 | 2014-06-23 07:16:46 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
| Victor Zverovich | 56fc525 | 2014-08-28 07:48:55 -0700 | [diff] [blame] | 776 | Arg arg = get_arg(s, arg_index); |
| Victor Zverovich | e4c4e4e | 2014-07-30 06:51:35 -0700 | [diff] [blame] | 777 | if (spec.flag(HASH_FLAG) && IsZeroInt().visit(arg)) |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 778 | spec.flags_ &= ~HASH_FLAG; |
| 779 | if (spec.fill_ == '0') { |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 780 | if (arg.type <= Arg::LAST_NUMERIC_TYPE) |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 781 | spec.align_ = ALIGN_NUMERIC; |
| 782 | else |
| 783 | spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| Victor Zverovich | f4156b5 | 2014-07-30 08:39:07 -0700 | [diff] [blame] | 786 | // Parse length and convert the argument to the required type. |
| Victor Zverovich | 316ae7e | 2014-08-09 10:04:35 -0700 | [diff] [blame] | 787 | switch (*s++) { |
| 788 | case 'h': |
| Victor Zverovich | f4156b5 | 2014-07-30 08:39:07 -0700 | [diff] [blame] | 789 | if (*s == 'h') |
| 790 | ArgConverter<signed char>(arg, *++s).visit(arg); |
| Victor Zverovich | 39b0930 | 2014-07-30 08:08:08 -0700 | [diff] [blame] | 791 | else |
| Victor Zverovich | f4156b5 | 2014-07-30 08:39:07 -0700 | [diff] [blame] | 792 | ArgConverter<short>(arg, *s).visit(arg); |
| Victor Zverovich | eeca223 | 2014-07-30 07:37:16 -0700 | [diff] [blame] | 793 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 794 | case 'l': |
| Victor Zverovich | d3a7039 | 2014-08-12 08:36:19 -0700 | [diff] [blame] | 795 | if (*s == 'l') |
| Victor Zverovich | e488a28 | 2014-08-13 06:53:43 -0700 | [diff] [blame] | 796 | ArgConverter<fmt::LongLong>(arg, *++s).visit(arg); |
| Victor Zverovich | d3a7039 | 2014-08-12 08:36:19 -0700 | [diff] [blame] | 797 | else |
| 798 | ArgConverter<long>(arg, *s).visit(arg); |
| Victor Zverovich | d81fafc | 2014-07-31 07:43:14 -0700 | [diff] [blame] | 799 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 800 | case 'j': |
| Victor Zverovich | 316ae7e | 2014-08-09 10:04:35 -0700 | [diff] [blame] | 801 | ArgConverter<intmax_t>(arg, *s).visit(arg); |
| 802 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 803 | case 'z': |
| Victor Zverovich | 316ae7e | 2014-08-09 10:04:35 -0700 | [diff] [blame] | 804 | ArgConverter<size_t>(arg, *s).visit(arg); |
| 805 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 806 | case 't': |
| Victor Zverovich | 316ae7e | 2014-08-09 10:04:35 -0700 | [diff] [blame] | 807 | ArgConverter<ptrdiff_t>(arg, *s).visit(arg); |
| 808 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 809 | case 'L': |
| Victor Zverovich | c4a4a05 | 2014-08-19 08:14:21 -0700 | [diff] [blame] | 810 | // printf produces garbage when 'L' is omitted for long double, no |
| 811 | // need to do the same. |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 812 | break; |
| Victor Zverovich | 316ae7e | 2014-08-09 10:04:35 -0700 | [diff] [blame] | 813 | default: |
| 814 | --s; |
| Victor Zverovich | be00d8b | 2014-08-19 08:49:10 -0700 | [diff] [blame] | 815 | ArgConverter<int>(arg, *s).visit(arg); |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 816 | } |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 817 | |
| 818 | // Parse type. |
| 819 | if (!*s) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 820 | FMT_THROW(FormatError("invalid format string")); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 821 | spec.type_ = static_cast<char>(*s++); |
| Victor Zverovich | c4a4a05 | 2014-08-19 08:14:21 -0700 | [diff] [blame] | 822 | if (arg.type <= Arg::LAST_INTEGER_TYPE) { |
| 823 | // Normalize type. |
| 824 | switch (spec.type_) { |
| 825 | case 'i': case 'u': |
| Victor Zverovich | f4156b5 | 2014-07-30 08:39:07 -0700 | [diff] [blame] | 826 | spec.type_ = 'd'; |
| Victor Zverovich | c4a4a05 | 2014-08-19 08:14:21 -0700 | [diff] [blame] | 827 | break; |
| 828 | case 'c': |
| 829 | // TODO: handle wchar_t |
| 830 | CharConverter(arg).visit(arg); |
| 831 | break; |
| 832 | } |
| Victor Zverovich | f4156b5 | 2014-07-30 08:39:07 -0700 | [diff] [blame] | 833 | } |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 834 | |
| 835 | start = s; |
| 836 | |
| 837 | // Format argument. |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 838 | switch (arg.type) { |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 839 | case Arg::INT: |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 840 | writer.write_int(arg.int_value, spec); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 841 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 842 | case Arg::UINT: |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 843 | writer.write_int(arg.uint_value, spec); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 844 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 845 | case Arg::LONG_LONG: |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 846 | writer.write_int(arg.long_long_value, spec); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 847 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 848 | case Arg::ULONG_LONG: |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 849 | writer.write_int(arg.ulong_long_value, spec); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 850 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 851 | case Arg::CHAR: { |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 852 | if (spec.type_ && spec.type_ != 'c') |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 853 | writer.write_int(arg.int_value, spec); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 854 | typedef typename BasicWriter<Char>::CharPtr CharPtr; |
| 855 | CharPtr out = CharPtr(); |
| 856 | if (spec.width_ > 1) { |
| Victor Zverovich | a7d94f0 | 2014-07-22 12:37:10 -0700 | [diff] [blame] | 857 | Char fill = ' '; |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 858 | out = writer.grow_buffer(spec.width_); |
| Victor Zverovich | a7d94f0 | 2014-07-22 12:37:10 -0700 | [diff] [blame] | 859 | if (spec.align_ != ALIGN_LEFT) { |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 860 | std::fill_n(out, spec.width_ - 1, fill); |
| 861 | out += spec.width_ - 1; |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 862 | } else { |
| 863 | std::fill_n(out + 1, spec.width_ - 1, fill); |
| 864 | } |
| 865 | } else { |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 866 | out = writer.grow_buffer(1); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 867 | } |
| Victor Zverovich | cb743c0 | 2014-06-19 07:40:35 -0700 | [diff] [blame] | 868 | *out = static_cast<Char>(arg.int_value); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 869 | break; |
| 870 | } |
| Victor Zverovich | a7d94f0 | 2014-07-22 12:37:10 -0700 | [diff] [blame] | 871 | case Arg::DOUBLE: |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 872 | writer.write_double(arg.double_value, spec); |
| Victor Zverovich | a7d94f0 | 2014-07-22 12:37:10 -0700 | [diff] [blame] | 873 | break; |
| 874 | case Arg::LONG_DOUBLE: |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 875 | writer.write_double(arg.long_double_value, spec); |
| Victor Zverovich | a7d94f0 | 2014-07-22 12:37:10 -0700 | [diff] [blame] | 876 | break; |
| Victor Zverovich | 75a2ea0 | 2014-09-25 07:10:44 -0700 | [diff] [blame] | 877 | case Arg::CSTRING: |
| 878 | arg.string.size = 0; |
| 879 | writer.write_str(arg.string, spec); |
| 880 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 881 | case Arg::STRING: |
| Victor Zverovich | 5320103 | 2014-06-30 14:26:29 -0700 | [diff] [blame] | 882 | writer.write_str(arg.string, spec); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 883 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 884 | case Arg::WSTRING: |
| Victor Zverovich | 512e2ce | 2014-07-14 08:47:03 -0700 | [diff] [blame] | 885 | writer.write_str(ignore_incompatible_str<Char>(arg.wstring), spec); |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 886 | break; |
| 887 | case Arg::POINTER: |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 888 | if (spec.type_ && spec.type_ != 'p') |
| Victor Zverovich | f634ccb | 2014-07-26 08:42:19 -0700 | [diff] [blame] | 889 | internal::report_unknown_type(spec.type_, "pointer"); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 890 | spec.flags_= HASH_FLAG; |
| 891 | spec.type_ = 'x'; |
| Victor Zverovich | ab6e759 | 2014-09-23 08:21:58 -0700 | [diff] [blame] | 892 | writer.write_int(reinterpret_cast<uintptr_t>(arg.pointer), spec); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 893 | break; |
| Victor Zverovich | 271fa8c | 2014-08-29 07:23:54 -0700 | [diff] [blame] | 894 | case Arg::CUSTOM: { |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 895 | if (spec.type_) |
| Victor Zverovich | f634ccb | 2014-07-26 08:42:19 -0700 | [diff] [blame] | 896 | internal::report_unknown_type(spec.type_, "object"); |
| Victor Zverovich | f9fc8fd | 2014-12-09 07:45:54 -0800 | [diff] [blame] | 897 | const void *str_format = "s"; |
| 898 | arg.custom.format(&writer, arg.custom.value, &str_format); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 899 | break; |
| Victor Zverovich | 271fa8c | 2014-08-29 07:23:54 -0700 | [diff] [blame] | 900 | } |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 901 | default: |
| 902 | assert(false); |
| 903 | break; |
| 904 | } |
| 905 | } |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 906 | write(writer, start, s); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | template <typename Char> |
| Victor Zverovich | b9a06ba | 2014-07-08 16:38:50 -0700 | [diff] [blame] | 910 | const Char *fmt::BasicFormatter<Char>::format( |
| Victor Zverovich | 271fa8c | 2014-08-29 07:23:54 -0700 | [diff] [blame] | 911 | const Char *&format_str, const Arg &arg) { |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 912 | const Char *s = format_str; |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 913 | FormatSpec spec; |
| 914 | if (*s == ':') { |
| 915 | if (arg.type == Arg::CUSTOM) { |
| Victor Zverovich | 271fa8c | 2014-08-29 07:23:54 -0700 | [diff] [blame] | 916 | arg.custom.format(this, arg.custom.value, &s); |
| 917 | return s; |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 918 | } |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 919 | ++s; |
| 920 | // Parse fill and alignment. |
| 921 | if (Char c = *s) { |
| 922 | const Char *p = s + 1; |
| 923 | spec.align_ = ALIGN_DEFAULT; |
| 924 | do { |
| 925 | switch (*p) { |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 926 | case '<': |
| 927 | spec.align_ = ALIGN_LEFT; |
| 928 | break; |
| 929 | case '>': |
| 930 | spec.align_ = ALIGN_RIGHT; |
| 931 | break; |
| 932 | case '=': |
| 933 | spec.align_ = ALIGN_NUMERIC; |
| 934 | break; |
| 935 | case '^': |
| 936 | spec.align_ = ALIGN_CENTER; |
| 937 | break; |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 938 | } |
| 939 | if (spec.align_ != ALIGN_DEFAULT) { |
| 940 | if (p != s) { |
| 941 | if (c == '}') break; |
| 942 | if (c == '{') |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 943 | FMT_THROW(FormatError("invalid fill character '{'")); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 944 | s += 2; |
| 945 | spec.fill_ = c; |
| 946 | } else ++s; |
| Victor Zverovich | 5debb2a | 2014-08-29 08:16:10 -0700 | [diff] [blame] | 947 | if (spec.align_ == ALIGN_NUMERIC) |
| 948 | require_numeric_argument(arg, '='); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 949 | break; |
| 950 | } |
| 951 | } while (--p >= s); |
| 952 | } |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 953 | |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 954 | // Parse sign. |
| 955 | switch (*s) { |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 956 | case '+': |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 957 | check_sign(s, arg); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 958 | spec.flags_ |= SIGN_FLAG | PLUS_FLAG; |
| 959 | break; |
| 960 | case '-': |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 961 | check_sign(s, arg); |
| Victor Zverovich | a7d94f0 | 2014-07-22 12:37:10 -0700 | [diff] [blame] | 962 | spec.flags_ |= MINUS_FLAG; |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 963 | break; |
| 964 | case ' ': |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 965 | check_sign(s, arg); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 966 | spec.flags_ |= SIGN_FLAG; |
| 967 | break; |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 968 | } |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 969 | |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 970 | if (*s == '#') { |
| Victor Zverovich | 5debb2a | 2014-08-29 08:16:10 -0700 | [diff] [blame] | 971 | require_numeric_argument(arg, '#'); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 972 | spec.flags_ |= HASH_FLAG; |
| 973 | ++s; |
| 974 | } |
| 975 | |
| 976 | // Parse width and zero flag. |
| 977 | if ('0' <= *s && *s <= '9') { |
| 978 | if (*s == '0') { |
| Victor Zverovich | 5debb2a | 2014-08-29 08:16:10 -0700 | [diff] [blame] | 979 | require_numeric_argument(arg, '0'); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 980 | spec.align_ = ALIGN_NUMERIC; |
| 981 | spec.fill_ = '0'; |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 982 | } |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 983 | // Zero may be parsed again as a part of the width, but it is simpler |
| 984 | // and more efficient than checking if the next char is a digit. |
| Victor Zverovich | 526b7fc | 2014-08-28 06:42:59 -0700 | [diff] [blame] | 985 | spec.width_ = parse_nonnegative_int(s); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 986 | } |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 987 | |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 988 | // Parse precision. |
| 989 | if (*s == '.') { |
| 990 | ++s; |
| 991 | spec.precision_ = 0; |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 992 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | 526b7fc | 2014-08-28 06:42:59 -0700 | [diff] [blame] | 993 | spec.precision_ = parse_nonnegative_int(s); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 994 | } else if (*s == '{') { |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 995 | ++s; |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 996 | const Arg &precision_arg = parse_arg_index(s); |
| Victor Zverovich | 42de4f1 | 2014-08-27 08:24:31 -0700 | [diff] [blame] | 997 | if (*s++ != '}') |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 998 | FMT_THROW(FormatError("invalid format string")); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 999 | ULongLong value = 0; |
| 1000 | switch (precision_arg.type) { |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 1001 | case Arg::INT: |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1002 | if (precision_arg.int_value < 0) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 1003 | FMT_THROW(FormatError("negative precision")); |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1004 | value = precision_arg.int_value; |
| 1005 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 1006 | case Arg::UINT: |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1007 | value = precision_arg.uint_value; |
| 1008 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 1009 | case Arg::LONG_LONG: |
| Victor Zverovich | 56f12b7 | 2013-11-22 07:45:43 -0800 | [diff] [blame] | 1010 | if (precision_arg.long_long_value < 0) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 1011 | FMT_THROW(FormatError("negative precision")); |
| Victor Zverovich | 56f12b7 | 2013-11-22 07:45:43 -0800 | [diff] [blame] | 1012 | value = precision_arg.long_long_value; |
| 1013 | break; |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 1014 | case Arg::ULONG_LONG: |
| Gregory Czajkowski | a65542b | 2013-11-18 22:58:39 -0800 | [diff] [blame] | 1015 | value = precision_arg.ulong_long_value; |
| Victor Zverovich | a4e72b4 | 2013-11-21 09:11:58 -0800 | [diff] [blame] | 1016 | break; |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1017 | default: |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 1018 | FMT_THROW(FormatError("precision is not integer")); |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1019 | } |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1020 | if (value > INT_MAX) |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 1021 | FMT_THROW(FormatError("number is too big")); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1022 | spec.precision_ = static_cast<int>(value); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1023 | } else { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 1024 | FMT_THROW(FormatError("missing precision specifier")); |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1025 | } |
| Victor Zverovich | 43aebf5 | 2015-01-08 07:56:08 -0800 | [diff] [blame] | 1026 | if (arg.type < Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) { |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 1027 | FMT_THROW(FormatError( |
| Victor Zverovich | 43aebf5 | 2015-01-08 07:56:08 -0800 | [diff] [blame] | 1028 | fmt::format("precision not allowed in {} format specifier", |
| 1029 | arg.type == Arg::POINTER ? "pointer" : "integer"))); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1030 | } |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1033 | // Parse type. |
| 1034 | if (*s != '}' && *s) |
| 1035 | spec.type_ = static_cast<char>(*s++); |
| 1036 | } |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1037 | |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1038 | if (*s++ != '}') |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 1039 | FMT_THROW(FormatError("missing '}' in format string")); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1040 | start_ = s; |
| 1041 | |
| 1042 | // Format argument. |
| Victor Zverovich | 4edc88f | 2014-07-16 08:38:15 -0700 | [diff] [blame] | 1043 | internal::ArgFormatter<Char>(*this, spec, s - 1).visit(arg); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1044 | return s; |
| 1045 | } |
| 1046 | |
| 1047 | template <typename Char> |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 1048 | void fmt::BasicFormatter<Char>::format( |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1049 | BasicStringRef<Char> format_str, const ArgList &args) { |
| 1050 | const Char *s = start_ = format_str.c_str(); |
| Victor Zverovich | 605d260 | 2014-08-29 07:45:55 -0700 | [diff] [blame] | 1051 | set_args(args); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1052 | while (*s) { |
| 1053 | Char c = *s++; |
| 1054 | if (c != '{' && c != '}') continue; |
| 1055 | if (*s == c) { |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 1056 | write(writer_, start_, s); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1057 | start_ = ++s; |
| 1058 | continue; |
| 1059 | } |
| 1060 | if (c == '}') |
| Victor Zverovich | 8b76e97 | 2014-10-06 08:30:55 -0700 | [diff] [blame] | 1061 | FMT_THROW(FormatError("unmatched '}' in format string")); |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 1062 | write(writer_, start_, s - 1); |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 1063 | Arg arg = parse_arg_index(s); |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1064 | s = format(s, arg); |
| 1065 | } |
| Victor Zverovich | c1db293 | 2014-07-24 08:53:27 -0700 | [diff] [blame] | 1066 | write(writer_, start_, s); |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 1069 | FMT_FUNC void fmt::report_system_error( |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 1070 | int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) { |
| Victor Zverovich | d8b9f41 | 2014-07-29 06:38:05 -0700 | [diff] [blame] | 1071 | report_error(internal::format_system_error, error_code, message); |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
| Victor Zverovich | 400812a | 2014-04-30 12:38:17 -0700 | [diff] [blame] | 1074 | #ifdef _WIN32 |
| Victor Zverovich | b49a1b4 | 2014-12-09 06:32:07 -0800 | [diff] [blame] | 1075 | FMT_FUNC void fmt::report_windows_error( |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 1076 | int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) { |
| Victor Zverovich | d8b9f41 | 2014-07-29 06:38:05 -0700 | [diff] [blame] | 1077 | report_error(internal::format_windows_error, error_code, message); |
| Victor Zverovich | 1a2d7be | 2014-05-03 09:48:54 -0700 | [diff] [blame] | 1078 | } |
| Victor Zverovich | 400812a | 2014-04-30 12:38:17 -0700 | [diff] [blame] | 1079 | #endif |
| Victor Zverovich | f793986 | 2014-04-30 10:18:11 -0700 | [diff] [blame] | 1080 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 1081 | FMT_FUNC void fmt::print(std::FILE *f, StringRef format_str, ArgList args) { |
| Victor Zverovich | d1ded56 | 2014-09-29 08:48:16 -0700 | [diff] [blame] | 1082 | MemoryWriter w; |
| Victor Zverovich | dd4323f | 2014-08-21 08:49:13 -0700 | [diff] [blame] | 1083 | w.write(format_str, args); |
| Victor Zverovich | d5b8196 | 2014-06-28 21:56:40 -0700 | [diff] [blame] | 1084 | std::fwrite(w.data(), 1, w.size(), f); |
| 1085 | } |
| 1086 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 1087 | FMT_FUNC void fmt::print(StringRef format_str, ArgList args) { |
| Victor Zverovich | 163178e | 2014-09-25 07:08:25 -0700 | [diff] [blame] | 1088 | print(stdout, format_str, args); |
| 1089 | } |
| 1090 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 1091 | FMT_FUNC void fmt::print(std::ostream &os, StringRef format_str, ArgList args) { |
| Victor Zverovich | d1ded56 | 2014-09-29 08:48:16 -0700 | [diff] [blame] | 1092 | MemoryWriter w; |
| Victor Zverovich | dd4323f | 2014-08-21 08:49:13 -0700 | [diff] [blame] | 1093 | w.write(format_str, args); |
| Victor Zverovich | e3a44c1 | 2014-07-09 06:56:36 -0700 | [diff] [blame] | 1094 | os.write(w.data(), w.size()); |
| 1095 | } |
| 1096 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 1097 | FMT_FUNC void fmt::print_colored(Color c, StringRef format, ArgList args) { |
| Victor Zverovich | 2dc108b | 2014-07-01 09:10:43 -0700 | [diff] [blame] | 1098 | char escape[] = "\x1b[30m"; |
| 1099 | escape[3] = '0' + static_cast<char>(c); |
| 1100 | std::fputs(escape, stdout); |
| 1101 | print(format, args); |
| 1102 | std::fputs(RESET_COLOR, stdout); |
| 1103 | } |
| 1104 | |
| Victor Zverovich | d9c605c | 2014-11-28 06:40:57 -0800 | [diff] [blame] | 1105 | FMT_FUNC int fmt::fprintf(std::FILE *f, StringRef format, ArgList args) { |
| Victor Zverovich | d1ded56 | 2014-09-29 08:48:16 -0700 | [diff] [blame] | 1106 | MemoryWriter w; |
| Victor Zverovich | 21111cc | 2014-06-29 19:52:26 -0700 | [diff] [blame] | 1107 | printf(w, format, args); |
| Victor Zverovich | 615c1ee | 2014-11-14 09:40:01 -0800 | [diff] [blame] | 1108 | std::size_t size = w.size(); |
| 1109 | return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size); |
| Victor Zverovich | d5b8196 | 2014-06-28 21:56:40 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 1112 | // Explicit instantiations for char. |
| 1113 | |
| Victor Zverovich | f43caef | 2014-09-25 07:21:48 -0700 | [diff] [blame] | 1114 | template const char *fmt::BasicFormatter<char>::format( |
| 1115 | const char *&format_str, const fmt::internal::Arg &arg); |
| 1116 | |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 1117 | template void fmt::BasicFormatter<char>::format( |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1118 | BasicStringRef<char> format, const ArgList &args); |
| Victor Zverovich | 7cae763 | 2013-09-06 20:23:42 -0700 | [diff] [blame] | 1119 | |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 1120 | template void fmt::internal::PrintfFormatter<char>::format( |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 1121 | BasicWriter<char> &writer, BasicStringRef<char> format, const ArgList &args); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 1122 | |
| Victor Zverovich | 14f2577 | 2014-09-19 08:45:05 -0700 | [diff] [blame] | 1123 | template int fmt::internal::CharTraits<char>::format_float( |
| 1124 | char *buffer, std::size_t size, const char *format, |
| 1125 | unsigned width, int precision, double value); |
| 1126 | |
| 1127 | template int fmt::internal::CharTraits<char>::format_float( |
| 1128 | char *buffer, std::size_t size, const char *format, |
| 1129 | unsigned width, int precision, long double value); |
| 1130 | |
| Victor Zverovich | 9ff3b97 | 2013-09-07 10:15:08 -0700 | [diff] [blame] | 1131 | // Explicit instantiations for wchar_t. |
| 1132 | |
| Victor Zverovich | f43caef | 2014-09-25 07:21:48 -0700 | [diff] [blame] | 1133 | template const wchar_t *fmt::BasicFormatter<wchar_t>::format( |
| 1134 | const wchar_t *&format_str, const fmt::internal::Arg &arg); |
| 1135 | |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 1136 | template void fmt::BasicFormatter<wchar_t>::format( |
| Victor Zverovich | e825156 | 2014-07-08 16:20:33 -0700 | [diff] [blame] | 1137 | BasicStringRef<wchar_t> format, const ArgList &args); |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 1138 | |
| Victor Zverovich | 4d049cf | 2014-07-28 08:41:50 -0700 | [diff] [blame] | 1139 | template void fmt::internal::PrintfFormatter<wchar_t>::format( |
| Victor Zverovich | bf8b29f | 2014-06-06 06:38:37 -0700 | [diff] [blame] | 1140 | BasicWriter<wchar_t> &writer, BasicStringRef<wchar_t> format, |
| Victor Zverovich | ea99bfb | 2014-06-24 07:54:26 -0700 | [diff] [blame] | 1141 | const ArgList &args); |
| jdale88 | a9862fd | 2014-03-11 18:56:24 +0000 | [diff] [blame] | 1142 | |
| Victor Zverovich | 14f2577 | 2014-09-19 08:45:05 -0700 | [diff] [blame] | 1143 | template int fmt::internal::CharTraits<wchar_t>::format_float( |
| 1144 | wchar_t *buffer, std::size_t size, const wchar_t *format, |
| 1145 | unsigned width, int precision, double value); |
| 1146 | |
| 1147 | template int fmt::internal::CharTraits<wchar_t>::format_float( |
| 1148 | wchar_t *buffer, std::size_t size, const wchar_t *format, |
| 1149 | unsigned width, int precision, long double value); |
| 1150 | |
| jdale88 | a9862fd | 2014-03-11 18:56:24 +0000 | [diff] [blame] | 1151 | #if _MSC_VER |
| 1152 | # pragma warning(pop) |
| 1153 | #endif |