blob: db9f5b831631819f97f226277c7a50cd2baa32cf [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,
35 "Cannot specify ',' with '%c'.",
36 (char)presentation_type);
37 else
38 PyErr_Format(PyExc_ValueError,
39 "Cannot specify ',' with '\\x%x'.",
40 (unsigned int)presentation_type);
41}
42
43/*
44 get_integer consumes 0 or more decimal digit characters from an
45 input string, updates *result with the corresponding positive
46 integer, and returns the number of digits consumed.
47
48 returns -1 on error.
49*/
50static int
Serhiy Storchaka1f932612016-08-29 15:57:26 +030051get_integer(PyObject *str, Py_ssize_t *ppos, Py_ssize_t end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020052 Py_ssize_t *result)
53{
Serhiy Storchaka1f932612016-08-29 15:57:26 +030054 Py_ssize_t accumulator, digitval, pos = *ppos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020055 int numdigits;
Serhiy Storchaka1f932612016-08-29 15:57:26 +030056 int kind = PyUnicode_KIND(str);
57 void *data = PyUnicode_DATA(str);
58
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020059 accumulator = numdigits = 0;
Serhiy Storchaka1f932612016-08-29 15:57:26 +030060 for (; pos < end; pos++, numdigits++) {
61 digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ(kind, data, pos));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020062 if (digitval < 0)
63 break;
64 /*
Mark Dickinson47862d42011-12-01 15:27:04 +000065 Detect possible overflow before it happens:
66
67 accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
68 accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020069 */
Mark Dickinson47862d42011-12-01 15:27:04 +000070 if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020071 PyErr_Format(PyExc_ValueError,
72 "Too many decimal digits in format string");
Serhiy Storchaka1f932612016-08-29 15:57:26 +030073 *ppos = pos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020074 return -1;
75 }
Mark Dickinson47862d42011-12-01 15:27:04 +000076 accumulator = accumulator * 10 + digitval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020077 }
Serhiy Storchaka1f932612016-08-29 15:57:26 +030078 *ppos = pos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020079 *result = accumulator;
80 return numdigits;
81}
82
83/************************************************************************/
84/*********** standard format specifier parsing **************************/
85/************************************************************************/
86
87/* returns true if this character is a specifier alignment token */
88Py_LOCAL_INLINE(int)
89is_alignment_token(Py_UCS4 c)
90{
91 switch (c) {
92 case '<': case '>': case '=': case '^':
93 return 1;
94 default:
95 return 0;
96 }
97}
98
99/* returns true if this character is a sign element */
100Py_LOCAL_INLINE(int)
101is_sign_element(Py_UCS4 c)
102{
103 switch (c) {
104 case ' ': case '+': case '-':
105 return 1;
106 default:
107 return 0;
108 }
109}
Eric Smith8c663262007-08-25 02:26:07 +0000110
Eric Smith4a7d76d2008-05-30 18:10:19 +0000111
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200112typedef struct {
113 Py_UCS4 fill_char;
114 Py_UCS4 align;
115 int alternate;
116 Py_UCS4 sign;
117 Py_ssize_t width;
118 int thousands_separators;
119 Py_ssize_t precision;
120 Py_UCS4 type;
121} InternalFormatSpec;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000122
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200123#if 0
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700124/* Occasionally useful for debugging. Should normally be commented out. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200125static void
126DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
127{
128 printf("internal format spec: fill_char %d\n", format->fill_char);
129 printf("internal format spec: align %d\n", format->align);
130 printf("internal format spec: alternate %d\n", format->alternate);
131 printf("internal format spec: sign %d\n", format->sign);
132 printf("internal format spec: width %zd\n", format->width);
133 printf("internal format spec: thousands_separators %d\n",
134 format->thousands_separators);
135 printf("internal format spec: precision %zd\n", format->precision);
136 printf("internal format spec: type %c\n", format->type);
137 printf("\n");
138}
139#endif
140
141
142/*
143 ptr points to the start of the format_spec, end points just past its end.
144 fills in format with the parsed information.
145 returns 1 on success, 0 on failure.
146 if failure, sets the exception
147*/
148static int
149parse_internal_render_format_spec(PyObject *format_spec,
150 Py_ssize_t start, Py_ssize_t end,
151 InternalFormatSpec *format,
152 char default_type,
153 char default_align)
154{
155 Py_ssize_t pos = start;
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300156 int kind = PyUnicode_KIND(format_spec);
157 void *data = PyUnicode_DATA(format_spec);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200158 /* end-pos is used throughout this code to specify the length of
159 the input string */
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300160#define READ_spec(index) PyUnicode_READ(kind, data, index)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200161
162 Py_ssize_t consumed;
163 int align_specified = 0;
Eric V. Smith2ea97122014-04-14 11:55:10 -0400164 int fill_char_specified = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200165
Eric V. Smith2ea97122014-04-14 11:55:10 -0400166 format->fill_char = ' ';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200167 format->align = default_align;
168 format->alternate = 0;
169 format->sign = '\0';
170 format->width = -1;
171 format->thousands_separators = 0;
172 format->precision = -1;
173 format->type = default_type;
174
175 /* If the second char is an alignment token,
176 then parse the fill char */
177 if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) {
178 format->align = READ_spec(pos+1);
179 format->fill_char = READ_spec(pos);
Eric V. Smith2ea97122014-04-14 11:55:10 -0400180 fill_char_specified = 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200181 align_specified = 1;
182 pos += 2;
183 }
184 else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) {
185 format->align = READ_spec(pos);
186 align_specified = 1;
187 ++pos;
188 }
189
190 /* Parse the various sign options */
191 if (end-pos >= 1 && is_sign_element(READ_spec(pos))) {
192 format->sign = READ_spec(pos);
193 ++pos;
194 }
195
196 /* If the next character is #, we're in alternate mode. This only
197 applies to integers. */
198 if (end-pos >= 1 && READ_spec(pos) == '#') {
199 format->alternate = 1;
200 ++pos;
201 }
202
203 /* The special case for 0-padding (backwards compat) */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400204 if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200205 format->fill_char = '0';
206 if (!align_specified) {
207 format->align = '=';
208 }
209 ++pos;
210 }
211
212 consumed = get_integer(format_spec, &pos, end, &format->width);
213 if (consumed == -1)
214 /* Overflow error. Exception already set. */
215 return 0;
216
217 /* If consumed is 0, we didn't consume any characters for the
218 width. In that case, reset the width to -1, because
219 get_integer() will have set it to zero. -1 is how we record
220 that the width wasn't specified. */
221 if (consumed == 0)
222 format->width = -1;
223
224 /* Comma signifies add thousands separators */
225 if (end-pos && READ_spec(pos) == ',') {
226 format->thousands_separators = 1;
227 ++pos;
228 }
229
230 /* Parse field precision */
231 if (end-pos && READ_spec(pos) == '.') {
232 ++pos;
233
234 consumed = get_integer(format_spec, &pos, end, &format->precision);
235 if (consumed == -1)
236 /* Overflow error. Exception already set. */
237 return 0;
238
239 /* Not having a precision after a dot is an error. */
240 if (consumed == 0) {
241 PyErr_Format(PyExc_ValueError,
242 "Format specifier missing precision");
243 return 0;
244 }
245
246 }
247
248 /* Finally, parse the type field. */
249
250 if (end-pos > 1) {
Eric V. Smithd25cfe62012-01-19 20:04:28 -0500251 /* More than one char remain, invalid format specifier. */
252 PyErr_Format(PyExc_ValueError, "Invalid format specifier");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200253 return 0;
254 }
255
256 if (end-pos == 1) {
257 format->type = READ_spec(pos);
258 ++pos;
259 }
260
261 /* Do as much validating as we can, just by looking at the format
262 specifier. Do not take into account what type of formatting
263 we're doing (int, float, string). */
264
265 if (format->thousands_separators) {
266 switch (format->type) {
267 case 'd':
268 case 'e':
269 case 'f':
270 case 'g':
271 case 'E':
272 case 'G':
273 case '%':
274 case 'F':
275 case '\0':
276 /* These are allowed. See PEP 378.*/
277 break;
278 default:
279 invalid_comma_type(format->type);
280 return 0;
281 }
282 }
283
Victor Stinnera4ac6002012-01-21 15:50:49 +0100284 assert (format->align <= 127);
285 assert (format->sign <= 127);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200286 return 1;
287}
288
289/* Calculate the padding needed. */
290static void
291calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align,
292 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
293 Py_ssize_t *n_total)
294{
295 if (width >= 0) {
296 if (nchars > width)
297 *n_total = nchars;
298 else
299 *n_total = width;
300 }
301 else {
302 /* not specified, use all of the chars and no more */
303 *n_total = nchars;
304 }
305
306 /* Figure out how much leading space we need, based on the
307 aligning */
308 if (align == '>')
309 *n_lpadding = *n_total - nchars;
310 else if (align == '^')
311 *n_lpadding = (*n_total - nchars) / 2;
312 else if (align == '<' || align == '=')
313 *n_lpadding = 0;
314 else {
315 /* We should never have an unspecified alignment. */
316 *n_lpadding = 0;
317 assert(0);
318 }
319
320 *n_rpadding = *n_total - nchars - *n_lpadding;
321}
322
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200323/* Do the padding, and return a pointer to where the caller-supplied
324 content goes. */
Victor Stinner9ce59bb2013-05-17 00:04:56 +0200325static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200326fill_padding(_PyUnicodeWriter *writer,
327 Py_ssize_t nchars,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200328 Py_UCS4 fill_char, Py_ssize_t n_lpadding,
329 Py_ssize_t n_rpadding)
330{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200331 Py_ssize_t pos;
332
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200333 /* Pad on left. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200334 if (n_lpadding) {
335 pos = writer->pos;
336 _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char);
337 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200338
339 /* Pad on right. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200340 if (n_rpadding) {
341 pos = writer->pos + nchars + n_lpadding;
342 _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char);
343 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200344
345 /* Pointer to the user content. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200346 writer->pos += n_lpadding;
347 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200348}
349
350/************************************************************************/
351/*********** common routines for numeric formatting *********************/
352/************************************************************************/
353
354/* Locale type codes. */
355#define LT_CURRENT_LOCALE 0
356#define LT_DEFAULT_LOCALE 1
357#define LT_NO_LOCALE 2
358
359/* Locale info needed for formatting integers and the part of floats
360 before and including the decimal. Note that locales only support
361 8-bit chars, not unicode. */
362typedef struct {
Victor Stinner41a863c2012-02-24 00:37:51 +0100363 PyObject *decimal_point;
364 PyObject *thousands_sep;
365 const char *grouping;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200366} LocaleInfo;
367
Victor Stinner41a863c2012-02-24 00:37:51 +0100368#define STATIC_LOCALE_INFO_INIT {0, 0, 0}
369
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200370/* describes the layout for an integer, see the comment in
371 calc_number_widths() for details */
372typedef struct {
373 Py_ssize_t n_lpadding;
374 Py_ssize_t n_prefix;
375 Py_ssize_t n_spadding;
376 Py_ssize_t n_rpadding;
377 char sign;
378 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
379 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
380 any grouping chars. */
381 Py_ssize_t n_decimal; /* 0 if only an integer */
382 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
383 excluding the decimal itself, if
384 present. */
385
386 /* These 2 are not the widths of fields, but are needed by
387 STRINGLIB_GROUPING. */
388 Py_ssize_t n_digits; /* The number of digits before a decimal
389 or exponent. */
390 Py_ssize_t n_min_width; /* The min_width we used when we computed
391 the n_grouped_digits width. */
392} NumberFieldWidths;
393
394
395/* Given a number of the form:
396 digits[remainder]
397 where ptr points to the start and end points to the end, find where
398 the integer part ends. This could be a decimal, an exponent, both,
399 or neither.
400 If a decimal point is present, set *has_decimal and increment
401 remainder beyond it.
402 Results are undefined (but shouldn't crash) for improperly
403 formatted strings.
404*/
405static void
406parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end,
407 Py_ssize_t *n_remainder, int *has_decimal)
408{
409 Py_ssize_t remainder;
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300410 int kind = PyUnicode_KIND(s);
411 void *data = PyUnicode_DATA(s);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200412
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300413 while (pos<end && Py_ISDIGIT(PyUnicode_READ(kind, data, pos)))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200414 ++pos;
415 remainder = pos;
416
417 /* Does remainder start with a decimal point? */
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300418 *has_decimal = pos<end && PyUnicode_READ(kind, data, remainder) == '.';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200419
420 /* Skip the decimal point. */
421 if (*has_decimal)
422 remainder++;
423
424 *n_remainder = end - remainder;
425}
426
427/* not all fields of format are used. for example, precision is
428 unused. should this take discrete params in order to be more clear
429 about what it does? or is passing a single format parameter easier
430 and more efficient enough to justify a little obfuscation? */
431static Py_ssize_t
432calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
433 Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start,
434 Py_ssize_t n_end, Py_ssize_t n_remainder,
435 int has_decimal, const LocaleInfo *locale,
Victor Stinner41a863c2012-02-24 00:37:51 +0100436 const InternalFormatSpec *format, Py_UCS4 *maxchar)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200437{
438 Py_ssize_t n_non_digit_non_padding;
439 Py_ssize_t n_padding;
440
441 spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0);
442 spec->n_lpadding = 0;
443 spec->n_prefix = n_prefix;
Victor Stinner41a863c2012-02-24 00:37:51 +0100444 spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200445 spec->n_remainder = n_remainder;
446 spec->n_spadding = 0;
447 spec->n_rpadding = 0;
448 spec->sign = '\0';
449 spec->n_sign = 0;
450
451 /* the output will look like:
452 | |
453 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
454 | |
455
456 sign is computed from format->sign and the actual
457 sign of the number
458
459 prefix is given (it's for the '0x' prefix)
460
461 digits is already known
462
463 the total width is either given, or computed from the
464 actual digits
465
466 only one of lpadding, spadding, and rpadding can be non-zero,
467 and it's calculated from the width and other fields
468 */
469
470 /* compute the various parts we're going to write */
471 switch (format->sign) {
472 case '+':
473 /* always put a + or - */
474 spec->n_sign = 1;
475 spec->sign = (sign_char == '-' ? '-' : '+');
476 break;
477 case ' ':
478 spec->n_sign = 1;
479 spec->sign = (sign_char == '-' ? '-' : ' ');
480 break;
481 default:
482 /* Not specified, or the default (-) */
483 if (sign_char == '-') {
484 spec->n_sign = 1;
485 spec->sign = '-';
486 }
487 }
488
489 /* The number of chars used for non-digits and non-padding. */
490 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
491 spec->n_remainder;
492
493 /* min_width can go negative, that's okay. format->width == -1 means
494 we don't care. */
495 if (format->fill_char == '0' && format->align == '=')
496 spec->n_min_width = format->width - n_non_digit_non_padding;
497 else
498 spec->n_min_width = 0;
499
500 if (spec->n_digits == 0)
501 /* This case only occurs when using 'c' formatting, we need
502 to special case it because the grouping code always wants
503 to have at least one character. */
504 spec->n_grouped_digits = 0;
Victor Stinner41a863c2012-02-24 00:37:51 +0100505 else {
506 Py_UCS4 grouping_maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200507 spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
Victor Stinner41a863c2012-02-24 00:37:51 +0100508 NULL, 0,
509 0, NULL,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200510 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100511 locale->grouping, locale->thousands_sep, &grouping_maxchar);
512 *maxchar = Py_MAX(*maxchar, grouping_maxchar);
513 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200514
515 /* Given the desired width and the total of digit and non-digit
516 space we consume, see if we need any padding. format->width can
517 be negative (meaning no padding), but this code still works in
518 that case. */
519 n_padding = format->width -
520 (n_non_digit_non_padding + spec->n_grouped_digits);
521 if (n_padding > 0) {
522 /* Some padding is needed. Determine if it's left, space, or right. */
523 switch (format->align) {
524 case '<':
525 spec->n_rpadding = n_padding;
526 break;
527 case '^':
528 spec->n_lpadding = n_padding / 2;
529 spec->n_rpadding = n_padding - spec->n_lpadding;
530 break;
531 case '=':
532 spec->n_spadding = n_padding;
533 break;
534 case '>':
535 spec->n_lpadding = n_padding;
536 break;
537 default:
538 /* Shouldn't get here, but treat it as '>' */
539 spec->n_lpadding = n_padding;
540 assert(0);
541 break;
542 }
543 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100544
545 if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding)
546 *maxchar = Py_MAX(*maxchar, format->fill_char);
547
Victor Stinner90f50d42012-02-24 01:44:47 +0100548 if (spec->n_decimal)
549 *maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point));
550
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200551 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
552 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
553 spec->n_remainder + spec->n_rpadding;
554}
555
556/* Fill in the digit parts of a numbers's string representation,
557 as determined in calc_number_widths().
Victor Stinnerafbaa202011-09-28 21:50:16 +0200558 Return -1 on error, or 0 on success. */
559static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200560fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200561 PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end,
Victor Stinnerafbaa202011-09-28 21:50:16 +0200562 PyObject *prefix, Py_ssize_t p_start,
563 Py_UCS4 fill_char,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200564 LocaleInfo *locale, int toupper)
565{
566 /* Used to keep track of digits, decimal, and remainder. */
567 Py_ssize_t d_pos = d_start;
Victor Stinner22c103b2013-05-07 23:50:03 +0200568 const unsigned int kind = writer->kind;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200569 const void *data = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200570 Py_ssize_t r;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200571
572 if (spec->n_lpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200573 _PyUnicode_FastFill(writer->buffer,
574 writer->pos, spec->n_lpadding, fill_char);
575 writer->pos += spec->n_lpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200576 }
577 if (spec->n_sign == 1) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200578 PyUnicode_WRITE(kind, data, writer->pos, spec->sign);
579 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200580 }
581 if (spec->n_prefix) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200582 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
583 prefix, p_start,
584 spec->n_prefix);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200585 if (toupper) {
586 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500587 for (t = 0; t < spec->n_prefix; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200588 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100589 c = Py_TOUPPER(c);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100590 assert (c <= 127);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200591 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500592 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200593 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200594 writer->pos += spec->n_prefix;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200595 }
596 if (spec->n_spadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200597 _PyUnicode_FastFill(writer->buffer,
598 writer->pos, spec->n_spadding, fill_char);
599 writer->pos += spec->n_spadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200600 }
601
602 /* Only for type 'c' special case, it has no digits. */
603 if (spec->n_digits != 0) {
604 /* Fill the digits with InsertThousandsGrouping. */
Victor Stinnerdba2dee2011-09-28 21:50:42 +0200605 char *pdigits;
606 if (PyUnicode_READY(digits))
607 return -1;
608 pdigits = PyUnicode_DATA(digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200609 if (PyUnicode_KIND(digits) < kind) {
610 pdigits = _PyUnicode_AsKind(digits, kind);
Victor Stinnerafbaa202011-09-28 21:50:16 +0200611 if (pdigits == NULL)
612 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200613 }
Victor Stinner90f50d42012-02-24 01:44:47 +0100614 r = _PyUnicode_InsertThousandsGrouping(
Victor Stinnerd3f08822012-05-29 12:57:52 +0200615 writer->buffer, writer->pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200616 spec->n_grouped_digits,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200617 pdigits + kind * d_pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200618 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100619 locale->grouping, locale->thousands_sep, NULL);
Victor Stinner90f50d42012-02-24 01:44:47 +0100620 if (r == -1)
621 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200622 assert(r == spec->n_grouped_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200623 if (PyUnicode_KIND(digits) < kind)
624 PyMem_Free(pdigits);
625 d_pos += spec->n_digits;
626 }
627 if (toupper) {
628 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500629 for (t = 0; t < spec->n_grouped_digits; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200630 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100631 c = Py_TOUPPER(c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500632 if (c > 127) {
633 PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
634 return -1;
635 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200636 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500637 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200638 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200639 writer->pos += spec->n_grouped_digits;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640
641 if (spec->n_decimal) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200642 _PyUnicode_FastCopyCharacters(
643 writer->buffer, writer->pos,
644 locale->decimal_point, 0, spec->n_decimal);
645 writer->pos += spec->n_decimal;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200646 d_pos += 1;
647 }
648
649 if (spec->n_remainder) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200650 _PyUnicode_FastCopyCharacters(
651 writer->buffer, writer->pos,
652 digits, d_pos, spec->n_remainder);
653 writer->pos += spec->n_remainder;
Brett Cannon8a250fa2012-06-25 16:13:44 -0400654 /* d_pos += spec->n_remainder; */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200655 }
656
657 if (spec->n_rpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200658 _PyUnicode_FastFill(writer->buffer,
659 writer->pos, spec->n_rpadding,
660 fill_char);
661 writer->pos += spec->n_rpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200662 }
Victor Stinnerafbaa202011-09-28 21:50:16 +0200663 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200664}
665
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200666static const char no_grouping[1] = {CHAR_MAX};
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200667
668/* Find the decimal point character(s?), thousands_separator(s?), and
669 grouping description, either for the current locale if type is
670 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
671 none if LT_NO_LOCALE. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100672static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200673get_locale_info(int type, LocaleInfo *locale_info)
674{
675 switch (type) {
676 case LT_CURRENT_LOCALE: {
677 struct lconv *locale_data = localeconv();
Victor Stinner41a863c2012-02-24 00:37:51 +0100678 locale_info->decimal_point = PyUnicode_DecodeLocale(
679 locale_data->decimal_point,
680 NULL);
681 if (locale_info->decimal_point == NULL)
682 return -1;
683 locale_info->thousands_sep = PyUnicode_DecodeLocale(
684 locale_data->thousands_sep,
685 NULL);
686 if (locale_info->thousands_sep == NULL) {
687 Py_DECREF(locale_info->decimal_point);
688 return -1;
689 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200690 locale_info->grouping = locale_data->grouping;
691 break;
692 }
693 case LT_DEFAULT_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100694 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
695 locale_info->thousands_sep = PyUnicode_FromOrdinal(',');
696 if (!locale_info->decimal_point || !locale_info->thousands_sep) {
697 Py_XDECREF(locale_info->decimal_point);
698 Py_XDECREF(locale_info->thousands_sep);
699 return -1;
700 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200701 locale_info->grouping = "\3"; /* Group every 3 characters. The
702 (implicit) trailing 0 means repeat
703 infinitely. */
704 break;
705 case LT_NO_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100706 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
707 locale_info->thousands_sep = PyUnicode_New(0, 0);
708 if (!locale_info->decimal_point || !locale_info->thousands_sep) {
709 Py_XDECREF(locale_info->decimal_point);
710 Py_XDECREF(locale_info->thousands_sep);
711 return -1;
712 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200713 locale_info->grouping = no_grouping;
714 break;
715 default:
716 assert(0);
717 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100718 return 0;
719}
720
721static void
722free_locale_info(LocaleInfo *locale_info)
723{
724 Py_XDECREF(locale_info->decimal_point);
725 Py_XDECREF(locale_info->thousands_sep);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200726}
727
728/************************************************************************/
729/*********** string formatting ******************************************/
730/************************************************************************/
731
Victor Stinnerd3f08822012-05-29 12:57:52 +0200732static int
733format_string_internal(PyObject *value, const InternalFormatSpec *format,
734 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200735{
736 Py_ssize_t lpad;
737 Py_ssize_t rpad;
738 Py_ssize_t total;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200739 Py_ssize_t len;
740 int result = -1;
Victor Stinnerece58de2012-04-23 23:36:38 +0200741 Py_UCS4 maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200742
Victor Stinnerd3f08822012-05-29 12:57:52 +0200743 assert(PyUnicode_IS_READY(value));
744 len = PyUnicode_GET_LENGTH(value);
745
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200746 /* sign is not allowed on strings */
747 if (format->sign != '\0') {
748 PyErr_SetString(PyExc_ValueError,
749 "Sign not allowed in string format specifier");
750 goto done;
751 }
752
753 /* alternate is not allowed on strings */
754 if (format->alternate) {
755 PyErr_SetString(PyExc_ValueError,
756 "Alternate form (#) not allowed in string format "
757 "specifier");
758 goto done;
759 }
760
761 /* '=' alignment not allowed on strings */
762 if (format->align == '=') {
763 PyErr_SetString(PyExc_ValueError,
764 "'=' alignment not allowed "
765 "in string format specifier");
766 goto done;
767 }
768
Victor Stinner621ef3d2012-10-02 00:33:47 +0200769 if ((format->width == -1 || format->width <= len)
770 && (format->precision == -1 || format->precision >= len)) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200771 /* Fast path */
772 return _PyUnicodeWriter_WriteStr(writer, value);
773 }
774
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200775 /* if precision is specified, output no more that format.precision
776 characters */
777 if (format->precision >= 0 && len >= format->precision) {
778 len = format->precision;
779 }
780
781 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
782
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200783 maxchar = writer->maxchar;
Victor Stinnera4ac6002012-01-21 15:50:49 +0100784 if (lpad != 0 || rpad != 0)
785 maxchar = Py_MAX(maxchar, format->fill_char);
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200786 if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
787 Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
788 maxchar = Py_MAX(maxchar, valmaxchar);
789 }
Victor Stinnera4ac6002012-01-21 15:50:49 +0100790
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200791 /* allocate the resulting string */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200792 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200793 goto done;
794
795 /* Write into that space. First the padding. */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400796 result = fill_padding(writer, len, format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200797 if (result == -1)
798 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200799
800 /* Then the source string. */
Victor Stinnerc9d369f2012-06-16 02:22:37 +0200801 if (len) {
802 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
803 value, 0, len);
804 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200805 writer->pos += (len + rpad);
806 result = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200807
808done:
809 return result;
810}
811
812
813/************************************************************************/
814/*********** long formatting ********************************************/
815/************************************************************************/
816
Victor Stinnerd3f08822012-05-29 12:57:52 +0200817static int
818format_long_internal(PyObject *value, const InternalFormatSpec *format,
819 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200820{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200821 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +0100822 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200823 PyObject *tmp = NULL;
824 Py_ssize_t inumeric_chars;
825 Py_UCS4 sign_char = '\0';
826 Py_ssize_t n_digits; /* count of digits need from the computed
827 string */
828 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
829 produces non-digits */
830 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
831 Py_ssize_t n_total;
Victor Stinnered277852012-02-01 00:22:23 +0100832 Py_ssize_t prefix = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200833 NumberFieldWidths spec;
834 long x;
835
836 /* Locale settings, either from the actual locale or
837 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +0100838 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200839
840 /* no precision allowed on integers */
841 if (format->precision != -1) {
842 PyErr_SetString(PyExc_ValueError,
843 "Precision not allowed in integer format specifier");
844 goto done;
845 }
846
847 /* special case for character formatting */
848 if (format->type == 'c') {
849 /* error to specify a sign */
850 if (format->sign != '\0') {
851 PyErr_SetString(PyExc_ValueError,
852 "Sign not allowed with integer"
853 " format specifier 'c'");
854 goto done;
855 }
Eric V. Smitha12572f2014-04-15 22:37:55 -0400856 /* error to request alternate format */
857 if (format->alternate) {
858 PyErr_SetString(PyExc_ValueError,
859 "Alternate form (#) not allowed with integer"
860 " format specifier 'c'");
861 goto done;
862 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200863
864 /* taken from unicodeobject.c formatchar() */
865 /* Integer input truncated to a character */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200866 x = PyLong_AsLong(value);
867 if (x == -1 && PyErr_Occurred())
868 goto done;
869 if (x < 0 || x > 0x10ffff) {
870 PyErr_SetString(PyExc_OverflowError,
Victor Stinnera4ac6002012-01-21 15:50:49 +0100871 "%c arg not in range(0x110000)");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200872 goto done;
873 }
874 tmp = PyUnicode_FromOrdinal(x);
875 inumeric_chars = 0;
876 n_digits = 1;
Amaury Forgeot d'Arc6d766fc2012-01-23 23:20:43 +0100877 maxchar = Py_MAX(maxchar, (Py_UCS4)x);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200878
879 /* As a sort-of hack, we tell calc_number_widths that we only
880 have "remainder" characters. calc_number_widths thinks
881 these are characters that don't get formatted, only copied
882 into the output string. We do this for 'c' formatting,
883 because the characters are likely to be non-digits. */
884 n_remainder = 1;
885 }
886 else {
887 int base;
888 int leading_chars_to_skip = 0; /* Number of characters added by
889 PyNumber_ToBase that we want to
890 skip over. */
891
892 /* Compute the base and how many characters will be added by
893 PyNumber_ToBase */
894 switch (format->type) {
895 case 'b':
896 base = 2;
897 leading_chars_to_skip = 2; /* 0b */
898 break;
899 case 'o':
900 base = 8;
901 leading_chars_to_skip = 2; /* 0o */
902 break;
903 case 'x':
904 case 'X':
905 base = 16;
906 leading_chars_to_skip = 2; /* 0x */
907 break;
908 default: /* shouldn't be needed, but stops a compiler warning */
909 case 'd':
910 case 'n':
911 base = 10;
912 break;
913 }
914
Victor Stinnerd3f08822012-05-29 12:57:52 +0200915 if (format->sign != '+' && format->sign != ' '
916 && format->width == -1
917 && format->type != 'X' && format->type != 'n'
918 && !format->thousands_separators
919 && PyLong_CheckExact(value))
920 {
921 /* Fast path */
922 return _PyLong_FormatWriter(writer, value, base, format->alternate);
923 }
924
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200925 /* The number of prefix chars is the same as the leading
926 chars to skip */
927 if (format->alternate)
928 n_prefix = leading_chars_to_skip;
929
930 /* Do the hard part, converting to a string in a given base */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200931 tmp = _PyLong_Format(value, base);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200932 if (tmp == NULL || PyUnicode_READY(tmp) == -1)
933 goto done;
934
935 inumeric_chars = 0;
936 n_digits = PyUnicode_GET_LENGTH(tmp);
937
938 prefix = inumeric_chars;
939
940 /* Is a sign character present in the output? If so, remember it
941 and skip it */
942 if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
943 sign_char = '-';
944 ++prefix;
945 ++leading_chars_to_skip;
946 }
947
948 /* Skip over the leading chars (0x, 0b, etc.) */
949 n_digits -= leading_chars_to_skip;
950 inumeric_chars += leading_chars_to_skip;
951 }
952
953 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100954 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
955 (format->thousands_separators ?
956 LT_DEFAULT_LOCALE :
957 LT_NO_LOCALE),
958 &locale) == -1)
959 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200960
961 /* Calculate how much memory we'll need. */
962 n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars,
Victor Stinner41a863c2012-02-24 00:37:51 +0100963 inumeric_chars + n_digits, n_remainder, 0,
964 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100965
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200966 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200967 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968 goto done;
969
970 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200971 result = fill_number(writer, &spec,
972 tmp, inumeric_chars, inumeric_chars + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -0400973 tmp, prefix, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +0200974 &locale, format->type == 'X');
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200975
976done:
977 Py_XDECREF(tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +0100978 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200979 return result;
980}
981
982/************************************************************************/
983/*********** float formatting *******************************************/
984/************************************************************************/
985
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200986/* much of this is taken from unicodeobject.c */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200987static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200988format_float_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +0200989 const InternalFormatSpec *format,
990 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200991{
992 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
993 Py_ssize_t n_digits;
994 Py_ssize_t n_remainder;
995 Py_ssize_t n_total;
996 int has_decimal;
997 double val;
Victor Stinner76d38502013-06-24 23:34:15 +0200998 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200999 Py_UCS4 type = format->type;
1000 int add_pct = 0;
1001 Py_ssize_t index;
1002 NumberFieldWidths spec;
1003 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001004 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001005 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001006 Py_UCS4 sign_char = '\0';
1007 int float_type; /* Used to see if we have a nan, inf, or regular float. */
1008 PyObject *unicode_tmp = NULL;
1009
1010 /* Locale settings, either from the actual locale or
1011 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001012 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001013
Victor Stinner2f084ec2013-06-23 14:54:30 +02001014 if (format->precision > INT_MAX) {
1015 PyErr_SetString(PyExc_ValueError, "precision too big");
1016 goto done;
1017 }
1018 precision = (int)format->precision;
1019
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001020 if (format->alternate)
1021 flags |= Py_DTSF_ALT;
1022
1023 if (type == '\0') {
1024 /* Omitted type specifier. Behaves in the same way as repr(x)
1025 and str(x) if no precision is given, else like 'g', but with
1026 at least one digit after the decimal point. */
1027 flags |= Py_DTSF_ADD_DOT_0;
1028 type = 'r';
1029 default_precision = 0;
1030 }
1031
1032 if (type == 'n')
1033 /* 'n' is the same as 'g', except for the locale used to
1034 format the result. We take care of that later. */
1035 type = 'g';
1036
1037 val = PyFloat_AsDouble(value);
1038 if (val == -1.0 && PyErr_Occurred())
1039 goto done;
1040
1041 if (type == '%') {
1042 type = 'f';
1043 val *= 100;
1044 add_pct = 1;
1045 }
1046
1047 if (precision < 0)
1048 precision = default_precision;
1049 else if (type == 'r')
1050 type = 'g';
1051
Martin Panter4c359642016-05-08 13:53:41 +00001052 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001053 8-bit char. This is safe, because we've restricted what "type"
1054 can be. */
1055 buf = PyOS_double_to_string(val, (char)type, precision, flags,
1056 &float_type);
1057 if (buf == NULL)
1058 goto done;
1059 n_digits = strlen(buf);
1060
1061 if (add_pct) {
1062 /* We know that buf has a trailing zero (since we just called
1063 strlen() on it), and we don't use that fact any more. So we
1064 can just write over the trailing zero. */
1065 buf[n_digits] = '%';
1066 n_digits += 1;
1067 }
1068
Victor Stinnerd3f08822012-05-29 12:57:52 +02001069 if (format->sign != '+' && format->sign != ' '
1070 && format->width == -1
1071 && format->type != 'n'
1072 && !format->thousands_separators)
1073 {
1074 /* Fast path */
Victor Stinner4a587072013-11-19 12:54:53 +01001075 result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits);
1076 PyMem_Free(buf);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001077 return result;
1078 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001079
Victor Stinner4a587072013-11-19 12:54:53 +01001080 /* Since there is no unicode version of PyOS_double_to_string,
1081 just use the 8 bit version and then convert to unicode. */
1082 unicode_tmp = _PyUnicode_FromASCII(buf, n_digits);
1083 PyMem_Free(buf);
1084 if (unicode_tmp == NULL)
1085 goto done;
1086
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001087 /* Is a sign character present in the output? If so, remember it
1088 and skip it */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001089 index = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001090 if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') {
1091 sign_char = '-';
1092 ++index;
1093 --n_digits;
1094 }
1095
1096 /* Determine if we have any "remainder" (after the digits, might include
1097 decimal or exponent or both (or neither)) */
1098 parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal);
1099
1100 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001101 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1102 (format->thousands_separators ?
1103 LT_DEFAULT_LOCALE :
1104 LT_NO_LOCALE),
1105 &locale) == -1)
1106 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001107
1108 /* Calculate how much memory we'll need. */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001109 n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001110 index + n_digits, n_remainder, has_decimal,
Victor Stinner41a863c2012-02-24 00:37:51 +01001111 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +01001112
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001113 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001114 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001115 goto done;
1116
1117 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001118 result = fill_number(writer, &spec,
1119 unicode_tmp, index, index + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001120 NULL, 0, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001121 &locale, 0);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001122
1123done:
Stefan Krahd9c1bf72012-09-06 13:02:46 +02001124 Py_XDECREF(unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001125 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001126 return result;
1127}
1128
1129/************************************************************************/
1130/*********** complex formatting *****************************************/
1131/************************************************************************/
1132
Victor Stinnerd3f08822012-05-29 12:57:52 +02001133static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001134format_complex_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001135 const InternalFormatSpec *format,
1136 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001137{
1138 double re;
1139 double im;
1140 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1141 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1142
1143 InternalFormatSpec tmp_format = *format;
1144 Py_ssize_t n_re_digits;
1145 Py_ssize_t n_im_digits;
1146 Py_ssize_t n_re_remainder;
1147 Py_ssize_t n_im_remainder;
1148 Py_ssize_t n_re_total;
1149 Py_ssize_t n_im_total;
1150 int re_has_decimal;
1151 int im_has_decimal;
Victor Stinner76d38502013-06-24 23:34:15 +02001152 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001153 Py_UCS4 type = format->type;
1154 Py_ssize_t i_re;
1155 Py_ssize_t i_im;
1156 NumberFieldWidths re_spec;
1157 NumberFieldWidths im_spec;
1158 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001159 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001160 Py_UCS4 maxchar = 127;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001161 enum PyUnicode_Kind rkind;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001162 void *rdata;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001163 Py_UCS4 re_sign_char = '\0';
1164 Py_UCS4 im_sign_char = '\0';
1165 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1166 int im_float_type;
1167 int add_parens = 0;
1168 int skip_re = 0;
1169 Py_ssize_t lpad;
1170 Py_ssize_t rpad;
1171 Py_ssize_t total;
1172 PyObject *re_unicode_tmp = NULL;
1173 PyObject *im_unicode_tmp = NULL;
1174
1175 /* Locale settings, either from the actual locale or
1176 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001177 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001178
Victor Stinner2f084ec2013-06-23 14:54:30 +02001179 if (format->precision > INT_MAX) {
1180 PyErr_SetString(PyExc_ValueError, "precision too big");
1181 goto done;
1182 }
1183 precision = (int)format->precision;
1184
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001185 /* Zero padding is not allowed. */
1186 if (format->fill_char == '0') {
1187 PyErr_SetString(PyExc_ValueError,
1188 "Zero padding is not allowed in complex format "
1189 "specifier");
1190 goto done;
1191 }
1192
1193 /* Neither is '=' alignment . */
1194 if (format->align == '=') {
1195 PyErr_SetString(PyExc_ValueError,
1196 "'=' alignment flag is not allowed in complex format "
1197 "specifier");
1198 goto done;
1199 }
1200
1201 re = PyComplex_RealAsDouble(value);
1202 if (re == -1.0 && PyErr_Occurred())
1203 goto done;
1204 im = PyComplex_ImagAsDouble(value);
1205 if (im == -1.0 && PyErr_Occurred())
1206 goto done;
1207
1208 if (format->alternate)
1209 flags |= Py_DTSF_ALT;
1210
1211 if (type == '\0') {
1212 /* Omitted type specifier. Should be like str(self). */
1213 type = 'r';
1214 default_precision = 0;
1215 if (re == 0.0 && copysign(1.0, re) == 1.0)
1216 skip_re = 1;
1217 else
1218 add_parens = 1;
1219 }
1220
1221 if (type == 'n')
1222 /* 'n' is the same as 'g', except for the locale used to
1223 format the result. We take care of that later. */
1224 type = 'g';
1225
1226 if (precision < 0)
1227 precision = default_precision;
1228 else if (type == 'r')
1229 type = 'g';
1230
Martin Panter4c359642016-05-08 13:53:41 +00001231 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001232 8-bit char. This is safe, because we've restricted what "type"
1233 can be. */
1234 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1235 &re_float_type);
1236 if (re_buf == NULL)
1237 goto done;
1238 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1239 &im_float_type);
1240 if (im_buf == NULL)
1241 goto done;
1242
1243 n_re_digits = strlen(re_buf);
1244 n_im_digits = strlen(im_buf);
1245
1246 /* Since there is no unicode version of PyOS_double_to_string,
1247 just use the 8 bit version and then convert to unicode. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001248 re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001249 if (re_unicode_tmp == NULL)
1250 goto done;
1251 i_re = 0;
1252
Victor Stinnerd3f08822012-05-29 12:57:52 +02001253 im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001254 if (im_unicode_tmp == NULL)
1255 goto done;
1256 i_im = 0;
1257
1258 /* Is a sign character present in the output? If so, remember it
1259 and skip it */
1260 if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') {
1261 re_sign_char = '-';
1262 ++i_re;
1263 --n_re_digits;
1264 }
1265 if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') {
1266 im_sign_char = '-';
1267 ++i_im;
1268 --n_im_digits;
1269 }
1270
1271 /* Determine if we have any "remainder" (after the digits, might include
1272 decimal or exponent or both (or neither)) */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001273 parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001274 &n_re_remainder, &re_has_decimal);
Victor Stinnerafbaa202011-09-28 21:50:16 +02001275 parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001276 &n_im_remainder, &im_has_decimal);
1277
1278 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001279 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1280 (format->thousands_separators ?
1281 LT_DEFAULT_LOCALE :
1282 LT_NO_LOCALE),
1283 &locale) == -1)
1284 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001285
1286 /* Turn off any padding. We'll do it later after we've composed
1287 the numbers without padding. */
1288 tmp_format.fill_char = '\0';
1289 tmp_format.align = '<';
1290 tmp_format.width = -1;
1291
1292 /* Calculate how much memory we'll need. */
1293 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp,
1294 i_re, i_re + n_re_digits, n_re_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001295 re_has_decimal, &locale, &tmp_format,
1296 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001297
1298 /* Same formatting, but always include a sign, unless the real part is
1299 * going to be omitted, in which case we use whatever sign convention was
1300 * requested by the original format. */
1301 if (!skip_re)
1302 tmp_format.sign = '+';
1303 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp,
1304 i_im, i_im + n_im_digits, n_im_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001305 im_has_decimal, &locale, &tmp_format,
1306 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001307
1308 if (skip_re)
1309 n_re_total = 0;
1310
1311 /* Add 1 for the 'j', and optionally 2 for parens. */
1312 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1313 format->width, format->align, &lpad, &rpad, &total);
1314
Victor Stinner41a863c2012-02-24 00:37:51 +01001315 if (lpad || rpad)
Victor Stinnera4ac6002012-01-21 15:50:49 +01001316 maxchar = Py_MAX(maxchar, format->fill_char);
1317
Victor Stinnerd3f08822012-05-29 12:57:52 +02001318 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001319 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001320 rkind = writer->kind;
1321 rdata = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001322
1323 /* Populate the memory. First, the padding. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001324 result = fill_padding(writer,
1325 n_re_total + n_im_total + 1 + add_parens * 2,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001326 format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001327 if (result == -1)
1328 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001329
Victor Stinnerd3f08822012-05-29 12:57:52 +02001330 if (add_parens) {
1331 PyUnicode_WRITE(rkind, rdata, writer->pos, '(');
1332 writer->pos++;
1333 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001334
1335 if (!skip_re) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001336 result = fill_number(writer, &re_spec,
1337 re_unicode_tmp, i_re, i_re + n_re_digits,
1338 NULL, 0,
1339 0,
1340 &locale, 0);
1341 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001342 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001343 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001344 result = fill_number(writer, &im_spec,
1345 im_unicode_tmp, i_im, i_im + n_im_digits,
1346 NULL, 0,
1347 0,
1348 &locale, 0);
1349 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001350 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001351 PyUnicode_WRITE(rkind, rdata, writer->pos, 'j');
1352 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001353
Victor Stinnerd3f08822012-05-29 12:57:52 +02001354 if (add_parens) {
1355 PyUnicode_WRITE(rkind, rdata, writer->pos, ')');
1356 writer->pos++;
1357 }
1358
1359 writer->pos += rpad;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001360
1361done:
1362 PyMem_Free(re_buf);
1363 PyMem_Free(im_buf);
1364 Py_XDECREF(re_unicode_tmp);
1365 Py_XDECREF(im_unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001366 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001367 return result;
1368}
1369
1370/************************************************************************/
1371/*********** built in formatters ****************************************/
1372/************************************************************************/
doko@ubuntu.com39378f72012-06-21 12:12:20 +02001373static int
Victor Stinnerd3f08822012-05-29 12:57:52 +02001374format_obj(PyObject *obj, _PyUnicodeWriter *writer)
1375{
1376 PyObject *str;
1377 int err;
1378
1379 str = PyObject_Str(obj);
1380 if (str == NULL)
1381 return -1;
1382 err = _PyUnicodeWriter_WriteStr(writer, str);
1383 Py_DECREF(str);
1384 return err;
1385}
1386
1387int
1388_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1389 PyObject *obj,
1390 PyObject *format_spec,
1391 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001392{
1393 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001394
1395 assert(PyUnicode_Check(obj));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001396
1397 /* check for the special case of zero length format spec, make
1398 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001399 if (start == end) {
1400 if (PyUnicode_CheckExact(obj))
1401 return _PyUnicodeWriter_WriteStr(writer, obj);
1402 else
1403 return format_obj(obj, writer);
1404 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001405
1406 /* parse the format_spec */
1407 if (!parse_internal_render_format_spec(format_spec, start, end,
1408 &format, 's', '<'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001409 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001410
1411 /* type conversion? */
1412 switch (format.type) {
1413 case 's':
1414 /* no type conversion needed, already a string. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001415 return format_string_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001416 default:
1417 /* unknown */
1418 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001419 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001420 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001421}
1422
Victor Stinnerd3f08822012-05-29 12:57:52 +02001423int
1424_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1425 PyObject *obj,
1426 PyObject *format_spec,
1427 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001428{
Victor Stinnerd3f08822012-05-29 12:57:52 +02001429 PyObject *tmp = NULL, *str = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001430 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001431 int result = -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001432
1433 /* check for the special case of zero length format spec, make
1434 it equivalent to str(obj) */
1435 if (start == end) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001436 if (PyLong_CheckExact(obj))
1437 return _PyLong_FormatWriter(writer, obj, 10, 0);
1438 else
1439 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001440 }
1441
1442 /* parse the format_spec */
1443 if (!parse_internal_render_format_spec(format_spec, start, end,
1444 &format, 'd', '>'))
1445 goto done;
1446
1447 /* type conversion? */
1448 switch (format.type) {
1449 case 'b':
1450 case 'c':
1451 case 'd':
1452 case 'o':
1453 case 'x':
1454 case 'X':
1455 case 'n':
Serhiy Storchaka95949422013-08-27 19:40:23 +03001456 /* no type conversion needed, already an int. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001457 result = format_long_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001458 break;
1459
1460 case 'e':
1461 case 'E':
1462 case 'f':
1463 case 'F':
1464 case 'g':
1465 case 'G':
1466 case '%':
1467 /* convert to float */
1468 tmp = PyNumber_Float(obj);
1469 if (tmp == NULL)
1470 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001471 result = format_float_internal(tmp, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001472 break;
1473
1474 default:
1475 /* unknown */
1476 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1477 goto done;
1478 }
1479
1480done:
1481 Py_XDECREF(tmp);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001482 Py_XDECREF(str);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001483 return result;
1484}
1485
Victor Stinnerd3f08822012-05-29 12:57:52 +02001486int
1487_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1488 PyObject *obj,
1489 PyObject *format_spec,
1490 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001491{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001492 InternalFormatSpec format;
1493
1494 /* check for the special case of zero length format spec, make
1495 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001496 if (start == end)
1497 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001498
1499 /* parse the format_spec */
1500 if (!parse_internal_render_format_spec(format_spec, start, end,
1501 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001502 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001503
1504 /* type conversion? */
1505 switch (format.type) {
1506 case '\0': /* No format code: like 'g', but with at least one decimal. */
1507 case 'e':
1508 case 'E':
1509 case 'f':
1510 case 'F':
1511 case 'g':
1512 case 'G':
1513 case 'n':
1514 case '%':
1515 /* no conversion, already a float. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001516 return format_float_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001517
1518 default:
1519 /* unknown */
1520 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001521 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001522 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001523}
1524
Victor Stinnerd3f08822012-05-29 12:57:52 +02001525int
1526_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1527 PyObject *obj,
1528 PyObject *format_spec,
1529 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001530{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001531 InternalFormatSpec format;
1532
1533 /* check for the special case of zero length format spec, make
1534 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001535 if (start == end)
1536 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001537
1538 /* parse the format_spec */
1539 if (!parse_internal_render_format_spec(format_spec, start, end,
1540 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001541 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001542
1543 /* type conversion? */
1544 switch (format.type) {
1545 case '\0': /* No format code: like 'g', but with at least one decimal. */
1546 case 'e':
1547 case 'E':
1548 case 'f':
1549 case 'F':
1550 case 'g':
1551 case 'G':
1552 case 'n':
1553 /* no conversion, already a complex. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001554 return format_complex_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001555
1556 default:
1557 /* unknown */
1558 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001559 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001560 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001561}