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