blob: ba09cc67becfccbbf8a5154bdbfcbc4f0aeab98b [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
Miss Islington (bot)cd4dd932018-10-01 22:12:02 -070031invalid_thousands_separator_type(char specifier, Py_UCS4 presentation_type)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020032{
Miss Islington (bot)cd4dd932018-10-01 22:12:02 -070033 assert(specifier == ',' || specifier == '_');
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020034 if (presentation_type > 32 && presentation_type < 128)
35 PyErr_Format(PyExc_ValueError,
Miss Islington (bot)cd4dd932018-10-01 22:12:02 -070036 "Cannot specify '%c' with '%c'.",
37 specifier, (char)presentation_type);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020038 else
39 PyErr_Format(PyExc_ValueError,
Miss Islington (bot)cd4dd932018-10-01 22:12:02 -070040 "Cannot specify '%c' with '\\x%x'.",
41 specifier, (unsigned int)presentation_type);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020042}
43
Eric V. Smith89e1b1a2016-09-09 23:06:47 -040044static void
Benjamin Petersoneb0dfa92016-09-09 20:14:05 -070045invalid_comma_and_underscore(void)
Eric V. Smith89e1b1a2016-09-09 23:06:47 -040046{
47 PyErr_Format(PyExc_ValueError, "Cannot specify both ',' and '_'.");
48}
49
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020050/*
51 get_integer consumes 0 or more decimal digit characters from an
52 input string, updates *result with the corresponding positive
53 integer, and returns the number of digits consumed.
54
55 returns -1 on error.
56*/
57static int
Serhiy Storchaka1f932612016-08-29 15:57:26 +030058get_integer(PyObject *str, Py_ssize_t *ppos, Py_ssize_t end,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020059 Py_ssize_t *result)
60{
Serhiy Storchaka1f932612016-08-29 15:57:26 +030061 Py_ssize_t accumulator, digitval, pos = *ppos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020062 int numdigits;
Serhiy Storchaka1f932612016-08-29 15:57:26 +030063 int kind = PyUnicode_KIND(str);
64 void *data = PyUnicode_DATA(str);
65
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020066 accumulator = numdigits = 0;
Serhiy Storchaka1f932612016-08-29 15:57:26 +030067 for (; pos < end; pos++, numdigits++) {
68 digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ(kind, data, pos));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020069 if (digitval < 0)
70 break;
71 /*
Mark Dickinson47862d42011-12-01 15:27:04 +000072 Detect possible overflow before it happens:
73
74 accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
75 accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020076 */
Mark Dickinson47862d42011-12-01 15:27:04 +000077 if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020078 PyErr_Format(PyExc_ValueError,
79 "Too many decimal digits in format string");
Serhiy Storchaka1f932612016-08-29 15:57:26 +030080 *ppos = pos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020081 return -1;
82 }
Mark Dickinson47862d42011-12-01 15:27:04 +000083 accumulator = accumulator * 10 + digitval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020084 }
Serhiy Storchaka1f932612016-08-29 15:57:26 +030085 *ppos = pos;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020086 *result = accumulator;
87 return numdigits;
88}
89
90/************************************************************************/
91/*********** standard format specifier parsing **************************/
92/************************************************************************/
93
94/* returns true if this character is a specifier alignment token */
95Py_LOCAL_INLINE(int)
96is_alignment_token(Py_UCS4 c)
97{
98 switch (c) {
99 case '<': case '>': case '=': case '^':
100 return 1;
101 default:
102 return 0;
103 }
104}
105
106/* returns true if this character is a sign element */
107Py_LOCAL_INLINE(int)
108is_sign_element(Py_UCS4 c)
109{
110 switch (c) {
111 case ' ': case '+': case '-':
112 return 1;
113 default:
114 return 0;
115 }
116}
Eric Smith8c663262007-08-25 02:26:07 +0000117
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400118/* Locale type codes. LT_NO_LOCALE must be zero. */
Benjamin Peterson995026a2016-09-13 22:46:15 -0700119enum LocaleType {
120 LT_NO_LOCALE = 0,
Miss Islington (bot)cd4dd932018-10-01 22:12:02 -0700121 LT_DEFAULT_LOCALE = ',',
122 LT_UNDERSCORE_LOCALE = '_',
Benjamin Peterson995026a2016-09-13 22:46:15 -0700123 LT_UNDER_FOUR_LOCALE,
124 LT_CURRENT_LOCALE
125};
Eric Smith4a7d76d2008-05-30 18:10:19 +0000126
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200127typedef struct {
128 Py_UCS4 fill_char;
129 Py_UCS4 align;
130 int alternate;
131 Py_UCS4 sign;
132 Py_ssize_t width;
Benjamin Peterson995026a2016-09-13 22:46:15 -0700133 enum LocaleType thousands_separators;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200134 Py_ssize_t precision;
135 Py_UCS4 type;
136} InternalFormatSpec;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000137
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200138#if 0
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700139/* Occasionally useful for debugging. Should normally be commented out. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200140static void
141DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
142{
143 printf("internal format spec: fill_char %d\n", format->fill_char);
144 printf("internal format spec: align %d\n", format->align);
145 printf("internal format spec: alternate %d\n", format->alternate);
146 printf("internal format spec: sign %d\n", format->sign);
147 printf("internal format spec: width %zd\n", format->width);
148 printf("internal format spec: thousands_separators %d\n",
149 format->thousands_separators);
150 printf("internal format spec: precision %zd\n", format->precision);
151 printf("internal format spec: type %c\n", format->type);
152 printf("\n");
153}
154#endif
155
156
157/*
158 ptr points to the start of the format_spec, end points just past its end.
159 fills in format with the parsed information.
160 returns 1 on success, 0 on failure.
161 if failure, sets the exception
162*/
163static int
164parse_internal_render_format_spec(PyObject *format_spec,
165 Py_ssize_t start, Py_ssize_t end,
166 InternalFormatSpec *format,
167 char default_type,
168 char default_align)
169{
170 Py_ssize_t pos = start;
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300171 int kind = PyUnicode_KIND(format_spec);
172 void *data = PyUnicode_DATA(format_spec);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200173 /* end-pos is used throughout this code to specify the length of
174 the input string */
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300175#define READ_spec(index) PyUnicode_READ(kind, data, index)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200176
177 Py_ssize_t consumed;
178 int align_specified = 0;
Eric V. Smith2ea97122014-04-14 11:55:10 -0400179 int fill_char_specified = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200180
Eric V. Smith2ea97122014-04-14 11:55:10 -0400181 format->fill_char = ' ';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200182 format->align = default_align;
183 format->alternate = 0;
184 format->sign = '\0';
185 format->width = -1;
Benjamin Peterson995026a2016-09-13 22:46:15 -0700186 format->thousands_separators = LT_NO_LOCALE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200187 format->precision = -1;
188 format->type = default_type;
189
190 /* If the second char is an alignment token,
191 then parse the fill char */
192 if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) {
193 format->align = READ_spec(pos+1);
194 format->fill_char = READ_spec(pos);
Eric V. Smith2ea97122014-04-14 11:55:10 -0400195 fill_char_specified = 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200196 align_specified = 1;
197 pos += 2;
198 }
199 else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) {
200 format->align = READ_spec(pos);
201 align_specified = 1;
202 ++pos;
203 }
204
205 /* Parse the various sign options */
206 if (end-pos >= 1 && is_sign_element(READ_spec(pos))) {
207 format->sign = READ_spec(pos);
208 ++pos;
209 }
210
211 /* If the next character is #, we're in alternate mode. This only
212 applies to integers. */
213 if (end-pos >= 1 && READ_spec(pos) == '#') {
214 format->alternate = 1;
215 ++pos;
216 }
217
218 /* The special case for 0-padding (backwards compat) */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400219 if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200220 format->fill_char = '0';
221 if (!align_specified) {
222 format->align = '=';
223 }
224 ++pos;
225 }
226
227 consumed = get_integer(format_spec, &pos, end, &format->width);
228 if (consumed == -1)
229 /* Overflow error. Exception already set. */
230 return 0;
231
232 /* If consumed is 0, we didn't consume any characters for the
233 width. In that case, reset the width to -1, because
234 get_integer() will have set it to zero. -1 is how we record
235 that the width wasn't specified. */
236 if (consumed == 0)
237 format->width = -1;
238
239 /* Comma signifies add thousands separators */
240 if (end-pos && READ_spec(pos) == ',') {
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400241 format->thousands_separators = LT_DEFAULT_LOCALE;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200242 ++pos;
243 }
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400244 /* Underscore signifies add thousands separators */
245 if (end-pos && READ_spec(pos) == '_') {
Benjamin Peterson995026a2016-09-13 22:46:15 -0700246 if (format->thousands_separators != LT_NO_LOCALE) {
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400247 invalid_comma_and_underscore();
248 return 0;
249 }
250 format->thousands_separators = LT_UNDERSCORE_LOCALE;
251 ++pos;
252 }
253 if (end-pos && READ_spec(pos) == ',') {
254 invalid_comma_and_underscore();
255 return 0;
256 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200257
258 /* Parse field precision */
259 if (end-pos && READ_spec(pos) == '.') {
260 ++pos;
261
262 consumed = get_integer(format_spec, &pos, end, &format->precision);
263 if (consumed == -1)
264 /* Overflow error. Exception already set. */
265 return 0;
266
267 /* Not having a precision after a dot is an error. */
268 if (consumed == 0) {
269 PyErr_Format(PyExc_ValueError,
270 "Format specifier missing precision");
271 return 0;
272 }
273
274 }
275
276 /* Finally, parse the type field. */
277
278 if (end-pos > 1) {
Eric V. Smithd25cfe62012-01-19 20:04:28 -0500279 /* More than one char remain, invalid format specifier. */
280 PyErr_Format(PyExc_ValueError, "Invalid format specifier");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200281 return 0;
282 }
283
284 if (end-pos == 1) {
285 format->type = READ_spec(pos);
286 ++pos;
287 }
288
289 /* Do as much validating as we can, just by looking at the format
290 specifier. Do not take into account what type of formatting
291 we're doing (int, float, string). */
292
293 if (format->thousands_separators) {
294 switch (format->type) {
295 case 'd':
296 case 'e':
297 case 'f':
298 case 'g':
299 case 'E':
300 case 'G':
301 case '%':
302 case 'F':
303 case '\0':
304 /* These are allowed. See PEP 378.*/
305 break;
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400306 case 'b':
307 case 'o':
308 case 'x':
309 case 'X':
310 /* Underscores are allowed in bin/oct/hex. See PEP 515. */
311 if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
312 /* Every four digits, not every three, in bin/oct/hex. */
313 format->thousands_separators = LT_UNDER_FOUR_LOCALE;
314 break;
315 }
Stefan Krahf432a322017-08-21 13:09:59 +0200316 /* fall through */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200317 default:
Miss Islington (bot)cd4dd932018-10-01 22:12:02 -0700318 invalid_thousands_separator_type(format->thousands_separators, format->type);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200319 return 0;
320 }
321 }
322
Victor Stinnera4ac6002012-01-21 15:50:49 +0100323 assert (format->align <= 127);
324 assert (format->sign <= 127);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200325 return 1;
326}
327
328/* Calculate the padding needed. */
329static void
330calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align,
331 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
332 Py_ssize_t *n_total)
333{
334 if (width >= 0) {
335 if (nchars > width)
336 *n_total = nchars;
337 else
338 *n_total = width;
339 }
340 else {
341 /* not specified, use all of the chars and no more */
342 *n_total = nchars;
343 }
344
345 /* Figure out how much leading space we need, based on the
346 aligning */
347 if (align == '>')
348 *n_lpadding = *n_total - nchars;
349 else if (align == '^')
350 *n_lpadding = (*n_total - nchars) / 2;
351 else if (align == '<' || align == '=')
352 *n_lpadding = 0;
353 else {
354 /* We should never have an unspecified alignment. */
Barry Warsawb2e57942017-09-14 18:13:16 -0700355 Py_UNREACHABLE();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200356 }
357
358 *n_rpadding = *n_total - nchars - *n_lpadding;
359}
360
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200361/* Do the padding, and return a pointer to where the caller-supplied
362 content goes. */
Victor Stinner9ce59bb2013-05-17 00:04:56 +0200363static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200364fill_padding(_PyUnicodeWriter *writer,
365 Py_ssize_t nchars,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200366 Py_UCS4 fill_char, Py_ssize_t n_lpadding,
367 Py_ssize_t n_rpadding)
368{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200369 Py_ssize_t pos;
370
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200371 /* Pad on left. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200372 if (n_lpadding) {
373 pos = writer->pos;
374 _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char);
375 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200376
377 /* Pad on right. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200378 if (n_rpadding) {
379 pos = writer->pos + nchars + n_lpadding;
380 _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char);
381 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200382
383 /* Pointer to the user content. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200384 writer->pos += n_lpadding;
385 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200386}
387
388/************************************************************************/
389/*********** common routines for numeric formatting *********************/
390/************************************************************************/
391
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200392/* Locale info needed for formatting integers and the part of floats
393 before and including the decimal. Note that locales only support
394 8-bit chars, not unicode. */
395typedef struct {
Victor Stinner41a863c2012-02-24 00:37:51 +0100396 PyObject *decimal_point;
397 PyObject *thousands_sep;
398 const char *grouping;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200399} LocaleInfo;
400
Victor Stinner41a863c2012-02-24 00:37:51 +0100401#define STATIC_LOCALE_INFO_INIT {0, 0, 0}
402
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200403/* describes the layout for an integer, see the comment in
404 calc_number_widths() for details */
405typedef struct {
406 Py_ssize_t n_lpadding;
407 Py_ssize_t n_prefix;
408 Py_ssize_t n_spadding;
409 Py_ssize_t n_rpadding;
410 char sign;
411 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
412 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
413 any grouping chars. */
414 Py_ssize_t n_decimal; /* 0 if only an integer */
415 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
416 excluding the decimal itself, if
417 present. */
418
419 /* These 2 are not the widths of fields, but are needed by
420 STRINGLIB_GROUPING. */
421 Py_ssize_t n_digits; /* The number of digits before a decimal
422 or exponent. */
423 Py_ssize_t n_min_width; /* The min_width we used when we computed
424 the n_grouped_digits width. */
425} NumberFieldWidths;
426
427
428/* Given a number of the form:
429 digits[remainder]
430 where ptr points to the start and end points to the end, find where
431 the integer part ends. This could be a decimal, an exponent, both,
432 or neither.
433 If a decimal point is present, set *has_decimal and increment
434 remainder beyond it.
435 Results are undefined (but shouldn't crash) for improperly
436 formatted strings.
437*/
438static void
439parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end,
440 Py_ssize_t *n_remainder, int *has_decimal)
441{
442 Py_ssize_t remainder;
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300443 int kind = PyUnicode_KIND(s);
444 void *data = PyUnicode_DATA(s);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200445
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300446 while (pos<end && Py_ISDIGIT(PyUnicode_READ(kind, data, pos)))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200447 ++pos;
448 remainder = pos;
449
450 /* Does remainder start with a decimal point? */
Serhiy Storchaka1f932612016-08-29 15:57:26 +0300451 *has_decimal = pos<end && PyUnicode_READ(kind, data, remainder) == '.';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200452
453 /* Skip the decimal point. */
454 if (*has_decimal)
455 remainder++;
456
457 *n_remainder = end - remainder;
458}
459
460/* not all fields of format are used. for example, precision is
461 unused. should this take discrete params in order to be more clear
462 about what it does? or is passing a single format parameter easier
463 and more efficient enough to justify a little obfuscation? */
464static Py_ssize_t
465calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
466 Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start,
467 Py_ssize_t n_end, Py_ssize_t n_remainder,
468 int has_decimal, const LocaleInfo *locale,
Victor Stinner41a863c2012-02-24 00:37:51 +0100469 const InternalFormatSpec *format, Py_UCS4 *maxchar)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200470{
471 Py_ssize_t n_non_digit_non_padding;
472 Py_ssize_t n_padding;
473
474 spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0);
475 spec->n_lpadding = 0;
476 spec->n_prefix = n_prefix;
Victor Stinner41a863c2012-02-24 00:37:51 +0100477 spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200478 spec->n_remainder = n_remainder;
479 spec->n_spadding = 0;
480 spec->n_rpadding = 0;
481 spec->sign = '\0';
482 spec->n_sign = 0;
483
484 /* the output will look like:
485 | |
486 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
487 | |
488
489 sign is computed from format->sign and the actual
490 sign of the number
491
492 prefix is given (it's for the '0x' prefix)
493
494 digits is already known
495
496 the total width is either given, or computed from the
497 actual digits
498
499 only one of lpadding, spadding, and rpadding can be non-zero,
500 and it's calculated from the width and other fields
501 */
502
503 /* compute the various parts we're going to write */
504 switch (format->sign) {
505 case '+':
506 /* always put a + or - */
507 spec->n_sign = 1;
508 spec->sign = (sign_char == '-' ? '-' : '+');
509 break;
510 case ' ':
511 spec->n_sign = 1;
512 spec->sign = (sign_char == '-' ? '-' : ' ');
513 break;
514 default:
515 /* Not specified, or the default (-) */
516 if (sign_char == '-') {
517 spec->n_sign = 1;
518 spec->sign = '-';
519 }
520 }
521
522 /* The number of chars used for non-digits and non-padding. */
523 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
524 spec->n_remainder;
525
526 /* min_width can go negative, that's okay. format->width == -1 means
527 we don't care. */
528 if (format->fill_char == '0' && format->align == '=')
529 spec->n_min_width = format->width - n_non_digit_non_padding;
530 else
531 spec->n_min_width = 0;
532
533 if (spec->n_digits == 0)
534 /* This case only occurs when using 'c' formatting, we need
535 to special case it because the grouping code always wants
536 to have at least one character. */
537 spec->n_grouped_digits = 0;
Victor Stinner41a863c2012-02-24 00:37:51 +0100538 else {
539 Py_UCS4 grouping_maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200540 spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
Victor Stinner41a863c2012-02-24 00:37:51 +0100541 NULL, 0,
542 0, NULL,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200543 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100544 locale->grouping, locale->thousands_sep, &grouping_maxchar);
545 *maxchar = Py_MAX(*maxchar, grouping_maxchar);
546 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200547
548 /* Given the desired width and the total of digit and non-digit
549 space we consume, see if we need any padding. format->width can
550 be negative (meaning no padding), but this code still works in
551 that case. */
552 n_padding = format->width -
553 (n_non_digit_non_padding + spec->n_grouped_digits);
554 if (n_padding > 0) {
555 /* Some padding is needed. Determine if it's left, space, or right. */
556 switch (format->align) {
557 case '<':
558 spec->n_rpadding = n_padding;
559 break;
560 case '^':
561 spec->n_lpadding = n_padding / 2;
562 spec->n_rpadding = n_padding - spec->n_lpadding;
563 break;
564 case '=':
565 spec->n_spadding = n_padding;
566 break;
567 case '>':
568 spec->n_lpadding = n_padding;
569 break;
570 default:
571 /* Shouldn't get here, but treat it as '>' */
Barry Warsawb2e57942017-09-14 18:13:16 -0700572 Py_UNREACHABLE();
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200573 }
574 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100575
576 if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding)
577 *maxchar = Py_MAX(*maxchar, format->fill_char);
578
Victor Stinner90f50d42012-02-24 01:44:47 +0100579 if (spec->n_decimal)
580 *maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point));
581
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
583 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
584 spec->n_remainder + spec->n_rpadding;
585}
586
587/* Fill in the digit parts of a numbers's string representation,
588 as determined in calc_number_widths().
Victor Stinnerafbaa202011-09-28 21:50:16 +0200589 Return -1 on error, or 0 on success. */
590static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200591fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end,
Victor Stinnerafbaa202011-09-28 21:50:16 +0200593 PyObject *prefix, Py_ssize_t p_start,
594 Py_UCS4 fill_char,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200595 LocaleInfo *locale, int toupper)
596{
597 /* Used to keep track of digits, decimal, and remainder. */
598 Py_ssize_t d_pos = d_start;
Victor Stinner22c103b2013-05-07 23:50:03 +0200599 const unsigned int kind = writer->kind;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200600 const void *data = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200601 Py_ssize_t r;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200602
603 if (spec->n_lpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200604 _PyUnicode_FastFill(writer->buffer,
605 writer->pos, spec->n_lpadding, fill_char);
606 writer->pos += spec->n_lpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200607 }
608 if (spec->n_sign == 1) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200609 PyUnicode_WRITE(kind, data, writer->pos, spec->sign);
610 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200611 }
612 if (spec->n_prefix) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200613 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
614 prefix, p_start,
615 spec->n_prefix);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200616 if (toupper) {
617 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500618 for (t = 0; t < spec->n_prefix; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200619 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100620 c = Py_TOUPPER(c);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100621 assert (c <= 127);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200622 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500623 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200624 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200625 writer->pos += spec->n_prefix;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200626 }
627 if (spec->n_spadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200628 _PyUnicode_FastFill(writer->buffer,
629 writer->pos, spec->n_spadding, fill_char);
630 writer->pos += spec->n_spadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200631 }
632
633 /* Only for type 'c' special case, it has no digits. */
634 if (spec->n_digits != 0) {
635 /* Fill the digits with InsertThousandsGrouping. */
Victor Stinnerdba2dee2011-09-28 21:50:42 +0200636 char *pdigits;
637 if (PyUnicode_READY(digits))
638 return -1;
639 pdigits = PyUnicode_DATA(digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640 if (PyUnicode_KIND(digits) < kind) {
641 pdigits = _PyUnicode_AsKind(digits, kind);
Victor Stinnerafbaa202011-09-28 21:50:16 +0200642 if (pdigits == NULL)
643 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200644 }
Victor Stinner90f50d42012-02-24 01:44:47 +0100645 r = _PyUnicode_InsertThousandsGrouping(
Victor Stinnerd3f08822012-05-29 12:57:52 +0200646 writer->buffer, writer->pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200647 spec->n_grouped_digits,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200648 pdigits + kind * d_pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200649 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100650 locale->grouping, locale->thousands_sep, NULL);
Victor Stinner90f50d42012-02-24 01:44:47 +0100651 if (r == -1)
652 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200653 assert(r == spec->n_grouped_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 if (PyUnicode_KIND(digits) < kind)
655 PyMem_Free(pdigits);
656 d_pos += spec->n_digits;
657 }
658 if (toupper) {
659 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500660 for (t = 0; t < spec->n_grouped_digits; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200661 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100662 c = Py_TOUPPER(c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500663 if (c > 127) {
664 PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
665 return -1;
666 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200667 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500668 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200669 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200670 writer->pos += spec->n_grouped_digits;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200671
672 if (spec->n_decimal) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200673 _PyUnicode_FastCopyCharacters(
674 writer->buffer, writer->pos,
675 locale->decimal_point, 0, spec->n_decimal);
676 writer->pos += spec->n_decimal;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200677 d_pos += 1;
678 }
679
680 if (spec->n_remainder) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200681 _PyUnicode_FastCopyCharacters(
682 writer->buffer, writer->pos,
683 digits, d_pos, spec->n_remainder);
684 writer->pos += spec->n_remainder;
Brett Cannon8a250fa2012-06-25 16:13:44 -0400685 /* d_pos += spec->n_remainder; */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200686 }
687
688 if (spec->n_rpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200689 _PyUnicode_FastFill(writer->buffer,
690 writer->pos, spec->n_rpadding,
691 fill_char);
692 writer->pos += spec->n_rpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200693 }
Victor Stinnerafbaa202011-09-28 21:50:16 +0200694 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200695}
696
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200697static const char no_grouping[1] = {CHAR_MAX};
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200698
699/* Find the decimal point character(s?), thousands_separator(s?), and
700 grouping description, either for the current locale if type is
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400701 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE or
702 LT_UNDERSCORE_LOCALE/LT_UNDER_FOUR_LOCALE, or none if LT_NO_LOCALE. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100703static int
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700704get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200705{
706 switch (type) {
707 case LT_CURRENT_LOCALE: {
Victor Stinnercb064fc2018-01-15 15:58:02 +0100708 if (_Py_GetLocaleconvNumeric(&locale_info->decimal_point,
709 &locale_info->thousands_sep,
710 &locale_info->grouping) < 0) {
Victor Stinner41a863c2012-02-24 00:37:51 +0100711 return -1;
Victor Stinnercb064fc2018-01-15 15:58:02 +0100712 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200713 break;
714 }
715 case LT_DEFAULT_LOCALE:
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400716 case LT_UNDERSCORE_LOCALE:
717 case LT_UNDER_FOUR_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100718 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400719 locale_info->thousands_sep = PyUnicode_FromOrdinal(
720 type == LT_DEFAULT_LOCALE ? ',' : '_');
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700721 if (!locale_info->decimal_point || !locale_info->thousands_sep)
Victor Stinner41a863c2012-02-24 00:37:51 +0100722 return -1;
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400723 if (type != LT_UNDER_FOUR_LOCALE)
724 locale_info->grouping = "\3"; /* Group every 3 characters. The
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200725 (implicit) trailing 0 means repeat
726 infinitely. */
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400727 else
728 locale_info->grouping = "\4"; /* Bin/oct/hex group every four. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200729 break;
730 case LT_NO_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100731 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
732 locale_info->thousands_sep = PyUnicode_New(0, 0);
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700733 if (!locale_info->decimal_point || !locale_info->thousands_sep)
Victor Stinner41a863c2012-02-24 00:37:51 +0100734 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200735 locale_info->grouping = no_grouping;
736 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200737 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100738 return 0;
739}
740
741static void
742free_locale_info(LocaleInfo *locale_info)
743{
744 Py_XDECREF(locale_info->decimal_point);
745 Py_XDECREF(locale_info->thousands_sep);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200746}
747
748/************************************************************************/
749/*********** string formatting ******************************************/
750/************************************************************************/
751
Victor Stinnerd3f08822012-05-29 12:57:52 +0200752static int
753format_string_internal(PyObject *value, const InternalFormatSpec *format,
754 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200755{
756 Py_ssize_t lpad;
757 Py_ssize_t rpad;
758 Py_ssize_t total;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200759 Py_ssize_t len;
760 int result = -1;
Victor Stinnerece58de2012-04-23 23:36:38 +0200761 Py_UCS4 maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200762
Victor Stinnerd3f08822012-05-29 12:57:52 +0200763 assert(PyUnicode_IS_READY(value));
764 len = PyUnicode_GET_LENGTH(value);
765
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200766 /* sign is not allowed on strings */
767 if (format->sign != '\0') {
768 PyErr_SetString(PyExc_ValueError,
769 "Sign not allowed in string format specifier");
770 goto done;
771 }
772
773 /* alternate is not allowed on strings */
774 if (format->alternate) {
775 PyErr_SetString(PyExc_ValueError,
776 "Alternate form (#) not allowed in string format "
777 "specifier");
778 goto done;
779 }
780
781 /* '=' alignment not allowed on strings */
782 if (format->align == '=') {
783 PyErr_SetString(PyExc_ValueError,
784 "'=' alignment not allowed "
785 "in string format specifier");
786 goto done;
787 }
788
Victor Stinner621ef3d2012-10-02 00:33:47 +0200789 if ((format->width == -1 || format->width <= len)
790 && (format->precision == -1 || format->precision >= len)) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200791 /* Fast path */
792 return _PyUnicodeWriter_WriteStr(writer, value);
793 }
794
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200795 /* if precision is specified, output no more that format.precision
796 characters */
797 if (format->precision >= 0 && len >= format->precision) {
798 len = format->precision;
799 }
800
801 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
802
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200803 maxchar = writer->maxchar;
Victor Stinnera4ac6002012-01-21 15:50:49 +0100804 if (lpad != 0 || rpad != 0)
805 maxchar = Py_MAX(maxchar, format->fill_char);
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200806 if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
807 Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
808 maxchar = Py_MAX(maxchar, valmaxchar);
809 }
Victor Stinnera4ac6002012-01-21 15:50:49 +0100810
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200811 /* allocate the resulting string */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200812 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200813 goto done;
814
815 /* Write into that space. First the padding. */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400816 result = fill_padding(writer, len, format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200817 if (result == -1)
818 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200819
820 /* Then the source string. */
Victor Stinnerc9d369f2012-06-16 02:22:37 +0200821 if (len) {
822 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
823 value, 0, len);
824 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200825 writer->pos += (len + rpad);
826 result = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200827
828done:
829 return result;
830}
831
832
833/************************************************************************/
834/*********** long formatting ********************************************/
835/************************************************************************/
836
Victor Stinnerd3f08822012-05-29 12:57:52 +0200837static int
838format_long_internal(PyObject *value, const InternalFormatSpec *format,
839 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200840{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200841 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +0100842 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200843 PyObject *tmp = NULL;
844 Py_ssize_t inumeric_chars;
845 Py_UCS4 sign_char = '\0';
846 Py_ssize_t n_digits; /* count of digits need from the computed
847 string */
848 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
849 produces non-digits */
850 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
851 Py_ssize_t n_total;
Victor Stinnered277852012-02-01 00:22:23 +0100852 Py_ssize_t prefix = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200853 NumberFieldWidths spec;
854 long x;
855
856 /* Locale settings, either from the actual locale or
857 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +0100858 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200859
860 /* no precision allowed on integers */
861 if (format->precision != -1) {
862 PyErr_SetString(PyExc_ValueError,
863 "Precision not allowed in integer format specifier");
864 goto done;
865 }
866
867 /* special case for character formatting */
868 if (format->type == 'c') {
869 /* error to specify a sign */
870 if (format->sign != '\0') {
871 PyErr_SetString(PyExc_ValueError,
872 "Sign not allowed with integer"
873 " format specifier 'c'");
874 goto done;
875 }
Eric V. Smitha12572f2014-04-15 22:37:55 -0400876 /* error to request alternate format */
877 if (format->alternate) {
878 PyErr_SetString(PyExc_ValueError,
879 "Alternate form (#) not allowed with integer"
880 " format specifier 'c'");
881 goto done;
882 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200883
884 /* taken from unicodeobject.c formatchar() */
885 /* Integer input truncated to a character */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200886 x = PyLong_AsLong(value);
887 if (x == -1 && PyErr_Occurred())
888 goto done;
889 if (x < 0 || x > 0x10ffff) {
890 PyErr_SetString(PyExc_OverflowError,
Victor Stinnera4ac6002012-01-21 15:50:49 +0100891 "%c arg not in range(0x110000)");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200892 goto done;
893 }
894 tmp = PyUnicode_FromOrdinal(x);
895 inumeric_chars = 0;
896 n_digits = 1;
Amaury Forgeot d'Arc6d766fc2012-01-23 23:20:43 +0100897 maxchar = Py_MAX(maxchar, (Py_UCS4)x);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200898
899 /* As a sort-of hack, we tell calc_number_widths that we only
900 have "remainder" characters. calc_number_widths thinks
901 these are characters that don't get formatted, only copied
902 into the output string. We do this for 'c' formatting,
903 because the characters are likely to be non-digits. */
904 n_remainder = 1;
905 }
906 else {
907 int base;
908 int leading_chars_to_skip = 0; /* Number of characters added by
909 PyNumber_ToBase that we want to
910 skip over. */
911
912 /* Compute the base and how many characters will be added by
913 PyNumber_ToBase */
914 switch (format->type) {
915 case 'b':
916 base = 2;
917 leading_chars_to_skip = 2; /* 0b */
918 break;
919 case 'o':
920 base = 8;
921 leading_chars_to_skip = 2; /* 0o */
922 break;
923 case 'x':
924 case 'X':
925 base = 16;
926 leading_chars_to_skip = 2; /* 0x */
927 break;
928 default: /* shouldn't be needed, but stops a compiler warning */
929 case 'd':
930 case 'n':
931 base = 10;
932 break;
933 }
934
Victor Stinnerd3f08822012-05-29 12:57:52 +0200935 if (format->sign != '+' && format->sign != ' '
936 && format->width == -1
937 && format->type != 'X' && format->type != 'n'
938 && !format->thousands_separators
939 && PyLong_CheckExact(value))
940 {
941 /* Fast path */
942 return _PyLong_FormatWriter(writer, value, base, format->alternate);
943 }
944
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200945 /* The number of prefix chars is the same as the leading
946 chars to skip */
947 if (format->alternate)
948 n_prefix = leading_chars_to_skip;
949
950 /* Do the hard part, converting to a string in a given base */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200951 tmp = _PyLong_Format(value, base);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200952 if (tmp == NULL || PyUnicode_READY(tmp) == -1)
953 goto done;
954
955 inumeric_chars = 0;
956 n_digits = PyUnicode_GET_LENGTH(tmp);
957
958 prefix = inumeric_chars;
959
960 /* Is a sign character present in the output? If so, remember it
961 and skip it */
962 if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
963 sign_char = '-';
964 ++prefix;
965 ++leading_chars_to_skip;
966 }
967
968 /* Skip over the leading chars (0x, 0b, etc.) */
969 n_digits -= leading_chars_to_skip;
970 inumeric_chars += leading_chars_to_skip;
971 }
972
973 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100974 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -0400975 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +0100976 &locale) == -1)
977 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200978
979 /* Calculate how much memory we'll need. */
980 n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars,
Victor Stinner41a863c2012-02-24 00:37:51 +0100981 inumeric_chars + n_digits, n_remainder, 0,
982 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100983
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200984 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200985 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200986 goto done;
987
988 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200989 result = fill_number(writer, &spec,
990 tmp, inumeric_chars, inumeric_chars + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -0400991 tmp, prefix, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +0200992 &locale, format->type == 'X');
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200993
994done:
995 Py_XDECREF(tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +0100996 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200997 return result;
998}
999
1000/************************************************************************/
1001/*********** float formatting *******************************************/
1002/************************************************************************/
1003
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001004/* much of this is taken from unicodeobject.c */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001005static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001006format_float_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001007 const InternalFormatSpec *format,
1008 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001009{
1010 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
1011 Py_ssize_t n_digits;
1012 Py_ssize_t n_remainder;
1013 Py_ssize_t n_total;
1014 int has_decimal;
1015 double val;
Victor Stinner76d38502013-06-24 23:34:15 +02001016 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001017 Py_UCS4 type = format->type;
1018 int add_pct = 0;
1019 Py_ssize_t index;
1020 NumberFieldWidths spec;
1021 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001022 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001023 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001024 Py_UCS4 sign_char = '\0';
1025 int float_type; /* Used to see if we have a nan, inf, or regular float. */
1026 PyObject *unicode_tmp = NULL;
1027
1028 /* Locale settings, either from the actual locale or
1029 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001030 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001031
Victor Stinner2f084ec2013-06-23 14:54:30 +02001032 if (format->precision > INT_MAX) {
1033 PyErr_SetString(PyExc_ValueError, "precision too big");
1034 goto done;
1035 }
1036 precision = (int)format->precision;
1037
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001038 if (format->alternate)
1039 flags |= Py_DTSF_ALT;
1040
1041 if (type == '\0') {
1042 /* Omitted type specifier. Behaves in the same way as repr(x)
1043 and str(x) if no precision is given, else like 'g', but with
1044 at least one digit after the decimal point. */
1045 flags |= Py_DTSF_ADD_DOT_0;
1046 type = 'r';
1047 default_precision = 0;
1048 }
1049
1050 if (type == 'n')
1051 /* 'n' is the same as 'g', except for the locale used to
1052 format the result. We take care of that later. */
1053 type = 'g';
1054
1055 val = PyFloat_AsDouble(value);
1056 if (val == -1.0 && PyErr_Occurred())
1057 goto done;
1058
1059 if (type == '%') {
1060 type = 'f';
1061 val *= 100;
1062 add_pct = 1;
1063 }
1064
1065 if (precision < 0)
1066 precision = default_precision;
1067 else if (type == 'r')
1068 type = 'g';
1069
Martin Panter4c359642016-05-08 13:53:41 +00001070 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001071 8-bit char. This is safe, because we've restricted what "type"
1072 can be. */
1073 buf = PyOS_double_to_string(val, (char)type, precision, flags,
1074 &float_type);
1075 if (buf == NULL)
1076 goto done;
1077 n_digits = strlen(buf);
1078
1079 if (add_pct) {
1080 /* We know that buf has a trailing zero (since we just called
1081 strlen() on it), and we don't use that fact any more. So we
1082 can just write over the trailing zero. */
1083 buf[n_digits] = '%';
1084 n_digits += 1;
1085 }
1086
Victor Stinnerd3f08822012-05-29 12:57:52 +02001087 if (format->sign != '+' && format->sign != ' '
1088 && format->width == -1
1089 && format->type != 'n'
1090 && !format->thousands_separators)
1091 {
1092 /* Fast path */
Victor Stinner4a587072013-11-19 12:54:53 +01001093 result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits);
1094 PyMem_Free(buf);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001095 return result;
1096 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001097
Victor Stinner4a587072013-11-19 12:54:53 +01001098 /* Since there is no unicode version of PyOS_double_to_string,
1099 just use the 8 bit version and then convert to unicode. */
1100 unicode_tmp = _PyUnicode_FromASCII(buf, n_digits);
1101 PyMem_Free(buf);
1102 if (unicode_tmp == NULL)
1103 goto done;
1104
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001105 /* Is a sign character present in the output? If so, remember it
1106 and skip it */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001107 index = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001108 if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') {
1109 sign_char = '-';
1110 ++index;
1111 --n_digits;
1112 }
1113
1114 /* Determine if we have any "remainder" (after the digits, might include
1115 decimal or exponent or both (or neither)) */
1116 parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal);
1117
1118 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001119 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -04001120 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +01001121 &locale) == -1)
1122 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001123
1124 /* Calculate how much memory we'll need. */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001125 n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001126 index + n_digits, n_remainder, has_decimal,
Victor Stinner41a863c2012-02-24 00:37:51 +01001127 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +01001128
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001129 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001130 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001131 goto done;
1132
1133 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001134 result = fill_number(writer, &spec,
1135 unicode_tmp, index, index + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001136 NULL, 0, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001137 &locale, 0);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001138
1139done:
Stefan Krahd9c1bf72012-09-06 13:02:46 +02001140 Py_XDECREF(unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001141 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001142 return result;
1143}
1144
1145/************************************************************************/
1146/*********** complex formatting *****************************************/
1147/************************************************************************/
1148
Victor Stinnerd3f08822012-05-29 12:57:52 +02001149static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001150format_complex_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001151 const InternalFormatSpec *format,
1152 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001153{
1154 double re;
1155 double im;
1156 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1157 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1158
1159 InternalFormatSpec tmp_format = *format;
1160 Py_ssize_t n_re_digits;
1161 Py_ssize_t n_im_digits;
1162 Py_ssize_t n_re_remainder;
1163 Py_ssize_t n_im_remainder;
1164 Py_ssize_t n_re_total;
1165 Py_ssize_t n_im_total;
1166 int re_has_decimal;
1167 int im_has_decimal;
Victor Stinner76d38502013-06-24 23:34:15 +02001168 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001169 Py_UCS4 type = format->type;
1170 Py_ssize_t i_re;
1171 Py_ssize_t i_im;
1172 NumberFieldWidths re_spec;
1173 NumberFieldWidths im_spec;
1174 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001175 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001176 Py_UCS4 maxchar = 127;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001177 enum PyUnicode_Kind rkind;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001178 void *rdata;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001179 Py_UCS4 re_sign_char = '\0';
1180 Py_UCS4 im_sign_char = '\0';
1181 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1182 int im_float_type;
1183 int add_parens = 0;
1184 int skip_re = 0;
1185 Py_ssize_t lpad;
1186 Py_ssize_t rpad;
1187 Py_ssize_t total;
1188 PyObject *re_unicode_tmp = NULL;
1189 PyObject *im_unicode_tmp = NULL;
1190
1191 /* Locale settings, either from the actual locale or
1192 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001193 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001194
Victor Stinner2f084ec2013-06-23 14:54:30 +02001195 if (format->precision > INT_MAX) {
1196 PyErr_SetString(PyExc_ValueError, "precision too big");
1197 goto done;
1198 }
1199 precision = (int)format->precision;
1200
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001201 /* Zero padding is not allowed. */
1202 if (format->fill_char == '0') {
1203 PyErr_SetString(PyExc_ValueError,
1204 "Zero padding is not allowed in complex format "
1205 "specifier");
1206 goto done;
1207 }
1208
1209 /* Neither is '=' alignment . */
1210 if (format->align == '=') {
1211 PyErr_SetString(PyExc_ValueError,
1212 "'=' alignment flag is not allowed in complex format "
1213 "specifier");
1214 goto done;
1215 }
1216
1217 re = PyComplex_RealAsDouble(value);
1218 if (re == -1.0 && PyErr_Occurred())
1219 goto done;
1220 im = PyComplex_ImagAsDouble(value);
1221 if (im == -1.0 && PyErr_Occurred())
1222 goto done;
1223
1224 if (format->alternate)
1225 flags |= Py_DTSF_ALT;
1226
1227 if (type == '\0') {
1228 /* Omitted type specifier. Should be like str(self). */
1229 type = 'r';
1230 default_precision = 0;
1231 if (re == 0.0 && copysign(1.0, re) == 1.0)
1232 skip_re = 1;
1233 else
1234 add_parens = 1;
1235 }
1236
1237 if (type == 'n')
1238 /* 'n' is the same as 'g', except for the locale used to
1239 format the result. We take care of that later. */
1240 type = 'g';
1241
1242 if (precision < 0)
1243 precision = default_precision;
1244 else if (type == 'r')
1245 type = 'g';
1246
Martin Panter4c359642016-05-08 13:53:41 +00001247 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001248 8-bit char. This is safe, because we've restricted what "type"
1249 can be. */
1250 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1251 &re_float_type);
1252 if (re_buf == NULL)
1253 goto done;
1254 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1255 &im_float_type);
1256 if (im_buf == NULL)
1257 goto done;
1258
1259 n_re_digits = strlen(re_buf);
1260 n_im_digits = strlen(im_buf);
1261
1262 /* Since there is no unicode version of PyOS_double_to_string,
1263 just use the 8 bit version and then convert to unicode. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001264 re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001265 if (re_unicode_tmp == NULL)
1266 goto done;
1267 i_re = 0;
1268
Victor Stinnerd3f08822012-05-29 12:57:52 +02001269 im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001270 if (im_unicode_tmp == NULL)
1271 goto done;
1272 i_im = 0;
1273
1274 /* Is a sign character present in the output? If so, remember it
1275 and skip it */
1276 if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') {
1277 re_sign_char = '-';
1278 ++i_re;
1279 --n_re_digits;
1280 }
1281 if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') {
1282 im_sign_char = '-';
1283 ++i_im;
1284 --n_im_digits;
1285 }
1286
1287 /* Determine if we have any "remainder" (after the digits, might include
1288 decimal or exponent or both (or neither)) */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001289 parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001290 &n_re_remainder, &re_has_decimal);
Victor Stinnerafbaa202011-09-28 21:50:16 +02001291 parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001292 &n_im_remainder, &im_has_decimal);
1293
1294 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001295 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
Eric V. Smith89e1b1a2016-09-09 23:06:47 -04001296 format->thousands_separators,
Victor Stinner41a863c2012-02-24 00:37:51 +01001297 &locale) == -1)
1298 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001299
1300 /* Turn off any padding. We'll do it later after we've composed
1301 the numbers without padding. */
1302 tmp_format.fill_char = '\0';
1303 tmp_format.align = '<';
1304 tmp_format.width = -1;
1305
1306 /* Calculate how much memory we'll need. */
1307 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp,
1308 i_re, i_re + n_re_digits, n_re_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001309 re_has_decimal, &locale, &tmp_format,
1310 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001311
1312 /* Same formatting, but always include a sign, unless the real part is
1313 * going to be omitted, in which case we use whatever sign convention was
1314 * requested by the original format. */
1315 if (!skip_re)
1316 tmp_format.sign = '+';
1317 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp,
1318 i_im, i_im + n_im_digits, n_im_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001319 im_has_decimal, &locale, &tmp_format,
1320 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001321
1322 if (skip_re)
1323 n_re_total = 0;
1324
1325 /* Add 1 for the 'j', and optionally 2 for parens. */
1326 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1327 format->width, format->align, &lpad, &rpad, &total);
1328
Victor Stinner41a863c2012-02-24 00:37:51 +01001329 if (lpad || rpad)
Victor Stinnera4ac6002012-01-21 15:50:49 +01001330 maxchar = Py_MAX(maxchar, format->fill_char);
1331
Victor Stinnerd3f08822012-05-29 12:57:52 +02001332 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001333 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001334 rkind = writer->kind;
1335 rdata = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001336
1337 /* Populate the memory. First, the padding. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001338 result = fill_padding(writer,
1339 n_re_total + n_im_total + 1 + add_parens * 2,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001340 format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001341 if (result == -1)
1342 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001343
Victor Stinnerd3f08822012-05-29 12:57:52 +02001344 if (add_parens) {
1345 PyUnicode_WRITE(rkind, rdata, writer->pos, '(');
1346 writer->pos++;
1347 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001348
1349 if (!skip_re) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001350 result = fill_number(writer, &re_spec,
1351 re_unicode_tmp, i_re, i_re + n_re_digits,
1352 NULL, 0,
1353 0,
1354 &locale, 0);
1355 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001356 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001357 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001358 result = fill_number(writer, &im_spec,
1359 im_unicode_tmp, i_im, i_im + n_im_digits,
1360 NULL, 0,
1361 0,
1362 &locale, 0);
1363 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001364 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001365 PyUnicode_WRITE(rkind, rdata, writer->pos, 'j');
1366 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001367
Victor Stinnerd3f08822012-05-29 12:57:52 +02001368 if (add_parens) {
1369 PyUnicode_WRITE(rkind, rdata, writer->pos, ')');
1370 writer->pos++;
1371 }
1372
1373 writer->pos += rpad;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001374
1375done:
1376 PyMem_Free(re_buf);
1377 PyMem_Free(im_buf);
1378 Py_XDECREF(re_unicode_tmp);
1379 Py_XDECREF(im_unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001380 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001381 return result;
1382}
1383
1384/************************************************************************/
1385/*********** built in formatters ****************************************/
1386/************************************************************************/
doko@ubuntu.com39378f72012-06-21 12:12:20 +02001387static int
Victor Stinnerd3f08822012-05-29 12:57:52 +02001388format_obj(PyObject *obj, _PyUnicodeWriter *writer)
1389{
1390 PyObject *str;
1391 int err;
1392
1393 str = PyObject_Str(obj);
1394 if (str == NULL)
1395 return -1;
1396 err = _PyUnicodeWriter_WriteStr(writer, str);
1397 Py_DECREF(str);
1398 return err;
1399}
1400
1401int
1402_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1403 PyObject *obj,
1404 PyObject *format_spec,
1405 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001406{
1407 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001408
1409 assert(PyUnicode_Check(obj));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001410
1411 /* check for the special case of zero length format spec, make
1412 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001413 if (start == end) {
1414 if (PyUnicode_CheckExact(obj))
1415 return _PyUnicodeWriter_WriteStr(writer, obj);
1416 else
1417 return format_obj(obj, writer);
1418 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001419
1420 /* parse the format_spec */
1421 if (!parse_internal_render_format_spec(format_spec, start, end,
1422 &format, 's', '<'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001423 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001424
1425 /* type conversion? */
1426 switch (format.type) {
1427 case 's':
1428 /* no type conversion needed, already a string. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001429 return format_string_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001430 default:
1431 /* unknown */
1432 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001433 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001434 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001435}
1436
Victor Stinnerd3f08822012-05-29 12:57:52 +02001437int
1438_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1439 PyObject *obj,
1440 PyObject *format_spec,
1441 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001442{
Victor Stinnerd3f08822012-05-29 12:57:52 +02001443 PyObject *tmp = NULL, *str = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001444 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001445 int result = -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001446
1447 /* check for the special case of zero length format spec, make
1448 it equivalent to str(obj) */
1449 if (start == end) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001450 if (PyLong_CheckExact(obj))
1451 return _PyLong_FormatWriter(writer, obj, 10, 0);
1452 else
1453 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001454 }
1455
1456 /* parse the format_spec */
1457 if (!parse_internal_render_format_spec(format_spec, start, end,
1458 &format, 'd', '>'))
1459 goto done;
1460
1461 /* type conversion? */
1462 switch (format.type) {
1463 case 'b':
1464 case 'c':
1465 case 'd':
1466 case 'o':
1467 case 'x':
1468 case 'X':
1469 case 'n':
Serhiy Storchaka95949422013-08-27 19:40:23 +03001470 /* no type conversion needed, already an int. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001471 result = format_long_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001472 break;
1473
1474 case 'e':
1475 case 'E':
1476 case 'f':
1477 case 'F':
1478 case 'g':
1479 case 'G':
1480 case '%':
1481 /* convert to float */
1482 tmp = PyNumber_Float(obj);
1483 if (tmp == NULL)
1484 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001485 result = format_float_internal(tmp, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001486 break;
1487
1488 default:
1489 /* unknown */
1490 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1491 goto done;
1492 }
1493
1494done:
1495 Py_XDECREF(tmp);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001496 Py_XDECREF(str);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001497 return result;
1498}
1499
Victor Stinnerd3f08822012-05-29 12:57:52 +02001500int
1501_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1502 PyObject *obj,
1503 PyObject *format_spec,
1504 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001505{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001506 InternalFormatSpec format;
1507
1508 /* check for the special case of zero length format spec, make
1509 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001510 if (start == end)
1511 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001512
1513 /* parse the format_spec */
1514 if (!parse_internal_render_format_spec(format_spec, start, end,
1515 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001516 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001517
1518 /* type conversion? */
1519 switch (format.type) {
1520 case '\0': /* No format code: like 'g', but with at least one decimal. */
1521 case 'e':
1522 case 'E':
1523 case 'f':
1524 case 'F':
1525 case 'g':
1526 case 'G':
1527 case 'n':
1528 case '%':
1529 /* no conversion, already a float. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001530 return format_float_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001531
1532 default:
1533 /* unknown */
1534 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001535 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001536 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001537}
1538
Victor Stinnerd3f08822012-05-29 12:57:52 +02001539int
1540_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1541 PyObject *obj,
1542 PyObject *format_spec,
1543 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001544{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001545 InternalFormatSpec format;
1546
1547 /* check for the special case of zero length format spec, make
1548 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001549 if (start == end)
1550 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001551
1552 /* parse the format_spec */
1553 if (!parse_internal_render_format_spec(format_spec, start, end,
1554 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001555 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001556
1557 /* type conversion? */
1558 switch (format.type) {
1559 case '\0': /* No format code: like 'g', but with at least one decimal. */
1560 case 'e':
1561 case 'E':
1562 case 'f':
1563 case 'F':
1564 case 'g':
1565 case 'G':
1566 case 'n':
1567 /* no conversion, already a complex. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001568 return format_complex_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001569
1570 default:
1571 /* unknown */
1572 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001573 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001574 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001575}