blob: df07b11e13f8a40940732adb91cb64245b03e96f [file] [log] [blame]
Eric Smith8c663262007-08-25 02:26:07 +00001/* implements the string, long, and float formatters. that is,
2 string.__format__, etc. */
3
Eric Smith0923d1d2009-04-16 20:16:10 +00004#include <locale.h>
5
Eric Smith8c663262007-08-25 02:26:07 +00006/* Before including this, you must include either:
7 stringlib/unicodedefs.h
8 stringlib/stringdefs.h
9
10 Also, you should define the names:
11 FORMAT_STRING
12 FORMAT_LONG
13 FORMAT_FLOAT
Eric Smith58a42242009-04-30 01:00:33 +000014 FORMAT_COMPLEX
Eric Smith8c663262007-08-25 02:26:07 +000015 to be whatever you want the public names of these functions to
16 be. These are the only non-static functions defined here.
17*/
18
Eric Smith5e5c0db2009-02-20 14:25:03 +000019/* Raises an exception about an unknown presentation type for this
20 * type. */
21
22static void
23unknown_presentation_type(STRINGLIB_CHAR presentation_type,
24 const char* type_name)
25{
26#if STRINGLIB_IS_UNICODE
27 /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
28 hence the two cases. If it is char, gcc complains that the
29 condition below is always true, hence the ifdef. */
30 if (presentation_type > 32 && presentation_type < 128)
31#endif
32 PyErr_Format(PyExc_ValueError,
33 "Unknown format code '%c' "
34 "for object of type '%.200s'",
35 presentation_type,
36 type_name);
37#if STRINGLIB_IS_UNICODE
38 else
39 PyErr_Format(PyExc_ValueError,
40 "Unknown format code '\\x%x' "
41 "for object of type '%.200s'",
42 (unsigned int)presentation_type,
43 type_name);
44#endif
45}
46
Eric Smith8c663262007-08-25 02:26:07 +000047/*
48 get_integer consumes 0 or more decimal digit characters from an
49 input string, updates *result with the corresponding positive
50 integer, and returns the number of digits consumed.
51
52 returns -1 on error.
53*/
54static int
55get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
56 Py_ssize_t *result)
57{
58 Py_ssize_t accumulator, digitval, oldaccumulator;
59 int numdigits;
60 accumulator = numdigits = 0;
61 for (;;(*ptr)++, numdigits++) {
62 if (*ptr >= end)
63 break;
64 digitval = STRINGLIB_TODECIMAL(**ptr);
65 if (digitval < 0)
66 break;
67 /*
68 This trick was copied from old Unicode format code. It's cute,
69 but would really suck on an old machine with a slow divide
70 implementation. Fortunately, in the normal case we do not
71 expect too many digits.
72 */
73 oldaccumulator = accumulator;
74 accumulator *= 10;
75 if ((accumulator+10)/10 != oldaccumulator+1) {
76 PyErr_Format(PyExc_ValueError,
77 "Too many decimal digits in format string");
78 return -1;
79 }
80 accumulator += digitval;
81 }
82 *result = accumulator;
83 return numdigits;
84}
85
86/************************************************************************/
87/*********** standard format specifier parsing **************************/
88/************************************************************************/
89
90/* returns true if this character is a specifier alignment token */
91Py_LOCAL_INLINE(int)
92is_alignment_token(STRINGLIB_CHAR c)
93{
94 switch (c) {
95 case '<': case '>': case '=': case '^':
96 return 1;
97 default:
98 return 0;
99 }
100}
101
102/* returns true if this character is a sign element */
103Py_LOCAL_INLINE(int)
104is_sign_element(STRINGLIB_CHAR c)
105{
106 switch (c) {
Eric Smithb7f5ba12007-08-29 12:38:45 +0000107 case ' ': case '+': case '-':
Eric Smith8c663262007-08-25 02:26:07 +0000108 return 1;
109 default:
110 return 0;
111 }
112}
113
114
115typedef struct {
116 STRINGLIB_CHAR fill_char;
117 STRINGLIB_CHAR align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000118 int alternate;
Eric Smith8c663262007-08-25 02:26:07 +0000119 STRINGLIB_CHAR sign;
120 Py_ssize_t width;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000121 int thousands_separators;
Eric Smith8c663262007-08-25 02:26:07 +0000122 Py_ssize_t precision;
123 STRINGLIB_CHAR type;
124} InternalFormatSpec;
125
Eric Smith903fc052010-02-22 19:26:06 +0000126
127#if 0
128/* Occassionally useful for debugging. Should normally be commented out. */
129static void
130DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
131{
132 printf("internal format spec: fill_char %d\n", format->fill_char);
133 printf("internal format spec: align %d\n", format->align);
134 printf("internal format spec: alternate %d\n", format->alternate);
135 printf("internal format spec: sign %d\n", format->sign);
136 printf("internal format spec: width %d\n", format->width);
137 printf("internal format spec: thousands_separators %d\n",
138 format->thousands_separators);
139 printf("internal format spec: precision %d\n", format->precision);
140 printf("internal format spec: type %c\n", format->type);
141 printf("\n");
142}
143#endif
144
145
Eric Smith8c663262007-08-25 02:26:07 +0000146/*
147 ptr points to the start of the format_spec, end points just past its end.
148 fills in format with the parsed information.
149 returns 1 on success, 0 on failure.
150 if failure, sets the exception
151*/
152static int
Eric Smith4a7d76d2008-05-30 18:10:19 +0000153parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000154 Py_ssize_t format_spec_len,
Eric Smith8c663262007-08-25 02:26:07 +0000155 InternalFormatSpec *format,
Eric Smith903fc052010-02-22 19:26:06 +0000156 char default_type,
157 char default_align)
Eric Smith8c663262007-08-25 02:26:07 +0000158{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000159 STRINGLIB_CHAR *ptr = format_spec;
160 STRINGLIB_CHAR *end = format_spec + format_spec_len;
Eric Smith8c663262007-08-25 02:26:07 +0000161
162 /* end-ptr is used throughout this code to specify the length of
163 the input string */
164
Eric Smith0923d1d2009-04-16 20:16:10 +0000165 Py_ssize_t consumed;
Eric Smith8c663262007-08-25 02:26:07 +0000166
167 format->fill_char = '\0';
Eric Smith903fc052010-02-22 19:26:06 +0000168 format->align = default_align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000169 format->alternate = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000170 format->sign = '\0';
171 format->width = -1;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000172 format->thousands_separators = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000173 format->precision = -1;
174 format->type = default_type;
175
176 /* If the second char is an alignment token,
177 then parse the fill char */
178 if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
179 format->align = ptr[1];
180 format->fill_char = ptr[0];
181 ptr += 2;
Eric Smith0cb431c2007-08-28 01:07:27 +0000182 }
183 else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
Eric Smith8c663262007-08-25 02:26:07 +0000184 format->align = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000185 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000186 }
187
188 /* Parse the various sign options */
189 if (end-ptr >= 1 && is_sign_element(ptr[0])) {
190 format->sign = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000191 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000192 }
193
Eric Smithd68af8f2008-07-16 00:15:35 +0000194 /* If the next character is #, we're in alternate mode. This only
195 applies to integers. */
196 if (end-ptr >= 1 && ptr[0] == '#') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000197 format->alternate = 1;
198 ++ptr;
Eric Smithd68af8f2008-07-16 00:15:35 +0000199 }
200
Eric Smith8c663262007-08-25 02:26:07 +0000201 /* The special case for 0-padding (backwards compat) */
Eric Smith185e30c2007-08-30 22:23:08 +0000202 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
Eric Smith8c663262007-08-25 02:26:07 +0000203 format->fill_char = '0';
204 if (format->align == '\0') {
205 format->align = '=';
206 }
Christian Heimesc3f30c42008-02-22 16:37:40 +0000207 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000208 }
209
Eric Smith0923d1d2009-04-16 20:16:10 +0000210 consumed = get_integer(&ptr, end, &format->width);
211 if (consumed == -1)
212 /* Overflow error. Exception already set. */
213 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000214
Eric Smith0923d1d2009-04-16 20:16:10 +0000215 /* If consumed is 0, we didn't consume any characters for the
216 width. In that case, reset the width to -1, because
217 get_integer() will have set it to zero. -1 is how we record
218 that the width wasn't specified. */
219 if (consumed == 0)
Eric Smith8c663262007-08-25 02:26:07 +0000220 format->width = -1;
Eric Smith8c663262007-08-25 02:26:07 +0000221
Eric Smitha3b1ac82009-04-03 14:45:06 +0000222 /* Comma signifies add thousands separators */
223 if (end-ptr && ptr[0] == ',') {
224 format->thousands_separators = 1;
225 ++ptr;
226 }
227
Eric Smith8c663262007-08-25 02:26:07 +0000228 /* Parse field precision */
229 if (end-ptr && ptr[0] == '.') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000230 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000231
Eric Smith0923d1d2009-04-16 20:16:10 +0000232 consumed = get_integer(&ptr, end, &format->precision);
233 if (consumed == -1)
234 /* Overflow error. Exception already set. */
235 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000236
Eric Smith0923d1d2009-04-16 20:16:10 +0000237 /* Not having a precision after a dot is an error. */
238 if (consumed == 0) {
Eric Smith8c663262007-08-25 02:26:07 +0000239 PyErr_Format(PyExc_ValueError,
240 "Format specifier missing precision");
241 return 0;
242 }
243
244 }
245
Eric Smith0923d1d2009-04-16 20:16:10 +0000246 /* Finally, parse the type field. */
Eric Smith8c663262007-08-25 02:26:07 +0000247
248 if (end-ptr > 1) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000249 /* More than one char remain, invalid conversion spec. */
Eric Smith8c663262007-08-25 02:26:07 +0000250 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
251 return 0;
252 }
253
254 if (end-ptr == 1) {
255 format->type = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000256 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000257 }
258
Eric Smith0923d1d2009-04-16 20:16:10 +0000259 /* Do as much validating as we can, just by looking at the format
260 specifier. Do not take into account what type of formatting
261 we're doing (int, float, string). */
262
263 if (format->thousands_separators) {
264 switch (format->type) {
265 case 'd':
266 case 'e':
267 case 'f':
268 case 'g':
269 case 'E':
270 case 'G':
271 case '%':
272 case 'F':
Eric Smith937491d2009-04-22 17:04:27 +0000273 case '\0':
Eric Smith0923d1d2009-04-16 20:16:10 +0000274 /* These are allowed. See PEP 378.*/
275 break;
276 default:
277 PyErr_Format(PyExc_ValueError,
278 "Cannot specify ',' with '%c'.", format->type);
279 return 0;
280 }
Eric Smitha3b1ac82009-04-03 14:45:06 +0000281 }
282
Eric Smith8c663262007-08-25 02:26:07 +0000283 return 1;
284}
285
Eric Smith58a42242009-04-30 01:00:33 +0000286/* Calculate the padding needed. */
287static void
288calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
289 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
290 Py_ssize_t *n_total)
291{
292 if (width >= 0) {
293 if (nchars > width)
294 *n_total = nchars;
295 else
296 *n_total = width;
297 }
298 else {
299 /* not specified, use all of the chars and no more */
300 *n_total = nchars;
301 }
302
Eric Smith903fc052010-02-22 19:26:06 +0000303 /* Figure out how much leading space we need, based on the
Eric Smith58a42242009-04-30 01:00:33 +0000304 aligning */
305 if (align == '>')
306 *n_lpadding = *n_total - nchars;
307 else if (align == '^')
308 *n_lpadding = (*n_total - nchars) / 2;
Eric Smith903fc052010-02-22 19:26:06 +0000309 else if (align == '<' || align == '=')
Eric Smith58a42242009-04-30 01:00:33 +0000310 *n_lpadding = 0;
Eric Smith903fc052010-02-22 19:26:06 +0000311 else {
312 /* We should never have an unspecified alignment. */
313 *n_lpadding = 0;
314 assert(0);
315 }
Eric Smith58a42242009-04-30 01:00:33 +0000316
317 *n_rpadding = *n_total - nchars - *n_lpadding;
318}
319
320/* Do the padding, and return a pointer to where the caller-supplied
321 content goes. */
322static STRINGLIB_CHAR *
323fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
324 Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
325{
326 /* Pad on left. */
327 if (n_lpadding)
328 STRINGLIB_FILL(p, fill_char, n_lpadding);
329
330 /* Pad on right. */
331 if (n_rpadding)
332 STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
333
334 /* Pointer to the user content. */
335 return p + n_lpadding;
336}
337
338#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
Eric Smith8c663262007-08-25 02:26:07 +0000339/************************************************************************/
340/*********** common routines for numeric formatting *********************/
341/************************************************************************/
342
Eric Smith0923d1d2009-04-16 20:16:10 +0000343/* Locale type codes. */
344#define LT_CURRENT_LOCALE 0
345#define LT_DEFAULT_LOCALE 1
346#define LT_NO_LOCALE 2
347
348/* Locale info needed for formatting integers and the part of floats
349 before and including the decimal. Note that locales only support
350 8-bit chars, not unicode. */
351typedef struct {
352 char *decimal_point;
353 char *thousands_sep;
354 char *grouping;
355} LocaleInfo;
356
Eric Smith8c663262007-08-25 02:26:07 +0000357/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000358 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000359typedef struct {
360 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000361 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000362 Py_ssize_t n_spadding;
363 Py_ssize_t n_rpadding;
Eric Smith0923d1d2009-04-16 20:16:10 +0000364 char sign;
365 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
366 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
367 any grouping chars. */
368 Py_ssize_t n_decimal; /* 0 if only an integer */
369 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
370 excluding the decimal itself, if
371 present. */
372
373 /* These 2 are not the widths of fields, but are needed by
374 STRINGLIB_GROUPING. */
375 Py_ssize_t n_digits; /* The number of digits before a decimal
376 or exponent. */
377 Py_ssize_t n_min_width; /* The min_width we used when we computed
378 the n_grouped_digits width. */
Eric Smith8c663262007-08-25 02:26:07 +0000379} NumberFieldWidths;
380
Eric Smith58a42242009-04-30 01:00:33 +0000381
Eric Smith0923d1d2009-04-16 20:16:10 +0000382/* Given a number of the form:
383 digits[remainder]
384 where ptr points to the start and end points to the end, find where
385 the integer part ends. This could be a decimal, an exponent, both,
386 or neither.
387 If a decimal point is present, set *has_decimal and increment
388 remainder beyond it.
389 Results are undefined (but shouldn't crash) for improperly
390 formatted strings.
391*/
392static void
393parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
394 Py_ssize_t *n_remainder, int *has_decimal)
395{
396 STRINGLIB_CHAR *end = ptr + len;
397 STRINGLIB_CHAR *remainder;
398
399 while (ptr<end && isdigit(*ptr))
400 ++ptr;
401 remainder = ptr;
402
403 /* Does remainder start with a decimal point? */
404 *has_decimal = ptr<end && *remainder == '.';
405
406 /* Skip the decimal point. */
407 if (*has_decimal)
408 remainder++;
409
410 *n_remainder = end - remainder;
411}
412
Eric Smith8c663262007-08-25 02:26:07 +0000413/* not all fields of format are used. for example, precision is
414 unused. should this take discrete params in order to be more clear
415 about what it does? or is passing a single format parameter easier
416 and more efficient enough to justify a little obfuscation? */
Eric Smith0923d1d2009-04-16 20:16:10 +0000417static Py_ssize_t
418calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
419 STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
420 Py_ssize_t n_number, Py_ssize_t n_remainder,
421 int has_decimal, const LocaleInfo *locale,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000422 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000423{
Eric Smith0923d1d2009-04-16 20:16:10 +0000424 Py_ssize_t n_non_digit_non_padding;
425 Py_ssize_t n_padding;
426
427 spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
Eric Smith05212a12008-07-16 19:41:14 +0000428 spec->n_lpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000429 spec->n_prefix = n_prefix;
430 spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
431 spec->n_remainder = n_remainder;
Eric Smith05212a12008-07-16 19:41:14 +0000432 spec->n_spadding = 0;
433 spec->n_rpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000434 spec->sign = '\0';
435 spec->n_sign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000436
437 /* the output will look like:
Eric Smith0923d1d2009-04-16 20:16:10 +0000438 | |
439 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
440 | |
Eric Smith8c663262007-08-25 02:26:07 +0000441
Eric Smith0923d1d2009-04-16 20:16:10 +0000442 sign is computed from format->sign and the actual
Eric Smith8c663262007-08-25 02:26:07 +0000443 sign of the number
444
Eric Smithb1ebcc62008-07-15 13:02:41 +0000445 prefix is given (it's for the '0x' prefix)
446
Eric Smith8c663262007-08-25 02:26:07 +0000447 digits is already known
448
449 the total width is either given, or computed from the
450 actual digits
451
452 only one of lpadding, spadding, and rpadding can be non-zero,
453 and it's calculated from the width and other fields
454 */
455
456 /* compute the various parts we're going to write */
Eric Smith0923d1d2009-04-16 20:16:10 +0000457 switch (format->sign) {
458 case '+':
Eric Smith8c663262007-08-25 02:26:07 +0000459 /* always put a + or - */
Eric Smith0923d1d2009-04-16 20:16:10 +0000460 spec->n_sign = 1;
461 spec->sign = (sign_char == '-' ? '-' : '+');
462 break;
463 case ' ':
464 spec->n_sign = 1;
465 spec->sign = (sign_char == '-' ? '-' : ' ');
466 break;
467 default:
468 /* Not specified, or the default (-) */
469 if (sign_char == '-') {
470 spec->n_sign = 1;
471 spec->sign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000472 }
473 }
474
Eric Smith0923d1d2009-04-16 20:16:10 +0000475 /* The number of chars used for non-digits and non-padding. */
476 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
477 spec->n_remainder;
Eric Smithd68af8f2008-07-16 00:15:35 +0000478
Eric Smith0923d1d2009-04-16 20:16:10 +0000479 /* min_width can go negative, that's okay. format->width == -1 means
480 we don't care. */
481 if (format->fill_char == '0')
482 spec->n_min_width = format->width - n_non_digit_non_padding;
483 else
484 spec->n_min_width = 0;
485
486 if (spec->n_digits == 0)
487 /* This case only occurs when using 'c' formatting, we need
488 to special case it because the grouping code always wants
489 to have at least one character. */
490 spec->n_grouped_digits = 0;
491 else
492 spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
493 spec->n_digits,
494 spec->n_min_width,
495 locale->grouping,
496 locale->thousands_sep);
497
498 /* Given the desired width and the total of digit and non-digit
499 space we consume, see if we need any padding. format->width can
500 be negative (meaning no padding), but this code still works in
501 that case. */
502 n_padding = format->width -
503 (n_non_digit_non_padding + spec->n_grouped_digits);
504 if (n_padding > 0) {
505 /* Some padding is needed. Determine if it's left, space, or right. */
506 switch (format->align) {
507 case '<':
508 spec->n_rpadding = n_padding;
509 break;
510 case '^':
511 spec->n_lpadding = n_padding / 2;
512 spec->n_rpadding = n_padding - spec->n_lpadding;
513 break;
514 case '=':
515 spec->n_spadding = n_padding;
516 break;
Eric Smith903fc052010-02-22 19:26:06 +0000517 case '>':
Eric Smith0923d1d2009-04-16 20:16:10 +0000518 spec->n_lpadding = n_padding;
519 break;
Eric Smith903fc052010-02-22 19:26:06 +0000520 default:
521 /* Shouldn't get here, but treat it as '>' */
522 spec->n_lpadding = n_padding;
523 assert(0);
524 break;
Eric Smith8c663262007-08-25 02:26:07 +0000525 }
526 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000527 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
528 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
529 spec->n_remainder + spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000530}
531
Eric Smith0923d1d2009-04-16 20:16:10 +0000532/* Fill in the digit parts of a numbers's string representation,
533 as determined in calc_number_widths().
534 No error checking, since we know the buffer is the correct size. */
535static void
536fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
537 STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
538 STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
539 LocaleInfo *locale, int toupper)
Eric Smith8c663262007-08-25 02:26:07 +0000540{
Eric Smith0923d1d2009-04-16 20:16:10 +0000541 /* Used to keep track of digits, decimal, and remainder. */
542 STRINGLIB_CHAR *p = digits;
543
544#ifndef NDEBUG
545 Py_ssize_t r;
546#endif
Eric Smith8c663262007-08-25 02:26:07 +0000547
548 if (spec->n_lpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000549 STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
550 buf += spec->n_lpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000551 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000552 if (spec->n_sign == 1) {
553 *buf++ = spec->sign;
Eric Smith8c663262007-08-25 02:26:07 +0000554 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000555 if (spec->n_prefix) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000556 memmove(buf,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000557 prefix,
558 spec->n_prefix * sizeof(STRINGLIB_CHAR));
Eric Smith0923d1d2009-04-16 20:16:10 +0000559 if (toupper) {
560 Py_ssize_t t;
561 for (t = 0; t < spec->n_prefix; ++t)
562 buf[t] = STRINGLIB_TOUPPER(buf[t]);
563 }
564 buf += spec->n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000565 }
Eric Smith8c663262007-08-25 02:26:07 +0000566 if (spec->n_spadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000567 STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
568 buf += spec->n_spadding;
Eric Smith8c663262007-08-25 02:26:07 +0000569 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000570
571 /* Only for type 'c' special case, it has no digits. */
572 if (spec->n_digits != 0) {
573 /* Fill the digits with InsertThousandsGrouping. */
574#ifndef NDEBUG
575 r =
576#endif
577 STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
578 spec->n_digits, spec->n_min_width,
579 locale->grouping, locale->thousands_sep);
580#ifndef NDEBUG
581 assert(r == spec->n_grouped_digits);
582#endif
583 p += spec->n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000584 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000585 if (toupper) {
586 Py_ssize_t t;
587 for (t = 0; t < spec->n_grouped_digits; ++t)
588 buf[t] = STRINGLIB_TOUPPER(buf[t]);
589 }
590 buf += spec->n_grouped_digits;
591
592 if (spec->n_decimal) {
593 Py_ssize_t t;
594 for (t = 0; t < spec->n_decimal; ++t)
595 buf[t] = locale->decimal_point[t];
596 buf += spec->n_decimal;
597 p += 1;
598 }
599
600 if (spec->n_remainder) {
601 memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
602 buf += spec->n_remainder;
603 p += spec->n_remainder;
604 }
605
Eric Smith8c663262007-08-25 02:26:07 +0000606 if (spec->n_rpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000607 STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
608 buf += spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000609 }
Eric Smith8c663262007-08-25 02:26:07 +0000610}
Eric Smith0923d1d2009-04-16 20:16:10 +0000611
612static char no_grouping[1] = {CHAR_MAX};
613
614/* Find the decimal point character(s?), thousands_separator(s?), and
615 grouping description, either for the current locale if type is
616 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
617 none if LT_NO_LOCALE. */
618static void
619get_locale_info(int type, LocaleInfo *locale_info)
620{
621 switch (type) {
622 case LT_CURRENT_LOCALE: {
623 struct lconv *locale_data = localeconv();
624 locale_info->decimal_point = locale_data->decimal_point;
625 locale_info->thousands_sep = locale_data->thousands_sep;
626 locale_info->grouping = locale_data->grouping;
627 break;
628 }
629 case LT_DEFAULT_LOCALE:
630 locale_info->decimal_point = ".";
631 locale_info->thousands_sep = ",";
632 locale_info->grouping = "\3"; /* Group every 3 characters,
633 trailing 0 means repeat
634 infinitely. */
635 break;
636 case LT_NO_LOCALE:
637 locale_info->decimal_point = ".";
638 locale_info->thousands_sep = "";
639 locale_info->grouping = no_grouping;
640 break;
641 default:
642 assert(0);
643 }
644}
645
Eric Smith58a42242009-04-30 01:00:33 +0000646#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
Eric Smith8c663262007-08-25 02:26:07 +0000647
648/************************************************************************/
649/*********** string formatting ******************************************/
650/************************************************************************/
651
652static PyObject *
653format_string_internal(PyObject *value, const InternalFormatSpec *format)
654{
Eric Smith8c663262007-08-25 02:26:07 +0000655 Py_ssize_t lpad;
Eric Smith58a42242009-04-30 01:00:33 +0000656 Py_ssize_t rpad;
657 Py_ssize_t total;
658 STRINGLIB_CHAR *p;
Eric Smith8c663262007-08-25 02:26:07 +0000659 Py_ssize_t len = STRINGLIB_LEN(value);
660 PyObject *result = NULL;
661
662 /* sign is not allowed on strings */
663 if (format->sign != '\0') {
664 PyErr_SetString(PyExc_ValueError,
665 "Sign not allowed in string format specifier");
666 goto done;
667 }
668
Eric Smithb1ebcc62008-07-15 13:02:41 +0000669 /* alternate is not allowed on strings */
670 if (format->alternate) {
671 PyErr_SetString(PyExc_ValueError,
672 "Alternate form (#) not allowed in string format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000673 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000674 goto done;
675 }
676
Eric Smith8c663262007-08-25 02:26:07 +0000677 /* '=' alignment not allowed on strings */
678 if (format->align == '=') {
679 PyErr_SetString(PyExc_ValueError,
680 "'=' alignment not allowed "
681 "in string format specifier");
682 goto done;
683 }
684
685 /* if precision is specified, output no more that format.precision
686 characters */
687 if (format->precision >= 0 && len >= format->precision) {
688 len = format->precision;
689 }
690
Eric Smith58a42242009-04-30 01:00:33 +0000691 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
Eric Smith8c663262007-08-25 02:26:07 +0000692
693 /* allocate the resulting string */
Eric Smith58a42242009-04-30 01:00:33 +0000694 result = STRINGLIB_NEW(NULL, total);
Eric Smith8c663262007-08-25 02:26:07 +0000695 if (result == NULL)
696 goto done;
697
Eric Smith58a42242009-04-30 01:00:33 +0000698 /* Write into that space. First the padding. */
699 p = fill_padding(STRINGLIB_STR(result), len,
700 format->fill_char=='\0'?' ':format->fill_char,
701 lpad, rpad);
Eric Smith8c663262007-08-25 02:26:07 +0000702
Eric Smith58a42242009-04-30 01:00:33 +0000703 /* Then the source string. */
704 memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
Eric Smith8c663262007-08-25 02:26:07 +0000705
706done:
707 return result;
708}
709
710
711/************************************************************************/
712/*********** long formatting ********************************************/
713/************************************************************************/
714
Eric Smith8fd3eba2008-02-17 19:48:00 +0000715#if defined FORMAT_LONG || defined FORMAT_INT
716typedef PyObject*
717(*IntOrLongToString)(PyObject *value, int base);
718
Eric Smith8c663262007-08-25 02:26:07 +0000719static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000720format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000721 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000722{
723 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000724 PyObject *tmp = NULL;
725 STRINGLIB_CHAR *pnumeric_chars;
726 STRINGLIB_CHAR numeric_char;
Eric Smith0923d1d2009-04-16 20:16:10 +0000727 STRINGLIB_CHAR sign_char = '\0';
Eric Smith8c663262007-08-25 02:26:07 +0000728 Py_ssize_t n_digits; /* count of digits need from the computed
729 string */
Eric Smith0923d1d2009-04-16 20:16:10 +0000730 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
731 produces non-digits */
Eric Smithd68af8f2008-07-16 00:15:35 +0000732 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
Eric Smith0923d1d2009-04-16 20:16:10 +0000733 Py_ssize_t n_total;
Eric Smithd68af8f2008-07-16 00:15:35 +0000734 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000735 NumberFieldWidths spec;
736 long x;
737
Eric Smith0923d1d2009-04-16 20:16:10 +0000738 /* Locale settings, either from the actual locale or
739 from a hard-code pseudo-locale */
740 LocaleInfo locale;
741
Eric Smith8c663262007-08-25 02:26:07 +0000742 /* no precision allowed on integers */
743 if (format->precision != -1) {
744 PyErr_SetString(PyExc_ValueError,
745 "Precision not allowed in integer format specifier");
746 goto done;
747 }
748
Eric Smith8c663262007-08-25 02:26:07 +0000749 /* special case for character formatting */
750 if (format->type == 'c') {
751 /* error to specify a sign */
752 if (format->sign != '\0') {
753 PyErr_SetString(PyExc_ValueError,
754 "Sign not allowed with integer"
755 " format specifier 'c'");
756 goto done;
757 }
758
Eric Smith0923d1d2009-04-16 20:16:10 +0000759 /* Error to specify a comma. */
760 if (format->thousands_separators) {
761 PyErr_SetString(PyExc_ValueError,
762 "Thousands separators not allowed with integer"
763 " format specifier 'c'");
764 goto done;
765 }
766
Eric Smith8c663262007-08-25 02:26:07 +0000767 /* taken from unicodeobject.c formatchar() */
768 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000769/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000770 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000771 if (x == -1 && PyErr_Occurred())
772 goto done;
773#ifdef Py_UNICODE_WIDE
774 if (x < 0 || x > 0x10ffff) {
775 PyErr_SetString(PyExc_OverflowError,
776 "%c arg not in range(0x110000) "
777 "(wide Python build)");
778 goto done;
779 }
780#else
781 if (x < 0 || x > 0xffff) {
782 PyErr_SetString(PyExc_OverflowError,
783 "%c arg not in range(0x10000) "
784 "(narrow Python build)");
785 goto done;
786 }
787#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000788 numeric_char = (STRINGLIB_CHAR)x;
789 pnumeric_chars = &numeric_char;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000790 n_digits = 1;
Eric Smith0923d1d2009-04-16 20:16:10 +0000791
792 /* As a sort-of hack, we tell calc_number_widths that we only
793 have "remainder" characters. calc_number_widths thinks
794 these are characters that don't get formatted, only copied
795 into the output string. We do this for 'c' formatting,
796 because the characters are likely to be non-digits. */
797 n_remainder = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000798 }
799 else {
Eric Smith8c663262007-08-25 02:26:07 +0000800 int base;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000801 int leading_chars_to_skip = 0; /* Number of characters added by
802 PyNumber_ToBase that we want to
803 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000804
805 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000806 PyNumber_ToBase */
807 switch (format->type) {
808 case 'b':
809 base = 2;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000810 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000811 break;
812 case 'o':
813 base = 8;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000814 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000815 break;
816 case 'x':
817 case 'X':
818 base = 16;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000819 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000820 break;
821 default: /* shouldn't be needed, but stops a compiler warning */
822 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000823 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000824 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000825 break;
826 }
827
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000828 /* The number of prefix chars is the same as the leading
829 chars to skip */
830 if (format->alternate)
831 n_prefix = leading_chars_to_skip;
Eric Smithd68af8f2008-07-16 00:15:35 +0000832
Eric Smith8fd3eba2008-02-17 19:48:00 +0000833 /* Do the hard part, converting to a string in a given base */
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000834 tmp = tostring(value, base);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000835 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000836 goto done;
837
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000838 pnumeric_chars = STRINGLIB_STR(tmp);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000839 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000840
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000841 prefix = pnumeric_chars;
Eric Smithd68af8f2008-07-16 00:15:35 +0000842
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000843 /* Remember not to modify what pnumeric_chars points to. it
844 might be interned. Only modify it after we copy it into a
845 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000846
Eric Smith8fd3eba2008-02-17 19:48:00 +0000847 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000848 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000849 if (pnumeric_chars[0] == '-') {
850 sign_char = pnumeric_chars[0];
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000851 ++prefix;
852 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000853 }
854
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000855 /* Skip over the leading chars (0x, 0b, etc.) */
856 n_digits -= leading_chars_to_skip;
857 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000858 }
859
Eric Smith0923d1d2009-04-16 20:16:10 +0000860 /* Determine the grouping, separator, and decimal point, if any. */
861 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
862 (format->thousands_separators ?
863 LT_DEFAULT_LOCALE :
864 LT_NO_LOCALE),
865 &locale);
Eric Smith5807c412008-05-11 21:00:57 +0000866
Eric Smith0923d1d2009-04-16 20:16:10 +0000867 /* Calculate how much memory we'll need. */
868 n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
869 n_digits, n_remainder, 0, &locale, format);
Eric Smithb151a452008-06-24 11:21:04 +0000870
Eric Smith0923d1d2009-04-16 20:16:10 +0000871 /* Allocate the memory. */
872 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000873 if (!result)
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000874 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000875
Eric Smith0923d1d2009-04-16 20:16:10 +0000876 /* Populate the memory. */
877 fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
878 prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
879 &locale, format->type == 'X');
Eric Smithd68af8f2008-07-16 00:15:35 +0000880
Eric Smith8c663262007-08-25 02:26:07 +0000881done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000882 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000883 return result;
884}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000885#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000886
887/************************************************************************/
888/*********** float formatting *******************************************/
889/************************************************************************/
890
Eric Smith8fd3eba2008-02-17 19:48:00 +0000891#ifdef FORMAT_FLOAT
892#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000893static void
894strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
Eric Smith8c663262007-08-25 02:26:07 +0000895{
Eric Smith0923d1d2009-04-16 20:16:10 +0000896 Py_ssize_t i;
897 for (i = 0; i < len; ++i)
898 buffer[i] = (Py_UNICODE)charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000899}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000900#endif
Eric Smith8c663262007-08-25 02:26:07 +0000901
Eric Smith8c663262007-08-25 02:26:07 +0000902/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000903static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000904format_float_internal(PyObject *value,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000905 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000906{
Eric Smith0923d1d2009-04-16 20:16:10 +0000907 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
Eric Smith8c663262007-08-25 02:26:07 +0000908 Py_ssize_t n_digits;
Eric Smith0923d1d2009-04-16 20:16:10 +0000909 Py_ssize_t n_remainder;
910 Py_ssize_t n_total;
911 int has_decimal;
912 double val;
Eric Smith8c663262007-08-25 02:26:07 +0000913 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +0000914 Py_ssize_t default_precision = 6;
Eric Smith0923d1d2009-04-16 20:16:10 +0000915 STRINGLIB_CHAR type = format->type;
916 int add_pct = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000917 STRINGLIB_CHAR *p;
918 NumberFieldWidths spec;
Eric Smith0923d1d2009-04-16 20:16:10 +0000919 int flags = 0;
920 PyObject *result = NULL;
921 STRINGLIB_CHAR sign_char = '\0';
922 int float_type; /* Used to see if we have a nan, inf, or regular float. */
Eric Smith8c663262007-08-25 02:26:07 +0000923
924#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000925 Py_UNICODE *unicode_tmp = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000926#endif
927
Eric Smith0923d1d2009-04-16 20:16:10 +0000928 /* Locale settings, either from the actual locale or
929 from a hard-code pseudo-locale */
930 LocaleInfo locale;
931
932 /* Alternate is not allowed on floats. */
Eric Smithb1ebcc62008-07-15 13:02:41 +0000933 if (format->alternate) {
934 PyErr_SetString(PyExc_ValueError,
935 "Alternate form (#) not allowed in float format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000936 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000937 goto done;
938 }
939
Eric Smith0923d1d2009-04-16 20:16:10 +0000940 if (type == '\0') {
Eric Smith63376222009-05-05 14:04:18 +0000941 /* Omitted type specifier. This is like 'g' but with at least one
942 digit after the decimal point, and different default precision.*/
Eric Smith0923d1d2009-04-16 20:16:10 +0000943 type = 'g';
Eric Smith63376222009-05-05 14:04:18 +0000944 default_precision = PyFloat_STR_PRECISION;
Eric Smith0923d1d2009-04-16 20:16:10 +0000945 flags |= Py_DTSF_ADD_DOT_0;
946 }
947
948 if (type == 'n')
949 /* 'n' is the same as 'g', except for the locale used to
950 format the result. We take care of that later. */
951 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000952
Eric Smith7bc66b12009-07-27 02:12:11 +0000953#if PY_VERSION_HEX < 0x0301000
954 /* 'F' is the same as 'f', per the PEP */
955 /* This is no longer the case in 3.x */
956 if (type == 'F')
957 type = 'f';
958#endif
959
Eric Smith0923d1d2009-04-16 20:16:10 +0000960 val = PyFloat_AsDouble(value);
961 if (val == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000962 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000963
964 if (type == '%') {
965 type = 'f';
Eric Smith0923d1d2009-04-16 20:16:10 +0000966 val *= 100;
967 add_pct = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000968 }
969
970 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +0000971 precision = default_precision;
Eric Smith8c663262007-08-25 02:26:07 +0000972
Eric Smith7255f182009-05-02 12:15:39 +0000973#if PY_VERSION_HEX < 0x03010000
974 /* 3.1 no longer converts large 'f' to 'g'. */
Eric Smith7bc66b12009-07-27 02:12:11 +0000975 if ((type == 'f' || type == 'F') && fabs(val) >= 1e50)
976 type = 'g';
Eric Smith7255f182009-05-02 12:15:39 +0000977#endif
978
Eric Smith0923d1d2009-04-16 20:16:10 +0000979 /* Cast "type", because if we're in unicode we need to pass a
980 8-bit char. This is safe, because we've restricted what "type"
981 can be. */
982 buf = PyOS_double_to_string(val, (char)type, precision, flags,
983 &float_type);
984 if (buf == NULL)
985 goto done;
986 n_digits = strlen(buf);
Eric Smith8c663262007-08-25 02:26:07 +0000987
Eric Smith0923d1d2009-04-16 20:16:10 +0000988 if (add_pct) {
989 /* We know that buf has a trailing zero (since we just called
990 strlen() on it), and we don't use that fact any more. So we
991 can just write over the trailing zero. */
992 buf[n_digits] = '%';
993 n_digits += 1;
994 }
Eric Smith8c663262007-08-25 02:26:07 +0000995
Eric Smith0923d1d2009-04-16 20:16:10 +0000996 /* Since there is no unicode version of PyOS_double_to_string,
997 just use the 8 bit version and then convert to unicode. */
Eric Smith8c663262007-08-25 02:26:07 +0000998#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000999 unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
1000 if (unicode_tmp == NULL) {
1001 PyErr_NoMemory();
1002 goto done;
1003 }
1004 strtounicode(unicode_tmp, buf, n_digits);
1005 p = unicode_tmp;
Eric Smith8c663262007-08-25 02:26:07 +00001006#else
Eric Smith0923d1d2009-04-16 20:16:10 +00001007 p = buf;
Eric Smith8c663262007-08-25 02:26:07 +00001008#endif
1009
Eric Smith0923d1d2009-04-16 20:16:10 +00001010 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +00001011 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +00001012 if (*p == '-') {
1013 sign_char = *p;
Christian Heimesc3f30c42008-02-22 16:37:40 +00001014 ++p;
1015 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +00001016 }
1017
Eric Smith0923d1d2009-04-16 20:16:10 +00001018 /* Determine if we have any "remainder" (after the digits, might include
1019 decimal or exponent or both (or neither)) */
1020 parse_number(p, n_digits, &n_remainder, &has_decimal);
Eric Smith8c663262007-08-25 02:26:07 +00001021
Eric Smith0923d1d2009-04-16 20:16:10 +00001022 /* Determine the grouping, separator, and decimal point, if any. */
1023 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1024 (format->thousands_separators ?
1025 LT_DEFAULT_LOCALE :
1026 LT_NO_LOCALE),
1027 &locale);
1028
1029 /* Calculate how much memory we'll need. */
1030 n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
1031 n_remainder, has_decimal, &locale, format);
1032
1033 /* Allocate the memory. */
1034 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8c663262007-08-25 02:26:07 +00001035 if (result == NULL)
1036 goto done;
1037
Eric Smith0923d1d2009-04-16 20:16:10 +00001038 /* Populate the memory. */
1039 fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
1040 format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
1041 0);
Eric Smith8c663262007-08-25 02:26:07 +00001042
1043done:
Eric Smith0923d1d2009-04-16 20:16:10 +00001044 PyMem_Free(buf);
1045#if STRINGLIB_IS_UNICODE
1046 PyMem_Free(unicode_tmp);
1047#endif
Eric Smith8c663262007-08-25 02:26:07 +00001048 return result;
1049}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001050#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +00001051
1052/************************************************************************/
Eric Smith58a42242009-04-30 01:00:33 +00001053/*********** complex formatting *****************************************/
1054/************************************************************************/
1055
1056#ifdef FORMAT_COMPLEX
1057
1058static PyObject *
1059format_complex_internal(PyObject *value,
1060 const InternalFormatSpec *format)
1061{
1062 double re;
1063 double im;
1064 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1065 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1066
1067 InternalFormatSpec tmp_format = *format;
1068 Py_ssize_t n_re_digits;
1069 Py_ssize_t n_im_digits;
1070 Py_ssize_t n_re_remainder;
1071 Py_ssize_t n_im_remainder;
1072 Py_ssize_t n_re_total;
1073 Py_ssize_t n_im_total;
1074 int re_has_decimal;
1075 int im_has_decimal;
1076 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +00001077 Py_ssize_t default_precision = 6;
Eric Smith58a42242009-04-30 01:00:33 +00001078 STRINGLIB_CHAR type = format->type;
1079 STRINGLIB_CHAR *p_re;
1080 STRINGLIB_CHAR *p_im;
1081 NumberFieldWidths re_spec;
1082 NumberFieldWidths im_spec;
1083 int flags = 0;
1084 PyObject *result = NULL;
1085 STRINGLIB_CHAR *p;
1086 STRINGLIB_CHAR re_sign_char = '\0';
1087 STRINGLIB_CHAR im_sign_char = '\0';
1088 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1089 int im_float_type;
1090 int add_parens = 0;
1091 int skip_re = 0;
1092 Py_ssize_t lpad;
1093 Py_ssize_t rpad;
1094 Py_ssize_t total;
1095
1096#if STRINGLIB_IS_UNICODE
1097 Py_UNICODE *re_unicode_tmp = NULL;
1098 Py_UNICODE *im_unicode_tmp = NULL;
1099#endif
1100
1101 /* Locale settings, either from the actual locale or
1102 from a hard-code pseudo-locale */
1103 LocaleInfo locale;
1104
1105 /* Alternate is not allowed on complex. */
1106 if (format->alternate) {
1107 PyErr_SetString(PyExc_ValueError,
1108 "Alternate form (#) not allowed in complex format "
1109 "specifier");
1110 goto done;
1111 }
1112
1113 /* Neither is zero pading. */
1114 if (format->fill_char == '0') {
1115 PyErr_SetString(PyExc_ValueError,
1116 "Zero padding is not allowed in complex format "
1117 "specifier");
1118 goto done;
1119 }
1120
1121 /* Neither is '=' alignment . */
1122 if (format->align == '=') {
1123 PyErr_SetString(PyExc_ValueError,
1124 "'=' alignment flag is not allowed in complex format "
1125 "specifier");
1126 goto done;
1127 }
1128
1129 re = PyComplex_RealAsDouble(value);
1130 if (re == -1.0 && PyErr_Occurred())
1131 goto done;
1132 im = PyComplex_ImagAsDouble(value);
1133 if (im == -1.0 && PyErr_Occurred())
1134 goto done;
1135
1136 if (type == '\0') {
1137 /* Omitted type specifier. Should be like str(self). */
1138 type = 'g';
Eric Smith63376222009-05-05 14:04:18 +00001139 default_precision = PyFloat_STR_PRECISION;
Eric Smith58a42242009-04-30 01:00:33 +00001140 add_parens = 1;
1141 if (re == 0.0)
1142 skip_re = 1;
1143 }
1144
1145 if (type == 'n')
1146 /* 'n' is the same as 'g', except for the locale used to
1147 format the result. We take care of that later. */
1148 type = 'g';
1149
Eric Smith7bc66b12009-07-27 02:12:11 +00001150#if PY_VERSION_HEX < 0x03010000
1151 /* This is no longer the case in 3.x */
1152 /* 'F' is the same as 'f', per the PEP */
1153 if (type == 'F')
1154 type = 'f';
1155#endif
1156
Eric Smith58a42242009-04-30 01:00:33 +00001157 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +00001158 precision = default_precision;
Eric Smith58a42242009-04-30 01:00:33 +00001159
1160 /* Cast "type", because if we're in unicode we need to pass a
1161 8-bit char. This is safe, because we've restricted what "type"
1162 can be. */
1163 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1164 &re_float_type);
1165 if (re_buf == NULL)
1166 goto done;
1167 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1168 &im_float_type);
1169 if (im_buf == NULL)
1170 goto done;
1171
1172 n_re_digits = strlen(re_buf);
1173 n_im_digits = strlen(im_buf);
1174
1175 /* Since there is no unicode version of PyOS_double_to_string,
1176 just use the 8 bit version and then convert to unicode. */
1177#if STRINGLIB_IS_UNICODE
1178 re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
1179 if (re_unicode_tmp == NULL) {
1180 PyErr_NoMemory();
1181 goto done;
1182 }
1183 strtounicode(re_unicode_tmp, re_buf, n_re_digits);
1184 p_re = re_unicode_tmp;
1185
1186 im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
1187 if (im_unicode_tmp == NULL) {
1188 PyErr_NoMemory();
1189 goto done;
1190 }
1191 strtounicode(im_unicode_tmp, im_buf, n_im_digits);
1192 p_im = im_unicode_tmp;
1193#else
1194 p_re = re_buf;
1195 p_im = im_buf;
1196#endif
1197
1198 /* Is a sign character present in the output? If so, remember it
1199 and skip it */
1200 if (*p_re == '-') {
1201 re_sign_char = *p_re;
1202 ++p_re;
1203 --n_re_digits;
1204 }
1205 if (*p_im == '-') {
1206 im_sign_char = *p_im;
1207 ++p_im;
1208 --n_im_digits;
1209 }
1210
1211 /* Determine if we have any "remainder" (after the digits, might include
1212 decimal or exponent or both (or neither)) */
1213 parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
1214 parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
1215
1216 /* Determine the grouping, separator, and decimal point, if any. */
1217 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1218 (format->thousands_separators ?
1219 LT_DEFAULT_LOCALE :
1220 LT_NO_LOCALE),
1221 &locale);
1222
1223 /* Turn off any padding. We'll do it later after we've composed
1224 the numbers without padding. */
1225 tmp_format.fill_char = '\0';
Eric Smith903fc052010-02-22 19:26:06 +00001226 tmp_format.align = '<';
Eric Smith58a42242009-04-30 01:00:33 +00001227 tmp_format.width = -1;
1228
1229 /* Calculate how much memory we'll need. */
1230 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
1231 n_re_digits, n_re_remainder,
1232 re_has_decimal, &locale, &tmp_format);
1233
1234 /* Same formatting, but always include a sign. */
1235 tmp_format.sign = '+';
1236 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
1237 n_im_digits, n_im_remainder,
1238 im_has_decimal, &locale, &tmp_format);
1239
1240 if (skip_re)
1241 n_re_total = 0;
1242
1243 /* Add 1 for the 'j', and optionally 2 for parens. */
1244 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1245 format->width, format->align, &lpad, &rpad, &total);
1246
1247 result = STRINGLIB_NEW(NULL, total);
1248 if (result == NULL)
1249 goto done;
1250
1251 /* Populate the memory. First, the padding. */
1252 p = fill_padding(STRINGLIB_STR(result),
1253 n_re_total + n_im_total + 1 + add_parens * 2,
1254 format->fill_char=='\0' ? ' ' : format->fill_char,
1255 lpad, rpad);
1256
1257 if (add_parens)
1258 *p++ = '(';
1259
1260 if (!skip_re) {
1261 fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
1262 p += n_re_total;
1263 }
1264 fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
1265 p += n_im_total;
1266 *p++ = 'j';
1267
1268 if (add_parens)
1269 *p++ = ')';
1270
1271done:
1272 PyMem_Free(re_buf);
1273 PyMem_Free(im_buf);
1274#if STRINGLIB_IS_UNICODE
1275 PyMem_Free(re_unicode_tmp);
1276 PyMem_Free(im_unicode_tmp);
1277#endif
1278 return result;
1279}
1280#endif /* FORMAT_COMPLEX */
1281
1282/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001283/*********** built in formatters ****************************************/
1284/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001285PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001286FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001287 STRINGLIB_CHAR *format_spec,
1288 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001289{
Eric Smith8c663262007-08-25 02:26:07 +00001290 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +00001291 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001292
1293 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001294 it equivalent to str(obj) */
1295 if (format_spec_len == 0) {
1296 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001297 goto done;
1298 }
1299
1300 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001301 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001302 &format, 's', '<'))
Eric Smith8c663262007-08-25 02:26:07 +00001303 goto done;
1304
1305 /* type conversion? */
1306 switch (format.type) {
1307 case 's':
1308 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001309 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001310 break;
Eric Smith8c663262007-08-25 02:26:07 +00001311 default:
1312 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001313 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001314 goto done;
1315 }
1316
1317done:
Eric Smith8c663262007-08-25 02:26:07 +00001318 return result;
1319}
1320
Eric Smith8fd3eba2008-02-17 19:48:00 +00001321#if defined FORMAT_LONG || defined FORMAT_INT
1322static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +00001323format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001324 STRINGLIB_CHAR *format_spec,
1325 Py_ssize_t format_spec_len,
1326 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +00001327{
Eric Smith8c663262007-08-25 02:26:07 +00001328 PyObject *result = NULL;
1329 PyObject *tmp = NULL;
1330 InternalFormatSpec format;
1331
Eric Smith8c663262007-08-25 02:26:07 +00001332 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001333 it equivalent to str(obj) */
1334 if (format_spec_len == 0) {
1335 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001336 goto done;
1337 }
1338
1339 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001340 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001341 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001342 &format, 'd', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001343 goto done;
1344
1345 /* type conversion? */
1346 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +00001347 case 'b':
1348 case 'c':
1349 case 'd':
1350 case 'o':
1351 case 'x':
1352 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +00001353 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +00001354 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001355 the formatting */
1356 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +00001357 break;
1358
Eric Smithfa767ef2008-01-28 10:59:27 +00001359 case 'e':
1360 case 'E':
1361 case 'f':
1362 case 'F':
1363 case 'g':
1364 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +00001365 case '%':
1366 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001367 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +00001368 if (tmp == NULL)
1369 goto done;
Eric Smithf64bce82009-04-13 00:50:23 +00001370 result = format_float_internal(tmp, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +00001371 break;
1372
Eric Smith8c663262007-08-25 02:26:07 +00001373 default:
1374 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001375 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001376 goto done;
1377 }
1378
1379done:
1380 Py_XDECREF(tmp);
1381 return result;
1382}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001383#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +00001384
Eric Smith8fd3eba2008-02-17 19:48:00 +00001385#ifdef FORMAT_LONG
1386/* Need to define long_format as a function that will convert a long
1387 to a string. In 3.0, _PyLong_Format has the correct signature. In
1388 2.x, we need to fudge a few parameters */
1389#if PY_VERSION_HEX >= 0x03000000
1390#define long_format _PyLong_Format
1391#else
1392static PyObject*
1393long_format(PyObject* value, int base)
1394{
1395 /* Convert to base, don't add trailing 'L', and use the new octal
1396 format. We already know this is a long object */
1397 assert(PyLong_Check(value));
1398 /* convert to base, don't add 'L', and use the new octal format */
1399 return _PyLong_Format(value, base, 0, 1);
1400}
1401#endif
1402
1403PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001404FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001405 STRINGLIB_CHAR *format_spec,
1406 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001407{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001408 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001409 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001410}
1411#endif /* FORMAT_LONG */
1412
1413#ifdef FORMAT_INT
1414/* this is only used for 2.x, not 3.0 */
1415static PyObject*
1416int_format(PyObject* value, int base)
1417{
1418 /* Convert to base, and use the new octal format. We already
1419 know this is an int object */
1420 assert(PyInt_Check(value));
1421 return _PyInt_Format((PyIntObject*)value, base, 1);
1422}
1423
1424PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001425FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001426 STRINGLIB_CHAR *format_spec,
1427 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001428{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001429 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001430 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001431}
1432#endif /* FORMAT_INT */
1433
1434#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001435PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001436FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001437 STRINGLIB_CHAR *format_spec,
1438 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001439{
Eric Smith8c663262007-08-25 02:26:07 +00001440 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001441 InternalFormatSpec format;
1442
Eric Smith8c663262007-08-25 02:26:07 +00001443 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001444 it equivalent to str(obj) */
1445 if (format_spec_len == 0) {
1446 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001447 goto done;
1448 }
1449
1450 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001451 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001452 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001453 &format, '\0', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001454 goto done;
1455
1456 /* type conversion? */
1457 switch (format.type) {
Eric Smith0923d1d2009-04-16 20:16:10 +00001458 case '\0': /* No format code: like 'g', but with at least one decimal. */
Eric Smith8c663262007-08-25 02:26:07 +00001459 case 'e':
1460 case 'E':
1461 case 'f':
1462 case 'F':
1463 case 'g':
1464 case 'G':
1465 case 'n':
1466 case '%':
1467 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001468 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001469 break;
1470
1471 default:
1472 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001473 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001474 goto done;
1475 }
1476
1477done:
Eric Smith8c663262007-08-25 02:26:07 +00001478 return result;
1479}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001480#endif /* FORMAT_FLOAT */
Eric Smith58a42242009-04-30 01:00:33 +00001481
1482#ifdef FORMAT_COMPLEX
1483PyObject *
1484FORMAT_COMPLEX(PyObject *obj,
1485 STRINGLIB_CHAR *format_spec,
1486 Py_ssize_t format_spec_len)
1487{
1488 PyObject *result = NULL;
1489 InternalFormatSpec format;
1490
1491 /* check for the special case of zero length format spec, make
1492 it equivalent to str(obj) */
1493 if (format_spec_len == 0) {
1494 result = STRINGLIB_TOSTR(obj);
1495 goto done;
1496 }
1497
1498 /* parse the format_spec */
1499 if (!parse_internal_render_format_spec(format_spec,
1500 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001501 &format, '\0', '>'))
Eric Smith58a42242009-04-30 01:00:33 +00001502 goto done;
1503
1504 /* type conversion? */
1505 switch (format.type) {
1506 case '\0': /* No format code: like 'g', but with at least one decimal. */
1507 case 'e':
1508 case 'E':
1509 case 'f':
1510 case 'F':
1511 case 'g':
1512 case 'G':
1513 case 'n':
1514 /* no conversion, already a complex. do the formatting */
1515 result = format_complex_internal(obj, &format);
1516 break;
1517
1518 default:
1519 /* unknown */
1520 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1521 goto done;
1522 }
1523
1524done:
1525 return result;
1526}
1527#endif /* FORMAT_COMPLEX */