blob: 4fdc62d650dccb4c73ca9cc78080580252c22077 [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);
Eric Smithabb28c62010-02-23 00:22:24 +0000154 printf("internal format spec: width %zd\n", format->width);
Eric Smith4e260c52010-02-22 18:54:44 +0000155 printf("internal format spec: thousands_separators %d\n",
156 format->thousands_separators);
Eric Smithabb28c62010-02-23 00:22:24 +0000157 printf("internal format spec: precision %zd\n", format->precision);
Eric Smith4e260c52010-02-22 18:54:44 +0000158 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 Smithabb28c62010-02-23 00:22:24 +0000184 int align_specified = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000185
186 format->fill_char = '\0';
Eric Smith4e260c52010-02-22 18:54:44 +0000187 format->align = default_align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000188 format->alternate = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000189 format->sign = '\0';
190 format->width = -1;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000191 format->thousands_separators = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000192 format->precision = -1;
193 format->type = default_type;
194
195 /* If the second char is an alignment token,
196 then parse the fill char */
197 if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
198 format->align = ptr[1];
199 format->fill_char = ptr[0];
Eric Smithabb28c62010-02-23 00:22:24 +0000200 align_specified = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000201 ptr += 2;
Eric Smith0cb431c2007-08-28 01:07:27 +0000202 }
203 else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
Eric Smith8c663262007-08-25 02:26:07 +0000204 format->align = ptr[0];
Eric Smithabb28c62010-02-23 00:22:24 +0000205 align_specified = 1;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000206 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000207 }
208
209 /* Parse the various sign options */
210 if (end-ptr >= 1 && is_sign_element(ptr[0])) {
211 format->sign = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000212 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000213 }
214
Eric Smithd68af8f2008-07-16 00:15:35 +0000215 /* If the next character is #, we're in alternate mode. This only
216 applies to integers. */
217 if (end-ptr >= 1 && ptr[0] == '#') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000218 format->alternate = 1;
219 ++ptr;
Eric Smithd68af8f2008-07-16 00:15:35 +0000220 }
221
Eric Smith8c663262007-08-25 02:26:07 +0000222 /* The special case for 0-padding (backwards compat) */
Eric Smith185e30c2007-08-30 22:23:08 +0000223 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
Eric Smith8c663262007-08-25 02:26:07 +0000224 format->fill_char = '0';
Eric Smithabb28c62010-02-23 00:22:24 +0000225 if (!align_specified) {
Eric Smith8c663262007-08-25 02:26:07 +0000226 format->align = '=';
227 }
Christian Heimesc3f30c42008-02-22 16:37:40 +0000228 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000229 }
230
Eric Smith0923d1d2009-04-16 20:16:10 +0000231 consumed = get_integer(&ptr, end, &format->width);
232 if (consumed == -1)
233 /* Overflow error. Exception already set. */
234 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000235
Eric Smith0923d1d2009-04-16 20:16:10 +0000236 /* If consumed is 0, we didn't consume any characters for the
237 width. In that case, reset the width to -1, because
238 get_integer() will have set it to zero. -1 is how we record
239 that the width wasn't specified. */
240 if (consumed == 0)
Eric Smith8c663262007-08-25 02:26:07 +0000241 format->width = -1;
Eric Smith8c663262007-08-25 02:26:07 +0000242
Eric Smitha3b1ac82009-04-03 14:45:06 +0000243 /* Comma signifies add thousands separators */
244 if (end-ptr && ptr[0] == ',') {
245 format->thousands_separators = 1;
246 ++ptr;
247 }
248
Eric Smith8c663262007-08-25 02:26:07 +0000249 /* Parse field precision */
250 if (end-ptr && ptr[0] == '.') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000251 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000252
Eric Smith0923d1d2009-04-16 20:16:10 +0000253 consumed = get_integer(&ptr, end, &format->precision);
254 if (consumed == -1)
255 /* Overflow error. Exception already set. */
256 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000257
Eric Smith0923d1d2009-04-16 20:16:10 +0000258 /* Not having a precision after a dot is an error. */
259 if (consumed == 0) {
Eric Smith8c663262007-08-25 02:26:07 +0000260 PyErr_Format(PyExc_ValueError,
261 "Format specifier missing precision");
262 return 0;
263 }
264
265 }
266
Eric Smith0923d1d2009-04-16 20:16:10 +0000267 /* Finally, parse the type field. */
Eric Smith8c663262007-08-25 02:26:07 +0000268
269 if (end-ptr > 1) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000270 /* More than one char remain, invalid conversion spec. */
Eric Smith8c663262007-08-25 02:26:07 +0000271 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
272 return 0;
273 }
274
275 if (end-ptr == 1) {
276 format->type = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000277 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000278 }
279
Eric Smith0923d1d2009-04-16 20:16:10 +0000280 /* Do as much validating as we can, just by looking at the format
281 specifier. Do not take into account what type of formatting
282 we're doing (int, float, string). */
283
284 if (format->thousands_separators) {
285 switch (format->type) {
286 case 'd':
287 case 'e':
288 case 'f':
289 case 'g':
290 case 'E':
291 case 'G':
292 case '%':
293 case 'F':
Eric Smith937491d2009-04-22 17:04:27 +0000294 case '\0':
Eric Smith0923d1d2009-04-16 20:16:10 +0000295 /* These are allowed. See PEP 378.*/
296 break;
297 default:
Eric Smithbeddd702009-07-30 13:43:08 +0000298 invalid_comma_type(format->type);
Eric Smith0923d1d2009-04-16 20:16:10 +0000299 return 0;
300 }
Eric Smitha3b1ac82009-04-03 14:45:06 +0000301 }
302
Eric Smith8c663262007-08-25 02:26:07 +0000303 return 1;
304}
305
Eric Smith58a42242009-04-30 01:00:33 +0000306/* Calculate the padding needed. */
307static void
308calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
309 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
310 Py_ssize_t *n_total)
311{
312 if (width >= 0) {
313 if (nchars > width)
314 *n_total = nchars;
315 else
316 *n_total = width;
317 }
318 else {
319 /* not specified, use all of the chars and no more */
320 *n_total = nchars;
321 }
322
Eric Smith4e260c52010-02-22 18:54:44 +0000323 /* Figure out how much leading space we need, based on the
Eric Smith58a42242009-04-30 01:00:33 +0000324 aligning */
325 if (align == '>')
326 *n_lpadding = *n_total - nchars;
327 else if (align == '^')
328 *n_lpadding = (*n_total - nchars) / 2;
Eric Smith4e260c52010-02-22 18:54:44 +0000329 else if (align == '<' || align == '=')
Eric Smith58a42242009-04-30 01:00:33 +0000330 *n_lpadding = 0;
Eric Smith4e260c52010-02-22 18:54:44 +0000331 else {
332 /* We should never have an unspecified alignment. */
333 *n_lpadding = 0;
334 assert(0);
335 }
Eric Smith58a42242009-04-30 01:00:33 +0000336
337 *n_rpadding = *n_total - nchars - *n_lpadding;
338}
339
340/* Do the padding, and return a pointer to where the caller-supplied
341 content goes. */
342static STRINGLIB_CHAR *
343fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
344 Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
345{
346 /* Pad on left. */
347 if (n_lpadding)
348 STRINGLIB_FILL(p, fill_char, n_lpadding);
349
350 /* Pad on right. */
351 if (n_rpadding)
352 STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
353
354 /* Pointer to the user content. */
355 return p + n_lpadding;
356}
357
358#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
Eric Smith8c663262007-08-25 02:26:07 +0000359/************************************************************************/
360/*********** common routines for numeric formatting *********************/
361/************************************************************************/
362
Eric Smith0923d1d2009-04-16 20:16:10 +0000363/* Locale type codes. */
364#define LT_CURRENT_LOCALE 0
365#define LT_DEFAULT_LOCALE 1
366#define LT_NO_LOCALE 2
367
368/* Locale info needed for formatting integers and the part of floats
369 before and including the decimal. Note that locales only support
370 8-bit chars, not unicode. */
371typedef struct {
372 char *decimal_point;
373 char *thousands_sep;
374 char *grouping;
375} LocaleInfo;
376
Eric Smith8c663262007-08-25 02:26:07 +0000377/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000378 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000379typedef struct {
380 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000381 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000382 Py_ssize_t n_spadding;
383 Py_ssize_t n_rpadding;
Eric Smith0923d1d2009-04-16 20:16:10 +0000384 char sign;
385 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
386 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
387 any grouping chars. */
388 Py_ssize_t n_decimal; /* 0 if only an integer */
389 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
390 excluding the decimal itself, if
391 present. */
392
393 /* These 2 are not the widths of fields, but are needed by
394 STRINGLIB_GROUPING. */
395 Py_ssize_t n_digits; /* The number of digits before a decimal
396 or exponent. */
397 Py_ssize_t n_min_width; /* The min_width we used when we computed
398 the n_grouped_digits width. */
Eric Smith8c663262007-08-25 02:26:07 +0000399} NumberFieldWidths;
400
Eric Smith58a42242009-04-30 01:00:33 +0000401
Eric Smith0923d1d2009-04-16 20:16:10 +0000402/* Given a number of the form:
403 digits[remainder]
404 where ptr points to the start and end points to the end, find where
405 the integer part ends. This could be a decimal, an exponent, both,
406 or neither.
407 If a decimal point is present, set *has_decimal and increment
408 remainder beyond it.
409 Results are undefined (but shouldn't crash) for improperly
410 formatted strings.
411*/
412static void
413parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
414 Py_ssize_t *n_remainder, int *has_decimal)
415{
416 STRINGLIB_CHAR *end = ptr + len;
417 STRINGLIB_CHAR *remainder;
418
419 while (ptr<end && isdigit(*ptr))
420 ++ptr;
421 remainder = ptr;
422
423 /* Does remainder start with a decimal point? */
424 *has_decimal = ptr<end && *remainder == '.';
425
426 /* Skip the decimal point. */
427 if (*has_decimal)
428 remainder++;
429
430 *n_remainder = end - remainder;
431}
432
Eric Smith8c663262007-08-25 02:26:07 +0000433/* not all fields of format are used. for example, precision is
434 unused. should this take discrete params in order to be more clear
435 about what it does? or is passing a single format parameter easier
436 and more efficient enough to justify a little obfuscation? */
Eric Smith0923d1d2009-04-16 20:16:10 +0000437static Py_ssize_t
438calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
439 STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
440 Py_ssize_t n_number, Py_ssize_t n_remainder,
441 int has_decimal, const LocaleInfo *locale,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000442 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000443{
Eric Smith0923d1d2009-04-16 20:16:10 +0000444 Py_ssize_t n_non_digit_non_padding;
445 Py_ssize_t n_padding;
446
447 spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
Eric Smith05212a12008-07-16 19:41:14 +0000448 spec->n_lpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000449 spec->n_prefix = n_prefix;
450 spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
451 spec->n_remainder = n_remainder;
Eric Smith05212a12008-07-16 19:41:14 +0000452 spec->n_spadding = 0;
453 spec->n_rpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000454 spec->sign = '\0';
455 spec->n_sign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000456
457 /* the output will look like:
Eric Smith0923d1d2009-04-16 20:16:10 +0000458 | |
459 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
460 | |
Eric Smith8c663262007-08-25 02:26:07 +0000461
Eric Smith0923d1d2009-04-16 20:16:10 +0000462 sign is computed from format->sign and the actual
Eric Smith8c663262007-08-25 02:26:07 +0000463 sign of the number
464
Eric Smithb1ebcc62008-07-15 13:02:41 +0000465 prefix is given (it's for the '0x' prefix)
466
Eric Smith8c663262007-08-25 02:26:07 +0000467 digits is already known
468
469 the total width is either given, or computed from the
470 actual digits
471
472 only one of lpadding, spadding, and rpadding can be non-zero,
473 and it's calculated from the width and other fields
474 */
475
476 /* compute the various parts we're going to write */
Eric Smith0923d1d2009-04-16 20:16:10 +0000477 switch (format->sign) {
478 case '+':
Eric Smith8c663262007-08-25 02:26:07 +0000479 /* always put a + or - */
Eric Smith0923d1d2009-04-16 20:16:10 +0000480 spec->n_sign = 1;
481 spec->sign = (sign_char == '-' ? '-' : '+');
482 break;
483 case ' ':
484 spec->n_sign = 1;
485 spec->sign = (sign_char == '-' ? '-' : ' ');
486 break;
487 default:
488 /* Not specified, or the default (-) */
489 if (sign_char == '-') {
490 spec->n_sign = 1;
491 spec->sign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000492 }
493 }
494
Eric Smith0923d1d2009-04-16 20:16:10 +0000495 /* The number of chars used for non-digits and non-padding. */
496 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
497 spec->n_remainder;
Eric Smithd68af8f2008-07-16 00:15:35 +0000498
Eric Smith0923d1d2009-04-16 20:16:10 +0000499 /* min_width can go negative, that's okay. format->width == -1 means
500 we don't care. */
Eric Smithabb28c62010-02-23 00:22:24 +0000501 if (format->fill_char == '0' && format->align == '=')
Eric Smith0923d1d2009-04-16 20:16:10 +0000502 spec->n_min_width = format->width - n_non_digit_non_padding;
503 else
504 spec->n_min_width = 0;
505
506 if (spec->n_digits == 0)
507 /* This case only occurs when using 'c' formatting, we need
508 to special case it because the grouping code always wants
509 to have at least one character. */
510 spec->n_grouped_digits = 0;
511 else
512 spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
513 spec->n_digits,
514 spec->n_min_width,
515 locale->grouping,
516 locale->thousands_sep);
517
518 /* Given the desired width and the total of digit and non-digit
519 space we consume, see if we need any padding. format->width can
520 be negative (meaning no padding), but this code still works in
521 that case. */
522 n_padding = format->width -
523 (n_non_digit_non_padding + spec->n_grouped_digits);
524 if (n_padding > 0) {
525 /* Some padding is needed. Determine if it's left, space, or right. */
526 switch (format->align) {
527 case '<':
528 spec->n_rpadding = n_padding;
529 break;
530 case '^':
531 spec->n_lpadding = n_padding / 2;
532 spec->n_rpadding = n_padding - spec->n_lpadding;
533 break;
534 case '=':
535 spec->n_spadding = n_padding;
536 break;
Eric Smith4e260c52010-02-22 18:54:44 +0000537 case '>':
Eric Smith0923d1d2009-04-16 20:16:10 +0000538 spec->n_lpadding = n_padding;
539 break;
Eric Smith4e260c52010-02-22 18:54:44 +0000540 default:
541 /* Shouldn't get here, but treat it as '>' */
542 spec->n_lpadding = n_padding;
543 assert(0);
544 break;
Eric Smith8c663262007-08-25 02:26:07 +0000545 }
546 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000547 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
548 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
549 spec->n_remainder + spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000550}
551
Eric Smith0923d1d2009-04-16 20:16:10 +0000552/* Fill in the digit parts of a numbers's string representation,
553 as determined in calc_number_widths().
554 No error checking, since we know the buffer is the correct size. */
555static void
556fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
557 STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
558 STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
559 LocaleInfo *locale, int toupper)
Eric Smith8c663262007-08-25 02:26:07 +0000560{
Eric Smith0923d1d2009-04-16 20:16:10 +0000561 /* Used to keep track of digits, decimal, and remainder. */
562 STRINGLIB_CHAR *p = digits;
563
564#ifndef NDEBUG
565 Py_ssize_t r;
566#endif
Eric Smith8c663262007-08-25 02:26:07 +0000567
568 if (spec->n_lpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000569 STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
570 buf += spec->n_lpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000571 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000572 if (spec->n_sign == 1) {
573 *buf++ = spec->sign;
Eric Smith8c663262007-08-25 02:26:07 +0000574 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000575 if (spec->n_prefix) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000576 memmove(buf,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000577 prefix,
578 spec->n_prefix * sizeof(STRINGLIB_CHAR));
Eric Smith0923d1d2009-04-16 20:16:10 +0000579 if (toupper) {
580 Py_ssize_t t;
581 for (t = 0; t < spec->n_prefix; ++t)
582 buf[t] = STRINGLIB_TOUPPER(buf[t]);
583 }
584 buf += spec->n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000585 }
Eric Smith8c663262007-08-25 02:26:07 +0000586 if (spec->n_spadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000587 STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
588 buf += spec->n_spadding;
Eric Smith8c663262007-08-25 02:26:07 +0000589 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000590
591 /* Only for type 'c' special case, it has no digits. */
592 if (spec->n_digits != 0) {
593 /* Fill the digits with InsertThousandsGrouping. */
594#ifndef NDEBUG
595 r =
596#endif
597 STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
598 spec->n_digits, spec->n_min_width,
599 locale->grouping, locale->thousands_sep);
600#ifndef NDEBUG
601 assert(r == spec->n_grouped_digits);
602#endif
603 p += spec->n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000604 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000605 if (toupper) {
606 Py_ssize_t t;
607 for (t = 0; t < spec->n_grouped_digits; ++t)
608 buf[t] = STRINGLIB_TOUPPER(buf[t]);
609 }
610 buf += spec->n_grouped_digits;
611
612 if (spec->n_decimal) {
613 Py_ssize_t t;
614 for (t = 0; t < spec->n_decimal; ++t)
615 buf[t] = locale->decimal_point[t];
616 buf += spec->n_decimal;
617 p += 1;
618 }
619
620 if (spec->n_remainder) {
621 memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
622 buf += spec->n_remainder;
623 p += spec->n_remainder;
624 }
625
Eric Smith8c663262007-08-25 02:26:07 +0000626 if (spec->n_rpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000627 STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
628 buf += spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000629 }
Eric Smith8c663262007-08-25 02:26:07 +0000630}
Eric Smith0923d1d2009-04-16 20:16:10 +0000631
632static char no_grouping[1] = {CHAR_MAX};
633
634/* Find the decimal point character(s?), thousands_separator(s?), and
635 grouping description, either for the current locale if type is
636 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
637 none if LT_NO_LOCALE. */
638static void
639get_locale_info(int type, LocaleInfo *locale_info)
640{
641 switch (type) {
642 case LT_CURRENT_LOCALE: {
643 struct lconv *locale_data = localeconv();
644 locale_info->decimal_point = locale_data->decimal_point;
645 locale_info->thousands_sep = locale_data->thousands_sep;
646 locale_info->grouping = locale_data->grouping;
647 break;
648 }
649 case LT_DEFAULT_LOCALE:
650 locale_info->decimal_point = ".";
651 locale_info->thousands_sep = ",";
Benjamin Peterson504b6e82010-06-07 22:35:08 +0000652 locale_info->grouping = "\3"; /* Group every 3 characters. The
653 (implicit) trailing 0 means repeat
Eric Smith0923d1d2009-04-16 20:16:10 +0000654 infinitely. */
655 break;
656 case LT_NO_LOCALE:
657 locale_info->decimal_point = ".";
658 locale_info->thousands_sep = "";
659 locale_info->grouping = no_grouping;
660 break;
661 default:
662 assert(0);
663 }
664}
665
Eric Smith58a42242009-04-30 01:00:33 +0000666#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
Eric Smith8c663262007-08-25 02:26:07 +0000667
668/************************************************************************/
669/*********** string formatting ******************************************/
670/************************************************************************/
671
672static PyObject *
673format_string_internal(PyObject *value, const InternalFormatSpec *format)
674{
Eric Smith8c663262007-08-25 02:26:07 +0000675 Py_ssize_t lpad;
Eric Smith58a42242009-04-30 01:00:33 +0000676 Py_ssize_t rpad;
677 Py_ssize_t total;
678 STRINGLIB_CHAR *p;
Eric Smith8c663262007-08-25 02:26:07 +0000679 Py_ssize_t len = STRINGLIB_LEN(value);
680 PyObject *result = NULL;
681
682 /* sign is not allowed on strings */
683 if (format->sign != '\0') {
684 PyErr_SetString(PyExc_ValueError,
685 "Sign not allowed in string format specifier");
686 goto done;
687 }
688
Eric Smithb1ebcc62008-07-15 13:02:41 +0000689 /* alternate is not allowed on strings */
690 if (format->alternate) {
691 PyErr_SetString(PyExc_ValueError,
692 "Alternate form (#) not allowed in string format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000693 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000694 goto done;
695 }
696
Eric Smith8c663262007-08-25 02:26:07 +0000697 /* '=' alignment not allowed on strings */
698 if (format->align == '=') {
699 PyErr_SetString(PyExc_ValueError,
700 "'=' alignment not allowed "
701 "in string format specifier");
702 goto done;
703 }
704
705 /* if precision is specified, output no more that format.precision
706 characters */
707 if (format->precision >= 0 && len >= format->precision) {
708 len = format->precision;
709 }
710
Eric Smith58a42242009-04-30 01:00:33 +0000711 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
Eric Smith8c663262007-08-25 02:26:07 +0000712
713 /* allocate the resulting string */
Eric Smith58a42242009-04-30 01:00:33 +0000714 result = STRINGLIB_NEW(NULL, total);
Eric Smith8c663262007-08-25 02:26:07 +0000715 if (result == NULL)
716 goto done;
717
Eric Smith58a42242009-04-30 01:00:33 +0000718 /* Write into that space. First the padding. */
719 p = fill_padding(STRINGLIB_STR(result), len,
720 format->fill_char=='\0'?' ':format->fill_char,
721 lpad, rpad);
Eric Smith8c663262007-08-25 02:26:07 +0000722
Eric Smith58a42242009-04-30 01:00:33 +0000723 /* Then the source string. */
724 memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
Eric Smith8c663262007-08-25 02:26:07 +0000725
726done:
727 return result;
728}
729
730
731/************************************************************************/
732/*********** long formatting ********************************************/
733/************************************************************************/
734
Eric Smith8fd3eba2008-02-17 19:48:00 +0000735#if defined FORMAT_LONG || defined FORMAT_INT
736typedef PyObject*
737(*IntOrLongToString)(PyObject *value, int base);
738
Eric Smith8c663262007-08-25 02:26:07 +0000739static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000740format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000741 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000742{
743 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000744 PyObject *tmp = NULL;
745 STRINGLIB_CHAR *pnumeric_chars;
746 STRINGLIB_CHAR numeric_char;
Eric Smith0923d1d2009-04-16 20:16:10 +0000747 STRINGLIB_CHAR sign_char = '\0';
Eric Smith8c663262007-08-25 02:26:07 +0000748 Py_ssize_t n_digits; /* count of digits need from the computed
749 string */
Eric Smith0923d1d2009-04-16 20:16:10 +0000750 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
751 produces non-digits */
Eric Smithd68af8f2008-07-16 00:15:35 +0000752 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
Eric Smith0923d1d2009-04-16 20:16:10 +0000753 Py_ssize_t n_total;
Eric Smithd68af8f2008-07-16 00:15:35 +0000754 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000755 NumberFieldWidths spec;
756 long x;
757
Eric Smith0923d1d2009-04-16 20:16:10 +0000758 /* Locale settings, either from the actual locale or
759 from a hard-code pseudo-locale */
760 LocaleInfo locale;
761
Eric Smith8c663262007-08-25 02:26:07 +0000762 /* no precision allowed on integers */
763 if (format->precision != -1) {
764 PyErr_SetString(PyExc_ValueError,
765 "Precision not allowed in integer format specifier");
766 goto done;
767 }
768
Eric Smith8c663262007-08-25 02:26:07 +0000769 /* special case for character formatting */
770 if (format->type == 'c') {
771 /* error to specify a sign */
772 if (format->sign != '\0') {
773 PyErr_SetString(PyExc_ValueError,
774 "Sign not allowed with integer"
775 " format specifier 'c'");
776 goto done;
777 }
778
779 /* taken from unicodeobject.c formatchar() */
780 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000781/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000782 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000783 if (x == -1 && PyErr_Occurred())
784 goto done;
785#ifdef Py_UNICODE_WIDE
786 if (x < 0 || x > 0x10ffff) {
787 PyErr_SetString(PyExc_OverflowError,
788 "%c arg not in range(0x110000) "
789 "(wide Python build)");
790 goto done;
791 }
792#else
793 if (x < 0 || x > 0xffff) {
794 PyErr_SetString(PyExc_OverflowError,
795 "%c arg not in range(0x10000) "
796 "(narrow Python build)");
797 goto done;
798 }
799#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000800 numeric_char = (STRINGLIB_CHAR)x;
801 pnumeric_chars = &numeric_char;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000802 n_digits = 1;
Eric Smith0923d1d2009-04-16 20:16:10 +0000803
804 /* As a sort-of hack, we tell calc_number_widths that we only
805 have "remainder" characters. calc_number_widths thinks
806 these are characters that don't get formatted, only copied
807 into the output string. We do this for 'c' formatting,
808 because the characters are likely to be non-digits. */
809 n_remainder = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000810 }
811 else {
Eric Smith8c663262007-08-25 02:26:07 +0000812 int base;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000813 int leading_chars_to_skip = 0; /* Number of characters added by
814 PyNumber_ToBase that we want to
815 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000816
817 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000818 PyNumber_ToBase */
819 switch (format->type) {
820 case 'b':
821 base = 2;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000822 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000823 break;
824 case 'o':
825 base = 8;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000826 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000827 break;
828 case 'x':
829 case 'X':
830 base = 16;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000831 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000832 break;
833 default: /* shouldn't be needed, but stops a compiler warning */
834 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000835 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000836 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000837 break;
838 }
839
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000840 /* The number of prefix chars is the same as the leading
841 chars to skip */
842 if (format->alternate)
843 n_prefix = leading_chars_to_skip;
Eric Smithd68af8f2008-07-16 00:15:35 +0000844
Eric Smith8fd3eba2008-02-17 19:48:00 +0000845 /* Do the hard part, converting to a string in a given base */
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000846 tmp = tostring(value, base);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000847 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000848 goto done;
849
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000850 pnumeric_chars = STRINGLIB_STR(tmp);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000851 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000852
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000853 prefix = pnumeric_chars;
Eric Smithd68af8f2008-07-16 00:15:35 +0000854
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000855 /* Remember not to modify what pnumeric_chars points to. it
856 might be interned. Only modify it after we copy it into a
857 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000858
Eric Smith8fd3eba2008-02-17 19:48:00 +0000859 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000860 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000861 if (pnumeric_chars[0] == '-') {
862 sign_char = pnumeric_chars[0];
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000863 ++prefix;
864 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000865 }
866
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000867 /* Skip over the leading chars (0x, 0b, etc.) */
868 n_digits -= leading_chars_to_skip;
869 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000870 }
871
Eric Smith0923d1d2009-04-16 20:16:10 +0000872 /* Determine the grouping, separator, and decimal point, if any. */
873 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
874 (format->thousands_separators ?
875 LT_DEFAULT_LOCALE :
876 LT_NO_LOCALE),
877 &locale);
Eric Smith5807c412008-05-11 21:00:57 +0000878
Eric Smith0923d1d2009-04-16 20:16:10 +0000879 /* Calculate how much memory we'll need. */
880 n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
881 n_digits, n_remainder, 0, &locale, format);
Eric Smithb151a452008-06-24 11:21:04 +0000882
Eric Smith0923d1d2009-04-16 20:16:10 +0000883 /* Allocate the memory. */
884 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000885 if (!result)
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000886 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000887
Eric Smith0923d1d2009-04-16 20:16:10 +0000888 /* Populate the memory. */
889 fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
890 prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
891 &locale, format->type == 'X');
Eric Smithd68af8f2008-07-16 00:15:35 +0000892
Eric Smith8c663262007-08-25 02:26:07 +0000893done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000894 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000895 return result;
896}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000897#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000898
899/************************************************************************/
900/*********** float formatting *******************************************/
901/************************************************************************/
902
Eric Smith8fd3eba2008-02-17 19:48:00 +0000903#ifdef FORMAT_FLOAT
904#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000905static void
906strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
Eric Smith8c663262007-08-25 02:26:07 +0000907{
Eric Smith0923d1d2009-04-16 20:16:10 +0000908 Py_ssize_t i;
909 for (i = 0; i < len; ++i)
910 buffer[i] = (Py_UNICODE)charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000911}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000912#endif
Eric Smith8c663262007-08-25 02:26:07 +0000913
Eric Smith8c663262007-08-25 02:26:07 +0000914/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000915static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000916format_float_internal(PyObject *value,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000917 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000918{
Eric Smith0923d1d2009-04-16 20:16:10 +0000919 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
Eric Smith8c663262007-08-25 02:26:07 +0000920 Py_ssize_t n_digits;
Eric Smith0923d1d2009-04-16 20:16:10 +0000921 Py_ssize_t n_remainder;
922 Py_ssize_t n_total;
923 int has_decimal;
924 double val;
Eric Smith8c663262007-08-25 02:26:07 +0000925 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +0000926 Py_ssize_t default_precision = 6;
Eric Smith0923d1d2009-04-16 20:16:10 +0000927 STRINGLIB_CHAR type = format->type;
928 int add_pct = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000929 STRINGLIB_CHAR *p;
930 NumberFieldWidths spec;
Eric Smith0923d1d2009-04-16 20:16:10 +0000931 int flags = 0;
932 PyObject *result = NULL;
933 STRINGLIB_CHAR sign_char = '\0';
934 int float_type; /* Used to see if we have a nan, inf, or regular float. */
Eric Smith8c663262007-08-25 02:26:07 +0000935
936#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000937 Py_UNICODE *unicode_tmp = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000938#endif
939
Eric Smith0923d1d2009-04-16 20:16:10 +0000940 /* Locale settings, either from the actual locale or
941 from a hard-code pseudo-locale */
942 LocaleInfo locale;
943
Eric Smith984bb582010-11-25 16:08:06 +0000944 if (format->alternate)
945 flags |= Py_DTSF_ALT;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000946
Eric Smith0923d1d2009-04-16 20:16:10 +0000947 if (type == '\0') {
Mark Dickinson388122d2010-08-04 20:56:28 +0000948 /* Omitted type specifier. Behaves in the same way as repr(x)
949 and str(x) if no precision is given, else like 'g', but with
950 at least one digit after the decimal point. */
Eric Smith0923d1d2009-04-16 20:16:10 +0000951 flags |= Py_DTSF_ADD_DOT_0;
Mark Dickinson388122d2010-08-04 20:56:28 +0000952 type = 'r';
953 default_precision = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000954 }
955
956 if (type == 'n')
957 /* 'n' is the same as 'g', except for the locale used to
958 format the result. We take care of that later. */
959 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000960
Eric Smith0923d1d2009-04-16 20:16:10 +0000961 val = PyFloat_AsDouble(value);
962 if (val == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000963 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000964
965 if (type == '%') {
966 type = 'f';
Eric Smith0923d1d2009-04-16 20:16:10 +0000967 val *= 100;
968 add_pct = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000969 }
970
971 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +0000972 precision = default_precision;
Mark Dickinson388122d2010-08-04 20:56:28 +0000973 else if (type == 'r')
974 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000975
Eric Smith0923d1d2009-04-16 20:16:10 +0000976 /* Cast "type", because if we're in unicode we need to pass a
977 8-bit char. This is safe, because we've restricted what "type"
978 can be. */
979 buf = PyOS_double_to_string(val, (char)type, precision, flags,
980 &float_type);
981 if (buf == NULL)
982 goto done;
983 n_digits = strlen(buf);
Eric Smith8c663262007-08-25 02:26:07 +0000984
Eric Smith0923d1d2009-04-16 20:16:10 +0000985 if (add_pct) {
986 /* We know that buf has a trailing zero (since we just called
987 strlen() on it), and we don't use that fact any more. So we
988 can just write over the trailing zero. */
989 buf[n_digits] = '%';
990 n_digits += 1;
991 }
Eric Smith8c663262007-08-25 02:26:07 +0000992
Eric Smith0923d1d2009-04-16 20:16:10 +0000993 /* Since there is no unicode version of PyOS_double_to_string,
994 just use the 8 bit version and then convert to unicode. */
Eric Smith8c663262007-08-25 02:26:07 +0000995#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000996 unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
997 if (unicode_tmp == NULL) {
998 PyErr_NoMemory();
999 goto done;
1000 }
1001 strtounicode(unicode_tmp, buf, n_digits);
1002 p = unicode_tmp;
Eric Smith8c663262007-08-25 02:26:07 +00001003#else
Eric Smith0923d1d2009-04-16 20:16:10 +00001004 p = buf;
Eric Smith8c663262007-08-25 02:26:07 +00001005#endif
1006
Eric Smith0923d1d2009-04-16 20:16:10 +00001007 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +00001008 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +00001009 if (*p == '-') {
1010 sign_char = *p;
Christian Heimesc3f30c42008-02-22 16:37:40 +00001011 ++p;
1012 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +00001013 }
1014
Eric Smith0923d1d2009-04-16 20:16:10 +00001015 /* Determine if we have any "remainder" (after the digits, might include
1016 decimal or exponent or both (or neither)) */
1017 parse_number(p, n_digits, &n_remainder, &has_decimal);
Eric Smith8c663262007-08-25 02:26:07 +00001018
Eric Smith0923d1d2009-04-16 20:16:10 +00001019 /* Determine the grouping, separator, and decimal point, if any. */
1020 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1021 (format->thousands_separators ?
1022 LT_DEFAULT_LOCALE :
1023 LT_NO_LOCALE),
1024 &locale);
1025
1026 /* Calculate how much memory we'll need. */
1027 n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
1028 n_remainder, has_decimal, &locale, format);
1029
1030 /* Allocate the memory. */
1031 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8c663262007-08-25 02:26:07 +00001032 if (result == NULL)
1033 goto done;
1034
Eric Smith0923d1d2009-04-16 20:16:10 +00001035 /* Populate the memory. */
1036 fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
1037 format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
1038 0);
Eric Smith8c663262007-08-25 02:26:07 +00001039
1040done:
Eric Smith0923d1d2009-04-16 20:16:10 +00001041 PyMem_Free(buf);
1042#if STRINGLIB_IS_UNICODE
1043 PyMem_Free(unicode_tmp);
1044#endif
Eric Smith8c663262007-08-25 02:26:07 +00001045 return result;
1046}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001047#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +00001048
1049/************************************************************************/
Eric Smith58a42242009-04-30 01:00:33 +00001050/*********** complex formatting *****************************************/
1051/************************************************************************/
1052
1053#ifdef FORMAT_COMPLEX
1054
1055static PyObject *
1056format_complex_internal(PyObject *value,
1057 const InternalFormatSpec *format)
1058{
1059 double re;
1060 double im;
1061 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1062 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1063
1064 InternalFormatSpec tmp_format = *format;
1065 Py_ssize_t n_re_digits;
1066 Py_ssize_t n_im_digits;
1067 Py_ssize_t n_re_remainder;
1068 Py_ssize_t n_im_remainder;
1069 Py_ssize_t n_re_total;
1070 Py_ssize_t n_im_total;
1071 int re_has_decimal;
1072 int im_has_decimal;
1073 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +00001074 Py_ssize_t default_precision = 6;
Eric Smith58a42242009-04-30 01:00:33 +00001075 STRINGLIB_CHAR type = format->type;
1076 STRINGLIB_CHAR *p_re;
1077 STRINGLIB_CHAR *p_im;
1078 NumberFieldWidths re_spec;
1079 NumberFieldWidths im_spec;
1080 int flags = 0;
1081 PyObject *result = NULL;
1082 STRINGLIB_CHAR *p;
1083 STRINGLIB_CHAR re_sign_char = '\0';
1084 STRINGLIB_CHAR im_sign_char = '\0';
1085 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1086 int im_float_type;
1087 int add_parens = 0;
1088 int skip_re = 0;
1089 Py_ssize_t lpad;
1090 Py_ssize_t rpad;
1091 Py_ssize_t total;
1092
1093#if STRINGLIB_IS_UNICODE
1094 Py_UNICODE *re_unicode_tmp = NULL;
1095 Py_UNICODE *im_unicode_tmp = NULL;
1096#endif
1097
1098 /* Locale settings, either from the actual locale or
1099 from a hard-code pseudo-locale */
1100 LocaleInfo locale;
1101
Eric Smith984bb582010-11-25 16:08:06 +00001102 /* Zero padding is not allowed. */
Eric Smith58a42242009-04-30 01:00:33 +00001103 if (format->fill_char == '0') {
1104 PyErr_SetString(PyExc_ValueError,
1105 "Zero padding is not allowed in complex format "
1106 "specifier");
1107 goto done;
1108 }
1109
1110 /* Neither is '=' alignment . */
1111 if (format->align == '=') {
1112 PyErr_SetString(PyExc_ValueError,
1113 "'=' alignment flag is not allowed in complex format "
1114 "specifier");
1115 goto done;
1116 }
1117
1118 re = PyComplex_RealAsDouble(value);
1119 if (re == -1.0 && PyErr_Occurred())
1120 goto done;
1121 im = PyComplex_ImagAsDouble(value);
1122 if (im == -1.0 && PyErr_Occurred())
1123 goto done;
1124
Eric Smith984bb582010-11-25 16:08:06 +00001125 if (format->alternate)
1126 flags |= Py_DTSF_ALT;
1127
Eric Smith58a42242009-04-30 01:00:33 +00001128 if (type == '\0') {
1129 /* Omitted type specifier. Should be like str(self). */
Mark Dickinson388122d2010-08-04 20:56:28 +00001130 type = 'r';
1131 default_precision = 0;
Mark Dickinson5b65df72010-08-01 10:41:49 +00001132 if (re == 0.0 && copysign(1.0, re) == 1.0)
Eric Smith58a42242009-04-30 01:00:33 +00001133 skip_re = 1;
Mark Dickinson5b65df72010-08-01 10:41:49 +00001134 else
1135 add_parens = 1;
Eric Smith58a42242009-04-30 01:00:33 +00001136 }
1137
1138 if (type == 'n')
1139 /* 'n' is the same as 'g', except for the locale used to
1140 format the result. We take care of that later. */
1141 type = 'g';
1142
Eric Smith58a42242009-04-30 01:00:33 +00001143 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +00001144 precision = default_precision;
Mark Dickinson388122d2010-08-04 20:56:28 +00001145 else if (type == 'r')
1146 type = 'g';
Eric Smith58a42242009-04-30 01:00:33 +00001147
1148 /* Cast "type", because if we're in unicode we need to pass a
1149 8-bit char. This is safe, because we've restricted what "type"
1150 can be. */
1151 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1152 &re_float_type);
1153 if (re_buf == NULL)
1154 goto done;
1155 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1156 &im_float_type);
1157 if (im_buf == NULL)
1158 goto done;
1159
1160 n_re_digits = strlen(re_buf);
1161 n_im_digits = strlen(im_buf);
1162
1163 /* Since there is no unicode version of PyOS_double_to_string,
1164 just use the 8 bit version and then convert to unicode. */
1165#if STRINGLIB_IS_UNICODE
1166 re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
1167 if (re_unicode_tmp == NULL) {
1168 PyErr_NoMemory();
1169 goto done;
1170 }
1171 strtounicode(re_unicode_tmp, re_buf, n_re_digits);
1172 p_re = re_unicode_tmp;
1173
1174 im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
1175 if (im_unicode_tmp == NULL) {
1176 PyErr_NoMemory();
1177 goto done;
1178 }
1179 strtounicode(im_unicode_tmp, im_buf, n_im_digits);
1180 p_im = im_unicode_tmp;
1181#else
1182 p_re = re_buf;
1183 p_im = im_buf;
1184#endif
1185
1186 /* Is a sign character present in the output? If so, remember it
1187 and skip it */
1188 if (*p_re == '-') {
1189 re_sign_char = *p_re;
1190 ++p_re;
1191 --n_re_digits;
1192 }
1193 if (*p_im == '-') {
1194 im_sign_char = *p_im;
1195 ++p_im;
1196 --n_im_digits;
1197 }
1198
1199 /* Determine if we have any "remainder" (after the digits, might include
1200 decimal or exponent or both (or neither)) */
1201 parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
1202 parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
1203
1204 /* Determine the grouping, separator, and decimal point, if any. */
1205 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1206 (format->thousands_separators ?
1207 LT_DEFAULT_LOCALE :
1208 LT_NO_LOCALE),
1209 &locale);
1210
1211 /* Turn off any padding. We'll do it later after we've composed
1212 the numbers without padding. */
1213 tmp_format.fill_char = '\0';
Eric Smith4e260c52010-02-22 18:54:44 +00001214 tmp_format.align = '<';
Eric Smith58a42242009-04-30 01:00:33 +00001215 tmp_format.width = -1;
1216
1217 /* Calculate how much memory we'll need. */
1218 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
1219 n_re_digits, n_re_remainder,
1220 re_has_decimal, &locale, &tmp_format);
1221
Mark Dickinson5b65df72010-08-01 10:41:49 +00001222 /* Same formatting, but always include a sign, unless the real part is
1223 * going to be omitted, in which case we use whatever sign convention was
1224 * requested by the original format. */
1225 if (!skip_re)
1226 tmp_format.sign = '+';
Eric Smith58a42242009-04-30 01:00:33 +00001227 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
1228 n_im_digits, n_im_remainder,
1229 im_has_decimal, &locale, &tmp_format);
1230
1231 if (skip_re)
1232 n_re_total = 0;
1233
1234 /* Add 1 for the 'j', and optionally 2 for parens. */
1235 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1236 format->width, format->align, &lpad, &rpad, &total);
1237
1238 result = STRINGLIB_NEW(NULL, total);
1239 if (result == NULL)
1240 goto done;
1241
1242 /* Populate the memory. First, the padding. */
1243 p = fill_padding(STRINGLIB_STR(result),
1244 n_re_total + n_im_total + 1 + add_parens * 2,
1245 format->fill_char=='\0' ? ' ' : format->fill_char,
1246 lpad, rpad);
1247
1248 if (add_parens)
1249 *p++ = '(';
1250
1251 if (!skip_re) {
1252 fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
1253 p += n_re_total;
1254 }
1255 fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
1256 p += n_im_total;
1257 *p++ = 'j';
1258
1259 if (add_parens)
1260 *p++ = ')';
1261
1262done:
1263 PyMem_Free(re_buf);
1264 PyMem_Free(im_buf);
1265#if STRINGLIB_IS_UNICODE
1266 PyMem_Free(re_unicode_tmp);
1267 PyMem_Free(im_unicode_tmp);
1268#endif
1269 return result;
1270}
1271#endif /* FORMAT_COMPLEX */
1272
1273/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001274/*********** built in formatters ****************************************/
1275/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001276PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001277FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001278 STRINGLIB_CHAR *format_spec,
1279 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001280{
Eric Smith8c663262007-08-25 02:26:07 +00001281 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +00001282 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001283
1284 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001285 it equivalent to str(obj) */
1286 if (format_spec_len == 0) {
1287 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001288 goto done;
1289 }
1290
1291 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001292 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001293 &format, 's', '<'))
Eric Smith8c663262007-08-25 02:26:07 +00001294 goto done;
1295
1296 /* type conversion? */
1297 switch (format.type) {
1298 case 's':
1299 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001300 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001301 break;
Eric Smith8c663262007-08-25 02:26:07 +00001302 default:
1303 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001304 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001305 goto done;
1306 }
1307
1308done:
Eric Smith8c663262007-08-25 02:26:07 +00001309 return result;
1310}
1311
Eric Smith8fd3eba2008-02-17 19:48:00 +00001312#if defined FORMAT_LONG || defined FORMAT_INT
1313static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +00001314format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001315 STRINGLIB_CHAR *format_spec,
1316 Py_ssize_t format_spec_len,
1317 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +00001318{
Eric Smith8c663262007-08-25 02:26:07 +00001319 PyObject *result = NULL;
1320 PyObject *tmp = NULL;
1321 InternalFormatSpec format;
1322
Eric Smith8c663262007-08-25 02:26:07 +00001323 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001324 it equivalent to str(obj) */
1325 if (format_spec_len == 0) {
1326 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001327 goto done;
1328 }
1329
1330 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001331 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001332 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001333 &format, 'd', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001334 goto done;
1335
1336 /* type conversion? */
1337 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +00001338 case 'b':
1339 case 'c':
1340 case 'd':
1341 case 'o':
1342 case 'x':
1343 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +00001344 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +00001345 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001346 the formatting */
1347 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +00001348 break;
1349
Eric Smithfa767ef2008-01-28 10:59:27 +00001350 case 'e':
1351 case 'E':
1352 case 'f':
1353 case 'F':
1354 case 'g':
1355 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +00001356 case '%':
1357 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001358 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +00001359 if (tmp == NULL)
1360 goto done;
Eric Smithf64bce82009-04-13 00:50:23 +00001361 result = format_float_internal(tmp, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +00001362 break;
1363
Eric Smith8c663262007-08-25 02:26:07 +00001364 default:
1365 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001366 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001367 goto done;
1368 }
1369
1370done:
1371 Py_XDECREF(tmp);
1372 return result;
1373}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001374#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +00001375
Eric Smith8fd3eba2008-02-17 19:48:00 +00001376#ifdef FORMAT_LONG
1377/* Need to define long_format as a function that will convert a long
1378 to a string. In 3.0, _PyLong_Format has the correct signature. In
1379 2.x, we need to fudge a few parameters */
1380#if PY_VERSION_HEX >= 0x03000000
1381#define long_format _PyLong_Format
1382#else
1383static PyObject*
1384long_format(PyObject* value, int base)
1385{
1386 /* Convert to base, don't add trailing 'L', and use the new octal
1387 format. We already know this is a long object */
1388 assert(PyLong_Check(value));
1389 /* convert to base, don't add 'L', and use the new octal format */
1390 return _PyLong_Format(value, base, 0, 1);
1391}
1392#endif
1393
1394PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001395FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001396 STRINGLIB_CHAR *format_spec,
1397 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001398{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001399 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001400 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001401}
1402#endif /* FORMAT_LONG */
1403
1404#ifdef FORMAT_INT
1405/* this is only used for 2.x, not 3.0 */
1406static PyObject*
1407int_format(PyObject* value, int base)
1408{
1409 /* Convert to base, and use the new octal format. We already
1410 know this is an int object */
1411 assert(PyInt_Check(value));
1412 return _PyInt_Format((PyIntObject*)value, base, 1);
1413}
1414
1415PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001416FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001417 STRINGLIB_CHAR *format_spec,
1418 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001419{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001420 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001421 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001422}
1423#endif /* FORMAT_INT */
1424
1425#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001426PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001427FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001428 STRINGLIB_CHAR *format_spec,
1429 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001430{
Eric Smith8c663262007-08-25 02:26:07 +00001431 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001432 InternalFormatSpec format;
1433
Eric Smith8c663262007-08-25 02:26:07 +00001434 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001435 it equivalent to str(obj) */
1436 if (format_spec_len == 0) {
1437 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001438 goto done;
1439 }
1440
1441 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001442 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001443 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001444 &format, '\0', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001445 goto done;
1446
1447 /* type conversion? */
1448 switch (format.type) {
Eric Smith0923d1d2009-04-16 20:16:10 +00001449 case '\0': /* No format code: like 'g', but with at least one decimal. */
Eric Smith8c663262007-08-25 02:26:07 +00001450 case 'e':
1451 case 'E':
1452 case 'f':
1453 case 'F':
1454 case 'g':
1455 case 'G':
1456 case 'n':
1457 case '%':
1458 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001459 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001460 break;
1461
1462 default:
1463 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001464 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001465 goto done;
1466 }
1467
1468done:
Eric Smith8c663262007-08-25 02:26:07 +00001469 return result;
1470}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001471#endif /* FORMAT_FLOAT */
Eric Smith58a42242009-04-30 01:00:33 +00001472
1473#ifdef FORMAT_COMPLEX
1474PyObject *
1475FORMAT_COMPLEX(PyObject *obj,
1476 STRINGLIB_CHAR *format_spec,
1477 Py_ssize_t format_spec_len)
1478{
1479 PyObject *result = NULL;
1480 InternalFormatSpec format;
1481
1482 /* check for the special case of zero length format spec, make
1483 it equivalent to str(obj) */
1484 if (format_spec_len == 0) {
1485 result = STRINGLIB_TOSTR(obj);
1486 goto done;
1487 }
1488
1489 /* parse the format_spec */
1490 if (!parse_internal_render_format_spec(format_spec,
1491 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001492 &format, '\0', '>'))
Eric Smith58a42242009-04-30 01:00:33 +00001493 goto done;
1494
1495 /* type conversion? */
1496 switch (format.type) {
1497 case '\0': /* No format code: like 'g', but with at least one decimal. */
1498 case 'e':
1499 case 'E':
1500 case 'f':
1501 case 'F':
1502 case 'g':
1503 case 'G':
1504 case 'n':
1505 /* no conversion, already a complex. do the formatting */
1506 result = format_complex_internal(obj, &format);
1507 break;
1508
1509 default:
1510 /* unknown */
1511 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1512 goto done;
1513 }
1514
1515done:
1516 return result;
1517}
1518#endif /* FORMAT_COMPLEX */