blob: 1bd7acb03cd929ba78d7e7dbde01e5f59788c449 [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'",
Eric Smithbeddd702009-07-30 13:43:08 +000035 (char)presentation_type,
Eric Smith5e5c0db2009-02-20 14:25:03 +000036 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 Smithbeddd702009-07-30 13:43:08 +000047static void
48invalid_comma_type(STRINGLIB_CHAR presentation_type)
49{
50#if STRINGLIB_IS_UNICODE
51 /* See comment in unknown_presentation_type */
52 if (presentation_type > 32 && presentation_type < 128)
53#endif
54 PyErr_Format(PyExc_ValueError,
55 "Cannot specify ',' with '%c'.",
56 (char)presentation_type);
57#if STRINGLIB_IS_UNICODE
58 else
59 PyErr_Format(PyExc_ValueError,
60 "Cannot specify ',' with '\\x%x'.",
61 (unsigned int)presentation_type);
62#endif
63}
64
Eric Smith8c663262007-08-25 02:26:07 +000065/*
66 get_integer consumes 0 or more decimal digit characters from an
67 input string, updates *result with the corresponding positive
68 integer, and returns the number of digits consumed.
69
70 returns -1 on error.
71*/
72static int
73get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
74 Py_ssize_t *result)
75{
76 Py_ssize_t accumulator, digitval, oldaccumulator;
77 int numdigits;
78 accumulator = numdigits = 0;
79 for (;;(*ptr)++, numdigits++) {
80 if (*ptr >= end)
81 break;
82 digitval = STRINGLIB_TODECIMAL(**ptr);
83 if (digitval < 0)
84 break;
85 /*
86 This trick was copied from old Unicode format code. It's cute,
87 but would really suck on an old machine with a slow divide
88 implementation. Fortunately, in the normal case we do not
89 expect too many digits.
90 */
91 oldaccumulator = accumulator;
92 accumulator *= 10;
93 if ((accumulator+10)/10 != oldaccumulator+1) {
94 PyErr_Format(PyExc_ValueError,
95 "Too many decimal digits in format string");
96 return -1;
97 }
98 accumulator += digitval;
99 }
100 *result = accumulator;
101 return numdigits;
102}
103
104/************************************************************************/
105/*********** standard format specifier parsing **************************/
106/************************************************************************/
107
108/* returns true if this character is a specifier alignment token */
109Py_LOCAL_INLINE(int)
110is_alignment_token(STRINGLIB_CHAR c)
111{
112 switch (c) {
113 case '<': case '>': case '=': case '^':
114 return 1;
115 default:
116 return 0;
117 }
118}
119
120/* returns true if this character is a sign element */
121Py_LOCAL_INLINE(int)
122is_sign_element(STRINGLIB_CHAR c)
123{
124 switch (c) {
Eric Smithb7f5ba12007-08-29 12:38:45 +0000125 case ' ': case '+': case '-':
Eric Smith8c663262007-08-25 02:26:07 +0000126 return 1;
127 default:
128 return 0;
129 }
130}
131
132
133typedef struct {
134 STRINGLIB_CHAR fill_char;
135 STRINGLIB_CHAR align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000136 int alternate;
Eric Smith8c663262007-08-25 02:26:07 +0000137 STRINGLIB_CHAR sign;
138 Py_ssize_t width;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000139 int thousands_separators;
Eric Smith8c663262007-08-25 02:26:07 +0000140 Py_ssize_t precision;
141 STRINGLIB_CHAR type;
142} InternalFormatSpec;
143
Eric Smith4e260c52010-02-22 18:54:44 +0000144
145#if 0
146/* Occassionally useful for debugging. Should normally be commented out. */
147static void
148DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
149{
150 printf("internal format spec: fill_char %d\n", format->fill_char);
151 printf("internal format spec: align %d\n", format->align);
152 printf("internal format spec: alternate %d\n", format->alternate);
153 printf("internal format spec: sign %d\n", format->sign);
154 printf("internal format spec: width %d\n", format->width);
155 printf("internal format spec: thousands_separators %d\n",
156 format->thousands_separators);
157 printf("internal format spec: precision %d\n", format->precision);
158 printf("internal format spec: type %c\n", format->type);
159 printf("\n");
160}
161#endif
162
163
Eric Smith8c663262007-08-25 02:26:07 +0000164/*
165 ptr points to the start of the format_spec, end points just past its end.
166 fills in format with the parsed information.
167 returns 1 on success, 0 on failure.
168 if failure, sets the exception
169*/
170static int
Eric Smith4a7d76d2008-05-30 18:10:19 +0000171parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000172 Py_ssize_t format_spec_len,
Eric Smith8c663262007-08-25 02:26:07 +0000173 InternalFormatSpec *format,
Eric Smith4e260c52010-02-22 18:54:44 +0000174 char default_type,
175 char default_align)
Eric Smith8c663262007-08-25 02:26:07 +0000176{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000177 STRINGLIB_CHAR *ptr = format_spec;
178 STRINGLIB_CHAR *end = format_spec + format_spec_len;
Eric Smith8c663262007-08-25 02:26:07 +0000179
180 /* end-ptr is used throughout this code to specify the length of
181 the input string */
182
Eric Smith0923d1d2009-04-16 20:16:10 +0000183 Py_ssize_t consumed;
Eric Smith8c663262007-08-25 02:26:07 +0000184
185 format->fill_char = '\0';
Eric Smith4e260c52010-02-22 18:54:44 +0000186 format->align = default_align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000187 format->alternate = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000188 format->sign = '\0';
189 format->width = -1;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000190 format->thousands_separators = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000191 format->precision = -1;
192 format->type = default_type;
193
194 /* If the second char is an alignment token,
195 then parse the fill char */
196 if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
197 format->align = ptr[1];
198 format->fill_char = ptr[0];
199 ptr += 2;
Eric Smith0cb431c2007-08-28 01:07:27 +0000200 }
201 else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
Eric Smith8c663262007-08-25 02:26:07 +0000202 format->align = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000203 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000204 }
205
206 /* Parse the various sign options */
207 if (end-ptr >= 1 && is_sign_element(ptr[0])) {
208 format->sign = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000209 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000210 }
211
Eric Smithd68af8f2008-07-16 00:15:35 +0000212 /* If the next character is #, we're in alternate mode. This only
213 applies to integers. */
214 if (end-ptr >= 1 && ptr[0] == '#') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000215 format->alternate = 1;
216 ++ptr;
Eric Smithd68af8f2008-07-16 00:15:35 +0000217 }
218
Eric Smith8c663262007-08-25 02:26:07 +0000219 /* The special case for 0-padding (backwards compat) */
Eric Smith185e30c2007-08-30 22:23:08 +0000220 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
Eric Smith8c663262007-08-25 02:26:07 +0000221 format->fill_char = '0';
222 if (format->align == '\0') {
223 format->align = '=';
224 }
Christian Heimesc3f30c42008-02-22 16:37:40 +0000225 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000226 }
227
Eric Smith0923d1d2009-04-16 20:16:10 +0000228 consumed = get_integer(&ptr, end, &format->width);
229 if (consumed == -1)
230 /* Overflow error. Exception already set. */
231 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000232
Eric Smith0923d1d2009-04-16 20:16:10 +0000233 /* If consumed is 0, we didn't consume any characters for the
234 width. In that case, reset the width to -1, because
235 get_integer() will have set it to zero. -1 is how we record
236 that the width wasn't specified. */
237 if (consumed == 0)
Eric Smith8c663262007-08-25 02:26:07 +0000238 format->width = -1;
Eric Smith8c663262007-08-25 02:26:07 +0000239
Eric Smitha3b1ac82009-04-03 14:45:06 +0000240 /* Comma signifies add thousands separators */
241 if (end-ptr && ptr[0] == ',') {
242 format->thousands_separators = 1;
243 ++ptr;
244 }
245
Eric Smith8c663262007-08-25 02:26:07 +0000246 /* Parse field precision */
247 if (end-ptr && ptr[0] == '.') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000248 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000249
Eric Smith0923d1d2009-04-16 20:16:10 +0000250 consumed = get_integer(&ptr, end, &format->precision);
251 if (consumed == -1)
252 /* Overflow error. Exception already set. */
253 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000254
Eric Smith0923d1d2009-04-16 20:16:10 +0000255 /* Not having a precision after a dot is an error. */
256 if (consumed == 0) {
Eric Smith8c663262007-08-25 02:26:07 +0000257 PyErr_Format(PyExc_ValueError,
258 "Format specifier missing precision");
259 return 0;
260 }
261
262 }
263
Eric Smith0923d1d2009-04-16 20:16:10 +0000264 /* Finally, parse the type field. */
Eric Smith8c663262007-08-25 02:26:07 +0000265
266 if (end-ptr > 1) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000267 /* More than one char remain, invalid conversion spec. */
Eric Smith8c663262007-08-25 02:26:07 +0000268 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
269 return 0;
270 }
271
272 if (end-ptr == 1) {
273 format->type = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000274 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000275 }
276
Eric Smith0923d1d2009-04-16 20:16:10 +0000277 /* Do as much validating as we can, just by looking at the format
278 specifier. Do not take into account what type of formatting
279 we're doing (int, float, string). */
280
281 if (format->thousands_separators) {
282 switch (format->type) {
283 case 'd':
284 case 'e':
285 case 'f':
286 case 'g':
287 case 'E':
288 case 'G':
289 case '%':
290 case 'F':
Eric Smith937491d2009-04-22 17:04:27 +0000291 case '\0':
Eric Smith0923d1d2009-04-16 20:16:10 +0000292 /* These are allowed. See PEP 378.*/
293 break;
294 default:
Eric Smithbeddd702009-07-30 13:43:08 +0000295 invalid_comma_type(format->type);
Eric Smith0923d1d2009-04-16 20:16:10 +0000296 return 0;
297 }
Eric Smitha3b1ac82009-04-03 14:45:06 +0000298 }
299
Eric Smith8c663262007-08-25 02:26:07 +0000300 return 1;
301}
302
Eric Smith58a42242009-04-30 01:00:33 +0000303/* Calculate the padding needed. */
304static void
305calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
306 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
307 Py_ssize_t *n_total)
308{
309 if (width >= 0) {
310 if (nchars > width)
311 *n_total = nchars;
312 else
313 *n_total = width;
314 }
315 else {
316 /* not specified, use all of the chars and no more */
317 *n_total = nchars;
318 }
319
Eric Smith4e260c52010-02-22 18:54:44 +0000320 /* Figure out how much leading space we need, based on the
Eric Smith58a42242009-04-30 01:00:33 +0000321 aligning */
322 if (align == '>')
323 *n_lpadding = *n_total - nchars;
324 else if (align == '^')
325 *n_lpadding = (*n_total - nchars) / 2;
Eric Smith4e260c52010-02-22 18:54:44 +0000326 else if (align == '<' || align == '=')
Eric Smith58a42242009-04-30 01:00:33 +0000327 *n_lpadding = 0;
Eric Smith4e260c52010-02-22 18:54:44 +0000328 else {
329 /* We should never have an unspecified alignment. */
330 *n_lpadding = 0;
331 assert(0);
332 }
Eric Smith58a42242009-04-30 01:00:33 +0000333
334 *n_rpadding = *n_total - nchars - *n_lpadding;
335}
336
337/* Do the padding, and return a pointer to where the caller-supplied
338 content goes. */
339static STRINGLIB_CHAR *
340fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
341 Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
342{
343 /* Pad on left. */
344 if (n_lpadding)
345 STRINGLIB_FILL(p, fill_char, n_lpadding);
346
347 /* Pad on right. */
348 if (n_rpadding)
349 STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
350
351 /* Pointer to the user content. */
352 return p + n_lpadding;
353}
354
355#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
Eric Smith8c663262007-08-25 02:26:07 +0000356/************************************************************************/
357/*********** common routines for numeric formatting *********************/
358/************************************************************************/
359
Eric Smith0923d1d2009-04-16 20:16:10 +0000360/* Locale type codes. */
361#define LT_CURRENT_LOCALE 0
362#define LT_DEFAULT_LOCALE 1
363#define LT_NO_LOCALE 2
364
365/* Locale info needed for formatting integers and the part of floats
366 before and including the decimal. Note that locales only support
367 8-bit chars, not unicode. */
368typedef struct {
369 char *decimal_point;
370 char *thousands_sep;
371 char *grouping;
372} LocaleInfo;
373
Eric Smith8c663262007-08-25 02:26:07 +0000374/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000375 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000376typedef struct {
377 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000378 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000379 Py_ssize_t n_spadding;
380 Py_ssize_t n_rpadding;
Eric Smith0923d1d2009-04-16 20:16:10 +0000381 char sign;
382 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
383 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
384 any grouping chars. */
385 Py_ssize_t n_decimal; /* 0 if only an integer */
386 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
387 excluding the decimal itself, if
388 present. */
389
390 /* These 2 are not the widths of fields, but are needed by
391 STRINGLIB_GROUPING. */
392 Py_ssize_t n_digits; /* The number of digits before a decimal
393 or exponent. */
394 Py_ssize_t n_min_width; /* The min_width we used when we computed
395 the n_grouped_digits width. */
Eric Smith8c663262007-08-25 02:26:07 +0000396} NumberFieldWidths;
397
Eric Smith58a42242009-04-30 01:00:33 +0000398
Eric Smith0923d1d2009-04-16 20:16:10 +0000399/* Given a number of the form:
400 digits[remainder]
401 where ptr points to the start and end points to the end, find where
402 the integer part ends. This could be a decimal, an exponent, both,
403 or neither.
404 If a decimal point is present, set *has_decimal and increment
405 remainder beyond it.
406 Results are undefined (but shouldn't crash) for improperly
407 formatted strings.
408*/
409static void
410parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
411 Py_ssize_t *n_remainder, int *has_decimal)
412{
413 STRINGLIB_CHAR *end = ptr + len;
414 STRINGLIB_CHAR *remainder;
415
416 while (ptr<end && isdigit(*ptr))
417 ++ptr;
418 remainder = ptr;
419
420 /* Does remainder start with a decimal point? */
421 *has_decimal = ptr<end && *remainder == '.';
422
423 /* Skip the decimal point. */
424 if (*has_decimal)
425 remainder++;
426
427 *n_remainder = end - remainder;
428}
429
Eric Smith8c663262007-08-25 02:26:07 +0000430/* not all fields of format are used. for example, precision is
431 unused. should this take discrete params in order to be more clear
432 about what it does? or is passing a single format parameter easier
433 and more efficient enough to justify a little obfuscation? */
Eric Smith0923d1d2009-04-16 20:16:10 +0000434static Py_ssize_t
435calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
436 STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
437 Py_ssize_t n_number, Py_ssize_t n_remainder,
438 int has_decimal, const LocaleInfo *locale,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000439 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000440{
Eric Smith0923d1d2009-04-16 20:16:10 +0000441 Py_ssize_t n_non_digit_non_padding;
442 Py_ssize_t n_padding;
443
444 spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
Eric Smith05212a12008-07-16 19:41:14 +0000445 spec->n_lpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000446 spec->n_prefix = n_prefix;
447 spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
448 spec->n_remainder = n_remainder;
Eric Smith05212a12008-07-16 19:41:14 +0000449 spec->n_spadding = 0;
450 spec->n_rpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000451 spec->sign = '\0';
452 spec->n_sign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000453
454 /* the output will look like:
Eric Smith0923d1d2009-04-16 20:16:10 +0000455 | |
456 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
457 | |
Eric Smith8c663262007-08-25 02:26:07 +0000458
Eric Smith0923d1d2009-04-16 20:16:10 +0000459 sign is computed from format->sign and the actual
Eric Smith8c663262007-08-25 02:26:07 +0000460 sign of the number
461
Eric Smithb1ebcc62008-07-15 13:02:41 +0000462 prefix is given (it's for the '0x' prefix)
463
Eric Smith8c663262007-08-25 02:26:07 +0000464 digits is already known
465
466 the total width is either given, or computed from the
467 actual digits
468
469 only one of lpadding, spadding, and rpadding can be non-zero,
470 and it's calculated from the width and other fields
471 */
472
473 /* compute the various parts we're going to write */
Eric Smith0923d1d2009-04-16 20:16:10 +0000474 switch (format->sign) {
475 case '+':
Eric Smith8c663262007-08-25 02:26:07 +0000476 /* always put a + or - */
Eric Smith0923d1d2009-04-16 20:16:10 +0000477 spec->n_sign = 1;
478 spec->sign = (sign_char == '-' ? '-' : '+');
479 break;
480 case ' ':
481 spec->n_sign = 1;
482 spec->sign = (sign_char == '-' ? '-' : ' ');
483 break;
484 default:
485 /* Not specified, or the default (-) */
486 if (sign_char == '-') {
487 spec->n_sign = 1;
488 spec->sign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000489 }
490 }
491
Eric Smith0923d1d2009-04-16 20:16:10 +0000492 /* The number of chars used for non-digits and non-padding. */
493 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
494 spec->n_remainder;
Eric Smithd68af8f2008-07-16 00:15:35 +0000495
Eric Smith0923d1d2009-04-16 20:16:10 +0000496 /* min_width can go negative, that's okay. format->width == -1 means
497 we don't care. */
498 if (format->fill_char == '0')
499 spec->n_min_width = format->width - n_non_digit_non_padding;
500 else
501 spec->n_min_width = 0;
502
503 if (spec->n_digits == 0)
504 /* This case only occurs when using 'c' formatting, we need
505 to special case it because the grouping code always wants
506 to have at least one character. */
507 spec->n_grouped_digits = 0;
508 else
509 spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
510 spec->n_digits,
511 spec->n_min_width,
512 locale->grouping,
513 locale->thousands_sep);
514
515 /* Given the desired width and the total of digit and non-digit
516 space we consume, see if we need any padding. format->width can
517 be negative (meaning no padding), but this code still works in
518 that case. */
519 n_padding = format->width -
520 (n_non_digit_non_padding + spec->n_grouped_digits);
521 if (n_padding > 0) {
522 /* Some padding is needed. Determine if it's left, space, or right. */
523 switch (format->align) {
524 case '<':
525 spec->n_rpadding = n_padding;
526 break;
527 case '^':
528 spec->n_lpadding = n_padding / 2;
529 spec->n_rpadding = n_padding - spec->n_lpadding;
530 break;
531 case '=':
532 spec->n_spadding = n_padding;
533 break;
Eric Smith4e260c52010-02-22 18:54:44 +0000534 case '>':
Eric Smith0923d1d2009-04-16 20:16:10 +0000535 spec->n_lpadding = n_padding;
536 break;
Eric Smith4e260c52010-02-22 18:54:44 +0000537 default:
538 /* Shouldn't get here, but treat it as '>' */
539 spec->n_lpadding = n_padding;
540 assert(0);
541 break;
Eric Smith8c663262007-08-25 02:26:07 +0000542 }
543 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000544 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;
Eric Smith8c663262007-08-25 02:26:07 +0000547}
548
Eric Smith0923d1d2009-04-16 20:16:10 +0000549/* Fill in the digit parts of a numbers's string representation,
550 as determined in calc_number_widths().
551 No error checking, since we know the buffer is the correct size. */
552static void
553fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
554 STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
555 STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
556 LocaleInfo *locale, int toupper)
Eric Smith8c663262007-08-25 02:26:07 +0000557{
Eric Smith0923d1d2009-04-16 20:16:10 +0000558 /* Used to keep track of digits, decimal, and remainder. */
559 STRINGLIB_CHAR *p = digits;
560
561#ifndef NDEBUG
562 Py_ssize_t r;
563#endif
Eric Smith8c663262007-08-25 02:26:07 +0000564
565 if (spec->n_lpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000566 STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
567 buf += spec->n_lpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000568 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000569 if (spec->n_sign == 1) {
570 *buf++ = spec->sign;
Eric Smith8c663262007-08-25 02:26:07 +0000571 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000572 if (spec->n_prefix) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000573 memmove(buf,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000574 prefix,
575 spec->n_prefix * sizeof(STRINGLIB_CHAR));
Eric Smith0923d1d2009-04-16 20:16:10 +0000576 if (toupper) {
577 Py_ssize_t t;
578 for (t = 0; t < spec->n_prefix; ++t)
579 buf[t] = STRINGLIB_TOUPPER(buf[t]);
580 }
581 buf += spec->n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000582 }
Eric Smith8c663262007-08-25 02:26:07 +0000583 if (spec->n_spadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000584 STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
585 buf += spec->n_spadding;
Eric Smith8c663262007-08-25 02:26:07 +0000586 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000587
588 /* Only for type 'c' special case, it has no digits. */
589 if (spec->n_digits != 0) {
590 /* Fill the digits with InsertThousandsGrouping. */
591#ifndef NDEBUG
592 r =
593#endif
594 STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
595 spec->n_digits, spec->n_min_width,
596 locale->grouping, locale->thousands_sep);
597#ifndef NDEBUG
598 assert(r == spec->n_grouped_digits);
599#endif
600 p += spec->n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000601 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000602 if (toupper) {
603 Py_ssize_t t;
604 for (t = 0; t < spec->n_grouped_digits; ++t)
605 buf[t] = STRINGLIB_TOUPPER(buf[t]);
606 }
607 buf += spec->n_grouped_digits;
608
609 if (spec->n_decimal) {
610 Py_ssize_t t;
611 for (t = 0; t < spec->n_decimal; ++t)
612 buf[t] = locale->decimal_point[t];
613 buf += spec->n_decimal;
614 p += 1;
615 }
616
617 if (spec->n_remainder) {
618 memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
619 buf += spec->n_remainder;
620 p += spec->n_remainder;
621 }
622
Eric Smith8c663262007-08-25 02:26:07 +0000623 if (spec->n_rpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000624 STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
625 buf += spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000626 }
Eric Smith8c663262007-08-25 02:26:07 +0000627}
Eric Smith0923d1d2009-04-16 20:16:10 +0000628
629static char no_grouping[1] = {CHAR_MAX};
630
631/* Find the decimal point character(s?), thousands_separator(s?), and
632 grouping description, either for the current locale if type is
633 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
634 none if LT_NO_LOCALE. */
635static void
636get_locale_info(int type, LocaleInfo *locale_info)
637{
638 switch (type) {
639 case LT_CURRENT_LOCALE: {
640 struct lconv *locale_data = localeconv();
641 locale_info->decimal_point = locale_data->decimal_point;
642 locale_info->thousands_sep = locale_data->thousands_sep;
643 locale_info->grouping = locale_data->grouping;
644 break;
645 }
646 case LT_DEFAULT_LOCALE:
647 locale_info->decimal_point = ".";
648 locale_info->thousands_sep = ",";
649 locale_info->grouping = "\3"; /* Group every 3 characters,
650 trailing 0 means repeat
651 infinitely. */
652 break;
653 case LT_NO_LOCALE:
654 locale_info->decimal_point = ".";
655 locale_info->thousands_sep = "";
656 locale_info->grouping = no_grouping;
657 break;
658 default:
659 assert(0);
660 }
661}
662
Eric Smith58a42242009-04-30 01:00:33 +0000663#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
Eric Smith8c663262007-08-25 02:26:07 +0000664
665/************************************************************************/
666/*********** string formatting ******************************************/
667/************************************************************************/
668
669static PyObject *
670format_string_internal(PyObject *value, const InternalFormatSpec *format)
671{
Eric Smith8c663262007-08-25 02:26:07 +0000672 Py_ssize_t lpad;
Eric Smith58a42242009-04-30 01:00:33 +0000673 Py_ssize_t rpad;
674 Py_ssize_t total;
675 STRINGLIB_CHAR *p;
Eric Smith8c663262007-08-25 02:26:07 +0000676 Py_ssize_t len = STRINGLIB_LEN(value);
677 PyObject *result = NULL;
678
679 /* sign is not allowed on strings */
680 if (format->sign != '\0') {
681 PyErr_SetString(PyExc_ValueError,
682 "Sign not allowed in string format specifier");
683 goto done;
684 }
685
Eric Smithb1ebcc62008-07-15 13:02:41 +0000686 /* alternate is not allowed on strings */
687 if (format->alternate) {
688 PyErr_SetString(PyExc_ValueError,
689 "Alternate form (#) not allowed in string format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000690 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000691 goto done;
692 }
693
Eric Smith8c663262007-08-25 02:26:07 +0000694 /* '=' alignment not allowed on strings */
695 if (format->align == '=') {
696 PyErr_SetString(PyExc_ValueError,
697 "'=' alignment not allowed "
698 "in string format specifier");
699 goto done;
700 }
701
702 /* if precision is specified, output no more that format.precision
703 characters */
704 if (format->precision >= 0 && len >= format->precision) {
705 len = format->precision;
706 }
707
Eric Smith58a42242009-04-30 01:00:33 +0000708 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
Eric Smith8c663262007-08-25 02:26:07 +0000709
710 /* allocate the resulting string */
Eric Smith58a42242009-04-30 01:00:33 +0000711 result = STRINGLIB_NEW(NULL, total);
Eric Smith8c663262007-08-25 02:26:07 +0000712 if (result == NULL)
713 goto done;
714
Eric Smith58a42242009-04-30 01:00:33 +0000715 /* Write into that space. First the padding. */
716 p = fill_padding(STRINGLIB_STR(result), len,
717 format->fill_char=='\0'?' ':format->fill_char,
718 lpad, rpad);
Eric Smith8c663262007-08-25 02:26:07 +0000719
Eric Smith58a42242009-04-30 01:00:33 +0000720 /* Then the source string. */
721 memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
Eric Smith8c663262007-08-25 02:26:07 +0000722
723done:
724 return result;
725}
726
727
728/************************************************************************/
729/*********** long formatting ********************************************/
730/************************************************************************/
731
Eric Smith8fd3eba2008-02-17 19:48:00 +0000732#if defined FORMAT_LONG || defined FORMAT_INT
733typedef PyObject*
734(*IntOrLongToString)(PyObject *value, int base);
735
Eric Smith8c663262007-08-25 02:26:07 +0000736static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000737format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000738 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000739{
740 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000741 PyObject *tmp = NULL;
742 STRINGLIB_CHAR *pnumeric_chars;
743 STRINGLIB_CHAR numeric_char;
Eric Smith0923d1d2009-04-16 20:16:10 +0000744 STRINGLIB_CHAR sign_char = '\0';
Eric Smith8c663262007-08-25 02:26:07 +0000745 Py_ssize_t n_digits; /* count of digits need from the computed
746 string */
Eric Smith0923d1d2009-04-16 20:16:10 +0000747 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
748 produces non-digits */
Eric Smithd68af8f2008-07-16 00:15:35 +0000749 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
Eric Smith0923d1d2009-04-16 20:16:10 +0000750 Py_ssize_t n_total;
Eric Smithd68af8f2008-07-16 00:15:35 +0000751 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000752 NumberFieldWidths spec;
753 long x;
754
Eric Smith0923d1d2009-04-16 20:16:10 +0000755 /* Locale settings, either from the actual locale or
756 from a hard-code pseudo-locale */
757 LocaleInfo locale;
758
Eric Smith8c663262007-08-25 02:26:07 +0000759 /* no precision allowed on integers */
760 if (format->precision != -1) {
761 PyErr_SetString(PyExc_ValueError,
762 "Precision not allowed in integer format specifier");
763 goto done;
764 }
765
Eric Smith8c663262007-08-25 02:26:07 +0000766 /* special case for character formatting */
767 if (format->type == 'c') {
768 /* error to specify a sign */
769 if (format->sign != '\0') {
770 PyErr_SetString(PyExc_ValueError,
771 "Sign not allowed with integer"
772 " format specifier 'c'");
773 goto done;
774 }
775
Eric Smith0923d1d2009-04-16 20:16:10 +0000776 /* Error to specify a comma. */
777 if (format->thousands_separators) {
778 PyErr_SetString(PyExc_ValueError,
779 "Thousands separators not allowed with integer"
780 " format specifier 'c'");
781 goto done;
782 }
783
Eric Smith8c663262007-08-25 02:26:07 +0000784 /* taken from unicodeobject.c formatchar() */
785 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000786/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000787 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000788 if (x == -1 && PyErr_Occurred())
789 goto done;
790#ifdef Py_UNICODE_WIDE
791 if (x < 0 || x > 0x10ffff) {
792 PyErr_SetString(PyExc_OverflowError,
793 "%c arg not in range(0x110000) "
794 "(wide Python build)");
795 goto done;
796 }
797#else
798 if (x < 0 || x > 0xffff) {
799 PyErr_SetString(PyExc_OverflowError,
800 "%c arg not in range(0x10000) "
801 "(narrow Python build)");
802 goto done;
803 }
804#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000805 numeric_char = (STRINGLIB_CHAR)x;
806 pnumeric_chars = &numeric_char;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000807 n_digits = 1;
Eric Smith0923d1d2009-04-16 20:16:10 +0000808
809 /* As a sort-of hack, we tell calc_number_widths that we only
810 have "remainder" characters. calc_number_widths thinks
811 these are characters that don't get formatted, only copied
812 into the output string. We do this for 'c' formatting,
813 because the characters are likely to be non-digits. */
814 n_remainder = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000815 }
816 else {
Eric Smith8c663262007-08-25 02:26:07 +0000817 int base;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000818 int leading_chars_to_skip = 0; /* Number of characters added by
819 PyNumber_ToBase that we want to
820 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000821
822 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000823 PyNumber_ToBase */
824 switch (format->type) {
825 case 'b':
826 base = 2;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000827 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000828 break;
829 case 'o':
830 base = 8;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000831 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000832 break;
833 case 'x':
834 case 'X':
835 base = 16;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000836 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000837 break;
838 default: /* shouldn't be needed, but stops a compiler warning */
839 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000840 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000841 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000842 break;
843 }
844
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000845 /* The number of prefix chars is the same as the leading
846 chars to skip */
847 if (format->alternate)
848 n_prefix = leading_chars_to_skip;
Eric Smithd68af8f2008-07-16 00:15:35 +0000849
Eric Smith8fd3eba2008-02-17 19:48:00 +0000850 /* Do the hard part, converting to a string in a given base */
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000851 tmp = tostring(value, base);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000852 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000853 goto done;
854
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000855 pnumeric_chars = STRINGLIB_STR(tmp);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000856 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000857
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000858 prefix = pnumeric_chars;
Eric Smithd68af8f2008-07-16 00:15:35 +0000859
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000860 /* Remember not to modify what pnumeric_chars points to. it
861 might be interned. Only modify it after we copy it into a
862 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000863
Eric Smith8fd3eba2008-02-17 19:48:00 +0000864 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000865 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000866 if (pnumeric_chars[0] == '-') {
867 sign_char = pnumeric_chars[0];
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000868 ++prefix;
869 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000870 }
871
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000872 /* Skip over the leading chars (0x, 0b, etc.) */
873 n_digits -= leading_chars_to_skip;
874 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000875 }
876
Eric Smith0923d1d2009-04-16 20:16:10 +0000877 /* Determine the grouping, separator, and decimal point, if any. */
878 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
879 (format->thousands_separators ?
880 LT_DEFAULT_LOCALE :
881 LT_NO_LOCALE),
882 &locale);
Eric Smith5807c412008-05-11 21:00:57 +0000883
Eric Smith0923d1d2009-04-16 20:16:10 +0000884 /* Calculate how much memory we'll need. */
885 n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
886 n_digits, n_remainder, 0, &locale, format);
Eric Smithb151a452008-06-24 11:21:04 +0000887
Eric Smith0923d1d2009-04-16 20:16:10 +0000888 /* Allocate the memory. */
889 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000890 if (!result)
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000891 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000892
Eric Smith0923d1d2009-04-16 20:16:10 +0000893 /* Populate the memory. */
894 fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
895 prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
896 &locale, format->type == 'X');
Eric Smithd68af8f2008-07-16 00:15:35 +0000897
Eric Smith8c663262007-08-25 02:26:07 +0000898done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000899 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000900 return result;
901}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000902#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000903
904/************************************************************************/
905/*********** float formatting *******************************************/
906/************************************************************************/
907
Eric Smith8fd3eba2008-02-17 19:48:00 +0000908#ifdef FORMAT_FLOAT
909#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000910static void
911strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
Eric Smith8c663262007-08-25 02:26:07 +0000912{
Eric Smith0923d1d2009-04-16 20:16:10 +0000913 Py_ssize_t i;
914 for (i = 0; i < len; ++i)
915 buffer[i] = (Py_UNICODE)charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000916}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000917#endif
Eric Smith8c663262007-08-25 02:26:07 +0000918
Eric Smith8c663262007-08-25 02:26:07 +0000919/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000920static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000921format_float_internal(PyObject *value,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000922 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000923{
Eric Smith0923d1d2009-04-16 20:16:10 +0000924 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
Eric Smith8c663262007-08-25 02:26:07 +0000925 Py_ssize_t n_digits;
Eric Smith0923d1d2009-04-16 20:16:10 +0000926 Py_ssize_t n_remainder;
927 Py_ssize_t n_total;
928 int has_decimal;
929 double val;
Eric Smith8c663262007-08-25 02:26:07 +0000930 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +0000931 Py_ssize_t default_precision = 6;
Eric Smith0923d1d2009-04-16 20:16:10 +0000932 STRINGLIB_CHAR type = format->type;
933 int add_pct = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000934 STRINGLIB_CHAR *p;
935 NumberFieldWidths spec;
Eric Smith0923d1d2009-04-16 20:16:10 +0000936 int flags = 0;
937 PyObject *result = NULL;
938 STRINGLIB_CHAR sign_char = '\0';
939 int float_type; /* Used to see if we have a nan, inf, or regular float. */
Eric Smith8c663262007-08-25 02:26:07 +0000940
941#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000942 Py_UNICODE *unicode_tmp = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000943#endif
944
Eric Smith0923d1d2009-04-16 20:16:10 +0000945 /* Locale settings, either from the actual locale or
946 from a hard-code pseudo-locale */
947 LocaleInfo locale;
948
949 /* Alternate is not allowed on floats. */
Eric Smithb1ebcc62008-07-15 13:02:41 +0000950 if (format->alternate) {
951 PyErr_SetString(PyExc_ValueError,
952 "Alternate form (#) not allowed in float format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000953 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000954 goto done;
955 }
956
Eric Smith0923d1d2009-04-16 20:16:10 +0000957 if (type == '\0') {
Eric Smith63376222009-05-05 14:04:18 +0000958 /* Omitted type specifier. This is like 'g' but with at least one
959 digit after the decimal point, and different default precision.*/
Eric Smith0923d1d2009-04-16 20:16:10 +0000960 type = 'g';
Eric Smith63376222009-05-05 14:04:18 +0000961 default_precision = PyFloat_STR_PRECISION;
Eric Smith0923d1d2009-04-16 20:16:10 +0000962 flags |= Py_DTSF_ADD_DOT_0;
963 }
964
965 if (type == 'n')
966 /* 'n' is the same as 'g', except for the locale used to
967 format the result. We take care of that later. */
968 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000969
Eric Smith0923d1d2009-04-16 20:16:10 +0000970 val = PyFloat_AsDouble(value);
971 if (val == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000972 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000973
974 if (type == '%') {
975 type = 'f';
Eric Smith0923d1d2009-04-16 20:16:10 +0000976 val *= 100;
977 add_pct = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000978 }
979
980 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +0000981 precision = default_precision;
Eric Smith8c663262007-08-25 02:26:07 +0000982
Eric Smith0923d1d2009-04-16 20:16:10 +0000983 /* Cast "type", because if we're in unicode we need to pass a
984 8-bit char. This is safe, because we've restricted what "type"
985 can be. */
986 buf = PyOS_double_to_string(val, (char)type, precision, flags,
987 &float_type);
988 if (buf == NULL)
989 goto done;
990 n_digits = strlen(buf);
Eric Smith8c663262007-08-25 02:26:07 +0000991
Eric Smith0923d1d2009-04-16 20:16:10 +0000992 if (add_pct) {
993 /* We know that buf has a trailing zero (since we just called
994 strlen() on it), and we don't use that fact any more. So we
995 can just write over the trailing zero. */
996 buf[n_digits] = '%';
997 n_digits += 1;
998 }
Eric Smith8c663262007-08-25 02:26:07 +0000999
Eric Smith0923d1d2009-04-16 20:16:10 +00001000 /* Since there is no unicode version of PyOS_double_to_string,
1001 just use the 8 bit version and then convert to unicode. */
Eric Smith8c663262007-08-25 02:26:07 +00001002#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +00001003 unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
1004 if (unicode_tmp == NULL) {
1005 PyErr_NoMemory();
1006 goto done;
1007 }
1008 strtounicode(unicode_tmp, buf, n_digits);
1009 p = unicode_tmp;
Eric Smith8c663262007-08-25 02:26:07 +00001010#else
Eric Smith0923d1d2009-04-16 20:16:10 +00001011 p = buf;
Eric Smith8c663262007-08-25 02:26:07 +00001012#endif
1013
Eric Smith0923d1d2009-04-16 20:16:10 +00001014 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +00001015 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +00001016 if (*p == '-') {
1017 sign_char = *p;
Christian Heimesc3f30c42008-02-22 16:37:40 +00001018 ++p;
1019 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +00001020 }
1021
Eric Smith0923d1d2009-04-16 20:16:10 +00001022 /* Determine if we have any "remainder" (after the digits, might include
1023 decimal or exponent or both (or neither)) */
1024 parse_number(p, n_digits, &n_remainder, &has_decimal);
Eric Smith8c663262007-08-25 02:26:07 +00001025
Eric Smith0923d1d2009-04-16 20:16:10 +00001026 /* Determine the grouping, separator, and decimal point, if any. */
1027 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1028 (format->thousands_separators ?
1029 LT_DEFAULT_LOCALE :
1030 LT_NO_LOCALE),
1031 &locale);
1032
1033 /* Calculate how much memory we'll need. */
1034 n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
1035 n_remainder, has_decimal, &locale, format);
1036
1037 /* Allocate the memory. */
1038 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8c663262007-08-25 02:26:07 +00001039 if (result == NULL)
1040 goto done;
1041
Eric Smith0923d1d2009-04-16 20:16:10 +00001042 /* Populate the memory. */
1043 fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
1044 format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
1045 0);
Eric Smith8c663262007-08-25 02:26:07 +00001046
1047done:
Eric Smith0923d1d2009-04-16 20:16:10 +00001048 PyMem_Free(buf);
1049#if STRINGLIB_IS_UNICODE
1050 PyMem_Free(unicode_tmp);
1051#endif
Eric Smith8c663262007-08-25 02:26:07 +00001052 return result;
1053}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001054#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +00001055
1056/************************************************************************/
Eric Smith58a42242009-04-30 01:00:33 +00001057/*********** complex formatting *****************************************/
1058/************************************************************************/
1059
1060#ifdef FORMAT_COMPLEX
1061
1062static PyObject *
1063format_complex_internal(PyObject *value,
1064 const InternalFormatSpec *format)
1065{
1066 double re;
1067 double im;
1068 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1069 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1070
1071 InternalFormatSpec tmp_format = *format;
1072 Py_ssize_t n_re_digits;
1073 Py_ssize_t n_im_digits;
1074 Py_ssize_t n_re_remainder;
1075 Py_ssize_t n_im_remainder;
1076 Py_ssize_t n_re_total;
1077 Py_ssize_t n_im_total;
1078 int re_has_decimal;
1079 int im_has_decimal;
1080 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +00001081 Py_ssize_t default_precision = 6;
Eric Smith58a42242009-04-30 01:00:33 +00001082 STRINGLIB_CHAR type = format->type;
1083 STRINGLIB_CHAR *p_re;
1084 STRINGLIB_CHAR *p_im;
1085 NumberFieldWidths re_spec;
1086 NumberFieldWidths im_spec;
1087 int flags = 0;
1088 PyObject *result = NULL;
1089 STRINGLIB_CHAR *p;
1090 STRINGLIB_CHAR re_sign_char = '\0';
1091 STRINGLIB_CHAR im_sign_char = '\0';
1092 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1093 int im_float_type;
1094 int add_parens = 0;
1095 int skip_re = 0;
1096 Py_ssize_t lpad;
1097 Py_ssize_t rpad;
1098 Py_ssize_t total;
1099
1100#if STRINGLIB_IS_UNICODE
1101 Py_UNICODE *re_unicode_tmp = NULL;
1102 Py_UNICODE *im_unicode_tmp = NULL;
1103#endif
1104
1105 /* Locale settings, either from the actual locale or
1106 from a hard-code pseudo-locale */
1107 LocaleInfo locale;
1108
1109 /* Alternate is not allowed on complex. */
1110 if (format->alternate) {
1111 PyErr_SetString(PyExc_ValueError,
1112 "Alternate form (#) not allowed in complex format "
1113 "specifier");
1114 goto done;
1115 }
1116
1117 /* Neither is zero pading. */
1118 if (format->fill_char == '0') {
1119 PyErr_SetString(PyExc_ValueError,
1120 "Zero padding is not allowed in complex format "
1121 "specifier");
1122 goto done;
1123 }
1124
1125 /* Neither is '=' alignment . */
1126 if (format->align == '=') {
1127 PyErr_SetString(PyExc_ValueError,
1128 "'=' alignment flag is not allowed in complex format "
1129 "specifier");
1130 goto done;
1131 }
1132
1133 re = PyComplex_RealAsDouble(value);
1134 if (re == -1.0 && PyErr_Occurred())
1135 goto done;
1136 im = PyComplex_ImagAsDouble(value);
1137 if (im == -1.0 && PyErr_Occurred())
1138 goto done;
1139
1140 if (type == '\0') {
1141 /* Omitted type specifier. Should be like str(self). */
1142 type = 'g';
Eric Smith63376222009-05-05 14:04:18 +00001143 default_precision = PyFloat_STR_PRECISION;
Eric Smith58a42242009-04-30 01:00:33 +00001144 add_parens = 1;
1145 if (re == 0.0)
1146 skip_re = 1;
1147 }
1148
1149 if (type == 'n')
1150 /* 'n' is the same as 'g', except for the locale used to
1151 format the result. We take care of that later. */
1152 type = 'g';
1153
Eric Smith58a42242009-04-30 01:00:33 +00001154 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +00001155 precision = default_precision;
Eric Smith58a42242009-04-30 01:00:33 +00001156
1157 /* Cast "type", because if we're in unicode we need to pass a
1158 8-bit char. This is safe, because we've restricted what "type"
1159 can be. */
1160 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1161 &re_float_type);
1162 if (re_buf == NULL)
1163 goto done;
1164 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1165 &im_float_type);
1166 if (im_buf == NULL)
1167 goto done;
1168
1169 n_re_digits = strlen(re_buf);
1170 n_im_digits = strlen(im_buf);
1171
1172 /* Since there is no unicode version of PyOS_double_to_string,
1173 just use the 8 bit version and then convert to unicode. */
1174#if STRINGLIB_IS_UNICODE
1175 re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
1176 if (re_unicode_tmp == NULL) {
1177 PyErr_NoMemory();
1178 goto done;
1179 }
1180 strtounicode(re_unicode_tmp, re_buf, n_re_digits);
1181 p_re = re_unicode_tmp;
1182
1183 im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
1184 if (im_unicode_tmp == NULL) {
1185 PyErr_NoMemory();
1186 goto done;
1187 }
1188 strtounicode(im_unicode_tmp, im_buf, n_im_digits);
1189 p_im = im_unicode_tmp;
1190#else
1191 p_re = re_buf;
1192 p_im = im_buf;
1193#endif
1194
1195 /* Is a sign character present in the output? If so, remember it
1196 and skip it */
1197 if (*p_re == '-') {
1198 re_sign_char = *p_re;
1199 ++p_re;
1200 --n_re_digits;
1201 }
1202 if (*p_im == '-') {
1203 im_sign_char = *p_im;
1204 ++p_im;
1205 --n_im_digits;
1206 }
1207
1208 /* Determine if we have any "remainder" (after the digits, might include
1209 decimal or exponent or both (or neither)) */
1210 parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
1211 parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
1212
1213 /* Determine the grouping, separator, and decimal point, if any. */
1214 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1215 (format->thousands_separators ?
1216 LT_DEFAULT_LOCALE :
1217 LT_NO_LOCALE),
1218 &locale);
1219
1220 /* Turn off any padding. We'll do it later after we've composed
1221 the numbers without padding. */
1222 tmp_format.fill_char = '\0';
Eric Smith4e260c52010-02-22 18:54:44 +00001223 tmp_format.align = '<';
Eric Smith58a42242009-04-30 01:00:33 +00001224 tmp_format.width = -1;
1225
1226 /* Calculate how much memory we'll need. */
1227 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
1228 n_re_digits, n_re_remainder,
1229 re_has_decimal, &locale, &tmp_format);
1230
1231 /* Same formatting, but always include a sign. */
1232 tmp_format.sign = '+';
1233 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
1234 n_im_digits, n_im_remainder,
1235 im_has_decimal, &locale, &tmp_format);
1236
1237 if (skip_re)
1238 n_re_total = 0;
1239
1240 /* Add 1 for the 'j', and optionally 2 for parens. */
1241 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1242 format->width, format->align, &lpad, &rpad, &total);
1243
1244 result = STRINGLIB_NEW(NULL, total);
1245 if (result == NULL)
1246 goto done;
1247
1248 /* Populate the memory. First, the padding. */
1249 p = fill_padding(STRINGLIB_STR(result),
1250 n_re_total + n_im_total + 1 + add_parens * 2,
1251 format->fill_char=='\0' ? ' ' : format->fill_char,
1252 lpad, rpad);
1253
1254 if (add_parens)
1255 *p++ = '(';
1256
1257 if (!skip_re) {
1258 fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
1259 p += n_re_total;
1260 }
1261 fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
1262 p += n_im_total;
1263 *p++ = 'j';
1264
1265 if (add_parens)
1266 *p++ = ')';
1267
1268done:
1269 PyMem_Free(re_buf);
1270 PyMem_Free(im_buf);
1271#if STRINGLIB_IS_UNICODE
1272 PyMem_Free(re_unicode_tmp);
1273 PyMem_Free(im_unicode_tmp);
1274#endif
1275 return result;
1276}
1277#endif /* FORMAT_COMPLEX */
1278
1279/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001280/*********** built in formatters ****************************************/
1281/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001282PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001283FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001284 STRINGLIB_CHAR *format_spec,
1285 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001286{
Eric Smith8c663262007-08-25 02:26:07 +00001287 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +00001288 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001289
1290 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001291 it equivalent to str(obj) */
1292 if (format_spec_len == 0) {
1293 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001294 goto done;
1295 }
1296
1297 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001298 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001299 &format, 's', '<'))
Eric Smith8c663262007-08-25 02:26:07 +00001300 goto done;
1301
1302 /* type conversion? */
1303 switch (format.type) {
1304 case 's':
1305 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001306 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001307 break;
Eric Smith8c663262007-08-25 02:26:07 +00001308 default:
1309 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001310 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001311 goto done;
1312 }
1313
1314done:
Eric Smith8c663262007-08-25 02:26:07 +00001315 return result;
1316}
1317
Eric Smith8fd3eba2008-02-17 19:48:00 +00001318#if defined FORMAT_LONG || defined FORMAT_INT
1319static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +00001320format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001321 STRINGLIB_CHAR *format_spec,
1322 Py_ssize_t format_spec_len,
1323 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +00001324{
Eric Smith8c663262007-08-25 02:26:07 +00001325 PyObject *result = NULL;
1326 PyObject *tmp = NULL;
1327 InternalFormatSpec format;
1328
Eric Smith8c663262007-08-25 02:26:07 +00001329 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001330 it equivalent to str(obj) */
1331 if (format_spec_len == 0) {
1332 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001333 goto done;
1334 }
1335
1336 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001337 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001338 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001339 &format, 'd', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001340 goto done;
1341
1342 /* type conversion? */
1343 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +00001344 case 'b':
1345 case 'c':
1346 case 'd':
1347 case 'o':
1348 case 'x':
1349 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +00001350 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +00001351 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001352 the formatting */
1353 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +00001354 break;
1355
Eric Smithfa767ef2008-01-28 10:59:27 +00001356 case 'e':
1357 case 'E':
1358 case 'f':
1359 case 'F':
1360 case 'g':
1361 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +00001362 case '%':
1363 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001364 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +00001365 if (tmp == NULL)
1366 goto done;
Eric Smithf64bce82009-04-13 00:50:23 +00001367 result = format_float_internal(tmp, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +00001368 break;
1369
Eric Smith8c663262007-08-25 02:26:07 +00001370 default:
1371 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001372 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001373 goto done;
1374 }
1375
1376done:
1377 Py_XDECREF(tmp);
1378 return result;
1379}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001380#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +00001381
Eric Smith8fd3eba2008-02-17 19:48:00 +00001382#ifdef FORMAT_LONG
1383/* Need to define long_format as a function that will convert a long
1384 to a string. In 3.0, _PyLong_Format has the correct signature. In
1385 2.x, we need to fudge a few parameters */
1386#if PY_VERSION_HEX >= 0x03000000
1387#define long_format _PyLong_Format
1388#else
1389static PyObject*
1390long_format(PyObject* value, int base)
1391{
1392 /* Convert to base, don't add trailing 'L', and use the new octal
1393 format. We already know this is a long object */
1394 assert(PyLong_Check(value));
1395 /* convert to base, don't add 'L', and use the new octal format */
1396 return _PyLong_Format(value, base, 0, 1);
1397}
1398#endif
1399
1400PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001401FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001402 STRINGLIB_CHAR *format_spec,
1403 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001404{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001405 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001406 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001407}
1408#endif /* FORMAT_LONG */
1409
1410#ifdef FORMAT_INT
1411/* this is only used for 2.x, not 3.0 */
1412static PyObject*
1413int_format(PyObject* value, int base)
1414{
1415 /* Convert to base, and use the new octal format. We already
1416 know this is an int object */
1417 assert(PyInt_Check(value));
1418 return _PyInt_Format((PyIntObject*)value, base, 1);
1419}
1420
1421PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001422FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001423 STRINGLIB_CHAR *format_spec,
1424 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001425{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001426 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001427 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001428}
1429#endif /* FORMAT_INT */
1430
1431#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001432PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001433FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001434 STRINGLIB_CHAR *format_spec,
1435 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001436{
Eric Smith8c663262007-08-25 02:26:07 +00001437 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001438 InternalFormatSpec format;
1439
Eric Smith8c663262007-08-25 02:26:07 +00001440 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001441 it equivalent to str(obj) */
1442 if (format_spec_len == 0) {
1443 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001444 goto done;
1445 }
1446
1447 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001448 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001449 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001450 &format, '\0', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001451 goto done;
1452
1453 /* type conversion? */
1454 switch (format.type) {
Eric Smith0923d1d2009-04-16 20:16:10 +00001455 case '\0': /* No format code: like 'g', but with at least one decimal. */
Eric Smith8c663262007-08-25 02:26:07 +00001456 case 'e':
1457 case 'E':
1458 case 'f':
1459 case 'F':
1460 case 'g':
1461 case 'G':
1462 case 'n':
1463 case '%':
1464 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001465 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001466 break;
1467
1468 default:
1469 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001470 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001471 goto done;
1472 }
1473
1474done:
Eric Smith8c663262007-08-25 02:26:07 +00001475 return result;
1476}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001477#endif /* FORMAT_FLOAT */
Eric Smith58a42242009-04-30 01:00:33 +00001478
1479#ifdef FORMAT_COMPLEX
1480PyObject *
1481FORMAT_COMPLEX(PyObject *obj,
1482 STRINGLIB_CHAR *format_spec,
1483 Py_ssize_t format_spec_len)
1484{
1485 PyObject *result = NULL;
1486 InternalFormatSpec format;
1487
1488 /* check for the special case of zero length format spec, make
1489 it equivalent to str(obj) */
1490 if (format_spec_len == 0) {
1491 result = STRINGLIB_TOSTR(obj);
1492 goto done;
1493 }
1494
1495 /* parse the format_spec */
1496 if (!parse_internal_render_format_spec(format_spec,
1497 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001498 &format, '\0', '>'))
Eric Smith58a42242009-04-30 01:00:33 +00001499 goto done;
1500
1501 /* type conversion? */
1502 switch (format.type) {
1503 case '\0': /* No format code: like 'g', but with at least one decimal. */
1504 case 'e':
1505 case 'E':
1506 case 'f':
1507 case 'F':
1508 case 'g':
1509 case 'G':
1510 case 'n':
1511 /* no conversion, already a complex. do the formatting */
1512 result = format_complex_internal(obj, &format);
1513 break;
1514
1515 default:
1516 /* unknown */
1517 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1518 goto done;
1519 }
1520
1521done:
1522 return result;
1523}
1524#endif /* FORMAT_COMPLEX */