blob: 61ca12badf81cec4b5cc42a78091a6a5f4dcaa1a [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':
251 /* These are allowed. See PEP 378.*/
252 break;
253 default:
254 PyErr_Format(PyExc_ValueError,
255 "Cannot specify ',' with '%c'.", format->type);
256 return 0;
257 }
Eric Smitha3b1ac82009-04-03 14:45:06 +0000258 }
259
Eric Smith8c663262007-08-25 02:26:07 +0000260 return 1;
261}
262
Eric Smith8fd3eba2008-02-17 19:48:00 +0000263#if defined FORMAT_FLOAT || defined FORMAT_LONG
Eric Smith8c663262007-08-25 02:26:07 +0000264/************************************************************************/
265/*********** common routines for numeric formatting *********************/
266/************************************************************************/
267
Eric Smith0923d1d2009-04-16 20:16:10 +0000268/* Locale type codes. */
269#define LT_CURRENT_LOCALE 0
270#define LT_DEFAULT_LOCALE 1
271#define LT_NO_LOCALE 2
272
273/* Locale info needed for formatting integers and the part of floats
274 before and including the decimal. Note that locales only support
275 8-bit chars, not unicode. */
276typedef struct {
277 char *decimal_point;
278 char *thousands_sep;
279 char *grouping;
280} LocaleInfo;
281
Eric Smith8c663262007-08-25 02:26:07 +0000282/* describes the layout for an integer, see the comment in
Eric Smithd68af8f2008-07-16 00:15:35 +0000283 calc_number_widths() for details */
Eric Smith8c663262007-08-25 02:26:07 +0000284typedef struct {
285 Py_ssize_t n_lpadding;
Eric Smithd68af8f2008-07-16 00:15:35 +0000286 Py_ssize_t n_prefix;
Eric Smith8c663262007-08-25 02:26:07 +0000287 Py_ssize_t n_spadding;
288 Py_ssize_t n_rpadding;
Eric Smith0923d1d2009-04-16 20:16:10 +0000289 char sign;
290 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
291 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
292 any grouping chars. */
293 Py_ssize_t n_decimal; /* 0 if only an integer */
294 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
295 excluding the decimal itself, if
296 present. */
297
298 /* These 2 are not the widths of fields, but are needed by
299 STRINGLIB_GROUPING. */
300 Py_ssize_t n_digits; /* The number of digits before a decimal
301 or exponent. */
302 Py_ssize_t n_min_width; /* The min_width we used when we computed
303 the n_grouped_digits width. */
Eric Smith8c663262007-08-25 02:26:07 +0000304} NumberFieldWidths;
305
Eric Smith0923d1d2009-04-16 20:16:10 +0000306/* Given a number of the form:
307 digits[remainder]
308 where ptr points to the start and end points to the end, find where
309 the integer part ends. This could be a decimal, an exponent, both,
310 or neither.
311 If a decimal point is present, set *has_decimal and increment
312 remainder beyond it.
313 Results are undefined (but shouldn't crash) for improperly
314 formatted strings.
315*/
316static void
317parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
318 Py_ssize_t *n_remainder, int *has_decimal)
319{
320 STRINGLIB_CHAR *end = ptr + len;
321 STRINGLIB_CHAR *remainder;
322
323 while (ptr<end && isdigit(*ptr))
324 ++ptr;
325 remainder = ptr;
326
327 /* Does remainder start with a decimal point? */
328 *has_decimal = ptr<end && *remainder == '.';
329
330 /* Skip the decimal point. */
331 if (*has_decimal)
332 remainder++;
333
334 *n_remainder = end - remainder;
335}
336
Eric Smith8c663262007-08-25 02:26:07 +0000337/* not all fields of format are used. for example, precision is
338 unused. should this take discrete params in order to be more clear
339 about what it does? or is passing a single format parameter easier
340 and more efficient enough to justify a little obfuscation? */
Eric Smith0923d1d2009-04-16 20:16:10 +0000341static Py_ssize_t
342calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
343 STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
344 Py_ssize_t n_number, Py_ssize_t n_remainder,
345 int has_decimal, const LocaleInfo *locale,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000346 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000347{
Eric Smith0923d1d2009-04-16 20:16:10 +0000348 Py_ssize_t n_non_digit_non_padding;
349 Py_ssize_t n_padding;
350
351 spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
Eric Smith05212a12008-07-16 19:41:14 +0000352 spec->n_lpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000353 spec->n_prefix = n_prefix;
354 spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
355 spec->n_remainder = n_remainder;
Eric Smith05212a12008-07-16 19:41:14 +0000356 spec->n_spadding = 0;
357 spec->n_rpadding = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000358 spec->sign = '\0';
359 spec->n_sign = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000360
361 /* the output will look like:
Eric Smith0923d1d2009-04-16 20:16:10 +0000362 | |
363 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
364 | |
Eric Smith8c663262007-08-25 02:26:07 +0000365
Eric Smith0923d1d2009-04-16 20:16:10 +0000366 sign is computed from format->sign and the actual
Eric Smith8c663262007-08-25 02:26:07 +0000367 sign of the number
368
Eric Smithb1ebcc62008-07-15 13:02:41 +0000369 prefix is given (it's for the '0x' prefix)
370
Eric Smith8c663262007-08-25 02:26:07 +0000371 digits is already known
372
373 the total width is either given, or computed from the
374 actual digits
375
376 only one of lpadding, spadding, and rpadding can be non-zero,
377 and it's calculated from the width and other fields
378 */
379
380 /* compute the various parts we're going to write */
Eric Smith0923d1d2009-04-16 20:16:10 +0000381 switch (format->sign) {
382 case '+':
Eric Smith8c663262007-08-25 02:26:07 +0000383 /* always put a + or - */
Eric Smith0923d1d2009-04-16 20:16:10 +0000384 spec->n_sign = 1;
385 spec->sign = (sign_char == '-' ? '-' : '+');
386 break;
387 case ' ':
388 spec->n_sign = 1;
389 spec->sign = (sign_char == '-' ? '-' : ' ');
390 break;
391 default:
392 /* Not specified, or the default (-) */
393 if (sign_char == '-') {
394 spec->n_sign = 1;
395 spec->sign = '-';
Eric Smith8c663262007-08-25 02:26:07 +0000396 }
397 }
398
Eric Smith0923d1d2009-04-16 20:16:10 +0000399 /* The number of chars used for non-digits and non-padding. */
400 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
401 spec->n_remainder;
Eric Smithd68af8f2008-07-16 00:15:35 +0000402
Eric Smith0923d1d2009-04-16 20:16:10 +0000403 /* min_width can go negative, that's okay. format->width == -1 means
404 we don't care. */
405 if (format->fill_char == '0')
406 spec->n_min_width = format->width - n_non_digit_non_padding;
407 else
408 spec->n_min_width = 0;
409
410 if (spec->n_digits == 0)
411 /* This case only occurs when using 'c' formatting, we need
412 to special case it because the grouping code always wants
413 to have at least one character. */
414 spec->n_grouped_digits = 0;
415 else
416 spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
417 spec->n_digits,
418 spec->n_min_width,
419 locale->grouping,
420 locale->thousands_sep);
421
422 /* Given the desired width and the total of digit and non-digit
423 space we consume, see if we need any padding. format->width can
424 be negative (meaning no padding), but this code still works in
425 that case. */
426 n_padding = format->width -
427 (n_non_digit_non_padding + spec->n_grouped_digits);
428 if (n_padding > 0) {
429 /* Some padding is needed. Determine if it's left, space, or right. */
430 switch (format->align) {
431 case '<':
432 spec->n_rpadding = n_padding;
433 break;
434 case '^':
435 spec->n_lpadding = n_padding / 2;
436 spec->n_rpadding = n_padding - spec->n_lpadding;
437 break;
438 case '=':
439 spec->n_spadding = n_padding;
440 break;
441 default:
442 /* Handles '>', plus catch-all just in case. */
443 spec->n_lpadding = n_padding;
444 break;
Eric Smith8c663262007-08-25 02:26:07 +0000445 }
446 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000447 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
448 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
449 spec->n_remainder + spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000450}
451
Eric Smith0923d1d2009-04-16 20:16:10 +0000452/* Fill in the digit parts of a numbers's string representation,
453 as determined in calc_number_widths().
454 No error checking, since we know the buffer is the correct size. */
455static void
456fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
457 STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
458 STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
459 LocaleInfo *locale, int toupper)
Eric Smith8c663262007-08-25 02:26:07 +0000460{
Eric Smith0923d1d2009-04-16 20:16:10 +0000461 /* Used to keep track of digits, decimal, and remainder. */
462 STRINGLIB_CHAR *p = digits;
463
464#ifndef NDEBUG
465 Py_ssize_t r;
466#endif
Eric Smith8c663262007-08-25 02:26:07 +0000467
468 if (spec->n_lpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000469 STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
470 buf += spec->n_lpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000471 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000472 if (spec->n_sign == 1) {
473 *buf++ = spec->sign;
Eric Smith8c663262007-08-25 02:26:07 +0000474 }
Eric Smithd68af8f2008-07-16 00:15:35 +0000475 if (spec->n_prefix) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000476 memmove(buf,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000477 prefix,
478 spec->n_prefix * sizeof(STRINGLIB_CHAR));
Eric Smith0923d1d2009-04-16 20:16:10 +0000479 if (toupper) {
480 Py_ssize_t t;
481 for (t = 0; t < spec->n_prefix; ++t)
482 buf[t] = STRINGLIB_TOUPPER(buf[t]);
483 }
484 buf += spec->n_prefix;
Eric Smithd68af8f2008-07-16 00:15:35 +0000485 }
Eric Smith8c663262007-08-25 02:26:07 +0000486 if (spec->n_spadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000487 STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
488 buf += spec->n_spadding;
Eric Smith8c663262007-08-25 02:26:07 +0000489 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000490
491 /* Only for type 'c' special case, it has no digits. */
492 if (spec->n_digits != 0) {
493 /* Fill the digits with InsertThousandsGrouping. */
494#ifndef NDEBUG
495 r =
496#endif
497 STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
498 spec->n_digits, spec->n_min_width,
499 locale->grouping, locale->thousands_sep);
500#ifndef NDEBUG
501 assert(r == spec->n_grouped_digits);
502#endif
503 p += spec->n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000504 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000505 if (toupper) {
506 Py_ssize_t t;
507 for (t = 0; t < spec->n_grouped_digits; ++t)
508 buf[t] = STRINGLIB_TOUPPER(buf[t]);
509 }
510 buf += spec->n_grouped_digits;
511
512 if (spec->n_decimal) {
513 Py_ssize_t t;
514 for (t = 0; t < spec->n_decimal; ++t)
515 buf[t] = locale->decimal_point[t];
516 buf += spec->n_decimal;
517 p += 1;
518 }
519
520 if (spec->n_remainder) {
521 memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
522 buf += spec->n_remainder;
523 p += spec->n_remainder;
524 }
525
Eric Smith8c663262007-08-25 02:26:07 +0000526 if (spec->n_rpadding) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000527 STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
528 buf += spec->n_rpadding;
Eric Smith8c663262007-08-25 02:26:07 +0000529 }
Eric Smith8c663262007-08-25 02:26:07 +0000530}
Eric Smith0923d1d2009-04-16 20:16:10 +0000531
532static char no_grouping[1] = {CHAR_MAX};
533
534/* Find the decimal point character(s?), thousands_separator(s?), and
535 grouping description, either for the current locale if type is
536 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
537 none if LT_NO_LOCALE. */
538static void
539get_locale_info(int type, LocaleInfo *locale_info)
540{
541 switch (type) {
542 case LT_CURRENT_LOCALE: {
543 struct lconv *locale_data = localeconv();
544 locale_info->decimal_point = locale_data->decimal_point;
545 locale_info->thousands_sep = locale_data->thousands_sep;
546 locale_info->grouping = locale_data->grouping;
547 break;
548 }
549 case LT_DEFAULT_LOCALE:
550 locale_info->decimal_point = ".";
551 locale_info->thousands_sep = ",";
552 locale_info->grouping = "\3"; /* Group every 3 characters,
553 trailing 0 means repeat
554 infinitely. */
555 break;
556 case LT_NO_LOCALE:
557 locale_info->decimal_point = ".";
558 locale_info->thousands_sep = "";
559 locale_info->grouping = no_grouping;
560 break;
561 default:
562 assert(0);
563 }
564}
565
Eric Smith8fd3eba2008-02-17 19:48:00 +0000566#endif /* FORMAT_FLOAT || FORMAT_LONG */
Eric Smith8c663262007-08-25 02:26:07 +0000567
568/************************************************************************/
569/*********** string formatting ******************************************/
570/************************************************************************/
571
572static PyObject *
573format_string_internal(PyObject *value, const InternalFormatSpec *format)
574{
575 Py_ssize_t width; /* total field width */
576 Py_ssize_t lpad;
577 STRINGLIB_CHAR *dst;
578 STRINGLIB_CHAR *src = STRINGLIB_STR(value);
579 Py_ssize_t len = STRINGLIB_LEN(value);
580 PyObject *result = NULL;
581
582 /* sign is not allowed on strings */
583 if (format->sign != '\0') {
584 PyErr_SetString(PyExc_ValueError,
585 "Sign not allowed in string format specifier");
586 goto done;
587 }
588
Eric Smithb1ebcc62008-07-15 13:02:41 +0000589 /* alternate is not allowed on strings */
590 if (format->alternate) {
591 PyErr_SetString(PyExc_ValueError,
592 "Alternate form (#) not allowed in string format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000593 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000594 goto done;
595 }
596
Eric Smith8c663262007-08-25 02:26:07 +0000597 /* '=' alignment not allowed on strings */
598 if (format->align == '=') {
599 PyErr_SetString(PyExc_ValueError,
600 "'=' alignment not allowed "
601 "in string format specifier");
602 goto done;
603 }
604
605 /* if precision is specified, output no more that format.precision
606 characters */
607 if (format->precision >= 0 && len >= format->precision) {
608 len = format->precision;
609 }
610
611 if (format->width >= 0) {
612 width = format->width;
613
614 /* but use at least len characters */
615 if (len > width) {
616 width = len;
617 }
Eric Smith0cb431c2007-08-28 01:07:27 +0000618 }
619 else {
Eric Smith8c663262007-08-25 02:26:07 +0000620 /* not specified, use all of the chars and no more */
621 width = len;
622 }
623
624 /* allocate the resulting string */
625 result = STRINGLIB_NEW(NULL, width);
626 if (result == NULL)
627 goto done;
628
629 /* now write into that space */
630 dst = STRINGLIB_STR(result);
631
632 /* figure out how much leading space we need, based on the
633 aligning */
634 if (format->align == '>')
635 lpad = width - len;
636 else if (format->align == '^')
637 lpad = (width - len) / 2;
638 else
639 lpad = 0;
640
641 /* if right aligning, increment the destination allow space on the
642 left */
643 memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR));
644
645 /* do any padding */
646 if (width > len) {
647 STRINGLIB_CHAR fill_char = format->fill_char;
648 if (fill_char == '\0') {
649 /* use the default, if not specified */
650 fill_char = ' ';
651 }
652
653 /* pad on left */
654 if (lpad)
655 STRINGLIB_FILL(dst, fill_char, lpad);
656
657 /* pad on right */
658 if (width - len - lpad)
659 STRINGLIB_FILL(dst + len + lpad, fill_char, width - len - lpad);
660 }
661
662done:
663 return result;
664}
665
666
667/************************************************************************/
668/*********** long formatting ********************************************/
669/************************************************************************/
670
Eric Smith8fd3eba2008-02-17 19:48:00 +0000671#if defined FORMAT_LONG || defined FORMAT_INT
672typedef PyObject*
673(*IntOrLongToString)(PyObject *value, int base);
674
Eric Smith8c663262007-08-25 02:26:07 +0000675static PyObject *
Eric Smith8fd3eba2008-02-17 19:48:00 +0000676format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000677 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +0000678{
679 PyObject *result = NULL;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000680 PyObject *tmp = NULL;
681 STRINGLIB_CHAR *pnumeric_chars;
682 STRINGLIB_CHAR numeric_char;
Eric Smith0923d1d2009-04-16 20:16:10 +0000683 STRINGLIB_CHAR sign_char = '\0';
Eric Smith8c663262007-08-25 02:26:07 +0000684 Py_ssize_t n_digits; /* count of digits need from the computed
685 string */
Eric Smith0923d1d2009-04-16 20:16:10 +0000686 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
687 produces non-digits */
Eric Smithd68af8f2008-07-16 00:15:35 +0000688 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
Eric Smith0923d1d2009-04-16 20:16:10 +0000689 Py_ssize_t n_total;
Eric Smithd68af8f2008-07-16 00:15:35 +0000690 STRINGLIB_CHAR *prefix = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000691 NumberFieldWidths spec;
692 long x;
693
Eric Smith0923d1d2009-04-16 20:16:10 +0000694 /* Locale settings, either from the actual locale or
695 from a hard-code pseudo-locale */
696 LocaleInfo locale;
697
Eric Smith8c663262007-08-25 02:26:07 +0000698 /* no precision allowed on integers */
699 if (format->precision != -1) {
700 PyErr_SetString(PyExc_ValueError,
701 "Precision not allowed in integer format specifier");
702 goto done;
703 }
704
Eric Smith8c663262007-08-25 02:26:07 +0000705 /* special case for character formatting */
706 if (format->type == 'c') {
707 /* error to specify a sign */
708 if (format->sign != '\0') {
709 PyErr_SetString(PyExc_ValueError,
710 "Sign not allowed with integer"
711 " format specifier 'c'");
712 goto done;
713 }
714
Eric Smith0923d1d2009-04-16 20:16:10 +0000715 /* Error to specify a comma. */
716 if (format->thousands_separators) {
717 PyErr_SetString(PyExc_ValueError,
718 "Thousands separators not allowed with integer"
719 " format specifier 'c'");
720 goto done;
721 }
722
Eric Smith8c663262007-08-25 02:26:07 +0000723 /* taken from unicodeobject.c formatchar() */
724 /* Integer input truncated to a character */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000725/* XXX: won't work for int */
Christian Heimes217cfd12007-12-02 14:31:20 +0000726 x = PyLong_AsLong(value);
Eric Smith8c663262007-08-25 02:26:07 +0000727 if (x == -1 && PyErr_Occurred())
728 goto done;
729#ifdef Py_UNICODE_WIDE
730 if (x < 0 || x > 0x10ffff) {
731 PyErr_SetString(PyExc_OverflowError,
732 "%c arg not in range(0x110000) "
733 "(wide Python build)");
734 goto done;
735 }
736#else
737 if (x < 0 || x > 0xffff) {
738 PyErr_SetString(PyExc_OverflowError,
739 "%c arg not in range(0x10000) "
740 "(narrow Python build)");
741 goto done;
742 }
743#endif
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000744 numeric_char = (STRINGLIB_CHAR)x;
745 pnumeric_chars = &numeric_char;
Eric Smith8fd3eba2008-02-17 19:48:00 +0000746 n_digits = 1;
Eric Smith0923d1d2009-04-16 20:16:10 +0000747
748 /* As a sort-of hack, we tell calc_number_widths that we only
749 have "remainder" characters. calc_number_widths thinks
750 these are characters that don't get formatted, only copied
751 into the output string. We do this for 'c' formatting,
752 because the characters are likely to be non-digits. */
753 n_remainder = 1;
Eric Smith0cb431c2007-08-28 01:07:27 +0000754 }
755 else {
Eric Smith8c663262007-08-25 02:26:07 +0000756 int base;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000757 int leading_chars_to_skip = 0; /* Number of characters added by
758 PyNumber_ToBase that we want to
759 skip over. */
Eric Smith8fd3eba2008-02-17 19:48:00 +0000760
761 /* Compute the base and how many characters will be added by
Eric Smith8c663262007-08-25 02:26:07 +0000762 PyNumber_ToBase */
763 switch (format->type) {
764 case 'b':
765 base = 2;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000766 leading_chars_to_skip = 2; /* 0b */
Eric Smith8c663262007-08-25 02:26:07 +0000767 break;
768 case 'o':
769 base = 8;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000770 leading_chars_to_skip = 2; /* 0o */
Eric Smith8c663262007-08-25 02:26:07 +0000771 break;
772 case 'x':
773 case 'X':
774 base = 16;
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000775 leading_chars_to_skip = 2; /* 0x */
Eric Smith8c663262007-08-25 02:26:07 +0000776 break;
777 default: /* shouldn't be needed, but stops a compiler warning */
778 case 'd':
Eric Smith5807c412008-05-11 21:00:57 +0000779 case 'n':
Eric Smith8c663262007-08-25 02:26:07 +0000780 base = 10;
Eric Smith8c663262007-08-25 02:26:07 +0000781 break;
782 }
783
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000784 /* The number of prefix chars is the same as the leading
785 chars to skip */
786 if (format->alternate)
787 n_prefix = leading_chars_to_skip;
Eric Smithd68af8f2008-07-16 00:15:35 +0000788
Eric Smith8fd3eba2008-02-17 19:48:00 +0000789 /* Do the hard part, converting to a string in a given base */
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000790 tmp = tostring(value, base);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000791 if (tmp == NULL)
Eric Smith8c663262007-08-25 02:26:07 +0000792 goto done;
793
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000794 pnumeric_chars = STRINGLIB_STR(tmp);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000795 n_digits = STRINGLIB_LEN(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000796
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000797 prefix = pnumeric_chars;
Eric Smithd68af8f2008-07-16 00:15:35 +0000798
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000799 /* Remember not to modify what pnumeric_chars points to. it
800 might be interned. Only modify it after we copy it into a
801 newly allocated output buffer. */
Eric Smith8c663262007-08-25 02:26:07 +0000802
Eric Smith8fd3eba2008-02-17 19:48:00 +0000803 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000804 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000805 if (pnumeric_chars[0] == '-') {
806 sign_char = pnumeric_chars[0];
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000807 ++prefix;
808 ++leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000809 }
810
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000811 /* Skip over the leading chars (0x, 0b, etc.) */
812 n_digits -= leading_chars_to_skip;
813 pnumeric_chars += leading_chars_to_skip;
Eric Smith8c663262007-08-25 02:26:07 +0000814 }
815
Eric Smith0923d1d2009-04-16 20:16:10 +0000816 /* Determine the grouping, separator, and decimal point, if any. */
817 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
818 (format->thousands_separators ?
819 LT_DEFAULT_LOCALE :
820 LT_NO_LOCALE),
821 &locale);
Eric Smith5807c412008-05-11 21:00:57 +0000822
Eric Smith0923d1d2009-04-16 20:16:10 +0000823 /* Calculate how much memory we'll need. */
824 n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
825 n_digits, n_remainder, 0, &locale, format);
Eric Smithb151a452008-06-24 11:21:04 +0000826
Eric Smith0923d1d2009-04-16 20:16:10 +0000827 /* Allocate the memory. */
828 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000829 if (!result)
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000830 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000831
Eric Smith0923d1d2009-04-16 20:16:10 +0000832 /* Populate the memory. */
833 fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
834 prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
835 &locale, format->type == 'X');
Eric Smithd68af8f2008-07-16 00:15:35 +0000836
Eric Smith8c663262007-08-25 02:26:07 +0000837done:
Eric Smith8fd3eba2008-02-17 19:48:00 +0000838 Py_XDECREF(tmp);
Eric Smith8c663262007-08-25 02:26:07 +0000839 return result;
840}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000841#endif /* defined FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +0000842
843/************************************************************************/
844/*********** float formatting *******************************************/
845/************************************************************************/
846
Eric Smith8fd3eba2008-02-17 19:48:00 +0000847#ifdef FORMAT_FLOAT
848#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000849static void
850strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
Eric Smith8c663262007-08-25 02:26:07 +0000851{
Eric Smith0923d1d2009-04-16 20:16:10 +0000852 Py_ssize_t i;
853 for (i = 0; i < len; ++i)
854 buffer[i] = (Py_UNICODE)charbuffer[i];
Eric Smith8c663262007-08-25 02:26:07 +0000855}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000856#endif
Eric Smith8c663262007-08-25 02:26:07 +0000857
Eric Smith8c663262007-08-25 02:26:07 +0000858/* much of this is taken from unicodeobject.c */
Eric Smith8c663262007-08-25 02:26:07 +0000859static PyObject *
Christian Heimesc3f30c42008-02-22 16:37:40 +0000860format_float_internal(PyObject *value,
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000861 const InternalFormatSpec *format)
Eric Smith8c663262007-08-25 02:26:07 +0000862{
Eric Smith0923d1d2009-04-16 20:16:10 +0000863 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
Eric Smith8c663262007-08-25 02:26:07 +0000864 Py_ssize_t n_digits;
Eric Smith0923d1d2009-04-16 20:16:10 +0000865 Py_ssize_t n_remainder;
866 Py_ssize_t n_total;
867 int has_decimal;
868 double val;
Eric Smith8c663262007-08-25 02:26:07 +0000869 Py_ssize_t precision = format->precision;
Eric Smith0923d1d2009-04-16 20:16:10 +0000870 STRINGLIB_CHAR type = format->type;
871 int add_pct = 0;
Eric Smith8c663262007-08-25 02:26:07 +0000872 STRINGLIB_CHAR *p;
873 NumberFieldWidths spec;
Eric Smith0923d1d2009-04-16 20:16:10 +0000874 int flags = 0;
875 PyObject *result = NULL;
876 STRINGLIB_CHAR sign_char = '\0';
877 int float_type; /* Used to see if we have a nan, inf, or regular float. */
Eric Smith8c663262007-08-25 02:26:07 +0000878
879#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000880 Py_UNICODE *unicode_tmp = NULL;
Eric Smith8c663262007-08-25 02:26:07 +0000881#endif
882
Eric Smith0923d1d2009-04-16 20:16:10 +0000883 /* Locale settings, either from the actual locale or
884 from a hard-code pseudo-locale */
885 LocaleInfo locale;
886
887 /* Alternate is not allowed on floats. */
Eric Smithb1ebcc62008-07-15 13:02:41 +0000888 if (format->alternate) {
889 PyErr_SetString(PyExc_ValueError,
890 "Alternate form (#) not allowed in float format "
Eric Smithf8c8b6d2009-04-03 11:19:31 +0000891 "specifier");
Eric Smithb1ebcc62008-07-15 13:02:41 +0000892 goto done;
893 }
894
Eric Smith0923d1d2009-04-16 20:16:10 +0000895 if (type == '\0') {
896 /* Omitted type specifier. This is like 'g' but with at least
897 one digit after the decimal point. */
898 type = 'g';
899 flags |= Py_DTSF_ADD_DOT_0;
900 }
901
902 if (type == 'n')
903 /* 'n' is the same as 'g', except for the locale used to
904 format the result. We take care of that later. */
905 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000906
Eric Smith22b85b32008-07-17 19:18:29 +0000907 /* 'F' is the same as 'f', per the PEP */
908 if (type == 'F')
909 type = 'f';
910
Eric Smith0923d1d2009-04-16 20:16:10 +0000911 val = PyFloat_AsDouble(value);
912 if (val == -1.0 && PyErr_Occurred())
Eric Smith185e30c2007-08-30 22:23:08 +0000913 goto done;
Eric Smith8c663262007-08-25 02:26:07 +0000914
915 if (type == '%') {
916 type = 'f';
Eric Smith0923d1d2009-04-16 20:16:10 +0000917 val *= 100;
918 add_pct = 1;
Eric Smith8c663262007-08-25 02:26:07 +0000919 }
920
921 if (precision < 0)
Eric Smith185e30c2007-08-30 22:23:08 +0000922 precision = 6;
Eric Smith0923d1d2009-04-16 20:16:10 +0000923 if ((type == 'f' || type == 'F') && fabs(val) >= 1e50)
Eric Smith22b85b32008-07-17 19:18:29 +0000924 type = 'g';
Eric Smith8c663262007-08-25 02:26:07 +0000925
Eric Smith0923d1d2009-04-16 20:16:10 +0000926 /* Cast "type", because if we're in unicode we need to pass a
927 8-bit char. This is safe, because we've restricted what "type"
928 can be. */
929 buf = PyOS_double_to_string(val, (char)type, precision, flags,
930 &float_type);
931 if (buf == NULL)
932 goto done;
933 n_digits = strlen(buf);
Eric Smith8c663262007-08-25 02:26:07 +0000934
Eric Smith0923d1d2009-04-16 20:16:10 +0000935 if (add_pct) {
936 /* We know that buf has a trailing zero (since we just called
937 strlen() on it), and we don't use that fact any more. So we
938 can just write over the trailing zero. */
939 buf[n_digits] = '%';
940 n_digits += 1;
941 }
Eric Smith8c663262007-08-25 02:26:07 +0000942
Eric Smith0923d1d2009-04-16 20:16:10 +0000943 /* Since there is no unicode version of PyOS_double_to_string,
944 just use the 8 bit version and then convert to unicode. */
Eric Smith8c663262007-08-25 02:26:07 +0000945#if STRINGLIB_IS_UNICODE
Eric Smith0923d1d2009-04-16 20:16:10 +0000946 unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
947 if (unicode_tmp == NULL) {
948 PyErr_NoMemory();
949 goto done;
950 }
951 strtounicode(unicode_tmp, buf, n_digits);
952 p = unicode_tmp;
Eric Smith8c663262007-08-25 02:26:07 +0000953#else
Eric Smith0923d1d2009-04-16 20:16:10 +0000954 p = buf;
Eric Smith8c663262007-08-25 02:26:07 +0000955#endif
956
Eric Smith0923d1d2009-04-16 20:16:10 +0000957 /* Is a sign character present in the output? If so, remember it
Eric Smith8c663262007-08-25 02:26:07 +0000958 and skip it */
Eric Smith0923d1d2009-04-16 20:16:10 +0000959 if (*p == '-') {
960 sign_char = *p;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000961 ++p;
962 --n_digits;
Eric Smith8c663262007-08-25 02:26:07 +0000963 }
964
Eric Smith0923d1d2009-04-16 20:16:10 +0000965 /* Determine if we have any "remainder" (after the digits, might include
966 decimal or exponent or both (or neither)) */
967 parse_number(p, n_digits, &n_remainder, &has_decimal);
Eric Smith8c663262007-08-25 02:26:07 +0000968
Eric Smith0923d1d2009-04-16 20:16:10 +0000969 /* Determine the grouping, separator, and decimal point, if any. */
970 get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
971 (format->thousands_separators ?
972 LT_DEFAULT_LOCALE :
973 LT_NO_LOCALE),
974 &locale);
975
976 /* Calculate how much memory we'll need. */
977 n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
978 n_remainder, has_decimal, &locale, format);
979
980 /* Allocate the memory. */
981 result = STRINGLIB_NEW(NULL, n_total);
Eric Smith8c663262007-08-25 02:26:07 +0000982 if (result == NULL)
983 goto done;
984
Eric Smith0923d1d2009-04-16 20:16:10 +0000985 /* Populate the memory. */
986 fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
987 format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
988 0);
Eric Smith8c663262007-08-25 02:26:07 +0000989
990done:
Eric Smith0923d1d2009-04-16 20:16:10 +0000991 PyMem_Free(buf);
992#if STRINGLIB_IS_UNICODE
993 PyMem_Free(unicode_tmp);
994#endif
Eric Smith8c663262007-08-25 02:26:07 +0000995 return result;
996}
Eric Smith8fd3eba2008-02-17 19:48:00 +0000997#endif /* FORMAT_FLOAT */
Eric Smith8c663262007-08-25 02:26:07 +0000998
999/************************************************************************/
1000/*********** built in formatters ****************************************/
1001/************************************************************************/
Eric Smith8c663262007-08-25 02:26:07 +00001002PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001003FORMAT_STRING(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001004 STRINGLIB_CHAR *format_spec,
1005 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001006{
Eric Smith8c663262007-08-25 02:26:07 +00001007 InternalFormatSpec format;
Eric Smith4a7d76d2008-05-30 18:10:19 +00001008 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001009
1010 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001011 it equivalent to str(obj) */
1012 if (format_spec_len == 0) {
1013 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001014 goto done;
1015 }
1016
1017 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001018 if (!parse_internal_render_format_spec(format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001019 &format, 's'))
Eric Smith8c663262007-08-25 02:26:07 +00001020 goto done;
1021
1022 /* type conversion? */
1023 switch (format.type) {
1024 case 's':
1025 /* no type conversion needed, already a string. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001026 result = format_string_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001027 break;
Eric Smith8c663262007-08-25 02:26:07 +00001028 default:
1029 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001030 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001031 goto done;
1032 }
1033
1034done:
Eric Smith8c663262007-08-25 02:26:07 +00001035 return result;
1036}
1037
Eric Smith8fd3eba2008-02-17 19:48:00 +00001038#if defined FORMAT_LONG || defined FORMAT_INT
1039static PyObject*
Eric Smith4a7d76d2008-05-30 18:10:19 +00001040format_int_or_long(PyObject* obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001041 STRINGLIB_CHAR *format_spec,
1042 Py_ssize_t format_spec_len,
1043 IntOrLongToString tostring)
Eric Smith8c663262007-08-25 02:26:07 +00001044{
Eric Smith8c663262007-08-25 02:26:07 +00001045 PyObject *result = NULL;
1046 PyObject *tmp = NULL;
1047 InternalFormatSpec format;
1048
Eric Smith8c663262007-08-25 02:26:07 +00001049 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001050 it equivalent to str(obj) */
1051 if (format_spec_len == 0) {
1052 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001053 goto done;
1054 }
1055
1056 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001057 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001058 format_spec_len,
1059 &format, 'd'))
Eric Smith8c663262007-08-25 02:26:07 +00001060 goto done;
1061
1062 /* type conversion? */
1063 switch (format.type) {
Eric Smith8c663262007-08-25 02:26:07 +00001064 case 'b':
1065 case 'c':
1066 case 'd':
1067 case 'o':
1068 case 'x':
1069 case 'X':
Eric Smith5807c412008-05-11 21:00:57 +00001070 case 'n':
Eric Smith8fd3eba2008-02-17 19:48:00 +00001071 /* no type conversion needed, already an int (or long). do
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001072 the formatting */
1073 result = format_int_or_long_internal(obj, &format, tostring);
Eric Smith8c663262007-08-25 02:26:07 +00001074 break;
1075
Eric Smithfa767ef2008-01-28 10:59:27 +00001076 case 'e':
1077 case 'E':
1078 case 'f':
1079 case 'F':
1080 case 'g':
1081 case 'G':
Eric Smithfa767ef2008-01-28 10:59:27 +00001082 case '%':
1083 /* convert to float */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001084 tmp = PyNumber_Float(obj);
Eric Smithfa767ef2008-01-28 10:59:27 +00001085 if (tmp == NULL)
1086 goto done;
Eric Smithf64bce82009-04-13 00:50:23 +00001087 result = format_float_internal(tmp, &format);
Eric Smithfa767ef2008-01-28 10:59:27 +00001088 break;
1089
Eric Smith8c663262007-08-25 02:26:07 +00001090 default:
1091 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001092 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001093 goto done;
1094 }
1095
1096done:
1097 Py_XDECREF(tmp);
1098 return result;
1099}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001100#endif /* FORMAT_LONG || defined FORMAT_INT */
Eric Smith8c663262007-08-25 02:26:07 +00001101
Eric Smith8fd3eba2008-02-17 19:48:00 +00001102#ifdef FORMAT_LONG
1103/* Need to define long_format as a function that will convert a long
1104 to a string. In 3.0, _PyLong_Format has the correct signature. In
1105 2.x, we need to fudge a few parameters */
1106#if PY_VERSION_HEX >= 0x03000000
1107#define long_format _PyLong_Format
1108#else
1109static PyObject*
1110long_format(PyObject* value, int base)
1111{
1112 /* Convert to base, don't add trailing 'L', and use the new octal
1113 format. We already know this is a long object */
1114 assert(PyLong_Check(value));
1115 /* convert to base, don't add 'L', and use the new octal format */
1116 return _PyLong_Format(value, base, 0, 1);
1117}
1118#endif
1119
1120PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001121FORMAT_LONG(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001122 STRINGLIB_CHAR *format_spec,
1123 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001124{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001125 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001126 long_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001127}
1128#endif /* FORMAT_LONG */
1129
1130#ifdef FORMAT_INT
1131/* this is only used for 2.x, not 3.0 */
1132static PyObject*
1133int_format(PyObject* value, int base)
1134{
1135 /* Convert to base, and use the new octal format. We already
1136 know this is an int object */
1137 assert(PyInt_Check(value));
1138 return _PyInt_Format((PyIntObject*)value, base, 1);
1139}
1140
1141PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001142FORMAT_INT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001143 STRINGLIB_CHAR *format_spec,
1144 Py_ssize_t format_spec_len)
Eric Smith8fd3eba2008-02-17 19:48:00 +00001145{
Eric Smith4a7d76d2008-05-30 18:10:19 +00001146 return format_int_or_long(obj, format_spec, format_spec_len,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001147 int_format);
Eric Smith8fd3eba2008-02-17 19:48:00 +00001148}
1149#endif /* FORMAT_INT */
1150
1151#ifdef FORMAT_FLOAT
Eric Smith8c663262007-08-25 02:26:07 +00001152PyObject *
Eric Smith4a7d76d2008-05-30 18:10:19 +00001153FORMAT_FLOAT(PyObject *obj,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001154 STRINGLIB_CHAR *format_spec,
1155 Py_ssize_t format_spec_len)
Eric Smith8c663262007-08-25 02:26:07 +00001156{
Eric Smith8c663262007-08-25 02:26:07 +00001157 PyObject *result = NULL;
Eric Smith8c663262007-08-25 02:26:07 +00001158 InternalFormatSpec format;
1159
Eric Smith8c663262007-08-25 02:26:07 +00001160 /* check for the special case of zero length format spec, make
Eric Smith4a7d76d2008-05-30 18:10:19 +00001161 it equivalent to str(obj) */
1162 if (format_spec_len == 0) {
1163 result = STRINGLIB_TOSTR(obj);
Eric Smith8c663262007-08-25 02:26:07 +00001164 goto done;
1165 }
1166
1167 /* parse the format_spec */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001168 if (!parse_internal_render_format_spec(format_spec,
Eric Smithf8c8b6d2009-04-03 11:19:31 +00001169 format_spec_len,
1170 &format, '\0'))
Eric Smith8c663262007-08-25 02:26:07 +00001171 goto done;
1172
1173 /* type conversion? */
1174 switch (format.type) {
Eric Smith0923d1d2009-04-16 20:16:10 +00001175 case '\0': /* No format code: like 'g', but with at least one decimal. */
Eric Smith8c663262007-08-25 02:26:07 +00001176 case 'e':
1177 case 'E':
1178 case 'f':
1179 case 'F':
1180 case 'g':
1181 case 'G':
1182 case 'n':
1183 case '%':
1184 /* no conversion, already a float. do the formatting */
Eric Smith4a7d76d2008-05-30 18:10:19 +00001185 result = format_float_internal(obj, &format);
Eric Smith8c663262007-08-25 02:26:07 +00001186 break;
1187
1188 default:
1189 /* unknown */
Eric Smith5e5c0db2009-02-20 14:25:03 +00001190 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Eric Smith8c663262007-08-25 02:26:07 +00001191 goto done;
1192 }
1193
1194done:
Eric Smith8c663262007-08-25 02:26:07 +00001195 return result;
1196}
Eric Smith8fd3eba2008-02-17 19:48:00 +00001197#endif /* FORMAT_FLOAT */