| 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 | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 34 | #include "format.h" |
| 35 | |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 36 | #include <math.h> |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 37 | #include <stdint.h> |
| 38 | |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 39 | #include <cassert> |
| Victor Zverovich | 72f896d | 2012-12-12 09:17:28 -0800 | [diff] [blame] | 40 | #include <cctype> |
| Victor Zverovich | 63539c0 | 2012-12-08 08:17:12 -0800 | [diff] [blame] | 41 | #include <climits> |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 42 | #include <cstring> |
| 43 | #include <algorithm> |
| 44 | |
| 45 | using std::size_t; |
| Victor Zverovich | 3f73987 | 2012-12-25 17:55:41 -0800 | [diff] [blame] | 46 | using fmt::BasicFormatter; |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 47 | using fmt::Formatter; |
| Victor Zverovich | f28ecaf | 2012-12-21 09:14:18 -0800 | [diff] [blame] | 48 | using fmt::FormatSpec; |
| Victor Zverovich | 0f46a3d | 2012-12-25 13:30:42 -0800 | [diff] [blame] | 49 | using fmt::StringRef; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 50 | |
| Victor Zverovich | e8ba960 | 2012-12-12 09:29:50 -0800 | [diff] [blame] | 51 | #if _MSC_VER |
| Victor Zverovich | f8c9106 | 2012-12-17 15:41:00 -0800 | [diff] [blame] | 52 | # undef snprintf |
| Victor Zverovich | e8ba960 | 2012-12-12 09:29:50 -0800 | [diff] [blame] | 53 | # define snprintf _snprintf |
| 54 | #endif |
| 55 | |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 56 | namespace { |
| 57 | |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 58 | // Flags. |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 59 | enum { SIGN_FLAG = 1, PLUS_FLAG = 2, HASH_FLAG = 4 }; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 60 | |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 61 | void ReportUnknownType(char code, const char *type) { |
| Victor Zverovich | 1dc0cc3 | 2012-12-16 15:56:44 -0800 | [diff] [blame] | 62 | if (std::isprint(static_cast<unsigned char>(code))) { |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 63 | throw fmt::FormatError( |
| 64 | str(fmt::Format("unknown format code '{0}' for {1}") << code << type)); |
| 65 | } |
| 66 | throw fmt::FormatError( |
| 67 | str(fmt::Format("unknown format code '\\x{0:02x}' for {1}") |
| 68 | << static_cast<unsigned>(code) << type)); |
| 69 | } |
| 70 | |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame] | 71 | // Information about an integer type. |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 72 | template <typename T> |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame] | 73 | struct IntTraits { |
| 74 | typedef T UnsignedType; |
| 75 | static bool IsNegative(T) { return false; } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | template <> |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame] | 79 | struct IntTraits<int> { |
| 80 | typedef unsigned UnsignedType; |
| 81 | static bool IsNegative(int value) { return value < 0; } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | template <> |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame] | 85 | struct IntTraits<long> { |
| 86 | typedef unsigned long UnsignedType; |
| 87 | static bool IsNegative(long value) { return value < 0; } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | template <typename T> |
| 91 | struct IsLongDouble { enum {VALUE = 0}; }; |
| 92 | |
| 93 | template <> |
| 94 | struct IsLongDouble<long double> { enum {VALUE = 1}; }; |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 95 | |
| Victor Zverovich | 4d1ee0b | 2012-12-24 18:19:33 -0800 | [diff] [blame] | 96 | inline unsigned CountDigits(uint64_t n) { |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 97 | unsigned count = 1; |
| 98 | for (;;) { |
| 99 | // Integer division is slow so do it for a group of four digits instead |
| 100 | // of for every digit. The idea comes from the talk by Alexandrescu |
| 101 | // "Three Optimization Tips for C++". See speed-test for a comparison. |
| 102 | if (n < 10) return count; |
| 103 | if (n < 100) return count + 1; |
| 104 | if (n < 1000) return count + 2; |
| 105 | if (n < 10000) return count + 3; |
| 106 | n /= 10000u; |
| 107 | count += 4; |
| 108 | } |
| 109 | } |
| Victor Zverovich | d963379 | 2012-12-25 09:00:11 -0800 | [diff] [blame] | 110 | |
| 111 | const char DIGITS[] = |
| 112 | "0001020304050607080910111213141516171819" |
| 113 | "2021222324252627282930313233343536373839" |
| 114 | "4041424344454647484950515253545556575859" |
| 115 | "6061626364656667686970717273747576777879" |
| 116 | "8081828384858687888990919293949596979899"; |
| 117 | |
| 118 | void FormatDecimal(char *buffer, uint64_t value, unsigned num_digits) { |
| 119 | --num_digits; |
| 120 | while (value >= 100) { |
| 121 | // Integer division is slow so do it for a group of two digits instead |
| 122 | // of for every digit. The idea comes from the talk by Alexandrescu |
| 123 | // "Three Optimization Tips for C++". See speed-test for a comparison. |
| 124 | unsigned index = (value % 100) * 2; |
| 125 | value /= 100; |
| 126 | buffer[num_digits] = DIGITS[index + 1]; |
| 127 | buffer[num_digits - 1] = DIGITS[index]; |
| 128 | num_digits -= 2; |
| 129 | } |
| 130 | if (value < 10) { |
| 131 | *buffer = static_cast<char>('0' + value); |
| 132 | return; |
| 133 | } |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 134 | unsigned index = static_cast<unsigned>(value * 2); |
| Victor Zverovich | d963379 | 2012-12-25 09:00:11 -0800 | [diff] [blame] | 135 | buffer[1] = DIGITS[index + 1]; |
| 136 | buffer[0] = DIGITS[index]; |
| 137 | } |
| Victor Zverovich | 10108c7 | 2012-12-28 09:08:29 -0800 | [diff] [blame] | 138 | |
| 139 | #ifdef _MSC_VER |
| 140 | int signbit(double value) { |
| 141 | if (value < 0) return 1; |
| Victor Zverovich | 7b97028 | 2012-12-28 09:24:06 -0800 | [diff] [blame] | 142 | if (value == value) return 0; |
| Victor Zverovich | 10108c7 | 2012-12-28 09:08:29 -0800 | [diff] [blame] | 143 | int dec = 0, sign = 0; |
| Victor Zverovich | 4762a8a | 2012-12-29 06:44:14 -0800 | [diff] [blame^] | 144 | _ecvt(value, 0, &dec, &sign); |
| Victor Zverovich | 10108c7 | 2012-12-28 09:08:29 -0800 | [diff] [blame] | 145 | return sign; |
| 146 | } |
| 147 | #endif |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| Victor Zverovich | 3f73987 | 2012-12-25 17:55:41 -0800 | [diff] [blame] | 150 | void BasicFormatter::operator<<(int value) { |
| 151 | unsigned abs_value = value; |
| 152 | unsigned num_digits = 0; |
| 153 | char *out = 0; |
| 154 | if (value >= 0) { |
| 155 | num_digits = CountDigits(abs_value); |
| 156 | out = GrowBuffer(num_digits); |
| 157 | } else { |
| 158 | abs_value = 0 - abs_value; |
| 159 | num_digits = CountDigits(abs_value); |
| 160 | out = GrowBuffer(num_digits + 1); |
| 161 | *out++ = '-'; |
| 162 | } |
| 163 | FormatDecimal(out, abs_value, num_digits); |
| 164 | } |
| 165 | |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 166 | // Throws Exception(message) if format contains '}', otherwise throws |
| 167 | // FormatError reporting unmatched '{'. The idea is that unmatched '{' |
| 168 | // should override other errors. |
| Victor Zverovich | 0a138ad | 2012-12-25 13:25:14 -0800 | [diff] [blame] | 169 | void Formatter::ReportError(const char *s, StringRef message) const { |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 170 | for (int num_open_braces = num_open_braces_; *s; ++s) { |
| 171 | if (*s == '{') { |
| 172 | ++num_open_braces; |
| 173 | } else if (*s == '}') { |
| 174 | if (--num_open_braces == 0) |
| 175 | throw fmt::FormatError(message); |
| 176 | } |
| 177 | } |
| 178 | throw fmt::FormatError("unmatched '{' in format"); |
| 179 | } |
| 180 | |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 181 | // Fills the padding around the content and returns the pointer to the |
| 182 | // content area. |
| 183 | char *FillPadding(char *buffer, |
| Victor Zverovich | 3f73987 | 2012-12-25 17:55:41 -0800 | [diff] [blame] | 184 | unsigned total_size, std::size_t content_size, char fill) { |
| Victor Zverovich | 8412ad6 | 2012-12-27 06:56:55 -0800 | [diff] [blame] | 185 | std::size_t padding = total_size - content_size; |
| 186 | std::size_t left_padding = padding / 2; |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 187 | std::fill_n(buffer, left_padding, fill); |
| 188 | buffer += left_padding; |
| 189 | char *content = buffer; |
| 190 | std::fill_n(buffer + content_size, padding - left_padding, fill); |
| 191 | return content; |
| 192 | } |
| 193 | |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 194 | char *Formatter::PrepareFilledBuffer( |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 195 | unsigned size, const FormatSpec &spec, char sign) { |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 196 | if (spec.width <= size) { |
| 197 | char *p = GrowBuffer(size); |
| 198 | *p = sign; |
| 199 | return p + size - 1; |
| 200 | } |
| 201 | char *p = GrowBuffer(spec.width); |
| 202 | char *end = p + spec.width; |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 203 | if (spec.align == ALIGN_LEFT) { |
| 204 | *p = sign; |
| 205 | p += size; |
| 206 | std::fill(p, end, spec.fill); |
| 207 | } else if (spec.align == ALIGN_CENTER) { |
| 208 | p = FillPadding(p, spec.width, size, spec.fill); |
| 209 | *p = sign; |
| 210 | p += size; |
| 211 | } else { |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 212 | if (spec.align == ALIGN_NUMERIC) { |
| 213 | if (sign) { |
| 214 | *p++ = sign; |
| 215 | --size; |
| 216 | } |
| 217 | } else { |
| 218 | *(end - size) = sign; |
| 219 | } |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 220 | std::fill(p, end - size, spec.fill); |
| 221 | p = end; |
| 222 | } |
| 223 | return p - 1; |
| 224 | } |
| 225 | |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 226 | template <typename T> |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 227 | void Formatter::FormatInt(T value, const FormatSpec &spec) { |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 228 | unsigned size = 0; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 229 | char sign = 0; |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame] | 230 | typedef typename IntTraits<T>::UnsignedType UnsignedType; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 231 | UnsignedType abs_value = value; |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame] | 232 | if (IntTraits<T>::IsNegative(value)) { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 233 | sign = '-'; |
| 234 | ++size; |
| Victor Zverovich | 5161376 | 2012-12-21 15:43:10 -0800 | [diff] [blame] | 235 | abs_value = 0 - abs_value; |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 236 | } else if ((spec.flags & SIGN_FLAG) != 0) { |
| 237 | sign = (spec.flags & PLUS_FLAG) != 0 ? '+' : ' '; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 238 | ++size; |
| 239 | } |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 240 | switch (spec.type) { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 241 | case 0: case 'd': { |
| Victor Zverovich | d963379 | 2012-12-25 09:00:11 -0800 | [diff] [blame] | 242 | unsigned num_digits = CountDigits(abs_value); |
| 243 | char *p = PrepareFilledBuffer(size + num_digits, spec, sign) |
| 244 | - num_digits + 1; |
| 245 | FormatDecimal(p, abs_value, num_digits); |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | case 'x': case 'X': { |
| 249 | UnsignedType n = abs_value; |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 250 | bool print_prefix = (spec.flags & HASH_FLAG) != 0; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 251 | if (print_prefix) size += 2; |
| 252 | do { |
| 253 | ++size; |
| 254 | } while ((n >>= 4) != 0); |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 255 | char *p = PrepareFilledBuffer(size, spec, sign); |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 256 | n = abs_value; |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 257 | const char *digits = spec.type == 'x' ? |
| 258 | "0123456789abcdef" : "0123456789ABCDEF"; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 259 | do { |
| 260 | *p-- = digits[n & 0xf]; |
| 261 | } while ((n >>= 4) != 0); |
| 262 | if (print_prefix) { |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 263 | *p-- = spec.type; |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 264 | *p = '0'; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 265 | } |
| 266 | break; |
| 267 | } |
| 268 | case 'o': { |
| 269 | UnsignedType n = abs_value; |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 270 | bool print_prefix = (spec.flags & HASH_FLAG) != 0; |
| 271 | if (print_prefix) ++size; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 272 | do { |
| 273 | ++size; |
| 274 | } while ((n >>= 3) != 0); |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 275 | char *p = PrepareFilledBuffer(size, spec, sign); |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 276 | n = abs_value; |
| 277 | do { |
| 278 | *p-- = '0' + (n & 7); |
| 279 | } while ((n >>= 3) != 0); |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 280 | if (print_prefix) |
| 281 | *p = '0'; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 282 | break; |
| 283 | } |
| 284 | default: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 285 | ReportUnknownType(spec.type, "integer"); |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 286 | break; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 287 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | template <typename T> |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 291 | void Formatter::FormatDouble(T value, const FormatSpec &spec, int precision) { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 292 | // Check type. |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 293 | char type = spec.type; |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 294 | bool upper = false; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 295 | switch (type) { |
| Victor Zverovich | d73306b | 2012-12-10 12:16:02 -0800 | [diff] [blame] | 296 | case 0: |
| 297 | type = 'g'; |
| 298 | break; |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 299 | case 'e': case 'f': case 'g': |
| Victor Zverovich | a2a8741 | 2012-12-12 10:11:40 -0800 | [diff] [blame] | 300 | break; |
| 301 | case 'F': |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 302 | #ifdef _MSC_VER |
| 303 | // MSVC's printf doesn't support 'F'. |
| Victor Zverovich | a2a8741 | 2012-12-12 10:11:40 -0800 | [diff] [blame] | 304 | type = 'f'; |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 305 | #endif |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 306 | // Fall through. |
| 307 | case 'E': case 'G': |
| 308 | upper = true; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 309 | break; |
| 310 | default: |
| Victor Zverovich | d599e3b | 2012-12-10 13:30:06 -0800 | [diff] [blame] | 311 | ReportUnknownType(type, "double"); |
| 312 | break; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 315 | char sign = 0; |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 316 | // Use signbit instead of value < 0 because the latter is always |
| 317 | // false for NaN. |
| 318 | if (signbit(value)) { |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 319 | sign = '-'; |
| 320 | value = -value; |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 321 | } else if ((spec.flags & SIGN_FLAG) != 0) { |
| 322 | sign = (spec.flags & PLUS_FLAG) != 0 ? '+' : ' '; |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 323 | } |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 324 | |
| Victor Zverovich | 529045b | 2012-12-28 09:41:03 -0800 | [diff] [blame] | 325 | if (value != value) { |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 326 | // Format NaN ourselves because sprintf's output is not consistent |
| 327 | // across platforms. |
| 328 | std::size_t size = 4; |
| 329 | const char *nan = upper ? " NAN" : " nan"; |
| 330 | if (!sign) { |
| 331 | --size; |
| 332 | ++nan; |
| 333 | } |
| 334 | char *out = FormatString(nan, size, spec); |
| 335 | if (sign) |
| 336 | *out = sign; |
| 337 | return; |
| 338 | } |
| 339 | |
| Victor Zverovich | 4762a8a | 2012-12-29 06:44:14 -0800 | [diff] [blame^] | 340 | if (isinf(value)) { |
| 341 | // Format infinity ourselves because sprintf's output is not consistent |
| 342 | // across platforms. |
| 343 | std::size_t size = 4; |
| 344 | const char *inf = upper ? " INF" : " inf"; |
| 345 | if (!sign) { |
| 346 | --size; |
| 347 | ++inf; |
| 348 | } |
| 349 | char *out = FormatString(inf, size, spec); |
| 350 | if (sign) |
| 351 | *out = sign; |
| 352 | return; |
| 353 | } |
| 354 | |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 355 | size_t offset = buffer_.size(); |
| 356 | unsigned width = spec.width; |
| 357 | if (sign) { |
| 358 | buffer_.reserve(buffer_.size() + std::max(width, 1u)); |
| 359 | if (width > 0) |
| 360 | --width; |
| 361 | ++offset; |
| 362 | } |
| 363 | |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 364 | // Build format string. |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 365 | enum { MAX_FORMAT_SIZE = 10}; // longest format: %#-*.*Lg |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 366 | char format[MAX_FORMAT_SIZE]; |
| 367 | char *format_ptr = format; |
| 368 | *format_ptr++ = '%'; |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 369 | unsigned width_for_sprintf = width; |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 370 | if ((spec.flags & HASH_FLAG) != 0) |
| 371 | *format_ptr++ = '#'; |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 372 | if (spec.align == ALIGN_CENTER) { |
| 373 | width_for_sprintf = 0; |
| 374 | } else { |
| 375 | if (spec.align == ALIGN_LEFT) |
| 376 | *format_ptr++ = '-'; |
| 377 | if (width != 0) |
| 378 | *format_ptr++ = '*'; |
| 379 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 380 | if (precision >= 0) { |
| 381 | *format_ptr++ = '.'; |
| 382 | *format_ptr++ = '*'; |
| 383 | } |
| 384 | if (IsLongDouble<T>::VALUE) |
| 385 | *format_ptr++ = 'L'; |
| Victor Zverovich | d73306b | 2012-12-10 12:16:02 -0800 | [diff] [blame] | 386 | *format_ptr++ = type; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 387 | *format_ptr = '\0'; |
| 388 | |
| 389 | // Format using snprintf. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 390 | for (;;) { |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 391 | size_t size = buffer_.capacity() - offset; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 392 | int n = 0; |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 393 | char *start = &buffer_[offset]; |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 394 | if (width_for_sprintf == 0) { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 395 | n = precision < 0 ? |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 396 | snprintf(start, size, format, value) : |
| 397 | snprintf(start, size, format, precision, value); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 398 | } else { |
| 399 | n = precision < 0 ? |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 400 | snprintf(start, size, format, width_for_sprintf, value) : |
| 401 | snprintf(start, size, format, width_for_sprintf, precision, value); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 402 | } |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 403 | if (n >= 0 && offset + n < buffer_.capacity()) { |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 404 | if (sign) { |
| 405 | if ((spec.align != ALIGN_RIGHT && spec.align != ALIGN_DEFAULT) || |
| 406 | *start != ' ') { |
| 407 | *(start - 1) = sign; |
| 408 | sign = 0; |
| 409 | } else { |
| 410 | *(start - 1) = spec.fill; |
| 411 | } |
| 412 | ++n; |
| 413 | } |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 414 | if (spec.align == ALIGN_CENTER && spec.width > static_cast<unsigned>(n)) { |
| 415 | char *p = GrowBuffer(spec.width); |
| 416 | std::copy(p, p + n, p + (spec.width - n) / 2); |
| 417 | FillPadding(p, spec.width, n, spec.fill); |
| 418 | return; |
| 419 | } |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 420 | if (spec.fill != ' ' || sign) { |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 421 | while (*start == ' ') |
| 422 | *start++ = spec.fill; |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 423 | if (sign) |
| 424 | *(start - 1) = sign; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 425 | } |
| Victor Zverovich | 14e0f87 | 2012-12-10 18:08:04 -0800 | [diff] [blame] | 426 | GrowBuffer(n); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 427 | return; |
| 428 | } |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 429 | buffer_.reserve(n >= 0 ? offset + n + 1 : 2 * buffer_.capacity()); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 433 | char *Formatter::FormatString( |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 434 | const char *s, std::size_t size, const FormatSpec &spec) { |
| 435 | char *out = 0; |
| 436 | if (spec.width > size) { |
| 437 | out = GrowBuffer(spec.width); |
| Victor Zverovich | 100149e | 2012-12-24 11:41:20 -0800 | [diff] [blame] | 438 | if (spec.align == ALIGN_RIGHT) { |
| 439 | std::fill_n(out, spec.width - size, spec.fill); |
| 440 | out += spec.width - size; |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 441 | } else if (spec.align == ALIGN_CENTER) { |
| 442 | out = FillPadding(out, spec.width, size, spec.fill); |
| Victor Zverovich | 100149e | 2012-12-24 11:41:20 -0800 | [diff] [blame] | 443 | } else { |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 444 | std::fill_n(out + size, spec.width - size, spec.fill); |
| Victor Zverovich | 100149e | 2012-12-24 11:41:20 -0800 | [diff] [blame] | 445 | } |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 446 | } else { |
| 447 | out = GrowBuffer(size); |
| 448 | } |
| 449 | std::copy(s, s + size, out); |
| Victor Zverovich | ce60483 | 2012-12-28 08:27:54 -0800 | [diff] [blame] | 450 | return out; |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 453 | // Parses an unsigned integer advancing s to the end of the parsed input. |
| 454 | // This function assumes that the first character of s is a digit. |
| 455 | unsigned Formatter::ParseUInt(const char *&s) const { |
| 456 | assert('0' <= *s && *s <= '9'); |
| 457 | unsigned value = 0; |
| 458 | do { |
| 459 | unsigned new_value = value * 10 + (*s++ - '0'); |
| 460 | if (new_value < value) // Check if value wrapped around. |
| 461 | ReportError(s, "number is too big in format"); |
| 462 | value = new_value; |
| 463 | } while ('0' <= *s && *s <= '9'); |
| 464 | return value; |
| 465 | } |
| 466 | |
| Victor Zverovich | 8412ad6 | 2012-12-27 06:56:55 -0800 | [diff] [blame] | 467 | const Formatter::Arg &Formatter::ParseArgIndex(const char *&s) { |
| 468 | unsigned arg_index = 0; |
| 469 | if (*s < '0' || *s > '9') { |
| 470 | if (*s != '}' && *s != ':') |
| 471 | ReportError(s, "invalid argument index in format string"); |
| 472 | if (next_arg_index_ < 0) { |
| 473 | ReportError(s, |
| 474 | "cannot switch from manual to automatic argument indexing"); |
| 475 | } |
| 476 | arg_index = next_arg_index_++; |
| 477 | } else { |
| 478 | if (next_arg_index_ > 0) { |
| 479 | ReportError(s, |
| 480 | "cannot switch from automatic to manual argument indexing"); |
| 481 | } |
| 482 | next_arg_index_ = -1; |
| 483 | arg_index = ParseUInt(s); |
| 484 | if (arg_index >= args_.size()) |
| 485 | ReportError(s, "argument index is out of range in format"); |
| 486 | } |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 487 | return *args_[arg_index]; |
| 488 | } |
| 489 | |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 490 | void Formatter::CheckSign(const char *&s, const Arg &arg) { |
| 491 | if (arg.type > LAST_NUMERIC_TYPE) { |
| 492 | ReportError(s, |
| 493 | Format("format specifier '{0}' requires numeric argument") << *s); |
| 494 | } |
| 495 | if (arg.type == UINT || arg.type == ULONG) { |
| 496 | ReportError(s, |
| 497 | Format("format specifier '{0}' requires signed argument") << *s); |
| 498 | } |
| 499 | ++s; |
| 500 | } |
| 501 | |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 502 | void Formatter::DoFormat() { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 503 | const char *start = format_; |
| Victor Zverovich | 57dbd2c | 2012-12-11 12:23:52 -0800 | [diff] [blame] | 504 | format_ = 0; |
| Victor Zverovich | 8412ad6 | 2012-12-27 06:56:55 -0800 | [diff] [blame] | 505 | next_arg_index_ = 0; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 506 | const char *s = start; |
| Victor Zverovich | 5f3ed20 | 2012-12-07 17:48:10 -0800 | [diff] [blame] | 507 | while (*s) { |
| Victor Zverovich | 31a5070 | 2012-12-10 15:04:55 -0800 | [diff] [blame] | 508 | char c = *s++; |
| 509 | if (c != '{' && c != '}') continue; |
| 510 | if (*s == c) { |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 511 | buffer_.append(start, s); |
| Victor Zverovich | 31a5070 | 2012-12-10 15:04:55 -0800 | [diff] [blame] | 512 | start = ++s; |
| 513 | continue; |
| 514 | } |
| 515 | if (c == '}') |
| 516 | throw FormatError("unmatched '}' in format"); |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 517 | num_open_braces_= 1; |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 518 | buffer_.append(start, s - 1); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 519 | |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 520 | const Arg &arg = ParseArgIndex(s); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 521 | |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 522 | FormatSpec spec; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 523 | int precision = -1; |
| 524 | if (*s == ':') { |
| 525 | ++s; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 526 | |
| 527 | // Parse fill and alignment. |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 528 | if (char c = *s) { |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 529 | const char *p = s + 1; |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 530 | spec.align = ALIGN_DEFAULT; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 531 | do { |
| 532 | switch (*p) { |
| 533 | case '<': |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 534 | spec.align = ALIGN_LEFT; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 535 | break; |
| 536 | case '>': |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 537 | spec.align = ALIGN_RIGHT; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 538 | break; |
| 539 | case '=': |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 540 | spec.align = ALIGN_NUMERIC; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 541 | break; |
| 542 | case '^': |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 543 | spec.align = ALIGN_CENTER; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 544 | break; |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 545 | } |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 546 | if (spec.align != ALIGN_DEFAULT) { |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 547 | if (p != s) { |
| 548 | if (c == '}') break; |
| 549 | if (c == '{') |
| 550 | ReportError(s, "invalid fill character '{'"); |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 551 | s += 2; |
| 552 | spec.fill = c; |
| 553 | } else ++s; |
| Victor Zverovich | ccbe941 | 2012-12-24 08:34:44 -0800 | [diff] [blame] | 554 | if (spec.align == ALIGN_NUMERIC && arg.type > LAST_NUMERIC_TYPE) |
| 555 | ReportError(s, "format specifier '=' requires numeric argument"); |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 556 | break; |
| 557 | } |
| 558 | } while (--p >= s); |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 559 | } |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 560 | |
| 561 | // Parse sign. |
| Victor Zverovich | 0a138ad | 2012-12-25 13:25:14 -0800 | [diff] [blame] | 562 | switch (*s) { |
| 563 | case '+': |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 564 | CheckSign(s, arg); |
| 565 | spec.flags |= SIGN_FLAG | PLUS_FLAG; |
| 566 | break; |
| Victor Zverovich | 0a138ad | 2012-12-25 13:25:14 -0800 | [diff] [blame] | 567 | case '-': |
| Victor Zverovich | 17ca809 | 2012-12-25 13:45:12 -0800 | [diff] [blame] | 568 | CheckSign(s, arg); |
| 569 | break; |
| 570 | case ' ': |
| 571 | CheckSign(s, arg); |
| 572 | spec.flags |= SIGN_FLAG; |
| Victor Zverovich | 0a138ad | 2012-12-25 13:25:14 -0800 | [diff] [blame] | 573 | break; |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 574 | } |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 575 | |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 576 | if (*s == '#') { |
| 577 | if (arg.type > LAST_NUMERIC_TYPE) |
| 578 | ReportError(s, "format specifier '#' requires numeric argument"); |
| 579 | spec.flags |= HASH_FLAG; |
| 580 | ++s; |
| 581 | } |
| 582 | |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 583 | // Parse width and zero flag. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 584 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 585 | if (*s == '0') { |
| 586 | if (arg.type > LAST_NUMERIC_TYPE) |
| 587 | ReportError(s, "format specifier '0' requires numeric argument"); |
| Victor Zverovich | be6e54d | 2012-12-22 14:05:56 -0800 | [diff] [blame] | 588 | spec.align = ALIGN_NUMERIC; |
| Victor Zverovich | 6423689 | 2012-12-21 09:12:04 -0800 | [diff] [blame] | 589 | spec.fill = '0'; |
| 590 | } |
| 591 | // Zero may be parsed again as a part of the width, but it is simpler |
| 592 | // and more efficient than checking if the next char is a digit. |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 593 | unsigned value = ParseUInt(s); |
| 594 | if (value > INT_MAX) |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 595 | ReportError(s, "number is too big in format"); |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 596 | spec.width = value; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // Parse precision. |
| 600 | if (*s == '.') { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 601 | ++s; |
| 602 | precision = 0; |
| 603 | if ('0' <= *s && *s <= '9') { |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 604 | unsigned value = ParseUInt(s); |
| 605 | if (value > INT_MAX) |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 606 | ReportError(s, "number is too big in format"); |
| Victor Zverovich | 095b43a | 2012-12-09 11:32:39 -0800 | [diff] [blame] | 607 | precision = value; |
| Victor Zverovich | 3633527 | 2012-12-12 15:21:11 -0800 | [diff] [blame] | 608 | } else if (*s == '{') { |
| 609 | ++s; |
| 610 | ++num_open_braces_; |
| 611 | const Arg &precision_arg = ParseArgIndex(s); |
| 612 | unsigned long value = 0; |
| 613 | switch (precision_arg.type) { |
| 614 | case INT: |
| 615 | if (precision_arg.int_value < 0) |
| 616 | ReportError(s, "negative precision in format"); |
| 617 | value = precision_arg.int_value; |
| 618 | break; |
| 619 | case UINT: |
| 620 | value = precision_arg.uint_value; |
| 621 | break; |
| 622 | case LONG: |
| 623 | if (precision_arg.long_value < 0) |
| 624 | ReportError(s, "negative precision in format"); |
| 625 | value = precision_arg.long_value; |
| 626 | break; |
| 627 | case ULONG: |
| 628 | value = precision_arg.ulong_value; |
| 629 | break; |
| 630 | default: |
| 631 | ReportError(s, "precision is not integer"); |
| 632 | } |
| 633 | if (value > INT_MAX) |
| 634 | ReportError(s, "number is too big in format"); |
| 635 | precision = value; |
| 636 | if (*s++ != '}') |
| 637 | throw FormatError("unmatched '{' in format"); |
| 638 | --num_open_braces_; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 639 | } else { |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 640 | ReportError(s, "missing precision in format"); |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 641 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 642 | if (arg.type != DOUBLE && arg.type != LONG_DOUBLE) { |
| 643 | ReportError(s, |
| 644 | "precision specifier requires floating-point argument"); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | // Parse type. |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 649 | if (*s != '}' && *s) |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 650 | spec.type = *s++; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | if (*s++ != '}') |
| Victor Zverovich | 8b1c709 | 2012-12-08 18:45:35 -0800 | [diff] [blame] | 654 | throw FormatError("unmatched '{' in format"); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 655 | start = s; |
| 656 | |
| 657 | // Format argument. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 658 | switch (arg.type) { |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 659 | case INT: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 660 | FormatInt(arg.int_value, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 661 | break; |
| 662 | case UINT: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 663 | FormatInt(arg.uint_value, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 664 | break; |
| 665 | case LONG: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 666 | FormatInt(arg.long_value, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 667 | break; |
| 668 | case ULONG: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 669 | FormatInt(arg.ulong_value, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 670 | break; |
| 671 | case DOUBLE: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 672 | FormatDouble(arg.double_value, spec, precision); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 673 | break; |
| 674 | case LONG_DOUBLE: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 675 | FormatDouble(arg.long_double_value, spec, precision); |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 676 | break; |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 677 | case CHAR: { |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 678 | if (spec.type && spec.type != 'c') |
| 679 | ReportUnknownType(spec.type, "char"); |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 680 | char *out = 0; |
| 681 | if (spec.width > 1) { |
| 682 | out = GrowBuffer(spec.width); |
| Victor Zverovich | 26d2ae6 | 2012-12-24 11:58:49 -0800 | [diff] [blame] | 683 | if (spec.align == ALIGN_RIGHT) { |
| 684 | std::fill_n(out, spec.width - 1, spec.fill); |
| 685 | out += spec.width - 1; |
| Victor Zverovich | 84310c3 | 2012-12-24 19:37:50 -0800 | [diff] [blame] | 686 | } else if (spec.align == ALIGN_CENTER) { |
| 687 | out = FillPadding(out, spec.width, 1, spec.fill); |
| Victor Zverovich | 26d2ae6 | 2012-12-24 11:58:49 -0800 | [diff] [blame] | 688 | } else { |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 689 | std::fill_n(out + 1, spec.width - 1, spec.fill); |
| Victor Zverovich | 26d2ae6 | 2012-12-24 11:58:49 -0800 | [diff] [blame] | 690 | } |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 691 | } else { |
| 692 | out = GrowBuffer(1); |
| 693 | } |
| 694 | *out = arg.int_value; |
| Victor Zverovich | bbd13a4 | 2012-12-09 14:13:23 -0800 | [diff] [blame] | 695 | break; |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 696 | } |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 697 | case STRING: { |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 698 | if (spec.type && spec.type != 's') |
| 699 | ReportUnknownType(spec.type, "string"); |
| Victor Zverovich | 33bb6ee | 2012-12-11 21:47:05 -0800 | [diff] [blame] | 700 | const char *str = arg.string.value; |
| 701 | size_t size = arg.string.size; |
| Victor Zverovich | 1b3c197 | 2012-12-17 16:39:49 -0800 | [diff] [blame] | 702 | if (size == 0) { |
| 703 | if (!str) |
| 704 | throw FormatError("string pointer is null"); |
| 705 | if (*str) |
| 706 | size = std::strlen(str); |
| 707 | } |
| Victor Zverovich | 1b9c22c | 2012-12-22 17:53:13 -0800 | [diff] [blame] | 708 | FormatString(str, size, spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 709 | break; |
| Victor Zverovich | b98b2e9 | 2012-12-10 11:08:16 -0800 | [diff] [blame] | 710 | } |
| 711 | case POINTER: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 712 | if (spec.type && spec.type != 'p') |
| 713 | ReportUnknownType(spec.type, "pointer"); |
| Victor Zverovich | 73f13ee | 2012-12-25 18:19:51 -0800 | [diff] [blame] | 714 | spec.flags = HASH_FLAG; |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 715 | spec.type = 'x'; |
| 716 | FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec); |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 717 | break; |
| Victor Zverovich | 280ea8b | 2012-12-09 09:03:47 -0800 | [diff] [blame] | 718 | case CUSTOM: |
| Victor Zverovich | a0d685c | 2012-12-20 20:10:55 -0800 | [diff] [blame] | 719 | if (spec.type) |
| 720 | ReportUnknownType(spec.type, "object"); |
| 721 | (this->*arg.custom.format)(arg.custom.value, spec); |
| Victor Zverovich | 63539c0 | 2012-12-08 08:17:12 -0800 | [diff] [blame] | 722 | break; |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 723 | default: |
| 724 | assert(false); |
| 725 | break; |
| 726 | } |
| 727 | } |
| Victor Zverovich | 198ebe9 | 2012-12-10 17:16:08 -0800 | [diff] [blame] | 728 | buffer_.append(start, s + 1); |
| Victor Zverovich | 0996e98 | 2012-12-10 20:37:35 -0800 | [diff] [blame] | 729 | buffer_.resize(buffer_.size() - 1); // Don't count the terminating zero. |
| Victor Zverovich | b076df4 | 2012-12-07 08:31:09 -0800 | [diff] [blame] | 730 | } |