| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 1 | /* |
| Victor Zverovich | faccb4c | 2012-12-12 07:44:41 -0800 | [diff] [blame] | 2 | String formatting library for C++ |
| 3 | |
| 4 | Copyright (c) 2012, Victor Zverovich |
| 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 | d53f209 | 2012-12-16 15:06:31 -0800 | [diff] [blame] | 28 | // Disable useless MSVC warnings. |
| Victor Zverovich | f8c9106 | 2012-12-17 15:41:00 -0800 | [diff] [blame] | 29 | #undef _CRT_SECURE_NO_WARNINGS |
| Victor Zverovich | d53f209 | 2012-12-16 15:06:31 -0800 | [diff] [blame] | 30 | #define _CRT_SECURE_NO_WARNINGS |
| Victor Zverovich | c240a12 | 2012-12-21 15:02:25 -0800 | [diff] [blame] | 31 | #undef _SCL_SECURE_NO_WARNINGS |
| 32 | #define _SCL_SECURE_NO_WARNINGS |
| Victor Zverovich | d53f209 | 2012-12-16 15:06:31 -0800 | [diff] [blame] | 33 | |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 34 | #include <math.h> |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 35 | |
| Victor Zverovich | fbfedcf | 2013-01-14 15:16:20 -0800 | [diff] [blame] | 36 | // Wrap signbit because when compiled in C++11 mode signbit is no longer a |
| 37 | // macro but a function defined in namespace std and the macro is undefined. |
| 38 | #ifndef _MSC_VER |
| 39 | inline int SignBit(double value) { return signbit(value); } |
| 40 | #endif |
| 41 | |
| 42 | #include "format.h" |
| 43 | |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 44 | #include <cassert> |
| Victor Zverovich | 72f896d | 2012-12-12 09:17:28 -0800 | [diff] [blame] | 45 | #include <cctype> |
| Victor Zverovich | 63539c0 | 2012-12-08 08:17:12 -0800 | [diff] [blame] | 46 | #include <climits> |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 47 | #include <cstring> |
| 48 | #include <algorithm> |
| 49 | |
| 50 | using std::size_t; |
| Victor Zverovich | 3f73987 | 2012-12-25 17:55:41 -0800 | [diff] [blame] | 51 | using fmt::BasicFormatter; |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 52 | using fmt::IntFormatter; |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 53 | using fmt::Formatter; |
| Victor Zverovich | 3c90a87 | 2013-01-12 10:08:16 -0800 | [diff] [blame] | 54 | using fmt::AlignSpec; |
| Victor Zverovich | f28ecaf | 2012-12-21 09:14:18 -0800 | [diff] [blame] | 55 | using fmt::FormatSpec; |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 56 | using fmt::WidthSpec; |
| Victor Zverovich | 0f46a3d | 2012-12-25 13:30:42 -0800 | [diff] [blame] | 57 | using fmt::StringRef; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 58 | |
| Victor Zverovich | e8ba960 | 2012-12-12 09:29:50 -0800 | [diff] [blame] | 59 | #if _MSC_VER |
| Victor Zverovich | f8c9106 | 2012-12-17 15:41:00 -0800 | [diff] [blame] | 60 | # undef snprintf |
| Victor Zverovich | e8ba960 | 2012-12-12 09:29:50 -0800 | [diff] [blame] | 61 | # define snprintf _snprintf |
| Victor Zverovich | 251e877 | 2012-12-29 09:54:57 -0800 | [diff] [blame] | 62 | # define isinf(x) (!_finite(x)) |
| Victor Zverovich | e8ba960 | 2012-12-12 09:29:50 -0800 | [diff] [blame] | 63 | #endif |
| 64 | |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 65 | namespace { |
| 66 | |
| Victor Zverovich | de17baa | 2013-01-04 09:14:34 -0800 | [diff] [blame] | 67 | #ifdef _MSC_VER |
| Victor Zverovich | fbfedcf | 2013-01-14 15:16:20 -0800 | [diff] [blame] | 68 | int SignBit(double value) { |
| Victor Zverovich | de17baa | 2013-01-04 09:14:34 -0800 | [diff] [blame] | 69 | if (value < 0) return 1; |
| 70 | if (value == value) return 0; |
| 71 | int dec = 0, sign = 0; |
| 72 | _ecvt(value, 0, &dec, &sign); |
| 73 | return sign; |
| 74 | } |
| 75 | #endif |
| 76 | } |
| 77 | |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame^] | 78 | const char fmt::internal::DIGITS[] = |
| 79 | "0001020304050607080910111213141516171819" |
| 80 | "2021222324252627282930313233343536373839" |
| 81 | "4041424344454647484950515253545556575859" |
| 82 | "6061626364656667686970717273747576777879" |
| 83 | "8081828384858687888990919293949596979899"; |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 84 | |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame^] | 85 | void fmt::internal::ReportUnknownType(char code, const char *type) { |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 86 | if (std::isprint(static_cast<unsigned char>(code))) { |
| 87 | throw fmt::FormatError(fmt::str( |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame^] | 88 | fmt::Format("unknown format code '{}' for {}") << code << type)); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 89 | } |
| 90 | throw fmt::FormatError( |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame^] | 91 | fmt::str(fmt::Format("unknown format code '\\x{:02x}' for {}") |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 92 | << static_cast<unsigned>(code) << type)); |
| 93 | } |
| 94 | |
| Victor Zverovich | de17baa | 2013-01-04 09:14:34 -0800 | [diff] [blame] | 95 | // Throws Exception(message) if format contains '}', otherwise throws |
| 96 | // FormatError reporting unmatched '{'. The idea is that unmatched '{' |
| 97 | // should override other errors. |
| 98 | void Formatter::ReportError(const char *s, StringRef message) const { |
| 99 | for (int num_open_braces = num_open_braces_; *s; ++s) { |
| 100 | if (*s == '{') { |
| 101 | ++num_open_braces; |
| 102 | } else if (*s == '}') { |
| 103 | if (--num_open_braces == 0) |
| 104 | throw fmt::FormatError(message); |
| 105 | } |
| 106 | } |
| 107 | throw fmt::FormatError("unmatched '{' in format"); |
| 108 | } |
| 109 | |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 110 | // Parses an unsigned integer advancing s to the end of the parsed input. |
| 111 | // This function assumes that the first character of s is a digit. |
| 112 | unsigned Formatter::ParseUInt(const char *&s) const { |
| 113 | assert('0' <= *s && *s <= '9'); |
| 114 | unsigned value = 0; |
| 115 | do { |
| 116 | unsigned new_value = value * 10 + (*s++ - '0'); |
| 117 | if (new_value < value) // Check if value wrapped around. |
| 118 | ReportError(s, "number is too big in format"); |
| 119 | value = new_value; |
| 120 | } while ('0' <= *s && *s <= '9'); |
| 121 | return value; |
| 122 | } |
| 123 | |
| Victor Zverovich | de17baa | 2013-01-04 09:14:34 -0800 | [diff] [blame] | 124 | inline const Formatter::Arg &Formatter::ParseArgIndex(const char *&s) { |
| Victor Zverovich | 8412ad6 | 2012-12-27 06:56:55 -0800 | [diff] [blame] | 125 | unsigned arg_index = 0; |
| 126 | if (*s < '0' || *s > '9') { |
| 127 | if (*s != '}' && *s != ':') |
| 128 | ReportError(s, "invalid argument index in format string"); |
| 129 | if (next_arg_index_ < 0) { |
| 130 | ReportError(s, |
| 131 | "cannot switch from manual to automatic argument indexing"); |
| 132 | } |
| 133 | arg_index = next_arg_index_++; |
| 134 | } else { |
| 135 | if (next_arg_index_ > 0) { |
| 136 | ReportError(s, |
| 137 | "cannot switch from automatic to manual argument indexing"); |
| 138 | } |
| 139 | next_arg_index_ = -1; |
| 140 | arg_index = ParseUInt(s); |
| 141 | if (arg_index >= args_.size()) |
| 142 | ReportError(s, "argument index is out of range in format"); |
| 143 | } |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 144 | return *args_[arg_index]; |
| 145 | } |
| 146 | |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 147 | void Formatter::CheckSign(const char *&s, const Arg &arg) { |
| 148 | if (arg.type > LAST_NUMERIC_TYPE) { |
| 149 | ReportError(s, |
| 150 | Format("format specifier '{0}' requires numeric argument") << *s); |
| 151 | } |
| 152 | if (arg.type == UINT || arg.type == ULONG) { |
| 153 | ReportError(s, |
| 154 | Format("format specifier '{0}' requires signed argument") << *s); |
| 155 | } |
| 156 | ++s; |
| 157 | } |
| 158 | |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 159 | void Formatter::DoFormat() { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 160 | const char *start = format_; |
| Victor Zverovich | 57dbd2c | 2012-12-11 12:23:52 -0800 | [diff] [blame] | 161 | format_ = 0; |
| Victor Zverovich | 8412ad6 | 2012-12-27 06:56:55 -0800 | [diff] [blame] | 162 | next_arg_index_ = 0; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 163 | const char *s = start; |
| Victor Zverovich | 5f3ed20 | 2012-12-07 17:48:10 -0800 | [diff] [blame] | 164 | while (*s) { |
| Victor Zverovich | 31a5070 | 2012-12-10 15:04:55 -0800 | [diff] [blame] | 165 | char c = *s++; |
| 166 | if (c != '{' && c != '}') continue; |
| 167 | if (*s == c) { |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 168 | buffer_.append(start, s); |
| Victor Zverovich | 31a5070 | 2012-12-10 15:04:55 -0800 | [diff] [blame] | 169 | start = ++s; |
| 170 | continue; |
| 171 | } |
| 172 | if (c == '}') |
| 173 | throw FormatError("unmatched '}' in format"); |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 174 | num_open_braces_= 1; |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 175 | buffer_.append(start, s - 1); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 176 | |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 177 | const Arg &arg = ParseArgIndex(s); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 178 | |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 179 | FormatSpec spec; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 180 | int precision = -1; |
| 181 | if (*s == ':') { |
| 182 | ++s; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 183 | |
| 184 | // Parse fill and alignment. |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 185 | if (char c = *s) { |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 186 | const char *p = s + 1; |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 187 | spec.align_ = ALIGN_DEFAULT; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 188 | do { |
| 189 | switch (*p) { |
| 190 | case '<': |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 191 | spec.align_ = ALIGN_LEFT; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 192 | break; |
| 193 | case '>': |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 194 | spec.align_ = ALIGN_RIGHT; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 195 | break; |
| 196 | case '=': |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 197 | spec.align_ = ALIGN_NUMERIC; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 198 | break; |
| 199 | case '^': |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 200 | spec.align_ = ALIGN_CENTER; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 201 | break; |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 202 | } |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 203 | if (spec.align_ != ALIGN_DEFAULT) { |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 204 | if (p != s) { |
| 205 | if (c == '}') break; |
| 206 | if (c == '{') |
| 207 | ReportError(s, "invalid fill character '{'"); |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 208 | s += 2; |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 209 | spec.fill_ = c; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 210 | } else ++s; |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 211 | if (spec.align_ == ALIGN_NUMERIC && arg.type > LAST_NUMERIC_TYPE) |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 212 | ReportError(s, "format specifier '=' requires numeric argument"); |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | } while (--p >= s); |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 216 | } |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 217 | |
| 218 | // Parse sign. |
| Victor Zverovich | 0a138ad | 2012-12-25 13:25:14 -0800 | [diff] [blame] | 219 | switch (*s) { |
| 220 | case '+': |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 221 | CheckSign(s, arg); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 222 | spec.flags_ |= SIGN_FLAG | PLUS_FLAG; |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 223 | break; |
| Victor Zverovich | 0a138ad | 2012-12-25 13:25:14 -0800 | [diff] [blame] | 224 | case '-': |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 225 | CheckSign(s, arg); |
| 226 | break; |
| 227 | case ' ': |
| 228 | CheckSign(s, arg); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 229 | spec.flags_ |= SIGN_FLAG; |
| Victor Zverovich | 0a138ad | 2012-12-25 13:25:14 -0800 | [diff] [blame] | 230 | break; |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 231 | } |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 232 | |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 233 | if (*s == '#') { |
| 234 | if (arg.type > LAST_NUMERIC_TYPE) |
| 235 | ReportError(s, "format specifier '#' requires numeric argument"); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 236 | spec.flags_ |= HASH_FLAG; |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 237 | ++s; |
| 238 | } |
| 239 | |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 240 | // Parse width and zero flag. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 241 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 242 | if (*s == '0') { |
| 243 | if (arg.type > LAST_NUMERIC_TYPE) |
| 244 | ReportError(s, "format specifier '0' requires numeric argument"); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 245 | spec.align_ = ALIGN_NUMERIC; |
| 246 | spec.fill_ = '0'; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 247 | } |
| 248 | // Zero may be parsed again as a part of the width, but it is simpler |
| 249 | // and more efficient than checking if the next char is a digit. |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 250 | unsigned value = ParseUInt(s); |
| 251 | if (value > INT_MAX) |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 252 | ReportError(s, "number is too big in format"); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 253 | spec.width_ = value; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Parse precision. |
| 257 | if (*s == '.') { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 258 | ++s; |
| 259 | precision = 0; |
| 260 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 261 | unsigned value = ParseUInt(s); |
| 262 | if (value > INT_MAX) |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 263 | ReportError(s, "number is too big in format"); |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 264 | precision = value; |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 265 | } else if (*s == '{') { |
| 266 | ++s; |
| 267 | ++num_open_braces_; |
| 268 | const Arg &precision_arg = ParseArgIndex(s); |
| 269 | unsigned long value = 0; |
| 270 | switch (precision_arg.type) { |
| 271 | case INT: |
| 272 | if (precision_arg.int_value < 0) |
| 273 | ReportError(s, "negative precision in format"); |
| 274 | value = precision_arg.int_value; |
| 275 | break; |
| 276 | case UINT: |
| 277 | value = precision_arg.uint_value; |
| 278 | break; |
| 279 | case LONG: |
| 280 | if (precision_arg.long_value < 0) |
| 281 | ReportError(s, "negative precision in format"); |
| 282 | value = precision_arg.long_value; |
| 283 | break; |
| 284 | case ULONG: |
| 285 | value = precision_arg.ulong_value; |
| 286 | break; |
| 287 | default: |
| 288 | ReportError(s, "precision is not integer"); |
| 289 | } |
| 290 | if (value > INT_MAX) |
| 291 | ReportError(s, "number is too big in format"); |
| 292 | precision = value; |
| 293 | if (*s++ != '}') |
| 294 | throw FormatError("unmatched '{' in format"); |
| 295 | --num_open_braces_; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 296 | } else { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 297 | ReportError(s, "missing precision in format"); |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 298 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 299 | if (arg.type != DOUBLE && arg.type != LONG_DOUBLE) { |
| 300 | ReportError(s, |
| 301 | "precision specifier requires floating-point argument"); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | // Parse type. |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 306 | if (*s != '}' && *s) |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 307 | spec.type_ = *s++; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (*s++ != '}') |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 311 | throw FormatError("unmatched '{' in format"); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 312 | start = s; |
| 313 | |
| 314 | // Format argument. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 315 | switch (arg.type) { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 316 | case INT: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 317 | FormatInt(arg.int_value, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 318 | break; |
| 319 | case UINT: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 320 | FormatInt(arg.uint_value, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 321 | break; |
| 322 | case LONG: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 323 | FormatInt(arg.long_value, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 324 | break; |
| 325 | case ULONG: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 326 | FormatInt(arg.ulong_value, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 327 | break; |
| 328 | case DOUBLE: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 329 | FormatDouble(arg.double_value, spec, precision); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 330 | break; |
| 331 | case LONG_DOUBLE: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 332 | FormatDouble(arg.long_double_value, spec, precision); |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 333 | break; |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 334 | case CHAR: { |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 335 | if (spec.type_ && spec.type_ != 'c') |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame^] | 336 | internal::ReportUnknownType(spec.type_, "char"); |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 337 | char *out = 0; |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 338 | if (spec.width_ > 1) { |
| 339 | out = GrowBuffer(spec.width_); |
| 340 | if (spec.align_ == ALIGN_RIGHT) { |
| 341 | std::fill_n(out, spec.width_ - 1, spec.fill_); |
| 342 | out += spec.width_ - 1; |
| 343 | } else if (spec.align_ == ALIGN_CENTER) { |
| 344 | out = FillPadding(out, spec.width_, 1, spec.fill_); |
| Victor Zverovich | 26d2ae6 | 2012-12-24 11:58:49 -0800 | [diff] [blame] | 345 | } else { |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 346 | std::fill_n(out + 1, spec.width_ - 1, spec.fill_); |
| Victor Zverovich | 26d2ae6 | 2012-12-24 11:58:49 -0800 | [diff] [blame] | 347 | } |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 348 | } else { |
| 349 | out = GrowBuffer(1); |
| 350 | } |
| 351 | *out = arg.int_value; |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 352 | break; |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 353 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 354 | case STRING: { |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 355 | if (spec.type_ && spec.type_ != 's') |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame^] | 356 | internal::ReportUnknownType(spec.type_, "string"); |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame] | 357 | const char *str = arg.string.value; |
| 358 | size_t size = arg.string.size; |
| Victor Zverovich | 1b3c197 | 2012-12-17 16:39:49 -0800 | [diff] [blame] | 359 | if (size == 0) { |
| 360 | if (!str) |
| 361 | throw FormatError("string pointer is null"); |
| 362 | if (*str) |
| 363 | size = std::strlen(str); |
| 364 | } |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 365 | FormatString(str, size, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 366 | break; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 367 | } |
| 368 | case POINTER: |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 369 | if (spec.type_ && spec.type_ != 'p') |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame^] | 370 | internal::ReportUnknownType(spec.type_, "pointer"); |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 371 | spec.flags_= HASH_FLAG; |
| 372 | spec.type_ = 'x'; |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 373 | FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 374 | break; |
| Victor Zverovich | 280ea8b | 2012-12-09 09:03:47 -0800 | [diff] [blame] | 375 | case CUSTOM: |
| Victor Zverovich | 877abaf | 2013-01-08 09:56:05 -0800 | [diff] [blame] | 376 | if (spec.type_) |
| Victor Zverovich | 687301c | 2013-01-26 16:07:28 -0800 | [diff] [blame^] | 377 | internal::ReportUnknownType(spec.type_, "object"); |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 378 | (this->*arg.custom.format)(arg.custom.value, spec); |
| Victor Zverovich | 63539c0 | 2012-12-08 08:17:12 -0800 | [diff] [blame] | 379 | break; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 380 | default: |
| 381 | assert(false); |
| 382 | break; |
| 383 | } |
| 384 | } |
| Victor Zverovich | de17baa | 2013-01-04 09:14:34 -0800 | [diff] [blame] | 385 | buffer_.append(start, s); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 386 | } |