blob: 397ae7faafc223efad4fb482f7e92d96596708bf [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,
Dargor28773ca2017-10-15 04:41:13 +010035 "Cannot specify ',' with '%c'.",
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020036 (char)presentation_type);
37 else
38 PyErr_Format(PyExc_ValueError,
Dargor28773ca2017-10-15 04:41:13 +010039 "Cannot specify ',' 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. */
Benjamin Peterson995026a2016-09-13 22:46:15 -0700118enum LocaleType {
119 LT_NO_LOCALE = 0,
120 LT_DEFAULT_LOCALE,
121 LT_UNDERSCORE_LOCALE,
122 LT_UNDER_FOUR_LOCALE,
123 LT_CURRENT_LOCALE
124};
Eric Smith4a7d76d2008-05-30 18:10:19 +0000125
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200126typedef struct {
127 Py_UCS4 fill_char;
128 Py_UCS4 align;
129 int alternate;
130 Py_UCS4 sign;
131 Py_ssize_t width;
Benjamin Peterson995026a2016-09-13 22:46:15 -0700132 enum LocaleType thousands_separators;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200133 Py_ssize_t precision;
134 Py_UCS4 type;
135} InternalFormatSpec;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000136
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200137#if 0
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700138/* Occasionally useful for debugging. Should normally be commented out. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200139static void
140DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
141{
142 printf("internal format spec: fill_char %d\n", format->fill_char);
143 printf("internal format spec: align %d\n", format->align);
144 printf("internal format spec: alternate %d\n", format->alternate);
145 printf("internal format spec: sign %d\n", format->sign);
146 printf("internal format spec: width %zd\n", format->width);
147 printf("internal format spec: thousands_separators %d\n",
148 format->thousands_separators);
149 printf("internal format spec: precision %zd\n", format->precision);
150 printf("internal format spec: type %c\n", format->type);
151 printf("\n");
152}
153#endif
154
155
156/*
157 ptr points to the start of the format_spec, end points just past its end.
158 fills in format with the parsed information.
159 returns 1 on success, 0 on failure.
160 if failure, sets the exception
161*/
162static int
163parse_internal_render_format_spec(PyObject *format_spec,
164 Py_ssize_t start, Py_ssize_t end,
165 InternalFormatSpec *format,
166 char default_type,
167 char default_align)
168{
169 Py_ssize_t pos = start;
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300170 int kind = PyUnicode_KIND(format_spec);
171 void *data = PyUnicode_DATA(format_spec);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200172 /* end-pos is used throughout this code to specify the length of
173 the input string */
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300174#define READ_spec(index) PyUnicode_READ(kind, data, index)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200175
176 Py_ssize_t consumed;
177 int align_specified = 0;
Eric V. Smith2ea97122014-04-14 11:55:10 -0400178 int fill_char_specified = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200179
Eric V. Smith2ea97122014-04-14 11:55:10 -0400180 format->fill_char = ' ';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200181 format->align = default_align;
182 format->alternate = 0;
183 format->sign = '\0';
184 format->width = -1;
Benjamin Peterson995026a2016-09-13 22:46:15 -0700185 format->thousands_separators = LT_NO_LOCALE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200186 format->precision = -1;
187 format->type = default_type;
188
189 /* If the second char is an alignment token,
190 then parse the fill char */
191 if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) {
192 format->align = READ_spec(pos+1);
193 format->fill_char = READ_spec(pos);
Eric V. Smith2ea97122014-04-14 11:55:10 -0400194 fill_char_specified = 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200195 align_specified = 1;
196 pos += 2;
197 }
198 else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) {
199 format->align = READ_spec(pos);
200 align_specified = 1;
201 ++pos;
202 }
203
204 /* Parse the various sign options */
205 if (end-pos >= 1 && is_sign_element(READ_spec(pos))) {
206 format->sign = READ_spec(pos);
207 ++pos;
208 }
209
210 /* If the next character is #, we're in alternate mode. This only
211 applies to integers. */
212 if (end-pos >= 1 && READ_spec(pos) == '#') {
213 format->alternate = 1;
214 ++pos;
215 }
216
217 /* The special case for 0-padding (backwards compat) */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400218 if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200219 format->fill_char = '0';
220 if (!align_specified) {
221 format->align = '=';
222 }
223 ++pos;
224 }
225
226 consumed = get_integer(format_spec, &pos, end, &format->width);
227 if (consumed == -1)
228 /* Overflow error. Exception already set. */
229 return 0;
230
231 /* If consumed is 0, we didn't consume any characters for the
232 width. In that case, reset the width to -1, because
233 get_integer() will have set it to zero. -1 is how we record
234 that the width wasn't specified. */
235 if (consumed == 0)
236 format->width = -1;
237
238 /* Comma signifies add thousands separators */
239 if (end-pos && READ_spec(pos) == ',') {
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400240 format->thousands_separators = LT_DEFAULT_LOCALE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200241 ++pos;
242 }
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400243 /* Underscore signifies add thousands separators */
244 if (end-pos && READ_spec(pos) == '_') {
Benjamin Peterson995026a2016-09-13 22:46:15 -0700245 if (format->thousands_separators != LT_NO_LOCALE) {
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400246 invalid_comma_and_underscore();
247 return 0;
248 }
249 format->thousands_separators = LT_UNDERSCORE_LOCALE;
250 ++pos;
251 }
252 if (end-pos && READ_spec(pos) == ',') {
253 invalid_comma_and_underscore();
254 return 0;
255 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200256
257 /* Parse field precision */
258 if (end-pos && READ_spec(pos) == '.') {
259 ++pos;
260
261 consumed = get_integer(format_spec, &pos, end, &format->precision);
262 if (consumed == -1)
263 /* Overflow error. Exception already set. */
264 return 0;
265
266 /* Not having a precision after a dot is an error. */
267 if (consumed == 0) {
268 PyErr_Format(PyExc_ValueError,
269 "Format specifier missing precision");
270 return 0;
271 }
272
273 }
274
275 /* Finally, parse the type field. */
276
277 if (end-pos > 1) {
Eric V. Smithd25cfe62012-01-19 20:04:28 -0500278 /* More than one char remain, invalid format specifier. */
279 PyErr_Format(PyExc_ValueError, "Invalid format specifier");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200280 return 0;
281 }
282
283 if (end-pos == 1) {
284 format->type = READ_spec(pos);
285 ++pos;
286 }
287
288 /* Do as much validating as we can, just by looking at the format
289 specifier. Do not take into account what type of formatting
290 we're doing (int, float, string). */
291
292 if (format->thousands_separators) {
293 switch (format->type) {
294 case 'd':
295 case 'e':
296 case 'f':
297 case 'g':
298 case 'E':
299 case 'G':
300 case '%':
301 case 'F':
302 case '\0':
303 /* These are allowed. See PEP 378.*/
304 break;
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400305 case 'b':
306 case 'o':
307 case 'x':
308 case 'X':
309 /* Underscores are allowed in bin/oct/hex. See PEP 515. */
310 if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
311 /* Every four digits, not every three, in bin/oct/hex. */
312 format->thousands_separators = LT_UNDER_FOUR_LOCALE;
313 break;
314 }
Stefan Krahf432a322017-08-21 13:09:59 +0200315 /* fall through */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200316 default:
317 invalid_comma_type(format->type);
318 return 0;
319 }
320 }
321
Victor Stinnera4ac6002012-01-21 15:50:49 +0100322 assert (format->align <= 127);
323 assert (format->sign <= 127);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200324 return 1;
325}
326
327/* Calculate the padding needed. */
328static void
329calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align,
330 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
331 Py_ssize_t *n_total)
332{
333 if (width >= 0) {
334 if (nchars > width)
335 *n_total = nchars;
336 else
337 *n_total = width;
338 }
339 else {
340 /* not specified, use all of the chars and no more */
341 *n_total = nchars;
342 }
343
344 /* Figure out how much leading space we need, based on the
345 aligning */
346 if (align == '>')
347 *n_lpadding = *n_total - nchars;
348 else if (align == '^')
349 *n_lpadding = (*n_total - nchars) / 2;
350 else if (align == '<' || align == '=')
351 *n_lpadding = 0;
352 else {
353 /* We should never have an unspecified alignment. */
Barry Warsawb2e57942017-09-14 18:13:16 -0700354 Py_UNREACHABLE();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200355 }
356
357 *n_rpadding = *n_total - nchars - *n_lpadding;
358}
359
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200360/* Do the padding, and return a pointer to where the caller-supplied
361 content goes. */
Victor Stinner9ce59bb2013-05-17 00:04:56 +0200362static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200363fill_padding(_PyUnicodeWriter *writer,
364 Py_ssize_t nchars,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200365 Py_UCS4 fill_char, Py_ssize_t n_lpadding,
366 Py_ssize_t n_rpadding)
367{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200368 Py_ssize_t pos;
369
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200370 /* Pad on left. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200371 if (n_lpadding) {
372 pos = writer->pos;
373 _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char);
374 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200375
376 /* Pad on right. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200377 if (n_rpadding) {
378 pos = writer->pos + nchars + n_lpadding;
379 _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char);
380 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200381
382 /* Pointer to the user content. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200383 writer->pos += n_lpadding;
384 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200385}
386
387/************************************************************************/
388/*********** common routines for numeric formatting *********************/
389/************************************************************************/
390
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200391/* Locale info needed for formatting integers and the part of floats
392 before and including the decimal. Note that locales only support
393 8-bit chars, not unicode. */
394typedef struct {
Victor Stinner41a863c2012-02-24 00:37:51 +0100395 PyObject *decimal_point;
396 PyObject *thousands_sep;
397 const char *grouping;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200398} LocaleInfo;
399
Victor Stinner41a863c2012-02-24 00:37:51 +0100400#define STATIC_LOCALE_INFO_INIT {0, 0, 0}
401
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200402/* describes the layout for an integer, see the comment in
403 calc_number_widths() for details */
404typedef struct {
405 Py_ssize_t n_lpadding;
406 Py_ssize_t n_prefix;
407 Py_ssize_t n_spadding;
408 Py_ssize_t n_rpadding;
409 char sign;
410 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
411 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
412 any grouping chars. */
413 Py_ssize_t n_decimal; /* 0 if only an integer */
414 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
415 excluding the decimal itself, if
416 present. */
417
418 /* These 2 are not the widths of fields, but are needed by
419 STRINGLIB_GROUPING. */
420 Py_ssize_t n_digits; /* The number of digits before a decimal
421 or exponent. */
422 Py_ssize_t n_min_width; /* The min_width we used when we computed
423 the n_grouped_digits width. */
424} NumberFieldWidths;
425
426
427/* Given a number of the form:
428 digits[remainder]
429 where ptr points to the start and end points to the end, find where
430 the integer part ends. This could be a decimal, an exponent, both,
431 or neither.
432 If a decimal point is present, set *has_decimal and increment
433 remainder beyond it.
434 Results are undefined (but shouldn't crash) for improperly
435 formatted strings.
436*/
437static void
438parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end,
439 Py_ssize_t *n_remainder, int *has_decimal)
440{
441 Py_ssize_t remainder;
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300442 int kind = PyUnicode_KIND(s);
443 void *data = PyUnicode_DATA(s);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200444
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300445 while (pos<end && Py_ISDIGIT(PyUnicode_READ(kind, data, pos)))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200446 ++pos;
447 remainder = pos;
448
449 /* Does remainder start with a decimal point? */
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300450 *has_decimal = pos<end && PyUnicode_READ(kind, data, remainder) == '.';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200451
452 /* Skip the decimal point. */
453 if (*has_decimal)
454 remainder++;
455
456 *n_remainder = end - remainder;
457}
458
459/* not all fields of format are used. for example, precision is
460 unused. should this take discrete params in order to be more clear
461 about what it does? or is passing a single format parameter easier
462 and more efficient enough to justify a little obfuscation? */
463static Py_ssize_t
464calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
465 Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start,
466 Py_ssize_t n_end, Py_ssize_t n_remainder,
467 int has_decimal, const LocaleInfo *locale,
Victor Stinner41a863c2012-02-24 00:37:51 +0100468 const InternalFormatSpec *format, Py_UCS4 *maxchar)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200469{
470 Py_ssize_t n_non_digit_non_padding;
471 Py_ssize_t n_padding;
472
473 spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0);
474 spec->n_lpadding = 0;
475 spec->n_prefix = n_prefix;
Victor Stinner41a863c2012-02-24 00:37:51 +0100476 spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200477 spec->n_remainder = n_remainder;
478 spec->n_spadding = 0;
479 spec->n_rpadding = 0;
480 spec->sign = '\0';
481 spec->n_sign = 0;
482
483 /* the output will look like:
484 | |
485 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
486 | |
487
488 sign is computed from format->sign and the actual
489 sign of the number
490
491 prefix is given (it's for the '0x' prefix)
492
493 digits is already known
494
495 the total width is either given, or computed from the
496 actual digits
497
498 only one of lpadding, spadding, and rpadding can be non-zero,
499 and it's calculated from the width and other fields
500 */
501
502 /* compute the various parts we're going to write */
503 switch (format->sign) {
504 case '+':
505 /* always put a + or - */
506 spec->n_sign = 1;
507 spec->sign = (sign_char == '-' ? '-' : '+');
508 break;
509 case ' ':
510 spec->n_sign = 1;
511 spec->sign = (sign_char == '-' ? '-' : ' ');
512 break;
513 default:
514 /* Not specified, or the default (-) */
515 if (sign_char == '-') {
516 spec->n_sign = 1;
517 spec->sign = '-';
518 }
519 }
520
521 /* The number of chars used for non-digits and non-padding. */
522 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
523 spec->n_remainder;
524
525 /* min_width can go negative, that's okay. format->width == -1 means
526 we don't care. */
527 if (format->fill_char == '0' && format->align == '=')
528 spec->n_min_width = format->width - n_non_digit_non_padding;
529 else
530 spec->n_min_width = 0;
531
532 if (spec->n_digits == 0)
533 /* This case only occurs when using 'c' formatting, we need
534 to special case it because the grouping code always wants
535 to have at least one character. */
536 spec->n_grouped_digits = 0;
Victor Stinner41a863c2012-02-24 00:37:51 +0100537 else {
538 Py_UCS4 grouping_maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200539 spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
Victor Stinner41a863c2012-02-24 00:37:51 +0100540 NULL, 0,
541 0, NULL,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200542 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100543 locale->grouping, locale->thousands_sep, &grouping_maxchar);
544 *maxchar = Py_MAX(*maxchar, grouping_maxchar);
545 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200546
547 /* Given the desired width and the total of digit and non-digit
548 space we consume, see if we need any padding. format->width can
549 be negative (meaning no padding), but this code still works in
550 that case. */
551 n_padding = format->width -
552 (n_non_digit_non_padding + spec->n_grouped_digits);
553 if (n_padding > 0) {
554 /* Some padding is needed. Determine if it's left, space, or right. */
555 switch (format->align) {
556 case '<':
557 spec->n_rpadding = n_padding;
558 break;
559 case '^':
560 spec->n_lpadding = n_padding / 2;
561 spec->n_rpadding = n_padding - spec->n_lpadding;
562 break;
563 case '=':
564 spec->n_spadding = n_padding;
565 break;
566 case '>':
567 spec->n_lpadding = n_padding;
568 break;
569 default:
570 /* Shouldn't get here, but treat it as '>' */
Barry Warsawb2e57942017-09-14 18:13:16 -0700571 Py_UNREACHABLE();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200572 }
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
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700703get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200704{
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);
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700716 if (locale_info->thousands_sep == NULL)
Victor Stinner41a863c2012-02-24 00:37:51 +0100717 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200718 locale_info->grouping = locale_data->grouping;
719 break;
720 }
721 case LT_DEFAULT_LOCALE:
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400722 case LT_UNDERSCORE_LOCALE:
723 case LT_UNDER_FOUR_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100724 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400725 locale_info->thousands_sep = PyUnicode_FromOrdinal(
726 type == LT_DEFAULT_LOCALE ? ',' : '_');
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700727 if (!locale_info->decimal_point || !locale_info->thousands_sep)
Victor Stinner41a863c2012-02-24 00:37:51 +0100728 return -1;
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400729 if (type != LT_UNDER_FOUR_LOCALE)
730 locale_info->grouping = "\3"; /* Group every 3 characters. The
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200731 (implicit) trailing 0 means repeat
732 infinitely. */
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400733 else
734 locale_info->grouping = "\4"; /* Bin/oct/hex group every four. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200735 break;
736 case LT_NO_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100737 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
738 locale_info->thousands_sep = PyUnicode_New(0, 0);
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700739 if (!locale_info->decimal_point || !locale_info->thousands_sep)
Victor Stinner41a863c2012-02-24 00:37:51 +0100740 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200741 locale_info->grouping = no_grouping;
742 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200743 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100744 return 0;
745}
746
747static void
748free_locale_info(LocaleInfo *locale_info)
749{
750 Py_XDECREF(locale_info->decimal_point);
751 Py_XDECREF(locale_info->thousands_sep);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200752}
753
754/************************************************************************/
755/*********** string formatting ******************************************/
756/************************************************************************/
757
Victor Stinnerd3f08822012-05-29 12:57:52 +0200758static int
759format_string_internal(PyObject *value, const InternalFormatSpec *format,
760 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200761{
762 Py_ssize_t lpad;
763 Py_ssize_t rpad;
764 Py_ssize_t total;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200765 Py_ssize_t len;
766 int result = -1;
Victor Stinnerece58de2012-04-23 23:36:38 +0200767 Py_UCS4 maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200768
Victor Stinnerd3f08822012-05-29 12:57:52 +0200769 assert(PyUnicode_IS_READY(value));
770 len = PyUnicode_GET_LENGTH(value);
771
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200772 /* sign is not allowed on strings */
773 if (format->sign != '\0') {
774 PyErr_SetString(PyExc_ValueError,
775 "Sign not allowed in string format specifier");
776 goto done;
777 }
778
779 /* alternate is not allowed on strings */
780 if (format->alternate) {
781 PyErr_SetString(PyExc_ValueError,
782 "Alternate form (#) not allowed in string format "
783 "specifier");
784 goto done;
785 }
786
787 /* '=' alignment not allowed on strings */
788 if (format->align == '=') {
789 PyErr_SetString(PyExc_ValueError,
790 "'=' alignment not allowed "
791 "in string format specifier");
792 goto done;
793 }
794
Victor Stinner621ef3d2012-10-02 00:33:47 +0200795 if ((format->width == -1 || format->width <= len)
796 && (format->precision == -1 || format->precision >= len)) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200797 /* Fast path */
798 return _PyUnicodeWriter_WriteStr(writer, value);
799 }
800
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200801 /* if precision is specified, output no more that format.precision
802 characters */
803 if (format->precision >= 0 && len >= format->precision) {
804 len = format->precision;
805 }
806
807 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
808
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200809 maxchar = writer->maxchar;
Victor Stinnera4ac6002012-01-21 15:50:49 +0100810 if (lpad != 0 || rpad != 0)
811 maxchar = Py_MAX(maxchar, format->fill_char);
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200812 if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
813 Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
814 maxchar = Py_MAX(maxchar, valmaxchar);
815 }
Victor Stinnera4ac6002012-01-21 15:50:49 +0100816
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200817 /* allocate the resulting string */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200818 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200819 goto done;
820
821 /* Write into that space. First the padding. */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400822 result = fill_padding(writer, len, format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200823 if (result == -1)
824 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200825
826 /* Then the source string. */
Victor Stinnerc9d369f2012-06-16 02:22:37 +0200827 if (len) {
828 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
829 value, 0, len);
830 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200831 writer->pos += (len + rpad);
832 result = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200833
834done:
835 return result;
836}
837
838
839/************************************************************************/
840/*********** long formatting ********************************************/
841/************************************************************************/
842
Victor Stinnerd3f08822012-05-29 12:57:52 +0200843static int
844format_long_internal(PyObject *value, const InternalFormatSpec *format,
845 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200846{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200847 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +0100848 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200849 PyObject *tmp = NULL;
850 Py_ssize_t inumeric_chars;
851 Py_UCS4 sign_char = '\0';
852 Py_ssize_t n_digits; /* count of digits need from the computed
853 string */
854 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
855 produces non-digits */
856 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
857 Py_ssize_t n_total;
Victor Stinnered277852012-02-01 00:22:23 +0100858 Py_ssize_t prefix = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200859 NumberFieldWidths spec;
860 long x;
861
862 /* Locale settings, either from the actual locale or
863 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +0100864 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200865
866 /* no precision allowed on integers */
867 if (format->precision != -1) {
868 PyErr_SetString(PyExc_ValueError,
869 "Precision not allowed in integer format specifier");
870 goto done;
871 }
872
873 /* special case for character formatting */
874 if (format->type == 'c') {
875 /* error to specify a sign */
876 if (format->sign != '\0') {
877 PyErr_SetString(PyExc_ValueError,
878 "Sign not allowed with integer"
879 " format specifier 'c'");
880 goto done;
881 }
Eric V. Smitha12572f2014-04-15 22:37:55 -0400882 /* error to request alternate format */
883 if (format->alternate) {
884 PyErr_SetString(PyExc_ValueError,
885 "Alternate form (#) not allowed with integer"
886 " format specifier 'c'");
887 goto done;
888 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200889
890 /* taken from unicodeobject.c formatchar() */
891 /* Integer input truncated to a character */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200892 x = PyLong_AsLong(value);
893 if (x == -1 && PyErr_Occurred())
894 goto done;
895 if (x < 0 || x > 0x10ffff) {
896 PyErr_SetString(PyExc_OverflowError,
Victor Stinnera4ac6002012-01-21 15:50:49 +0100897 "%c arg not in range(0x110000)");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200898 goto done;
899 }
900 tmp = PyUnicode_FromOrdinal(x);
901 inumeric_chars = 0;
902 n_digits = 1;
Amaury Forgeot d'Arc6d766fc2012-01-23 23:20:43 +0100903 maxchar = Py_MAX(maxchar, (Py_UCS4)x);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200904
905 /* As a sort-of hack, we tell calc_number_widths that we only
906 have "remainder" characters. calc_number_widths thinks
907 these are characters that don't get formatted, only copied
908 into the output string. We do this for 'c' formatting,
909 because the characters are likely to be non-digits. */
910 n_remainder = 1;
911 }
912 else {
913 int base;
914 int leading_chars_to_skip = 0; /* Number of characters added by
915 PyNumber_ToBase that we want to
916 skip over. */
917
918 /* Compute the base and how many characters will be added by
919 PyNumber_ToBase */
920 switch (format->type) {
921 case 'b':
922 base = 2;
923 leading_chars_to_skip = 2; /* 0b */
924 break;
925 case 'o':
926 base = 8;
927 leading_chars_to_skip = 2; /* 0o */
928 break;
929 case 'x':
930 case 'X':
931 base = 16;
932 leading_chars_to_skip = 2; /* 0x */
933 break;
934 default: /* shouldn't be needed, but stops a compiler warning */
935 case 'd':
936 case 'n':
937 base = 10;
938 break;
939 }
940
Victor Stinnerd3f08822012-05-29 12:57:52 +0200941 if (format->sign != '+' && format->sign != ' '
942 && format->width == -1
943 && format->type != 'X' && format->type != 'n'
944 && !format->thousands_separators
945 && PyLong_CheckExact(value))
946 {
947 /* Fast path */
948 return _PyLong_FormatWriter(writer, value, base, format->alternate);
949 }
950
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 /* The number of prefix chars is the same as the leading
952 chars to skip */
953 if (format->alternate)
954 n_prefix = leading_chars_to_skip;
955
956 /* Do the hard part, converting to a string in a given base */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200957 tmp = _PyLong_Format(value, base);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200958 if (tmp == NULL || PyUnicode_READY(tmp) == -1)
959 goto done;
960
961 inumeric_chars = 0;
962 n_digits = PyUnicode_GET_LENGTH(tmp);
963
964 prefix = inumeric_chars;
965
966 /* Is a sign character present in the output? If so, remember it
967 and skip it */
968 if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
969 sign_char = '-';
970 ++prefix;
971 ++leading_chars_to_skip;
972 }
973
974 /* Skip over the leading chars (0x, 0b, etc.) */
975 n_digits -= leading_chars_to_skip;
976 inumeric_chars += leading_chars_to_skip;
977 }
978
979 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100980 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400981 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +0100982 &locale) == -1)
983 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200984
985 /* Calculate how much memory we'll need. */
986 n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars,
Victor Stinner41a863c2012-02-24 00:37:51 +0100987 inumeric_chars + n_digits, n_remainder, 0,
988 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100989
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200990 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200991 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200992 goto done;
993
994 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200995 result = fill_number(writer, &spec,
996 tmp, inumeric_chars, inumeric_chars + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -0400997 tmp, prefix, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +0200998 &locale, format->type == 'X');
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200999
1000done:
1001 Py_XDECREF(tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001002 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001003 return result;
1004}
1005
1006/************************************************************************/
1007/*********** float formatting *******************************************/
1008/************************************************************************/
1009
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001010/* much of this is taken from unicodeobject.c */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001011static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001012format_float_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001013 const InternalFormatSpec *format,
1014 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001015{
1016 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
1017 Py_ssize_t n_digits;
1018 Py_ssize_t n_remainder;
1019 Py_ssize_t n_total;
1020 int has_decimal;
1021 double val;
Victor Stinner76d38502013-06-24 23:34:15 +02001022 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001023 Py_UCS4 type = format->type;
1024 int add_pct = 0;
1025 Py_ssize_t index;
1026 NumberFieldWidths spec;
1027 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001028 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001029 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001030 Py_UCS4 sign_char = '\0';
1031 int float_type; /* Used to see if we have a nan, inf, or regular float. */
1032 PyObject *unicode_tmp = NULL;
1033
1034 /* Locale settings, either from the actual locale or
1035 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001036 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001037
Victor Stinner2f084ec2013-06-23 14:54:30 +02001038 if (format->precision > INT_MAX) {
1039 PyErr_SetString(PyExc_ValueError, "precision too big");
1040 goto done;
1041 }
1042 precision = (int)format->precision;
1043
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001044 if (format->alternate)
1045 flags |= Py_DTSF_ALT;
1046
1047 if (type == '\0') {
1048 /* Omitted type specifier. Behaves in the same way as repr(x)
1049 and str(x) if no precision is given, else like 'g', but with
1050 at least one digit after the decimal point. */
1051 flags |= Py_DTSF_ADD_DOT_0;
1052 type = 'r';
1053 default_precision = 0;
1054 }
1055
1056 if (type == 'n')
1057 /* 'n' is the same as 'g', except for the locale used to
1058 format the result. We take care of that later. */
1059 type = 'g';
1060
1061 val = PyFloat_AsDouble(value);
1062 if (val == -1.0 && PyErr_Occurred())
1063 goto done;
1064
1065 if (type == '%') {
1066 type = 'f';
1067 val *= 100;
1068 add_pct = 1;
1069 }
1070
1071 if (precision < 0)
1072 precision = default_precision;
1073 else if (type == 'r')
1074 type = 'g';
1075
Martin Panter4c359642016-05-08 13:53:41 +00001076 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001077 8-bit char. This is safe, because we've restricted what "type"
1078 can be. */
1079 buf = PyOS_double_to_string(val, (char)type, precision, flags,
1080 &float_type);
1081 if (buf == NULL)
1082 goto done;
1083 n_digits = strlen(buf);
1084
1085 if (add_pct) {
1086 /* We know that buf has a trailing zero (since we just called
1087 strlen() on it), and we don't use that fact any more. So we
1088 can just write over the trailing zero. */
1089 buf[n_digits] = '%';
1090 n_digits += 1;
1091 }
1092
Victor Stinnerd3f08822012-05-29 12:57:52 +02001093 if (format->sign != '+' && format->sign != ' '
1094 && format->width == -1
1095 && format->type != 'n'
1096 && !format->thousands_separators)
1097 {
1098 /* Fast path */
Victor Stinner4a587072013-11-19 12:54:53 +01001099 result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits);
1100 PyMem_Free(buf);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001101 return result;
1102 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001103
Victor Stinner4a587072013-11-19 12:54:53 +01001104 /* Since there is no unicode version of PyOS_double_to_string,
1105 just use the 8 bit version and then convert to unicode. */
1106 unicode_tmp = _PyUnicode_FromASCII(buf, n_digits);
1107 PyMem_Free(buf);
1108 if (unicode_tmp == NULL)
1109 goto done;
1110
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001111 /* Is a sign character present in the output? If so, remember it
1112 and skip it */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001113 index = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001114 if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') {
1115 sign_char = '-';
1116 ++index;
1117 --n_digits;
1118 }
1119
1120 /* Determine if we have any "remainder" (after the digits, might include
1121 decimal or exponent or both (or neither)) */
1122 parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal);
1123
1124 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001125 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -04001126 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +01001127 &locale) == -1)
1128 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001129
1130 /* Calculate how much memory we'll need. */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001131 n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001132 index + n_digits, n_remainder, has_decimal,
Victor Stinner41a863c2012-02-24 00:37:51 +01001133 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +01001134
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001135 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001136 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001137 goto done;
1138
1139 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001140 result = fill_number(writer, &spec,
1141 unicode_tmp, index, index + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001142 NULL, 0, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001143 &locale, 0);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001144
1145done:
Stefan Krahd9c1bf72012-09-06 13:02:46 +02001146 Py_XDECREF(unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001147 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001148 return result;
1149}
1150
1151/************************************************************************/
1152/*********** complex formatting *****************************************/
1153/************************************************************************/
1154
Victor Stinnerd3f08822012-05-29 12:57:52 +02001155static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001156format_complex_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001157 const InternalFormatSpec *format,
1158 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001159{
1160 double re;
1161 double im;
1162 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1163 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1164
1165 InternalFormatSpec tmp_format = *format;
1166 Py_ssize_t n_re_digits;
1167 Py_ssize_t n_im_digits;
1168 Py_ssize_t n_re_remainder;
1169 Py_ssize_t n_im_remainder;
1170 Py_ssize_t n_re_total;
1171 Py_ssize_t n_im_total;
1172 int re_has_decimal;
1173 int im_has_decimal;
Victor Stinner76d38502013-06-24 23:34:15 +02001174 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001175 Py_UCS4 type = format->type;
1176 Py_ssize_t i_re;
1177 Py_ssize_t i_im;
1178 NumberFieldWidths re_spec;
1179 NumberFieldWidths im_spec;
1180 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001181 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001182 Py_UCS4 maxchar = 127;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001183 enum PyUnicode_Kind rkind;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001184 void *rdata;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001185 Py_UCS4 re_sign_char = '\0';
1186 Py_UCS4 im_sign_char = '\0';
1187 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1188 int im_float_type;
1189 int add_parens = 0;
1190 int skip_re = 0;
1191 Py_ssize_t lpad;
1192 Py_ssize_t rpad;
1193 Py_ssize_t total;
1194 PyObject *re_unicode_tmp = NULL;
1195 PyObject *im_unicode_tmp = NULL;
1196
1197 /* Locale settings, either from the actual locale or
1198 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001199 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001200
Victor Stinner2f084ec2013-06-23 14:54:30 +02001201 if (format->precision > INT_MAX) {
1202 PyErr_SetString(PyExc_ValueError, "precision too big");
1203 goto done;
1204 }
1205 precision = (int)format->precision;
1206
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001207 /* Zero padding is not allowed. */
1208 if (format->fill_char == '0') {
1209 PyErr_SetString(PyExc_ValueError,
1210 "Zero padding is not allowed in complex format "
1211 "specifier");
1212 goto done;
1213 }
1214
1215 /* Neither is '=' alignment . */
1216 if (format->align == '=') {
1217 PyErr_SetString(PyExc_ValueError,
1218 "'=' alignment flag is not allowed in complex format "
1219 "specifier");
1220 goto done;
1221 }
1222
1223 re = PyComplex_RealAsDouble(value);
1224 if (re == -1.0 && PyErr_Occurred())
1225 goto done;
1226 im = PyComplex_ImagAsDouble(value);
1227 if (im == -1.0 && PyErr_Occurred())
1228 goto done;
1229
1230 if (format->alternate)
1231 flags |= Py_DTSF_ALT;
1232
1233 if (type == '\0') {
1234 /* Omitted type specifier. Should be like str(self). */
1235 type = 'r';
1236 default_precision = 0;
1237 if (re == 0.0 && copysign(1.0, re) == 1.0)
1238 skip_re = 1;
1239 else
1240 add_parens = 1;
1241 }
1242
1243 if (type == 'n')
1244 /* 'n' is the same as 'g', except for the locale used to
1245 format the result. We take care of that later. */
1246 type = 'g';
1247
1248 if (precision < 0)
1249 precision = default_precision;
1250 else if (type == 'r')
1251 type = 'g';
1252
Martin Panter4c359642016-05-08 13:53:41 +00001253 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001254 8-bit char. This is safe, because we've restricted what "type"
1255 can be. */
1256 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1257 &re_float_type);
1258 if (re_buf == NULL)
1259 goto done;
1260 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1261 &im_float_type);
1262 if (im_buf == NULL)
1263 goto done;
1264
1265 n_re_digits = strlen(re_buf);
1266 n_im_digits = strlen(im_buf);
1267
1268 /* Since there is no unicode version of PyOS_double_to_string,
1269 just use the 8 bit version and then convert to unicode. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001270 re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001271 if (re_unicode_tmp == NULL)
1272 goto done;
1273 i_re = 0;
1274
Victor Stinnerd3f08822012-05-29 12:57:52 +02001275 im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001276 if (im_unicode_tmp == NULL)
1277 goto done;
1278 i_im = 0;
1279
1280 /* Is a sign character present in the output? If so, remember it
1281 and skip it */
1282 if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') {
1283 re_sign_char = '-';
1284 ++i_re;
1285 --n_re_digits;
1286 }
1287 if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') {
1288 im_sign_char = '-';
1289 ++i_im;
1290 --n_im_digits;
1291 }
1292
1293 /* Determine if we have any "remainder" (after the digits, might include
1294 decimal or exponent or both (or neither)) */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001295 parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001296 &n_re_remainder, &re_has_decimal);
Victor Stinnerafbaa202011-09-28 21:50:16 +02001297 parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001298 &n_im_remainder, &im_has_decimal);
1299
1300 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001301 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -04001302 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +01001303 &locale) == -1)
1304 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001305
1306 /* Turn off any padding. We'll do it later after we've composed
1307 the numbers without padding. */
1308 tmp_format.fill_char = '\0';
1309 tmp_format.align = '<';
1310 tmp_format.width = -1;
1311
1312 /* Calculate how much memory we'll need. */
1313 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp,
1314 i_re, i_re + n_re_digits, n_re_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001315 re_has_decimal, &locale, &tmp_format,
1316 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001317
1318 /* Same formatting, but always include a sign, unless the real part is
1319 * going to be omitted, in which case we use whatever sign convention was
1320 * requested by the original format. */
1321 if (!skip_re)
1322 tmp_format.sign = '+';
1323 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp,
1324 i_im, i_im + n_im_digits, n_im_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001325 im_has_decimal, &locale, &tmp_format,
1326 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001327
1328 if (skip_re)
1329 n_re_total = 0;
1330
1331 /* Add 1 for the 'j', and optionally 2 for parens. */
1332 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1333 format->width, format->align, &lpad, &rpad, &total);
1334
Victor Stinner41a863c2012-02-24 00:37:51 +01001335 if (lpad || rpad)
Victor Stinnera4ac6002012-01-21 15:50:49 +01001336 maxchar = Py_MAX(maxchar, format->fill_char);
1337
Victor Stinnerd3f08822012-05-29 12:57:52 +02001338 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001339 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001340 rkind = writer->kind;
1341 rdata = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001342
1343 /* Populate the memory. First, the padding. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001344 result = fill_padding(writer,
1345 n_re_total + n_im_total + 1 + add_parens * 2,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001346 format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001347 if (result == -1)
1348 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001349
Victor Stinnerd3f08822012-05-29 12:57:52 +02001350 if (add_parens) {
1351 PyUnicode_WRITE(rkind, rdata, writer->pos, '(');
1352 writer->pos++;
1353 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001354
1355 if (!skip_re) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001356 result = fill_number(writer, &re_spec,
1357 re_unicode_tmp, i_re, i_re + n_re_digits,
1358 NULL, 0,
1359 0,
1360 &locale, 0);
1361 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001362 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001363 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001364 result = fill_number(writer, &im_spec,
1365 im_unicode_tmp, i_im, i_im + n_im_digits,
1366 NULL, 0,
1367 0,
1368 &locale, 0);
1369 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001370 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001371 PyUnicode_WRITE(rkind, rdata, writer->pos, 'j');
1372 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001373
Victor Stinnerd3f08822012-05-29 12:57:52 +02001374 if (add_parens) {
1375 PyUnicode_WRITE(rkind, rdata, writer->pos, ')');
1376 writer->pos++;
1377 }
1378
1379 writer->pos += rpad;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001380
1381done:
1382 PyMem_Free(re_buf);
1383 PyMem_Free(im_buf);
1384 Py_XDECREF(re_unicode_tmp);
1385 Py_XDECREF(im_unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001386 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001387 return result;
1388}
1389
1390/************************************************************************/
1391/*********** built in formatters ****************************************/
1392/************************************************************************/
doko@ubuntu.com39378f72012-06-21 12:12:20 +02001393static int
Victor Stinnerd3f08822012-05-29 12:57:52 +02001394format_obj(PyObject *obj, _PyUnicodeWriter *writer)
1395{
1396 PyObject *str;
1397 int err;
1398
1399 str = PyObject_Str(obj);
1400 if (str == NULL)
1401 return -1;
1402 err = _PyUnicodeWriter_WriteStr(writer, str);
1403 Py_DECREF(str);
1404 return err;
1405}
1406
1407int
1408_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1409 PyObject *obj,
1410 PyObject *format_spec,
1411 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001412{
1413 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001414
1415 assert(PyUnicode_Check(obj));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001416
1417 /* check for the special case of zero length format spec, make
1418 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001419 if (start == end) {
1420 if (PyUnicode_CheckExact(obj))
1421 return _PyUnicodeWriter_WriteStr(writer, obj);
1422 else
1423 return format_obj(obj, writer);
1424 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001425
1426 /* parse the format_spec */
1427 if (!parse_internal_render_format_spec(format_spec, start, end,
1428 &format, 's', '<'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001429 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001430
1431 /* type conversion? */
1432 switch (format.type) {
1433 case 's':
1434 /* no type conversion needed, already a string. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001435 return format_string_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001436 default:
1437 /* unknown */
1438 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001439 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001440 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001441}
1442
Victor Stinnerd3f08822012-05-29 12:57:52 +02001443int
1444_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1445 PyObject *obj,
1446 PyObject *format_spec,
1447 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001448{
Victor Stinnerd3f08822012-05-29 12:57:52 +02001449 PyObject *tmp = NULL, *str = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001450 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001451 int result = -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001452
1453 /* check for the special case of zero length format spec, make
1454 it equivalent to str(obj) */
1455 if (start == end) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001456 if (PyLong_CheckExact(obj))
1457 return _PyLong_FormatWriter(writer, obj, 10, 0);
1458 else
1459 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001460 }
1461
1462 /* parse the format_spec */
1463 if (!parse_internal_render_format_spec(format_spec, start, end,
1464 &format, 'd', '>'))
1465 goto done;
1466
1467 /* type conversion? */
1468 switch (format.type) {
1469 case 'b':
1470 case 'c':
1471 case 'd':
1472 case 'o':
1473 case 'x':
1474 case 'X':
1475 case 'n':
Serhiy Storchaka95949422013-08-27 19:40:23 +03001476 /* no type conversion needed, already an int. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001477 result = format_long_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001478 break;
1479
1480 case 'e':
1481 case 'E':
1482 case 'f':
1483 case 'F':
1484 case 'g':
1485 case 'G':
1486 case '%':
1487 /* convert to float */
1488 tmp = PyNumber_Float(obj);
1489 if (tmp == NULL)
1490 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001491 result = format_float_internal(tmp, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001492 break;
1493
1494 default:
1495 /* unknown */
1496 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1497 goto done;
1498 }
1499
1500done:
1501 Py_XDECREF(tmp);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001502 Py_XDECREF(str);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001503 return result;
1504}
1505
Victor Stinnerd3f08822012-05-29 12:57:52 +02001506int
1507_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1508 PyObject *obj,
1509 PyObject *format_spec,
1510 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001511{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001512 InternalFormatSpec format;
1513
1514 /* check for the special case of zero length format spec, make
1515 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001516 if (start == end)
1517 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001518
1519 /* parse the format_spec */
1520 if (!parse_internal_render_format_spec(format_spec, start, end,
1521 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001522 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001523
1524 /* type conversion? */
1525 switch (format.type) {
1526 case '\0': /* No format code: like 'g', but with at least one decimal. */
1527 case 'e':
1528 case 'E':
1529 case 'f':
1530 case 'F':
1531 case 'g':
1532 case 'G':
1533 case 'n':
1534 case '%':
1535 /* no conversion, already a float. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001536 return format_float_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001537
1538 default:
1539 /* unknown */
1540 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001541 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001542 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001543}
1544
Victor Stinnerd3f08822012-05-29 12:57:52 +02001545int
1546_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1547 PyObject *obj,
1548 PyObject *format_spec,
1549 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001550{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001551 InternalFormatSpec format;
1552
1553 /* check for the special case of zero length format spec, make
1554 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001555 if (start == end)
1556 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001557
1558 /* parse the format_spec */
1559 if (!parse_internal_render_format_spec(format_spec, start, end,
1560 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001561 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001562
1563 /* type conversion? */
1564 switch (format.type) {
1565 case '\0': /* No format code: like 'g', but with at least one decimal. */
1566 case 'e':
1567 case 'E':
1568 case 'f':
1569 case 'F':
1570 case 'g':
1571 case 'G':
1572 case 'n':
1573 /* no conversion, already a complex. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001574 return format_complex_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001575
1576 default:
1577 /* unknown */
1578 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001579 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001580 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001581}