blob: 62849c5fae39b3c2ca84eaee9c3182898edfa9fd [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;
Eric Smith58a42242009-04-30 01:00:33 +00001143 add_parens = 1;
1144 if (re == 0.0)
1145 skip_re = 1;
1146 }
1147
1148 if (type == 'n')
1149 /* 'n' is the same as 'g', except for the locale used to
1150 format the result. We take care of that later. */
1151 type = 'g';
1152
Eric Smith7bc66b12009-07-27 02:12:11 +00001153#if PY_VERSION_HEX < 0x03010000
1154 /* This is no longer the case in 3.x */
1155 /* 'F' is the same as 'f', per the PEP */
1156 if (type == 'F')
1157 type = 'f';
1158#endif
1159
Eric Smith58a42242009-04-30 01:00:33 +00001160 if (precision < 0)
Eric Smith63376222009-05-05 14:04:18 +00001161 precision = default_precision;
Eric Smith58a42242009-04-30 01:00:33 +00001162
1163 /* Cast "type", because if we're in unicode we need to pass a
1164 8-bit char. This is safe, because we've restricted what "type"
1165 can be. */
1166 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1167 &re_float_type);
1168 if (re_buf == NULL)
1169 goto done;
1170 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1171 &im_float_type);
1172 if (im_buf == NULL)
1173 goto done;
1174
1175 n_re_digits = strlen(re_buf);
1176 n_im_digits = strlen(im_buf);
1177
1178 /* Since there is no unicode version of PyOS_double_to_string,
1179 just use the 8 bit version and then convert to unicode. */
1180#if STRINGLIB_IS_UNICODE
1181 re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
1182 if (re_unicode_tmp == NULL) {
1183 PyErr_NoMemory();
1184 goto done;
1185 }
1186 strtounicode(re_unicode_tmp, re_buf, n_re_digits);
1187 p_re = re_unicode_tmp;
1188
1189 im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
1190 if (im_unicode_tmp == NULL) {
1191 PyErr_NoMemory();
1192 goto done;
1193 }
1194 strtounicode(im_unicode_tmp, im_buf, n_im_digits);
1195 p_im = im_unicode_tmp;
1196#else
1197 p_re = re_buf;
1198 p_im = im_buf;
1199#endif
1200
1201 /* Is a sign character present in the output? If so, remember it
1202 and skip it */
1203 if (*p_re == '-') {
1204 re_sign_char = *p_re;
1205 ++p_re;
1206 --n_re_digits;
1207 }
1208 if (*p_im == '-') {
1209 im_sign_char = *p_im;
1210 ++p_im;
1211 --n_im_digits;
1212 }
1213
1214 /* Determine if we have any "remainder" (after the digits, might include
1215 decimal or exponent or both (or neither)) */
1216 parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
1217 parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
1218
1219 /* Determine the grouping, separator, and decimal point, if any. */
1220 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1221 (format->thousands_separators ?
1222 LT_DEFAULT_LOCALE :
1223 LT_NO_LOCALE),
1224 &locale);
1225
1226 /* Turn off any padding. We'll do it later after we've composed
1227 the numbers without padding. */
1228 tmp_format.fill_char = '\0';
Eric Smith903fc052010-02-22 19:26:06 +00001229 tmp_format.align = '<';
Eric Smith58a42242009-04-30 01:00:33 +00001230 tmp_format.width = -1;
1231
1232 /* Calculate how much memory we'll need. */
1233 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
1234 n_re_digits, n_re_remainder,
1235 re_has_decimal, &locale, &tmp_format);
1236
1237 /* Same formatting, but always include a sign. */
1238 tmp_format.sign = '+';
1239 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
1240 n_im_digits, n_im_remainder,
1241 im_has_decimal, &locale, &tmp_format);
1242
1243 if (skip_re)
1244 n_re_total = 0;
1245
1246 /* Add 1 for the 'j', and optionally 2 for parens. */
1247 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1248 format->width, format->align, &lpad, &rpad, &total);
1249
1250 result = STRINGLIB_NEW(NULL, total);
1251 if (result == NULL)
1252 goto done;
1253
1254 /* Populate the memory. First, the padding. */
1255 p = fill_padding(STRINGLIB_STR(result),
1256 n_re_total + n_im_total + 1 + add_parens * 2,
1257 format->fill_char=='\0' ? ' ' : format->fill_char,
1258 lpad, rpad);
1259
1260 if (add_parens)
1261 *p++ = '(';
1262
1263 if (!skip_re) {
1264 fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
1265 p += n_re_total;
1266 }
1267 fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
1268 p += n_im_total;
1269 *p++ = 'j';
1270
1271 if (add_parens)
1272 *p++ = ')';
1273
1274done:
1275 PyMem_Free(re_buf);
1276 PyMem_Free(im_buf);
1277#if STRINGLIB_IS_UNICODE
1278 PyMem_Free(re_unicode_tmp);
1279 PyMem_Free(im_unicode_tmp);
1280#endif
1281 return result;
1282}
1283#endif /* FORMAT_COMPLEX */
1284
1285/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001286/*********** built in formatters ****************************************/
1287/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001288PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001289FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001290 STRINGLIB_CHAR *format_spec,
1291 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001292{
Eric Smith8c663262007-08-25 02:26:07 +00001293 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +00001294 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001295
1296 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001297 it equivalent to str(obj) */
1298 if (format_spec_len == 0) {
1299 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001300 goto done;
1301 }
1302
1303 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001304 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001305 &format, 's', '<'))
Eric Smith8c663262007-08-25 02:26:07 +00001306 goto done;
1307
1308 /* type conversion? */
1309 switch (format.type) {
1310 case 's':
1311 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001312 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001313 break;
Eric Smith8c663262007-08-25 02:26:07 +00001314 default:
1315 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001316 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001317 goto done;
1318 }
1319
1320done:
Eric Smith8c663262007-08-25 02:26:07 +00001321 return result;
1322}
1323
Eric Smith8fd3eba2008-02-17 19:48:00 +00001324#if defined FORMAT_LONG || defined FORMAT_INT
1325static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +00001326format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001327 STRINGLIB_CHAR *format_spec,
1328 Py_ssize_t format_spec_len,
1329 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +00001330{
Eric Smith8c663262007-08-25 02:26:07 +00001331 PyObject *result = NULL;
1332 PyObject *tmp = NULL;
1333 InternalFormatSpec format;
1334
Eric Smith8c663262007-08-25 02:26:07 +00001335 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001336 it equivalent to str(obj) */
1337 if (format_spec_len == 0) {
1338 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001339 goto done;
1340 }
1341
1342 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001343 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001344 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001345 &format, 'd', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001346 goto done;
1347
1348 /* type conversion? */
1349 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +00001350 case 'b':
1351 case 'c':
1352 case 'd':
1353 case 'o':
1354 case 'x':
1355 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +00001356 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +00001357 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001358 the formatting */
1359 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +00001360 break;
1361
Eric Smithfa767ef2008-01-28 10:59:27 +00001362 case 'e':
1363 case 'E':
1364 case 'f':
1365 case 'F':
1366 case 'g':
1367 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +00001368 case '%':
1369 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001370 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +00001371 if (tmp == NULL)
1372 goto done;
Eric Smithf64bce82009-04-13 00:50:23 +00001373 result = format_float_internal(tmp, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +00001374 break;
1375
Eric Smith8c663262007-08-25 02:26:07 +00001376 default:
1377 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001378 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001379 goto done;
1380 }
1381
1382done:
1383 Py_XDECREF(tmp);
1384 return result;
1385}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001386#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +00001387
Eric Smith8fd3eba2008-02-17 19:48:00 +00001388#ifdef FORMAT_LONG
1389/* Need to define long_format as a function that will convert a long
1390 to a string. In 3.0, _PyLong_Format has the correct signature. In
1391 2.x, we need to fudge a few parameters */
1392#if PY_VERSION_HEX >= 0x03000000
1393#define long_format _PyLong_Format
1394#else
1395static PyObject*
1396long_format(PyObject* value, int base)
1397{
1398 /* Convert to base, don't add trailing 'L', and use the new octal
1399 format. We already know this is a long object */
1400 assert(PyLong_Check(value));
1401 /* convert to base, don't add 'L', and use the new octal format */
1402 return _PyLong_Format(value, base, 0, 1);
1403}
1404#endif
1405
1406PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001407FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001408 STRINGLIB_CHAR *format_spec,
1409 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001410{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001411 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001412 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001413}
1414#endif /* FORMAT_LONG */
1415
1416#ifdef FORMAT_INT
1417/* this is only used for 2.x, not 3.0 */
1418static PyObject*
1419int_format(PyObject* value, int base)
1420{
1421 /* Convert to base, and use the new octal format. We already
1422 know this is an int object */
1423 assert(PyInt_Check(value));
1424 return _PyInt_Format((PyIntObject*)value, base, 1);
1425}
1426
1427PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001428FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001429 STRINGLIB_CHAR *format_spec,
1430 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001431{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001432 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001433 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001434}
1435#endif /* FORMAT_INT */
1436
1437#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001438PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001439FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001440 STRINGLIB_CHAR *format_spec,
1441 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001442{
Eric Smith8c663262007-08-25 02:26:07 +00001443 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001444 InternalFormatSpec format;
1445
Eric Smith8c663262007-08-25 02:26:07 +00001446 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001447 it equivalent to str(obj) */
1448 if (format_spec_len == 0) {
1449 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001450 goto done;
1451 }
1452
1453 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001454 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001455 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001456 &format, '\0', '>'))
Eric Smith8c663262007-08-25 02:26:07 +00001457 goto done;
1458
1459 /* type conversion? */
1460 switch (format.type) {
Eric Smith0923d1d2009-04-16 20:16:10 +00001461 case '\0': /* No format code: like 'g', but with at least one decimal. */
Eric Smith8c663262007-08-25 02:26:07 +00001462 case 'e':
1463 case 'E':
1464 case 'f':
1465 case 'F':
1466 case 'g':
1467 case 'G':
1468 case 'n':
1469 case '%':
1470 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001471 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001472 break;
1473
1474 default:
1475 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001476 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001477 goto done;
1478 }
1479
1480done:
Eric Smith8c663262007-08-25 02:26:07 +00001481 return result;
1482}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001483#endif /* FORMAT_FLOAT */
Eric Smith58a42242009-04-30 01:00:33 +00001484
1485#ifdef FORMAT_COMPLEX
1486PyObject *
1487FORMAT_COMPLEX(PyObject *obj,
1488 STRINGLIB_CHAR *format_spec,
1489 Py_ssize_t format_spec_len)
1490{
1491 PyObject *result = NULL;
1492 InternalFormatSpec format;
1493
1494 /* check for the special case of zero length format spec, make
1495 it equivalent to str(obj) */
1496 if (format_spec_len == 0) {
1497 result = STRINGLIB_TOSTR(obj);
1498 goto done;
1499 }
1500
1501 /* parse the format_spec */
1502 if (!parse_internal_render_format_spec(format_spec,
1503 format_spec_len,
Eric Smith903fc052010-02-22 19:26:06 +00001504 &format, '\0', '>'))
Eric Smith58a42242009-04-30 01:00:33 +00001505 goto done;
1506
1507 /* type conversion? */
1508 switch (format.type) {
1509 case '\0': /* No format code: like 'g', but with at least one decimal. */
1510 case 'e':
1511 case 'E':
1512 case 'f':
1513 case 'F':
1514 case 'g':
1515 case 'G':
1516 case 'n':
1517 /* no conversion, already a complex. do the formatting */
1518 result = format_complex_internal(obj, &format);
1519 break;
1520
1521 default:
1522 /* unknown */
1523 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1524 goto done;
1525 }
1526
1527done:
1528 return result;
1529}
1530#endif /* FORMAT_COMPLEX */