blob: aa99123032a404d93a3632118d376c4e66e2857e [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 Smith8c663262007-08-25 02:26:07 +000018/*
19 get_integer consumes 0 or more decimal digit characters from an
20 input string, updates *result with the corresponding positive
21 integer, and returns the number of digits consumed.
22
23 returns -1 on error.
24*/
25static int
26get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
27 Py_ssize_t *result)
28{
29 Py_ssize_t accumulator, digitval, oldaccumulator;
30 int numdigits;
31 accumulator = numdigits = 0;
32 for (;;(*ptr)++, numdigits++) {
33 if (*ptr >= end)
34 break;
35 digitval = STRINGLIB_TODECIMAL(**ptr);
36 if (digitval < 0)
37 break;
38 /*
39 This trick was copied from old Unicode format code. It's cute,
40 but would really suck on an old machine with a slow divide
41 implementation. Fortunately, in the normal case we do not
42 expect too many digits.
43 */
44 oldaccumulator = accumulator;
45 accumulator *= 10;
46 if ((accumulator+10)/10 != oldaccumulator+1) {
47 PyErr_Format(PyExc_ValueError,
48 "Too many decimal digits in format string");
49 return -1;
50 }
51 accumulator += digitval;
52 }
53 *result = accumulator;
54 return numdigits;
55}
56
57/************************************************************************/
58/*********** standard format specifier parsing **************************/
59/************************************************************************/
60
61/* returns true if this character is a specifier alignment token */
62Py_LOCAL_INLINE(int)
63is_alignment_token(STRINGLIB_CHAR c)
64{
65 switch (c) {
66 case '<': case '>': case '=': case '^':
67 return 1;
68 default:
69 return 0;
70 }
71}
72
73/* returns true if this character is a sign element */
74Py_LOCAL_INLINE(int)
75is_sign_element(STRINGLIB_CHAR c)
76{
77 switch (c) {
Eric Smithb7f5ba12007-08-29 12:38:45 +000078 case ' ': case '+': case '-':
Eric Smith44300952007-08-29 12:43:12 +000079#if ALLOW_PARENS_FOR_SIGN
Eric Smithb7f5ba12007-08-29 12:38:45 +000080 case '(':
Eric Smith44300952007-08-29 12:43:12 +000081#endif
Eric Smith8c663262007-08-25 02:26:07 +000082 return 1;
83 default:
84 return 0;
85 }
86}
87
88
89typedef struct {
90 STRINGLIB_CHAR fill_char;
91 STRINGLIB_CHAR align;
Eric Smithb1ebcc62008-07-15 13:02:41 +000092 int alternate;
Eric Smith8c663262007-08-25 02:26:07 +000093 STRINGLIB_CHAR sign;
94 Py_ssize_t width;
95 Py_ssize_t precision;
96 STRINGLIB_CHAR type;
97} InternalFormatSpec;
98
99/*
100 ptr points to the start of the format_spec, end points just past its end.
101 fills in format with the parsed information.
102 returns 1 on success, 0 on failure.
103 if failure, sets the exception
104*/
105static int
Eric Smith4a7d76d2008-05-30 18:10:19 +0000106parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
107 Py_ssize_t format_spec_len,
Eric Smith8c663262007-08-25 02:26:07 +0000108 InternalFormatSpec *format,
109 char default_type)
110{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000111 STRINGLIB_CHAR *ptr = format_spec;
112 STRINGLIB_CHAR *end = format_spec + format_spec_len;
Eric Smith8c663262007-08-25 02:26:07 +0000113
114 /* end-ptr is used throughout this code to specify the length of
115 the input string */
116
117 Py_ssize_t specified_width;
118
119 format->fill_char = '\0';
120 format->align = '\0';
Eric Smithb1ebcc62008-07-15 13:02:41 +0000121 format->alternate = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000122 format->sign = '\0';
123 format->width = -1;
124 format->precision = -1;
125 format->type = default_type;
126
127 /* If the second char is an alignment token,
128 then parse the fill char */
129 if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
130 format->align = ptr[1];
131 format->fill_char = ptr[0];
132 ptr += 2;
Eric Smith0cb431c2007-08-28 01:07:27 +0000133 }
134 else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
Eric Smith8c663262007-08-25 02:26:07 +0000135 format->align = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000136 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000137 }
138
139 /* Parse the various sign options */
140 if (end-ptr >= 1 && is_sign_element(ptr[0])) {
141 format->sign = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000142 ++ptr;
Eric Smithb7f5ba12007-08-29 12:38:45 +0000143#if ALLOW_PARENS_FOR_SIGN
Eric Smith8c663262007-08-25 02:26:07 +0000144 if (end-ptr >= 1 && ptr[0] == ')') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000145 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000146 }
Eric Smithb7f5ba12007-08-29 12:38:45 +0000147#endif
Eric Smith8c663262007-08-25 02:26:07 +0000148 }
149
Eric Smithd68af8f2008-07-16 00:15:35 +0000150 /* If the next character is #, we're in alternate mode. This only
151 applies to integers. */
152 if (end-ptr >= 1 && ptr[0] == '#') {
153 format->alternate = 1;
154 ++ptr;
155 }
156
Eric Smith8c663262007-08-25 02:26:07 +0000157 /* The special case for 0-padding (backwards compat) */
Eric Smith185e30c2007-08-30 22:23:08 +0000158 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
Eric Smith8c663262007-08-25 02:26:07 +0000159 format->fill_char = '0';
160 if (format->align == '\0') {
161 format->align = '=';
162 }
Christian Heimesc3f30c42008-02-22 16:37:40 +0000163 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000164 }
165
166 /* XXX add error checking */
167 specified_width = get_integer(&ptr, end, &format->width);
168
169 /* if specified_width is 0, we didn't consume any characters for
170 the width. in that case, reset the width to -1, because
171 get_integer() will have set it to zero */
172 if (specified_width == 0) {
173 format->width = -1;
174 }
175
176 /* Parse field precision */
177 if (end-ptr && ptr[0] == '.') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000178 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000179
180 /* XXX add error checking */
181 specified_width = get_integer(&ptr, end, &format->precision);
182
183 /* not having a precision after a dot is an error */
184 if (specified_width == 0) {
185 PyErr_Format(PyExc_ValueError,
186 "Format specifier missing precision");
187 return 0;
188 }
189
190 }
191
192 /* Finally, parse the type field */
193
194 if (end-ptr > 1) {
195 /* invalid conversion spec */
196 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
197 return 0;
198 }
199
200 if (end-ptr == 1) {
201 format->type = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000202 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000203 }
204
205 return 1;
206}
207
Eric Smith8fd3eba2008-02-17 19:48:00 +0000208#if defined FORMAT_FLOAT || defined FORMAT_LONG
Eric Smith8c663262007-08-25 02:26:07 +0000209/************************************************************************/
210/*********** common routines for numeric formatting *********************/
211/************************************************************************/
212
213/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000214 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000215typedef struct {
216 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000217 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000218 Py_ssize_t n_spadding;
219 Py_ssize_t n_rpadding;
220 char lsign;
221 Py_ssize_t n_lsign;
222 char rsign;
223 Py_ssize_t n_rsign;
224 Py_ssize_t n_total; /* just a convenience, it's derivable from the
225 other fields */
226} NumberFieldWidths;
227
228/* not all fields of format are used. for example, precision is
229 unused. should this take discrete params in order to be more clear
230 about what it does? or is passing a single format parameter easier
231 and more efficient enough to justify a little obfuscation? */
232static void
Eric Smith05212a12008-07-16 19:41:14 +0000233calc_number_widths(NumberFieldWidths *spec, STRINGLIB_CHAR actual_sign,
Eric Smithb1ebcc62008-07-15 13:02:41 +0000234 Py_ssize_t n_prefix, Py_ssize_t n_digits,
235 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000236{
Eric Smith05212a12008-07-16 19:41:14 +0000237 spec->n_lpadding = 0;
238 spec->n_prefix = 0;
239 spec->n_spadding = 0;
240 spec->n_rpadding = 0;
241 spec->lsign = '\0';
242 spec->n_lsign = 0;
243 spec->rsign = '\0';
244 spec->n_rsign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000245
246 /* the output will look like:
Eric Smithb1ebcc62008-07-15 13:02:41 +0000247 | |
248 | <lpadding> <lsign> <prefix> <spadding> <digits> <rsign> <rpadding> |
249 | |
Eric Smith8c663262007-08-25 02:26:07 +0000250
251 lsign and rsign are computed from format->sign and the actual
252 sign of the number
253
Eric Smithb1ebcc62008-07-15 13:02:41 +0000254 prefix is given (it's for the '0x' prefix)
255
Eric Smith8c663262007-08-25 02:26:07 +0000256 digits is already known
257
258 the total width is either given, or computed from the
259 actual digits
260
261 only one of lpadding, spadding, and rpadding can be non-zero,
262 and it's calculated from the width and other fields
263 */
264
265 /* compute the various parts we're going to write */
266 if (format->sign == '+') {
267 /* always put a + or - */
Eric Smith05212a12008-07-16 19:41:14 +0000268 spec->n_lsign = 1;
269 spec->lsign = (actual_sign == '-' ? '-' : '+');
Eric Smith0cb431c2007-08-28 01:07:27 +0000270 }
Eric Smithb7f5ba12007-08-29 12:38:45 +0000271#if ALLOW_PARENS_FOR_SIGN
Eric Smith0cb431c2007-08-28 01:07:27 +0000272 else if (format->sign == '(') {
Eric Smith8c663262007-08-25 02:26:07 +0000273 if (actual_sign == '-') {
Eric Smith05212a12008-07-16 19:41:14 +0000274 spec->n_lsign = 1;
275 spec->lsign = '(';
276 spec->n_rsign = 1;
277 spec->rsign = ')';
Eric Smith8c663262007-08-25 02:26:07 +0000278 }
Eric Smith0cb431c2007-08-28 01:07:27 +0000279 }
Eric Smithb7f5ba12007-08-29 12:38:45 +0000280#endif
Eric Smith0cb431c2007-08-28 01:07:27 +0000281 else if (format->sign == ' ') {
Eric Smith05212a12008-07-16 19:41:14 +0000282 spec->n_lsign = 1;
283 spec->lsign = (actual_sign == '-' ? '-' : ' ');
Eric Smith0cb431c2007-08-28 01:07:27 +0000284 }
285 else {
Eric Smith8c663262007-08-25 02:26:07 +0000286 /* non specified, or the default (-) */
287 if (actual_sign == '-') {
Eric Smith05212a12008-07-16 19:41:14 +0000288 spec->n_lsign = 1;
289 spec->lsign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000290 }
291 }
292
Eric Smith05212a12008-07-16 19:41:14 +0000293 spec->n_prefix = n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000294
Eric Smith8c663262007-08-25 02:26:07 +0000295 /* now the number of padding characters */
296 if (format->width == -1) {
297 /* no padding at all, nothing to do */
Eric Smith0cb431c2007-08-28 01:07:27 +0000298 }
299 else {
Eric Smith8c663262007-08-25 02:26:07 +0000300 /* see if any padding is needed */
Eric Smith05212a12008-07-16 19:41:14 +0000301 if (spec->n_lsign + n_digits + spec->n_rsign +
302 spec->n_prefix >= format->width) {
Eric Smith8c663262007-08-25 02:26:07 +0000303 /* no padding needed, we're already bigger than the
304 requested width */
Eric Smith0cb431c2007-08-28 01:07:27 +0000305 }
306 else {
Eric Smith8c663262007-08-25 02:26:07 +0000307 /* determine which of left, space, or right padding is
308 needed */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000309 Py_ssize_t padding = format->width -
Eric Smith05212a12008-07-16 19:41:14 +0000310 (spec->n_lsign + spec->n_prefix +
311 n_digits + spec->n_rsign);
Eric Smith8c663262007-08-25 02:26:07 +0000312 if (format->align == '<')
Eric Smith05212a12008-07-16 19:41:14 +0000313 spec->n_rpadding = padding;
Eric Smith8c663262007-08-25 02:26:07 +0000314 else if (format->align == '>')
Eric Smith05212a12008-07-16 19:41:14 +0000315 spec->n_lpadding = padding;
Eric Smith8c663262007-08-25 02:26:07 +0000316 else if (format->align == '^') {
Eric Smith05212a12008-07-16 19:41:14 +0000317 spec->n_lpadding = padding / 2;
318 spec->n_rpadding = padding - spec->n_lpadding;
Eric Smith0cb431c2007-08-28 01:07:27 +0000319 }
Eric Smith185e30c2007-08-30 22:23:08 +0000320 else if (format->align == '=')
Eric Smith05212a12008-07-16 19:41:14 +0000321 spec->n_spadding = padding;
Eric Smith185e30c2007-08-30 22:23:08 +0000322 else
Eric Smith05212a12008-07-16 19:41:14 +0000323 spec->n_lpadding = padding;
Eric Smith8c663262007-08-25 02:26:07 +0000324 }
325 }
Eric Smith05212a12008-07-16 19:41:14 +0000326 spec->n_total = spec->n_lpadding + spec->n_lsign + spec->n_prefix +
327 spec->n_spadding + n_digits + spec->n_rsign + spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000328}
329
330/* fill in the non-digit parts of a numbers's string representation,
Eric Smithd68af8f2008-07-16 00:15:35 +0000331 as determined in calc_number_widths(). returns the pointer to
Eric Smith8c663262007-08-25 02:26:07 +0000332 where the digits go. */
333static STRINGLIB_CHAR *
Eric Smithb151a452008-06-24 11:21:04 +0000334fill_non_digits(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
Eric Smithd68af8f2008-07-16 00:15:35 +0000335 STRINGLIB_CHAR *prefix, Py_ssize_t n_digits,
336 STRINGLIB_CHAR fill_char)
Eric Smith8c663262007-08-25 02:26:07 +0000337{
Eric Smithd68af8f2008-07-16 00:15:35 +0000338 STRINGLIB_CHAR *p_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000339
340 if (spec->n_lpadding) {
341 STRINGLIB_FILL(p_buf, fill_char, spec->n_lpadding);
342 p_buf += spec->n_lpadding;
343 }
344 if (spec->n_lsign == 1) {
345 *p_buf++ = spec->lsign;
346 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000347 if (spec->n_prefix) {
348 memmove(p_buf,
349 prefix,
350 spec->n_prefix * sizeof(STRINGLIB_CHAR));
351 p_buf += spec->n_prefix;
352 }
Eric Smith8c663262007-08-25 02:26:07 +0000353 if (spec->n_spadding) {
354 STRINGLIB_FILL(p_buf, fill_char, spec->n_spadding);
355 p_buf += spec->n_spadding;
356 }
357 p_digits = p_buf;
358 p_buf += n_digits;
359 if (spec->n_rsign == 1) {
360 *p_buf++ = spec->rsign;
361 }
362 if (spec->n_rpadding) {
363 STRINGLIB_FILL(p_buf, fill_char, spec->n_rpadding);
364 p_buf += spec->n_rpadding;
365 }
366 return p_digits;
367}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000368#endif /* FORMAT_FLOAT || FORMAT_LONG */
Eric Smith8c663262007-08-25 02:26:07 +0000369
370/************************************************************************/
371/*********** string formatting ******************************************/
372/************************************************************************/
373
374static PyObject *
375format_string_internal(PyObject *value, const InternalFormatSpec *format)
376{
377 Py_ssize_t width; /* total field width */
378 Py_ssize_t lpad;
379 STRINGLIB_CHAR *dst;
380 STRINGLIB_CHAR *src = STRINGLIB_STR(value);
381 Py_ssize_t len = STRINGLIB_LEN(value);
382 PyObject *result = NULL;
383
384 /* sign is not allowed on strings */
385 if (format->sign != '\0') {
386 PyErr_SetString(PyExc_ValueError,
387 "Sign not allowed in string format specifier");
388 goto done;
389 }
390
Eric Smithb1ebcc62008-07-15 13:02:41 +0000391 /* alternate is not allowed on strings */
392 if (format->alternate) {
393 PyErr_SetString(PyExc_ValueError,
394 "Alternate form (#) not allowed in string format "
395 "specifier");
396 goto done;
397 }
398
Eric Smith8c663262007-08-25 02:26:07 +0000399 /* '=' alignment not allowed on strings */
400 if (format->align == '=') {
401 PyErr_SetString(PyExc_ValueError,
402 "'=' alignment not allowed "
403 "in string format specifier");
404 goto done;
405 }
406
407 /* if precision is specified, output no more that format.precision
408 characters */
409 if (format->precision >= 0 && len >= format->precision) {
410 len = format->precision;
411 }
412
413 if (format->width >= 0) {
414 width = format->width;
415
416 /* but use at least len characters */
417 if (len > width) {
418 width = len;
419 }
Eric Smith0cb431c2007-08-28 01:07:27 +0000420 }
421 else {
Eric Smith8c663262007-08-25 02:26:07 +0000422 /* not specified, use all of the chars and no more */
423 width = len;
424 }
425
426 /* allocate the resulting string */
427 result = STRINGLIB_NEW(NULL, width);
428 if (result == NULL)
429 goto done;
430
431 /* now write into that space */
432 dst = STRINGLIB_STR(result);
433
434 /* figure out how much leading space we need, based on the
435 aligning */
436 if (format->align == '>')
437 lpad = width - len;
438 else if (format->align == '^')
439 lpad = (width - len) / 2;
440 else
441 lpad = 0;
442
443 /* if right aligning, increment the destination allow space on the
444 left */
445 memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR));
446
447 /* do any padding */
448 if (width > len) {
449 STRINGLIB_CHAR fill_char = format->fill_char;
450 if (fill_char == '\0') {
451 /* use the default, if not specified */
452 fill_char = ' ';
453 }
454
455 /* pad on left */
456 if (lpad)
457 STRINGLIB_FILL(dst, fill_char, lpad);
458
459 /* pad on right */
460 if (width - len - lpad)
461 STRINGLIB_FILL(dst + len + lpad, fill_char, width - len - lpad);
462 }
463
464done:
465 return result;
466}
467
468
469/************************************************************************/
470/*********** long formatting ********************************************/
471/************************************************************************/
472
Eric Smith8fd3eba2008-02-17 19:48:00 +0000473#if defined FORMAT_LONG || defined FORMAT_INT
474typedef PyObject*
475(*IntOrLongToString)(PyObject *value, int base);
476
Eric Smith8c663262007-08-25 02:26:07 +0000477static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000478format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
479 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000480{
481 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000482 PyObject *tmp = NULL;
483 STRINGLIB_CHAR *pnumeric_chars;
484 STRINGLIB_CHAR numeric_char;
Eric Smith8c663262007-08-25 02:26:07 +0000485 STRINGLIB_CHAR sign = '\0';
486 STRINGLIB_CHAR *p;
487 Py_ssize_t n_digits; /* count of digits need from the computed
488 string */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000489 Py_ssize_t n_leading_chars;
Eric Smith5807c412008-05-11 21:00:57 +0000490 Py_ssize_t n_grouping_chars = 0; /* Count of additional chars to
491 allocate, used for 'n'
492 formatting. */
Eric Smithd68af8f2008-07-16 00:15:35 +0000493 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
494 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000495 NumberFieldWidths spec;
496 long x;
497
498 /* no precision allowed on integers */
499 if (format->precision != -1) {
500 PyErr_SetString(PyExc_ValueError,
501 "Precision not allowed in integer format specifier");
502 goto done;
503 }
504
505
506 /* special case for character formatting */
507 if (format->type == 'c') {
508 /* error to specify a sign */
509 if (format->sign != '\0') {
510 PyErr_SetString(PyExc_ValueError,
511 "Sign not allowed with integer"
512 " format specifier 'c'");
513 goto done;
514 }
515
516 /* taken from unicodeobject.c formatchar() */
517 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000518/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000519 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000520 if (x == -1 && PyErr_Occurred())
521 goto done;
522#ifdef Py_UNICODE_WIDE
523 if (x < 0 || x > 0x10ffff) {
524 PyErr_SetString(PyExc_OverflowError,
525 "%c arg not in range(0x110000) "
526 "(wide Python build)");
527 goto done;
528 }
529#else
530 if (x < 0 || x > 0xffff) {
531 PyErr_SetString(PyExc_OverflowError,
532 "%c arg not in range(0x10000) "
533 "(narrow Python build)");
534 goto done;
535 }
536#endif
Eric Smith8fd3eba2008-02-17 19:48:00 +0000537 numeric_char = (STRINGLIB_CHAR)x;
538 pnumeric_chars = &numeric_char;
539 n_digits = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000540 }
541 else {
Eric Smith8c663262007-08-25 02:26:07 +0000542 int base;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000543 int leading_chars_to_skip = 0; /* Number of characters added by
544 PyNumber_ToBase that we want to
545 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000546
547 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000548 PyNumber_ToBase */
549 switch (format->type) {
550 case 'b':
551 base = 2;
Eric Smithd68af8f2008-07-16 00:15:35 +0000552 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000553 break;
554 case 'o':
555 base = 8;
Eric Smithd68af8f2008-07-16 00:15:35 +0000556 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000557 break;
558 case 'x':
559 case 'X':
560 base = 16;
Eric Smithd68af8f2008-07-16 00:15:35 +0000561 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000562 break;
563 default: /* shouldn't be needed, but stops a compiler warning */
564 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000565 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000566 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000567 break;
568 }
569
Eric Smithd68af8f2008-07-16 00:15:35 +0000570 /* The number of prefix chars is the same as the leading
571 chars to skip */
572 if (format->alternate)
573 n_prefix = leading_chars_to_skip;
574
Eric Smith8fd3eba2008-02-17 19:48:00 +0000575 /* Do the hard part, converting to a string in a given base */
576 tmp = tostring(value, base);
577 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000578 goto done;
579
Eric Smith8fd3eba2008-02-17 19:48:00 +0000580 pnumeric_chars = STRINGLIB_STR(tmp);
581 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000582
Eric Smithd68af8f2008-07-16 00:15:35 +0000583 prefix = pnumeric_chars;
584
Eric Smith8fd3eba2008-02-17 19:48:00 +0000585 /* Remember not to modify what pnumeric_chars points to. it
586 might be interned. Only modify it after we copy it into a
587 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000588
Eric Smith8fd3eba2008-02-17 19:48:00 +0000589 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000590 and skip it */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000591 sign = pnumeric_chars[0];
Eric Smith8c663262007-08-25 02:26:07 +0000592 if (sign == '-') {
Eric Smithd68af8f2008-07-16 00:15:35 +0000593 ++prefix;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000594 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000595 }
596
Eric Smith8fd3eba2008-02-17 19:48:00 +0000597 /* Skip over the leading chars (0x, 0b, etc.) */
598 n_digits -= leading_chars_to_skip;
599 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000600 }
601
Eric Smith5807c412008-05-11 21:00:57 +0000602 if (format->type == 'n')
603 /* Compute how many additional chars we need to allocate
604 to hold the thousands grouping. */
Eric Smith6d7e7a72008-06-24 01:06:47 +0000605 STRINGLIB_GROUPING(NULL, n_digits, n_digits,
Eric Smith5807c412008-05-11 21:00:57 +0000606 0, &n_grouping_chars, 0);
607
Eric Smithb151a452008-06-24 11:21:04 +0000608 /* Calculate the widths of the various leading and trailing parts */
Eric Smithd68af8f2008-07-16 00:15:35 +0000609 calc_number_widths(&spec, sign, n_prefix, n_digits + n_grouping_chars,
610 format);
Eric Smithb151a452008-06-24 11:21:04 +0000611
Eric Smith8fd3eba2008-02-17 19:48:00 +0000612 /* Allocate a new string to hold the result */
Eric Smithb151a452008-06-24 11:21:04 +0000613 result = STRINGLIB_NEW(NULL, spec.n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000614 if (!result)
615 goto done;
616 p = STRINGLIB_STR(result);
Eric Smith8c663262007-08-25 02:26:07 +0000617
Eric Smithd68af8f2008-07-16 00:15:35 +0000618 /* XXX There is too much magic here regarding the internals of
619 spec and the location of the prefix and digits. It would be
620 better if calc_number_widths returned a number of logical
621 offsets into the buffer, and those were used. Maybe in a
622 future code cleanup. */
623
Eric Smith8fd3eba2008-02-17 19:48:00 +0000624 /* Fill in the digit parts */
Eric Smithd68af8f2008-07-16 00:15:35 +0000625 n_leading_chars = spec.n_lpadding + spec.n_lsign +
626 spec.n_prefix + spec.n_spadding;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000627 memmove(p + n_leading_chars,
628 pnumeric_chars,
629 n_digits * sizeof(STRINGLIB_CHAR));
630
Eric Smithd68af8f2008-07-16 00:15:35 +0000631 /* If type is 'X', convert the filled in digits to uppercase */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000632 if (format->type == 'X') {
633 Py_ssize_t t;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000634 for (t = 0; t < n_digits; ++t)
Eric Smith8fd3eba2008-02-17 19:48:00 +0000635 p[t + n_leading_chars] = STRINGLIB_TOUPPER(p[t + n_leading_chars]);
Eric Smith8c663262007-08-25 02:26:07 +0000636 }
637
Eric Smithd68af8f2008-07-16 00:15:35 +0000638 /* Insert the grouping, if any, after the uppercasing of the digits, so
639 we can ensure that grouping chars won't be affected. */
Eric Smithb151a452008-06-24 11:21:04 +0000640 if (n_grouping_chars) {
Eric Smith5807c412008-05-11 21:00:57 +0000641 /* We know this can't fail, since we've already
642 reserved enough space. */
643 STRINGLIB_CHAR *pstart = p + n_leading_chars;
Neal Norwitz2f99b242008-08-24 05:48:10 +0000644#ifndef NDEBUG
645 int r =
646#endif
647 STRINGLIB_GROUPING(pstart, n_digits, n_digits,
Eric Smithd68af8f2008-07-16 00:15:35 +0000648 spec.n_total+n_grouping_chars-n_leading_chars,
649 NULL, 0);
Eric Smith5807c412008-05-11 21:00:57 +0000650 assert(r);
651 }
652
Eric Smithb151a452008-06-24 11:21:04 +0000653 /* Fill in the non-digit parts (padding, sign, etc.) */
Eric Smithd68af8f2008-07-16 00:15:35 +0000654 fill_non_digits(p, &spec, prefix, n_digits + n_grouping_chars,
Eric Smithb151a452008-06-24 11:21:04 +0000655 format->fill_char == '\0' ? ' ' : format->fill_char);
Eric Smith8c663262007-08-25 02:26:07 +0000656
Eric Smithd68af8f2008-07-16 00:15:35 +0000657 /* If type is 'X', uppercase the prefix. This has to be done after the
658 prefix is filled in by fill_non_digits */
659 if (format->type == 'X') {
660 Py_ssize_t t;
661 for (t = 0; t < n_prefix; ++t)
662 p[t + spec.n_lpadding + spec.n_lsign] =
663 STRINGLIB_TOUPPER(p[t + spec.n_lpadding + spec.n_lsign]);
664 }
665
666
Eric Smith8c663262007-08-25 02:26:07 +0000667done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000668 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000669 return result;
670}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000671#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000672
673/************************************************************************/
674/*********** float formatting *******************************************/
675/************************************************************************/
676
Eric Smith8fd3eba2008-02-17 19:48:00 +0000677#ifdef FORMAT_FLOAT
678#if STRINGLIB_IS_UNICODE
Eric Smith8c663262007-08-25 02:26:07 +0000679/* taken from unicodeobject.c */
680static Py_ssize_t
681strtounicode(Py_UNICODE *buffer, const char *charbuffer)
682{
683 register Py_ssize_t i;
684 Py_ssize_t len = strlen(charbuffer);
Christian Heimesc3f30c42008-02-22 16:37:40 +0000685 for (i = len - 1; i >= 0; --i)
Eric Smith185e30c2007-08-30 22:23:08 +0000686 buffer[i] = (Py_UNICODE) charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000687
688 return len;
689}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000690#endif
Eric Smith8c663262007-08-25 02:26:07 +0000691
Eric Smith8c663262007-08-25 02:26:07 +0000692/* see FORMATBUFLEN in unicodeobject.c */
693#define FLOAT_FORMATBUFLEN 120
694
695/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000696static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000697format_float_internal(PyObject *value,
698 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000699{
700 /* fmt = '%.' + `prec` + `type` + '%%'
701 worst case length = 2 + 10 (len of INT_MAX) + 1 + 2 = 15 (use 20)*/
702 char fmt[20];
703
704 /* taken from unicodeobject.c */
705 /* Worst case length calc to ensure no buffer overrun:
706
707 'g' formats:
Eric Smith185e30c2007-08-30 22:23:08 +0000708 fmt = %#.<prec>g
709 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
710 for any double rep.)
711 len = 1 + prec + 1 + 2 + 5 = 9 + prec
Eric Smith8c663262007-08-25 02:26:07 +0000712
713 'f' formats:
Eric Smith185e30c2007-08-30 22:23:08 +0000714 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
715 len = 1 + 50 + 1 + prec = 52 + prec
Eric Smith8c663262007-08-25 02:26:07 +0000716
717 If prec=0 the effective precision is 1 (the leading digit is
718 always given), therefore increase the length by one.
719
720 */
721 char charbuf[FLOAT_FORMATBUFLEN];
722 Py_ssize_t n_digits;
723 double x;
724 Py_ssize_t precision = format->precision;
725 PyObject *result = NULL;
726 STRINGLIB_CHAR sign;
727 char* trailing = "";
728 STRINGLIB_CHAR *p;
729 NumberFieldWidths spec;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000730 STRINGLIB_CHAR type = format->type;
Eric Smith8c663262007-08-25 02:26:07 +0000731
732#if STRINGLIB_IS_UNICODE
733 Py_UNICODE unicodebuf[FLOAT_FORMATBUFLEN];
734#endif
735
Eric Smithb1ebcc62008-07-15 13:02:41 +0000736 /* alternate is not allowed on floats. */
737 if (format->alternate) {
738 PyErr_SetString(PyExc_ValueError,
739 "Alternate form (#) not allowed in float format "
740 "specifier");
741 goto done;
742 }
743
Eric Smith8c663262007-08-25 02:26:07 +0000744 /* first, do the conversion as 8-bit chars, using the platform's
745 snprintf. then, if needed, convert to unicode. */
746
Eric Smith22b85b32008-07-17 19:18:29 +0000747 /* 'F' is the same as 'f', per the PEP */
748 if (type == 'F')
749 type = 'f';
750
Eric Smith8c663262007-08-25 02:26:07 +0000751 x = PyFloat_AsDouble(value);
752
753 if (x == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000754 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000755
756 if (type == '%') {
757 type = 'f';
758 x *= 100;
759 trailing = "%";
760 }
761
762 if (precision < 0)
Eric Smith185e30c2007-08-30 22:23:08 +0000763 precision = 6;
Eric Smith22b85b32008-07-17 19:18:29 +0000764 if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
765 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000766
767 /* cast "type", because if we're in unicode we need to pass a
768 8-bit char. this is safe, because we've restricted what "type"
769 can be */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000770 PyOS_snprintf(fmt, sizeof(fmt), "%%.%" PY_FORMAT_SIZE_T "d%c", precision,
771 (char)type);
Eric Smith8c663262007-08-25 02:26:07 +0000772
Christian Heimesc3f30c42008-02-22 16:37:40 +0000773 /* do the actual formatting */
774 PyOS_ascii_formatd(charbuf, sizeof(charbuf), fmt, x);
Eric Smith8c663262007-08-25 02:26:07 +0000775
776 /* adding trailing to fmt with PyOS_snprintf doesn't work, not
777 sure why. we'll just concatentate it here, no harm done. we
778 know we can't have a buffer overflow from the fmt size
779 analysis */
780 strcat(charbuf, trailing);
781
782 /* rather than duplicate the code for snprintf for both unicode
783 and 8 bit strings, we just use the 8 bit version and then
784 convert to unicode in a separate code path. that's probably
785 the lesser of 2 evils. */
786#if STRINGLIB_IS_UNICODE
787 n_digits = strtounicode(unicodebuf, charbuf);
788 p = unicodebuf;
789#else
790 /* compute the length. I believe this is done because the return
791 value from snprintf above is unreliable */
792 n_digits = strlen(charbuf);
793 p = charbuf;
794#endif
795
796 /* is a sign character present in the output? if so, remember it
797 and skip it */
798 sign = p[0];
799 if (sign == '-') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000800 ++p;
801 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000802 }
803
Eric Smithb1ebcc62008-07-15 13:02:41 +0000804 calc_number_widths(&spec, sign, 0, n_digits, format);
Eric Smith8c663262007-08-25 02:26:07 +0000805
806 /* allocate a string with enough space */
807 result = STRINGLIB_NEW(NULL, spec.n_total);
808 if (result == NULL)
809 goto done;
810
Eric Smithb151a452008-06-24 11:21:04 +0000811 /* Fill in the non-digit parts (padding, sign, etc.) */
Eric Smithd68af8f2008-07-16 00:15:35 +0000812 fill_non_digits(STRINGLIB_STR(result), &spec, NULL, n_digits,
Eric Smithb151a452008-06-24 11:21:04 +0000813 format->fill_char == '\0' ? ' ' : format->fill_char);
Eric Smith8c663262007-08-25 02:26:07 +0000814
815 /* fill in the digit parts */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000816 memmove(STRINGLIB_STR(result) +
817 (spec.n_lpadding + spec.n_lsign + spec.n_spadding),
Eric Smith8c663262007-08-25 02:26:07 +0000818 p,
819 n_digits * sizeof(STRINGLIB_CHAR));
820
821done:
822 return result;
823}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000824#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +0000825
826/************************************************************************/
827/*********** built in formatters ****************************************/
828/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +0000829PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +0000830FORMAT_STRING(PyObject *obj,
831 STRINGLIB_CHAR *format_spec,
832 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +0000833{
Eric Smith8c663262007-08-25 02:26:07 +0000834 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000835 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000836
837 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +0000838 it equivalent to str(obj) */
839 if (format_spec_len == 0) {
840 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +0000841 goto done;
842 }
843
844 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000845 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
846 &format, 's'))
Eric Smith8c663262007-08-25 02:26:07 +0000847 goto done;
848
849 /* type conversion? */
850 switch (format.type) {
851 case 's':
852 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000853 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +0000854 break;
Eric Smith8c663262007-08-25 02:26:07 +0000855 default:
856 /* unknown */
Martin v. Löwis5a6f4582008-04-07 03:22:07 +0000857 #if STRINGLIB_IS_UNICODE
858 /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
859 hence the two cases. If it is char, gcc complains that the
860 condition below is always true, hence the ifdef. */
861 if (format.type > 32 && format.type <128)
862 #endif
863 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
864 (char)format.type);
865 #if STRINGLIB_IS_UNICODE
866 else
867 PyErr_Format(PyExc_ValueError, "Unknown conversion type '\\x%x'",
868 (unsigned int)format.type);
869 #endif
Eric Smith8c663262007-08-25 02:26:07 +0000870 goto done;
871 }
872
873done:
Eric Smith8c663262007-08-25 02:26:07 +0000874 return result;
875}
876
Eric Smith8fd3eba2008-02-17 19:48:00 +0000877#if defined FORMAT_LONG || defined FORMAT_INT
878static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +0000879format_int_or_long(PyObject* obj,
880 STRINGLIB_CHAR *format_spec,
881 Py_ssize_t format_spec_len,
882 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000883{
Eric Smith8c663262007-08-25 02:26:07 +0000884 PyObject *result = NULL;
885 PyObject *tmp = NULL;
886 InternalFormatSpec format;
887
Eric Smith8c663262007-08-25 02:26:07 +0000888 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +0000889 it equivalent to str(obj) */
890 if (format_spec_len == 0) {
891 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +0000892 goto done;
893 }
894
895 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000896 if (!parse_internal_render_format_spec(format_spec,
897 format_spec_len,
898 &format, 'd'))
Eric Smith8c663262007-08-25 02:26:07 +0000899 goto done;
900
901 /* type conversion? */
902 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +0000903 case 'b':
904 case 'c':
905 case 'd':
906 case 'o':
907 case 'x':
908 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +0000909 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +0000910 /* no type conversion needed, already an int (or long). do
911 the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000912 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +0000913 break;
914
Eric Smithfa767ef2008-01-28 10:59:27 +0000915 case 'e':
916 case 'E':
917 case 'f':
918 case 'F':
919 case 'g':
920 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +0000921 case '%':
922 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +0000923 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +0000924 if (tmp == NULL)
925 goto done;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000926 result = format_float_internal(obj, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +0000927 break;
928
Eric Smith8c663262007-08-25 02:26:07 +0000929 default:
930 /* unknown */
931 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
932 format.type);
933 goto done;
934 }
935
936done:
937 Py_XDECREF(tmp);
938 return result;
939}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000940#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000941
Eric Smith8fd3eba2008-02-17 19:48:00 +0000942#ifdef FORMAT_LONG
943/* Need to define long_format as a function that will convert a long
944 to a string. In 3.0, _PyLong_Format has the correct signature. In
945 2.x, we need to fudge a few parameters */
946#if PY_VERSION_HEX >= 0x03000000
947#define long_format _PyLong_Format
948#else
949static PyObject*
950long_format(PyObject* value, int base)
951{
952 /* Convert to base, don't add trailing 'L', and use the new octal
953 format. We already know this is a long object */
954 assert(PyLong_Check(value));
955 /* convert to base, don't add 'L', and use the new octal format */
956 return _PyLong_Format(value, base, 0, 1);
957}
958#endif
959
960PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +0000961FORMAT_LONG(PyObject *obj,
962 STRINGLIB_CHAR *format_spec,
963 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +0000964{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000965 return format_int_or_long(obj, format_spec, format_spec_len,
966 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000967}
968#endif /* FORMAT_LONG */
969
970#ifdef FORMAT_INT
971/* this is only used for 2.x, not 3.0 */
972static PyObject*
973int_format(PyObject* value, int base)
974{
975 /* Convert to base, and use the new octal format. We already
976 know this is an int object */
977 assert(PyInt_Check(value));
978 return _PyInt_Format((PyIntObject*)value, base, 1);
979}
980
981PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +0000982FORMAT_INT(PyObject *obj,
983 STRINGLIB_CHAR *format_spec,
984 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +0000985{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000986 return format_int_or_long(obj, format_spec, format_spec_len,
987 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000988}
989#endif /* FORMAT_INT */
990
991#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +0000992PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +0000993FORMAT_FLOAT(PyObject *obj,
994 STRINGLIB_CHAR *format_spec,
995 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +0000996{
Eric Smith8c663262007-08-25 02:26:07 +0000997 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000998 InternalFormatSpec format;
999
Eric Smith8c663262007-08-25 02:26:07 +00001000 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001001 it equivalent to str(obj) */
1002 if (format_spec_len == 0) {
1003 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001004 goto done;
1005 }
1006
1007 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001008 if (!parse_internal_render_format_spec(format_spec,
1009 format_spec_len,
1010 &format, '\0'))
Eric Smith8c663262007-08-25 02:26:07 +00001011 goto done;
1012
1013 /* type conversion? */
1014 switch (format.type) {
Christian Heimesb186d002008-03-18 15:15:01 +00001015 case '\0':
1016 /* 'Z' means like 'g', but with at least one decimal. See
1017 PyOS_ascii_formatd */
1018 format.type = 'Z';
1019 /* Deliberate fall through to the next case statement */
Eric Smith8c663262007-08-25 02:26:07 +00001020 case 'e':
1021 case 'E':
1022 case 'f':
1023 case 'F':
1024 case 'g':
1025 case 'G':
1026 case 'n':
1027 case '%':
1028 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001029 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001030 break;
1031
1032 default:
1033 /* unknown */
1034 PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
1035 format.type);
1036 goto done;
1037 }
1038
1039done:
Eric Smith8c663262007-08-25 02:26:07 +00001040 return result;
1041}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001042#endif /* FORMAT_FLOAT */