Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1 | /* implements the unicode (as opposed to string) version of the |
| 2 | built-in formatters for string, int, float. that is, the versions |
| 3 | of int.__float__, etc., that take and return unicode objects */ |
| 4 | |
| 5 | #include "Python.h" |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6 | #include <locale.h> |
| 7 | |
| 8 | /* Raises an exception about an unknown presentation type for this |
| 9 | * type. */ |
| 10 | |
| 11 | static void |
| 12 | unknown_presentation_type(Py_UCS4 presentation_type, |
| 13 | const char* type_name) |
| 14 | { |
| 15 | /* %c might be out-of-range, hence the two cases. */ |
| 16 | if (presentation_type > 32 && presentation_type < 128) |
| 17 | PyErr_Format(PyExc_ValueError, |
| 18 | "Unknown format code '%c' " |
| 19 | "for object of type '%.200s'", |
| 20 | (char)presentation_type, |
| 21 | type_name); |
| 22 | else |
| 23 | PyErr_Format(PyExc_ValueError, |
| 24 | "Unknown format code '\\x%x' " |
| 25 | "for object of type '%.200s'", |
| 26 | (unsigned int)presentation_type, |
| 27 | type_name); |
| 28 | } |
| 29 | |
| 30 | static void |
| 31 | invalid_comma_type(Py_UCS4 presentation_type) |
| 32 | { |
| 33 | if (presentation_type > 32 && presentation_type < 128) |
| 34 | PyErr_Format(PyExc_ValueError, |
| 35 | "Cannot specify ',' with '%c'.", |
| 36 | (char)presentation_type); |
| 37 | else |
| 38 | PyErr_Format(PyExc_ValueError, |
| 39 | "Cannot specify ',' with '\\x%x'.", |
| 40 | (unsigned int)presentation_type); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | get_integer consumes 0 or more decimal digit characters from an |
| 45 | input string, updates *result with the corresponding positive |
| 46 | integer, and returns the number of digits consumed. |
| 47 | |
| 48 | returns -1 on error. |
| 49 | */ |
| 50 | static int |
| 51 | get_integer(PyObject *str, Py_ssize_t *pos, Py_ssize_t end, |
| 52 | Py_ssize_t *result) |
| 53 | { |
Mark Dickinson | 47862d4 | 2011-12-01 15:27:04 +0000 | [diff] [blame] | 54 | Py_ssize_t accumulator, digitval; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 55 | int numdigits; |
| 56 | accumulator = numdigits = 0; |
| 57 | for (;;(*pos)++, numdigits++) { |
| 58 | if (*pos >= end) |
| 59 | break; |
| 60 | digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ_CHAR(str, *pos)); |
| 61 | if (digitval < 0) |
| 62 | break; |
| 63 | /* |
Mark Dickinson | 47862d4 | 2011-12-01 15:27:04 +0000 | [diff] [blame] | 64 | Detect possible overflow before it happens: |
| 65 | |
| 66 | accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if |
| 67 | accumulator > (PY_SSIZE_T_MAX - digitval) / 10. |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 68 | */ |
Mark Dickinson | 47862d4 | 2011-12-01 15:27:04 +0000 | [diff] [blame] | 69 | if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 70 | PyErr_Format(PyExc_ValueError, |
| 71 | "Too many decimal digits in format string"); |
| 72 | return -1; |
| 73 | } |
Mark Dickinson | 47862d4 | 2011-12-01 15:27:04 +0000 | [diff] [blame] | 74 | accumulator = accumulator * 10 + digitval; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 75 | } |
| 76 | *result = accumulator; |
| 77 | return numdigits; |
| 78 | } |
| 79 | |
| 80 | /************************************************************************/ |
| 81 | /*********** standard format specifier parsing **************************/ |
| 82 | /************************************************************************/ |
| 83 | |
| 84 | /* returns true if this character is a specifier alignment token */ |
| 85 | Py_LOCAL_INLINE(int) |
| 86 | is_alignment_token(Py_UCS4 c) |
| 87 | { |
| 88 | switch (c) { |
| 89 | case '<': case '>': case '=': case '^': |
| 90 | return 1; |
| 91 | default: |
| 92 | return 0; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* returns true if this character is a sign element */ |
| 97 | Py_LOCAL_INLINE(int) |
| 98 | is_sign_element(Py_UCS4 c) |
| 99 | { |
| 100 | switch (c) { |
| 101 | case ' ': case '+': case '-': |
| 102 | return 1; |
| 103 | default: |
| 104 | return 0; |
| 105 | } |
| 106 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 107 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 108 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 109 | typedef struct { |
| 110 | Py_UCS4 fill_char; |
| 111 | Py_UCS4 align; |
| 112 | int alternate; |
| 113 | Py_UCS4 sign; |
| 114 | Py_ssize_t width; |
| 115 | int thousands_separators; |
| 116 | Py_ssize_t precision; |
| 117 | Py_UCS4 type; |
| 118 | } InternalFormatSpec; |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 119 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 120 | #if 0 |
| 121 | /* Occassionally useful for debugging. Should normally be commented out. */ |
| 122 | static void |
| 123 | DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format) |
| 124 | { |
| 125 | printf("internal format spec: fill_char %d\n", format->fill_char); |
| 126 | printf("internal format spec: align %d\n", format->align); |
| 127 | printf("internal format spec: alternate %d\n", format->alternate); |
| 128 | printf("internal format spec: sign %d\n", format->sign); |
| 129 | printf("internal format spec: width %zd\n", format->width); |
| 130 | printf("internal format spec: thousands_separators %d\n", |
| 131 | format->thousands_separators); |
| 132 | printf("internal format spec: precision %zd\n", format->precision); |
| 133 | printf("internal format spec: type %c\n", format->type); |
| 134 | printf("\n"); |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | ptr points to the start of the format_spec, end points just past its end. |
| 141 | fills in format with the parsed information. |
| 142 | returns 1 on success, 0 on failure. |
| 143 | if failure, sets the exception |
| 144 | */ |
| 145 | static int |
| 146 | parse_internal_render_format_spec(PyObject *format_spec, |
| 147 | Py_ssize_t start, Py_ssize_t end, |
| 148 | InternalFormatSpec *format, |
| 149 | char default_type, |
| 150 | char default_align) |
| 151 | { |
| 152 | Py_ssize_t pos = start; |
| 153 | /* end-pos is used throughout this code to specify the length of |
| 154 | the input string */ |
| 155 | #define READ_spec(index) PyUnicode_READ_CHAR(format_spec, index) |
| 156 | |
| 157 | Py_ssize_t consumed; |
| 158 | int align_specified = 0; |
Eric V. Smith | 2ea9712 | 2014-04-14 11:55:10 -0400 | [diff] [blame] | 159 | int fill_char_specified = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 160 | |
Eric V. Smith | 2ea9712 | 2014-04-14 11:55:10 -0400 | [diff] [blame] | 161 | format->fill_char = ' '; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 162 | format->align = default_align; |
| 163 | format->alternate = 0; |
| 164 | format->sign = '\0'; |
| 165 | format->width = -1; |
| 166 | format->thousands_separators = 0; |
| 167 | format->precision = -1; |
| 168 | format->type = default_type; |
| 169 | |
| 170 | /* If the second char is an alignment token, |
| 171 | then parse the fill char */ |
| 172 | if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) { |
| 173 | format->align = READ_spec(pos+1); |
| 174 | format->fill_char = READ_spec(pos); |
Eric V. Smith | 2ea9712 | 2014-04-14 11:55:10 -0400 | [diff] [blame] | 175 | fill_char_specified = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 176 | align_specified = 1; |
| 177 | pos += 2; |
| 178 | } |
| 179 | else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) { |
| 180 | format->align = READ_spec(pos); |
| 181 | align_specified = 1; |
| 182 | ++pos; |
| 183 | } |
| 184 | |
| 185 | /* Parse the various sign options */ |
| 186 | if (end-pos >= 1 && is_sign_element(READ_spec(pos))) { |
| 187 | format->sign = READ_spec(pos); |
| 188 | ++pos; |
| 189 | } |
| 190 | |
| 191 | /* If the next character is #, we're in alternate mode. This only |
| 192 | applies to integers. */ |
| 193 | if (end-pos >= 1 && READ_spec(pos) == '#') { |
| 194 | format->alternate = 1; |
| 195 | ++pos; |
| 196 | } |
| 197 | |
| 198 | /* The special case for 0-padding (backwards compat) */ |
Eric V. Smith | 2ea9712 | 2014-04-14 11:55:10 -0400 | [diff] [blame] | 199 | if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 200 | format->fill_char = '0'; |
| 201 | if (!align_specified) { |
| 202 | format->align = '='; |
| 203 | } |
| 204 | ++pos; |
| 205 | } |
| 206 | |
| 207 | consumed = get_integer(format_spec, &pos, end, &format->width); |
| 208 | if (consumed == -1) |
| 209 | /* Overflow error. Exception already set. */ |
| 210 | return 0; |
| 211 | |
| 212 | /* If consumed is 0, we didn't consume any characters for the |
| 213 | width. In that case, reset the width to -1, because |
| 214 | get_integer() will have set it to zero. -1 is how we record |
| 215 | that the width wasn't specified. */ |
| 216 | if (consumed == 0) |
| 217 | format->width = -1; |
| 218 | |
| 219 | /* Comma signifies add thousands separators */ |
| 220 | if (end-pos && READ_spec(pos) == ',') { |
| 221 | format->thousands_separators = 1; |
| 222 | ++pos; |
| 223 | } |
| 224 | |
| 225 | /* Parse field precision */ |
| 226 | if (end-pos && READ_spec(pos) == '.') { |
| 227 | ++pos; |
| 228 | |
| 229 | consumed = get_integer(format_spec, &pos, end, &format->precision); |
| 230 | if (consumed == -1) |
| 231 | /* Overflow error. Exception already set. */ |
| 232 | return 0; |
| 233 | |
| 234 | /* Not having a precision after a dot is an error. */ |
| 235 | if (consumed == 0) { |
| 236 | PyErr_Format(PyExc_ValueError, |
| 237 | "Format specifier missing precision"); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | |
| 243 | /* Finally, parse the type field. */ |
| 244 | |
| 245 | if (end-pos > 1) { |
Eric V. Smith | d25cfe6 | 2012-01-19 20:04:28 -0500 | [diff] [blame] | 246 | /* More than one char remain, invalid format specifier. */ |
| 247 | PyErr_Format(PyExc_ValueError, "Invalid format specifier"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | if (end-pos == 1) { |
| 252 | format->type = READ_spec(pos); |
| 253 | ++pos; |
| 254 | } |
| 255 | |
| 256 | /* Do as much validating as we can, just by looking at the format |
| 257 | specifier. Do not take into account what type of formatting |
| 258 | we're doing (int, float, string). */ |
| 259 | |
| 260 | if (format->thousands_separators) { |
| 261 | switch (format->type) { |
| 262 | case 'd': |
| 263 | case 'e': |
| 264 | case 'f': |
| 265 | case 'g': |
| 266 | case 'E': |
| 267 | case 'G': |
| 268 | case '%': |
| 269 | case 'F': |
| 270 | case '\0': |
| 271 | /* These are allowed. See PEP 378.*/ |
| 272 | break; |
| 273 | default: |
| 274 | invalid_comma_type(format->type); |
| 275 | return 0; |
| 276 | } |
| 277 | } |
| 278 | |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 279 | assert (format->align <= 127); |
| 280 | assert (format->sign <= 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | /* Calculate the padding needed. */ |
| 285 | static void |
| 286 | calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align, |
| 287 | Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding, |
| 288 | Py_ssize_t *n_total) |
| 289 | { |
| 290 | if (width >= 0) { |
| 291 | if (nchars > width) |
| 292 | *n_total = nchars; |
| 293 | else |
| 294 | *n_total = width; |
| 295 | } |
| 296 | else { |
| 297 | /* not specified, use all of the chars and no more */ |
| 298 | *n_total = nchars; |
| 299 | } |
| 300 | |
| 301 | /* Figure out how much leading space we need, based on the |
| 302 | aligning */ |
| 303 | if (align == '>') |
| 304 | *n_lpadding = *n_total - nchars; |
| 305 | else if (align == '^') |
| 306 | *n_lpadding = (*n_total - nchars) / 2; |
| 307 | else if (align == '<' || align == '=') |
| 308 | *n_lpadding = 0; |
| 309 | else { |
| 310 | /* We should never have an unspecified alignment. */ |
| 311 | *n_lpadding = 0; |
| 312 | assert(0); |
| 313 | } |
| 314 | |
| 315 | *n_rpadding = *n_total - nchars - *n_lpadding; |
| 316 | } |
| 317 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 318 | /* Do the padding, and return a pointer to where the caller-supplied |
| 319 | content goes. */ |
Victor Stinner | 9ce59bb | 2013-05-17 00:04:56 +0200 | [diff] [blame] | 320 | static int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 321 | fill_padding(_PyUnicodeWriter *writer, |
| 322 | Py_ssize_t nchars, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 323 | Py_UCS4 fill_char, Py_ssize_t n_lpadding, |
| 324 | Py_ssize_t n_rpadding) |
| 325 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 326 | Py_ssize_t pos; |
| 327 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 328 | /* Pad on left. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 329 | if (n_lpadding) { |
| 330 | pos = writer->pos; |
| 331 | _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char); |
| 332 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 333 | |
| 334 | /* Pad on right. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 335 | if (n_rpadding) { |
| 336 | pos = writer->pos + nchars + n_lpadding; |
| 337 | _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char); |
| 338 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 339 | |
| 340 | /* Pointer to the user content. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 341 | writer->pos += n_lpadding; |
| 342 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /************************************************************************/ |
| 346 | /*********** common routines for numeric formatting *********************/ |
| 347 | /************************************************************************/ |
| 348 | |
| 349 | /* Locale type codes. */ |
| 350 | #define LT_CURRENT_LOCALE 0 |
| 351 | #define LT_DEFAULT_LOCALE 1 |
| 352 | #define LT_NO_LOCALE 2 |
| 353 | |
| 354 | /* Locale info needed for formatting integers and the part of floats |
| 355 | before and including the decimal. Note that locales only support |
| 356 | 8-bit chars, not unicode. */ |
| 357 | typedef struct { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 358 | PyObject *decimal_point; |
| 359 | PyObject *thousands_sep; |
| 360 | const char *grouping; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 361 | } LocaleInfo; |
| 362 | |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 363 | #define STATIC_LOCALE_INFO_INIT {0, 0, 0} |
| 364 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 365 | /* describes the layout for an integer, see the comment in |
| 366 | calc_number_widths() for details */ |
| 367 | typedef struct { |
| 368 | Py_ssize_t n_lpadding; |
| 369 | Py_ssize_t n_prefix; |
| 370 | Py_ssize_t n_spadding; |
| 371 | Py_ssize_t n_rpadding; |
| 372 | char sign; |
| 373 | Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */ |
| 374 | Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including |
| 375 | any grouping chars. */ |
| 376 | Py_ssize_t n_decimal; /* 0 if only an integer */ |
| 377 | Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part, |
| 378 | excluding the decimal itself, if |
| 379 | present. */ |
| 380 | |
| 381 | /* These 2 are not the widths of fields, but are needed by |
| 382 | STRINGLIB_GROUPING. */ |
| 383 | Py_ssize_t n_digits; /* The number of digits before a decimal |
| 384 | or exponent. */ |
| 385 | Py_ssize_t n_min_width; /* The min_width we used when we computed |
| 386 | the n_grouped_digits width. */ |
| 387 | } NumberFieldWidths; |
| 388 | |
| 389 | |
| 390 | /* Given a number of the form: |
| 391 | digits[remainder] |
| 392 | where ptr points to the start and end points to the end, find where |
| 393 | the integer part ends. This could be a decimal, an exponent, both, |
| 394 | or neither. |
| 395 | If a decimal point is present, set *has_decimal and increment |
| 396 | remainder beyond it. |
| 397 | Results are undefined (but shouldn't crash) for improperly |
| 398 | formatted strings. |
| 399 | */ |
| 400 | static void |
| 401 | parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end, |
| 402 | Py_ssize_t *n_remainder, int *has_decimal) |
| 403 | { |
| 404 | Py_ssize_t remainder; |
| 405 | |
Antoine Pitrou | c73c561 | 2013-02-09 23:14:42 +0100 | [diff] [blame] | 406 | while (pos<end && Py_ISDIGIT(PyUnicode_READ_CHAR(s, pos))) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 407 | ++pos; |
| 408 | remainder = pos; |
| 409 | |
| 410 | /* Does remainder start with a decimal point? */ |
| 411 | *has_decimal = pos<end && PyUnicode_READ_CHAR(s, remainder) == '.'; |
| 412 | |
| 413 | /* Skip the decimal point. */ |
| 414 | if (*has_decimal) |
| 415 | remainder++; |
| 416 | |
| 417 | *n_remainder = end - remainder; |
| 418 | } |
| 419 | |
| 420 | /* not all fields of format are used. for example, precision is |
| 421 | unused. should this take discrete params in order to be more clear |
| 422 | about what it does? or is passing a single format parameter easier |
| 423 | and more efficient enough to justify a little obfuscation? */ |
| 424 | static Py_ssize_t |
| 425 | calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix, |
| 426 | Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start, |
| 427 | Py_ssize_t n_end, Py_ssize_t n_remainder, |
| 428 | int has_decimal, const LocaleInfo *locale, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 429 | const InternalFormatSpec *format, Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 430 | { |
| 431 | Py_ssize_t n_non_digit_non_padding; |
| 432 | Py_ssize_t n_padding; |
| 433 | |
| 434 | spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0); |
| 435 | spec->n_lpadding = 0; |
| 436 | spec->n_prefix = n_prefix; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 437 | spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 438 | spec->n_remainder = n_remainder; |
| 439 | spec->n_spadding = 0; |
| 440 | spec->n_rpadding = 0; |
| 441 | spec->sign = '\0'; |
| 442 | spec->n_sign = 0; |
| 443 | |
| 444 | /* the output will look like: |
| 445 | | | |
| 446 | | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> | |
| 447 | | | |
| 448 | |
| 449 | sign is computed from format->sign and the actual |
| 450 | sign of the number |
| 451 | |
| 452 | prefix is given (it's for the '0x' prefix) |
| 453 | |
| 454 | digits is already known |
| 455 | |
| 456 | the total width is either given, or computed from the |
| 457 | actual digits |
| 458 | |
| 459 | only one of lpadding, spadding, and rpadding can be non-zero, |
| 460 | and it's calculated from the width and other fields |
| 461 | */ |
| 462 | |
| 463 | /* compute the various parts we're going to write */ |
| 464 | switch (format->sign) { |
| 465 | case '+': |
| 466 | /* always put a + or - */ |
| 467 | spec->n_sign = 1; |
| 468 | spec->sign = (sign_char == '-' ? '-' : '+'); |
| 469 | break; |
| 470 | case ' ': |
| 471 | spec->n_sign = 1; |
| 472 | spec->sign = (sign_char == '-' ? '-' : ' '); |
| 473 | break; |
| 474 | default: |
| 475 | /* Not specified, or the default (-) */ |
| 476 | if (sign_char == '-') { |
| 477 | spec->n_sign = 1; |
| 478 | spec->sign = '-'; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* The number of chars used for non-digits and non-padding. */ |
| 483 | n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal + |
| 484 | spec->n_remainder; |
| 485 | |
| 486 | /* min_width can go negative, that's okay. format->width == -1 means |
| 487 | we don't care. */ |
| 488 | if (format->fill_char == '0' && format->align == '=') |
| 489 | spec->n_min_width = format->width - n_non_digit_non_padding; |
| 490 | else |
| 491 | spec->n_min_width = 0; |
| 492 | |
| 493 | if (spec->n_digits == 0) |
| 494 | /* This case only occurs when using 'c' formatting, we need |
| 495 | to special case it because the grouping code always wants |
| 496 | to have at least one character. */ |
| 497 | spec->n_grouped_digits = 0; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 498 | else { |
| 499 | Py_UCS4 grouping_maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 500 | spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping( |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 501 | NULL, 0, |
| 502 | 0, NULL, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 503 | spec->n_digits, spec->n_min_width, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 504 | locale->grouping, locale->thousands_sep, &grouping_maxchar); |
| 505 | *maxchar = Py_MAX(*maxchar, grouping_maxchar); |
| 506 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 507 | |
| 508 | /* Given the desired width and the total of digit and non-digit |
| 509 | space we consume, see if we need any padding. format->width can |
| 510 | be negative (meaning no padding), but this code still works in |
| 511 | that case. */ |
| 512 | n_padding = format->width - |
| 513 | (n_non_digit_non_padding + spec->n_grouped_digits); |
| 514 | if (n_padding > 0) { |
| 515 | /* Some padding is needed. Determine if it's left, space, or right. */ |
| 516 | switch (format->align) { |
| 517 | case '<': |
| 518 | spec->n_rpadding = n_padding; |
| 519 | break; |
| 520 | case '^': |
| 521 | spec->n_lpadding = n_padding / 2; |
| 522 | spec->n_rpadding = n_padding - spec->n_lpadding; |
| 523 | break; |
| 524 | case '=': |
| 525 | spec->n_spadding = n_padding; |
| 526 | break; |
| 527 | case '>': |
| 528 | spec->n_lpadding = n_padding; |
| 529 | break; |
| 530 | default: |
| 531 | /* Shouldn't get here, but treat it as '>' */ |
| 532 | spec->n_lpadding = n_padding; |
| 533 | assert(0); |
| 534 | break; |
| 535 | } |
| 536 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 537 | |
| 538 | if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding) |
| 539 | *maxchar = Py_MAX(*maxchar, format->fill_char); |
| 540 | |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 541 | if (spec->n_decimal) |
| 542 | *maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point)); |
| 543 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 544 | return spec->n_lpadding + spec->n_sign + spec->n_prefix + |
| 545 | spec->n_spadding + spec->n_grouped_digits + spec->n_decimal + |
| 546 | spec->n_remainder + spec->n_rpadding; |
| 547 | } |
| 548 | |
| 549 | /* Fill in the digit parts of a numbers's string representation, |
| 550 | as determined in calc_number_widths(). |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 551 | Return -1 on error, or 0 on success. */ |
| 552 | static int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 553 | fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 554 | PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end, |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 555 | PyObject *prefix, Py_ssize_t p_start, |
| 556 | Py_UCS4 fill_char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 557 | LocaleInfo *locale, int toupper) |
| 558 | { |
| 559 | /* Used to keep track of digits, decimal, and remainder. */ |
| 560 | Py_ssize_t d_pos = d_start; |
Victor Stinner | 22c103b | 2013-05-07 23:50:03 +0200 | [diff] [blame] | 561 | const unsigned int kind = writer->kind; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 562 | const void *data = writer->data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 563 | Py_ssize_t r; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 564 | |
| 565 | if (spec->n_lpadding) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 566 | _PyUnicode_FastFill(writer->buffer, |
| 567 | writer->pos, spec->n_lpadding, fill_char); |
| 568 | writer->pos += spec->n_lpadding; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 569 | } |
| 570 | if (spec->n_sign == 1) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 571 | PyUnicode_WRITE(kind, data, writer->pos, spec->sign); |
| 572 | writer->pos++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 573 | } |
| 574 | if (spec->n_prefix) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 575 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 576 | prefix, p_start, |
| 577 | spec->n_prefix); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 578 | if (toupper) { |
| 579 | Py_ssize_t t; |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 580 | for (t = 0; t < spec->n_prefix; t++) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 581 | Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t); |
Victor Stinner | ed27785 | 2012-02-01 00:22:23 +0100 | [diff] [blame] | 582 | c = Py_TOUPPER(c); |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 583 | assert (c <= 127); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 584 | PyUnicode_WRITE(kind, data, writer->pos + t, c); |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 585 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 586 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 587 | writer->pos += spec->n_prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 588 | } |
| 589 | if (spec->n_spadding) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 590 | _PyUnicode_FastFill(writer->buffer, |
| 591 | writer->pos, spec->n_spadding, fill_char); |
| 592 | writer->pos += spec->n_spadding; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* Only for type 'c' special case, it has no digits. */ |
| 596 | if (spec->n_digits != 0) { |
| 597 | /* Fill the digits with InsertThousandsGrouping. */ |
Victor Stinner | dba2dee | 2011-09-28 21:50:42 +0200 | [diff] [blame] | 598 | char *pdigits; |
| 599 | if (PyUnicode_READY(digits)) |
| 600 | return -1; |
| 601 | pdigits = PyUnicode_DATA(digits); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 602 | if (PyUnicode_KIND(digits) < kind) { |
| 603 | pdigits = _PyUnicode_AsKind(digits, kind); |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 604 | if (pdigits == NULL) |
| 605 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 606 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 607 | r = _PyUnicode_InsertThousandsGrouping( |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 608 | writer->buffer, writer->pos, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 609 | spec->n_grouped_digits, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 610 | pdigits + kind * d_pos, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 611 | spec->n_digits, spec->n_min_width, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 612 | locale->grouping, locale->thousands_sep, NULL); |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 613 | if (r == -1) |
| 614 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 615 | assert(r == spec->n_grouped_digits); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 616 | if (PyUnicode_KIND(digits) < kind) |
| 617 | PyMem_Free(pdigits); |
| 618 | d_pos += spec->n_digits; |
| 619 | } |
| 620 | if (toupper) { |
| 621 | Py_ssize_t t; |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 622 | for (t = 0; t < spec->n_grouped_digits; t++) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 623 | Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t); |
Victor Stinner | ed27785 | 2012-02-01 00:22:23 +0100 | [diff] [blame] | 624 | c = Py_TOUPPER(c); |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 625 | if (c > 127) { |
| 626 | PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit"); |
| 627 | return -1; |
| 628 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 629 | PyUnicode_WRITE(kind, data, writer->pos + t, c); |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 630 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 631 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 632 | writer->pos += spec->n_grouped_digits; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 633 | |
| 634 | if (spec->n_decimal) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 635 | _PyUnicode_FastCopyCharacters( |
| 636 | writer->buffer, writer->pos, |
| 637 | locale->decimal_point, 0, spec->n_decimal); |
| 638 | writer->pos += spec->n_decimal; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 639 | d_pos += 1; |
| 640 | } |
| 641 | |
| 642 | if (spec->n_remainder) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 643 | _PyUnicode_FastCopyCharacters( |
| 644 | writer->buffer, writer->pos, |
| 645 | digits, d_pos, spec->n_remainder); |
| 646 | writer->pos += spec->n_remainder; |
Brett Cannon | 8a250fa | 2012-06-25 16:13:44 -0400 | [diff] [blame] | 647 | /* d_pos += spec->n_remainder; */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | if (spec->n_rpadding) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 651 | _PyUnicode_FastFill(writer->buffer, |
| 652 | writer->pos, spec->n_rpadding, |
| 653 | fill_char); |
| 654 | writer->pos += spec->n_rpadding; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 655 | } |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 656 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static char no_grouping[1] = {CHAR_MAX}; |
| 660 | |
| 661 | /* Find the decimal point character(s?), thousands_separator(s?), and |
| 662 | grouping description, either for the current locale if type is |
| 663 | LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or |
| 664 | none if LT_NO_LOCALE. */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 665 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 666 | get_locale_info(int type, LocaleInfo *locale_info) |
| 667 | { |
| 668 | switch (type) { |
| 669 | case LT_CURRENT_LOCALE: { |
| 670 | struct lconv *locale_data = localeconv(); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 671 | locale_info->decimal_point = PyUnicode_DecodeLocale( |
| 672 | locale_data->decimal_point, |
| 673 | NULL); |
| 674 | if (locale_info->decimal_point == NULL) |
| 675 | return -1; |
| 676 | locale_info->thousands_sep = PyUnicode_DecodeLocale( |
| 677 | locale_data->thousands_sep, |
| 678 | NULL); |
| 679 | if (locale_info->thousands_sep == NULL) { |
| 680 | Py_DECREF(locale_info->decimal_point); |
| 681 | return -1; |
| 682 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 683 | locale_info->grouping = locale_data->grouping; |
| 684 | break; |
| 685 | } |
| 686 | case LT_DEFAULT_LOCALE: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 687 | locale_info->decimal_point = PyUnicode_FromOrdinal('.'); |
| 688 | locale_info->thousands_sep = PyUnicode_FromOrdinal(','); |
| 689 | if (!locale_info->decimal_point || !locale_info->thousands_sep) { |
| 690 | Py_XDECREF(locale_info->decimal_point); |
| 691 | Py_XDECREF(locale_info->thousands_sep); |
| 692 | return -1; |
| 693 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 694 | locale_info->grouping = "\3"; /* Group every 3 characters. The |
| 695 | (implicit) trailing 0 means repeat |
| 696 | infinitely. */ |
| 697 | break; |
| 698 | case LT_NO_LOCALE: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 699 | locale_info->decimal_point = PyUnicode_FromOrdinal('.'); |
| 700 | locale_info->thousands_sep = PyUnicode_New(0, 0); |
| 701 | if (!locale_info->decimal_point || !locale_info->thousands_sep) { |
| 702 | Py_XDECREF(locale_info->decimal_point); |
| 703 | Py_XDECREF(locale_info->thousands_sep); |
| 704 | return -1; |
| 705 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 706 | locale_info->grouping = no_grouping; |
| 707 | break; |
| 708 | default: |
| 709 | assert(0); |
| 710 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | static void |
| 715 | free_locale_info(LocaleInfo *locale_info) |
| 716 | { |
| 717 | Py_XDECREF(locale_info->decimal_point); |
| 718 | Py_XDECREF(locale_info->thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /************************************************************************/ |
| 722 | /*********** string formatting ******************************************/ |
| 723 | /************************************************************************/ |
| 724 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 725 | static int |
| 726 | format_string_internal(PyObject *value, const InternalFormatSpec *format, |
| 727 | _PyUnicodeWriter *writer) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 728 | { |
| 729 | Py_ssize_t lpad; |
| 730 | Py_ssize_t rpad; |
| 731 | Py_ssize_t total; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 732 | Py_ssize_t len; |
| 733 | int result = -1; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 734 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 735 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 736 | assert(PyUnicode_IS_READY(value)); |
| 737 | len = PyUnicode_GET_LENGTH(value); |
| 738 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 739 | /* sign is not allowed on strings */ |
| 740 | if (format->sign != '\0') { |
| 741 | PyErr_SetString(PyExc_ValueError, |
| 742 | "Sign not allowed in string format specifier"); |
| 743 | goto done; |
| 744 | } |
| 745 | |
| 746 | /* alternate is not allowed on strings */ |
| 747 | if (format->alternate) { |
| 748 | PyErr_SetString(PyExc_ValueError, |
| 749 | "Alternate form (#) not allowed in string format " |
| 750 | "specifier"); |
| 751 | goto done; |
| 752 | } |
| 753 | |
| 754 | /* '=' alignment not allowed on strings */ |
| 755 | if (format->align == '=') { |
| 756 | PyErr_SetString(PyExc_ValueError, |
| 757 | "'=' alignment not allowed " |
| 758 | "in string format specifier"); |
| 759 | goto done; |
| 760 | } |
| 761 | |
Victor Stinner | 621ef3d | 2012-10-02 00:33:47 +0200 | [diff] [blame] | 762 | if ((format->width == -1 || format->width <= len) |
| 763 | && (format->precision == -1 || format->precision >= len)) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 764 | /* Fast path */ |
| 765 | return _PyUnicodeWriter_WriteStr(writer, value); |
| 766 | } |
| 767 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 768 | /* if precision is specified, output no more that format.precision |
| 769 | characters */ |
| 770 | if (format->precision >= 0 && len >= format->precision) { |
| 771 | len = format->precision; |
| 772 | } |
| 773 | |
| 774 | calc_padding(len, format->width, format->align, &lpad, &rpad, &total); |
| 775 | |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 776 | maxchar = writer->maxchar; |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 777 | if (lpad != 0 || rpad != 0) |
| 778 | maxchar = Py_MAX(maxchar, format->fill_char); |
Victor Stinner | eb4b5ac | 2013-04-03 02:02:33 +0200 | [diff] [blame] | 779 | if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) { |
| 780 | Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len); |
| 781 | maxchar = Py_MAX(maxchar, valmaxchar); |
| 782 | } |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 783 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 784 | /* allocate the resulting string */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 785 | if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 786 | goto done; |
| 787 | |
| 788 | /* Write into that space. First the padding. */ |
Eric V. Smith | 2ea9712 | 2014-04-14 11:55:10 -0400 | [diff] [blame] | 789 | result = fill_padding(writer, len, format->fill_char, lpad, rpad); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 790 | if (result == -1) |
| 791 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 792 | |
| 793 | /* Then the source string. */ |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 794 | if (len) { |
| 795 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 796 | value, 0, len); |
| 797 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 798 | writer->pos += (len + rpad); |
| 799 | result = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 800 | |
| 801 | done: |
| 802 | return result; |
| 803 | } |
| 804 | |
| 805 | |
| 806 | /************************************************************************/ |
| 807 | /*********** long formatting ********************************************/ |
| 808 | /************************************************************************/ |
| 809 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 810 | static int |
| 811 | format_long_internal(PyObject *value, const InternalFormatSpec *format, |
| 812 | _PyUnicodeWriter *writer) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 813 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 814 | int result = -1; |
Amaury Forgeot d'Arc | cd27df3 | 2012-01-23 22:42:19 +0100 | [diff] [blame] | 815 | Py_UCS4 maxchar = 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 816 | PyObject *tmp = NULL; |
| 817 | Py_ssize_t inumeric_chars; |
| 818 | Py_UCS4 sign_char = '\0'; |
| 819 | Py_ssize_t n_digits; /* count of digits need from the computed |
| 820 | string */ |
| 821 | Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which |
| 822 | produces non-digits */ |
| 823 | Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */ |
| 824 | Py_ssize_t n_total; |
Victor Stinner | ed27785 | 2012-02-01 00:22:23 +0100 | [diff] [blame] | 825 | Py_ssize_t prefix = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 826 | NumberFieldWidths spec; |
| 827 | long x; |
| 828 | |
| 829 | /* Locale settings, either from the actual locale or |
| 830 | from a hard-code pseudo-locale */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 831 | LocaleInfo locale = STATIC_LOCALE_INFO_INIT; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 832 | |
| 833 | /* no precision allowed on integers */ |
| 834 | if (format->precision != -1) { |
| 835 | PyErr_SetString(PyExc_ValueError, |
| 836 | "Precision not allowed in integer format specifier"); |
| 837 | goto done; |
| 838 | } |
| 839 | |
| 840 | /* special case for character formatting */ |
| 841 | if (format->type == 'c') { |
| 842 | /* error to specify a sign */ |
| 843 | if (format->sign != '\0') { |
| 844 | PyErr_SetString(PyExc_ValueError, |
| 845 | "Sign not allowed with integer" |
| 846 | " format specifier 'c'"); |
| 847 | goto done; |
| 848 | } |
Eric V. Smith | a12572f | 2014-04-15 22:37:55 -0400 | [diff] [blame] | 849 | /* error to request alternate format */ |
| 850 | if (format->alternate) { |
| 851 | PyErr_SetString(PyExc_ValueError, |
| 852 | "Alternate form (#) not allowed with integer" |
| 853 | " format specifier 'c'"); |
| 854 | goto done; |
| 855 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 856 | |
| 857 | /* taken from unicodeobject.c formatchar() */ |
| 858 | /* Integer input truncated to a character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 859 | x = PyLong_AsLong(value); |
| 860 | if (x == -1 && PyErr_Occurred()) |
| 861 | goto done; |
| 862 | if (x < 0 || x > 0x10ffff) { |
| 863 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 864 | "%c arg not in range(0x110000)"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 865 | goto done; |
| 866 | } |
| 867 | tmp = PyUnicode_FromOrdinal(x); |
| 868 | inumeric_chars = 0; |
| 869 | n_digits = 1; |
Amaury Forgeot d'Arc | 6d766fc | 2012-01-23 23:20:43 +0100 | [diff] [blame] | 870 | maxchar = Py_MAX(maxchar, (Py_UCS4)x); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 871 | |
| 872 | /* As a sort-of hack, we tell calc_number_widths that we only |
| 873 | have "remainder" characters. calc_number_widths thinks |
| 874 | these are characters that don't get formatted, only copied |
| 875 | into the output string. We do this for 'c' formatting, |
| 876 | because the characters are likely to be non-digits. */ |
| 877 | n_remainder = 1; |
| 878 | } |
| 879 | else { |
| 880 | int base; |
| 881 | int leading_chars_to_skip = 0; /* Number of characters added by |
| 882 | PyNumber_ToBase that we want to |
| 883 | skip over. */ |
| 884 | |
| 885 | /* Compute the base and how many characters will be added by |
| 886 | PyNumber_ToBase */ |
| 887 | switch (format->type) { |
| 888 | case 'b': |
| 889 | base = 2; |
| 890 | leading_chars_to_skip = 2; /* 0b */ |
| 891 | break; |
| 892 | case 'o': |
| 893 | base = 8; |
| 894 | leading_chars_to_skip = 2; /* 0o */ |
| 895 | break; |
| 896 | case 'x': |
| 897 | case 'X': |
| 898 | base = 16; |
| 899 | leading_chars_to_skip = 2; /* 0x */ |
| 900 | break; |
| 901 | default: /* shouldn't be needed, but stops a compiler warning */ |
| 902 | case 'd': |
| 903 | case 'n': |
| 904 | base = 10; |
| 905 | break; |
| 906 | } |
| 907 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 908 | if (format->sign != '+' && format->sign != ' ' |
| 909 | && format->width == -1 |
| 910 | && format->type != 'X' && format->type != 'n' |
| 911 | && !format->thousands_separators |
| 912 | && PyLong_CheckExact(value)) |
| 913 | { |
| 914 | /* Fast path */ |
| 915 | return _PyLong_FormatWriter(writer, value, base, format->alternate); |
| 916 | } |
| 917 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 918 | /* The number of prefix chars is the same as the leading |
| 919 | chars to skip */ |
| 920 | if (format->alternate) |
| 921 | n_prefix = leading_chars_to_skip; |
| 922 | |
| 923 | /* Do the hard part, converting to a string in a given base */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 924 | tmp = _PyLong_Format(value, base); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 925 | if (tmp == NULL || PyUnicode_READY(tmp) == -1) |
| 926 | goto done; |
| 927 | |
| 928 | inumeric_chars = 0; |
| 929 | n_digits = PyUnicode_GET_LENGTH(tmp); |
| 930 | |
| 931 | prefix = inumeric_chars; |
| 932 | |
| 933 | /* Is a sign character present in the output? If so, remember it |
| 934 | and skip it */ |
| 935 | if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') { |
| 936 | sign_char = '-'; |
| 937 | ++prefix; |
| 938 | ++leading_chars_to_skip; |
| 939 | } |
| 940 | |
| 941 | /* Skip over the leading chars (0x, 0b, etc.) */ |
| 942 | n_digits -= leading_chars_to_skip; |
| 943 | inumeric_chars += leading_chars_to_skip; |
| 944 | } |
| 945 | |
| 946 | /* Determine the grouping, separator, and decimal point, if any. */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 947 | if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : |
| 948 | (format->thousands_separators ? |
| 949 | LT_DEFAULT_LOCALE : |
| 950 | LT_NO_LOCALE), |
| 951 | &locale) == -1) |
| 952 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 953 | |
| 954 | /* Calculate how much memory we'll need. */ |
| 955 | n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 956 | inumeric_chars + n_digits, n_remainder, 0, |
| 957 | &locale, format, &maxchar); |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 958 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 959 | /* Allocate the memory. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 960 | if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 961 | goto done; |
| 962 | |
| 963 | /* Populate the memory. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 964 | result = fill_number(writer, &spec, |
| 965 | tmp, inumeric_chars, inumeric_chars + n_digits, |
Eric V. Smith | 2ea9712 | 2014-04-14 11:55:10 -0400 | [diff] [blame] | 966 | tmp, prefix, format->fill_char, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 967 | &locale, format->type == 'X'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 968 | |
| 969 | done: |
| 970 | Py_XDECREF(tmp); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 971 | free_locale_info(&locale); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 972 | return result; |
| 973 | } |
| 974 | |
| 975 | /************************************************************************/ |
| 976 | /*********** float formatting *******************************************/ |
| 977 | /************************************************************************/ |
| 978 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 979 | /* much of this is taken from unicodeobject.c */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 980 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 981 | format_float_internal(PyObject *value, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 982 | const InternalFormatSpec *format, |
| 983 | _PyUnicodeWriter *writer) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 984 | { |
| 985 | char *buf = NULL; /* buffer returned from PyOS_double_to_string */ |
| 986 | Py_ssize_t n_digits; |
| 987 | Py_ssize_t n_remainder; |
| 988 | Py_ssize_t n_total; |
| 989 | int has_decimal; |
| 990 | double val; |
Victor Stinner | 76d3850 | 2013-06-24 23:34:15 +0200 | [diff] [blame] | 991 | int precision, default_precision = 6; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 992 | Py_UCS4 type = format->type; |
| 993 | int add_pct = 0; |
| 994 | Py_ssize_t index; |
| 995 | NumberFieldWidths spec; |
| 996 | int flags = 0; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 997 | int result = -1; |
Amaury Forgeot d'Arc | cd27df3 | 2012-01-23 22:42:19 +0100 | [diff] [blame] | 998 | Py_UCS4 maxchar = 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 999 | Py_UCS4 sign_char = '\0'; |
| 1000 | int float_type; /* Used to see if we have a nan, inf, or regular float. */ |
| 1001 | PyObject *unicode_tmp = NULL; |
| 1002 | |
| 1003 | /* Locale settings, either from the actual locale or |
| 1004 | from a hard-code pseudo-locale */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1005 | LocaleInfo locale = STATIC_LOCALE_INFO_INIT; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1006 | |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 1007 | if (format->precision > INT_MAX) { |
| 1008 | PyErr_SetString(PyExc_ValueError, "precision too big"); |
| 1009 | goto done; |
| 1010 | } |
| 1011 | precision = (int)format->precision; |
| 1012 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1013 | if (format->alternate) |
| 1014 | flags |= Py_DTSF_ALT; |
| 1015 | |
| 1016 | if (type == '\0') { |
| 1017 | /* Omitted type specifier. Behaves in the same way as repr(x) |
| 1018 | and str(x) if no precision is given, else like 'g', but with |
| 1019 | at least one digit after the decimal point. */ |
| 1020 | flags |= Py_DTSF_ADD_DOT_0; |
| 1021 | type = 'r'; |
| 1022 | default_precision = 0; |
| 1023 | } |
| 1024 | |
| 1025 | if (type == 'n') |
| 1026 | /* 'n' is the same as 'g', except for the locale used to |
| 1027 | format the result. We take care of that later. */ |
| 1028 | type = 'g'; |
| 1029 | |
| 1030 | val = PyFloat_AsDouble(value); |
| 1031 | if (val == -1.0 && PyErr_Occurred()) |
| 1032 | goto done; |
| 1033 | |
| 1034 | if (type == '%') { |
| 1035 | type = 'f'; |
| 1036 | val *= 100; |
| 1037 | add_pct = 1; |
| 1038 | } |
| 1039 | |
| 1040 | if (precision < 0) |
| 1041 | precision = default_precision; |
| 1042 | else if (type == 'r') |
| 1043 | type = 'g'; |
| 1044 | |
| 1045 | /* Cast "type", because if we're in unicode we need to pass a |
| 1046 | 8-bit char. This is safe, because we've restricted what "type" |
| 1047 | can be. */ |
| 1048 | buf = PyOS_double_to_string(val, (char)type, precision, flags, |
| 1049 | &float_type); |
| 1050 | if (buf == NULL) |
| 1051 | goto done; |
| 1052 | n_digits = strlen(buf); |
| 1053 | |
| 1054 | if (add_pct) { |
| 1055 | /* We know that buf has a trailing zero (since we just called |
| 1056 | strlen() on it), and we don't use that fact any more. So we |
| 1057 | can just write over the trailing zero. */ |
| 1058 | buf[n_digits] = '%'; |
| 1059 | n_digits += 1; |
| 1060 | } |
| 1061 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1062 | if (format->sign != '+' && format->sign != ' ' |
| 1063 | && format->width == -1 |
| 1064 | && format->type != 'n' |
| 1065 | && !format->thousands_separators) |
| 1066 | { |
| 1067 | /* Fast path */ |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 1068 | result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits); |
| 1069 | PyMem_Free(buf); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1070 | return result; |
| 1071 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1072 | |
Victor Stinner | 4a58707 | 2013-11-19 12:54:53 +0100 | [diff] [blame] | 1073 | /* Since there is no unicode version of PyOS_double_to_string, |
| 1074 | just use the 8 bit version and then convert to unicode. */ |
| 1075 | unicode_tmp = _PyUnicode_FromASCII(buf, n_digits); |
| 1076 | PyMem_Free(buf); |
| 1077 | if (unicode_tmp == NULL) |
| 1078 | goto done; |
| 1079 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1080 | /* Is a sign character present in the output? If so, remember it |
| 1081 | and skip it */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1082 | index = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1083 | if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') { |
| 1084 | sign_char = '-'; |
| 1085 | ++index; |
| 1086 | --n_digits; |
| 1087 | } |
| 1088 | |
| 1089 | /* Determine if we have any "remainder" (after the digits, might include |
| 1090 | decimal or exponent or both (or neither)) */ |
| 1091 | parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal); |
| 1092 | |
| 1093 | /* Determine the grouping, separator, and decimal point, if any. */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1094 | if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : |
| 1095 | (format->thousands_separators ? |
| 1096 | LT_DEFAULT_LOCALE : |
| 1097 | LT_NO_LOCALE), |
| 1098 | &locale) == -1) |
| 1099 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1100 | |
| 1101 | /* Calculate how much memory we'll need. */ |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1102 | n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1103 | index + n_digits, n_remainder, has_decimal, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1104 | &locale, format, &maxchar); |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 1105 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1106 | /* Allocate the memory. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1107 | if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1108 | goto done; |
| 1109 | |
| 1110 | /* Populate the memory. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1111 | result = fill_number(writer, &spec, |
| 1112 | unicode_tmp, index, index + n_digits, |
Eric V. Smith | 2ea9712 | 2014-04-14 11:55:10 -0400 | [diff] [blame] | 1113 | NULL, 0, format->fill_char, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1114 | &locale, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1115 | |
| 1116 | done: |
Stefan Krah | d9c1bf7 | 2012-09-06 13:02:46 +0200 | [diff] [blame] | 1117 | Py_XDECREF(unicode_tmp); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1118 | free_locale_info(&locale); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1119 | return result; |
| 1120 | } |
| 1121 | |
| 1122 | /************************************************************************/ |
| 1123 | /*********** complex formatting *****************************************/ |
| 1124 | /************************************************************************/ |
| 1125 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1126 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1127 | format_complex_internal(PyObject *value, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1128 | const InternalFormatSpec *format, |
| 1129 | _PyUnicodeWriter *writer) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1130 | { |
| 1131 | double re; |
| 1132 | double im; |
| 1133 | char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */ |
| 1134 | char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */ |
| 1135 | |
| 1136 | InternalFormatSpec tmp_format = *format; |
| 1137 | Py_ssize_t n_re_digits; |
| 1138 | Py_ssize_t n_im_digits; |
| 1139 | Py_ssize_t n_re_remainder; |
| 1140 | Py_ssize_t n_im_remainder; |
| 1141 | Py_ssize_t n_re_total; |
| 1142 | Py_ssize_t n_im_total; |
| 1143 | int re_has_decimal; |
| 1144 | int im_has_decimal; |
Victor Stinner | 76d3850 | 2013-06-24 23:34:15 +0200 | [diff] [blame] | 1145 | int precision, default_precision = 6; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1146 | Py_UCS4 type = format->type; |
| 1147 | Py_ssize_t i_re; |
| 1148 | Py_ssize_t i_im; |
| 1149 | NumberFieldWidths re_spec; |
| 1150 | NumberFieldWidths im_spec; |
| 1151 | int flags = 0; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1152 | int result = -1; |
Amaury Forgeot d'Arc | cd27df3 | 2012-01-23 22:42:19 +0100 | [diff] [blame] | 1153 | Py_UCS4 maxchar = 127; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1154 | enum PyUnicode_Kind rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1155 | void *rdata; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | Py_UCS4 re_sign_char = '\0'; |
| 1157 | Py_UCS4 im_sign_char = '\0'; |
| 1158 | int re_float_type; /* Used to see if we have a nan, inf, or regular float. */ |
| 1159 | int im_float_type; |
| 1160 | int add_parens = 0; |
| 1161 | int skip_re = 0; |
| 1162 | Py_ssize_t lpad; |
| 1163 | Py_ssize_t rpad; |
| 1164 | Py_ssize_t total; |
| 1165 | PyObject *re_unicode_tmp = NULL; |
| 1166 | PyObject *im_unicode_tmp = NULL; |
| 1167 | |
| 1168 | /* Locale settings, either from the actual locale or |
| 1169 | from a hard-code pseudo-locale */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1170 | LocaleInfo locale = STATIC_LOCALE_INFO_INIT; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1171 | |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 1172 | if (format->precision > INT_MAX) { |
| 1173 | PyErr_SetString(PyExc_ValueError, "precision too big"); |
| 1174 | goto done; |
| 1175 | } |
| 1176 | precision = (int)format->precision; |
| 1177 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1178 | /* Zero padding is not allowed. */ |
| 1179 | if (format->fill_char == '0') { |
| 1180 | PyErr_SetString(PyExc_ValueError, |
| 1181 | "Zero padding is not allowed in complex format " |
| 1182 | "specifier"); |
| 1183 | goto done; |
| 1184 | } |
| 1185 | |
| 1186 | /* Neither is '=' alignment . */ |
| 1187 | if (format->align == '=') { |
| 1188 | PyErr_SetString(PyExc_ValueError, |
| 1189 | "'=' alignment flag is not allowed in complex format " |
| 1190 | "specifier"); |
| 1191 | goto done; |
| 1192 | } |
| 1193 | |
| 1194 | re = PyComplex_RealAsDouble(value); |
| 1195 | if (re == -1.0 && PyErr_Occurred()) |
| 1196 | goto done; |
| 1197 | im = PyComplex_ImagAsDouble(value); |
| 1198 | if (im == -1.0 && PyErr_Occurred()) |
| 1199 | goto done; |
| 1200 | |
| 1201 | if (format->alternate) |
| 1202 | flags |= Py_DTSF_ALT; |
| 1203 | |
| 1204 | if (type == '\0') { |
| 1205 | /* Omitted type specifier. Should be like str(self). */ |
| 1206 | type = 'r'; |
| 1207 | default_precision = 0; |
| 1208 | if (re == 0.0 && copysign(1.0, re) == 1.0) |
| 1209 | skip_re = 1; |
| 1210 | else |
| 1211 | add_parens = 1; |
| 1212 | } |
| 1213 | |
| 1214 | if (type == 'n') |
| 1215 | /* 'n' is the same as 'g', except for the locale used to |
| 1216 | format the result. We take care of that later. */ |
| 1217 | type = 'g'; |
| 1218 | |
| 1219 | if (precision < 0) |
| 1220 | precision = default_precision; |
| 1221 | else if (type == 'r') |
| 1222 | type = 'g'; |
| 1223 | |
| 1224 | /* Cast "type", because if we're in unicode we need to pass a |
| 1225 | 8-bit char. This is safe, because we've restricted what "type" |
| 1226 | can be. */ |
| 1227 | re_buf = PyOS_double_to_string(re, (char)type, precision, flags, |
| 1228 | &re_float_type); |
| 1229 | if (re_buf == NULL) |
| 1230 | goto done; |
| 1231 | im_buf = PyOS_double_to_string(im, (char)type, precision, flags, |
| 1232 | &im_float_type); |
| 1233 | if (im_buf == NULL) |
| 1234 | goto done; |
| 1235 | |
| 1236 | n_re_digits = strlen(re_buf); |
| 1237 | n_im_digits = strlen(im_buf); |
| 1238 | |
| 1239 | /* Since there is no unicode version of PyOS_double_to_string, |
| 1240 | just use the 8 bit version and then convert to unicode. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1241 | re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1242 | if (re_unicode_tmp == NULL) |
| 1243 | goto done; |
| 1244 | i_re = 0; |
| 1245 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1246 | im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1247 | if (im_unicode_tmp == NULL) |
| 1248 | goto done; |
| 1249 | i_im = 0; |
| 1250 | |
| 1251 | /* Is a sign character present in the output? If so, remember it |
| 1252 | and skip it */ |
| 1253 | if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') { |
| 1254 | re_sign_char = '-'; |
| 1255 | ++i_re; |
| 1256 | --n_re_digits; |
| 1257 | } |
| 1258 | if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') { |
| 1259 | im_sign_char = '-'; |
| 1260 | ++i_im; |
| 1261 | --n_im_digits; |
| 1262 | } |
| 1263 | |
| 1264 | /* Determine if we have any "remainder" (after the digits, might include |
| 1265 | decimal or exponent or both (or neither)) */ |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1266 | parse_number(re_unicode_tmp, i_re, i_re + n_re_digits, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1267 | &n_re_remainder, &re_has_decimal); |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1268 | parse_number(im_unicode_tmp, i_im, i_im + n_im_digits, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1269 | &n_im_remainder, &im_has_decimal); |
| 1270 | |
| 1271 | /* Determine the grouping, separator, and decimal point, if any. */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1272 | if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : |
| 1273 | (format->thousands_separators ? |
| 1274 | LT_DEFAULT_LOCALE : |
| 1275 | LT_NO_LOCALE), |
| 1276 | &locale) == -1) |
| 1277 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1278 | |
| 1279 | /* Turn off any padding. We'll do it later after we've composed |
| 1280 | the numbers without padding. */ |
| 1281 | tmp_format.fill_char = '\0'; |
| 1282 | tmp_format.align = '<'; |
| 1283 | tmp_format.width = -1; |
| 1284 | |
| 1285 | /* Calculate how much memory we'll need. */ |
| 1286 | n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp, |
| 1287 | i_re, i_re + n_re_digits, n_re_remainder, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1288 | re_has_decimal, &locale, &tmp_format, |
| 1289 | &maxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1290 | |
| 1291 | /* Same formatting, but always include a sign, unless the real part is |
| 1292 | * going to be omitted, in which case we use whatever sign convention was |
| 1293 | * requested by the original format. */ |
| 1294 | if (!skip_re) |
| 1295 | tmp_format.sign = '+'; |
| 1296 | n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp, |
| 1297 | i_im, i_im + n_im_digits, n_im_remainder, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1298 | im_has_decimal, &locale, &tmp_format, |
| 1299 | &maxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1300 | |
| 1301 | if (skip_re) |
| 1302 | n_re_total = 0; |
| 1303 | |
| 1304 | /* Add 1 for the 'j', and optionally 2 for parens. */ |
| 1305 | calc_padding(n_re_total + n_im_total + 1 + add_parens * 2, |
| 1306 | format->width, format->align, &lpad, &rpad, &total); |
| 1307 | |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1308 | if (lpad || rpad) |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 1309 | maxchar = Py_MAX(maxchar, format->fill_char); |
| 1310 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1311 | if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1312 | goto done; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1313 | rkind = writer->kind; |
| 1314 | rdata = writer->data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1315 | |
| 1316 | /* Populate the memory. First, the padding. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1317 | result = fill_padding(writer, |
| 1318 | n_re_total + n_im_total + 1 + add_parens * 2, |
Eric V. Smith | 2ea9712 | 2014-04-14 11:55:10 -0400 | [diff] [blame] | 1319 | format->fill_char, lpad, rpad); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1320 | if (result == -1) |
| 1321 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1322 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1323 | if (add_parens) { |
| 1324 | PyUnicode_WRITE(rkind, rdata, writer->pos, '('); |
| 1325 | writer->pos++; |
| 1326 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1327 | |
| 1328 | if (!skip_re) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1329 | result = fill_number(writer, &re_spec, |
| 1330 | re_unicode_tmp, i_re, i_re + n_re_digits, |
| 1331 | NULL, 0, |
| 1332 | 0, |
| 1333 | &locale, 0); |
| 1334 | if (result == -1) |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1335 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1336 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1337 | result = fill_number(writer, &im_spec, |
| 1338 | im_unicode_tmp, i_im, i_im + n_im_digits, |
| 1339 | NULL, 0, |
| 1340 | 0, |
| 1341 | &locale, 0); |
| 1342 | if (result == -1) |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1343 | goto done; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1344 | PyUnicode_WRITE(rkind, rdata, writer->pos, 'j'); |
| 1345 | writer->pos++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1347 | if (add_parens) { |
| 1348 | PyUnicode_WRITE(rkind, rdata, writer->pos, ')'); |
| 1349 | writer->pos++; |
| 1350 | } |
| 1351 | |
| 1352 | writer->pos += rpad; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | |
| 1354 | done: |
| 1355 | PyMem_Free(re_buf); |
| 1356 | PyMem_Free(im_buf); |
| 1357 | Py_XDECREF(re_unicode_tmp); |
| 1358 | Py_XDECREF(im_unicode_tmp); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1359 | free_locale_info(&locale); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1360 | return result; |
| 1361 | } |
| 1362 | |
| 1363 | /************************************************************************/ |
| 1364 | /*********** built in formatters ****************************************/ |
| 1365 | /************************************************************************/ |
doko@ubuntu.com | 39378f7 | 2012-06-21 12:12:20 +0200 | [diff] [blame] | 1366 | static int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1367 | format_obj(PyObject *obj, _PyUnicodeWriter *writer) |
| 1368 | { |
| 1369 | PyObject *str; |
| 1370 | int err; |
| 1371 | |
| 1372 | str = PyObject_Str(obj); |
| 1373 | if (str == NULL) |
| 1374 | return -1; |
| 1375 | err = _PyUnicodeWriter_WriteStr(writer, str); |
| 1376 | Py_DECREF(str); |
| 1377 | return err; |
| 1378 | } |
| 1379 | |
| 1380 | int |
| 1381 | _PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer, |
| 1382 | PyObject *obj, |
| 1383 | PyObject *format_spec, |
| 1384 | Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1385 | { |
| 1386 | InternalFormatSpec format; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1387 | |
| 1388 | assert(PyUnicode_Check(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1389 | |
| 1390 | /* check for the special case of zero length format spec, make |
| 1391 | it equivalent to str(obj) */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1392 | if (start == end) { |
| 1393 | if (PyUnicode_CheckExact(obj)) |
| 1394 | return _PyUnicodeWriter_WriteStr(writer, obj); |
| 1395 | else |
| 1396 | return format_obj(obj, writer); |
| 1397 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1398 | |
| 1399 | /* parse the format_spec */ |
| 1400 | if (!parse_internal_render_format_spec(format_spec, start, end, |
| 1401 | &format, 's', '<')) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1402 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1403 | |
| 1404 | /* type conversion? */ |
| 1405 | switch (format.type) { |
| 1406 | case 's': |
| 1407 | /* no type conversion needed, already a string. do the formatting */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1408 | return format_string_internal(obj, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1409 | default: |
| 1410 | /* unknown */ |
| 1411 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1412 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1413 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 | } |
| 1415 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1416 | int |
| 1417 | _PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer, |
| 1418 | PyObject *obj, |
| 1419 | PyObject *format_spec, |
| 1420 | Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1421 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1422 | PyObject *tmp = NULL, *str = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1423 | InternalFormatSpec format; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1424 | int result = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1425 | |
| 1426 | /* check for the special case of zero length format spec, make |
| 1427 | it equivalent to str(obj) */ |
| 1428 | if (start == end) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1429 | if (PyLong_CheckExact(obj)) |
| 1430 | return _PyLong_FormatWriter(writer, obj, 10, 0); |
| 1431 | else |
| 1432 | return format_obj(obj, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /* parse the format_spec */ |
| 1436 | if (!parse_internal_render_format_spec(format_spec, start, end, |
| 1437 | &format, 'd', '>')) |
| 1438 | goto done; |
| 1439 | |
| 1440 | /* type conversion? */ |
| 1441 | switch (format.type) { |
| 1442 | case 'b': |
| 1443 | case 'c': |
| 1444 | case 'd': |
| 1445 | case 'o': |
| 1446 | case 'x': |
| 1447 | case 'X': |
| 1448 | case 'n': |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1449 | /* no type conversion needed, already an int. do the formatting */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1450 | result = format_long_internal(obj, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1451 | break; |
| 1452 | |
| 1453 | case 'e': |
| 1454 | case 'E': |
| 1455 | case 'f': |
| 1456 | case 'F': |
| 1457 | case 'g': |
| 1458 | case 'G': |
| 1459 | case '%': |
| 1460 | /* convert to float */ |
| 1461 | tmp = PyNumber_Float(obj); |
| 1462 | if (tmp == NULL) |
| 1463 | goto done; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1464 | result = format_float_internal(tmp, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1465 | break; |
| 1466 | |
| 1467 | default: |
| 1468 | /* unknown */ |
| 1469 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
| 1470 | goto done; |
| 1471 | } |
| 1472 | |
| 1473 | done: |
| 1474 | Py_XDECREF(tmp); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1475 | Py_XDECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1476 | return result; |
| 1477 | } |
| 1478 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1479 | int |
| 1480 | _PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer, |
| 1481 | PyObject *obj, |
| 1482 | PyObject *format_spec, |
| 1483 | Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1484 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1485 | InternalFormatSpec format; |
| 1486 | |
| 1487 | /* check for the special case of zero length format spec, make |
| 1488 | it equivalent to str(obj) */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1489 | if (start == end) |
| 1490 | return format_obj(obj, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1491 | |
| 1492 | /* parse the format_spec */ |
| 1493 | if (!parse_internal_render_format_spec(format_spec, start, end, |
| 1494 | &format, '\0', '>')) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1495 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1496 | |
| 1497 | /* type conversion? */ |
| 1498 | switch (format.type) { |
| 1499 | case '\0': /* No format code: like 'g', but with at least one decimal. */ |
| 1500 | case 'e': |
| 1501 | case 'E': |
| 1502 | case 'f': |
| 1503 | case 'F': |
| 1504 | case 'g': |
| 1505 | case 'G': |
| 1506 | case 'n': |
| 1507 | case '%': |
| 1508 | /* no conversion, already a float. do the formatting */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1509 | return format_float_internal(obj, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1510 | |
| 1511 | default: |
| 1512 | /* unknown */ |
| 1513 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1514 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1515 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1518 | int |
| 1519 | _PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer, |
| 1520 | PyObject *obj, |
| 1521 | PyObject *format_spec, |
| 1522 | Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1523 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1524 | InternalFormatSpec format; |
| 1525 | |
| 1526 | /* check for the special case of zero length format spec, make |
| 1527 | it equivalent to str(obj) */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1528 | if (start == end) |
| 1529 | return format_obj(obj, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1530 | |
| 1531 | /* parse the format_spec */ |
| 1532 | if (!parse_internal_render_format_spec(format_spec, start, end, |
| 1533 | &format, '\0', '>')) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1534 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1535 | |
| 1536 | /* type conversion? */ |
| 1537 | switch (format.type) { |
| 1538 | case '\0': /* No format code: like 'g', but with at least one decimal. */ |
| 1539 | case 'e': |
| 1540 | case 'E': |
| 1541 | case 'f': |
| 1542 | case 'F': |
| 1543 | case 'g': |
| 1544 | case 'G': |
| 1545 | case 'n': |
| 1546 | /* no conversion, already a complex. do the formatting */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1547 | return format_complex_internal(obj, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1548 | |
| 1549 | default: |
| 1550 | /* unknown */ |
| 1551 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1552 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1553 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1554 | } |