blob: 9cbd2cc9310b35cb44565a1b2f8777245e256459 [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
14 to be whatever you want the public names of these functions to
15 be. These are the only non-static functions defined here.
16*/
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 Smith8c663262007-08-25 02:26:07 +0000107 return 1;
108 default:
109 return 0;
110 }
111}
112
113
114typedef struct {
115 STRINGLIB_CHAR fill_char;
116 STRINGLIB_CHAR align;
Eric Smithb1ebcc62008-07-15 13:02:41 +0000117 int alternate;
Eric Smith8c663262007-08-25 02:26:07 +0000118 STRINGLIB_CHAR sign;
119 Py_ssize_t width;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000120 int thousands_separators;
Eric Smith8c663262007-08-25 02:26:07 +0000121 Py_ssize_t precision;
122 STRINGLIB_CHAR type;
123} InternalFormatSpec;
124
125/*
126 ptr points to the start of the format_spec, end points just past its end.
127 fills in format with the parsed information.
128 returns 1 on success, 0 on failure.
129 if failure, sets the exception
130*/
131static int
Eric Smith4a7d76d2008-05-30 18:10:19 +0000132parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000133 Py_ssize_t format_spec_len,
Eric Smith8c663262007-08-25 02:26:07 +0000134 InternalFormatSpec *format,
135 char default_type)
136{
Eric Smith4a7d76d2008-05-30 18:10:19 +0000137 STRINGLIB_CHAR *ptr = format_spec;
138 STRINGLIB_CHAR *end = format_spec + format_spec_len;
Eric Smith8c663262007-08-25 02:26:07 +0000139
140 /* end-ptr is used throughout this code to specify the length of
141 the input string */
142
Eric Smith0923d1d2009-04-16 20:16:10 +0000143 Py_ssize_t consumed;
Eric Smith8c663262007-08-25 02:26:07 +0000144
145 format->fill_char = '\0';
146 format->align = '\0';
Eric Smithb1ebcc62008-07-15 13:02:41 +0000147 format->alternate = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000148 format->sign = '\0';
149 format->width = -1;
Eric Smitha3b1ac82009-04-03 14:45:06 +0000150 format->thousands_separators = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000151 format->precision = -1;
152 format->type = default_type;
153
154 /* If the second char is an alignment token,
155 then parse the fill char */
156 if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
157 format->align = ptr[1];
158 format->fill_char = ptr[0];
159 ptr += 2;
Eric Smith0cb431c2007-08-28 01:07:27 +0000160 }
161 else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
Eric Smith8c663262007-08-25 02:26:07 +0000162 format->align = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000163 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000164 }
165
166 /* Parse the various sign options */
167 if (end-ptr >= 1 && is_sign_element(ptr[0])) {
168 format->sign = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000169 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000170 }
171
Eric Smithd68af8f2008-07-16 00:15:35 +0000172 /* If the next character is #, we're in alternate mode. This only
173 applies to integers. */
174 if (end-ptr >= 1 && ptr[0] == '#') {
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000175 format->alternate = 1;
176 ++ptr;
Eric Smithd68af8f2008-07-16 00:15:35 +0000177 }
178
Eric Smith8c663262007-08-25 02:26:07 +0000179 /* The special case for 0-padding (backwards compat) */
Eric Smith185e30c2007-08-30 22:23:08 +0000180 if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
Eric Smith8c663262007-08-25 02:26:07 +0000181 format->fill_char = '0';
182 if (format->align == '\0') {
183 format->align = '=';
184 }
Christian Heimesc3f30c42008-02-22 16:37:40 +0000185 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000186 }
187
Eric Smith0923d1d2009-04-16 20:16:10 +0000188 consumed = get_integer(&ptr, end, &format->width);
189 if (consumed == -1)
190 /* Overflow error. Exception already set. */
191 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000192
Eric Smith0923d1d2009-04-16 20:16:10 +0000193 /* If consumed is 0, we didn't consume any characters for the
194 width. In that case, reset the width to -1, because
195 get_integer() will have set it to zero. -1 is how we record
196 that the width wasn't specified. */
197 if (consumed == 0)
Eric Smith8c663262007-08-25 02:26:07 +0000198 format->width = -1;
Eric Smith8c663262007-08-25 02:26:07 +0000199
Eric Smitha3b1ac82009-04-03 14:45:06 +0000200 /* Comma signifies add thousands separators */
201 if (end-ptr && ptr[0] == ',') {
202 format->thousands_separators = 1;
203 ++ptr;
204 }
205
Eric Smith8c663262007-08-25 02:26:07 +0000206 /* Parse field precision */
207 if (end-ptr && ptr[0] == '.') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000208 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000209
Eric Smith0923d1d2009-04-16 20:16:10 +0000210 consumed = get_integer(&ptr, end, &format->precision);
211 if (consumed == -1)
212 /* Overflow error. Exception already set. */
213 return 0;
Eric Smith8c663262007-08-25 02:26:07 +0000214
Eric Smith0923d1d2009-04-16 20:16:10 +0000215 /* Not having a precision after a dot is an error. */
216 if (consumed == 0) {
Eric Smith8c663262007-08-25 02:26:07 +0000217 PyErr_Format(PyExc_ValueError,
218 "Format specifier missing precision");
219 return 0;
220 }
221
222 }
223
Eric Smith0923d1d2009-04-16 20:16:10 +0000224 /* Finally, parse the type field. */
Eric Smith8c663262007-08-25 02:26:07 +0000225
226 if (end-ptr > 1) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000227 /* More than one char remain, invalid conversion spec. */
Eric Smith8c663262007-08-25 02:26:07 +0000228 PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
229 return 0;
230 }
231
232 if (end-ptr == 1) {
233 format->type = ptr[0];
Christian Heimesc3f30c42008-02-22 16:37:40 +0000234 ++ptr;
Eric Smith8c663262007-08-25 02:26:07 +0000235 }
236
Eric Smith0923d1d2009-04-16 20:16:10 +0000237 /* Do as much validating as we can, just by looking at the format
238 specifier. Do not take into account what type of formatting
239 we're doing (int, float, string). */
240
241 if (format->thousands_separators) {
242 switch (format->type) {
243 case 'd':
244 case 'e':
245 case 'f':
246 case 'g':
247 case 'E':
248 case 'G':
249 case '%':
250 case 'F':
Eric Smith937491d2009-04-22 17:04:27 +0000251 case '\0':
Eric Smith0923d1d2009-04-16 20:16:10 +0000252 /* These are allowed. See PEP 378.*/
253 break;
254 default:
255 PyErr_Format(PyExc_ValueError,
256 "Cannot specify ',' with '%c'.", format->type);
257 return 0;
258 }
Eric Smitha3b1ac82009-04-03 14:45:06 +0000259 }
260
Eric Smith8c663262007-08-25 02:26:07 +0000261 return 1;
262}
263
Eric Smith8fd3eba2008-02-17 19:48:00 +0000264#if defined FORMAT_FLOAT || defined FORMAT_LONG
Eric Smith8c663262007-08-25 02:26:07 +0000265/************************************************************************/
266/*********** common routines for numeric formatting *********************/
267/************************************************************************/
268
Eric Smith0923d1d2009-04-16 20:16:10 +0000269/* Locale type codes. */
270#define LT_CURRENT_LOCALE 0
271#define LT_DEFAULT_LOCALE 1
272#define LT_NO_LOCALE 2
273
274/* Locale info needed for formatting integers and the part of floats
275 before and including the decimal. Note that locales only support
276 8-bit chars, not unicode. */
277typedef struct {
278 char *decimal_point;
279 char *thousands_sep;
280 char *grouping;
281} LocaleInfo;
282
Eric Smith8c663262007-08-25 02:26:07 +0000283/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000284 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000285typedef struct {
286 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000287 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000288 Py_ssize_t n_spadding;
289 Py_ssize_t n_rpadding;
Eric Smith0923d1d2009-04-16 20:16:10 +0000290 char sign;
291 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
292 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
293 any grouping chars. */
294 Py_ssize_t n_decimal; /* 0 if only an integer */
295 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
296 excluding the decimal itself, if
297 present. */
298
299 /* These 2 are not the widths of fields, but are needed by
300 STRINGLIB_GROUPING. */
301 Py_ssize_t n_digits; /* The number of digits before a decimal
302 or exponent. */
303 Py_ssize_t n_min_width; /* The min_width we used when we computed
304 the n_grouped_digits width. */
Eric Smith8c663262007-08-25 02:26:07 +0000305} NumberFieldWidths;
306
Eric Smith0923d1d2009-04-16 20:16:10 +0000307/* Given a number of the form:
308 digits[remainder]
309 where ptr points to the start and end points to the end, find where
310 the integer part ends. This could be a decimal, an exponent, both,
311 or neither.
312 If a decimal point is present, set *has_decimal and increment
313 remainder beyond it.
314 Results are undefined (but shouldn't crash) for improperly
315 formatted strings.
316*/
317static void
318parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
319 Py_ssize_t *n_remainder, int *has_decimal)
320{
321 STRINGLIB_CHAR *end = ptr + len;
322 STRINGLIB_CHAR *remainder;
323
324 while (ptr<end && isdigit(*ptr))
325 ++ptr;
326 remainder = ptr;
327
328 /* Does remainder start with a decimal point? */
329 *has_decimal = ptr<end && *remainder == '.';
330
331 /* Skip the decimal point. */
332 if (*has_decimal)
333 remainder++;
334
335 *n_remainder = end - remainder;
336}
337
Eric Smith8c663262007-08-25 02:26:07 +0000338/* not all fields of format are used. for example, precision is
339 unused. should this take discrete params in order to be more clear
340 about what it does? or is passing a single format parameter easier
341 and more efficient enough to justify a little obfuscation? */
Eric Smith0923d1d2009-04-16 20:16:10 +0000342static Py_ssize_t
343calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
344 STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
345 Py_ssize_t n_number, Py_ssize_t n_remainder,
346 int has_decimal, const LocaleInfo *locale,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000347 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000348{
Eric Smith0923d1d2009-04-16 20:16:10 +0000349 Py_ssize_t n_non_digit_non_padding;
350 Py_ssize_t n_padding;
351
352 spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
Eric Smith05212a12008-07-16 19:41:14 +0000353 spec->n_lpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000354 spec->n_prefix = n_prefix;
355 spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
356 spec->n_remainder = n_remainder;
Eric Smith05212a12008-07-16 19:41:14 +0000357 spec->n_spadding = 0;
358 spec->n_rpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000359 spec->sign = '\0';
360 spec->n_sign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000361
362 /* the output will look like:
Eric Smith0923d1d2009-04-16 20:16:10 +0000363 | |
364 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
365 | |
Eric Smith8c663262007-08-25 02:26:07 +0000366
Eric Smith0923d1d2009-04-16 20:16:10 +0000367 sign is computed from format->sign and the actual
Eric Smith8c663262007-08-25 02:26:07 +0000368 sign of the number
369
Eric Smithb1ebcc62008-07-15 13:02:41 +0000370 prefix is given (it's for the '0x' prefix)
371
Eric Smith8c663262007-08-25 02:26:07 +0000372 digits is already known
373
374 the total width is either given, or computed from the
375 actual digits
376
377 only one of lpadding, spadding, and rpadding can be non-zero,
378 and it's calculated from the width and other fields
379 */
380
381 /* compute the various parts we're going to write */
Eric Smith0923d1d2009-04-16 20:16:10 +0000382 switch (format->sign) {
383 case '+':
Eric Smith8c663262007-08-25 02:26:07 +0000384 /* always put a + or - */
Eric Smith0923d1d2009-04-16 20:16:10 +0000385 spec->n_sign = 1;
386 spec->sign = (sign_char == '-' ? '-' : '+');
387 break;
388 case ' ':
389 spec->n_sign = 1;
390 spec->sign = (sign_char == '-' ? '-' : ' ');
391 break;
392 default:
393 /* Not specified, or the default (-) */
394 if (sign_char == '-') {
395 spec->n_sign = 1;
396 spec->sign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000397 }
398 }
399
Eric Smith0923d1d2009-04-16 20:16:10 +0000400 /* The number of chars used for non-digits and non-padding. */
401 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
402 spec->n_remainder;
Eric Smithd68af8f2008-07-16 00:15:35 +0000403
Eric Smith0923d1d2009-04-16 20:16:10 +0000404 /* min_width can go negative, that's okay. format->width == -1 means
405 we don't care. */
406 if (format->fill_char == '0')
407 spec->n_min_width = format->width - n_non_digit_non_padding;
408 else
409 spec->n_min_width = 0;
410
411 if (spec->n_digits == 0)
412 /* This case only occurs when using 'c' formatting, we need
413 to special case it because the grouping code always wants
414 to have at least one character. */
415 spec->n_grouped_digits = 0;
416 else
417 spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
418 spec->n_digits,
419 spec->n_min_width,
420 locale->grouping,
421 locale->thousands_sep);
422
423 /* Given the desired width and the total of digit and non-digit
424 space we consume, see if we need any padding. format->width can
425 be negative (meaning no padding), but this code still works in
426 that case. */
427 n_padding = format->width -
428 (n_non_digit_non_padding + spec->n_grouped_digits);
429 if (n_padding > 0) {
430 /* Some padding is needed. Determine if it's left, space, or right. */
431 switch (format->align) {
432 case '<':
433 spec->n_rpadding = n_padding;
434 break;
435 case '^':
436 spec->n_lpadding = n_padding / 2;
437 spec->n_rpadding = n_padding - spec->n_lpadding;
438 break;
439 case '=':
440 spec->n_spadding = n_padding;
441 break;
442 default:
443 /* Handles '>', plus catch-all just in case. */
444 spec->n_lpadding = n_padding;
445 break;
Eric Smith8c663262007-08-25 02:26:07 +0000446 }
447 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000448 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
449 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
450 spec->n_remainder + spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000451}
452
Eric Smith0923d1d2009-04-16 20:16:10 +0000453/* Fill in the digit parts of a numbers's string representation,
454 as determined in calc_number_widths().
455 No error checking, since we know the buffer is the correct size. */
456static void
457fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
458 STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
459 STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
460 LocaleInfo *locale, int toupper)
Eric Smith8c663262007-08-25 02:26:07 +0000461{
Eric Smith0923d1d2009-04-16 20:16:10 +0000462 /* Used to keep track of digits, decimal, and remainder. */
463 STRINGLIB_CHAR *p = digits;
464
465#ifndef NDEBUG
466 Py_ssize_t r;
467#endif
Eric Smith8c663262007-08-25 02:26:07 +0000468
469 if (spec->n_lpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000470 STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
471 buf += spec->n_lpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000472 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000473 if (spec->n_sign == 1) {
474 *buf++ = spec->sign;
Eric Smith8c663262007-08-25 02:26:07 +0000475 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000476 if (spec->n_prefix) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000477 memmove(buf,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000478 prefix,
479 spec->n_prefix * sizeof(STRINGLIB_CHAR));
Eric Smith0923d1d2009-04-16 20:16:10 +0000480 if (toupper) {
481 Py_ssize_t t;
482 for (t = 0; t < spec->n_prefix; ++t)
483 buf[t] = STRINGLIB_TOUPPER(buf[t]);
484 }
485 buf += spec->n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000486 }
Eric Smith8c663262007-08-25 02:26:07 +0000487 if (spec->n_spadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000488 STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
489 buf += spec->n_spadding;
Eric Smith8c663262007-08-25 02:26:07 +0000490 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000491
492 /* Only for type 'c' special case, it has no digits. */
493 if (spec->n_digits != 0) {
494 /* Fill the digits with InsertThousandsGrouping. */
495#ifndef NDEBUG
496 r =
497#endif
498 STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
499 spec->n_digits, spec->n_min_width,
500 locale->grouping, locale->thousands_sep);
501#ifndef NDEBUG
502 assert(r == spec->n_grouped_digits);
503#endif
504 p += spec->n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000505 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000506 if (toupper) {
507 Py_ssize_t t;
508 for (t = 0; t < spec->n_grouped_digits; ++t)
509 buf[t] = STRINGLIB_TOUPPER(buf[t]);
510 }
511 buf += spec->n_grouped_digits;
512
513 if (spec->n_decimal) {
514 Py_ssize_t t;
515 for (t = 0; t < spec->n_decimal; ++t)
516 buf[t] = locale->decimal_point[t];
517 buf += spec->n_decimal;
518 p += 1;
519 }
520
521 if (spec->n_remainder) {
522 memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
523 buf += spec->n_remainder;
524 p += spec->n_remainder;
525 }
526
Eric Smith8c663262007-08-25 02:26:07 +0000527 if (spec->n_rpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000528 STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
529 buf += spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000530 }
Eric Smith8c663262007-08-25 02:26:07 +0000531}
Eric Smith0923d1d2009-04-16 20:16:10 +0000532
533static char no_grouping[1] = {CHAR_MAX};
534
535/* Find the decimal point character(s?), thousands_separator(s?), and
536 grouping description, either for the current locale if type is
537 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
538 none if LT_NO_LOCALE. */
539static void
540get_locale_info(int type, LocaleInfo *locale_info)
541{
542 switch (type) {
543 case LT_CURRENT_LOCALE: {
544 struct lconv *locale_data = localeconv();
545 locale_info->decimal_point = locale_data->decimal_point;
546 locale_info->thousands_sep = locale_data->thousands_sep;
547 locale_info->grouping = locale_data->grouping;
548 break;
549 }
550 case LT_DEFAULT_LOCALE:
551 locale_info->decimal_point = ".";
552 locale_info->thousands_sep = ",";
553 locale_info->grouping = "\3"; /* Group every 3 characters,
554 trailing 0 means repeat
555 infinitely. */
556 break;
557 case LT_NO_LOCALE:
558 locale_info->decimal_point = ".";
559 locale_info->thousands_sep = "";
560 locale_info->grouping = no_grouping;
561 break;
562 default:
563 assert(0);
564 }
565}
566
Eric Smith8fd3eba2008-02-17 19:48:00 +0000567#endif /* FORMAT_FLOAT || FORMAT_LONG */
Eric Smith8c663262007-08-25 02:26:07 +0000568
569/************************************************************************/
570/*********** string formatting ******************************************/
571/************************************************************************/
572
573static PyObject *
574format_string_internal(PyObject *value, const InternalFormatSpec *format)
575{
576 Py_ssize_t width; /* total field width */
577 Py_ssize_t lpad;
578 STRINGLIB_CHAR *dst;
579 STRINGLIB_CHAR *src = STRINGLIB_STR(value);
580 Py_ssize_t len = STRINGLIB_LEN(value);
581 PyObject *result = NULL;
582
583 /* sign is not allowed on strings */
584 if (format->sign != '\0') {
585 PyErr_SetString(PyExc_ValueError,
586 "Sign not allowed in string format specifier");
587 goto done;
588 }
589
Eric Smithb1ebcc62008-07-15 13:02:41 +0000590 /* alternate is not allowed on strings */
591 if (format->alternate) {
592 PyErr_SetString(PyExc_ValueError,
593 "Alternate form (#) not allowed in string format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000594 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000595 goto done;
596 }
597
Eric Smith8c663262007-08-25 02:26:07 +0000598 /* '=' alignment not allowed on strings */
599 if (format->align == '=') {
600 PyErr_SetString(PyExc_ValueError,
601 "'=' alignment not allowed "
602 "in string format specifier");
603 goto done;
604 }
605
606 /* if precision is specified, output no more that format.precision
607 characters */
608 if (format->precision >= 0 && len >= format->precision) {
609 len = format->precision;
610 }
611
612 if (format->width >= 0) {
613 width = format->width;
614
615 /* but use at least len characters */
616 if (len > width) {
617 width = len;
618 }
Eric Smith0cb431c2007-08-28 01:07:27 +0000619 }
620 else {
Eric Smith8c663262007-08-25 02:26:07 +0000621 /* not specified, use all of the chars and no more */
622 width = len;
623 }
624
625 /* allocate the resulting string */
626 result = STRINGLIB_NEW(NULL, width);
627 if (result == NULL)
628 goto done;
629
630 /* now write into that space */
631 dst = STRINGLIB_STR(result);
632
633 /* figure out how much leading space we need, based on the
634 aligning */
635 if (format->align == '>')
636 lpad = width - len;
637 else if (format->align == '^')
638 lpad = (width - len) / 2;
639 else
640 lpad = 0;
641
642 /* if right aligning, increment the destination allow space on the
643 left */
644 memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR));
645
646 /* do any padding */
647 if (width > len) {
648 STRINGLIB_CHAR fill_char = format->fill_char;
649 if (fill_char == '\0') {
650 /* use the default, if not specified */
651 fill_char = ' ';
652 }
653
654 /* pad on left */
655 if (lpad)
656 STRINGLIB_FILL(dst, fill_char, lpad);
657
658 /* pad on right */
659 if (width - len - lpad)
660 STRINGLIB_FILL(dst + len + lpad, fill_char, width - len - lpad);
661 }
662
663done:
664 return result;
665}
666
667
668/************************************************************************/
669/*********** long formatting ********************************************/
670/************************************************************************/
671
Eric Smith8fd3eba2008-02-17 19:48:00 +0000672#if defined FORMAT_LONG || defined FORMAT_INT
673typedef PyObject*
674(*IntOrLongToString)(PyObject *value, int base);
675
Eric Smith8c663262007-08-25 02:26:07 +0000676static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000677format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000678 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000679{
680 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000681 PyObject *tmp = NULL;
682 STRINGLIB_CHAR *pnumeric_chars;
683 STRINGLIB_CHAR numeric_char;
Eric Smith0923d1d2009-04-16 20:16:10 +0000684 STRINGLIB_CHAR sign_char = '\0';
Eric Smith8c663262007-08-25 02:26:07 +0000685 Py_ssize_t n_digits; /* count of digits need from the computed
686 string */
Eric Smith0923d1d2009-04-16 20:16:10 +0000687 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
688 produces non-digits */
Eric Smithd68af8f2008-07-16 00:15:35 +0000689 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
Eric Smith0923d1d2009-04-16 20:16:10 +0000690 Py_ssize_t n_total;
Eric Smithd68af8f2008-07-16 00:15:35 +0000691 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000692 NumberFieldWidths spec;
693 long x;
694
Eric Smith0923d1d2009-04-16 20:16:10 +0000695 /* Locale settings, either from the actual locale or
696 from a hard-code pseudo-locale */
697 LocaleInfo locale;
698
Eric Smith8c663262007-08-25 02:26:07 +0000699 /* no precision allowed on integers */
700 if (format->precision != -1) {
701 PyErr_SetString(PyExc_ValueError,
702 "Precision not allowed in integer format specifier");
703 goto done;
704 }
705
Eric Smith8c663262007-08-25 02:26:07 +0000706 /* special case for character formatting */
707 if (format->type == 'c') {
708 /* error to specify a sign */
709 if (format->sign != '\0') {
710 PyErr_SetString(PyExc_ValueError,
711 "Sign not allowed with integer"
712 " format specifier 'c'");
713 goto done;
714 }
715
Eric Smith0923d1d2009-04-16 20:16:10 +0000716 /* Error to specify a comma. */
717 if (format->thousands_separators) {
718 PyErr_SetString(PyExc_ValueError,
719 "Thousands separators not allowed with integer"
720 " format specifier 'c'");
721 goto done;
722 }
723
Eric Smith8c663262007-08-25 02:26:07 +0000724 /* taken from unicodeobject.c formatchar() */
725 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000726/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000727 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000728 if (x == -1 && PyErr_Occurred())
729 goto done;
730#ifdef Py_UNICODE_WIDE
731 if (x < 0 || x > 0x10ffff) {
732 PyErr_SetString(PyExc_OverflowError,
733 "%c arg not in range(0x110000) "
734 "(wide Python build)");
735 goto done;
736 }
737#else
738 if (x < 0 || x > 0xffff) {
739 PyErr_SetString(PyExc_OverflowError,
740 "%c arg not in range(0x10000) "
741 "(narrow Python build)");
742 goto done;
743 }
744#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000745 numeric_char = (STRINGLIB_CHAR)x;
746 pnumeric_chars = &numeric_char;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000747 n_digits = 1;
Eric Smith0923d1d2009-04-16 20:16:10 +0000748
749 /* As a sort-of hack, we tell calc_number_widths that we only
750 have "remainder" characters. calc_number_widths thinks
751 these are characters that don't get formatted, only copied
752 into the output string. We do this for 'c' formatting,
753 because the characters are likely to be non-digits. */
754 n_remainder = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000755 }
756 else {
Eric Smith8c663262007-08-25 02:26:07 +0000757 int base;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000758 int leading_chars_to_skip = 0; /* Number of characters added by
759 PyNumber_ToBase that we want to
760 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000761
762 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000763 PyNumber_ToBase */
764 switch (format->type) {
765 case 'b':
766 base = 2;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000767 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000768 break;
769 case 'o':
770 base = 8;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000771 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000772 break;
773 case 'x':
774 case 'X':
775 base = 16;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000776 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000777 break;
778 default: /* shouldn't be needed, but stops a compiler warning */
779 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000780 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000781 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000782 break;
783 }
784
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000785 /* The number of prefix chars is the same as the leading
786 chars to skip */
787 if (format->alternate)
788 n_prefix = leading_chars_to_skip;
Eric Smithd68af8f2008-07-16 00:15:35 +0000789
Eric Smith8fd3eba2008-02-17 19:48:00 +0000790 /* Do the hard part, converting to a string in a given base */
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000791 tmp = tostring(value, base);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000792 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000793 goto done;
794
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000795 pnumeric_chars = STRINGLIB_STR(tmp);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000796 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000797
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000798 prefix = pnumeric_chars;
Eric Smithd68af8f2008-07-16 00:15:35 +0000799
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000800 /* Remember not to modify what pnumeric_chars points to. it
801 might be interned. Only modify it after we copy it into a
802 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000803
Eric Smith8fd3eba2008-02-17 19:48:00 +0000804 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000805 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000806 if (pnumeric_chars[0] == '-') {
807 sign_char = pnumeric_chars[0];
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000808 ++prefix;
809 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000810 }
811
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000812 /* Skip over the leading chars (0x, 0b, etc.) */
813 n_digits -= leading_chars_to_skip;
814 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000815 }
816
Eric Smith0923d1d2009-04-16 20:16:10 +0000817 /* Determine the grouping, separator, and decimal point, if any. */
818 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
819 (format->thousands_separators ?
820 LT_DEFAULT_LOCALE :
821 LT_NO_LOCALE),
822 &locale);
Eric Smith5807c412008-05-11 21:00:57 +0000823
Eric Smith0923d1d2009-04-16 20:16:10 +0000824 /* Calculate how much memory we'll need. */
825 n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
826 n_digits, n_remainder, 0, &locale, format);
Eric Smithb151a452008-06-24 11:21:04 +0000827
Eric Smith0923d1d2009-04-16 20:16:10 +0000828 /* Allocate the memory. */
829 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000830 if (!result)
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000831 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000832
Eric Smith0923d1d2009-04-16 20:16:10 +0000833 /* Populate the memory. */
834 fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
835 prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
836 &locale, format->type == 'X');
Eric Smithd68af8f2008-07-16 00:15:35 +0000837
Eric Smith8c663262007-08-25 02:26:07 +0000838done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000839 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000840 return result;
841}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000842#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000843
844/************************************************************************/
845/*********** float formatting *******************************************/
846/************************************************************************/
847
Eric Smith8fd3eba2008-02-17 19:48:00 +0000848#ifdef FORMAT_FLOAT
849#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000850static void
851strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
Eric Smith8c663262007-08-25 02:26:07 +0000852{
Eric Smith0923d1d2009-04-16 20:16:10 +0000853 Py_ssize_t i;
854 for (i = 0; i < len; ++i)
855 buffer[i] = (Py_UNICODE)charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000856}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000857#endif
Eric Smith8c663262007-08-25 02:26:07 +0000858
Eric Smith8c663262007-08-25 02:26:07 +0000859/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000860static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000861format_float_internal(PyObject *value,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000862 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000863{
Eric Smith0923d1d2009-04-16 20:16:10 +0000864 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
Eric Smith8c663262007-08-25 02:26:07 +0000865 Py_ssize_t n_digits;
Eric Smith0923d1d2009-04-16 20:16:10 +0000866 Py_ssize_t n_remainder;
867 Py_ssize_t n_total;
868 int has_decimal;
869 double val;
Eric Smith8c663262007-08-25 02:26:07 +0000870 Py_ssize_t precision = format->precision;
Eric Smith0923d1d2009-04-16 20:16:10 +0000871 STRINGLIB_CHAR type = format->type;
872 int add_pct = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000873 STRINGLIB_CHAR *p;
874 NumberFieldWidths spec;
Eric Smith0923d1d2009-04-16 20:16:10 +0000875 int flags = 0;
876 PyObject *result = NULL;
877 STRINGLIB_CHAR sign_char = '\0';
878 int float_type; /* Used to see if we have a nan, inf, or regular float. */
Eric Smith8c663262007-08-25 02:26:07 +0000879
880#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000881 Py_UNICODE *unicode_tmp = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000882#endif
883
Eric Smith0923d1d2009-04-16 20:16:10 +0000884 /* Locale settings, either from the actual locale or
885 from a hard-code pseudo-locale */
886 LocaleInfo locale;
887
888 /* Alternate is not allowed on floats. */
Eric Smithb1ebcc62008-07-15 13:02:41 +0000889 if (format->alternate) {
890 PyErr_SetString(PyExc_ValueError,
891 "Alternate form (#) not allowed in float format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000892 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000893 goto done;
894 }
895
Eric Smith0923d1d2009-04-16 20:16:10 +0000896 if (type == '\0') {
897 /* Omitted type specifier. This is like 'g' but with at least
898 one digit after the decimal point. */
899 type = 'g';
900 flags |= Py_DTSF_ADD_DOT_0;
901 }
902
903 if (type == 'n')
904 /* 'n' is the same as 'g', except for the locale used to
905 format the result. We take care of that later. */
906 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000907
Eric Smith22b85b32008-07-17 19:18:29 +0000908 /* 'F' is the same as 'f', per the PEP */
909 if (type == 'F')
910 type = 'f';
911
Eric Smith0923d1d2009-04-16 20:16:10 +0000912 val = PyFloat_AsDouble(value);
913 if (val == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000914 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000915
916 if (type == '%') {
917 type = 'f';
Eric Smith0923d1d2009-04-16 20:16:10 +0000918 val *= 100;
919 add_pct = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000920 }
921
922 if (precision < 0)
Eric Smith185e30c2007-08-30 22:23:08 +0000923 precision = 6;
Eric Smith0923d1d2009-04-16 20:16:10 +0000924 if ((type == 'f' || type == 'F') && fabs(val) >= 1e50)
Eric Smith22b85b32008-07-17 19:18:29 +0000925 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000926
Eric Smith0923d1d2009-04-16 20:16:10 +0000927 /* Cast "type", because if we're in unicode we need to pass a
928 8-bit char. This is safe, because we've restricted what "type"
929 can be. */
930 buf = PyOS_double_to_string(val, (char)type, precision, flags,
931 &float_type);
932 if (buf == NULL)
933 goto done;
934 n_digits = strlen(buf);
Eric Smith8c663262007-08-25 02:26:07 +0000935
Eric Smith0923d1d2009-04-16 20:16:10 +0000936 if (add_pct) {
937 /* We know that buf has a trailing zero (since we just called
938 strlen() on it), and we don't use that fact any more. So we
939 can just write over the trailing zero. */
940 buf[n_digits] = '%';
941 n_digits += 1;
942 }
Eric Smith8c663262007-08-25 02:26:07 +0000943
Eric Smith0923d1d2009-04-16 20:16:10 +0000944 /* Since there is no unicode version of PyOS_double_to_string,
945 just use the 8 bit version and then convert to unicode. */
Eric Smith8c663262007-08-25 02:26:07 +0000946#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000947 unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
948 if (unicode_tmp == NULL) {
949 PyErr_NoMemory();
950 goto done;
951 }
952 strtounicode(unicode_tmp, buf, n_digits);
953 p = unicode_tmp;
Eric Smith8c663262007-08-25 02:26:07 +0000954#else
Eric Smith0923d1d2009-04-16 20:16:10 +0000955 p = buf;
Eric Smith8c663262007-08-25 02:26:07 +0000956#endif
957
Eric Smith0923d1d2009-04-16 20:16:10 +0000958 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000959 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000960 if (*p == '-') {
961 sign_char = *p;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000962 ++p;
963 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000964 }
965
Eric Smith0923d1d2009-04-16 20:16:10 +0000966 /* Determine if we have any "remainder" (after the digits, might include
967 decimal or exponent or both (or neither)) */
968 parse_number(p, n_digits, &n_remainder, &has_decimal);
Eric Smith8c663262007-08-25 02:26:07 +0000969
Eric Smith0923d1d2009-04-16 20:16:10 +0000970 /* Determine the grouping, separator, and decimal point, if any. */
971 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
972 (format->thousands_separators ?
973 LT_DEFAULT_LOCALE :
974 LT_NO_LOCALE),
975 &locale);
976
977 /* Calculate how much memory we'll need. */
978 n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
979 n_remainder, has_decimal, &locale, format);
980
981 /* Allocate the memory. */
982 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8c663262007-08-25 02:26:07 +0000983 if (result == NULL)
984 goto done;
985
Eric Smith0923d1d2009-04-16 20:16:10 +0000986 /* Populate the memory. */
987 fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
988 format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
989 0);
Eric Smith8c663262007-08-25 02:26:07 +0000990
991done:
Eric Smith0923d1d2009-04-16 20:16:10 +0000992 PyMem_Free(buf);
993#if STRINGLIB_IS_UNICODE
994 PyMem_Free(unicode_tmp);
995#endif
Eric Smith8c663262007-08-25 02:26:07 +0000996 return result;
997}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000998#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +0000999
1000/************************************************************************/
1001/*********** built in formatters ****************************************/
1002/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001003PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001004FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001005 STRINGLIB_CHAR *format_spec,
1006 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001007{
Eric Smith8c663262007-08-25 02:26:07 +00001008 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +00001009 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001010
1011 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001012 it equivalent to str(obj) */
1013 if (format_spec_len == 0) {
1014 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001015 goto done;
1016 }
1017
1018 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001019 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001020 &format, 's'))
Eric Smith8c663262007-08-25 02:26:07 +00001021 goto done;
1022
1023 /* type conversion? */
1024 switch (format.type) {
1025 case 's':
1026 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001027 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001028 break;
Eric Smith8c663262007-08-25 02:26:07 +00001029 default:
1030 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001031 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001032 goto done;
1033 }
1034
1035done:
Eric Smith8c663262007-08-25 02:26:07 +00001036 return result;
1037}
1038
Eric Smith8fd3eba2008-02-17 19:48:00 +00001039#if defined FORMAT_LONG || defined FORMAT_INT
1040static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +00001041format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001042 STRINGLIB_CHAR *format_spec,
1043 Py_ssize_t format_spec_len,
1044 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +00001045{
Eric Smith8c663262007-08-25 02:26:07 +00001046 PyObject *result = NULL;
1047 PyObject *tmp = NULL;
1048 InternalFormatSpec format;
1049
Eric Smith8c663262007-08-25 02:26:07 +00001050 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001051 it equivalent to str(obj) */
1052 if (format_spec_len == 0) {
1053 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001054 goto done;
1055 }
1056
1057 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001058 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001059 format_spec_len,
1060 &format, 'd'))
Eric Smith8c663262007-08-25 02:26:07 +00001061 goto done;
1062
1063 /* type conversion? */
1064 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +00001065 case 'b':
1066 case 'c':
1067 case 'd':
1068 case 'o':
1069 case 'x':
1070 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +00001071 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +00001072 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001073 the formatting */
1074 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +00001075 break;
1076
Eric Smithfa767ef2008-01-28 10:59:27 +00001077 case 'e':
1078 case 'E':
1079 case 'f':
1080 case 'F':
1081 case 'g':
1082 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +00001083 case '%':
1084 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001085 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +00001086 if (tmp == NULL)
1087 goto done;
Eric Smithf64bce82009-04-13 00:50:23 +00001088 result = format_float_internal(tmp, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +00001089 break;
1090
Eric Smith8c663262007-08-25 02:26:07 +00001091 default:
1092 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001093 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001094 goto done;
1095 }
1096
1097done:
1098 Py_XDECREF(tmp);
1099 return result;
1100}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001101#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +00001102
Eric Smith8fd3eba2008-02-17 19:48:00 +00001103#ifdef FORMAT_LONG
1104/* Need to define long_format as a function that will convert a long
1105 to a string. In 3.0, _PyLong_Format has the correct signature. In
1106 2.x, we need to fudge a few parameters */
1107#if PY_VERSION_HEX >= 0x03000000
1108#define long_format _PyLong_Format
1109#else
1110static PyObject*
1111long_format(PyObject* value, int base)
1112{
1113 /* Convert to base, don't add trailing 'L', and use the new octal
1114 format. We already know this is a long object */
1115 assert(PyLong_Check(value));
1116 /* convert to base, don't add 'L', and use the new octal format */
1117 return _PyLong_Format(value, base, 0, 1);
1118}
1119#endif
1120
1121PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001122FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001123 STRINGLIB_CHAR *format_spec,
1124 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001125{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001126 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001127 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001128}
1129#endif /* FORMAT_LONG */
1130
1131#ifdef FORMAT_INT
1132/* this is only used for 2.x, not 3.0 */
1133static PyObject*
1134int_format(PyObject* value, int base)
1135{
1136 /* Convert to base, and use the new octal format. We already
1137 know this is an int object */
1138 assert(PyInt_Check(value));
1139 return _PyInt_Format((PyIntObject*)value, base, 1);
1140}
1141
1142PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001143FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001144 STRINGLIB_CHAR *format_spec,
1145 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001146{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001147 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001148 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001149}
1150#endif /* FORMAT_INT */
1151
1152#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001153PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001154FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001155 STRINGLIB_CHAR *format_spec,
1156 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001157{
Eric Smith8c663262007-08-25 02:26:07 +00001158 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001159 InternalFormatSpec format;
1160
Eric Smith8c663262007-08-25 02:26:07 +00001161 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001162 it equivalent to str(obj) */
1163 if (format_spec_len == 0) {
1164 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001165 goto done;
1166 }
1167
1168 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001169 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001170 format_spec_len,
1171 &format, '\0'))
Eric Smith8c663262007-08-25 02:26:07 +00001172 goto done;
1173
1174 /* type conversion? */
1175 switch (format.type) {
Eric Smith0923d1d2009-04-16 20:16:10 +00001176 case '\0': /* No format code: like 'g', but with at least one decimal. */
Eric Smith8c663262007-08-25 02:26:07 +00001177 case 'e':
1178 case 'E':
1179 case 'f':
1180 case 'F':
1181 case 'g':
1182 case 'G':
1183 case 'n':
1184 case '%':
1185 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001186 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001187 break;
1188
1189 default:
1190 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001191 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001192 goto done;
1193 }
1194
1195done:
Eric Smith8c663262007-08-25 02:26:07 +00001196 return result;
1197}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001198#endif /* FORMAT_FLOAT */