LCOV - code coverage report
Current view: top level - Objects/stringlib - formatter.h (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 157 532 29.5 %
Date: 2017-04-19 Functions: 12 25 48.0 %

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

Generated by: LCOV version 1.10