blob: 47708636bed6d99e24be416b15a6b5efda55808d [file] [log] [blame]
Eric Smith8c663262007-08-25 02:26:07 +00001/* implements the string, long, and float formatters. that is,
2 string.__format__, etc. */
3
Eric Smith0923d1d2009-04-16 20:16:10 +00004#include <locale.h>
5
Eric Smith8c663262007-08-25 02:26:07 +00006/* Before including this, you must include either:
7 stringlib/unicodedefs.h
8 stringlib/stringdefs.h
9
10 Also, you should define the names:
11 FORMAT_STRING
12 FORMAT_LONG
13 FORMAT_FLOAT
Eric Smith58a42242009-04-30 01:00:33 +000014 FORMAT_COMPLEX
Eric Smith8c663262007-08-25 02:26:07 +000015 to be whatever you want the public names of these functions to
16 be. These are the only non-static functions defined here.
17*/
18
Eric Smith5e5c0db2009-02-20 14:25:03 +000019/* Raises an exception about an unknown presentation type for this
20 * type. */
21
22static void
23unknown_presentation_type(STRINGLIB_CHAR presentation_type,
24 const char* type_name)
25{
26#if STRINGLIB_IS_UNICODE
27 /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
28 hence the two cases. If it is char, gcc complains that the
29 condition below is always true, hence the ifdef. */
30 if (presentation_type > 32 && presentation_type < 128)
31#endif
32 PyErr_Format(PyExc_ValueError,
33 "Unknown format code '%c' "
34 "for object of type '%.200s'",
35 presentation_type,
36 type_name);
37#if STRINGLIB_IS_UNICODE
38 else
39 PyErr_Format(PyExc_ValueError,
40 "Unknown format code '\\x%x' "
41 "for object of type '%.200s'",
42 (unsigned int)presentation_type,
43 type_name);
44#endif
45}
46
Eric Smith8c663262007-08-25 02:26:07 +000047/*
48 get_integer consumes 0 or more decimal digit characters from an
49 input string, updates *result with the corresponding positive
50 integer, and returns the number of digits consumed.
51
52 returns -1 on error.
53*/
54static int
55get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
56 Py_ssize_t *result)
57{
58 Py_ssize_t accumulator, digitval, oldaccumulator;
59 int numdigits;
60 accumulator = numdigits = 0;
61 for (;;(*ptr)++, numdigits++) {
62 if (*ptr >= end)
63 break;
64 digitval = STRINGLIB_TODECIMAL(**ptr);
65 if (digitval < 0)
66 break;
67 /*
68 This trick was copied from old Unicode format code. It's cute,
69 but would really suck on an old machine with a slow divide
70 implementation. Fortunately, in the normal case we do not
71 expect too many digits.
72 */
73 oldaccumulator = accumulator;
74 accumulator *= 10;
75 if ((accumulator+10)/10 != oldaccumulator+1) {
76 PyErr_Format(PyExc_ValueError,
77 "Too many decimal digits in format string");
78 return -1;
79 }
80 accumulator += digitval;
81 }
82 *result = accumulator;
83 return numdigits;
84}
85
86/************************************************************************/
87/*********** standard format specifier parsing **************************/
88/************************************************************************/
89
90/* returns true if this character is a specifier alignment token */
91Py_LOCAL_INLINE(int)
92is_alignment_token(STRINGLIB_CHAR c)
93{
94 switch (c) {
95 case '<': case '>': case '=': case '^':
96 return 1;
97 default:
98 return 0;
99 }
100}
101
102/* returns true if this character is a sign element */
103Py_LOCAL_INLINE(int)
104is_sign_element(STRINGLIB_CHAR c)
105{
106 switch (c) {
Eric Smithb7f5ba12007-08-29 12:38:45 +0000107 case ' ': case '+': case '-':
Eric Smith8c663262007-08-25 02:26:07 +0000108 return 1;
109 default:
110 return 0;
111 }
112}
113
114
115typedef struct {
116 STRINGLIB_CHAR fill_char;
117 STRINGLIB_CHAR align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000118 int alternate;
Eric Smith8c663262007-08-25 02:26:07 +0000119 STRINGLIB_CHAR sign;
120 Py_ssize_t width;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000121 int thousands_separators;
Eric Smith8c663262007-08-25 02:26:07 +0000122 Py_ssize_t precision;
123 STRINGLIB_CHAR type;
124} InternalFormatSpec;
125
Eric Smith903fc052010-02-22 19:26:06 +0000126
127#if 0
128/* Occassionally useful for debugging. Should normally be commented out. */
129static void
130DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
131{
132 printf("internal format spec: fill_char %d\n", format->fill_char);
133 printf("internal format spec: align %d\n", format->align);
134 printf("internal format spec: alternate %d\n", format->alternate);
135 printf("internal format spec: sign %d\n", format->sign);
Eric Smith53f2f2e2010-02-23 00:37:54 +0000136 printf("internal format spec: width %zd\n", format->width);
Eric Smith903fc052010-02-22 19:26:06 +0000137 printf("internal format spec: thousands_separators %d\n",
138 format->thousands_separators);
Eric Smith53f2f2e2010-02-23 00:37:54 +0000139 printf("internal format spec: precision %zd\n", format->precision);
Eric Smith903fc052010-02-22 19:26:06 +0000140 printf("internal format spec: type %c\n", format->type);
141 printf("\n");
142}
143#endif
144
145
Eric Smith8c663262007-08-25 02:26:07 +0000146/*
147 ptr points to the start of the format_spec, end points just past its end.
148 fills in format with the parsed information.
149 returns 1 on success, 0 on failure.
150 if failure, sets the exception
151*/
152static int
Eric Smith4a7d76d2008-05-30 18:10:19 +0000153parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000154 Py_ssize_t format_spec_len,
Eric Smith8c663262007-08-25 02:26:07 +0000155 InternalFormatSpec *format,
Eric Smith903fc052010-02-22 19:26:06 +0000156 char default_type,
157 char default_align)
Eric Smith8c663262007-08-25 02:26:07 +0000158{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000159 STRINGLIB_CHAR *ptr = format_spec;
160 STRINGLIB_CHAR *end = format_spec + format_spec_len;
Eric Smith8c663262007-08-25 02:26:07 +0000161
162 /* end-ptr is used throughout this code to specify the length of
163 the input string */
164
Eric Smith0923d1d2009-04-16 20:16:10 +0000165 Py_ssize_t consumed;
Eric Smith53f2f2e2010-02-23 00:37:54 +0000166 int align_specified = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000167
168 format->fill_char = '\0';
Eric Smith903fc052010-02-22 19:26:06 +0000169 format->align = default_align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000170 format->alternate = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000171 format->sign = '\0';
172 format->width = -1;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000173 format->thousands_separators = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000174 format->precision = -1;
175 format->type = default_type;
176
177 /* If the second char is an alignment token,
178 then parse the fill char */
179 if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
180 format->align = ptr[1];
181 format->fill_char = ptr[0];
Eric Smith53f2f2e2010-02-23 00:37:54 +0000182 align_specified = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000183 ptr += 2;
Eric Smith0cb431c2007-08-28 01:07:27 +0000184 }
185 else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
Eric Smith8c663262007-08-25 02:26:07 +0000186 format->align = ptr[0];
Eric Smith53f2f2e2010-02-23 00:37:54 +0000187 align_specified = 1;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000188 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000189 }
190
191 /* Parse the various sign options */
192 if (end-ptr >= 1 && is_sign_element(ptr[0])) {
193 format->sign = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000194 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000195 }
196
Eric Smithd68af8f2008-07-16 00:15:35 +0000197 /* If the next character is #, we're in alternate mode. This only
198 applies to integers. */
199 if (end-ptr >= 1 && ptr[0] == '#') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000200 format->alternate = 1;
201 ++ptr;
Eric Smithd68af8f2008-07-16 00:15:35 +0000202 }
203
Eric Smith8c663262007-08-25 02:26:07 +0000204 /* The special case for 0-padding (backwards compat) */
Eric Smith185e30c2007-08-30 22:23:08 +0000205 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
Eric Smith8c663262007-08-25 02:26:07 +0000206 format->fill_char = '0';
Eric Smith53f2f2e2010-02-23 00:37:54 +0000207 if (!align_specified) {
Eric Smith8c663262007-08-25 02:26:07 +0000208 format->align = '=';
209 }
Christian Heimesc3f30c42008-02-22 16:37:40 +0000210 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000211 }
212
Eric Smith0923d1d2009-04-16 20:16:10 +0000213 consumed = get_integer(&ptr, end, &format->width);
214 if (consumed == -1)
215 /* Overflow error. Exception already set. */
216 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000217
Eric Smith0923d1d2009-04-16 20:16:10 +0000218 /* If consumed is 0, we didn't consume any characters for the
219 width. In that case, reset the width to -1, because
220 get_integer() will have set it to zero. -1 is how we record
221 that the width wasn't specified. */
222 if (consumed == 0)
Eric Smith8c663262007-08-25 02:26:07 +0000223 format->width = -1;
Eric Smith8c663262007-08-25 02:26:07 +0000224
Eric Smitha3b1ac82009-04-03 14:45:06 +0000225 /* Comma signifies add thousands separators */
226 if (end-ptr && ptr[0] == ',') {
227 format->thousands_separators = 1;
228 ++ptr;
229 }
230
Eric Smith8c663262007-08-25 02:26:07 +0000231 /* Parse field precision */
232 if (end-ptr && ptr[0] == '.') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000233 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000234
Eric Smith0923d1d2009-04-16 20:16:10 +0000235 consumed = get_integer(&ptr, end, &format->precision);
236 if (consumed == -1)
237 /* Overflow error. Exception already set. */
238 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000239
Eric Smith0923d1d2009-04-16 20:16:10 +0000240 /* Not having a precision after a dot is an error. */
241 if (consumed == 0) {
Eric Smith8c663262007-08-25 02:26:07 +0000242 PyErr_Format(PyExc_ValueError,
243 "Format specifier missing precision");
244 return 0;
245 }
246
247 }
248
Eric Smith0923d1d2009-04-16 20:16:10 +0000249 /* Finally, parse the type field. */
Eric Smith8c663262007-08-25 02:26:07 +0000250
251 if (end-ptr > 1) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000252 /* More than one char remain, invalid conversion spec. */
Eric Smith8c663262007-08-25 02:26:07 +0000253 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
254 return 0;
255 }
256
257 if (end-ptr == 1) {
258 format->type = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000259 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000260 }
261
Eric Smith0923d1d2009-04-16 20:16:10 +0000262 /* Do as much validating as we can, just by looking at the format
263 specifier. Do not take into account what type of formatting
264 we're doing (int, float, string). */
265
266 if (format->thousands_separators) {
267 switch (format->type) {
268 case 'd':
269 case 'e':
270 case 'f':
271 case 'g':
272 case 'E':
273 case 'G':
274 case '%':
275 case 'F':
Eric Smith937491d2009-04-22 17:04:27 +0000276 case '\0':
Eric Smith0923d1d2009-04-16 20:16:10 +0000277 /* These are allowed. See PEP 378.*/
278 break;
279 default:
280 PyErr_Format(PyExc_ValueError,
281 "Cannot specify ',' with '%c'.", format->type);
282 return 0;
283 }
Eric Smitha3b1ac82009-04-03 14:45:06 +0000284 }
285
Eric Smith8c663262007-08-25 02:26:07 +0000286 return 1;
287}
288
Eric Smith58a42242009-04-30 01:00:33 +0000289/* Calculate the padding needed. */
290static void
291calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
292 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
293 Py_ssize_t *n_total)
294{
295 if (width >= 0) {
296 if (nchars > width)
297 *n_total = nchars;
298 else
299 *n_total = width;
300 }
301 else {
302 /* not specified, use all of the chars and no more */
303 *n_total = nchars;
304 }
305
Eric Smith903fc052010-02-22 19:26:06 +0000306 /* Figure out how much leading space we need, based on the
Eric Smith58a42242009-04-30 01:00:33 +0000307 aligning */
308 if (align == '>')
309 *n_lpadding = *n_total - nchars;
310 else if (align == '^')
311 *n_lpadding = (*n_total - nchars) / 2;
Eric Smith903fc052010-02-22 19:26:06 +0000312 else if (align == '<' || align == '=')
Eric Smith58a42242009-04-30 01:00:33 +0000313 *n_lpadding = 0;
Eric Smith903fc052010-02-22 19:26:06 +0000314 else {
315 /* We should never have an unspecified alignment. */
316 *n_lpadding = 0;
317 assert(0);
318 }
Eric Smith58a42242009-04-30 01:00:33 +0000319
320 *n_rpadding = *n_total - nchars - *n_lpadding;
321}
322
323/* Do the padding, and return a pointer to where the caller-supplied
324 content goes. */
325static STRINGLIB_CHAR *
326fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
327 Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
328{
329 /* Pad on left. */
330 if (n_lpadding)
331 STRINGLIB_FILL(p, fill_char, n_lpadding);
332
333 /* Pad on right. */
334 if (n_rpadding)
335 STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
336
337 /* Pointer to the user content. */
338 return p + n_lpadding;
339}
340
341#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
Eric Smith8c663262007-08-25 02:26:07 +0000342/************************************************************************/
343/*********** common routines for numeric formatting *********************/
344/************************************************************************/
345
Eric Smith0923d1d2009-04-16 20:16:10 +0000346/* Locale type codes. */
347#define LT_CURRENT_LOCALE 0
348#define LT_DEFAULT_LOCALE 1
349#define LT_NO_LOCALE 2
350
351/* Locale info needed for formatting integers and the part of floats
352 before and including the decimal. Note that locales only support
353 8-bit chars, not unicode. */
354typedef struct {
355 char *decimal_point;
356 char *thousands_sep;
357 char *grouping;
358} LocaleInfo;
359
Eric Smith8c663262007-08-25 02:26:07 +0000360/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000361 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000362typedef struct {
363 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000364 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000365 Py_ssize_t n_spadding;
366 Py_ssize_t n_rpadding;
Eric Smith0923d1d2009-04-16 20:16:10 +0000367 char sign;
368 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
369 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
370 any grouping chars. */
371 Py_ssize_t n_decimal; /* 0 if only an integer */
372 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
373 excluding the decimal itself, if
374 present. */
375
376 /* These 2 are not the widths of fields, but are needed by
377 STRINGLIB_GROUPING. */
378 Py_ssize_t n_digits; /* The number of digits before a decimal
379 or exponent. */
380 Py_ssize_t n_min_width; /* The min_width we used when we computed
381 the n_grouped_digits width. */
Eric Smith8c663262007-08-25 02:26:07 +0000382} NumberFieldWidths;
383
Eric Smith58a42242009-04-30 01:00:33 +0000384
Eric Smith0923d1d2009-04-16 20:16:10 +0000385/* Given a number of the form:
386 digits[remainder]
387 where ptr points to the start and end points to the end, find where
388 the integer part ends. This could be a decimal, an exponent, both,
389 or neither.
390 If a decimal point is present, set *has_decimal and increment
391 remainder beyond it.
392 Results are undefined (but shouldn't crash) for improperly
393 formatted strings.
394*/
395static void
396parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
397 Py_ssize_t *n_remainder, int *has_decimal)
398{
399 STRINGLIB_CHAR *end = ptr + len;
400 STRINGLIB_CHAR *remainder;
401
402 while (ptr<end && isdigit(*ptr))
403 ++ptr;
404 remainder = ptr;
405
406 /* Does remainder start with a decimal point? */
407 *has_decimal = ptr<end && *remainder == '.';
408
409 /* Skip the decimal point. */
410 if (*has_decimal)
411 remainder++;
412
413 *n_remainder = end - remainder;
414}
415
Eric Smith8c663262007-08-25 02:26:07 +0000416/* not all fields of format are used. for example, precision is
417 unused. should this take discrete params in order to be more clear
418 about what it does? or is passing a single format parameter easier
419 and more efficient enough to justify a little obfuscation? */
Eric Smith0923d1d2009-04-16 20:16:10 +0000420static Py_ssize_t
421calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
422 STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
423 Py_ssize_t n_number, Py_ssize_t n_remainder,
424 int has_decimal, const LocaleInfo *locale,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000425 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000426{
Eric Smith0923d1d2009-04-16 20:16:10 +0000427 Py_ssize_t n_non_digit_non_padding;
428 Py_ssize_t n_padding;
429
430 spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
Eric Smith05212a12008-07-16 19:41:14 +0000431 spec->n_lpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000432 spec->n_prefix = n_prefix;
433 spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
434 spec->n_remainder = n_remainder;
Eric Smith05212a12008-07-16 19:41:14 +0000435 spec->n_spadding = 0;
436 spec->n_rpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000437 spec->sign = '\0';
438 spec->n_sign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000439
440 /* the output will look like:
Eric Smith0923d1d2009-04-16 20:16:10 +0000441 | |
442 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
443 | |
Eric Smith8c663262007-08-25 02:26:07 +0000444
Eric Smith0923d1d2009-04-16 20:16:10 +0000445 sign is computed from format->sign and the actual
Eric Smith8c663262007-08-25 02:26:07 +0000446 sign of the number
447
Eric Smithb1ebcc62008-07-15 13:02:41 +0000448 prefix is given (it's for the '0x' prefix)
449
Eric Smith8c663262007-08-25 02:26:07 +0000450 digits is already known
451
452 the total width is either given, or computed from the
453 actual digits
454
455 only one of lpadding, spadding, and rpadding can be non-zero,
456 and it's calculated from the width and other fields
457 */
458
459 /* compute the various parts we're going to write */
Eric Smith0923d1d2009-04-16 20:16:10 +0000460 switch (format->sign) {
461 case '+':
Eric Smith8c663262007-08-25 02:26:07 +0000462 /* always put a + or - */
Eric Smith0923d1d2009-04-16 20:16:10 +0000463 spec->n_sign = 1;
464 spec->sign = (sign_char == '-' ? '-' : '+');
465 break;
466 case ' ':
467 spec->n_sign = 1;
468 spec->sign = (sign_char == '-' ? '-' : ' ');
469 break;
470 default:
471 /* Not specified, or the default (-) */
472 if (sign_char == '-') {
473 spec->n_sign = 1;
474 spec->sign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000475 }
476 }
477
Eric Smith0923d1d2009-04-16 20:16:10 +0000478 /* The number of chars used for non-digits and non-padding. */
479 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
480 spec->n_remainder;
Eric Smithd68af8f2008-07-16 00:15:35 +0000481
Eric Smith0923d1d2009-04-16 20:16:10 +0000482 /* min_width can go negative, that's okay. format->width == -1 means
483 we don't care. */
Eric Smith53f2f2e2010-02-23 00:37:54 +0000484 if (format->fill_char == '0' && format->align == '=')
Eric Smith0923d1d2009-04-16 20:16:10 +0000485 spec->n_min_width = format->width - n_non_digit_non_padding;
486 else
487 spec->n_min_width = 0;
488
489 if (spec->n_digits == 0)
490 /* This case only occurs when using 'c' formatting, we need
491 to special case it because the grouping code always wants
492 to have at least one character. */
493 spec->n_grouped_digits = 0;
494 else
495 spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
496 spec->n_digits,
497 spec->n_min_width,
498 locale->grouping,
499 locale->thousands_sep);
500
501 /* Given the desired width and the total of digit and non-digit
502 space we consume, see if we need any padding. format->width can
503 be negative (meaning no padding), but this code still works in
504 that case. */
505 n_padding = format->width -
506 (n_non_digit_non_padding + spec->n_grouped_digits);
507 if (n_padding > 0) {
508 /* Some padding is needed. Determine if it's left, space, or right. */
509 switch (format->align) {
510 case '<':
511 spec->n_rpadding = n_padding;
512 break;
513 case '^':
514 spec->n_lpadding = n_padding / 2;
515 spec->n_rpadding = n_padding - spec->n_lpadding;
516 break;
517 case '=':
518 spec->n_spadding = n_padding;
519 break;
Eric Smith903fc052010-02-22 19:26:06 +0000520 case '>':
Eric Smith0923d1d2009-04-16 20:16:10 +0000521 spec->n_lpadding = n_padding;
522 break;
Eric Smith903fc052010-02-22 19:26:06 +0000523 default:
524 /* Shouldn't get here, but treat it as '>' */
525 spec->n_lpadding = n_padding;
526 assert(0);
527 break;
Eric Smith8c663262007-08-25 02:26:07 +0000528 }
529 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000530 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
531 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
532 spec->n_remainder + spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000533}
534
Eric Smith0923d1d2009-04-16 20:16:10 +0000535/* Fill in the digit parts of a numbers's string representation,
536 as determined in calc_number_widths().
537 No error checking, since we know the buffer is the correct size. */
538static void
539fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
540 STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
541 STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
542 LocaleInfo *locale, int toupper)
Eric Smith8c663262007-08-25 02:26:07 +0000543{
Eric Smith0923d1d2009-04-16 20:16:10 +0000544 /* Used to keep track of digits, decimal, and remainder. */
545 STRINGLIB_CHAR *p = digits;
546
547#ifndef NDEBUG
548 Py_ssize_t r;
549#endif
Eric Smith8c663262007-08-25 02:26:07 +0000550
551 if (spec->n_lpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000552 STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
553 buf += spec->n_lpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000554 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000555 if (spec->n_sign == 1) {
556 *buf++ = spec->sign;
Eric Smith8c663262007-08-25 02:26:07 +0000557 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000558 if (spec->n_prefix) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000559 memmove(buf,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000560 prefix,
561 spec->n_prefix * sizeof(STRINGLIB_CHAR));
Eric Smith0923d1d2009-04-16 20:16:10 +0000562 if (toupper) {
563 Py_ssize_t t;
564 for (t = 0; t < spec->n_prefix; ++t)
565 buf[t] = STRINGLIB_TOUPPER(buf[t]);
566 }
567 buf += spec->n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000568 }
Eric Smith8c663262007-08-25 02:26:07 +0000569 if (spec->n_spadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000570 STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
571 buf += spec->n_spadding;
Eric Smith8c663262007-08-25 02:26:07 +0000572 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000573
574 /* Only for type 'c' special case, it has no digits. */
575 if (spec->n_digits != 0) {
576 /* Fill the digits with InsertThousandsGrouping. */
577#ifndef NDEBUG
578 r =
579#endif
580 STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
581 spec->n_digits, spec->n_min_width,
582 locale->grouping, locale->thousands_sep);
583#ifndef NDEBUG
584 assert(r == spec->n_grouped_digits);
585#endif
586 p += spec->n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000587 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000588 if (toupper) {
589 Py_ssize_t t;
590 for (t = 0; t < spec->n_grouped_digits; ++t)
591 buf[t] = STRINGLIB_TOUPPER(buf[t]);
592 }
593 buf += spec->n_grouped_digits;
594
595 if (spec->n_decimal) {
596 Py_ssize_t t;
597 for (t = 0; t < spec->n_decimal; ++t)
598 buf[t] = locale->decimal_point[t];
599 buf += spec->n_decimal;
600 p += 1;
601 }
602
603 if (spec->n_remainder) {
604 memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
605 buf += spec->n_remainder;
606 p += spec->n_remainder;
607 }
608
Eric Smith8c663262007-08-25 02:26:07 +0000609 if (spec->n_rpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000610 STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
611 buf += spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000612 }
Eric Smith8c663262007-08-25 02:26:07 +0000613}
Eric Smith0923d1d2009-04-16 20:16:10 +0000614
615static char no_grouping[1] = {CHAR_MAX};
616
617/* Find the decimal point character(s?), thousands_separator(s?), and
618 grouping description, either for the current locale if type is
619 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
620 none if LT_NO_LOCALE. */
621static void
622get_locale_info(int type, LocaleInfo *locale_info)
623{
624 switch (type) {
625 case LT_CURRENT_LOCALE: {
626 struct lconv *locale_data = localeconv();
627 locale_info->decimal_point = locale_data->decimal_point;
628 locale_info->thousands_sep = locale_data->thousands_sep;
629 locale_info->grouping = locale_data->grouping;
630 break;
631 }
632 case LT_DEFAULT_LOCALE:
633 locale_info->decimal_point = ".";
634 locale_info->thousands_sep = ",";
635 locale_info->grouping = "\3"; /* Group every 3 characters,
636 trailing 0 means repeat
637 infinitely. */
638 break;
639 case LT_NO_LOCALE:
640 locale_info->decimal_point = ".";
641 locale_info->thousands_sep = "";
642 locale_info->grouping = no_grouping;
643 break;
644 default:
645 assert(0);
646 }
647}
648
Eric Smith58a42242009-04-30 01:00:33 +0000649#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
Eric Smith8c663262007-08-25 02:26:07 +0000650
651/************************************************************************/
652/*********** string formatting ******************************************/
653/************************************************************************/
654
655static PyObject *
656format_string_internal(PyObject *value, const InternalFormatSpec *format)
657{
Eric Smith8c663262007-08-25 02:26:07 +0000658 Py_ssize_t lpad;
Eric Smith58a42242009-04-30 01:00:33 +0000659 Py_ssize_t rpad;
660 Py_ssize_t total;
661 STRINGLIB_CHAR *p;
Eric Smith8c663262007-08-25 02:26:07 +0000662 Py_ssize_t len = STRINGLIB_LEN(value);
663 PyObject *result = NULL;
664
665 /* sign is not allowed on strings */
666 if (format->sign != '\0') {
667 PyErr_SetString(PyExc_ValueError,
668 "Sign not allowed in string format specifier");
669 goto done;
670 }
671
Eric Smithb1ebcc62008-07-15 13:02:41 +0000672 /* alternate is not allowed on strings */
673 if (format->alternate) {
674 PyErr_SetString(PyExc_ValueError,
675 "Alternate form (#) not allowed in string format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000676 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000677 goto done;
678 }
679
Eric Smith8c663262007-08-25 02:26:07 +0000680 /* '=' alignment not allowed on strings */
681 if (format->align == '=') {
682 PyErr_SetString(PyExc_ValueError,
683 "'=' alignment not allowed "
684 "in string format specifier");
685 goto done;
686 }
687
688 /* if precision is specified, output no more that format.precision
689 characters */
690 if (format->precision >= 0 && len >= format->precision) {
691 len = format->precision;
692 }
693
Eric Smith58a42242009-04-30 01:00:33 +0000694 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
Eric Smith8c663262007-08-25 02:26:07 +0000695
696 /* allocate the resulting string */
Eric Smith58a42242009-04-30 01:00:33 +0000697 result = STRINGLIB_NEW(NULL, total);
Eric Smith8c663262007-08-25 02:26:07 +0000698 if (result == NULL)
699 goto done;
700
Eric Smith58a42242009-04-30 01:00:33 +0000701 /* Write into that space. First the padding. */
702 p = fill_padding(STRINGLIB_STR(result), len,
703 format->fill_char=='\0'?' ':format->fill_char,
704 lpad, rpad);
Eric Smith8c663262007-08-25 02:26:07 +0000705
Eric Smith58a42242009-04-30 01:00:33 +0000706 /* Then the source string. */
707 memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
Eric Smith8c663262007-08-25 02:26:07 +0000708
709done:
710 return result;
711}
712
713
714/************************************************************************/
715/*********** long formatting ********************************************/
716/************************************************************************/
717
Eric Smith8fd3eba2008-02-17 19:48:00 +0000718#if defined FORMAT_LONG || defined FORMAT_INT
719typedef PyObject*
720(*IntOrLongToString)(PyObject *value, int base);
721
Eric Smith8c663262007-08-25 02:26:07 +0000722static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000723format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000724 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000725{
726 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000727 PyObject *tmp = NULL;
728 STRINGLIB_CHAR *pnumeric_chars;
729 STRINGLIB_CHAR numeric_char;
Eric Smith0923d1d2009-04-16 20:16:10 +0000730 STRINGLIB_CHAR sign_char = '\0';
Eric Smith8c663262007-08-25 02:26:07 +0000731 Py_ssize_t n_digits; /* count of digits need from the computed
732 string */
Eric Smith0923d1d2009-04-16 20:16:10 +0000733 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
734 produces non-digits */
Eric Smithd68af8f2008-07-16 00:15:35 +0000735 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
Eric Smith0923d1d2009-04-16 20:16:10 +0000736 Py_ssize_t n_total;
Eric Smithd68af8f2008-07-16 00:15:35 +0000737 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000738 NumberFieldWidths spec;
739 long x;
740
Eric Smith0923d1d2009-04-16 20:16:10 +0000741 /* Locale settings, either from the actual locale or
742 from a hard-code pseudo-locale */
743 LocaleInfo locale;
744
Eric Smith8c663262007-08-25 02:26:07 +0000745 /* no precision allowed on integers */
746 if (format->precision != -1) {
747 PyErr_SetString(PyExc_ValueError,
748 "Precision not allowed in integer format specifier");
749 goto done;
750 }
751
Eric Smith8c663262007-08-25 02:26:07 +0000752 /* special case for character formatting */
753 if (format->type == 'c') {
754 /* error to specify a sign */
755 if (format->sign != '\0') {
756 PyErr_SetString(PyExc_ValueError,
757 "Sign not allowed with integer"
758 " format specifier 'c'");
759 goto done;
760 }
761
Eric Smith0923d1d2009-04-16 20:16:10 +0000762 /* Error to specify a comma. */
763 if (format->thousands_separators) {
764 PyErr_SetString(PyExc_ValueError,
765 "Thousands separators not allowed with integer"
766 " format specifier 'c'");
767 goto done;
768 }
769
Eric Smith8c663262007-08-25 02:26:07 +0000770 /* taken from unicodeobject.c formatchar() */
771 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000772/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000773 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000774 if (x == -1 && PyErr_Occurred())
775 goto done;
776#ifdef Py_UNICODE_WIDE
777 if (x < 0 || x > 0x10ffff) {
778 PyErr_SetString(PyExc_OverflowError,
779 "%c arg not in range(0x110000) "
780 "(wide Python build)");
781 goto done;
782 }
783#else
784 if (x < 0 || x > 0xffff) {
785 PyErr_SetString(PyExc_OverflowError,
786 "%c arg not in range(0x10000) "
787 "(narrow Python build)");
788 goto done;
789 }
790#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000791 numeric_char = (STRINGLIB_CHAR)x;
792 pnumeric_chars = &numeric_char;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000793 n_digits = 1;
Eric Smith0923d1d2009-04-16 20:16:10 +0000794
795 /* As a sort-of hack, we tell calc_number_widths that we only
796 have "remainder" characters. calc_number_widths thinks
797 these are characters that don't get formatted, only copied
798 into the output string. We do this for 'c' formatting,
799 because the characters are likely to be non-digits. */
800 n_remainder = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000801 }
802 else {
Eric Smith8c663262007-08-25 02:26:07 +0000803 int base;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000804 int leading_chars_to_skip = 0; /* Number of characters added by
805 PyNumber_ToBase that we want to
806 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000807
808 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000809 PyNumber_ToBase */
810 switch (format->type) {
811 case 'b':
812 base = 2;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000813 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000814 break;
815 case 'o':
816 base = 8;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000817 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000818 break;
819 case 'x':
820 case 'X':
821 base = 16;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000822 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000823 break;
824 default: /* shouldn't be needed, but stops a compiler warning */
825 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000826 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000827 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000828 break;
829 }
830
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000831 /* The number of prefix chars is the same as the leading
832 chars to skip */
833 if (format->alternate)
834 n_prefix = leading_chars_to_skip;
Eric Smithd68af8f2008-07-16 00:15:35 +0000835
Eric Smith8fd3eba2008-02-17 19:48:00 +0000836 /* Do the hard part, converting to a string in a given base */
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000837 tmp = tostring(value, base);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000838 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000839 goto done;
840
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000841 pnumeric_chars = STRINGLIB_STR(tmp);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000842 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000843
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000844 prefix = pnumeric_chars;
Eric Smithd68af8f2008-07-16 00:15:35 +0000845
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000846 /* Remember not to modify what pnumeric_chars points to. it
847 might be interned. Only modify it after we copy it into a
848 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000849
Eric Smith8fd3eba2008-02-17 19:48:00 +0000850 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000851 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000852 if (pnumeric_chars[0] == '-') {
853 sign_char = pnumeric_chars[0];
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000854 ++prefix;
855 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000856 }
857
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000858 /* Skip over the leading chars (0x, 0b, etc.) */
859 n_digits -= leading_chars_to_skip;
860 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000861 }
862
Eric Smith0923d1d2009-04-16 20:16:10 +0000863 /* Determine the grouping, separator, and decimal point, if any. */
864 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
865 (format->thousands_separators ?
866 LT_DEFAULT_LOCALE :
867 LT_NO_LOCALE),
868 &locale);
Eric Smith5807c412008-05-11 21:00:57 +0000869
Eric Smith0923d1d2009-04-16 20:16:10 +0000870 /* Calculate how much memory we'll need. */
871 n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
872 n_digits, n_remainder, 0, &locale, format);
Eric Smithb151a452008-06-24 11:21:04 +0000873
Eric Smith0923d1d2009-04-16 20:16:10 +0000874 /* Allocate the memory. */
875 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000876 if (!result)
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000877 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000878
Eric Smith0923d1d2009-04-16 20:16:10 +0000879 /* Populate the memory. */
880 fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
881 prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
882 &locale, format->type == 'X');
Eric Smithd68af8f2008-07-16 00:15:35 +0000883
Eric Smith8c663262007-08-25 02:26:07 +0000884done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000885 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000886 return result;
887}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000888#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000889
890/************************************************************************/
891/*********** float formatting *******************************************/
892/************************************************************************/
893
Eric Smith8fd3eba2008-02-17 19:48:00 +0000894#ifdef FORMAT_FLOAT
895#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000896static void
897strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
Eric Smith8c663262007-08-25 02:26:07 +0000898{
Eric Smith0923d1d2009-04-16 20:16:10 +0000899 Py_ssize_t i;
900 for (i = 0; i < len; ++i)
901 buffer[i] = (Py_UNICODE)charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000902}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000903#endif
Eric Smith8c663262007-08-25 02:26:07 +0000904
Eric Smith8c663262007-08-25 02:26:07 +0000905/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000906static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000907format_float_internal(PyObject *value,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000908 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000909{
Eric Smith0923d1d2009-04-16 20:16:10 +0000910 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
Eric Smith8c663262007-08-25 02:26:07 +0000911 Py_ssize_t n_digits;
Eric Smith0923d1d2009-04-16 20:16:10 +0000912 Py_ssize_t n_remainder;
913 Py_ssize_t n_total;
914 int has_decimal;
915 double val;
Eric Smith8c663262007-08-25 02:26:07 +0000916 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +0000917 Py_ssize_t default_precision = 6;
Eric Smith0923d1d2009-04-16 20:16:10 +0000918 STRINGLIB_CHAR type = format->type;
919 int add_pct = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000920 STRINGLIB_CHAR *p;
921 NumberFieldWidths spec;
Eric Smith0923d1d2009-04-16 20:16:10 +0000922 int flags = 0;
923 PyObject *result = NULL;
924 STRINGLIB_CHAR sign_char = '\0';
925 int float_type; /* Used to see if we have a nan, inf, or regular float. */
Eric Smith8c663262007-08-25 02:26:07 +0000926
927#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000928 Py_UNICODE *unicode_tmp = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000929#endif
930
Eric Smith0923d1d2009-04-16 20:16:10 +0000931 /* Locale settings, either from the actual locale or
932 from a hard-code pseudo-locale */
933 LocaleInfo locale;
934
935 /* Alternate is not allowed on floats. */
Eric Smithb1ebcc62008-07-15 13:02:41 +0000936 if (format->alternate) {
937 PyErr_SetString(PyExc_ValueError,
938 "Alternate form (#) not allowed in float format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000939 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000940 goto done;
941 }
942
Eric Smith0923d1d2009-04-16 20:16:10 +0000943 if (type == '\0') {
Eric Smith63376222009-05-05 14:04:18 +0000944 /* Omitted type specifier. This is like 'g' but with at least one
945 digit after the decimal point, and different default precision.*/
Eric Smith0923d1d2009-04-16 20:16:10 +0000946 type = 'g';
Eric Smith63376222009-05-05 14:04:18 +0000947 default_precision = PyFloat_STR_PRECISION;
Eric Smith0923d1d2009-04-16 20:16:10 +0000948 flags |= Py_DTSF_ADD_DOT_0;
949 }
950
951 if (type == 'n')
952 /* 'n' is the same as 'g', except for the locale used to
953 format the result. We take care of that later. */
954 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000955
Eric Smith7bc66b12009-07-27 02:12:11 +0000956#if PY_VERSION_HEX < 0x0301000
957 /* 'F' is the same as 'f', per the PEP */
958 /* This is no longer the case in 3.x */
959 if (type == 'F')
960 type = 'f';
961#endif
962
Eric Smith0923d1d2009-04-16 20:16:10 +0000963 val = PyFloat_AsDouble(value);
964 if (val == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000965 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000966
967 if (type == '%') {
968 type = 'f';
Eric Smith0923d1d2009-04-16 20:16:10 +0000969 val *= 100;
970 add_pct = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000971 }
972
973 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +0000974 precision = default_precision;
Eric Smith8c663262007-08-25 02:26:07 +0000975
Eric Smith7255f182009-05-02 12:15:39 +0000976#if PY_VERSION_HEX < 0x03010000
977 /* 3.1 no longer converts large 'f' to 'g'. */
Eric Smith7bc66b12009-07-27 02:12:11 +0000978 if ((type == 'f' || type == 'F') && fabs(val) >= 1e50)
979 type = 'g';
Eric Smith7255f182009-05-02 12:15:39 +0000980#endif
981
Eric Smith0923d1d2009-04-16 20:16:10 +0000982 /* Cast "type", because if we're in unicode we need to pass a
983 8-bit char. This is safe, because we've restricted what "type"
984 can be. */
985 buf = PyOS_double_to_string(val, (char)type, precision, flags,
986 &float_type);
987 if (buf == NULL)
988 goto done;
989 n_digits = strlen(buf);
Eric Smith8c663262007-08-25 02:26:07 +0000990
Eric Smith0923d1d2009-04-16 20:16:10 +0000991 if (add_pct) {
992 /* We know that buf has a trailing zero (since we just called
993 strlen() on it), and we don't use that fact any more. So we
994 can just write over the trailing zero. */
995 buf[n_digits] = '%';
996 n_digits += 1;
997 }
Eric Smith8c663262007-08-25 02:26:07 +0000998
Eric Smith0923d1d2009-04-16 20:16:10 +0000999 /* Since there is no unicode version of PyOS_double_to_string,
1000 just use the 8 bit version and then convert to unicode. */
Eric Smith8c663262007-08-25 02:26:07 +00001001#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +00001002 unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
1003 if (unicode_tmp == NULL) {
1004 PyErr_NoMemory();
1005 goto done;
1006 }
1007 strtounicode(unicode_tmp, buf, n_digits);
1008 p = unicode_tmp;
Eric Smith8c663262007-08-25 02:26:07 +00001009#else
Eric Smith0923d1d2009-04-16 20:16:10 +00001010 p = buf;
Eric Smith8c663262007-08-25 02:26:07 +00001011#endif
1012
Eric Smith0923d1d2009-04-16 20:16:10 +00001013 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +00001014 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +00001015 if (*p == '-') {
1016 sign_char = *p;
Christian Heimesc3f30c42008-02-22 16:37:40 +00001017 ++p;
1018 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +00001019 }
1020
Eric Smith0923d1d2009-04-16 20:16:10 +00001021 /* Determine if we have any "remainder" (after the digits, might include
1022 decimal or exponent or both (or neither)) */
1023 parse_number(p, n_digits, &n_remainder, &has_decimal);
Eric Smith8c663262007-08-25 02:26:07 +00001024
Eric Smith0923d1d2009-04-16 20:16:10 +00001025 /* Determine the grouping, separator, and decimal point, if any. */
1026 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1027 (format->thousands_separators ?
1028 LT_DEFAULT_LOCALE :
1029 LT_NO_LOCALE),
1030 &locale);
1031
1032 /* Calculate how much memory we'll need. */
1033 n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
1034 n_remainder, has_decimal, &locale, format);
1035
1036 /* Allocate the memory. */
1037 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8c663262007-08-25 02:26:07 +00001038 if (result == NULL)
1039 goto done;
1040
Eric Smith0923d1d2009-04-16 20:16:10 +00001041 /* Populate the memory. */
1042 fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
1043 format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
1044 0);
Eric Smith8c663262007-08-25 02:26:07 +00001045
1046done:
Eric Smith0923d1d2009-04-16 20:16:10 +00001047 PyMem_Free(buf);
1048#if STRINGLIB_IS_UNICODE
1049 PyMem_Free(unicode_tmp);
1050#endif
Eric Smith8c663262007-08-25 02:26:07 +00001051 return result;
1052}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001053#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +00001054
1055/************************************************************************/
Eric Smith58a42242009-04-30 01:00:33 +00001056/*********** complex formatting *****************************************/
1057/************************************************************************/
1058
1059#ifdef FORMAT_COMPLEX
1060
1061static PyObject *
1062format_complex_internal(PyObject *value,
1063 const InternalFormatSpec *format)
1064{
1065 double re;
1066 double im;
1067 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1068 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1069
1070 InternalFormatSpec tmp_format = *format;
1071 Py_ssize_t n_re_digits;
1072 Py_ssize_t n_im_digits;
1073 Py_ssize_t n_re_remainder;
1074 Py_ssize_t n_im_remainder;
1075 Py_ssize_t n_re_total;
1076 Py_ssize_t n_im_total;
1077 int re_has_decimal;
1078 int im_has_decimal;
1079 Py_ssize_t precision = format->precision;
Eric Smith63376222009-05-05 14:04:18 +00001080 Py_ssize_t default_precision = 6;
Eric Smith58a42242009-04-30 01:00:33 +00001081 STRINGLIB_CHAR type = format->type;
1082 STRINGLIB_CHAR *p_re;
1083 STRINGLIB_CHAR *p_im;
1084 NumberFieldWidths re_spec;
1085 NumberFieldWidths im_spec;
1086 int flags = 0;
1087 PyObject *result = NULL;
1088 STRINGLIB_CHAR *p;
1089 STRINGLIB_CHAR re_sign_char = '\0';
1090 STRINGLIB_CHAR im_sign_char = '\0';
1091 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1092 int im_float_type;
1093 int add_parens = 0;
1094 int skip_re = 0;
1095 Py_ssize_t lpad;
1096 Py_ssize_t rpad;
1097 Py_ssize_t total;
1098
1099#if STRINGLIB_IS_UNICODE
1100 Py_UNICODE *re_unicode_tmp = NULL;
1101 Py_UNICODE *im_unicode_tmp = NULL;
1102#endif
1103
1104 /* Locale settings, either from the actual locale or
1105 from a hard-code pseudo-locale */
1106 LocaleInfo locale;
1107
1108 /* Alternate is not allowed on complex. */
1109 if (format->alternate) {
1110 PyErr_SetString(PyExc_ValueError,
1111 "Alternate form (#) not allowed in complex format "
1112 "specifier");
1113 goto done;
1114 }
1115
1116 /* Neither is zero pading. */
1117 if (format->fill_char == '0') {
1118 PyErr_SetString(PyExc_ValueError,
1119 "Zero padding is not allowed in complex format "
1120 "specifier");
1121 goto done;
1122 }
1123
1124 /* Neither is '=' alignment . */
1125 if (format->align == '=') {
1126 PyErr_SetString(PyExc_ValueError,
1127 "'=' alignment flag is not allowed in complex format "
1128 "specifier");
1129 goto done;
1130 }
1131
1132 re = PyComplex_RealAsDouble(value);
1133 if (re == -1.0 && PyErr_Occurred())
1134 goto done;
1135 im = PyComplex_ImagAsDouble(value);
1136 if (im == -1.0 && PyErr_Occurred())
1137 goto done;
1138
1139 if (type == '\0') {
1140 /* Omitted type specifier. Should be like str(self). */
1141 type = 'g';
Eric Smith63376222009-05-05 14:04:18 +00001142 default_precision = PyFloat_STR_PRECISION;
Mark Dickinsonfc070312010-08-01 10:43:42 +00001143 if (re == 0.0 && copysign(1.0, re) == 1.0)
Eric Smith58a42242009-04-30 01:00:33 +00001144 skip_re = 1;
Mark Dickinsonfc070312010-08-01 10:43:42 +00001145 else
1146 add_parens = 1;
Eric Smith58a42242009-04-30 01:00:33 +00001147 }
1148
1149 if (type == 'n')
1150 /* 'n' is the same as 'g', except for the locale used to
1151 format the result. We take care of that later. */
1152 type = 'g';
1153
Eric Smith7bc66b12009-07-27 02:12:11 +00001154#if PY_VERSION_HEX < 0x03010000
1155 /* This is no longer the case in 3.x */
1156 /* 'F' is the same as 'f', per the PEP */
1157 if (type == 'F')
1158 type = 'f';
1159#endif
1160
Eric Smith58a42242009-04-30 01:00:33 +00001161 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +00001162 precision = default_precision;
Eric Smith58a42242009-04-30 01:00:33 +00001163
1164 /* Cast "type", because if we're in unicode we need to pass a
1165 8-bit char. This is safe, because we've restricted what "type"
1166 can be. */
1167 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1168 &re_float_type);
1169 if (re_buf == NULL)
1170 goto done;
1171 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1172 &im_float_type);
1173 if (im_buf == NULL)
1174 goto done;
1175
1176 n_re_digits = strlen(re_buf);
1177 n_im_digits = strlen(im_buf);
1178
1179 /* Since there is no unicode version of PyOS_double_to_string,
1180 just use the 8 bit version and then convert to unicode. */
1181#if STRINGLIB_IS_UNICODE
1182 re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
1183 if (re_unicode_tmp == NULL) {
1184 PyErr_NoMemory();
1185 goto done;
1186 }
1187 strtounicode(re_unicode_tmp, re_buf, n_re_digits);
1188 p_re = re_unicode_tmp;
1189
1190 im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
1191 if (im_unicode_tmp == NULL) {
1192 PyErr_NoMemory();
1193 goto done;
1194 }
1195 strtounicode(im_unicode_tmp, im_buf, n_im_digits);
1196 p_im = im_unicode_tmp;
1197#else
1198 p_re = re_buf;
1199 p_im = im_buf;
1200#endif
1201
1202 /* Is a sign character present in the output? If so, remember it
1203 and skip it */
1204 if (*p_re == '-') {
1205 re_sign_char = *p_re;
1206 ++p_re;
1207 --n_re_digits;
1208 }
1209 if (*p_im == '-') {
1210 im_sign_char = *p_im;
1211 ++p_im;
1212 --n_im_digits;
1213 }
1214
1215 /* Determine if we have any "remainder" (after the digits, might include
1216 decimal or exponent or both (or neither)) */
1217 parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
1218 parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
1219
1220 /* Determine the grouping, separator, and decimal point, if any. */
1221 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1222 (format->thousands_separators ?
1223 LT_DEFAULT_LOCALE :
1224 LT_NO_LOCALE),
1225 &locale);
1226
1227 /* Turn off any padding. We'll do it later after we've composed
1228 the numbers without padding. */
1229 tmp_format.fill_char = '\0';
Eric Smith903fc052010-02-22 19:26:06 +00001230 tmp_format.align = '<';
Eric Smith58a42242009-04-30 01:00:33 +00001231 tmp_format.width = -1;
1232
1233 /* Calculate how much memory we'll need. */
1234 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
1235 n_re_digits, n_re_remainder,
1236 re_has_decimal, &locale, &tmp_format);
1237
Mark Dickinsonfc070312010-08-01 10:43:42 +00001238 /* Same formatting, but always include a sign, unless the real part is
1239 * going to be omitted, in which case we use whatever sign convention was
1240 * requested by the original format. */
1241 if (!skip_re)
1242 tmp_format.sign = '+';
Eric Smith58a42242009-04-30 01:00:33 +00001243 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
1244 n_im_digits, n_im_remainder,
1245 im_has_decimal, &locale, &tmp_format);
1246
1247 if (skip_re)
1248 n_re_total = 0;
1249
1250 /* Add 1 for the 'j', and optionally 2 for parens. */
1251 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1252 format->width, format->align, &lpad, &rpad, &total);
1253
1254 result = STRINGLIB_NEW(NULL, total);
1255 if (result == NULL)
1256 goto done;
1257
1258 /* Populate the memory. First, the padding. */
1259 p = fill_padding(STRINGLIB_STR(result),
1260 n_re_total + n_im_total + 1 + add_parens * 2,
1261 format->fill_char=='\0' ? ' ' : format->fill_char,
1262 lpad, rpad);
1263
1264 if (add_parens)
1265 *p++ = '(';
1266
1267 if (!skip_re) {
1268 fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
1269 p += n_re_total;
1270 }
1271 fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
1272 p += n_im_total;
1273 *p++ = 'j';
1274
1275 if (add_parens)
1276 *p++ = ')';
1277
1278done:
1279 PyMem_Free(re_buf);
1280 PyMem_Free(im_buf);
1281#if STRINGLIB_IS_UNICODE
1282 PyMem_Free(re_unicode_tmp);
1283 PyMem_Free(im_unicode_tmp);
1284#endif
1285 return result;
1286}
1287#endif /* FORMAT_COMPLEX */
1288
1289/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001290/*********** built in formatters ****************************************/
1291/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001292PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001293FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001294 STRINGLIB_CHAR *format_spec,
1295 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001296{
Eric Smith8c663262007-08-25 02:26:07 +00001297 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +00001298 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001299
1300 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001301 it equivalent to str(obj) */
1302 if (format_spec_len == 0) {
1303 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001304 goto done;
1305 }
1306
1307 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001308 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001309 &format, 's', '<'))
Eric Smith8c663262007-08-25 02:26:07 +00001310 goto done;
1311
1312 /* type conversion? */
1313 switch (format.type) {
1314 case 's':
1315 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001316 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001317 break;
Eric Smith8c663262007-08-25 02:26:07 +00001318 default:
1319 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001320 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001321 goto done;
1322 }
1323
1324done:
Eric Smith8c663262007-08-25 02:26:07 +00001325 return result;
1326}
1327
Eric Smith8fd3eba2008-02-17 19:48:00 +00001328#if defined FORMAT_LONG || defined FORMAT_INT
1329static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +00001330format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001331 STRINGLIB_CHAR *format_spec,
1332 Py_ssize_t format_spec_len,
1333 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +00001334{
Eric Smith8c663262007-08-25 02:26:07 +00001335 PyObject *result = NULL;
1336 PyObject *tmp = NULL;
1337 InternalFormatSpec format;
1338
Eric Smith8c663262007-08-25 02:26:07 +00001339 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001340 it equivalent to str(obj) */
1341 if (format_spec_len == 0) {
1342 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001343 goto done;
1344 }
1345
1346 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001347 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001348 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001349 &format, 'd', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001350 goto done;
1351
1352 /* type conversion? */
1353 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +00001354 case 'b':
1355 case 'c':
1356 case 'd':
1357 case 'o':
1358 case 'x':
1359 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +00001360 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +00001361 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001362 the formatting */
1363 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +00001364 break;
1365
Eric Smithfa767ef2008-01-28 10:59:27 +00001366 case 'e':
1367 case 'E':
1368 case 'f':
1369 case 'F':
1370 case 'g':
1371 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +00001372 case '%':
1373 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001374 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +00001375 if (tmp == NULL)
1376 goto done;
Eric Smithf64bce82009-04-13 00:50:23 +00001377 result = format_float_internal(tmp, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +00001378 break;
1379
Eric Smith8c663262007-08-25 02:26:07 +00001380 default:
1381 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001382 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001383 goto done;
1384 }
1385
1386done:
1387 Py_XDECREF(tmp);
1388 return result;
1389}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001390#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +00001391
Eric Smith8fd3eba2008-02-17 19:48:00 +00001392#ifdef FORMAT_LONG
1393/* Need to define long_format as a function that will convert a long
1394 to a string. In 3.0, _PyLong_Format has the correct signature. In
1395 2.x, we need to fudge a few parameters */
1396#if PY_VERSION_HEX >= 0x03000000
1397#define long_format _PyLong_Format
1398#else
1399static PyObject*
1400long_format(PyObject* value, int base)
1401{
1402 /* Convert to base, don't add trailing 'L', and use the new octal
1403 format. We already know this is a long object */
1404 assert(PyLong_Check(value));
1405 /* convert to base, don't add 'L', and use the new octal format */
1406 return _PyLong_Format(value, base, 0, 1);
1407}
1408#endif
1409
1410PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001411FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001412 STRINGLIB_CHAR *format_spec,
1413 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001414{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001415 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001416 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001417}
1418#endif /* FORMAT_LONG */
1419
1420#ifdef FORMAT_INT
1421/* this is only used for 2.x, not 3.0 */
1422static PyObject*
1423int_format(PyObject* value, int base)
1424{
1425 /* Convert to base, and use the new octal format. We already
1426 know this is an int object */
1427 assert(PyInt_Check(value));
1428 return _PyInt_Format((PyIntObject*)value, base, 1);
1429}
1430
1431PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001432FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001433 STRINGLIB_CHAR *format_spec,
1434 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001435{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001436 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001437 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001438}
1439#endif /* FORMAT_INT */
1440
1441#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001442PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001443FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001444 STRINGLIB_CHAR *format_spec,
1445 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001446{
Eric Smith8c663262007-08-25 02:26:07 +00001447 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001448 InternalFormatSpec format;
1449
Eric Smith8c663262007-08-25 02:26:07 +00001450 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001451 it equivalent to str(obj) */
1452 if (format_spec_len == 0) {
1453 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001454 goto done;
1455 }
1456
1457 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001458 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001459 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001460 &format, '\0', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001461 goto done;
1462
1463 /* type conversion? */
1464 switch (format.type) {
Eric Smith0923d1d2009-04-16 20:16:10 +00001465 case '\0': /* No format code: like 'g', but with at least one decimal. */
Eric Smith8c663262007-08-25 02:26:07 +00001466 case 'e':
1467 case 'E':
1468 case 'f':
1469 case 'F':
1470 case 'g':
1471 case 'G':
1472 case 'n':
1473 case '%':
1474 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001475 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001476 break;
1477
1478 default:
1479 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001480 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001481 goto done;
1482 }
1483
1484done:
Eric Smith8c663262007-08-25 02:26:07 +00001485 return result;
1486}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001487#endif /* FORMAT_FLOAT */
Eric Smith58a42242009-04-30 01:00:33 +00001488
1489#ifdef FORMAT_COMPLEX
1490PyObject *
1491FORMAT_COMPLEX(PyObject *obj,
1492 STRINGLIB_CHAR *format_spec,
1493 Py_ssize_t format_spec_len)
1494{
1495 PyObject *result = NULL;
1496 InternalFormatSpec format;
1497
1498 /* check for the special case of zero length format spec, make
1499 it equivalent to str(obj) */
1500 if (format_spec_len == 0) {
1501 result = STRINGLIB_TOSTR(obj);
1502 goto done;
1503 }
1504
1505 /* parse the format_spec */
1506 if (!parse_internal_render_format_spec(format_spec,
1507 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001508 &format, '\0', '>'))
Eric Smith58a42242009-04-30 01:00:33 +00001509 goto done;
1510
1511 /* type conversion? */
1512 switch (format.type) {
1513 case '\0': /* No format code: like 'g', but with at least one decimal. */
1514 case 'e':
1515 case 'E':
1516 case 'f':
1517 case 'F':
1518 case 'g':
1519 case 'G':
1520 case 'n':
1521 /* no conversion, already a complex. do the formatting */
1522 result = format_complex_internal(obj, &format);
1523 break;
1524
1525 default:
1526 /* unknown */
1527 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1528 goto done;
1529 }
1530
1531done:
1532 return result;
1533}
1534#endif /* FORMAT_COMPLEX */