ยปCore Development>Code coverage>Python/formatter_unicode.c

Python code coverage for Python/formatter_unicode.c

#countcontent
1n/a/* implements the unicode (as opposed to string) version of the
2n/a built-in formatters for string, int, float. that is, the versions
3n/a of int.__float__, etc., that take and return unicode objects */
4n/a
5n/a#include "Python.h"
6n/a#include <locale.h>
7n/a
8n/a/* Raises an exception about an unknown presentation type for this
9n/a * type. */
10n/a
11n/astatic void
12n/aunknown_presentation_type(Py_UCS4 presentation_type,
13n/a const char* type_name)
14n/a{
15n/a /* %c might be out-of-range, hence the two cases. */
16n/a if (presentation_type > 32 && presentation_type < 128)
17n/a PyErr_Format(PyExc_ValueError,
18n/a "Unknown format code '%c' "
19n/a "for object of type '%.200s'",
20n/a (char)presentation_type,
21n/a type_name);
22n/a else
23n/a PyErr_Format(PyExc_ValueError,
24n/a "Unknown format code '\\x%x' "
25n/a "for object of type '%.200s'",
26n/a (unsigned int)presentation_type,
27n/a type_name);
28n/a}
29n/a
30n/astatic void
31n/ainvalid_comma_type(Py_UCS4 presentation_type)
32n/a{
33n/a if (presentation_type > 32 && presentation_type < 128)
34n/a PyErr_Format(PyExc_ValueError,
35n/a "Cannot specify ',' or '_' with '%c'.",
36n/a (char)presentation_type);
37n/a else
38n/a PyErr_Format(PyExc_ValueError,
39n/a "Cannot specify ',' or '_' with '\\x%x'.",
40n/a (unsigned int)presentation_type);
41n/a}
42n/a
43n/astatic void
44n/ainvalid_comma_and_underscore(void)
45n/a{
46n/a PyErr_Format(PyExc_ValueError, "Cannot specify both ',' and '_'.");
47n/a}
48n/a
49n/a/*
50n/a get_integer consumes 0 or more decimal digit characters from an
51n/a input string, updates *result with the corresponding positive
52n/a integer, and returns the number of digits consumed.
53n/a
54n/a returns -1 on error.
55n/a*/
56n/astatic int
57n/aget_integer(PyObject *str, Py_ssize_t *ppos, Py_ssize_t end,
58n/a Py_ssize_t *result)
59n/a{
60n/a Py_ssize_t accumulator, digitval, pos = *ppos;
61n/a int numdigits;
62n/a int kind = PyUnicode_KIND(str);
63n/a void *data = PyUnicode_DATA(str);
64n/a
65n/a accumulator = numdigits = 0;
66n/a for (; pos < end; pos++, numdigits++) {
67n/a digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ(kind, data, pos));
68n/a if (digitval < 0)
69n/a break;
70n/a /*
71n/a Detect possible overflow before it happens:
72n/a
73n/a accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
74n/a accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
75n/a */
76n/a if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
77n/a PyErr_Format(PyExc_ValueError,
78n/a "Too many decimal digits in format string");
79n/a *ppos = pos;
80n/a return -1;
81n/a }
82n/a accumulator = accumulator * 10 + digitval;
83n/a }
84n/a *ppos = pos;
85n/a *result = accumulator;
86n/a return numdigits;
87n/a}
88n/a
89n/a/************************************************************************/
90n/a/*********** standard format specifier parsing **************************/
91n/a/************************************************************************/
92n/a
93n/a/* returns true if this character is a specifier alignment token */
94n/aPy_LOCAL_INLINE(int)
95n/ais_alignment_token(Py_UCS4 c)
96n/a{
97n/a switch (c) {
98n/a case '<': case '>': case '=': case '^':
99n/a return 1;
100n/a default:
101n/a return 0;
102n/a }
103n/a}
104n/a
105n/a/* returns true if this character is a sign element */
106n/aPy_LOCAL_INLINE(int)
107n/ais_sign_element(Py_UCS4 c)
108n/a{
109n/a switch (c) {
110n/a case ' ': case '+': case '-':
111n/a return 1;
112n/a default:
113n/a return 0;
114n/a }
115n/a}
116n/a
117n/a/* Locale type codes. LT_NO_LOCALE must be zero. */
118n/aenum LocaleType {
119n/a LT_NO_LOCALE = 0,
120n/a LT_DEFAULT_LOCALE,
121n/a LT_UNDERSCORE_LOCALE,
122n/a LT_UNDER_FOUR_LOCALE,
123n/a LT_CURRENT_LOCALE
124n/a};
125n/a
126n/atypedef struct {
127n/a Py_UCS4 fill_char;
128n/a Py_UCS4 align;
129n/a int alternate;
130n/a Py_UCS4 sign;
131n/a Py_ssize_t width;
132n/a enum LocaleType thousands_separators;
133n/a Py_ssize_t precision;
134n/a Py_UCS4 type;
135n/a} InternalFormatSpec;
136n/a
137n/a#if 0
138n/a/* Occasionally useful for debugging. Should normally be commented out. */
139n/astatic void
140n/aDEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
141n/a{
142n/a printf("internal format spec: fill_char %d\n", format->fill_char);
143n/a printf("internal format spec: align %d\n", format->align);
144n/a printf("internal format spec: alternate %d\n", format->alternate);
145n/a printf("internal format spec: sign %d\n", format->sign);
146n/a printf("internal format spec: width %zd\n", format->width);
147n/a printf("internal format spec: thousands_separators %d\n",
148n/a format->thousands_separators);
149n/a printf("internal format spec: precision %zd\n", format->precision);
150n/a printf("internal format spec: type %c\n", format->type);
151n/a printf("\n");
152n/a}
153n/a#endif
154n/a
155n/a
156n/a/*
157n/a ptr points to the start of the format_spec, end points just past its end.
158n/a fills in format with the parsed information.
159n/a returns 1 on success, 0 on failure.
160n/a if failure, sets the exception
161n/a*/
162n/astatic int
163n/aparse_internal_render_format_spec(PyObject *format_spec,
164n/a Py_ssize_t start, Py_ssize_t end,
165n/a InternalFormatSpec *format,
166n/a char default_type,
167n/a char default_align)
168n/a{
169n/a Py_ssize_t pos = start;
170n/a int kind = PyUnicode_KIND(format_spec);
171n/a void *data = PyUnicode_DATA(format_spec);
172n/a /* end-pos is used throughout this code to specify the length of
173n/a the input string */
174n/a#define READ_spec(index) PyUnicode_READ(kind, data, index)
175n/a
176n/a Py_ssize_t consumed;
177n/a int align_specified = 0;
178n/a int fill_char_specified = 0;
179n/a
180n/a format->fill_char = ' ';
181n/a format->align = default_align;
182n/a format->alternate = 0;
183n/a format->sign = '\0';
184n/a format->width = -1;
185n/a format->thousands_separators = LT_NO_LOCALE;
186n/a format->precision = -1;
187n/a format->type = default_type;
188n/a
189n/a /* If the second char is an alignment token,
190n/a then parse the fill char */
191n/a if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) {
192n/a format->align = READ_spec(pos+1);
193n/a format->fill_char = READ_spec(pos);
194n/a fill_char_specified = 1;
195n/a align_specified = 1;
196n/a pos += 2;
197n/a }
198n/a else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) {
199n/a format->align = READ_spec(pos);
200n/a align_specified = 1;
201n/a ++pos;
202n/a }
203n/a
204n/a /* Parse the various sign options */
205n/a if (end-pos >= 1 && is_sign_element(READ_spec(pos))) {
206n/a format->sign = READ_spec(pos);
207n/a ++pos;
208n/a }
209n/a
210n/a /* If the next character is #, we're in alternate mode. This only
211n/a applies to integers. */
212n/a if (end-pos >= 1 && READ_spec(pos) == '#') {
213n/a format->alternate = 1;
214n/a ++pos;
215n/a }
216n/a
217n/a /* The special case for 0-padding (backwards compat) */
218n/a if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
219n/a format->fill_char = '0';
220n/a if (!align_specified) {
221n/a format->align = '=';
222n/a }
223n/a ++pos;
224n/a }
225n/a
226n/a consumed = get_integer(format_spec, &pos, end, &format->width);
227n/a if (consumed == -1)
228n/a /* Overflow error. Exception already set. */
229n/a return 0;
230n/a
231n/a /* If consumed is 0, we didn't consume any characters for the
232n/a width. In that case, reset the width to -1, because
233n/a get_integer() will have set it to zero. -1 is how we record
234n/a that the width wasn't specified. */
235n/a if (consumed == 0)
236n/a format->width = -1;
237n/a
238n/a /* Comma signifies add thousands separators */
239n/a if (end-pos && READ_spec(pos) == ',') {
240n/a format->thousands_separators = LT_DEFAULT_LOCALE;
241n/a ++pos;
242n/a }
243n/a /* Underscore signifies add thousands separators */
244n/a if (end-pos && READ_spec(pos) == '_') {
245n/a if (format->thousands_separators != LT_NO_LOCALE) {
246n/a invalid_comma_and_underscore();
247n/a return 0;
248n/a }
249n/a format->thousands_separators = LT_UNDERSCORE_LOCALE;
250n/a ++pos;
251n/a }
252n/a if (end-pos && READ_spec(pos) == ',') {
253n/a invalid_comma_and_underscore();
254n/a return 0;
255n/a }
256n/a
257n/a /* Parse field precision */
258n/a if (end-pos && READ_spec(pos) == '.') {
259n/a ++pos;
260n/a
261n/a consumed = get_integer(format_spec, &pos, end, &format->precision);
262n/a if (consumed == -1)
263n/a /* Overflow error. Exception already set. */
264n/a return 0;
265n/a
266n/a /* Not having a precision after a dot is an error. */
267n/a if (consumed == 0) {
268n/a PyErr_Format(PyExc_ValueError,
269n/a "Format specifier missing precision");
270n/a return 0;
271n/a }
272n/a
273n/a }
274n/a
275n/a /* Finally, parse the type field. */
276n/a
277n/a if (end-pos > 1) {
278n/a /* More than one char remain, invalid format specifier. */
279n/a PyErr_Format(PyExc_ValueError, "Invalid format specifier");
280n/a return 0;
281n/a }
282n/a
283n/a if (end-pos == 1) {
284n/a format->type = READ_spec(pos);
285n/a ++pos;
286n/a }
287n/a
288n/a /* Do as much validating as we can, just by looking at the format
289n/a specifier. Do not take into account what type of formatting
290n/a we're doing (int, float, string). */
291n/a
292n/a if (format->thousands_separators) {
293n/a switch (format->type) {
294n/a case 'd':
295n/a case 'e':
296n/a case 'f':
297n/a case 'g':
298n/a case 'E':
299n/a case 'G':
300n/a case '%':
301n/a case 'F':
302n/a case '\0':
303n/a /* These are allowed. See PEP 378.*/
304n/a break;
305n/a case 'b':
306n/a case 'o':
307n/a case 'x':
308n/a case 'X':
309n/a /* Underscores are allowed in bin/oct/hex. See PEP 515. */
310n/a if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
311n/a /* Every four digits, not every three, in bin/oct/hex. */
312n/a format->thousands_separators = LT_UNDER_FOUR_LOCALE;
313n/a break;
314n/a }
315n/a default:
316n/a invalid_comma_type(format->type);
317n/a return 0;
318n/a }
319n/a }
320n/a
321n/a assert (format->align <= 127);
322n/a assert (format->sign <= 127);
323n/a return 1;
324n/a}
325n/a
326n/a/* Calculate the padding needed. */
327n/astatic void
328n/acalc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align,
329n/a Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
330n/a Py_ssize_t *n_total)
331n/a{
332n/a if (width >= 0) {
333n/a if (nchars > width)
334n/a *n_total = nchars;
335n/a else
336n/a *n_total = width;
337n/a }
338n/a else {
339n/a /* not specified, use all of the chars and no more */
340n/a *n_total = nchars;
341n/a }
342n/a
343n/a /* Figure out how much leading space we need, based on the
344n/a aligning */
345n/a if (align == '>')
346n/a *n_lpadding = *n_total - nchars;
347n/a else if (align == '^')
348n/a *n_lpadding = (*n_total - nchars) / 2;
349n/a else if (align == '<' || align == '=')
350n/a *n_lpadding = 0;
351n/a else {
352n/a /* We should never have an unspecified alignment. */
353n/a *n_lpadding = 0;
354n/a assert(0);
355n/a }
356n/a
357n/a *n_rpadding = *n_total - nchars - *n_lpadding;
358n/a}
359n/a
360n/a/* Do the padding, and return a pointer to where the caller-supplied
361n/a content goes. */
362n/astatic int
363n/afill_padding(_PyUnicodeWriter *writer,
364n/a Py_ssize_t nchars,
365n/a Py_UCS4 fill_char, Py_ssize_t n_lpadding,
366n/a Py_ssize_t n_rpadding)
367n/a{
368n/a Py_ssize_t pos;
369n/a
370n/a /* Pad on left. */
371n/a if (n_lpadding) {
372n/a pos = writer->pos;
373n/a _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char);
374n/a }
375n/a
376n/a /* Pad on right. */
377n/a if (n_rpadding) {
378n/a pos = writer->pos + nchars + n_lpadding;
379n/a _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char);
380n/a }
381n/a
382n/a /* Pointer to the user content. */
383n/a writer->pos += n_lpadding;
384n/a return 0;
385n/a}
386n/a
387n/a/************************************************************************/
388n/a/*********** common routines for numeric formatting *********************/
389n/a/************************************************************************/
390n/a
391n/a/* Locale info needed for formatting integers and the part of floats
392n/a before and including the decimal. Note that locales only support
393n/a 8-bit chars, not unicode. */
394n/atypedef struct {
395n/a PyObject *decimal_point;
396n/a PyObject *thousands_sep;
397n/a const char *grouping;
398n/a} LocaleInfo;
399n/a
400n/a#define STATIC_LOCALE_INFO_INIT {0, 0, 0}
401n/a
402n/a/* describes the layout for an integer, see the comment in
403n/a calc_number_widths() for details */
404n/atypedef struct {
405n/a Py_ssize_t n_lpadding;
406n/a Py_ssize_t n_prefix;
407n/a Py_ssize_t n_spadding;
408n/a Py_ssize_t n_rpadding;
409n/a char sign;
410n/a Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
411n/a Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
412n/a any grouping chars. */
413n/a Py_ssize_t n_decimal; /* 0 if only an integer */
414n/a Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
415n/a excluding the decimal itself, if
416n/a present. */
417n/a
418n/a /* These 2 are not the widths of fields, but are needed by
419n/a STRINGLIB_GROUPING. */
420n/a Py_ssize_t n_digits; /* The number of digits before a decimal
421n/a or exponent. */
422n/a Py_ssize_t n_min_width; /* The min_width we used when we computed
423n/a the n_grouped_digits width. */
424n/a} NumberFieldWidths;
425n/a
426n/a
427n/a/* Given a number of the form:
428n/a digits[remainder]
429n/a where ptr points to the start and end points to the end, find where
430n/a the integer part ends. This could be a decimal, an exponent, both,
431n/a or neither.
432n/a If a decimal point is present, set *has_decimal and increment
433n/a remainder beyond it.
434n/a Results are undefined (but shouldn't crash) for improperly
435n/a formatted strings.
436n/a*/
437n/astatic void
438n/aparse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end,
439n/a Py_ssize_t *n_remainder, int *has_decimal)
440n/a{
441n/a Py_ssize_t remainder;
442n/a int kind = PyUnicode_KIND(s);
443n/a void *data = PyUnicode_DATA(s);
444n/a
445n/a while (pos<end && Py_ISDIGIT(PyUnicode_READ(kind, data, pos)))
446n/a ++pos;
447n/a remainder = pos;
448n/a
449n/a /* Does remainder start with a decimal point? */
450n/a *has_decimal = pos<end && PyUnicode_READ(kind, data, remainder) == '.';
451n/a
452n/a /* Skip the decimal point. */
453n/a if (*has_decimal)
454n/a remainder++;
455n/a
456n/a *n_remainder = end - remainder;
457n/a}
458n/a
459n/a/* not all fields of format are used. for example, precision is
460n/a unused. should this take discrete params in order to be more clear
461n/a about what it does? or is passing a single format parameter easier
462n/a and more efficient enough to justify a little obfuscation? */
463n/astatic Py_ssize_t
464n/acalc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
465n/a Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start,
466n/a Py_ssize_t n_end, Py_ssize_t n_remainder,
467n/a int has_decimal, const LocaleInfo *locale,
468n/a const InternalFormatSpec *format, Py_UCS4 *maxchar)
469n/a{
470n/a Py_ssize_t n_non_digit_non_padding;
471n/a Py_ssize_t n_padding;
472n/a
473n/a spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0);
474n/a spec->n_lpadding = 0;
475n/a spec->n_prefix = n_prefix;
476n/a spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0;
477n/a spec->n_remainder = n_remainder;
478n/a spec->n_spadding = 0;
479n/a spec->n_rpadding = 0;
480n/a spec->sign = '\0';
481n/a spec->n_sign = 0;
482n/a
483n/a /* the output will look like:
484n/a | |
485n/a | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
486n/a | |
487n/a
488n/a sign is computed from format->sign and the actual
489n/a sign of the number
490n/a
491n/a prefix is given (it's for the '0x' prefix)
492n/a
493n/a digits is already known
494n/a
495n/a the total width is either given, or computed from the
496n/a actual digits
497n/a
498n/a only one of lpadding, spadding, and rpadding can be non-zero,
499n/a and it's calculated from the width and other fields
500n/a */
501n/a
502n/a /* compute the various parts we're going to write */
503n/a switch (format->sign) {
504n/a case '+':
505n/a /* always put a + or - */
506n/a spec->n_sign = 1;
507n/a spec->sign = (sign_char == '-' ? '-' : '+');
508n/a break;
509n/a case ' ':
510n/a spec->n_sign = 1;
511n/a spec->sign = (sign_char == '-' ? '-' : ' ');
512n/a break;
513n/a default:
514n/a /* Not specified, or the default (-) */
515n/a if (sign_char == '-') {
516n/a spec->n_sign = 1;
517n/a spec->sign = '-';
518n/a }
519n/a }
520n/a
521n/a /* The number of chars used for non-digits and non-padding. */
522n/a n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
523n/a spec->n_remainder;
524n/a
525n/a /* min_width can go negative, that's okay. format->width == -1 means
526n/a we don't care. */
527n/a if (format->fill_char == '0' && format->align == '=')
528n/a spec->n_min_width = format->width - n_non_digit_non_padding;
529n/a else
530n/a spec->n_min_width = 0;
531n/a
532n/a if (spec->n_digits == 0)
533n/a /* This case only occurs when using 'c' formatting, we need
534n/a to special case it because the grouping code always wants
535n/a to have at least one character. */
536n/a spec->n_grouped_digits = 0;
537n/a else {
538n/a Py_UCS4 grouping_maxchar;
539n/a spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
540n/a NULL, 0,
541n/a 0, NULL,
542n/a spec->n_digits, spec->n_min_width,
543n/a locale->grouping, locale->thousands_sep, &grouping_maxchar);
544n/a *maxchar = Py_MAX(*maxchar, grouping_maxchar);
545n/a }
546n/a
547n/a /* Given the desired width and the total of digit and non-digit
548n/a space we consume, see if we need any padding. format->width can
549n/a be negative (meaning no padding), but this code still works in
550n/a that case. */
551n/a n_padding = format->width -
552n/a (n_non_digit_non_padding + spec->n_grouped_digits);
553n/a if (n_padding > 0) {
554n/a /* Some padding is needed. Determine if it's left, space, or right. */
555n/a switch (format->align) {
556n/a case '<':
557n/a spec->n_rpadding = n_padding;
558n/a break;
559n/a case '^':
560n/a spec->n_lpadding = n_padding / 2;
561n/a spec->n_rpadding = n_padding - spec->n_lpadding;
562n/a break;
563n/a case '=':
564n/a spec->n_spadding = n_padding;
565n/a break;
566n/a case '>':
567n/a spec->n_lpadding = n_padding;
568n/a break;
569n/a default:
570n/a /* Shouldn't get here, but treat it as '>' */
571n/a spec->n_lpadding = n_padding;
572n/a assert(0);
573n/a break;
574n/a }
575n/a }
576n/a
577n/a if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding)
578n/a *maxchar = Py_MAX(*maxchar, format->fill_char);
579n/a
580n/a if (spec->n_decimal)
581n/a *maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point));
582n/a
583n/a return spec->n_lpadding + spec->n_sign + spec->n_prefix +
584n/a spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
585n/a spec->n_remainder + spec->n_rpadding;
586n/a}
587n/a
588n/a/* Fill in the digit parts of a numbers's string representation,
589n/a as determined in calc_number_widths().
590n/a Return -1 on error, or 0 on success. */
591n/astatic int
592n/afill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
593n/a PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end,
594n/a PyObject *prefix, Py_ssize_t p_start,
595n/a Py_UCS4 fill_char,
596n/a LocaleInfo *locale, int toupper)
597n/a{
598n/a /* Used to keep track of digits, decimal, and remainder. */
599n/a Py_ssize_t d_pos = d_start;
600n/a const unsigned int kind = writer->kind;
601n/a const void *data = writer->data;
602n/a Py_ssize_t r;
603n/a
604n/a if (spec->n_lpadding) {
605n/a _PyUnicode_FastFill(writer->buffer,
606n/a writer->pos, spec->n_lpadding, fill_char);
607n/a writer->pos += spec->n_lpadding;
608n/a }
609n/a if (spec->n_sign == 1) {
610n/a PyUnicode_WRITE(kind, data, writer->pos, spec->sign);
611n/a writer->pos++;
612n/a }
613n/a if (spec->n_prefix) {
614n/a _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
615n/a prefix, p_start,
616n/a spec->n_prefix);
617n/a if (toupper) {
618n/a Py_ssize_t t;
619n/a for (t = 0; t < spec->n_prefix; t++) {
620n/a Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
621n/a c = Py_TOUPPER(c);
622n/a assert (c <= 127);
623n/a PyUnicode_WRITE(kind, data, writer->pos + t, c);
624n/a }
625n/a }
626n/a writer->pos += spec->n_prefix;
627n/a }
628n/a if (spec->n_spadding) {
629n/a _PyUnicode_FastFill(writer->buffer,
630n/a writer->pos, spec->n_spadding, fill_char);
631n/a writer->pos += spec->n_spadding;
632n/a }
633n/a
634n/a /* Only for type 'c' special case, it has no digits. */
635n/a if (spec->n_digits != 0) {
636n/a /* Fill the digits with InsertThousandsGrouping. */
637n/a char *pdigits;
638n/a if (PyUnicode_READY(digits))
639n/a return -1;
640n/a pdigits = PyUnicode_DATA(digits);
641n/a if (PyUnicode_KIND(digits) < kind) {
642n/a pdigits = _PyUnicode_AsKind(digits, kind);
643n/a if (pdigits == NULL)
644n/a return -1;
645n/a }
646n/a r = _PyUnicode_InsertThousandsGrouping(
647n/a writer->buffer, writer->pos,
648n/a spec->n_grouped_digits,
649n/a pdigits + kind * d_pos,
650n/a spec->n_digits, spec->n_min_width,
651n/a locale->grouping, locale->thousands_sep, NULL);
652n/a if (r == -1)
653n/a return -1;
654n/a assert(r == spec->n_grouped_digits);
655n/a if (PyUnicode_KIND(digits) < kind)
656n/a PyMem_Free(pdigits);
657n/a d_pos += spec->n_digits;
658n/a }
659n/a if (toupper) {
660n/a Py_ssize_t t;
661n/a for (t = 0; t < spec->n_grouped_digits; t++) {
662n/a Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
663n/a c = Py_TOUPPER(c);
664n/a if (c > 127) {
665n/a PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
666n/a return -1;
667n/a }
668n/a PyUnicode_WRITE(kind, data, writer->pos + t, c);
669n/a }
670n/a }
671n/a writer->pos += spec->n_grouped_digits;
672n/a
673n/a if (spec->n_decimal) {
674n/a _PyUnicode_FastCopyCharacters(
675n/a writer->buffer, writer->pos,
676n/a locale->decimal_point, 0, spec->n_decimal);
677n/a writer->pos += spec->n_decimal;
678n/a d_pos += 1;
679n/a }
680n/a
681n/a if (spec->n_remainder) {
682n/a _PyUnicode_FastCopyCharacters(
683n/a writer->buffer, writer->pos,
684n/a digits, d_pos, spec->n_remainder);
685n/a writer->pos += spec->n_remainder;
686n/a /* d_pos += spec->n_remainder; */
687n/a }
688n/a
689n/a if (spec->n_rpadding) {
690n/a _PyUnicode_FastFill(writer->buffer,
691n/a writer->pos, spec->n_rpadding,
692n/a fill_char);
693n/a writer->pos += spec->n_rpadding;
694n/a }
695n/a return 0;
696n/a}
697n/a
698n/astatic const char no_grouping[1] = {CHAR_MAX};
699n/a
700n/a/* Find the decimal point character(s?), thousands_separator(s?), and
701n/a grouping description, either for the current locale if type is
702n/a LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE or
703n/a LT_UNDERSCORE_LOCALE/LT_UNDER_FOUR_LOCALE, or none if LT_NO_LOCALE. */
704n/astatic int
705n/aget_locale_info(enum LocaleType type, LocaleInfo *locale_info)
706n/a{
707n/a switch (type) {
708n/a case LT_CURRENT_LOCALE: {
709n/a struct lconv *locale_data = localeconv();
710n/a locale_info->decimal_point = PyUnicode_DecodeLocale(
711n/a locale_data->decimal_point,
712n/a NULL);
713n/a if (locale_info->decimal_point == NULL)
714n/a return -1;
715n/a locale_info->thousands_sep = PyUnicode_DecodeLocale(
716n/a locale_data->thousands_sep,
717n/a NULL);
718n/a if (locale_info->thousands_sep == NULL)
719n/a return -1;
720n/a locale_info->grouping = locale_data->grouping;
721n/a break;
722n/a }
723n/a case LT_DEFAULT_LOCALE:
724n/a case LT_UNDERSCORE_LOCALE:
725n/a case LT_UNDER_FOUR_LOCALE:
726n/a locale_info->decimal_point = PyUnicode_FromOrdinal('.');
727n/a locale_info->thousands_sep = PyUnicode_FromOrdinal(
728n/a type == LT_DEFAULT_LOCALE ? ',' : '_');
729n/a if (!locale_info->decimal_point || !locale_info->thousands_sep)
730n/a return -1;
731n/a if (type != LT_UNDER_FOUR_LOCALE)
732n/a locale_info->grouping = "\3"; /* Group every 3 characters. The
733n/a (implicit) trailing 0 means repeat
734n/a infinitely. */
735n/a else
736n/a locale_info->grouping = "\4"; /* Bin/oct/hex group every four. */
737n/a break;
738n/a case LT_NO_LOCALE:
739n/a locale_info->decimal_point = PyUnicode_FromOrdinal('.');
740n/a locale_info->thousands_sep = PyUnicode_New(0, 0);
741n/a if (!locale_info->decimal_point || !locale_info->thousands_sep)
742n/a return -1;
743n/a locale_info->grouping = no_grouping;
744n/a break;
745n/a }
746n/a return 0;
747n/a}
748n/a
749n/astatic void
750n/afree_locale_info(LocaleInfo *locale_info)
751n/a{
752n/a Py_XDECREF(locale_info->decimal_point);
753n/a Py_XDECREF(locale_info->thousands_sep);
754n/a}
755n/a
756n/a/************************************************************************/
757n/a/*********** string formatting ******************************************/
758n/a/************************************************************************/
759n/a
760n/astatic int
761n/aformat_string_internal(PyObject *value, const InternalFormatSpec *format,
762n/a _PyUnicodeWriter *writer)
763n/a{
764n/a Py_ssize_t lpad;
765n/a Py_ssize_t rpad;
766n/a Py_ssize_t total;
767n/a Py_ssize_t len;
768n/a int result = -1;
769n/a Py_UCS4 maxchar;
770n/a
771n/a assert(PyUnicode_IS_READY(value));
772n/a len = PyUnicode_GET_LENGTH(value);
773n/a
774n/a /* sign is not allowed on strings */
775n/a if (format->sign != '\0') {
776n/a PyErr_SetString(PyExc_ValueError,
777n/a "Sign not allowed in string format specifier");
778n/a goto done;
779n/a }
780n/a
781n/a /* alternate is not allowed on strings */
782n/a if (format->alternate) {
783n/a PyErr_SetString(PyExc_ValueError,
784n/a "Alternate form (#) not allowed in string format "
785n/a "specifier");
786n/a goto done;
787n/a }
788n/a
789n/a /* '=' alignment not allowed on strings */
790n/a if (format->align == '=') {
791n/a PyErr_SetString(PyExc_ValueError,
792n/a "'=' alignment not allowed "
793n/a "in string format specifier");
794n/a goto done;
795n/a }
796n/a
797n/a if ((format->width == -1 || format->width <= len)
798n/a && (format->precision == -1 || format->precision >= len)) {
799n/a /* Fast path */
800n/a return _PyUnicodeWriter_WriteStr(writer, value);
801n/a }
802n/a
803n/a /* if precision is specified, output no more that format.precision
804n/a characters */
805n/a if (format->precision >= 0 && len >= format->precision) {
806n/a len = format->precision;
807n/a }
808n/a
809n/a calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
810n/a
811n/a maxchar = writer->maxchar;
812n/a if (lpad != 0 || rpad != 0)
813n/a maxchar = Py_MAX(maxchar, format->fill_char);
814n/a if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
815n/a Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
816n/a maxchar = Py_MAX(maxchar, valmaxchar);
817n/a }
818n/a
819n/a /* allocate the resulting string */
820n/a if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
821n/a goto done;
822n/a
823n/a /* Write into that space. First the padding. */
824n/a result = fill_padding(writer, len, format->fill_char, lpad, rpad);
825n/a if (result == -1)
826n/a goto done;
827n/a
828n/a /* Then the source string. */
829n/a if (len) {
830n/a _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
831n/a value, 0, len);
832n/a }
833n/a writer->pos += (len + rpad);
834n/a result = 0;
835n/a
836n/adone:
837n/a return result;
838n/a}
839n/a
840n/a
841n/a/************************************************************************/
842n/a/*********** long formatting ********************************************/
843n/a/************************************************************************/
844n/a
845n/astatic int
846n/aformat_long_internal(PyObject *value, const InternalFormatSpec *format,
847n/a _PyUnicodeWriter *writer)
848n/a{
849n/a int result = -1;
850n/a Py_UCS4 maxchar = 127;
851n/a PyObject *tmp = NULL;
852n/a Py_ssize_t inumeric_chars;
853n/a Py_UCS4 sign_char = '\0';
854n/a Py_ssize_t n_digits; /* count of digits need from the computed
855n/a string */
856n/a Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
857n/a produces non-digits */
858n/a Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
859n/a Py_ssize_t n_total;
860n/a Py_ssize_t prefix = 0;
861n/a NumberFieldWidths spec;
862n/a long x;
863n/a
864n/a /* Locale settings, either from the actual locale or
865n/a from a hard-code pseudo-locale */
866n/a LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
867n/a
868n/a /* no precision allowed on integers */
869n/a if (format->precision != -1) {
870n/a PyErr_SetString(PyExc_ValueError,
871n/a "Precision not allowed in integer format specifier");
872n/a goto done;
873n/a }
874n/a
875n/a /* special case for character formatting */
876n/a if (format->type == 'c') {
877n/a /* error to specify a sign */
878n/a if (format->sign != '\0') {
879n/a PyErr_SetString(PyExc_ValueError,
880n/a "Sign not allowed with integer"
881n/a " format specifier 'c'");
882n/a goto done;
883n/a }
884n/a /* error to request alternate format */
885n/a if (format->alternate) {
886n/a PyErr_SetString(PyExc_ValueError,
887n/a "Alternate form (#) not allowed with integer"
888n/a " format specifier 'c'");
889n/a goto done;
890n/a }
891n/a
892n/a /* taken from unicodeobject.c formatchar() */
893n/a /* Integer input truncated to a character */
894n/a x = PyLong_AsLong(value);
895n/a if (x == -1 && PyErr_Occurred())
896n/a goto done;
897n/a if (x < 0 || x > 0x10ffff) {
898n/a PyErr_SetString(PyExc_OverflowError,
899n/a "%c arg not in range(0x110000)");
900n/a goto done;
901n/a }
902n/a tmp = PyUnicode_FromOrdinal(x);
903n/a inumeric_chars = 0;
904n/a n_digits = 1;
905n/a maxchar = Py_MAX(maxchar, (Py_UCS4)x);
906n/a
907n/a /* As a sort-of hack, we tell calc_number_widths that we only
908n/a have "remainder" characters. calc_number_widths thinks
909n/a these are characters that don't get formatted, only copied
910n/a into the output string. We do this for 'c' formatting,
911n/a because the characters are likely to be non-digits. */
912n/a n_remainder = 1;
913n/a }
914n/a else {
915n/a int base;
916n/a int leading_chars_to_skip = 0; /* Number of characters added by
917n/a PyNumber_ToBase that we want to
918n/a skip over. */
919n/a
920n/a /* Compute the base and how many characters will be added by
921n/a PyNumber_ToBase */
922n/a switch (format->type) {
923n/a case 'b':
924n/a base = 2;
925n/a leading_chars_to_skip = 2; /* 0b */
926n/a break;
927n/a case 'o':
928n/a base = 8;
929n/a leading_chars_to_skip = 2; /* 0o */
930n/a break;
931n/a case 'x':
932n/a case 'X':
933n/a base = 16;
934n/a leading_chars_to_skip = 2; /* 0x */
935n/a break;
936n/a default: /* shouldn't be needed, but stops a compiler warning */
937n/a case 'd':
938n/a case 'n':
939n/a base = 10;
940n/a break;
941n/a }
942n/a
943n/a if (format->sign != '+' && format->sign != ' '
944n/a && format->width == -1
945n/a && format->type != 'X' && format->type != 'n'
946n/a && !format->thousands_separators
947n/a && PyLong_CheckExact(value))
948n/a {
949n/a /* Fast path */
950n/a return _PyLong_FormatWriter(writer, value, base, format->alternate);
951n/a }
952n/a
953n/a /* The number of prefix chars is the same as the leading
954n/a chars to skip */
955n/a if (format->alternate)
956n/a n_prefix = leading_chars_to_skip;
957n/a
958n/a /* Do the hard part, converting to a string in a given base */
959n/a tmp = _PyLong_Format(value, base);
960n/a if (tmp == NULL || PyUnicode_READY(tmp) == -1)
961n/a goto done;
962n/a
963n/a inumeric_chars = 0;
964n/a n_digits = PyUnicode_GET_LENGTH(tmp);
965n/a
966n/a prefix = inumeric_chars;
967n/a
968n/a /* Is a sign character present in the output? If so, remember it
969n/a and skip it */
970n/a if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
971n/a sign_char = '-';
972n/a ++prefix;
973n/a ++leading_chars_to_skip;
974n/a }
975n/a
976n/a /* Skip over the leading chars (0x, 0b, etc.) */
977n/a n_digits -= leading_chars_to_skip;
978n/a inumeric_chars += leading_chars_to_skip;
979n/a }
980n/a
981n/a /* Determine the grouping, separator, and decimal point, if any. */
982n/a if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
983n/a format->thousands_separators,
984n/a &locale) == -1)
985n/a goto done;
986n/a
987n/a /* Calculate how much memory we'll need. */
988n/a n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars,
989n/a inumeric_chars + n_digits, n_remainder, 0,
990n/a &locale, format, &maxchar);
991n/a
992n/a /* Allocate the memory. */
993n/a if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
994n/a goto done;
995n/a
996n/a /* Populate the memory. */
997n/a result = fill_number(writer, &spec,
998n/a tmp, inumeric_chars, inumeric_chars + n_digits,
999n/a tmp, prefix, format->fill_char,
1000n/a &locale, format->type == 'X');
1001n/a
1002n/adone:
1003n/a Py_XDECREF(tmp);
1004n/a free_locale_info(&locale);
1005n/a return result;
1006n/a}
1007n/a
1008n/a/************************************************************************/
1009n/a/*********** float formatting *******************************************/
1010n/a/************************************************************************/
1011n/a
1012n/a/* much of this is taken from unicodeobject.c */
1013n/astatic int
1014n/aformat_float_internal(PyObject *value,
1015n/a const InternalFormatSpec *format,
1016n/a _PyUnicodeWriter *writer)
1017n/a{
1018n/a char *buf = NULL; /* buffer returned from PyOS_double_to_string */
1019n/a Py_ssize_t n_digits;
1020n/a Py_ssize_t n_remainder;
1021n/a Py_ssize_t n_total;
1022n/a int has_decimal;
1023n/a double val;
1024n/a int precision, default_precision = 6;
1025n/a Py_UCS4 type = format->type;
1026n/a int add_pct = 0;
1027n/a Py_ssize_t index;
1028n/a NumberFieldWidths spec;
1029n/a int flags = 0;
1030n/a int result = -1;
1031n/a Py_UCS4 maxchar = 127;
1032n/a Py_UCS4 sign_char = '\0';
1033n/a int float_type; /* Used to see if we have a nan, inf, or regular float. */
1034n/a PyObject *unicode_tmp = NULL;
1035n/a
1036n/a /* Locale settings, either from the actual locale or
1037n/a from a hard-code pseudo-locale */
1038n/a LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
1039n/a
1040n/a if (format->precision > INT_MAX) {
1041n/a PyErr_SetString(PyExc_ValueError, "precision too big");
1042n/a goto done;
1043n/a }
1044n/a precision = (int)format->precision;
1045n/a
1046n/a if (format->alternate)
1047n/a flags |= Py_DTSF_ALT;
1048n/a
1049n/a if (type == '\0') {
1050n/a /* Omitted type specifier. Behaves in the same way as repr(x)
1051n/a and str(x) if no precision is given, else like 'g', but with
1052n/a at least one digit after the decimal point. */
1053n/a flags |= Py_DTSF_ADD_DOT_0;
1054n/a type = 'r';
1055n/a default_precision = 0;
1056n/a }
1057n/a
1058n/a if (type == 'n')
1059n/a /* 'n' is the same as 'g', except for the locale used to
1060n/a format the result. We take care of that later. */
1061n/a type = 'g';
1062n/a
1063n/a val = PyFloat_AsDouble(value);
1064n/a if (val == -1.0 && PyErr_Occurred())
1065n/a goto done;
1066n/a
1067n/a if (type == '%') {
1068n/a type = 'f';
1069n/a val *= 100;
1070n/a add_pct = 1;
1071n/a }
1072n/a
1073n/a if (precision < 0)
1074n/a precision = default_precision;
1075n/a else if (type == 'r')
1076n/a type = 'g';
1077n/a
1078n/a /* Cast "type", because if we're in unicode we need to pass an
1079n/a 8-bit char. This is safe, because we've restricted what "type"
1080n/a can be. */
1081n/a buf = PyOS_double_to_string(val, (char)type, precision, flags,
1082n/a &float_type);
1083n/a if (buf == NULL)
1084n/a goto done;
1085n/a n_digits = strlen(buf);
1086n/a
1087n/a if (add_pct) {
1088n/a /* We know that buf has a trailing zero (since we just called
1089n/a strlen() on it), and we don't use that fact any more. So we
1090n/a can just write over the trailing zero. */
1091n/a buf[n_digits] = '%';
1092n/a n_digits += 1;
1093n/a }
1094n/a
1095n/a if (format->sign != '+' && format->sign != ' '
1096n/a && format->width == -1
1097n/a && format->type != 'n'
1098n/a && !format->thousands_separators)
1099n/a {
1100n/a /* Fast path */
1101n/a result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits);
1102n/a PyMem_Free(buf);
1103n/a return result;
1104n/a }
1105n/a
1106n/a /* Since there is no unicode version of PyOS_double_to_string,
1107n/a just use the 8 bit version and then convert to unicode. */
1108n/a unicode_tmp = _PyUnicode_FromASCII(buf, n_digits);
1109n/a PyMem_Free(buf);
1110n/a if (unicode_tmp == NULL)
1111n/a goto done;
1112n/a
1113n/a /* Is a sign character present in the output? If so, remember it
1114n/a and skip it */
1115n/a index = 0;
1116n/a if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') {
1117n/a sign_char = '-';
1118n/a ++index;
1119n/a --n_digits;
1120n/a }
1121n/a
1122n/a /* Determine if we have any "remainder" (after the digits, might include
1123n/a decimal or exponent or both (or neither)) */
1124n/a parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal);
1125n/a
1126n/a /* Determine the grouping, separator, and decimal point, if any. */
1127n/a if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1128n/a format->thousands_separators,
1129n/a &locale) == -1)
1130n/a goto done;
1131n/a
1132n/a /* Calculate how much memory we'll need. */
1133n/a n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
1134n/a index + n_digits, n_remainder, has_decimal,
1135n/a &locale, format, &maxchar);
1136n/a
1137n/a /* Allocate the memory. */
1138n/a if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
1139n/a goto done;
1140n/a
1141n/a /* Populate the memory. */
1142n/a result = fill_number(writer, &spec,
1143n/a unicode_tmp, index, index + n_digits,
1144n/a NULL, 0, format->fill_char,
1145n/a &locale, 0);
1146n/a
1147n/adone:
1148n/a Py_XDECREF(unicode_tmp);
1149n/a free_locale_info(&locale);
1150n/a return result;
1151n/a}
1152n/a
1153n/a/************************************************************************/
1154n/a/*********** complex formatting *****************************************/
1155n/a/************************************************************************/
1156n/a
1157n/astatic int
1158n/aformat_complex_internal(PyObject *value,
1159n/a const InternalFormatSpec *format,
1160n/a _PyUnicodeWriter *writer)
1161n/a{
1162n/a double re;
1163n/a double im;
1164n/a char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1165n/a char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1166n/a
1167n/a InternalFormatSpec tmp_format = *format;
1168n/a Py_ssize_t n_re_digits;
1169n/a Py_ssize_t n_im_digits;
1170n/a Py_ssize_t n_re_remainder;
1171n/a Py_ssize_t n_im_remainder;
1172n/a Py_ssize_t n_re_total;
1173n/a Py_ssize_t n_im_total;
1174n/a int re_has_decimal;
1175n/a int im_has_decimal;
1176n/a int precision, default_precision = 6;
1177n/a Py_UCS4 type = format->type;
1178n/a Py_ssize_t i_re;
1179n/a Py_ssize_t i_im;
1180n/a NumberFieldWidths re_spec;
1181n/a NumberFieldWidths im_spec;
1182n/a int flags = 0;
1183n/a int result = -1;
1184n/a Py_UCS4 maxchar = 127;
1185n/a enum PyUnicode_Kind rkind;
1186n/a void *rdata;
1187n/a Py_UCS4 re_sign_char = '\0';
1188n/a Py_UCS4 im_sign_char = '\0';
1189n/a int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1190n/a int im_float_type;
1191n/a int add_parens = 0;
1192n/a int skip_re = 0;
1193n/a Py_ssize_t lpad;
1194n/a Py_ssize_t rpad;
1195n/a Py_ssize_t total;
1196n/a PyObject *re_unicode_tmp = NULL;
1197n/a PyObject *im_unicode_tmp = NULL;
1198n/a
1199n/a /* Locale settings, either from the actual locale or
1200n/a from a hard-code pseudo-locale */
1201n/a LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
1202n/a
1203n/a if (format->precision > INT_MAX) {
1204n/a PyErr_SetString(PyExc_ValueError, "precision too big");
1205n/a goto done;
1206n/a }
1207n/a precision = (int)format->precision;
1208n/a
1209n/a /* Zero padding is not allowed. */
1210n/a if (format->fill_char == '0') {
1211n/a PyErr_SetString(PyExc_ValueError,
1212n/a "Zero padding is not allowed in complex format "
1213n/a "specifier");
1214n/a goto done;
1215n/a }
1216n/a
1217n/a /* Neither is '=' alignment . */
1218n/a if (format->align == '=') {
1219n/a PyErr_SetString(PyExc_ValueError,
1220n/a "'=' alignment flag is not allowed in complex format "
1221n/a "specifier");
1222n/a goto done;
1223n/a }
1224n/a
1225n/a re = PyComplex_RealAsDouble(value);
1226n/a if (re == -1.0 && PyErr_Occurred())
1227n/a goto done;
1228n/a im = PyComplex_ImagAsDouble(value);
1229n/a if (im == -1.0 && PyErr_Occurred())
1230n/a goto done;
1231n/a
1232n/a if (format->alternate)
1233n/a flags |= Py_DTSF_ALT;
1234n/a
1235n/a if (type == '\0') {
1236n/a /* Omitted type specifier. Should be like str(self). */
1237n/a type = 'r';
1238n/a default_precision = 0;
1239n/a if (re == 0.0 && copysign(1.0, re) == 1.0)
1240n/a skip_re = 1;
1241n/a else
1242n/a add_parens = 1;
1243n/a }
1244n/a
1245n/a if (type == 'n')
1246n/a /* 'n' is the same as 'g', except for the locale used to
1247n/a format the result. We take care of that later. */
1248n/a type = 'g';
1249n/a
1250n/a if (precision < 0)
1251n/a precision = default_precision;
1252n/a else if (type == 'r')
1253n/a type = 'g';
1254n/a
1255n/a /* Cast "type", because if we're in unicode we need to pass an
1256n/a 8-bit char. This is safe, because we've restricted what "type"
1257n/a can be. */
1258n/a re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1259n/a &re_float_type);
1260n/a if (re_buf == NULL)
1261n/a goto done;
1262n/a im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1263n/a &im_float_type);
1264n/a if (im_buf == NULL)
1265n/a goto done;
1266n/a
1267n/a n_re_digits = strlen(re_buf);
1268n/a n_im_digits = strlen(im_buf);
1269n/a
1270n/a /* Since there is no unicode version of PyOS_double_to_string,
1271n/a just use the 8 bit version and then convert to unicode. */
1272n/a re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits);
1273n/a if (re_unicode_tmp == NULL)
1274n/a goto done;
1275n/a i_re = 0;
1276n/a
1277n/a im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits);
1278n/a if (im_unicode_tmp == NULL)
1279n/a goto done;
1280n/a i_im = 0;
1281n/a
1282n/a /* Is a sign character present in the output? If so, remember it
1283n/a and skip it */
1284n/a if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') {
1285n/a re_sign_char = '-';
1286n/a ++i_re;
1287n/a --n_re_digits;
1288n/a }
1289n/a if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') {
1290n/a im_sign_char = '-';
1291n/a ++i_im;
1292n/a --n_im_digits;
1293n/a }
1294n/a
1295n/a /* Determine if we have any "remainder" (after the digits, might include
1296n/a decimal or exponent or both (or neither)) */
1297n/a parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
1298n/a &n_re_remainder, &re_has_decimal);
1299n/a parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
1300n/a &n_im_remainder, &im_has_decimal);
1301n/a
1302n/a /* Determine the grouping, separator, and decimal point, if any. */
1303n/a if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1304n/a format->thousands_separators,
1305n/a &locale) == -1)
1306n/a goto done;
1307n/a
1308n/a /* Turn off any padding. We'll do it later after we've composed
1309n/a the numbers without padding. */
1310n/a tmp_format.fill_char = '\0';
1311n/a tmp_format.align = '<';
1312n/a tmp_format.width = -1;
1313n/a
1314n/a /* Calculate how much memory we'll need. */
1315n/a n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp,
1316n/a i_re, i_re + n_re_digits, n_re_remainder,
1317n/a re_has_decimal, &locale, &tmp_format,
1318n/a &maxchar);
1319n/a
1320n/a /* Same formatting, but always include a sign, unless the real part is
1321n/a * going to be omitted, in which case we use whatever sign convention was
1322n/a * requested by the original format. */
1323n/a if (!skip_re)
1324n/a tmp_format.sign = '+';
1325n/a n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp,
1326n/a i_im, i_im + n_im_digits, n_im_remainder,
1327n/a im_has_decimal, &locale, &tmp_format,
1328n/a &maxchar);
1329n/a
1330n/a if (skip_re)
1331n/a n_re_total = 0;
1332n/a
1333n/a /* Add 1 for the 'j', and optionally 2 for parens. */
1334n/a calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1335n/a format->width, format->align, &lpad, &rpad, &total);
1336n/a
1337n/a if (lpad || rpad)
1338n/a maxchar = Py_MAX(maxchar, format->fill_char);
1339n/a
1340n/a if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
1341n/a goto done;
1342n/a rkind = writer->kind;
1343n/a rdata = writer->data;
1344n/a
1345n/a /* Populate the memory. First, the padding. */
1346n/a result = fill_padding(writer,
1347n/a n_re_total + n_im_total + 1 + add_parens * 2,
1348n/a format->fill_char, lpad, rpad);
1349n/a if (result == -1)
1350n/a goto done;
1351n/a
1352n/a if (add_parens) {
1353n/a PyUnicode_WRITE(rkind, rdata, writer->pos, '(');
1354n/a writer->pos++;
1355n/a }
1356n/a
1357n/a if (!skip_re) {
1358n/a result = fill_number(writer, &re_spec,
1359n/a re_unicode_tmp, i_re, i_re + n_re_digits,
1360n/a NULL, 0,
1361n/a 0,
1362n/a &locale, 0);
1363n/a if (result == -1)
1364n/a goto done;
1365n/a }
1366n/a result = fill_number(writer, &im_spec,
1367n/a im_unicode_tmp, i_im, i_im + n_im_digits,
1368n/a NULL, 0,
1369n/a 0,
1370n/a &locale, 0);
1371n/a if (result == -1)
1372n/a goto done;
1373n/a PyUnicode_WRITE(rkind, rdata, writer->pos, 'j');
1374n/a writer->pos++;
1375n/a
1376n/a if (add_parens) {
1377n/a PyUnicode_WRITE(rkind, rdata, writer->pos, ')');
1378n/a writer->pos++;
1379n/a }
1380n/a
1381n/a writer->pos += rpad;
1382n/a
1383n/adone:
1384n/a PyMem_Free(re_buf);
1385n/a PyMem_Free(im_buf);
1386n/a Py_XDECREF(re_unicode_tmp);
1387n/a Py_XDECREF(im_unicode_tmp);
1388n/a free_locale_info(&locale);
1389n/a return result;
1390n/a}
1391n/a
1392n/a/************************************************************************/
1393n/a/*********** built in formatters ****************************************/
1394n/a/************************************************************************/
1395n/astatic int
1396n/aformat_obj(PyObject *obj, _PyUnicodeWriter *writer)
1397n/a{
1398n/a PyObject *str;
1399n/a int err;
1400n/a
1401n/a str = PyObject_Str(obj);
1402n/a if (str == NULL)
1403n/a return -1;
1404n/a err = _PyUnicodeWriter_WriteStr(writer, str);
1405n/a Py_DECREF(str);
1406n/a return err;
1407n/a}
1408n/a
1409n/aint
1410n/a_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1411n/a PyObject *obj,
1412n/a PyObject *format_spec,
1413n/a Py_ssize_t start, Py_ssize_t end)
1414n/a{
1415n/a InternalFormatSpec format;
1416n/a
1417n/a assert(PyUnicode_Check(obj));
1418n/a
1419n/a /* check for the special case of zero length format spec, make
1420n/a it equivalent to str(obj) */
1421n/a if (start == end) {
1422n/a if (PyUnicode_CheckExact(obj))
1423n/a return _PyUnicodeWriter_WriteStr(writer, obj);
1424n/a else
1425n/a return format_obj(obj, writer);
1426n/a }
1427n/a
1428n/a /* parse the format_spec */
1429n/a if (!parse_internal_render_format_spec(format_spec, start, end,
1430n/a &format, 's', '<'))
1431n/a return -1;
1432n/a
1433n/a /* type conversion? */
1434n/a switch (format.type) {
1435n/a case 's':
1436n/a /* no type conversion needed, already a string. do the formatting */
1437n/a return format_string_internal(obj, &format, writer);
1438n/a default:
1439n/a /* unknown */
1440n/a unknown_presentation_type(format.type, obj->ob_type->tp_name);
1441n/a return -1;
1442n/a }
1443n/a}
1444n/a
1445n/aint
1446n/a_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1447n/a PyObject *obj,
1448n/a PyObject *format_spec,
1449n/a Py_ssize_t start, Py_ssize_t end)
1450n/a{
1451n/a PyObject *tmp = NULL, *str = NULL;
1452n/a InternalFormatSpec format;
1453n/a int result = -1;
1454n/a
1455n/a /* check for the special case of zero length format spec, make
1456n/a it equivalent to str(obj) */
1457n/a if (start == end) {
1458n/a if (PyLong_CheckExact(obj))
1459n/a return _PyLong_FormatWriter(writer, obj, 10, 0);
1460n/a else
1461n/a return format_obj(obj, writer);
1462n/a }
1463n/a
1464n/a /* parse the format_spec */
1465n/a if (!parse_internal_render_format_spec(format_spec, start, end,
1466n/a &format, 'd', '>'))
1467n/a goto done;
1468n/a
1469n/a /* type conversion? */
1470n/a switch (format.type) {
1471n/a case 'b':
1472n/a case 'c':
1473n/a case 'd':
1474n/a case 'o':
1475n/a case 'x':
1476n/a case 'X':
1477n/a case 'n':
1478n/a /* no type conversion needed, already an int. do the formatting */
1479n/a result = format_long_internal(obj, &format, writer);
1480n/a break;
1481n/a
1482n/a case 'e':
1483n/a case 'E':
1484n/a case 'f':
1485n/a case 'F':
1486n/a case 'g':
1487n/a case 'G':
1488n/a case '%':
1489n/a /* convert to float */
1490n/a tmp = PyNumber_Float(obj);
1491n/a if (tmp == NULL)
1492n/a goto done;
1493n/a result = format_float_internal(tmp, &format, writer);
1494n/a break;
1495n/a
1496n/a default:
1497n/a /* unknown */
1498n/a unknown_presentation_type(format.type, obj->ob_type->tp_name);
1499n/a goto done;
1500n/a }
1501n/a
1502n/adone:
1503n/a Py_XDECREF(tmp);
1504n/a Py_XDECREF(str);
1505n/a return result;
1506n/a}
1507n/a
1508n/aint
1509n/a_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1510n/a PyObject *obj,
1511n/a PyObject *format_spec,
1512n/a Py_ssize_t start, Py_ssize_t end)
1513n/a{
1514n/a InternalFormatSpec format;
1515n/a
1516n/a /* check for the special case of zero length format spec, make
1517n/a it equivalent to str(obj) */
1518n/a if (start == end)
1519n/a return format_obj(obj, writer);
1520n/a
1521n/a /* parse the format_spec */
1522n/a if (!parse_internal_render_format_spec(format_spec, start, end,
1523n/a &format, '\0', '>'))
1524n/a return -1;
1525n/a
1526n/a /* type conversion? */
1527n/a switch (format.type) {
1528n/a case '\0': /* No format code: like 'g', but with at least one decimal. */
1529n/a case 'e':
1530n/a case 'E':
1531n/a case 'f':
1532n/a case 'F':
1533n/a case 'g':
1534n/a case 'G':
1535n/a case 'n':
1536n/a case '%':
1537n/a /* no conversion, already a float. do the formatting */
1538n/a return format_float_internal(obj, &format, writer);
1539n/a
1540n/a default:
1541n/a /* unknown */
1542n/a unknown_presentation_type(format.type, obj->ob_type->tp_name);
1543n/a return -1;
1544n/a }
1545n/a}
1546n/a
1547n/aint
1548n/a_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1549n/a PyObject *obj,
1550n/a PyObject *format_spec,
1551n/a Py_ssize_t start, Py_ssize_t end)
1552n/a{
1553n/a InternalFormatSpec format;
1554n/a
1555n/a /* check for the special case of zero length format spec, make
1556n/a it equivalent to str(obj) */
1557n/a if (start == end)
1558n/a return format_obj(obj, writer);
1559n/a
1560n/a /* parse the format_spec */
1561n/a if (!parse_internal_render_format_spec(format_spec, start, end,
1562n/a &format, '\0', '>'))
1563n/a return -1;
1564n/a
1565n/a /* type conversion? */
1566n/a switch (format.type) {
1567n/a case '\0': /* No format code: like 'g', but with at least one decimal. */
1568n/a case 'e':
1569n/a case 'E':
1570n/a case 'f':
1571n/a case 'F':
1572n/a case 'g':
1573n/a case 'G':
1574n/a case 'n':
1575n/a /* no conversion, already a complex. do the formatting */
1576n/a return format_complex_internal(obj, &format, writer);
1577n/a
1578n/a default:
1579n/a /* unknown */
1580n/a unknown_presentation_type(format.type, obj->ob_type->tp_name);
1581n/a return -1;
1582n/a }
1583n/a}