blob: 139b56cd8e03e0668b76bf58e7364d3f0d86c82b [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{
Mark Dickinson36f27c92011-09-24 19:11:53 +010076 Py_ssize_t accumulator, digitval;
Eric Smith8c663262007-08-25 02:26:07 +000077 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 /*
Mark Dickinson36f27c92011-09-24 19:11:53 +010086 Detect possible overflow before it happens:
87
88 accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
89 accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
Eric Smith8c663262007-08-25 02:26:07 +000090 */
Mark Dickinson36f27c92011-09-24 19:11:53 +010091 if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
Eric Smith8c663262007-08-25 02:26:07 +000092 PyErr_Format(PyExc_ValueError,
93 "Too many decimal digits in format string");
94 return -1;
95 }
Mark Dickinson36f27c92011-09-24 19:11:53 +010096 accumulator = accumulator * 10 + digitval;
Eric Smith8c663262007-08-25 02:26:07 +000097 }
98 *result = accumulator;
99 return numdigits;
100}
101
102/************************************************************************/
103/*********** standard format specifier parsing **************************/
104/************************************************************************/
105
106/* returns true if this character is a specifier alignment token */
107Py_LOCAL_INLINE(int)
108is_alignment_token(STRINGLIB_CHAR c)
109{
110 switch (c) {
111 case '<': case '>': case '=': case '^':
112 return 1;
113 default:
114 return 0;
115 }
116}
117
118/* returns true if this character is a sign element */
119Py_LOCAL_INLINE(int)
120is_sign_element(STRINGLIB_CHAR c)
121{
122 switch (c) {
Eric Smithb7f5ba12007-08-29 12:38:45 +0000123 case ' ': case '+': case '-':
Eric Smith8c663262007-08-25 02:26:07 +0000124 return 1;
125 default:
126 return 0;
127 }
128}
129
130
131typedef struct {
132 STRINGLIB_CHAR fill_char;
133 STRINGLIB_CHAR align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000134 int alternate;
Eric Smith8c663262007-08-25 02:26:07 +0000135 STRINGLIB_CHAR sign;
136 Py_ssize_t width;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000137 int thousands_separators;
Eric Smith8c663262007-08-25 02:26:07 +0000138 Py_ssize_t precision;
139 STRINGLIB_CHAR type;
140} InternalFormatSpec;
141
Eric Smith4e260c52010-02-22 18:54:44 +0000142
143#if 0
144/* Occassionally useful for debugging. Should normally be commented out. */
145static void
146DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
147{
148 printf("internal format spec: fill_char %d\n", format->fill_char);
149 printf("internal format spec: align %d\n", format->align);
150 printf("internal format spec: alternate %d\n", format->alternate);
151 printf("internal format spec: sign %d\n", format->sign);
Eric Smithabb28c62010-02-23 00:22:24 +0000152 printf("internal format spec: width %zd\n", format->width);
Eric Smith4e260c52010-02-22 18:54:44 +0000153 printf("internal format spec: thousands_separators %d\n",
154 format->thousands_separators);
Eric Smithabb28c62010-02-23 00:22:24 +0000155 printf("internal format spec: precision %zd\n", format->precision);
Eric Smith4e260c52010-02-22 18:54:44 +0000156 printf("internal format spec: type %c\n", format->type);
157 printf("\n");
158}
159#endif
160
161
Eric Smith8c663262007-08-25 02:26:07 +0000162/*
163 ptr points to the start of the format_spec, end points just past its end.
164 fills in format with the parsed information.
165 returns 1 on success, 0 on failure.
166 if failure, sets the exception
167*/
168static int
Eric Smith4a7d76d2008-05-30 18:10:19 +0000169parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000170 Py_ssize_t format_spec_len,
Eric Smith8c663262007-08-25 02:26:07 +0000171 InternalFormatSpec *format,
Eric Smith4e260c52010-02-22 18:54:44 +0000172 char default_type,
173 char default_align)
Eric Smith8c663262007-08-25 02:26:07 +0000174{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000175 STRINGLIB_CHAR *ptr = format_spec;
176 STRINGLIB_CHAR *end = format_spec + format_spec_len;
Eric Smith8c663262007-08-25 02:26:07 +0000177
178 /* end-ptr is used throughout this code to specify the length of
179 the input string */
180
Eric Smith0923d1d2009-04-16 20:16:10 +0000181 Py_ssize_t consumed;
Eric Smithabb28c62010-02-23 00:22:24 +0000182 int align_specified = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000183
184 format->fill_char = '\0';
Eric Smith4e260c52010-02-22 18:54:44 +0000185 format->align = default_align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000186 format->alternate = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000187 format->sign = '\0';
188 format->width = -1;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000189 format->thousands_separators = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000190 format->precision = -1;
191 format->type = default_type;
192
193 /* If the second char is an alignment token,
194 then parse the fill char */
195 if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
196 format->align = ptr[1];
197 format->fill_char = ptr[0];
Eric Smithabb28c62010-02-23 00:22:24 +0000198 align_specified = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000199 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];
Eric Smithabb28c62010-02-23 00:22:24 +0000203 align_specified = 1;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000204 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000205 }
206
207 /* Parse the various sign options */
208 if (end-ptr >= 1 && is_sign_element(ptr[0])) {
209 format->sign = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000210 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000211 }
212
Eric Smithd68af8f2008-07-16 00:15:35 +0000213 /* If the next character is #, we're in alternate mode. This only
214 applies to integers. */
215 if (end-ptr >= 1 && ptr[0] == '#') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000216 format->alternate = 1;
217 ++ptr;
Eric Smithd68af8f2008-07-16 00:15:35 +0000218 }
219
Eric Smith8c663262007-08-25 02:26:07 +0000220 /* The special case for 0-padding (backwards compat) */
Eric Smith185e30c2007-08-30 22:23:08 +0000221 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
Eric Smith8c663262007-08-25 02:26:07 +0000222 format->fill_char = '0';
Eric Smithabb28c62010-02-23 00:22:24 +0000223 if (!align_specified) {
Eric Smith8c663262007-08-25 02:26:07 +0000224 format->align = '=';
225 }
Christian Heimesc3f30c42008-02-22 16:37:40 +0000226 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000227 }
228
Eric Smith0923d1d2009-04-16 20:16:10 +0000229 consumed = get_integer(&ptr, end, &format->width);
230 if (consumed == -1)
231 /* Overflow error. Exception already set. */
232 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000233
Eric Smith0923d1d2009-04-16 20:16:10 +0000234 /* If consumed is 0, we didn't consume any characters for the
235 width. In that case, reset the width to -1, because
236 get_integer() will have set it to zero. -1 is how we record
237 that the width wasn't specified. */
238 if (consumed == 0)
Eric Smith8c663262007-08-25 02:26:07 +0000239 format->width = -1;
Eric Smith8c663262007-08-25 02:26:07 +0000240
Eric Smitha3b1ac82009-04-03 14:45:06 +0000241 /* Comma signifies add thousands separators */
242 if (end-ptr && ptr[0] == ',') {
243 format->thousands_separators = 1;
244 ++ptr;
245 }
246
Eric Smith8c663262007-08-25 02:26:07 +0000247 /* Parse field precision */
248 if (end-ptr && ptr[0] == '.') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000249 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000250
Eric Smith0923d1d2009-04-16 20:16:10 +0000251 consumed = get_integer(&ptr, end, &format->precision);
252 if (consumed == -1)
253 /* Overflow error. Exception already set. */
254 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000255
Eric Smith0923d1d2009-04-16 20:16:10 +0000256 /* Not having a precision after a dot is an error. */
257 if (consumed == 0) {
Eric Smith8c663262007-08-25 02:26:07 +0000258 PyErr_Format(PyExc_ValueError,
259 "Format specifier missing precision");
260 return 0;
261 }
262
263 }
264
Eric Smith0923d1d2009-04-16 20:16:10 +0000265 /* Finally, parse the type field. */
Eric Smith8c663262007-08-25 02:26:07 +0000266
267 if (end-ptr > 1) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000268 /* More than one char remain, invalid conversion spec. */
Eric Smith8c663262007-08-25 02:26:07 +0000269 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
270 return 0;
271 }
272
273 if (end-ptr == 1) {
274 format->type = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000275 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000276 }
277
Eric Smith0923d1d2009-04-16 20:16:10 +0000278 /* Do as much validating as we can, just by looking at the format
279 specifier. Do not take into account what type of formatting
280 we're doing (int, float, string). */
281
282 if (format->thousands_separators) {
283 switch (format->type) {
284 case 'd':
285 case 'e':
286 case 'f':
287 case 'g':
288 case 'E':
289 case 'G':
290 case '%':
291 case 'F':
Eric Smith937491d2009-04-22 17:04:27 +0000292 case '\0':
Eric Smith0923d1d2009-04-16 20:16:10 +0000293 /* These are allowed. See PEP 378.*/
294 break;
295 default:
Eric Smithbeddd702009-07-30 13:43:08 +0000296 invalid_comma_type(format->type);
Eric Smith0923d1d2009-04-16 20:16:10 +0000297 return 0;
298 }
Eric Smitha3b1ac82009-04-03 14:45:06 +0000299 }
300
Eric Smith8c663262007-08-25 02:26:07 +0000301 return 1;
302}
303
Eric Smith58a42242009-04-30 01:00:33 +0000304/* Calculate the padding needed. */
305static void
306calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
307 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
308 Py_ssize_t *n_total)
309{
310 if (width >= 0) {
311 if (nchars > width)
312 *n_total = nchars;
313 else
314 *n_total = width;
315 }
316 else {
317 /* not specified, use all of the chars and no more */
318 *n_total = nchars;
319 }
320
Eric Smith4e260c52010-02-22 18:54:44 +0000321 /* Figure out how much leading space we need, based on the
Eric Smith58a42242009-04-30 01:00:33 +0000322 aligning */
323 if (align == '>')
324 *n_lpadding = *n_total - nchars;
325 else if (align == '^')
326 *n_lpadding = (*n_total - nchars) / 2;
Eric Smith4e260c52010-02-22 18:54:44 +0000327 else if (align == '<' || align == '=')
Eric Smith58a42242009-04-30 01:00:33 +0000328 *n_lpadding = 0;
Eric Smith4e260c52010-02-22 18:54:44 +0000329 else {
330 /* We should never have an unspecified alignment. */
331 *n_lpadding = 0;
332 assert(0);
333 }
Eric Smith58a42242009-04-30 01:00:33 +0000334
335 *n_rpadding = *n_total - nchars - *n_lpadding;
336}
337
338/* Do the padding, and return a pointer to where the caller-supplied
339 content goes. */
340static STRINGLIB_CHAR *
341fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
342 Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
343{
344 /* Pad on left. */
345 if (n_lpadding)
346 STRINGLIB_FILL(p, fill_char, n_lpadding);
347
348 /* Pad on right. */
349 if (n_rpadding)
350 STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
351
352 /* Pointer to the user content. */
353 return p + n_lpadding;
354}
355
356#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
Eric Smith8c663262007-08-25 02:26:07 +0000357/************************************************************************/
358/*********** common routines for numeric formatting *********************/
359/************************************************************************/
360
Eric Smith0923d1d2009-04-16 20:16:10 +0000361/* Locale type codes. */
362#define LT_CURRENT_LOCALE 0
363#define LT_DEFAULT_LOCALE 1
364#define LT_NO_LOCALE 2
365
366/* Locale info needed for formatting integers and the part of floats
367 before and including the decimal. Note that locales only support
368 8-bit chars, not unicode. */
369typedef struct {
370 char *decimal_point;
371 char *thousands_sep;
372 char *grouping;
373} LocaleInfo;
374
Eric Smith8c663262007-08-25 02:26:07 +0000375/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000376 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000377typedef struct {
378 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000379 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000380 Py_ssize_t n_spadding;
381 Py_ssize_t n_rpadding;
Eric Smith0923d1d2009-04-16 20:16:10 +0000382 char sign;
383 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
384 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
385 any grouping chars. */
386 Py_ssize_t n_decimal; /* 0 if only an integer */
387 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
388 excluding the decimal itself, if
389 present. */
390
391 /* These 2 are not the widths of fields, but are needed by
392 STRINGLIB_GROUPING. */
393 Py_ssize_t n_digits; /* The number of digits before a decimal
394 or exponent. */
395 Py_ssize_t n_min_width; /* The min_width we used when we computed
396 the n_grouped_digits width. */
Eric Smith8c663262007-08-25 02:26:07 +0000397} NumberFieldWidths;
398
Eric Smith58a42242009-04-30 01:00:33 +0000399
Eric Smith0923d1d2009-04-16 20:16:10 +0000400/* Given a number of the form:
401 digits[remainder]
402 where ptr points to the start and end points to the end, find where
403 the integer part ends. This could be a decimal, an exponent, both,
404 or neither.
405 If a decimal point is present, set *has_decimal and increment
406 remainder beyond it.
407 Results are undefined (but shouldn't crash) for improperly
408 formatted strings.
409*/
410static void
411parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
412 Py_ssize_t *n_remainder, int *has_decimal)
413{
414 STRINGLIB_CHAR *end = ptr + len;
415 STRINGLIB_CHAR *remainder;
416
417 while (ptr<end && isdigit(*ptr))
418 ++ptr;
419 remainder = ptr;
420
421 /* Does remainder start with a decimal point? */
422 *has_decimal = ptr<end && *remainder == '.';
423
424 /* Skip the decimal point. */
425 if (*has_decimal)
426 remainder++;
427
428 *n_remainder = end - remainder;
429}
430
Eric Smith8c663262007-08-25 02:26:07 +0000431/* not all fields of format are used. for example, precision is
432 unused. should this take discrete params in order to be more clear
433 about what it does? or is passing a single format parameter easier
434 and more efficient enough to justify a little obfuscation? */
Eric Smith0923d1d2009-04-16 20:16:10 +0000435static Py_ssize_t
436calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
437 STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
438 Py_ssize_t n_number, Py_ssize_t n_remainder,
439 int has_decimal, const LocaleInfo *locale,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000440 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000441{
Eric Smith0923d1d2009-04-16 20:16:10 +0000442 Py_ssize_t n_non_digit_non_padding;
443 Py_ssize_t n_padding;
444
445 spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
Eric Smith05212a12008-07-16 19:41:14 +0000446 spec->n_lpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000447 spec->n_prefix = n_prefix;
448 spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
449 spec->n_remainder = n_remainder;
Eric Smith05212a12008-07-16 19:41:14 +0000450 spec->n_spadding = 0;
451 spec->n_rpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000452 spec->sign = '\0';
453 spec->n_sign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000454
455 /* the output will look like:
Eric Smith0923d1d2009-04-16 20:16:10 +0000456 | |
457 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
458 | |
Eric Smith8c663262007-08-25 02:26:07 +0000459
Eric Smith0923d1d2009-04-16 20:16:10 +0000460 sign is computed from format->sign and the actual
Eric Smith8c663262007-08-25 02:26:07 +0000461 sign of the number
462
Eric Smithb1ebcc62008-07-15 13:02:41 +0000463 prefix is given (it's for the '0x' prefix)
464
Eric Smith8c663262007-08-25 02:26:07 +0000465 digits is already known
466
467 the total width is either given, or computed from the
468 actual digits
469
470 only one of lpadding, spadding, and rpadding can be non-zero,
471 and it's calculated from the width and other fields
472 */
473
474 /* compute the various parts we're going to write */
Eric Smith0923d1d2009-04-16 20:16:10 +0000475 switch (format->sign) {
476 case '+':
Eric Smith8c663262007-08-25 02:26:07 +0000477 /* always put a + or - */
Eric Smith0923d1d2009-04-16 20:16:10 +0000478 spec->n_sign = 1;
479 spec->sign = (sign_char == '-' ? '-' : '+');
480 break;
481 case ' ':
482 spec->n_sign = 1;
483 spec->sign = (sign_char == '-' ? '-' : ' ');
484 break;
485 default:
486 /* Not specified, or the default (-) */
487 if (sign_char == '-') {
488 spec->n_sign = 1;
489 spec->sign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000490 }
491 }
492
Eric Smith0923d1d2009-04-16 20:16:10 +0000493 /* The number of chars used for non-digits and non-padding. */
494 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
495 spec->n_remainder;
Eric Smithd68af8f2008-07-16 00:15:35 +0000496
Eric Smith0923d1d2009-04-16 20:16:10 +0000497 /* min_width can go negative, that's okay. format->width == -1 means
498 we don't care. */
Eric Smithabb28c62010-02-23 00:22:24 +0000499 if (format->fill_char == '0' && format->align == '=')
Eric Smith0923d1d2009-04-16 20:16:10 +0000500 spec->n_min_width = format->width - n_non_digit_non_padding;
501 else
502 spec->n_min_width = 0;
503
504 if (spec->n_digits == 0)
505 /* This case only occurs when using 'c' formatting, we need
506 to special case it because the grouping code always wants
507 to have at least one character. */
508 spec->n_grouped_digits = 0;
509 else
510 spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
511 spec->n_digits,
512 spec->n_min_width,
513 locale->grouping,
514 locale->thousands_sep);
515
516 /* Given the desired width and the total of digit and non-digit
517 space we consume, see if we need any padding. format->width can
518 be negative (meaning no padding), but this code still works in
519 that case. */
520 n_padding = format->width -
521 (n_non_digit_non_padding + spec->n_grouped_digits);
522 if (n_padding > 0) {
523 /* Some padding is needed. Determine if it's left, space, or right. */
524 switch (format->align) {
525 case '<':
526 spec->n_rpadding = n_padding;
527 break;
528 case '^':
529 spec->n_lpadding = n_padding / 2;
530 spec->n_rpadding = n_padding - spec->n_lpadding;
531 break;
532 case '=':
533 spec->n_spadding = n_padding;
534 break;
Eric Smith4e260c52010-02-22 18:54:44 +0000535 case '>':
Eric Smith0923d1d2009-04-16 20:16:10 +0000536 spec->n_lpadding = n_padding;
537 break;
Eric Smith4e260c52010-02-22 18:54:44 +0000538 default:
539 /* Shouldn't get here, but treat it as '>' */
540 spec->n_lpadding = n_padding;
541 assert(0);
542 break;
Eric Smith8c663262007-08-25 02:26:07 +0000543 }
544 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000545 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
546 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
547 spec->n_remainder + spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000548}
549
Eric Smith0923d1d2009-04-16 20:16:10 +0000550/* Fill in the digit parts of a numbers's string representation,
551 as determined in calc_number_widths().
552 No error checking, since we know the buffer is the correct size. */
553static void
554fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
555 STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
556 STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
557 LocaleInfo *locale, int toupper)
Eric Smith8c663262007-08-25 02:26:07 +0000558{
Eric Smith0923d1d2009-04-16 20:16:10 +0000559 /* Used to keep track of digits, decimal, and remainder. */
560 STRINGLIB_CHAR *p = digits;
561
562#ifndef NDEBUG
563 Py_ssize_t r;
564#endif
Eric Smith8c663262007-08-25 02:26:07 +0000565
566 if (spec->n_lpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000567 STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
568 buf += spec->n_lpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000569 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000570 if (spec->n_sign == 1) {
571 *buf++ = spec->sign;
Eric Smith8c663262007-08-25 02:26:07 +0000572 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000573 if (spec->n_prefix) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000574 memmove(buf,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000575 prefix,
576 spec->n_prefix * sizeof(STRINGLIB_CHAR));
Eric Smith0923d1d2009-04-16 20:16:10 +0000577 if (toupper) {
578 Py_ssize_t t;
579 for (t = 0; t < spec->n_prefix; ++t)
580 buf[t] = STRINGLIB_TOUPPER(buf[t]);
581 }
582 buf += spec->n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000583 }
Eric Smith8c663262007-08-25 02:26:07 +0000584 if (spec->n_spadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000585 STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
586 buf += spec->n_spadding;
Eric Smith8c663262007-08-25 02:26:07 +0000587 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000588
589 /* Only for type 'c' special case, it has no digits. */
590 if (spec->n_digits != 0) {
591 /* Fill the digits with InsertThousandsGrouping. */
592#ifndef NDEBUG
593 r =
594#endif
595 STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
596 spec->n_digits, spec->n_min_width,
597 locale->grouping, locale->thousands_sep);
598#ifndef NDEBUG
599 assert(r == spec->n_grouped_digits);
600#endif
601 p += spec->n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000602 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000603 if (toupper) {
604 Py_ssize_t t;
605 for (t = 0; t < spec->n_grouped_digits; ++t)
606 buf[t] = STRINGLIB_TOUPPER(buf[t]);
607 }
608 buf += spec->n_grouped_digits;
609
610 if (spec->n_decimal) {
611 Py_ssize_t t;
612 for (t = 0; t < spec->n_decimal; ++t)
613 buf[t] = locale->decimal_point[t];
614 buf += spec->n_decimal;
615 p += 1;
616 }
617
618 if (spec->n_remainder) {
619 memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
620 buf += spec->n_remainder;
621 p += spec->n_remainder;
622 }
623
Eric Smith8c663262007-08-25 02:26:07 +0000624 if (spec->n_rpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000625 STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
626 buf += spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000627 }
Eric Smith8c663262007-08-25 02:26:07 +0000628}
Eric Smith0923d1d2009-04-16 20:16:10 +0000629
630static char no_grouping[1] = {CHAR_MAX};
631
632/* Find the decimal point character(s?), thousands_separator(s?), and
633 grouping description, either for the current locale if type is
634 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
635 none if LT_NO_LOCALE. */
636static void
637get_locale_info(int type, LocaleInfo *locale_info)
638{
639 switch (type) {
640 case LT_CURRENT_LOCALE: {
641 struct lconv *locale_data = localeconv();
642 locale_info->decimal_point = locale_data->decimal_point;
643 locale_info->thousands_sep = locale_data->thousands_sep;
644 locale_info->grouping = locale_data->grouping;
645 break;
646 }
647 case LT_DEFAULT_LOCALE:
648 locale_info->decimal_point = ".";
649 locale_info->thousands_sep = ",";
Benjamin Peterson504b6e82010-06-07 22:35:08 +0000650 locale_info->grouping = "\3"; /* Group every 3 characters. The
651 (implicit) trailing 0 means repeat
Eric Smith0923d1d2009-04-16 20:16:10 +0000652 infinitely. */
653 break;
654 case LT_NO_LOCALE:
655 locale_info->decimal_point = ".";
656 locale_info->thousands_sep = "";
657 locale_info->grouping = no_grouping;
658 break;
659 default:
660 assert(0);
661 }
662}
663
Eric Smith58a42242009-04-30 01:00:33 +0000664#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
Eric Smith8c663262007-08-25 02:26:07 +0000665
666/************************************************************************/
667/*********** string formatting ******************************************/
668/************************************************************************/
669
670static PyObject *
671format_string_internal(PyObject *value, const InternalFormatSpec *format)
672{
Eric Smith8c663262007-08-25 02:26:07 +0000673 Py_ssize_t lpad;
Eric Smith58a42242009-04-30 01:00:33 +0000674 Py_ssize_t rpad;
675 Py_ssize_t total;
676 STRINGLIB_CHAR *p;
Eric Smith8c663262007-08-25 02:26:07 +0000677 Py_ssize_t len = STRINGLIB_LEN(value);
678 PyObject *result = NULL;
679
680 /* sign is not allowed on strings */
681 if (format->sign != '\0') {
682 PyErr_SetString(PyExc_ValueError,
683 "Sign not allowed in string format specifier");
684 goto done;
685 }
686
Eric Smithb1ebcc62008-07-15 13:02:41 +0000687 /* alternate is not allowed on strings */
688 if (format->alternate) {
689 PyErr_SetString(PyExc_ValueError,
690 "Alternate form (#) not allowed in string format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000691 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000692 goto done;
693 }
694
Eric Smith8c663262007-08-25 02:26:07 +0000695 /* '=' alignment not allowed on strings */
696 if (format->align == '=') {
697 PyErr_SetString(PyExc_ValueError,
698 "'=' alignment not allowed "
699 "in string format specifier");
700 goto done;
701 }
702
703 /* if precision is specified, output no more that format.precision
704 characters */
705 if (format->precision >= 0 && len >= format->precision) {
706 len = format->precision;
707 }
708
Eric Smith58a42242009-04-30 01:00:33 +0000709 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
Eric Smith8c663262007-08-25 02:26:07 +0000710
711 /* allocate the resulting string */
Eric Smith58a42242009-04-30 01:00:33 +0000712 result = STRINGLIB_NEW(NULL, total);
Eric Smith8c663262007-08-25 02:26:07 +0000713 if (result == NULL)
714 goto done;
715
Eric Smith58a42242009-04-30 01:00:33 +0000716 /* Write into that space. First the padding. */
717 p = fill_padding(STRINGLIB_STR(result), len,
718 format->fill_char=='\0'?' ':format->fill_char,
719 lpad, rpad);
Eric Smith8c663262007-08-25 02:26:07 +0000720
Eric Smith58a42242009-04-30 01:00:33 +0000721 /* Then the source string. */
722 memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
Eric Smith8c663262007-08-25 02:26:07 +0000723
724done:
725 return result;
726}
727
728
729/************************************************************************/
730/*********** long formatting ********************************************/
731/************************************************************************/
732
Eric Smith8fd3eba2008-02-17 19:48:00 +0000733#if defined FORMAT_LONG || defined FORMAT_INT
734typedef PyObject*
735(*IntOrLongToString)(PyObject *value, int base);
736
Eric Smith8c663262007-08-25 02:26:07 +0000737static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000738format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000739 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000740{
741 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000742 PyObject *tmp = NULL;
743 STRINGLIB_CHAR *pnumeric_chars;
744 STRINGLIB_CHAR numeric_char;
Eric Smith0923d1d2009-04-16 20:16:10 +0000745 STRINGLIB_CHAR sign_char = '\0';
Eric Smith8c663262007-08-25 02:26:07 +0000746 Py_ssize_t n_digits; /* count of digits need from the computed
747 string */
Eric Smith0923d1d2009-04-16 20:16:10 +0000748 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
749 produces non-digits */
Eric Smithd68af8f2008-07-16 00:15:35 +0000750 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
Eric Smith0923d1d2009-04-16 20:16:10 +0000751 Py_ssize_t n_total;
Eric Smithd68af8f2008-07-16 00:15:35 +0000752 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000753 NumberFieldWidths spec;
754 long x;
755
Eric Smith0923d1d2009-04-16 20:16:10 +0000756 /* Locale settings, either from the actual locale or
757 from a hard-code pseudo-locale */
758 LocaleInfo locale;
759
Eric Smith8c663262007-08-25 02:26:07 +0000760 /* no precision allowed on integers */
761 if (format->precision != -1) {
762 PyErr_SetString(PyExc_ValueError,
763 "Precision not allowed in integer format specifier");
764 goto done;
765 }
766
Eric Smith8c663262007-08-25 02:26:07 +0000767 /* special case for character formatting */
768 if (format->type == 'c') {
769 /* error to specify a sign */
770 if (format->sign != '\0') {
771 PyErr_SetString(PyExc_ValueError,
772 "Sign not allowed with integer"
773 " format specifier 'c'");
774 goto done;
775 }
776
777 /* taken from unicodeobject.c formatchar() */
778 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000779/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000780 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000781 if (x == -1 && PyErr_Occurred())
782 goto done;
783#ifdef Py_UNICODE_WIDE
784 if (x < 0 || x > 0x10ffff) {
785 PyErr_SetString(PyExc_OverflowError,
786 "%c arg not in range(0x110000) "
787 "(wide Python build)");
788 goto done;
789 }
790#else
791 if (x < 0 || x > 0xffff) {
792 PyErr_SetString(PyExc_OverflowError,
793 "%c arg not in range(0x10000) "
794 "(narrow Python build)");
795 goto done;
796 }
797#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000798 numeric_char = (STRINGLIB_CHAR)x;
799 pnumeric_chars = &numeric_char;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000800 n_digits = 1;
Eric Smith0923d1d2009-04-16 20:16:10 +0000801
802 /* As a sort-of hack, we tell calc_number_widths that we only
803 have "remainder" characters. calc_number_widths thinks
804 these are characters that don't get formatted, only copied
805 into the output string. We do this for 'c' formatting,
806 because the characters are likely to be non-digits. */
807 n_remainder = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000808 }
809 else {
Eric Smith8c663262007-08-25 02:26:07 +0000810 int base;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000811 int leading_chars_to_skip = 0; /* Number of characters added by
812 PyNumber_ToBase that we want to
813 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000814
815 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000816 PyNumber_ToBase */
817 switch (format->type) {
818 case 'b':
819 base = 2;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000820 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000821 break;
822 case 'o':
823 base = 8;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000824 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000825 break;
826 case 'x':
827 case 'X':
828 base = 16;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000829 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000830 break;
831 default: /* shouldn't be needed, but stops a compiler warning */
832 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000833 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000834 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000835 break;
836 }
837
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000838 /* The number of prefix chars is the same as the leading
839 chars to skip */
840 if (format->alternate)
841 n_prefix = leading_chars_to_skip;
Eric Smithd68af8f2008-07-16 00:15:35 +0000842
Eric Smith8fd3eba2008-02-17 19:48:00 +0000843 /* Do the hard part, converting to a string in a given base */
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000844 tmp = tostring(value, base);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000845 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000846 goto done;
847
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000848 pnumeric_chars = STRINGLIB_STR(tmp);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000849 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000850
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000851 prefix = pnumeric_chars;
Eric Smithd68af8f2008-07-16 00:15:35 +0000852
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000853 /* Remember not to modify what pnumeric_chars points to. it
854 might be interned. Only modify it after we copy it into a
855 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000856
Eric Smith8fd3eba2008-02-17 19:48:00 +0000857 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000858 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000859 if (pnumeric_chars[0] == '-') {
860 sign_char = pnumeric_chars[0];
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000861 ++prefix;
862 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000863 }
864
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000865 /* Skip over the leading chars (0x, 0b, etc.) */
866 n_digits -= leading_chars_to_skip;
867 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000868 }
869
Eric Smith0923d1d2009-04-16 20:16:10 +0000870 /* Determine the grouping, separator, and decimal point, if any. */
871 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
872 (format->thousands_separators ?
873 LT_DEFAULT_LOCALE :
874 LT_NO_LOCALE),
875 &locale);
Eric Smith5807c412008-05-11 21:00:57 +0000876
Eric Smith0923d1d2009-04-16 20:16:10 +0000877 /* Calculate how much memory we'll need. */
878 n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
879 n_digits, n_remainder, 0, &locale, format);
Eric Smithb151a452008-06-24 11:21:04 +0000880
Eric Smith0923d1d2009-04-16 20:16:10 +0000881 /* Allocate the memory. */
882 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000883 if (!result)
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000884 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000885
Eric Smith0923d1d2009-04-16 20:16:10 +0000886 /* Populate the memory. */
887 fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
888 prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
889 &locale, format->type == 'X');
Eric Smithd68af8f2008-07-16 00:15:35 +0000890
Eric Smith8c663262007-08-25 02:26:07 +0000891done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000892 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000893 return result;
894}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000895#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000896
897/************************************************************************/
898/*********** float formatting *******************************************/
899/************************************************************************/
900
Eric Smith8fd3eba2008-02-17 19:48:00 +0000901#ifdef FORMAT_FLOAT
902#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000903static void
904strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
Eric Smith8c663262007-08-25 02:26:07 +0000905{
Eric Smith0923d1d2009-04-16 20:16:10 +0000906 Py_ssize_t i;
907 for (i = 0; i < len; ++i)
908 buffer[i] = (Py_UNICODE)charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000909}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000910#endif
Eric Smith8c663262007-08-25 02:26:07 +0000911
Eric Smith8c663262007-08-25 02:26:07 +0000912/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000913static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000914format_float_internal(PyObject *value,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000915 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000916{
Eric Smith0923d1d2009-04-16 20:16:10 +0000917 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
Eric Smith8c663262007-08-25 02:26:07 +0000918 Py_ssize_t n_digits;
Eric Smith0923d1d2009-04-16 20:16:10 +0000919 Py_ssize_t n_remainder;
920 Py_ssize_t n_total;
921 int has_decimal;
922 double val;
Eric Smith8c663262007-08-25 02:26:07 +0000923 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +0000924 Py_ssize_t default_precision = 6;
Eric Smith0923d1d2009-04-16 20:16:10 +0000925 STRINGLIB_CHAR type = format->type;
926 int add_pct = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000927 STRINGLIB_CHAR *p;
928 NumberFieldWidths spec;
Eric Smith0923d1d2009-04-16 20:16:10 +0000929 int flags = 0;
930 PyObject *result = NULL;
931 STRINGLIB_CHAR sign_char = '\0';
932 int float_type; /* Used to see if we have a nan, inf, or regular float. */
Eric Smith8c663262007-08-25 02:26:07 +0000933
934#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000935 Py_UNICODE *unicode_tmp = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000936#endif
937
Eric Smith0923d1d2009-04-16 20:16:10 +0000938 /* Locale settings, either from the actual locale or
939 from a hard-code pseudo-locale */
940 LocaleInfo locale;
941
Eric Smith984bb582010-11-25 16:08:06 +0000942 if (format->alternate)
943 flags |= Py_DTSF_ALT;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000944
Eric Smith0923d1d2009-04-16 20:16:10 +0000945 if (type == '\0') {
Mark Dickinson388122d2010-08-04 20:56:28 +0000946 /* Omitted type specifier. Behaves in the same way as repr(x)
947 and str(x) if no precision is given, else like 'g', but with
948 at least one digit after the decimal point. */
Eric Smith0923d1d2009-04-16 20:16:10 +0000949 flags |= Py_DTSF_ADD_DOT_0;
Mark Dickinson388122d2010-08-04 20:56:28 +0000950 type = 'r';
951 default_precision = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000952 }
953
954 if (type == 'n')
955 /* 'n' is the same as 'g', except for the locale used to
956 format the result. We take care of that later. */
957 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000958
Eric Smith0923d1d2009-04-16 20:16:10 +0000959 val = PyFloat_AsDouble(value);
960 if (val == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000961 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000962
963 if (type == '%') {
964 type = 'f';
Eric Smith0923d1d2009-04-16 20:16:10 +0000965 val *= 100;
966 add_pct = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000967 }
968
969 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +0000970 precision = default_precision;
Mark Dickinson388122d2010-08-04 20:56:28 +0000971 else if (type == 'r')
972 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000973
Eric Smith0923d1d2009-04-16 20:16:10 +0000974 /* Cast "type", because if we're in unicode we need to pass a
975 8-bit char. This is safe, because we've restricted what "type"
976 can be. */
977 buf = PyOS_double_to_string(val, (char)type, precision, flags,
978 &float_type);
979 if (buf == NULL)
980 goto done;
981 n_digits = strlen(buf);
Eric Smith8c663262007-08-25 02:26:07 +0000982
Eric Smith0923d1d2009-04-16 20:16:10 +0000983 if (add_pct) {
984 /* We know that buf has a trailing zero (since we just called
985 strlen() on it), and we don't use that fact any more. So we
986 can just write over the trailing zero. */
987 buf[n_digits] = '%';
988 n_digits += 1;
989 }
Eric Smith8c663262007-08-25 02:26:07 +0000990
Eric Smith0923d1d2009-04-16 20:16:10 +0000991 /* Since there is no unicode version of PyOS_double_to_string,
992 just use the 8 bit version and then convert to unicode. */
Eric Smith8c663262007-08-25 02:26:07 +0000993#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000994 unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
995 if (unicode_tmp == NULL) {
996 PyErr_NoMemory();
997 goto done;
998 }
999 strtounicode(unicode_tmp, buf, n_digits);
1000 p = unicode_tmp;
Eric Smith8c663262007-08-25 02:26:07 +00001001#else
Eric Smith0923d1d2009-04-16 20:16:10 +00001002 p = buf;
Eric Smith8c663262007-08-25 02:26:07 +00001003#endif
1004
Eric Smith0923d1d2009-04-16 20:16:10 +00001005 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +00001006 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +00001007 if (*p == '-') {
1008 sign_char = *p;
Christian Heimesc3f30c42008-02-22 16:37:40 +00001009 ++p;
1010 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +00001011 }
1012
Eric Smith0923d1d2009-04-16 20:16:10 +00001013 /* Determine if we have any "remainder" (after the digits, might include
1014 decimal or exponent or both (or neither)) */
1015 parse_number(p, n_digits, &n_remainder, &has_decimal);
Eric Smith8c663262007-08-25 02:26:07 +00001016
Eric Smith0923d1d2009-04-16 20:16:10 +00001017 /* Determine the grouping, separator, and decimal point, if any. */
1018 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1019 (format->thousands_separators ?
1020 LT_DEFAULT_LOCALE :
1021 LT_NO_LOCALE),
1022 &locale);
1023
1024 /* Calculate how much memory we'll need. */
1025 n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
1026 n_remainder, has_decimal, &locale, format);
1027
1028 /* Allocate the memory. */
1029 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8c663262007-08-25 02:26:07 +00001030 if (result == NULL)
1031 goto done;
1032
Eric Smith0923d1d2009-04-16 20:16:10 +00001033 /* Populate the memory. */
1034 fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
1035 format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
1036 0);
Eric Smith8c663262007-08-25 02:26:07 +00001037
1038done:
Eric Smith0923d1d2009-04-16 20:16:10 +00001039 PyMem_Free(buf);
1040#if STRINGLIB_IS_UNICODE
1041 PyMem_Free(unicode_tmp);
1042#endif
Eric Smith8c663262007-08-25 02:26:07 +00001043 return result;
1044}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001045#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +00001046
1047/************************************************************************/
Eric Smith58a42242009-04-30 01:00:33 +00001048/*********** complex formatting *****************************************/
1049/************************************************************************/
1050
1051#ifdef FORMAT_COMPLEX
1052
1053static PyObject *
1054format_complex_internal(PyObject *value,
1055 const InternalFormatSpec *format)
1056{
1057 double re;
1058 double im;
1059 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1060 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1061
1062 InternalFormatSpec tmp_format = *format;
1063 Py_ssize_t n_re_digits;
1064 Py_ssize_t n_im_digits;
1065 Py_ssize_t n_re_remainder;
1066 Py_ssize_t n_im_remainder;
1067 Py_ssize_t n_re_total;
1068 Py_ssize_t n_im_total;
1069 int re_has_decimal;
1070 int im_has_decimal;
1071 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +00001072 Py_ssize_t default_precision = 6;
Eric Smith58a42242009-04-30 01:00:33 +00001073 STRINGLIB_CHAR type = format->type;
1074 STRINGLIB_CHAR *p_re;
1075 STRINGLIB_CHAR *p_im;
1076 NumberFieldWidths re_spec;
1077 NumberFieldWidths im_spec;
1078 int flags = 0;
1079 PyObject *result = NULL;
1080 STRINGLIB_CHAR *p;
1081 STRINGLIB_CHAR re_sign_char = '\0';
1082 STRINGLIB_CHAR im_sign_char = '\0';
1083 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1084 int im_float_type;
1085 int add_parens = 0;
1086 int skip_re = 0;
1087 Py_ssize_t lpad;
1088 Py_ssize_t rpad;
1089 Py_ssize_t total;
1090
1091#if STRINGLIB_IS_UNICODE
1092 Py_UNICODE *re_unicode_tmp = NULL;
1093 Py_UNICODE *im_unicode_tmp = NULL;
1094#endif
1095
1096 /* Locale settings, either from the actual locale or
1097 from a hard-code pseudo-locale */
1098 LocaleInfo locale;
1099
Eric Smith984bb582010-11-25 16:08:06 +00001100 /* Zero padding is not allowed. */
Eric Smith58a42242009-04-30 01:00:33 +00001101 if (format->fill_char == '0') {
1102 PyErr_SetString(PyExc_ValueError,
1103 "Zero padding is not allowed in complex format "
1104 "specifier");
1105 goto done;
1106 }
1107
1108 /* Neither is '=' alignment . */
1109 if (format->align == '=') {
1110 PyErr_SetString(PyExc_ValueError,
1111 "'=' alignment flag is not allowed in complex format "
1112 "specifier");
1113 goto done;
1114 }
1115
1116 re = PyComplex_RealAsDouble(value);
1117 if (re == -1.0 && PyErr_Occurred())
1118 goto done;
1119 im = PyComplex_ImagAsDouble(value);
1120 if (im == -1.0 && PyErr_Occurred())
1121 goto done;
1122
Eric Smith984bb582010-11-25 16:08:06 +00001123 if (format->alternate)
1124 flags |= Py_DTSF_ALT;
1125
Eric Smith58a42242009-04-30 01:00:33 +00001126 if (type == '\0') {
1127 /* Omitted type specifier. Should be like str(self). */
Mark Dickinson388122d2010-08-04 20:56:28 +00001128 type = 'r';
1129 default_precision = 0;
Mark Dickinson5b65df72010-08-01 10:41:49 +00001130 if (re == 0.0 && copysign(1.0, re) == 1.0)
Eric Smith58a42242009-04-30 01:00:33 +00001131 skip_re = 1;
Mark Dickinson5b65df72010-08-01 10:41:49 +00001132 else
1133 add_parens = 1;
Eric Smith58a42242009-04-30 01:00:33 +00001134 }
1135
1136 if (type == 'n')
1137 /* 'n' is the same as 'g', except for the locale used to
1138 format the result. We take care of that later. */
1139 type = 'g';
1140
Eric Smith58a42242009-04-30 01:00:33 +00001141 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +00001142 precision = default_precision;
Mark Dickinson388122d2010-08-04 20:56:28 +00001143 else if (type == 'r')
1144 type = 'g';
Eric Smith58a42242009-04-30 01:00:33 +00001145
1146 /* Cast "type", because if we're in unicode we need to pass a
1147 8-bit char. This is safe, because we've restricted what "type"
1148 can be. */
1149 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1150 &re_float_type);
1151 if (re_buf == NULL)
1152 goto done;
1153 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1154 &im_float_type);
1155 if (im_buf == NULL)
1156 goto done;
1157
1158 n_re_digits = strlen(re_buf);
1159 n_im_digits = strlen(im_buf);
1160
1161 /* Since there is no unicode version of PyOS_double_to_string,
1162 just use the 8 bit version and then convert to unicode. */
1163#if STRINGLIB_IS_UNICODE
1164 re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
1165 if (re_unicode_tmp == NULL) {
1166 PyErr_NoMemory();
1167 goto done;
1168 }
1169 strtounicode(re_unicode_tmp, re_buf, n_re_digits);
1170 p_re = re_unicode_tmp;
1171
1172 im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
1173 if (im_unicode_tmp == NULL) {
1174 PyErr_NoMemory();
1175 goto done;
1176 }
1177 strtounicode(im_unicode_tmp, im_buf, n_im_digits);
1178 p_im = im_unicode_tmp;
1179#else
1180 p_re = re_buf;
1181 p_im = im_buf;
1182#endif
1183
1184 /* Is a sign character present in the output? If so, remember it
1185 and skip it */
1186 if (*p_re == '-') {
1187 re_sign_char = *p_re;
1188 ++p_re;
1189 --n_re_digits;
1190 }
1191 if (*p_im == '-') {
1192 im_sign_char = *p_im;
1193 ++p_im;
1194 --n_im_digits;
1195 }
1196
1197 /* Determine if we have any "remainder" (after the digits, might include
1198 decimal or exponent or both (or neither)) */
1199 parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
1200 parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
1201
1202 /* Determine the grouping, separator, and decimal point, if any. */
1203 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1204 (format->thousands_separators ?
1205 LT_DEFAULT_LOCALE :
1206 LT_NO_LOCALE),
1207 &locale);
1208
1209 /* Turn off any padding. We'll do it later after we've composed
1210 the numbers without padding. */
1211 tmp_format.fill_char = '\0';
Eric Smith4e260c52010-02-22 18:54:44 +00001212 tmp_format.align = '<';
Eric Smith58a42242009-04-30 01:00:33 +00001213 tmp_format.width = -1;
1214
1215 /* Calculate how much memory we'll need. */
1216 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
1217 n_re_digits, n_re_remainder,
1218 re_has_decimal, &locale, &tmp_format);
1219
Mark Dickinson5b65df72010-08-01 10:41:49 +00001220 /* Same formatting, but always include a sign, unless the real part is
1221 * going to be omitted, in which case we use whatever sign convention was
1222 * requested by the original format. */
1223 if (!skip_re)
1224 tmp_format.sign = '+';
Eric Smith58a42242009-04-30 01:00:33 +00001225 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
1226 n_im_digits, n_im_remainder,
1227 im_has_decimal, &locale, &tmp_format);
1228
1229 if (skip_re)
1230 n_re_total = 0;
1231
1232 /* Add 1 for the 'j', and optionally 2 for parens. */
1233 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1234 format->width, format->align, &lpad, &rpad, &total);
1235
1236 result = STRINGLIB_NEW(NULL, total);
1237 if (result == NULL)
1238 goto done;
1239
1240 /* Populate the memory. First, the padding. */
1241 p = fill_padding(STRINGLIB_STR(result),
1242 n_re_total + n_im_total + 1 + add_parens * 2,
1243 format->fill_char=='\0' ? ' ' : format->fill_char,
1244 lpad, rpad);
1245
1246 if (add_parens)
1247 *p++ = '(';
1248
1249 if (!skip_re) {
1250 fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
1251 p += n_re_total;
1252 }
1253 fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
1254 p += n_im_total;
1255 *p++ = 'j';
1256
1257 if (add_parens)
1258 *p++ = ')';
1259
1260done:
1261 PyMem_Free(re_buf);
1262 PyMem_Free(im_buf);
1263#if STRINGLIB_IS_UNICODE
1264 PyMem_Free(re_unicode_tmp);
1265 PyMem_Free(im_unicode_tmp);
1266#endif
1267 return result;
1268}
1269#endif /* FORMAT_COMPLEX */
1270
1271/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001272/*********** built in formatters ****************************************/
1273/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001274PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001275FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001276 STRINGLIB_CHAR *format_spec,
1277 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001278{
Eric Smith8c663262007-08-25 02:26:07 +00001279 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +00001280 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001281
1282 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001283 it equivalent to str(obj) */
1284 if (format_spec_len == 0) {
1285 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001286 goto done;
1287 }
1288
1289 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001290 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001291 &format, 's', '<'))
Eric Smith8c663262007-08-25 02:26:07 +00001292 goto done;
1293
1294 /* type conversion? */
1295 switch (format.type) {
1296 case 's':
1297 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001298 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001299 break;
Eric Smith8c663262007-08-25 02:26:07 +00001300 default:
1301 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001302 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001303 goto done;
1304 }
1305
1306done:
Eric Smith8c663262007-08-25 02:26:07 +00001307 return result;
1308}
1309
Eric Smith8fd3eba2008-02-17 19:48:00 +00001310#if defined FORMAT_LONG || defined FORMAT_INT
1311static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +00001312format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001313 STRINGLIB_CHAR *format_spec,
1314 Py_ssize_t format_spec_len,
1315 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +00001316{
Eric Smith8c663262007-08-25 02:26:07 +00001317 PyObject *result = NULL;
1318 PyObject *tmp = NULL;
1319 InternalFormatSpec format;
1320
Eric Smith8c663262007-08-25 02:26:07 +00001321 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001322 it equivalent to str(obj) */
1323 if (format_spec_len == 0) {
1324 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001325 goto done;
1326 }
1327
1328 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001329 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001330 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001331 &format, 'd', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001332 goto done;
1333
1334 /* type conversion? */
1335 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +00001336 case 'b':
1337 case 'c':
1338 case 'd':
1339 case 'o':
1340 case 'x':
1341 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +00001342 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +00001343 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001344 the formatting */
1345 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +00001346 break;
1347
Eric Smithfa767ef2008-01-28 10:59:27 +00001348 case 'e':
1349 case 'E':
1350 case 'f':
1351 case 'F':
1352 case 'g':
1353 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +00001354 case '%':
1355 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001356 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +00001357 if (tmp == NULL)
1358 goto done;
Eric Smithf64bce82009-04-13 00:50:23 +00001359 result = format_float_internal(tmp, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +00001360 break;
1361
Eric Smith8c663262007-08-25 02:26:07 +00001362 default:
1363 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001364 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001365 goto done;
1366 }
1367
1368done:
1369 Py_XDECREF(tmp);
1370 return result;
1371}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001372#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +00001373
Eric Smith8fd3eba2008-02-17 19:48:00 +00001374#ifdef FORMAT_LONG
1375/* Need to define long_format as a function that will convert a long
1376 to a string. In 3.0, _PyLong_Format has the correct signature. In
1377 2.x, we need to fudge a few parameters */
1378#if PY_VERSION_HEX >= 0x03000000
1379#define long_format _PyLong_Format
1380#else
1381static PyObject*
1382long_format(PyObject* value, int base)
1383{
1384 /* Convert to base, don't add trailing 'L', and use the new octal
1385 format. We already know this is a long object */
1386 assert(PyLong_Check(value));
1387 /* convert to base, don't add 'L', and use the new octal format */
1388 return _PyLong_Format(value, base, 0, 1);
1389}
1390#endif
1391
1392PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001393FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001394 STRINGLIB_CHAR *format_spec,
1395 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001396{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001397 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001398 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001399}
1400#endif /* FORMAT_LONG */
1401
1402#ifdef FORMAT_INT
1403/* this is only used for 2.x, not 3.0 */
1404static PyObject*
1405int_format(PyObject* value, int base)
1406{
1407 /* Convert to base, and use the new octal format. We already
1408 know this is an int object */
1409 assert(PyInt_Check(value));
1410 return _PyInt_Format((PyIntObject*)value, base, 1);
1411}
1412
1413PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001414FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001415 STRINGLIB_CHAR *format_spec,
1416 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001417{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001418 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001419 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001420}
1421#endif /* FORMAT_INT */
1422
1423#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001424PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001425FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001426 STRINGLIB_CHAR *format_spec,
1427 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001428{
Eric Smith8c663262007-08-25 02:26:07 +00001429 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001430 InternalFormatSpec format;
1431
Eric Smith8c663262007-08-25 02:26:07 +00001432 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001433 it equivalent to str(obj) */
1434 if (format_spec_len == 0) {
1435 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001436 goto done;
1437 }
1438
1439 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001440 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001441 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001442 &format, '\0', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001443 goto done;
1444
1445 /* type conversion? */
1446 switch (format.type) {
Eric Smith0923d1d2009-04-16 20:16:10 +00001447 case '\0': /* No format code: like 'g', but with at least one decimal. */
Eric Smith8c663262007-08-25 02:26:07 +00001448 case 'e':
1449 case 'E':
1450 case 'f':
1451 case 'F':
1452 case 'g':
1453 case 'G':
1454 case 'n':
1455 case '%':
1456 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001457 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001458 break;
1459
1460 default:
1461 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001462 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001463 goto done;
1464 }
1465
1466done:
Eric Smith8c663262007-08-25 02:26:07 +00001467 return result;
1468}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001469#endif /* FORMAT_FLOAT */
Eric Smith58a42242009-04-30 01:00:33 +00001470
1471#ifdef FORMAT_COMPLEX
1472PyObject *
1473FORMAT_COMPLEX(PyObject *obj,
1474 STRINGLIB_CHAR *format_spec,
1475 Py_ssize_t format_spec_len)
1476{
1477 PyObject *result = NULL;
1478 InternalFormatSpec format;
1479
1480 /* check for the special case of zero length format spec, make
1481 it equivalent to str(obj) */
1482 if (format_spec_len == 0) {
1483 result = STRINGLIB_TOSTR(obj);
1484 goto done;
1485 }
1486
1487 /* parse the format_spec */
1488 if (!parse_internal_render_format_spec(format_spec,
1489 format_spec_len,
Eric Smith4e260c52010-02-22 18:54:44 +00001490 &format, '\0', '>'))
Eric Smith58a42242009-04-30 01:00:33 +00001491 goto done;
1492
1493 /* type conversion? */
1494 switch (format.type) {
1495 case '\0': /* No format code: like 'g', but with at least one decimal. */
1496 case 'e':
1497 case 'E':
1498 case 'f':
1499 case 'F':
1500 case 'g':
1501 case 'G':
1502 case 'n':
1503 /* no conversion, already a complex. do the formatting */
1504 result = format_complex_internal(obj, &format);
1505 break;
1506
1507 default:
1508 /* unknown */
1509 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1510 goto done;
1511 }
1512
1513done:
1514 return result;
1515}
1516#endif /* FORMAT_COMPLEX */