blob: c4a2d9dbfec982e996bc2811adddbac7b3f2fd4e [file] [log] [blame]
Eric Smith8c663262007-08-25 02:26:07 +00001/* implements the unicode (as opposed to string) version of the
2 built-in formatters for string, int, float. that is, the versions
3 of int.__float__, etc., that take and return unicode objects */
4
5#include "Python.h"
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006#include <locale.h>
7
8/* Raises an exception about an unknown presentation type for this
9 * type. */
10
11static void
12unknown_presentation_type(Py_UCS4 presentation_type,
13 const char* type_name)
14{
15 /* %c might be out-of-range, hence the two cases. */
16 if (presentation_type > 32 && presentation_type < 128)
17 PyErr_Format(PyExc_ValueError,
18 "Unknown format code '%c' "
19 "for object of type '%.200s'",
20 (char)presentation_type,
21 type_name);
22 else
23 PyErr_Format(PyExc_ValueError,
24 "Unknown format code '\\x%x' "
25 "for object of type '%.200s'",
26 (unsigned int)presentation_type,
27 type_name);
28}
29
30static void
31invalid_comma_type(Py_UCS4 presentation_type)
32{
33 if (presentation_type > 32 && presentation_type < 128)
34 PyErr_Format(PyExc_ValueError,
Eric V. Smith89e1b1a2016-09-09 23:06:47 -040035 "Cannot specify ',' or '_' with '%c'.",
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020036 (char)presentation_type);
37 else
38 PyErr_Format(PyExc_ValueError,
Eric V. Smith89e1b1a2016-09-09 23:06:47 -040039 "Cannot specify ',' or '_' with '\\x%x'.",
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020040 (unsigned int)presentation_type);
41}
42
Eric V. Smith89e1b1a2016-09-09 23:06:47 -040043static void
Benjamin Petersoneb0dfa92016-09-09 20:14:05 -070044invalid_comma_and_underscore(void)
Eric V. Smith89e1b1a2016-09-09 23:06:47 -040045{
46 PyErr_Format(PyExc_ValueError, "Cannot specify both ',' and '_'.");
47}
48
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020049/*
50 get_integer consumes 0 or more decimal digit characters from an
51 input string, updates *result with the corresponding positive
52 integer, and returns the number of digits consumed.
53
54 returns -1 on error.
55*/
56static int
Serhiy Storchaka1f932612016-08-29 15:57:26 +030057get_integer(PyObject *str, Py_ssize_t *ppos, Py_ssize_t end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020058 Py_ssize_t *result)
59{
Serhiy Storchaka1f932612016-08-29 15:57:26 +030060 Py_ssize_t accumulator, digitval, pos = *ppos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020061 int numdigits;
Serhiy Storchaka1f932612016-08-29 15:57:26 +030062 int kind = PyUnicode_KIND(str);
63 void *data = PyUnicode_DATA(str);
64
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020065 accumulator = numdigits = 0;
Serhiy Storchaka1f932612016-08-29 15:57:26 +030066 for (; pos < end; pos++, numdigits++) {
67 digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ(kind, data, pos));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020068 if (digitval < 0)
69 break;
70 /*
Mark Dickinson47862d42011-12-01 15:27:04 +000071 Detect possible overflow before it happens:
72
73 accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
74 accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020075 */
Mark Dickinson47862d42011-12-01 15:27:04 +000076 if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020077 PyErr_Format(PyExc_ValueError,
78 "Too many decimal digits in format string");
Serhiy Storchaka1f932612016-08-29 15:57:26 +030079 *ppos = pos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020080 return -1;
81 }
Mark Dickinson47862d42011-12-01 15:27:04 +000082 accumulator = accumulator * 10 + digitval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020083 }
Serhiy Storchaka1f932612016-08-29 15:57:26 +030084 *ppos = pos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020085 *result = accumulator;
86 return numdigits;
87}
88
89/************************************************************************/
90/*********** standard format specifier parsing **************************/
91/************************************************************************/
92
93/* returns true if this character is a specifier alignment token */
94Py_LOCAL_INLINE(int)
95is_alignment_token(Py_UCS4 c)
96{
97 switch (c) {
98 case '<': case '>': case '=': case '^':
99 return 1;
100 default:
101 return 0;
102 }
103}
104
105/* returns true if this character is a sign element */
106Py_LOCAL_INLINE(int)
107is_sign_element(Py_UCS4 c)
108{
109 switch (c) {
110 case ' ': case '+': case '-':
111 return 1;
112 default:
113 return 0;
114 }
115}
Eric Smith8c663262007-08-25 02:26:07 +0000116
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400117/* Locale type codes. LT_NO_LOCALE must be zero. */
118#define LT_NO_LOCALE 0
119#define LT_DEFAULT_LOCALE 1
120#define LT_UNDERSCORE_LOCALE 2
121#define LT_UNDER_FOUR_LOCALE 3
122#define LT_CURRENT_LOCALE 4
Eric Smith4a7d76d2008-05-30 18:10:19 +0000123
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200124typedef struct {
125 Py_UCS4 fill_char;
126 Py_UCS4 align;
127 int alternate;
128 Py_UCS4 sign;
129 Py_ssize_t width;
130 int thousands_separators;
131 Py_ssize_t precision;
132 Py_UCS4 type;
133} InternalFormatSpec;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000134
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200135#if 0
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700136/* Occasionally useful for debugging. Should normally be commented out. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200137static void
138DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
139{
140 printf("internal format spec: fill_char %d\n", format->fill_char);
141 printf("internal format spec: align %d\n", format->align);
142 printf("internal format spec: alternate %d\n", format->alternate);
143 printf("internal format spec: sign %d\n", format->sign);
144 printf("internal format spec: width %zd\n", format->width);
145 printf("internal format spec: thousands_separators %d\n",
146 format->thousands_separators);
147 printf("internal format spec: precision %zd\n", format->precision);
148 printf("internal format spec: type %c\n", format->type);
149 printf("\n");
150}
151#endif
152
153
154/*
155 ptr points to the start of the format_spec, end points just past its end.
156 fills in format with the parsed information.
157 returns 1 on success, 0 on failure.
158 if failure, sets the exception
159*/
160static int
161parse_internal_render_format_spec(PyObject *format_spec,
162 Py_ssize_t start, Py_ssize_t end,
163 InternalFormatSpec *format,
164 char default_type,
165 char default_align)
166{
167 Py_ssize_t pos = start;
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300168 int kind = PyUnicode_KIND(format_spec);
169 void *data = PyUnicode_DATA(format_spec);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200170 /* end-pos is used throughout this code to specify the length of
171 the input string */
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300172#define READ_spec(index) PyUnicode_READ(kind, data, index)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200173
174 Py_ssize_t consumed;
175 int align_specified = 0;
Eric V. Smith2ea97122014-04-14 11:55:10 -0400176 int fill_char_specified = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200177
Eric V. Smith2ea97122014-04-14 11:55:10 -0400178 format->fill_char = ' ';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200179 format->align = default_align;
180 format->alternate = 0;
181 format->sign = '\0';
182 format->width = -1;
183 format->thousands_separators = 0;
184 format->precision = -1;
185 format->type = default_type;
186
187 /* If the second char is an alignment token,
188 then parse the fill char */
189 if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) {
190 format->align = READ_spec(pos+1);
191 format->fill_char = READ_spec(pos);
Eric V. Smith2ea97122014-04-14 11:55:10 -0400192 fill_char_specified = 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200193 align_specified = 1;
194 pos += 2;
195 }
196 else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) {
197 format->align = READ_spec(pos);
198 align_specified = 1;
199 ++pos;
200 }
201
202 /* Parse the various sign options */
203 if (end-pos >= 1 && is_sign_element(READ_spec(pos))) {
204 format->sign = READ_spec(pos);
205 ++pos;
206 }
207
208 /* If the next character is #, we're in alternate mode. This only
209 applies to integers. */
210 if (end-pos >= 1 && READ_spec(pos) == '#') {
211 format->alternate = 1;
212 ++pos;
213 }
214
215 /* The special case for 0-padding (backwards compat) */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400216 if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200217 format->fill_char = '0';
218 if (!align_specified) {
219 format->align = '=';
220 }
221 ++pos;
222 }
223
224 consumed = get_integer(format_spec, &pos, end, &format->width);
225 if (consumed == -1)
226 /* Overflow error. Exception already set. */
227 return 0;
228
229 /* If consumed is 0, we didn't consume any characters for the
230 width. In that case, reset the width to -1, because
231 get_integer() will have set it to zero. -1 is how we record
232 that the width wasn't specified. */
233 if (consumed == 0)
234 format->width = -1;
235
236 /* Comma signifies add thousands separators */
237 if (end-pos && READ_spec(pos) == ',') {
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400238 format->thousands_separators = LT_DEFAULT_LOCALE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200239 ++pos;
240 }
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400241 /* Underscore signifies add thousands separators */
242 if (end-pos && READ_spec(pos) == '_') {
243 if (format->thousands_separators != 0) {
244 invalid_comma_and_underscore();
245 return 0;
246 }
247 format->thousands_separators = LT_UNDERSCORE_LOCALE;
248 ++pos;
249 }
250 if (end-pos && READ_spec(pos) == ',') {
251 invalid_comma_and_underscore();
252 return 0;
253 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200254
255 /* Parse field precision */
256 if (end-pos && READ_spec(pos) == '.') {
257 ++pos;
258
259 consumed = get_integer(format_spec, &pos, end, &format->precision);
260 if (consumed == -1)
261 /* Overflow error. Exception already set. */
262 return 0;
263
264 /* Not having a precision after a dot is an error. */
265 if (consumed == 0) {
266 PyErr_Format(PyExc_ValueError,
267 "Format specifier missing precision");
268 return 0;
269 }
270
271 }
272
273 /* Finally, parse the type field. */
274
275 if (end-pos > 1) {
Eric V. Smithd25cfe62012-01-19 20:04:28 -0500276 /* More than one char remain, invalid format specifier. */
277 PyErr_Format(PyExc_ValueError, "Invalid format specifier");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200278 return 0;
279 }
280
281 if (end-pos == 1) {
282 format->type = READ_spec(pos);
283 ++pos;
284 }
285
286 /* Do as much validating as we can, just by looking at the format
287 specifier. Do not take into account what type of formatting
288 we're doing (int, float, string). */
289
290 if (format->thousands_separators) {
291 switch (format->type) {
292 case 'd':
293 case 'e':
294 case 'f':
295 case 'g':
296 case 'E':
297 case 'G':
298 case '%':
299 case 'F':
300 case '\0':
301 /* These are allowed. See PEP 378.*/
302 break;
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400303 case 'b':
304 case 'o':
305 case 'x':
306 case 'X':
307 /* Underscores are allowed in bin/oct/hex. See PEP 515. */
308 if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
309 /* Every four digits, not every three, in bin/oct/hex. */
310 format->thousands_separators = LT_UNDER_FOUR_LOCALE;
311 break;
312 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200313 default:
314 invalid_comma_type(format->type);
315 return 0;
316 }
317 }
318
Victor Stinnera4ac6002012-01-21 15:50:49 +0100319 assert (format->align <= 127);
320 assert (format->sign <= 127);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200321 return 1;
322}
323
324/* Calculate the padding needed. */
325static void
326calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align,
327 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
328 Py_ssize_t *n_total)
329{
330 if (width >= 0) {
331 if (nchars > width)
332 *n_total = nchars;
333 else
334 *n_total = width;
335 }
336 else {
337 /* not specified, use all of the chars and no more */
338 *n_total = nchars;
339 }
340
341 /* Figure out how much leading space we need, based on the
342 aligning */
343 if (align == '>')
344 *n_lpadding = *n_total - nchars;
345 else if (align == '^')
346 *n_lpadding = (*n_total - nchars) / 2;
347 else if (align == '<' || align == '=')
348 *n_lpadding = 0;
349 else {
350 /* We should never have an unspecified alignment. */
351 *n_lpadding = 0;
352 assert(0);
353 }
354
355 *n_rpadding = *n_total - nchars - *n_lpadding;
356}
357
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200358/* Do the padding, and return a pointer to where the caller-supplied
359 content goes. */
Victor Stinner9ce59bb2013-05-17 00:04:56 +0200360static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200361fill_padding(_PyUnicodeWriter *writer,
362 Py_ssize_t nchars,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200363 Py_UCS4 fill_char, Py_ssize_t n_lpadding,
364 Py_ssize_t n_rpadding)
365{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200366 Py_ssize_t pos;
367
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200368 /* Pad on left. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200369 if (n_lpadding) {
370 pos = writer->pos;
371 _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char);
372 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200373
374 /* Pad on right. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200375 if (n_rpadding) {
376 pos = writer->pos + nchars + n_lpadding;
377 _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char);
378 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200379
380 /* Pointer to the user content. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200381 writer->pos += n_lpadding;
382 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200383}
384
385/************************************************************************/
386/*********** common routines for numeric formatting *********************/
387/************************************************************************/
388
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200389/* Locale info needed for formatting integers and the part of floats
390 before and including the decimal. Note that locales only support
391 8-bit chars, not unicode. */
392typedef struct {
Victor Stinner41a863c2012-02-24 00:37:51 +0100393 PyObject *decimal_point;
394 PyObject *thousands_sep;
395 const char *grouping;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200396} LocaleInfo;
397
Victor Stinner41a863c2012-02-24 00:37:51 +0100398#define STATIC_LOCALE_INFO_INIT {0, 0, 0}
399
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200400/* describes the layout for an integer, see the comment in
401 calc_number_widths() for details */
402typedef struct {
403 Py_ssize_t n_lpadding;
404 Py_ssize_t n_prefix;
405 Py_ssize_t n_spadding;
406 Py_ssize_t n_rpadding;
407 char sign;
408 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
409 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
410 any grouping chars. */
411 Py_ssize_t n_decimal; /* 0 if only an integer */
412 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
413 excluding the decimal itself, if
414 present. */
415
416 /* These 2 are not the widths of fields, but are needed by
417 STRINGLIB_GROUPING. */
418 Py_ssize_t n_digits; /* The number of digits before a decimal
419 or exponent. */
420 Py_ssize_t n_min_width; /* The min_width we used when we computed
421 the n_grouped_digits width. */
422} NumberFieldWidths;
423
424
425/* Given a number of the form:
426 digits[remainder]
427 where ptr points to the start and end points to the end, find where
428 the integer part ends. This could be a decimal, an exponent, both,
429 or neither.
430 If a decimal point is present, set *has_decimal and increment
431 remainder beyond it.
432 Results are undefined (but shouldn't crash) for improperly
433 formatted strings.
434*/
435static void
436parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end,
437 Py_ssize_t *n_remainder, int *has_decimal)
438{
439 Py_ssize_t remainder;
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300440 int kind = PyUnicode_KIND(s);
441 void *data = PyUnicode_DATA(s);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200442
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300443 while (pos<end && Py_ISDIGIT(PyUnicode_READ(kind, data, pos)))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200444 ++pos;
445 remainder = pos;
446
447 /* Does remainder start with a decimal point? */
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300448 *has_decimal = pos<end && PyUnicode_READ(kind, data, remainder) == '.';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200449
450 /* Skip the decimal point. */
451 if (*has_decimal)
452 remainder++;
453
454 *n_remainder = end - remainder;
455}
456
457/* not all fields of format are used. for example, precision is
458 unused. should this take discrete params in order to be more clear
459 about what it does? or is passing a single format parameter easier
460 and more efficient enough to justify a little obfuscation? */
461static Py_ssize_t
462calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
463 Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start,
464 Py_ssize_t n_end, Py_ssize_t n_remainder,
465 int has_decimal, const LocaleInfo *locale,
Victor Stinner41a863c2012-02-24 00:37:51 +0100466 const InternalFormatSpec *format, Py_UCS4 *maxchar)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200467{
468 Py_ssize_t n_non_digit_non_padding;
469 Py_ssize_t n_padding;
470
471 spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0);
472 spec->n_lpadding = 0;
473 spec->n_prefix = n_prefix;
Victor Stinner41a863c2012-02-24 00:37:51 +0100474 spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200475 spec->n_remainder = n_remainder;
476 spec->n_spadding = 0;
477 spec->n_rpadding = 0;
478 spec->sign = '\0';
479 spec->n_sign = 0;
480
481 /* the output will look like:
482 | |
483 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
484 | |
485
486 sign is computed from format->sign and the actual
487 sign of the number
488
489 prefix is given (it's for the '0x' prefix)
490
491 digits is already known
492
493 the total width is either given, or computed from the
494 actual digits
495
496 only one of lpadding, spadding, and rpadding can be non-zero,
497 and it's calculated from the width and other fields
498 */
499
500 /* compute the various parts we're going to write */
501 switch (format->sign) {
502 case '+':
503 /* always put a + or - */
504 spec->n_sign = 1;
505 spec->sign = (sign_char == '-' ? '-' : '+');
506 break;
507 case ' ':
508 spec->n_sign = 1;
509 spec->sign = (sign_char == '-' ? '-' : ' ');
510 break;
511 default:
512 /* Not specified, or the default (-) */
513 if (sign_char == '-') {
514 spec->n_sign = 1;
515 spec->sign = '-';
516 }
517 }
518
519 /* The number of chars used for non-digits and non-padding. */
520 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
521 spec->n_remainder;
522
523 /* min_width can go negative, that's okay. format->width == -1 means
524 we don't care. */
525 if (format->fill_char == '0' && format->align == '=')
526 spec->n_min_width = format->width - n_non_digit_non_padding;
527 else
528 spec->n_min_width = 0;
529
530 if (spec->n_digits == 0)
531 /* This case only occurs when using 'c' formatting, we need
532 to special case it because the grouping code always wants
533 to have at least one character. */
534 spec->n_grouped_digits = 0;
Victor Stinner41a863c2012-02-24 00:37:51 +0100535 else {
536 Py_UCS4 grouping_maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200537 spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
Victor Stinner41a863c2012-02-24 00:37:51 +0100538 NULL, 0,
539 0, NULL,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200540 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100541 locale->grouping, locale->thousands_sep, &grouping_maxchar);
542 *maxchar = Py_MAX(*maxchar, grouping_maxchar);
543 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200544
545 /* Given the desired width and the total of digit and non-digit
546 space we consume, see if we need any padding. format->width can
547 be negative (meaning no padding), but this code still works in
548 that case. */
549 n_padding = format->width -
550 (n_non_digit_non_padding + spec->n_grouped_digits);
551 if (n_padding > 0) {
552 /* Some padding is needed. Determine if it's left, space, or right. */
553 switch (format->align) {
554 case '<':
555 spec->n_rpadding = n_padding;
556 break;
557 case '^':
558 spec->n_lpadding = n_padding / 2;
559 spec->n_rpadding = n_padding - spec->n_lpadding;
560 break;
561 case '=':
562 spec->n_spadding = n_padding;
563 break;
564 case '>':
565 spec->n_lpadding = n_padding;
566 break;
567 default:
568 /* Shouldn't get here, but treat it as '>' */
569 spec->n_lpadding = n_padding;
570 assert(0);
571 break;
572 }
573 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100574
575 if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding)
576 *maxchar = Py_MAX(*maxchar, format->fill_char);
577
Victor Stinner90f50d42012-02-24 01:44:47 +0100578 if (spec->n_decimal)
579 *maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point));
580
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200581 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
582 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
583 spec->n_remainder + spec->n_rpadding;
584}
585
586/* Fill in the digit parts of a numbers's string representation,
587 as determined in calc_number_widths().
Victor Stinnerafbaa202011-09-28 21:50:16 +0200588 Return -1 on error, or 0 on success. */
589static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200590fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200591 PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end,
Victor Stinnerafbaa202011-09-28 21:50:16 +0200592 PyObject *prefix, Py_ssize_t p_start,
593 Py_UCS4 fill_char,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200594 LocaleInfo *locale, int toupper)
595{
596 /* Used to keep track of digits, decimal, and remainder. */
597 Py_ssize_t d_pos = d_start;
Victor Stinner22c103b2013-05-07 23:50:03 +0200598 const unsigned int kind = writer->kind;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200599 const void *data = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200600 Py_ssize_t r;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200601
602 if (spec->n_lpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200603 _PyUnicode_FastFill(writer->buffer,
604 writer->pos, spec->n_lpadding, fill_char);
605 writer->pos += spec->n_lpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200606 }
607 if (spec->n_sign == 1) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200608 PyUnicode_WRITE(kind, data, writer->pos, spec->sign);
609 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200610 }
611 if (spec->n_prefix) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200612 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
613 prefix, p_start,
614 spec->n_prefix);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200615 if (toupper) {
616 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500617 for (t = 0; t < spec->n_prefix; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200618 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100619 c = Py_TOUPPER(c);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100620 assert (c <= 127);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200621 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500622 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200623 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200624 writer->pos += spec->n_prefix;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200625 }
626 if (spec->n_spadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200627 _PyUnicode_FastFill(writer->buffer,
628 writer->pos, spec->n_spadding, fill_char);
629 writer->pos += spec->n_spadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200630 }
631
632 /* Only for type 'c' special case, it has no digits. */
633 if (spec->n_digits != 0) {
634 /* Fill the digits with InsertThousandsGrouping. */
Victor Stinnerdba2dee2011-09-28 21:50:42 +0200635 char *pdigits;
636 if (PyUnicode_READY(digits))
637 return -1;
638 pdigits = PyUnicode_DATA(digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200639 if (PyUnicode_KIND(digits) < kind) {
640 pdigits = _PyUnicode_AsKind(digits, kind);
Victor Stinnerafbaa202011-09-28 21:50:16 +0200641 if (pdigits == NULL)
642 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 }
Victor Stinner90f50d42012-02-24 01:44:47 +0100644 r = _PyUnicode_InsertThousandsGrouping(
Victor Stinnerd3f08822012-05-29 12:57:52 +0200645 writer->buffer, writer->pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200646 spec->n_grouped_digits,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200647 pdigits + kind * d_pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200648 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100649 locale->grouping, locale->thousands_sep, NULL);
Victor Stinner90f50d42012-02-24 01:44:47 +0100650 if (r == -1)
651 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200652 assert(r == spec->n_grouped_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200653 if (PyUnicode_KIND(digits) < kind)
654 PyMem_Free(pdigits);
655 d_pos += spec->n_digits;
656 }
657 if (toupper) {
658 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500659 for (t = 0; t < spec->n_grouped_digits; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200660 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100661 c = Py_TOUPPER(c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500662 if (c > 127) {
663 PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
664 return -1;
665 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200666 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500667 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200668 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200669 writer->pos += spec->n_grouped_digits;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200670
671 if (spec->n_decimal) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200672 _PyUnicode_FastCopyCharacters(
673 writer->buffer, writer->pos,
674 locale->decimal_point, 0, spec->n_decimal);
675 writer->pos += spec->n_decimal;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200676 d_pos += 1;
677 }
678
679 if (spec->n_remainder) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200680 _PyUnicode_FastCopyCharacters(
681 writer->buffer, writer->pos,
682 digits, d_pos, spec->n_remainder);
683 writer->pos += spec->n_remainder;
Brett Cannon8a250fa2012-06-25 16:13:44 -0400684 /* d_pos += spec->n_remainder; */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200685 }
686
687 if (spec->n_rpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200688 _PyUnicode_FastFill(writer->buffer,
689 writer->pos, spec->n_rpadding,
690 fill_char);
691 writer->pos += spec->n_rpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200692 }
Victor Stinnerafbaa202011-09-28 21:50:16 +0200693 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200694}
695
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200696static const char no_grouping[1] = {CHAR_MAX};
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200697
698/* Find the decimal point character(s?), thousands_separator(s?), and
699 grouping description, either for the current locale if type is
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400700 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE or
701 LT_UNDERSCORE_LOCALE/LT_UNDER_FOUR_LOCALE, or none if LT_NO_LOCALE. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100702static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200703get_locale_info(int type, LocaleInfo *locale_info)
704{
705 switch (type) {
706 case LT_CURRENT_LOCALE: {
707 struct lconv *locale_data = localeconv();
Victor Stinner41a863c2012-02-24 00:37:51 +0100708 locale_info->decimal_point = PyUnicode_DecodeLocale(
709 locale_data->decimal_point,
710 NULL);
711 if (locale_info->decimal_point == NULL)
712 return -1;
713 locale_info->thousands_sep = PyUnicode_DecodeLocale(
714 locale_data->thousands_sep,
715 NULL);
716 if (locale_info->thousands_sep == NULL) {
717 Py_DECREF(locale_info->decimal_point);
718 return -1;
719 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200720 locale_info->grouping = locale_data->grouping;
721 break;
722 }
723 case LT_DEFAULT_LOCALE:
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400724 case LT_UNDERSCORE_LOCALE:
725 case LT_UNDER_FOUR_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100726 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400727 locale_info->thousands_sep = PyUnicode_FromOrdinal(
728 type == LT_DEFAULT_LOCALE ? ',' : '_');
Victor Stinner41a863c2012-02-24 00:37:51 +0100729 if (!locale_info->decimal_point || !locale_info->thousands_sep) {
730 Py_XDECREF(locale_info->decimal_point);
731 Py_XDECREF(locale_info->thousands_sep);
732 return -1;
733 }
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400734 if (type != LT_UNDER_FOUR_LOCALE)
735 locale_info->grouping = "\3"; /* Group every 3 characters. The
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200736 (implicit) trailing 0 means repeat
737 infinitely. */
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400738 else
739 locale_info->grouping = "\4"; /* Bin/oct/hex group every four. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200740 break;
741 case LT_NO_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100742 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
743 locale_info->thousands_sep = PyUnicode_New(0, 0);
744 if (!locale_info->decimal_point || !locale_info->thousands_sep) {
745 Py_XDECREF(locale_info->decimal_point);
746 Py_XDECREF(locale_info->thousands_sep);
747 return -1;
748 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200749 locale_info->grouping = no_grouping;
750 break;
751 default:
752 assert(0);
753 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100754 return 0;
755}
756
757static void
758free_locale_info(LocaleInfo *locale_info)
759{
760 Py_XDECREF(locale_info->decimal_point);
761 Py_XDECREF(locale_info->thousands_sep);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200762}
763
764/************************************************************************/
765/*********** string formatting ******************************************/
766/************************************************************************/
767
Victor Stinnerd3f08822012-05-29 12:57:52 +0200768static int
769format_string_internal(PyObject *value, const InternalFormatSpec *format,
770 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200771{
772 Py_ssize_t lpad;
773 Py_ssize_t rpad;
774 Py_ssize_t total;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200775 Py_ssize_t len;
776 int result = -1;
Victor Stinnerece58de2012-04-23 23:36:38 +0200777 Py_UCS4 maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200778
Victor Stinnerd3f08822012-05-29 12:57:52 +0200779 assert(PyUnicode_IS_READY(value));
780 len = PyUnicode_GET_LENGTH(value);
781
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200782 /* sign is not allowed on strings */
783 if (format->sign != '\0') {
784 PyErr_SetString(PyExc_ValueError,
785 "Sign not allowed in string format specifier");
786 goto done;
787 }
788
789 /* alternate is not allowed on strings */
790 if (format->alternate) {
791 PyErr_SetString(PyExc_ValueError,
792 "Alternate form (#) not allowed in string format "
793 "specifier");
794 goto done;
795 }
796
797 /* '=' alignment not allowed on strings */
798 if (format->align == '=') {
799 PyErr_SetString(PyExc_ValueError,
800 "'=' alignment not allowed "
801 "in string format specifier");
802 goto done;
803 }
804
Victor Stinner621ef3d2012-10-02 00:33:47 +0200805 if ((format->width == -1 || format->width <= len)
806 && (format->precision == -1 || format->precision >= len)) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200807 /* Fast path */
808 return _PyUnicodeWriter_WriteStr(writer, value);
809 }
810
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200811 /* if precision is specified, output no more that format.precision
812 characters */
813 if (format->precision >= 0 && len >= format->precision) {
814 len = format->precision;
815 }
816
817 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
818
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200819 maxchar = writer->maxchar;
Victor Stinnera4ac6002012-01-21 15:50:49 +0100820 if (lpad != 0 || rpad != 0)
821 maxchar = Py_MAX(maxchar, format->fill_char);
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200822 if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
823 Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
824 maxchar = Py_MAX(maxchar, valmaxchar);
825 }
Victor Stinnera4ac6002012-01-21 15:50:49 +0100826
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200827 /* allocate the resulting string */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200828 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200829 goto done;
830
831 /* Write into that space. First the padding. */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400832 result = fill_padding(writer, len, format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200833 if (result == -1)
834 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200835
836 /* Then the source string. */
Victor Stinnerc9d369f2012-06-16 02:22:37 +0200837 if (len) {
838 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
839 value, 0, len);
840 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200841 writer->pos += (len + rpad);
842 result = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200843
844done:
845 return result;
846}
847
848
849/************************************************************************/
850/*********** long formatting ********************************************/
851/************************************************************************/
852
Victor Stinnerd3f08822012-05-29 12:57:52 +0200853static int
854format_long_internal(PyObject *value, const InternalFormatSpec *format,
855 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200856{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200857 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +0100858 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200859 PyObject *tmp = NULL;
860 Py_ssize_t inumeric_chars;
861 Py_UCS4 sign_char = '\0';
862 Py_ssize_t n_digits; /* count of digits need from the computed
863 string */
864 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
865 produces non-digits */
866 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
867 Py_ssize_t n_total;
Victor Stinnered277852012-02-01 00:22:23 +0100868 Py_ssize_t prefix = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200869 NumberFieldWidths spec;
870 long x;
871
872 /* Locale settings, either from the actual locale or
873 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +0100874 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200875
876 /* no precision allowed on integers */
877 if (format->precision != -1) {
878 PyErr_SetString(PyExc_ValueError,
879 "Precision not allowed in integer format specifier");
880 goto done;
881 }
882
883 /* special case for character formatting */
884 if (format->type == 'c') {
885 /* error to specify a sign */
886 if (format->sign != '\0') {
887 PyErr_SetString(PyExc_ValueError,
888 "Sign not allowed with integer"
889 " format specifier 'c'");
890 goto done;
891 }
Eric V. Smitha12572f2014-04-15 22:37:55 -0400892 /* error to request alternate format */
893 if (format->alternate) {
894 PyErr_SetString(PyExc_ValueError,
895 "Alternate form (#) not allowed with integer"
896 " format specifier 'c'");
897 goto done;
898 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200899
900 /* taken from unicodeobject.c formatchar() */
901 /* Integer input truncated to a character */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200902 x = PyLong_AsLong(value);
903 if (x == -1 && PyErr_Occurred())
904 goto done;
905 if (x < 0 || x > 0x10ffff) {
906 PyErr_SetString(PyExc_OverflowError,
Victor Stinnera4ac6002012-01-21 15:50:49 +0100907 "%c arg not in range(0x110000)");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200908 goto done;
909 }
910 tmp = PyUnicode_FromOrdinal(x);
911 inumeric_chars = 0;
912 n_digits = 1;
Amaury Forgeot d'Arc6d766fc2012-01-23 23:20:43 +0100913 maxchar = Py_MAX(maxchar, (Py_UCS4)x);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200914
915 /* As a sort-of hack, we tell calc_number_widths that we only
916 have "remainder" characters. calc_number_widths thinks
917 these are characters that don't get formatted, only copied
918 into the output string. We do this for 'c' formatting,
919 because the characters are likely to be non-digits. */
920 n_remainder = 1;
921 }
922 else {
923 int base;
924 int leading_chars_to_skip = 0; /* Number of characters added by
925 PyNumber_ToBase that we want to
926 skip over. */
927
928 /* Compute the base and how many characters will be added by
929 PyNumber_ToBase */
930 switch (format->type) {
931 case 'b':
932 base = 2;
933 leading_chars_to_skip = 2; /* 0b */
934 break;
935 case 'o':
936 base = 8;
937 leading_chars_to_skip = 2; /* 0o */
938 break;
939 case 'x':
940 case 'X':
941 base = 16;
942 leading_chars_to_skip = 2; /* 0x */
943 break;
944 default: /* shouldn't be needed, but stops a compiler warning */
945 case 'd':
946 case 'n':
947 base = 10;
948 break;
949 }
950
Victor Stinnerd3f08822012-05-29 12:57:52 +0200951 if (format->sign != '+' && format->sign != ' '
952 && format->width == -1
953 && format->type != 'X' && format->type != 'n'
954 && !format->thousands_separators
955 && PyLong_CheckExact(value))
956 {
957 /* Fast path */
958 return _PyLong_FormatWriter(writer, value, base, format->alternate);
959 }
960
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200961 /* The number of prefix chars is the same as the leading
962 chars to skip */
963 if (format->alternate)
964 n_prefix = leading_chars_to_skip;
965
966 /* Do the hard part, converting to a string in a given base */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200967 tmp = _PyLong_Format(value, base);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968 if (tmp == NULL || PyUnicode_READY(tmp) == -1)
969 goto done;
970
971 inumeric_chars = 0;
972 n_digits = PyUnicode_GET_LENGTH(tmp);
973
974 prefix = inumeric_chars;
975
976 /* Is a sign character present in the output? If so, remember it
977 and skip it */
978 if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
979 sign_char = '-';
980 ++prefix;
981 ++leading_chars_to_skip;
982 }
983
984 /* Skip over the leading chars (0x, 0b, etc.) */
985 n_digits -= leading_chars_to_skip;
986 inumeric_chars += leading_chars_to_skip;
987 }
988
989 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100990 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400991 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +0100992 &locale) == -1)
993 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200994
995 /* Calculate how much memory we'll need. */
996 n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars,
Victor Stinner41a863c2012-02-24 00:37:51 +0100997 inumeric_chars + n_digits, n_remainder, 0,
998 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100999
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001000 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001001 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001002 goto done;
1003
1004 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001005 result = fill_number(writer, &spec,
1006 tmp, inumeric_chars, inumeric_chars + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001007 tmp, prefix, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001008 &locale, format->type == 'X');
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001009
1010done:
1011 Py_XDECREF(tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001012 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001013 return result;
1014}
1015
1016/************************************************************************/
1017/*********** float formatting *******************************************/
1018/************************************************************************/
1019
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001020/* much of this is taken from unicodeobject.c */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001021static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001022format_float_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001023 const InternalFormatSpec *format,
1024 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001025{
1026 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
1027 Py_ssize_t n_digits;
1028 Py_ssize_t n_remainder;
1029 Py_ssize_t n_total;
1030 int has_decimal;
1031 double val;
Victor Stinner76d38502013-06-24 23:34:15 +02001032 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001033 Py_UCS4 type = format->type;
1034 int add_pct = 0;
1035 Py_ssize_t index;
1036 NumberFieldWidths spec;
1037 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001038 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001039 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001040 Py_UCS4 sign_char = '\0';
1041 int float_type; /* Used to see if we have a nan, inf, or regular float. */
1042 PyObject *unicode_tmp = NULL;
1043
1044 /* Locale settings, either from the actual locale or
1045 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001046 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001047
Victor Stinner2f084ec2013-06-23 14:54:30 +02001048 if (format->precision > INT_MAX) {
1049 PyErr_SetString(PyExc_ValueError, "precision too big");
1050 goto done;
1051 }
1052 precision = (int)format->precision;
1053
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001054 if (format->alternate)
1055 flags |= Py_DTSF_ALT;
1056
1057 if (type == '\0') {
1058 /* Omitted type specifier. Behaves in the same way as repr(x)
1059 and str(x) if no precision is given, else like 'g', but with
1060 at least one digit after the decimal point. */
1061 flags |= Py_DTSF_ADD_DOT_0;
1062 type = 'r';
1063 default_precision = 0;
1064 }
1065
1066 if (type == 'n')
1067 /* 'n' is the same as 'g', except for the locale used to
1068 format the result. We take care of that later. */
1069 type = 'g';
1070
1071 val = PyFloat_AsDouble(value);
1072 if (val == -1.0 && PyErr_Occurred())
1073 goto done;
1074
1075 if (type == '%') {
1076 type = 'f';
1077 val *= 100;
1078 add_pct = 1;
1079 }
1080
1081 if (precision < 0)
1082 precision = default_precision;
1083 else if (type == 'r')
1084 type = 'g';
1085
Martin Panter4c359642016-05-08 13:53:41 +00001086 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001087 8-bit char. This is safe, because we've restricted what "type"
1088 can be. */
1089 buf = PyOS_double_to_string(val, (char)type, precision, flags,
1090 &float_type);
1091 if (buf == NULL)
1092 goto done;
1093 n_digits = strlen(buf);
1094
1095 if (add_pct) {
1096 /* We know that buf has a trailing zero (since we just called
1097 strlen() on it), and we don't use that fact any more. So we
1098 can just write over the trailing zero. */
1099 buf[n_digits] = '%';
1100 n_digits += 1;
1101 }
1102
Victor Stinnerd3f08822012-05-29 12:57:52 +02001103 if (format->sign != '+' && format->sign != ' '
1104 && format->width == -1
1105 && format->type != 'n'
1106 && !format->thousands_separators)
1107 {
1108 /* Fast path */
Victor Stinner4a587072013-11-19 12:54:53 +01001109 result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits);
1110 PyMem_Free(buf);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001111 return result;
1112 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001113
Victor Stinner4a587072013-11-19 12:54:53 +01001114 /* Since there is no unicode version of PyOS_double_to_string,
1115 just use the 8 bit version and then convert to unicode. */
1116 unicode_tmp = _PyUnicode_FromASCII(buf, n_digits);
1117 PyMem_Free(buf);
1118 if (unicode_tmp == NULL)
1119 goto done;
1120
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001121 /* Is a sign character present in the output? If so, remember it
1122 and skip it */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001123 index = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001124 if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') {
1125 sign_char = '-';
1126 ++index;
1127 --n_digits;
1128 }
1129
1130 /* Determine if we have any "remainder" (after the digits, might include
1131 decimal or exponent or both (or neither)) */
1132 parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal);
1133
1134 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001135 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -04001136 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +01001137 &locale) == -1)
1138 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001139
1140 /* Calculate how much memory we'll need. */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001141 n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001142 index + n_digits, n_remainder, has_decimal,
Victor Stinner41a863c2012-02-24 00:37:51 +01001143 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +01001144
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001145 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001146 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001147 goto done;
1148
1149 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001150 result = fill_number(writer, &spec,
1151 unicode_tmp, index, index + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001152 NULL, 0, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001153 &locale, 0);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001154
1155done:
Stefan Krahd9c1bf72012-09-06 13:02:46 +02001156 Py_XDECREF(unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001157 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001158 return result;
1159}
1160
1161/************************************************************************/
1162/*********** complex formatting *****************************************/
1163/************************************************************************/
1164
Victor Stinnerd3f08822012-05-29 12:57:52 +02001165static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001166format_complex_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001167 const InternalFormatSpec *format,
1168 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001169{
1170 double re;
1171 double im;
1172 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1173 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1174
1175 InternalFormatSpec tmp_format = *format;
1176 Py_ssize_t n_re_digits;
1177 Py_ssize_t n_im_digits;
1178 Py_ssize_t n_re_remainder;
1179 Py_ssize_t n_im_remainder;
1180 Py_ssize_t n_re_total;
1181 Py_ssize_t n_im_total;
1182 int re_has_decimal;
1183 int im_has_decimal;
Victor Stinner76d38502013-06-24 23:34:15 +02001184 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001185 Py_UCS4 type = format->type;
1186 Py_ssize_t i_re;
1187 Py_ssize_t i_im;
1188 NumberFieldWidths re_spec;
1189 NumberFieldWidths im_spec;
1190 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001191 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001192 Py_UCS4 maxchar = 127;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001193 enum PyUnicode_Kind rkind;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001194 void *rdata;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001195 Py_UCS4 re_sign_char = '\0';
1196 Py_UCS4 im_sign_char = '\0';
1197 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1198 int im_float_type;
1199 int add_parens = 0;
1200 int skip_re = 0;
1201 Py_ssize_t lpad;
1202 Py_ssize_t rpad;
1203 Py_ssize_t total;
1204 PyObject *re_unicode_tmp = NULL;
1205 PyObject *im_unicode_tmp = NULL;
1206
1207 /* Locale settings, either from the actual locale or
1208 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001209 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001210
Victor Stinner2f084ec2013-06-23 14:54:30 +02001211 if (format->precision > INT_MAX) {
1212 PyErr_SetString(PyExc_ValueError, "precision too big");
1213 goto done;
1214 }
1215 precision = (int)format->precision;
1216
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001217 /* Zero padding is not allowed. */
1218 if (format->fill_char == '0') {
1219 PyErr_SetString(PyExc_ValueError,
1220 "Zero padding is not allowed in complex format "
1221 "specifier");
1222 goto done;
1223 }
1224
1225 /* Neither is '=' alignment . */
1226 if (format->align == '=') {
1227 PyErr_SetString(PyExc_ValueError,
1228 "'=' alignment flag is not allowed in complex format "
1229 "specifier");
1230 goto done;
1231 }
1232
1233 re = PyComplex_RealAsDouble(value);
1234 if (re == -1.0 && PyErr_Occurred())
1235 goto done;
1236 im = PyComplex_ImagAsDouble(value);
1237 if (im == -1.0 && PyErr_Occurred())
1238 goto done;
1239
1240 if (format->alternate)
1241 flags |= Py_DTSF_ALT;
1242
1243 if (type == '\0') {
1244 /* Omitted type specifier. Should be like str(self). */
1245 type = 'r';
1246 default_precision = 0;
1247 if (re == 0.0 && copysign(1.0, re) == 1.0)
1248 skip_re = 1;
1249 else
1250 add_parens = 1;
1251 }
1252
1253 if (type == 'n')
1254 /* 'n' is the same as 'g', except for the locale used to
1255 format the result. We take care of that later. */
1256 type = 'g';
1257
1258 if (precision < 0)
1259 precision = default_precision;
1260 else if (type == 'r')
1261 type = 'g';
1262
Martin Panter4c359642016-05-08 13:53:41 +00001263 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001264 8-bit char. This is safe, because we've restricted what "type"
1265 can be. */
1266 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1267 &re_float_type);
1268 if (re_buf == NULL)
1269 goto done;
1270 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1271 &im_float_type);
1272 if (im_buf == NULL)
1273 goto done;
1274
1275 n_re_digits = strlen(re_buf);
1276 n_im_digits = strlen(im_buf);
1277
1278 /* Since there is no unicode version of PyOS_double_to_string,
1279 just use the 8 bit version and then convert to unicode. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001280 re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001281 if (re_unicode_tmp == NULL)
1282 goto done;
1283 i_re = 0;
1284
Victor Stinnerd3f08822012-05-29 12:57:52 +02001285 im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001286 if (im_unicode_tmp == NULL)
1287 goto done;
1288 i_im = 0;
1289
1290 /* Is a sign character present in the output? If so, remember it
1291 and skip it */
1292 if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') {
1293 re_sign_char = '-';
1294 ++i_re;
1295 --n_re_digits;
1296 }
1297 if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') {
1298 im_sign_char = '-';
1299 ++i_im;
1300 --n_im_digits;
1301 }
1302
1303 /* Determine if we have any "remainder" (after the digits, might include
1304 decimal or exponent or both (or neither)) */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001305 parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001306 &n_re_remainder, &re_has_decimal);
Victor Stinnerafbaa202011-09-28 21:50:16 +02001307 parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001308 &n_im_remainder, &im_has_decimal);
1309
1310 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001311 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -04001312 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +01001313 &locale) == -1)
1314 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001315
1316 /* Turn off any padding. We'll do it later after we've composed
1317 the numbers without padding. */
1318 tmp_format.fill_char = '\0';
1319 tmp_format.align = '<';
1320 tmp_format.width = -1;
1321
1322 /* Calculate how much memory we'll need. */
1323 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp,
1324 i_re, i_re + n_re_digits, n_re_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001325 re_has_decimal, &locale, &tmp_format,
1326 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001327
1328 /* Same formatting, but always include a sign, unless the real part is
1329 * going to be omitted, in which case we use whatever sign convention was
1330 * requested by the original format. */
1331 if (!skip_re)
1332 tmp_format.sign = '+';
1333 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp,
1334 i_im, i_im + n_im_digits, n_im_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001335 im_has_decimal, &locale, &tmp_format,
1336 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001337
1338 if (skip_re)
1339 n_re_total = 0;
1340
1341 /* Add 1 for the 'j', and optionally 2 for parens. */
1342 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1343 format->width, format->align, &lpad, &rpad, &total);
1344
Victor Stinner41a863c2012-02-24 00:37:51 +01001345 if (lpad || rpad)
Victor Stinnera4ac6002012-01-21 15:50:49 +01001346 maxchar = Py_MAX(maxchar, format->fill_char);
1347
Victor Stinnerd3f08822012-05-29 12:57:52 +02001348 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001349 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001350 rkind = writer->kind;
1351 rdata = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001352
1353 /* Populate the memory. First, the padding. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001354 result = fill_padding(writer,
1355 n_re_total + n_im_total + 1 + add_parens * 2,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001356 format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001357 if (result == -1)
1358 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001359
Victor Stinnerd3f08822012-05-29 12:57:52 +02001360 if (add_parens) {
1361 PyUnicode_WRITE(rkind, rdata, writer->pos, '(');
1362 writer->pos++;
1363 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001364
1365 if (!skip_re) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001366 result = fill_number(writer, &re_spec,
1367 re_unicode_tmp, i_re, i_re + n_re_digits,
1368 NULL, 0,
1369 0,
1370 &locale, 0);
1371 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001372 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001373 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001374 result = fill_number(writer, &im_spec,
1375 im_unicode_tmp, i_im, i_im + n_im_digits,
1376 NULL, 0,
1377 0,
1378 &locale, 0);
1379 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001380 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001381 PyUnicode_WRITE(rkind, rdata, writer->pos, 'j');
1382 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001383
Victor Stinnerd3f08822012-05-29 12:57:52 +02001384 if (add_parens) {
1385 PyUnicode_WRITE(rkind, rdata, writer->pos, ')');
1386 writer->pos++;
1387 }
1388
1389 writer->pos += rpad;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001390
1391done:
1392 PyMem_Free(re_buf);
1393 PyMem_Free(im_buf);
1394 Py_XDECREF(re_unicode_tmp);
1395 Py_XDECREF(im_unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001396 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001397 return result;
1398}
1399
1400/************************************************************************/
1401/*********** built in formatters ****************************************/
1402/************************************************************************/
doko@ubuntu.com39378f72012-06-21 12:12:20 +02001403static int
Victor Stinnerd3f08822012-05-29 12:57:52 +02001404format_obj(PyObject *obj, _PyUnicodeWriter *writer)
1405{
1406 PyObject *str;
1407 int err;
1408
1409 str = PyObject_Str(obj);
1410 if (str == NULL)
1411 return -1;
1412 err = _PyUnicodeWriter_WriteStr(writer, str);
1413 Py_DECREF(str);
1414 return err;
1415}
1416
1417int
1418_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1419 PyObject *obj,
1420 PyObject *format_spec,
1421 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001422{
1423 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001424
1425 assert(PyUnicode_Check(obj));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001426
1427 /* check for the special case of zero length format spec, make
1428 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001429 if (start == end) {
1430 if (PyUnicode_CheckExact(obj))
1431 return _PyUnicodeWriter_WriteStr(writer, obj);
1432 else
1433 return format_obj(obj, writer);
1434 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001435
1436 /* parse the format_spec */
1437 if (!parse_internal_render_format_spec(format_spec, start, end,
1438 &format, 's', '<'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001439 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001440
1441 /* type conversion? */
1442 switch (format.type) {
1443 case 's':
1444 /* no type conversion needed, already a string. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001445 return format_string_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001446 default:
1447 /* unknown */
1448 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001449 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001450 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001451}
1452
Victor Stinnerd3f08822012-05-29 12:57:52 +02001453int
1454_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1455 PyObject *obj,
1456 PyObject *format_spec,
1457 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001458{
Victor Stinnerd3f08822012-05-29 12:57:52 +02001459 PyObject *tmp = NULL, *str = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001460 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001461 int result = -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001462
1463 /* check for the special case of zero length format spec, make
1464 it equivalent to str(obj) */
1465 if (start == end) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001466 if (PyLong_CheckExact(obj))
1467 return _PyLong_FormatWriter(writer, obj, 10, 0);
1468 else
1469 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001470 }
1471
1472 /* parse the format_spec */
1473 if (!parse_internal_render_format_spec(format_spec, start, end,
1474 &format, 'd', '>'))
1475 goto done;
1476
1477 /* type conversion? */
1478 switch (format.type) {
1479 case 'b':
1480 case 'c':
1481 case 'd':
1482 case 'o':
1483 case 'x':
1484 case 'X':
1485 case 'n':
Serhiy Storchaka95949422013-08-27 19:40:23 +03001486 /* no type conversion needed, already an int. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001487 result = format_long_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001488 break;
1489
1490 case 'e':
1491 case 'E':
1492 case 'f':
1493 case 'F':
1494 case 'g':
1495 case 'G':
1496 case '%':
1497 /* convert to float */
1498 tmp = PyNumber_Float(obj);
1499 if (tmp == NULL)
1500 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001501 result = format_float_internal(tmp, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001502 break;
1503
1504 default:
1505 /* unknown */
1506 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1507 goto done;
1508 }
1509
1510done:
1511 Py_XDECREF(tmp);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001512 Py_XDECREF(str);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001513 return result;
1514}
1515
Victor Stinnerd3f08822012-05-29 12:57:52 +02001516int
1517_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1518 PyObject *obj,
1519 PyObject *format_spec,
1520 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001521{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001522 InternalFormatSpec format;
1523
1524 /* check for the special case of zero length format spec, make
1525 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001526 if (start == end)
1527 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001528
1529 /* parse the format_spec */
1530 if (!parse_internal_render_format_spec(format_spec, start, end,
1531 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001532 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001533
1534 /* type conversion? */
1535 switch (format.type) {
1536 case '\0': /* No format code: like 'g', but with at least one decimal. */
1537 case 'e':
1538 case 'E':
1539 case 'f':
1540 case 'F':
1541 case 'g':
1542 case 'G':
1543 case 'n':
1544 case '%':
1545 /* no conversion, already a float. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001546 return format_float_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001547
1548 default:
1549 /* unknown */
1550 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001551 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001552 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001553}
1554
Victor Stinnerd3f08822012-05-29 12:57:52 +02001555int
1556_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1557 PyObject *obj,
1558 PyObject *format_spec,
1559 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001560{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001561 InternalFormatSpec format;
1562
1563 /* check for the special case of zero length format spec, make
1564 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001565 if (start == end)
1566 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001567
1568 /* parse the format_spec */
1569 if (!parse_internal_render_format_spec(format_spec, start, end,
1570 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001571 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001572
1573 /* type conversion? */
1574 switch (format.type) {
1575 case '\0': /* No format code: like 'g', but with at least one decimal. */
1576 case 'e':
1577 case 'E':
1578 case 'f':
1579 case 'F':
1580 case 'g':
1581 case 'G':
1582 case 'n':
1583 /* no conversion, already a complex. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001584 return format_complex_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001585
1586 default:
1587 /* unknown */
1588 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001589 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001590 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001591}