blob: 57e54526fb793dd9e8675ca89fe8ee43b4f2313d [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
4/* Before including this, you must include either:
5 stringlib/unicodedefs.h
6 stringlib/stringdefs.h
7
8 Also, you should define the names:
9 FORMAT_STRING
10 FORMAT_LONG
11 FORMAT_FLOAT
12 to be whatever you want the public names of these functions to
13 be. These are the only non-static functions defined here.
14*/
15
Eric Smithb7f5ba12007-08-29 12:38:45 +000016#define ALLOW_PARENS_FOR_SIGN 0
17
Eric Smith5e5c0db2009-02-20 14:25:03 +000018/* Raises an exception about an unknown presentation type for this
19 * type. */
20
21static void
22unknown_presentation_type(STRINGLIB_CHAR presentation_type,
23 const char* type_name)
24{
25#if STRINGLIB_IS_UNICODE
26 /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
27 hence the two cases. If it is char, gcc complains that the
28 condition below is always true, hence the ifdef. */
29 if (presentation_type > 32 && presentation_type < 128)
30#endif
31 PyErr_Format(PyExc_ValueError,
32 "Unknown format code '%c' "
33 "for object of type '%.200s'",
34 presentation_type,
35 type_name);
36#if STRINGLIB_IS_UNICODE
37 else
38 PyErr_Format(PyExc_ValueError,
39 "Unknown format code '\\x%x' "
40 "for object of type '%.200s'",
41 (unsigned int)presentation_type,
42 type_name);
43#endif
44}
45
Eric Smith8c663262007-08-25 02:26:07 +000046/*
47 get_integer consumes 0 or more decimal digit characters from an
48 input string, updates *result with the corresponding positive
49 integer, and returns the number of digits consumed.
50
51 returns -1 on error.
52*/
53static int
54get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
55 Py_ssize_t *result)
56{
57 Py_ssize_t accumulator, digitval, oldaccumulator;
58 int numdigits;
59 accumulator = numdigits = 0;
60 for (;;(*ptr)++, numdigits++) {
61 if (*ptr >= end)
62 break;
63 digitval = STRINGLIB_TODECIMAL(**ptr);
64 if (digitval < 0)
65 break;
66 /*
67 This trick was copied from old Unicode format code. It's cute,
68 but would really suck on an old machine with a slow divide
69 implementation. Fortunately, in the normal case we do not
70 expect too many digits.
71 */
72 oldaccumulator = accumulator;
73 accumulator *= 10;
74 if ((accumulator+10)/10 != oldaccumulator+1) {
75 PyErr_Format(PyExc_ValueError,
76 "Too many decimal digits in format string");
77 return -1;
78 }
79 accumulator += digitval;
80 }
81 *result = accumulator;
82 return numdigits;
83}
84
85/************************************************************************/
86/*********** standard format specifier parsing **************************/
87/************************************************************************/
88
89/* returns true if this character is a specifier alignment token */
90Py_LOCAL_INLINE(int)
91is_alignment_token(STRINGLIB_CHAR c)
92{
93 switch (c) {
94 case '<': case '>': case '=': case '^':
95 return 1;
96 default:
97 return 0;
98 }
99}
100
101/* returns true if this character is a sign element */
102Py_LOCAL_INLINE(int)
103is_sign_element(STRINGLIB_CHAR c)
104{
105 switch (c) {
Eric Smithb7f5ba12007-08-29 12:38:45 +0000106 case ' ': case '+': case '-':
Eric Smith44300952007-08-29 12:43:12 +0000107#if ALLOW_PARENS_FOR_SIGN
Eric Smithb7f5ba12007-08-29 12:38:45 +0000108 case '(':
Eric Smith44300952007-08-29 12:43:12 +0000109#endif
Eric Smith8c663262007-08-25 02:26:07 +0000110 return 1;
111 default:
112 return 0;
113 }
114}
115
116
117typedef struct {
118 STRINGLIB_CHAR fill_char;
119 STRINGLIB_CHAR align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000120 int alternate;
Eric Smith8c663262007-08-25 02:26:07 +0000121 STRINGLIB_CHAR sign;
122 Py_ssize_t width;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000123 int thousands_separators;
Eric Smith8c663262007-08-25 02:26:07 +0000124 Py_ssize_t precision;
125 STRINGLIB_CHAR type;
126} InternalFormatSpec;
127
128/*
129 ptr points to the start of the format_spec, end points just past its end.
130 fills in format with the parsed information.
131 returns 1 on success, 0 on failure.
132 if failure, sets the exception
133*/
134static int
Eric Smith4a7d76d2008-05-30 18:10:19 +0000135parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000136 Py_ssize_t format_spec_len,
Eric Smith8c663262007-08-25 02:26:07 +0000137 InternalFormatSpec *format,
138 char default_type)
139{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000140 STRINGLIB_CHAR *ptr = format_spec;
141 STRINGLIB_CHAR *end = format_spec + format_spec_len;
Eric Smith8c663262007-08-25 02:26:07 +0000142
143 /* end-ptr is used throughout this code to specify the length of
144 the input string */
145
146 Py_ssize_t specified_width;
147
148 format->fill_char = '\0';
149 format->align = '\0';
Eric Smithb1ebcc62008-07-15 13:02:41 +0000150 format->alternate = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000151 format->sign = '\0';
152 format->width = -1;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000153 format->thousands_separators = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000154 format->precision = -1;
155 format->type = default_type;
156
157 /* If the second char is an alignment token,
158 then parse the fill char */
159 if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
160 format->align = ptr[1];
161 format->fill_char = ptr[0];
162 ptr += 2;
Eric Smith0cb431c2007-08-28 01:07:27 +0000163 }
164 else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
Eric Smith8c663262007-08-25 02:26:07 +0000165 format->align = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000166 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000167 }
168
169 /* Parse the various sign options */
170 if (end-ptr >= 1 && is_sign_element(ptr[0])) {
171 format->sign = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000172 ++ptr;
Eric Smithb7f5ba12007-08-29 12:38:45 +0000173#if ALLOW_PARENS_FOR_SIGN
Eric Smith8c663262007-08-25 02:26:07 +0000174 if (end-ptr >= 1 && ptr[0] == ')') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000175 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000176 }
Eric Smithb7f5ba12007-08-29 12:38:45 +0000177#endif
Eric Smith8c663262007-08-25 02:26:07 +0000178 }
179
Eric Smithd68af8f2008-07-16 00:15:35 +0000180 /* If the next character is #, we're in alternate mode. This only
181 applies to integers. */
182 if (end-ptr >= 1 && ptr[0] == '#') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000183 format->alternate = 1;
184 ++ptr;
Eric Smithd68af8f2008-07-16 00:15:35 +0000185 }
186
Eric Smith8c663262007-08-25 02:26:07 +0000187 /* The special case for 0-padding (backwards compat) */
Eric Smith185e30c2007-08-30 22:23:08 +0000188 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
Eric Smith8c663262007-08-25 02:26:07 +0000189 format->fill_char = '0';
190 if (format->align == '\0') {
191 format->align = '=';
192 }
Christian Heimesc3f30c42008-02-22 16:37:40 +0000193 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000194 }
195
196 /* XXX add error checking */
197 specified_width = get_integer(&ptr, end, &format->width);
198
199 /* if specified_width is 0, we didn't consume any characters for
200 the width. in that case, reset the width to -1, because
201 get_integer() will have set it to zero */
202 if (specified_width == 0) {
203 format->width = -1;
204 }
205
Eric Smitha3b1ac82009-04-03 14:45:06 +0000206 /* Comma signifies add thousands separators */
207 if (end-ptr && ptr[0] == ',') {
208 format->thousands_separators = 1;
209 ++ptr;
210 }
211
Eric Smith8c663262007-08-25 02:26:07 +0000212 /* Parse field precision */
213 if (end-ptr && ptr[0] == '.') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000214 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000215
216 /* XXX add error checking */
217 specified_width = get_integer(&ptr, end, &format->precision);
218
219 /* not having a precision after a dot is an error */
220 if (specified_width == 0) {
221 PyErr_Format(PyExc_ValueError,
222 "Format specifier missing precision");
223 return 0;
224 }
225
226 }
227
228 /* Finally, parse the type field */
229
230 if (end-ptr > 1) {
231 /* invalid conversion spec */
232 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
233 return 0;
234 }
235
236 if (end-ptr == 1) {
237 format->type = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000238 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000239 }
240
Eric Smitha3b1ac82009-04-03 14:45:06 +0000241 if (format->type == 'n' && format->thousands_separators) {
242 PyErr_Format(PyExc_ValueError, "Cannot specify ',' with 'n'.");
243 return 0;
244 }
245
Eric Smith8c663262007-08-25 02:26:07 +0000246 return 1;
247}
248
Eric Smith8fd3eba2008-02-17 19:48:00 +0000249#if defined FORMAT_FLOAT || defined FORMAT_LONG
Eric Smith8c663262007-08-25 02:26:07 +0000250/************************************************************************/
251/*********** common routines for numeric formatting *********************/
252/************************************************************************/
253
254/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000255 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000256typedef struct {
257 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000258 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000259 Py_ssize_t n_spadding;
260 Py_ssize_t n_rpadding;
261 char lsign;
262 Py_ssize_t n_lsign;
263 char rsign;
264 Py_ssize_t n_rsign;
265 Py_ssize_t n_total; /* just a convenience, it's derivable from the
266 other fields */
267} NumberFieldWidths;
268
269/* not all fields of format are used. for example, precision is
270 unused. should this take discrete params in order to be more clear
271 about what it does? or is passing a single format parameter easier
272 and more efficient enough to justify a little obfuscation? */
273static void
Eric Smith05212a12008-07-16 19:41:14 +0000274calc_number_widths(NumberFieldWidths *spec, STRINGLIB_CHAR actual_sign,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000275 Py_ssize_t n_prefix, Py_ssize_t n_digits,
276 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000277{
Eric Smith05212a12008-07-16 19:41:14 +0000278 spec->n_lpadding = 0;
279 spec->n_prefix = 0;
280 spec->n_spadding = 0;
281 spec->n_rpadding = 0;
282 spec->lsign = '\0';
283 spec->n_lsign = 0;
284 spec->rsign = '\0';
285 spec->n_rsign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000286
287 /* the output will look like:
Eric Smithb1ebcc62008-07-15 13:02:41 +0000288 | |
289 | <lpadding> <lsign> <prefix> <spadding> <digits> <rsign> <rpadding> |
290 | |
Eric Smith8c663262007-08-25 02:26:07 +0000291
292 lsign and rsign are computed from format->sign and the actual
293 sign of the number
294
Eric Smithb1ebcc62008-07-15 13:02:41 +0000295 prefix is given (it's for the '0x' prefix)
296
Eric Smith8c663262007-08-25 02:26:07 +0000297 digits is already known
298
299 the total width is either given, or computed from the
300 actual digits
301
302 only one of lpadding, spadding, and rpadding can be non-zero,
303 and it's calculated from the width and other fields
304 */
305
306 /* compute the various parts we're going to write */
307 if (format->sign == '+') {
308 /* always put a + or - */
Eric Smith05212a12008-07-16 19:41:14 +0000309 spec->n_lsign = 1;
310 spec->lsign = (actual_sign == '-' ? '-' : '+');
Eric Smith0cb431c2007-08-28 01:07:27 +0000311 }
Eric Smithb7f5ba12007-08-29 12:38:45 +0000312#if ALLOW_PARENS_FOR_SIGN
Eric Smith0cb431c2007-08-28 01:07:27 +0000313 else if (format->sign == '(') {
Eric Smith8c663262007-08-25 02:26:07 +0000314 if (actual_sign == '-') {
Eric Smith05212a12008-07-16 19:41:14 +0000315 spec->n_lsign = 1;
316 spec->lsign = '(';
317 spec->n_rsign = 1;
318 spec->rsign = ')';
Eric Smith8c663262007-08-25 02:26:07 +0000319 }
Eric Smith0cb431c2007-08-28 01:07:27 +0000320 }
Eric Smithb7f5ba12007-08-29 12:38:45 +0000321#endif
Eric Smith0cb431c2007-08-28 01:07:27 +0000322 else if (format->sign == ' ') {
Eric Smith05212a12008-07-16 19:41:14 +0000323 spec->n_lsign = 1;
324 spec->lsign = (actual_sign == '-' ? '-' : ' ');
Eric Smith0cb431c2007-08-28 01:07:27 +0000325 }
326 else {
Eric Smith8c663262007-08-25 02:26:07 +0000327 /* non specified, or the default (-) */
328 if (actual_sign == '-') {
Eric Smith05212a12008-07-16 19:41:14 +0000329 spec->n_lsign = 1;
330 spec->lsign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000331 }
332 }
333
Eric Smith05212a12008-07-16 19:41:14 +0000334 spec->n_prefix = n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000335
Eric Smith8c663262007-08-25 02:26:07 +0000336 /* now the number of padding characters */
337 if (format->width == -1) {
338 /* no padding at all, nothing to do */
Eric Smith0cb431c2007-08-28 01:07:27 +0000339 }
340 else {
Eric Smith8c663262007-08-25 02:26:07 +0000341 /* see if any padding is needed */
Eric Smith05212a12008-07-16 19:41:14 +0000342 if (spec->n_lsign + n_digits + spec->n_rsign +
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000343 spec->n_prefix >= format->width) {
Eric Smith8c663262007-08-25 02:26:07 +0000344 /* no padding needed, we're already bigger than the
345 requested width */
Eric Smith0cb431c2007-08-28 01:07:27 +0000346 }
347 else {
Eric Smith8c663262007-08-25 02:26:07 +0000348 /* determine which of left, space, or right padding is
349 needed */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000350 Py_ssize_t padding = format->width -
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000351 (spec->n_lsign + spec->n_prefix +
352 n_digits + spec->n_rsign);
Eric Smith8c663262007-08-25 02:26:07 +0000353 if (format->align == '<')
Eric Smith05212a12008-07-16 19:41:14 +0000354 spec->n_rpadding = padding;
Eric Smith8c663262007-08-25 02:26:07 +0000355 else if (format->align == '>')
Eric Smith05212a12008-07-16 19:41:14 +0000356 spec->n_lpadding = padding;
Eric Smith8c663262007-08-25 02:26:07 +0000357 else if (format->align == '^') {
Eric Smith05212a12008-07-16 19:41:14 +0000358 spec->n_lpadding = padding / 2;
359 spec->n_rpadding = padding - spec->n_lpadding;
Eric Smith0cb431c2007-08-28 01:07:27 +0000360 }
Eric Smith185e30c2007-08-30 22:23:08 +0000361 else if (format->align == '=')
Eric Smith05212a12008-07-16 19:41:14 +0000362 spec->n_spadding = padding;
Eric Smith185e30c2007-08-30 22:23:08 +0000363 else
Eric Smith05212a12008-07-16 19:41:14 +0000364 spec->n_lpadding = padding;
Eric Smith8c663262007-08-25 02:26:07 +0000365 }
366 }
Eric Smith05212a12008-07-16 19:41:14 +0000367 spec->n_total = spec->n_lpadding + spec->n_lsign + spec->n_prefix +
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000368 spec->n_spadding + n_digits + spec->n_rsign + spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000369}
370
371/* fill in the non-digit parts of a numbers's string representation,
Eric Smithd68af8f2008-07-16 00:15:35 +0000372 as determined in calc_number_widths(). returns the pointer to
Eric Smith8c663262007-08-25 02:26:07 +0000373 where the digits go. */
374static STRINGLIB_CHAR *
Eric Smithb151a452008-06-24 11:21:04 +0000375fill_non_digits(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000376 STRINGLIB_CHAR *prefix, Py_ssize_t n_digits,
377 STRINGLIB_CHAR fill_char)
Eric Smith8c663262007-08-25 02:26:07 +0000378{
Eric Smithd68af8f2008-07-16 00:15:35 +0000379 STRINGLIB_CHAR *p_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000380
381 if (spec->n_lpadding) {
382 STRINGLIB_FILL(p_buf, fill_char, spec->n_lpadding);
383 p_buf += spec->n_lpadding;
384 }
385 if (spec->n_lsign == 1) {
386 *p_buf++ = spec->lsign;
387 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000388 if (spec->n_prefix) {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000389 memmove(p_buf,
390 prefix,
391 spec->n_prefix * sizeof(STRINGLIB_CHAR));
392 p_buf += spec->n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000393 }
Eric Smith8c663262007-08-25 02:26:07 +0000394 if (spec->n_spadding) {
395 STRINGLIB_FILL(p_buf, fill_char, spec->n_spadding);
396 p_buf += spec->n_spadding;
397 }
398 p_digits = p_buf;
399 p_buf += n_digits;
400 if (spec->n_rsign == 1) {
401 *p_buf++ = spec->rsign;
402 }
403 if (spec->n_rpadding) {
404 STRINGLIB_FILL(p_buf, fill_char, spec->n_rpadding);
405 p_buf += spec->n_rpadding;
406 }
407 return p_digits;
408}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000409#endif /* FORMAT_FLOAT || FORMAT_LONG */
Eric Smith8c663262007-08-25 02:26:07 +0000410
411/************************************************************************/
412/*********** string formatting ******************************************/
413/************************************************************************/
414
415static PyObject *
416format_string_internal(PyObject *value, const InternalFormatSpec *format)
417{
418 Py_ssize_t width; /* total field width */
419 Py_ssize_t lpad;
420 STRINGLIB_CHAR *dst;
421 STRINGLIB_CHAR *src = STRINGLIB_STR(value);
422 Py_ssize_t len = STRINGLIB_LEN(value);
423 PyObject *result = NULL;
424
425 /* sign is not allowed on strings */
426 if (format->sign != '\0') {
427 PyErr_SetString(PyExc_ValueError,
428 "Sign not allowed in string format specifier");
429 goto done;
430 }
431
Eric Smithb1ebcc62008-07-15 13:02:41 +0000432 /* alternate is not allowed on strings */
433 if (format->alternate) {
434 PyErr_SetString(PyExc_ValueError,
435 "Alternate form (#) not allowed in string format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000436 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000437 goto done;
438 }
439
Eric Smith8c663262007-08-25 02:26:07 +0000440 /* '=' alignment not allowed on strings */
441 if (format->align == '=') {
442 PyErr_SetString(PyExc_ValueError,
443 "'=' alignment not allowed "
444 "in string format specifier");
445 goto done;
446 }
447
448 /* if precision is specified, output no more that format.precision
449 characters */
450 if (format->precision >= 0 && len >= format->precision) {
451 len = format->precision;
452 }
453
454 if (format->width >= 0) {
455 width = format->width;
456
457 /* but use at least len characters */
458 if (len > width) {
459 width = len;
460 }
Eric Smith0cb431c2007-08-28 01:07:27 +0000461 }
462 else {
Eric Smith8c663262007-08-25 02:26:07 +0000463 /* not specified, use all of the chars and no more */
464 width = len;
465 }
466
467 /* allocate the resulting string */
468 result = STRINGLIB_NEW(NULL, width);
469 if (result == NULL)
470 goto done;
471
472 /* now write into that space */
473 dst = STRINGLIB_STR(result);
474
475 /* figure out how much leading space we need, based on the
476 aligning */
477 if (format->align == '>')
478 lpad = width - len;
479 else if (format->align == '^')
480 lpad = (width - len) / 2;
481 else
482 lpad = 0;
483
484 /* if right aligning, increment the destination allow space on the
485 left */
486 memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR));
487
488 /* do any padding */
489 if (width > len) {
490 STRINGLIB_CHAR fill_char = format->fill_char;
491 if (fill_char == '\0') {
492 /* use the default, if not specified */
493 fill_char = ' ';
494 }
495
496 /* pad on left */
497 if (lpad)
498 STRINGLIB_FILL(dst, fill_char, lpad);
499
500 /* pad on right */
501 if (width - len - lpad)
502 STRINGLIB_FILL(dst + len + lpad, fill_char, width - len - lpad);
503 }
504
505done:
506 return result;
507}
508
509
510/************************************************************************/
511/*********** long formatting ********************************************/
512/************************************************************************/
513
Eric Smith8fd3eba2008-02-17 19:48:00 +0000514#if defined FORMAT_LONG || defined FORMAT_INT
515typedef PyObject*
516(*IntOrLongToString)(PyObject *value, int base);
517
Eric Smith8c663262007-08-25 02:26:07 +0000518static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000519format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000520 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000521{
522 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000523 PyObject *tmp = NULL;
524 STRINGLIB_CHAR *pnumeric_chars;
525 STRINGLIB_CHAR numeric_char;
Eric Smith8c663262007-08-25 02:26:07 +0000526 STRINGLIB_CHAR sign = '\0';
527 STRINGLIB_CHAR *p;
528 Py_ssize_t n_digits; /* count of digits need from the computed
529 string */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000530 Py_ssize_t n_leading_chars;
Eric Smith5807c412008-05-11 21:00:57 +0000531 Py_ssize_t n_grouping_chars = 0; /* Count of additional chars to
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000532 allocate, used for 'n'
533 formatting. */
Eric Smithd68af8f2008-07-16 00:15:35 +0000534 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
535 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000536 NumberFieldWidths spec;
537 long x;
538
539 /* no precision allowed on integers */
540 if (format->precision != -1) {
541 PyErr_SetString(PyExc_ValueError,
542 "Precision not allowed in integer format specifier");
543 goto done;
544 }
545
546
547 /* special case for character formatting */
548 if (format->type == 'c') {
549 /* error to specify a sign */
550 if (format->sign != '\0') {
551 PyErr_SetString(PyExc_ValueError,
552 "Sign not allowed with integer"
553 " format specifier 'c'");
554 goto done;
555 }
556
557 /* taken from unicodeobject.c formatchar() */
558 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000559/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000560 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000561 if (x == -1 && PyErr_Occurred())
562 goto done;
563#ifdef Py_UNICODE_WIDE
564 if (x < 0 || x > 0x10ffff) {
565 PyErr_SetString(PyExc_OverflowError,
566 "%c arg not in range(0x110000) "
567 "(wide Python build)");
568 goto done;
569 }
570#else
571 if (x < 0 || x > 0xffff) {
572 PyErr_SetString(PyExc_OverflowError,
573 "%c arg not in range(0x10000) "
574 "(narrow Python build)");
575 goto done;
576 }
577#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000578 numeric_char = (STRINGLIB_CHAR)x;
579 pnumeric_chars = &numeric_char;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000580 n_digits = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000581 }
582 else {
Eric Smith8c663262007-08-25 02:26:07 +0000583 int base;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000584 int leading_chars_to_skip = 0; /* Number of characters added by
585 PyNumber_ToBase that we want to
586 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000587
588 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000589 PyNumber_ToBase */
590 switch (format->type) {
591 case 'b':
592 base = 2;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000593 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000594 break;
595 case 'o':
596 base = 8;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000597 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000598 break;
599 case 'x':
600 case 'X':
601 base = 16;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000602 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000603 break;
604 default: /* shouldn't be needed, but stops a compiler warning */
605 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000606 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000607 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000608 break;
609 }
610
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000611 /* The number of prefix chars is the same as the leading
612 chars to skip */
613 if (format->alternate)
614 n_prefix = leading_chars_to_skip;
Eric Smithd68af8f2008-07-16 00:15:35 +0000615
Eric Smith8fd3eba2008-02-17 19:48:00 +0000616 /* Do the hard part, converting to a string in a given base */
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000617 tmp = tostring(value, base);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000618 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000619 goto done;
620
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000621 pnumeric_chars = STRINGLIB_STR(tmp);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000622 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000623
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000624 prefix = pnumeric_chars;
Eric Smithd68af8f2008-07-16 00:15:35 +0000625
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000626 /* Remember not to modify what pnumeric_chars points to. it
627 might be interned. Only modify it after we copy it into a
628 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000629
Eric Smith8fd3eba2008-02-17 19:48:00 +0000630 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000631 and skip it */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000632 sign = pnumeric_chars[0];
Eric Smith8c663262007-08-25 02:26:07 +0000633 if (sign == '-') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000634 ++prefix;
635 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000636 }
637
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000638 /* Skip over the leading chars (0x, 0b, etc.) */
639 n_digits -= leading_chars_to_skip;
640 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000641 }
642
Eric Smith5807c412008-05-11 21:00:57 +0000643 if (format->type == 'n')
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000644 /* Compute how many additional chars we need to allocate
645 to hold the thousands grouping. */
Eric Smitha3b1ac82009-04-03 14:45:06 +0000646 STRINGLIB_GROUPING_LOCALE(NULL, n_digits, n_digits,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000647 0, &n_grouping_chars, 0);
Eric Smitha3b1ac82009-04-03 14:45:06 +0000648 if (format->thousands_separators)
649 /* Compute how many additional chars we need to allocate
650 to hold the thousands grouping. */
651 STRINGLIB_GROUPING(NULL, n_digits, n_digits,
652 0, &n_grouping_chars, 0, "\3", ",");
Eric Smith5807c412008-05-11 21:00:57 +0000653
Eric Smithb151a452008-06-24 11:21:04 +0000654 /* Calculate the widths of the various leading and trailing parts */
Eric Smithd68af8f2008-07-16 00:15:35 +0000655 calc_number_widths(&spec, sign, n_prefix, n_digits + n_grouping_chars,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000656 format);
Eric Smithb151a452008-06-24 11:21:04 +0000657
Eric Smith8fd3eba2008-02-17 19:48:00 +0000658 /* Allocate a new string to hold the result */
Eric Smithb151a452008-06-24 11:21:04 +0000659 result = STRINGLIB_NEW(NULL, spec.n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000660 if (!result)
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000661 goto done;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000662 p = STRINGLIB_STR(result);
Eric Smith8c663262007-08-25 02:26:07 +0000663
Eric Smithd68af8f2008-07-16 00:15:35 +0000664 /* XXX There is too much magic here regarding the internals of
665 spec and the location of the prefix and digits. It would be
666 better if calc_number_widths returned a number of logical
667 offsets into the buffer, and those were used. Maybe in a
668 future code cleanup. */
669
Eric Smith8fd3eba2008-02-17 19:48:00 +0000670 /* Fill in the digit parts */
Eric Smithd68af8f2008-07-16 00:15:35 +0000671 n_leading_chars = spec.n_lpadding + spec.n_lsign +
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000672 spec.n_prefix + spec.n_spadding;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000673 memmove(p + n_leading_chars,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000674 pnumeric_chars,
675 n_digits * sizeof(STRINGLIB_CHAR));
Eric Smith8fd3eba2008-02-17 19:48:00 +0000676
Eric Smithd68af8f2008-07-16 00:15:35 +0000677 /* If type is 'X', convert the filled in digits to uppercase */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000678 if (format->type == 'X') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000679 Py_ssize_t t;
680 for (t = 0; t < n_digits; ++t)
681 p[t + n_leading_chars] = STRINGLIB_TOUPPER(p[t + n_leading_chars]);
Eric Smith8c663262007-08-25 02:26:07 +0000682 }
683
Eric Smithd68af8f2008-07-16 00:15:35 +0000684 /* Insert the grouping, if any, after the uppercasing of the digits, so
685 we can ensure that grouping chars won't be affected. */
Eric Smithb151a452008-06-24 11:21:04 +0000686 if (n_grouping_chars) {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000687 /* We know this can't fail, since we've already
688 reserved enough space. */
689 STRINGLIB_CHAR *pstart = p + n_leading_chars;
Neal Norwitz2f99b242008-08-24 05:48:10 +0000690#ifndef NDEBUG
Eric Smitha3b1ac82009-04-03 14:45:06 +0000691 int r;
Neal Norwitz2f99b242008-08-24 05:48:10 +0000692#endif
Eric Smitha3b1ac82009-04-03 14:45:06 +0000693 if (format->type == 'n')
694#ifndef NDEBUG
695 r =
696#endif
697 STRINGLIB_GROUPING_LOCALE(pstart, n_digits, n_digits,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000698 spec.n_total+n_grouping_chars-n_leading_chars,
699 NULL, 0);
Eric Smitha3b1ac82009-04-03 14:45:06 +0000700 else
701#ifndef NDEBUG
702 r =
703 STRINGLIB_GROUPING(pstart, n_digits, n_digits,
704 spec.n_total+n_grouping_chars-n_leading_chars,
705 NULL, 0, "\3", ",");
706#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000707 assert(r);
Eric Smith5807c412008-05-11 21:00:57 +0000708 }
709
Eric Smithb151a452008-06-24 11:21:04 +0000710 /* Fill in the non-digit parts (padding, sign, etc.) */
Eric Smithd68af8f2008-07-16 00:15:35 +0000711 fill_non_digits(p, &spec, prefix, n_digits + n_grouping_chars,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000712 format->fill_char == '\0' ? ' ' : format->fill_char);
Eric Smith8c663262007-08-25 02:26:07 +0000713
Eric Smithd68af8f2008-07-16 00:15:35 +0000714 /* If type is 'X', uppercase the prefix. This has to be done after the
715 prefix is filled in by fill_non_digits */
716 if (format->type == 'X') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000717 Py_ssize_t t;
718 for (t = 0; t < n_prefix; ++t)
719 p[t + spec.n_lpadding + spec.n_lsign] =
720 STRINGLIB_TOUPPER(p[t + spec.n_lpadding + spec.n_lsign]);
Eric Smithd68af8f2008-07-16 00:15:35 +0000721 }
722
723
Eric Smith8c663262007-08-25 02:26:07 +0000724done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000725 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000726 return result;
727}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000728#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000729
730/************************************************************************/
731/*********** float formatting *******************************************/
732/************************************************************************/
733
Eric Smith8fd3eba2008-02-17 19:48:00 +0000734#ifdef FORMAT_FLOAT
735#if STRINGLIB_IS_UNICODE
Eric Smith8c663262007-08-25 02:26:07 +0000736/* taken from unicodeobject.c */
737static Py_ssize_t
738strtounicode(Py_UNICODE *buffer, const char *charbuffer)
739{
740 register Py_ssize_t i;
741 Py_ssize_t len = strlen(charbuffer);
Christian Heimesc3f30c42008-02-22 16:37:40 +0000742 for (i = len - 1; i >= 0; --i)
Eric Smith185e30c2007-08-30 22:23:08 +0000743 buffer[i] = (Py_UNICODE) charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000744
745 return len;
746}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000747#endif
Eric Smith8c663262007-08-25 02:26:07 +0000748
Eric Smith8c663262007-08-25 02:26:07 +0000749/* see FORMATBUFLEN in unicodeobject.c */
750#define FLOAT_FORMATBUFLEN 120
751
752/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000753static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000754format_float_internal(PyObject *value,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000755 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000756{
757 /* fmt = '%.' + `prec` + `type` + '%%'
758 worst case length = 2 + 10 (len of INT_MAX) + 1 + 2 = 15 (use 20)*/
759 char fmt[20];
760
761 /* taken from unicodeobject.c */
762 /* Worst case length calc to ensure no buffer overrun:
763
764 'g' formats:
Eric Smith185e30c2007-08-30 22:23:08 +0000765 fmt = %#.<prec>g
766 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
767 for any double rep.)
768 len = 1 + prec + 1 + 2 + 5 = 9 + prec
Eric Smith8c663262007-08-25 02:26:07 +0000769
770 'f' formats:
Eric Smith185e30c2007-08-30 22:23:08 +0000771 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
772 len = 1 + 50 + 1 + prec = 52 + prec
Eric Smith8c663262007-08-25 02:26:07 +0000773
774 If prec=0 the effective precision is 1 (the leading digit is
775 always given), therefore increase the length by one.
776
777 */
778 char charbuf[FLOAT_FORMATBUFLEN];
779 Py_ssize_t n_digits;
780 double x;
781 Py_ssize_t precision = format->precision;
782 PyObject *result = NULL;
783 STRINGLIB_CHAR sign;
784 char* trailing = "";
785 STRINGLIB_CHAR *p;
786 NumberFieldWidths spec;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000787 STRINGLIB_CHAR type = format->type;
Eric Smith8c663262007-08-25 02:26:07 +0000788
789#if STRINGLIB_IS_UNICODE
790 Py_UNICODE unicodebuf[FLOAT_FORMATBUFLEN];
791#endif
792
Eric Smithb1ebcc62008-07-15 13:02:41 +0000793 /* alternate is not allowed on floats. */
794 if (format->alternate) {
795 PyErr_SetString(PyExc_ValueError,
796 "Alternate form (#) not allowed in float format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000797 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000798 goto done;
799 }
800
Eric Smith8c663262007-08-25 02:26:07 +0000801 /* first, do the conversion as 8-bit chars, using the platform's
802 snprintf. then, if needed, convert to unicode. */
803
Eric Smith22b85b32008-07-17 19:18:29 +0000804 /* 'F' is the same as 'f', per the PEP */
805 if (type == 'F')
806 type = 'f';
807
Eric Smith8c663262007-08-25 02:26:07 +0000808 x = PyFloat_AsDouble(value);
809
810 if (x == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000811 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000812
813 if (type == '%') {
814 type = 'f';
815 x *= 100;
816 trailing = "%";
817 }
818
819 if (precision < 0)
Eric Smith185e30c2007-08-30 22:23:08 +0000820 precision = 6;
Mark Dickinsonc8a608c2009-03-29 15:19:47 +0000821 if (type == 'f' && fabs(x) >= 1e50)
Eric Smith22b85b32008-07-17 19:18:29 +0000822 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000823
824 /* cast "type", because if we're in unicode we need to pass a
825 8-bit char. this is safe, because we've restricted what "type"
826 can be */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000827 PyOS_snprintf(fmt, sizeof(fmt), "%%.%" PY_FORMAT_SIZE_T "d%c", precision,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000828 (char)type);
Eric Smith8c663262007-08-25 02:26:07 +0000829
Christian Heimesc3f30c42008-02-22 16:37:40 +0000830 /* do the actual formatting */
831 PyOS_ascii_formatd(charbuf, sizeof(charbuf), fmt, x);
Eric Smith8c663262007-08-25 02:26:07 +0000832
833 /* adding trailing to fmt with PyOS_snprintf doesn't work, not
834 sure why. we'll just concatentate it here, no harm done. we
835 know we can't have a buffer overflow from the fmt size
836 analysis */
837 strcat(charbuf, trailing);
838
839 /* rather than duplicate the code for snprintf for both unicode
840 and 8 bit strings, we just use the 8 bit version and then
841 convert to unicode in a separate code path. that's probably
842 the lesser of 2 evils. */
843#if STRINGLIB_IS_UNICODE
844 n_digits = strtounicode(unicodebuf, charbuf);
845 p = unicodebuf;
846#else
847 /* compute the length. I believe this is done because the return
848 value from snprintf above is unreliable */
849 n_digits = strlen(charbuf);
850 p = charbuf;
851#endif
852
853 /* is a sign character present in the output? if so, remember it
854 and skip it */
855 sign = p[0];
856 if (sign == '-') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000857 ++p;
858 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000859 }
860
Eric Smithb1ebcc62008-07-15 13:02:41 +0000861 calc_number_widths(&spec, sign, 0, n_digits, format);
Eric Smith8c663262007-08-25 02:26:07 +0000862
863 /* allocate a string with enough space */
864 result = STRINGLIB_NEW(NULL, spec.n_total);
865 if (result == NULL)
866 goto done;
867
Eric Smithb151a452008-06-24 11:21:04 +0000868 /* Fill in the non-digit parts (padding, sign, etc.) */
Eric Smithd68af8f2008-07-16 00:15:35 +0000869 fill_non_digits(STRINGLIB_STR(result), &spec, NULL, n_digits,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000870 format->fill_char == '\0' ? ' ' : format->fill_char);
Eric Smith8c663262007-08-25 02:26:07 +0000871
872 /* fill in the digit parts */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000873 memmove(STRINGLIB_STR(result) +
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000874 (spec.n_lpadding + spec.n_lsign + spec.n_spadding),
Eric Smith8c663262007-08-25 02:26:07 +0000875 p,
876 n_digits * sizeof(STRINGLIB_CHAR));
877
878done:
879 return result;
880}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000881#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +0000882
883/************************************************************************/
884/*********** built in formatters ****************************************/
885/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +0000886PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +0000887FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000888 STRINGLIB_CHAR *format_spec,
889 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +0000890{
Eric Smith8c663262007-08-25 02:26:07 +0000891 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000892 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000893
894 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +0000895 it equivalent to str(obj) */
896 if (format_spec_len == 0) {
897 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +0000898 goto done;
899 }
900
901 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000902 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000903 &format, 's'))
Eric Smith8c663262007-08-25 02:26:07 +0000904 goto done;
905
906 /* type conversion? */
907 switch (format.type) {
908 case 's':
909 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000910 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +0000911 break;
Eric Smith8c663262007-08-25 02:26:07 +0000912 default:
913 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +0000914 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +0000915 goto done;
916 }
917
918done:
Eric Smith8c663262007-08-25 02:26:07 +0000919 return result;
920}
921
Eric Smith8fd3eba2008-02-17 19:48:00 +0000922#if defined FORMAT_LONG || defined FORMAT_INT
923static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +0000924format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000925 STRINGLIB_CHAR *format_spec,
926 Py_ssize_t format_spec_len,
927 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000928{
Eric Smith8c663262007-08-25 02:26:07 +0000929 PyObject *result = NULL;
930 PyObject *tmp = NULL;
931 InternalFormatSpec format;
932
Eric Smith8c663262007-08-25 02:26:07 +0000933 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +0000934 it equivalent to str(obj) */
935 if (format_spec_len == 0) {
936 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +0000937 goto done;
938 }
939
940 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000941 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000942 format_spec_len,
943 &format, 'd'))
Eric Smith8c663262007-08-25 02:26:07 +0000944 goto done;
945
946 /* type conversion? */
947 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +0000948 case 'b':
949 case 'c':
950 case 'd':
951 case 'o':
952 case 'x':
953 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +0000954 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +0000955 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000956 the formatting */
957 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +0000958 break;
959
Eric Smithfa767ef2008-01-28 10:59:27 +0000960 case 'e':
961 case 'E':
962 case 'f':
963 case 'F':
964 case 'g':
965 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +0000966 case '%':
967 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000968 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +0000969 if (tmp == NULL)
970 goto done;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000971 result = format_float_internal(obj, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +0000972 break;
973
Eric Smith8c663262007-08-25 02:26:07 +0000974 default:
975 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +0000976 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +0000977 goto done;
978 }
979
980done:
981 Py_XDECREF(tmp);
982 return result;
983}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000984#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000985
Eric Smith8fd3eba2008-02-17 19:48:00 +0000986#ifdef FORMAT_LONG
987/* Need to define long_format as a function that will convert a long
988 to a string. In 3.0, _PyLong_Format has the correct signature. In
989 2.x, we need to fudge a few parameters */
990#if PY_VERSION_HEX >= 0x03000000
991#define long_format _PyLong_Format
992#else
993static PyObject*
994long_format(PyObject* value, int base)
995{
996 /* Convert to base, don't add trailing 'L', and use the new octal
997 format. We already know this is a long object */
998 assert(PyLong_Check(value));
999 /* convert to base, don't add 'L', and use the new octal format */
1000 return _PyLong_Format(value, base, 0, 1);
1001}
1002#endif
1003
1004PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001005FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001006 STRINGLIB_CHAR *format_spec,
1007 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001008{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001009 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001010 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001011}
1012#endif /* FORMAT_LONG */
1013
1014#ifdef FORMAT_INT
1015/* this is only used for 2.x, not 3.0 */
1016static PyObject*
1017int_format(PyObject* value, int base)
1018{
1019 /* Convert to base, and use the new octal format. We already
1020 know this is an int object */
1021 assert(PyInt_Check(value));
1022 return _PyInt_Format((PyIntObject*)value, base, 1);
1023}
1024
1025PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001026FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001027 STRINGLIB_CHAR *format_spec,
1028 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001029{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001030 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001031 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001032}
1033#endif /* FORMAT_INT */
1034
1035#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001036PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001037FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001038 STRINGLIB_CHAR *format_spec,
1039 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001040{
Eric Smith8c663262007-08-25 02:26:07 +00001041 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001042 InternalFormatSpec format;
1043
Eric Smith8c663262007-08-25 02:26:07 +00001044 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001045 it equivalent to str(obj) */
1046 if (format_spec_len == 0) {
1047 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001048 goto done;
1049 }
1050
1051 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001052 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001053 format_spec_len,
1054 &format, '\0'))
Eric Smith8c663262007-08-25 02:26:07 +00001055 goto done;
1056
1057 /* type conversion? */
1058 switch (format.type) {
Christian Heimesb186d002008-03-18 15:15:01 +00001059 case '\0':
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001060 /* 'Z' means like 'g', but with at least one decimal. See
1061 PyOS_ascii_formatd */
1062 format.type = 'Z';
1063 /* Deliberate fall through to the next case statement */
Eric Smith8c663262007-08-25 02:26:07 +00001064 case 'e':
1065 case 'E':
1066 case 'f':
1067 case 'F':
1068 case 'g':
1069 case 'G':
1070 case 'n':
1071 case '%':
1072 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001073 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001074 break;
1075
1076 default:
1077 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001078 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001079 goto done;
1080 }
1081
1082done:
Eric Smith8c663262007-08-25 02:26:07 +00001083 return result;
1084}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001085#endif /* FORMAT_FLOAT */