Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
13 edited
1 copied

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Objects/stringlib/README.txt

    r2 r391  
    1414STRINGLIB_EMPTY
    1515
    16     a PyObject representing the empty string
    17 
    18 int STRINGLIB_CMP(STRINGLIB_CHAR*, STRINGLIB_CHAR*, Py_ssize_t)
    19 
    20     compares two strings. returns 0 if they match, and non-zero if not.
     16    a PyObject representing the empty string, only to be used if
     17    STRINGLIB_MUTABLE is 0
    2118
    2219Py_ssize_t STRINGLIB_LEN(PyObject*)
     
    3330    returns the pointer to the character data for the given string
    3431    object (which must be of the right type)
     32
     33int STRINGLIB_CHECK_EXACT(PyObject *)
     34
     35    returns true if the object is an instance of our type, not a subclass
     36
     37STRINGLIB_MUTABLE
     38
     39    must be 0 or 1 to tell the cpp macros in stringlib code if the object
     40    being operated on is mutable or not
  • python/trunk/Objects/stringlib/count.h

    r2 r391  
    1010Py_LOCAL_INLINE(Py_ssize_t)
    1111stringlib_count(const STRINGLIB_CHAR* str, Py_ssize_t str_len,
    12                 const STRINGLIB_CHAR* sub, Py_ssize_t sub_len)
     12                const STRINGLIB_CHAR* sub, Py_ssize_t sub_len,
     13                Py_ssize_t maxcount)
    1314{
    1415    Py_ssize_t count;
     
    1718        return 0; /* start > len(str) */
    1819    if (sub_len == 0)
    19         return str_len + 1;
     20        return (str_len < maxcount) ? str_len + 1 : maxcount;
    2021
    21     count = fastsearch(str, str_len, sub, sub_len, FAST_COUNT);
     22    count = fastsearch(str, str_len, sub, sub_len, maxcount, FAST_COUNT);
    2223
    2324    if (count < 0)
    24         count = 0; /* no match */
     25        return 0; /* no match */
    2526
    2627    return count;
     
    2829
    2930#endif
    30 
    31 /*
    32 Local variables:
    33 c-basic-offset: 4
    34 indent-tabs-mode: nil
    35 End:
    36 */
  • python/trunk/Objects/stringlib/ctype.h

    r2 r391  
    108108    return newobj;
    109109}
    110 
  • python/trunk/Objects/stringlib/fastsearch.h

    r2 r391  
    66/* fast search/count implementation, based on a mix between boyer-
    77   moore and horspool, with a few more bells and whistles on the top.
    8    for some more background, see: http://effbot.org/stringlib */
     8   for some more background, see: http://effbot.org/zone/stringlib.htm */
    99
    1010/* note: fastsearch may access s[n], which isn't a problem when using
     
    1717#define FAST_COUNT 0
    1818#define FAST_SEARCH 1
     19#define FAST_RSEARCH 2
     20
     21#if LONG_BIT >= 128
     22#define STRINGLIB_BLOOM_WIDTH 128
     23#elif LONG_BIT >= 64
     24#define STRINGLIB_BLOOM_WIDTH 64
     25#elif LONG_BIT >= 32
     26#define STRINGLIB_BLOOM_WIDTH 32
     27#else
     28#error "LONG_BIT is smaller than 32"
     29#endif
     30
     31#define STRINGLIB_BLOOM_ADD(mask, ch) \
     32    ((mask |= (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
     33#define STRINGLIB_BLOOM(mask, ch)     \
     34    ((mask &  (1UL << ((ch) & (STRINGLIB_BLOOM_WIDTH -1)))))
    1935
    2036Py_LOCAL_INLINE(Py_ssize_t)
    2137fastsearch(const STRINGLIB_CHAR* s, Py_ssize_t n,
    2238           const STRINGLIB_CHAR* p, Py_ssize_t m,
    23            int mode)
     39           Py_ssize_t maxcount, int mode)
    2440{
    25     long mask;
     41    unsigned long mask;
    2642    Py_ssize_t skip, count = 0;
    2743    Py_ssize_t i, j, mlast, w;
     
    2945    w = n - m;
    3046
    31     if (w < 0)
     47    if (w < 0 || (mode == FAST_COUNT && maxcount == 0))
    3248        return -1;
    3349
     
    3955        if (mode == FAST_COUNT) {
    4056            for (i = 0; i < n; i++)
     57                if (s[i] == p[0]) {
     58                    count++;
     59                    if (count == maxcount)
     60                        return maxcount;
     61                }
     62            return count;
     63        } else if (mode == FAST_SEARCH) {
     64            for (i = 0; i < n; i++)
    4165                if (s[i] == p[0])
    42                     count++;
    43             return count;
    44         } else {
    45             for (i = 0; i < n; i++)
     66                    return i;
     67        } else {    /* FAST_RSEARCH */
     68            for (i = n - 1; i > -1; i--)
    4669                if (s[i] == p[0])
    4770                    return i;
     
    5174
    5275    mlast = m - 1;
     76    skip = mlast - 1;
     77    mask = 0;
    5378
    54     /* create compressed boyer-moore delta 1 table */
    55     skip = mlast - 1;
    56     /* process pattern[:-1] */
    57     for (mask = i = 0; i < mlast; i++) {
    58         mask |= (1 << (p[i] & 0x1F));
    59         if (p[i] == p[mlast])
    60             skip = mlast - i - 1;
    61     }
    62     /* process pattern[-1] outside the loop */
    63     mask |= (1 << (p[mlast] & 0x1F));
     79    if (mode != FAST_RSEARCH) {
    6480
    65     for (i = 0; i <= w; i++) {
    66         /* note: using mlast in the skip path slows things down on x86 */
    67         if (s[i+m-1] == p[m-1]) {
    68             /* candidate match */
    69             for (j = 0; j < mlast; j++)
    70                 if (s[i+j] != p[j])
    71                     break;
    72             if (j == mlast) {
    73                 /* got a match! */
    74                 if (mode != FAST_COUNT)
     81        /* create compressed boyer-moore delta 1 table */
     82
     83        /* process pattern[:-1] */
     84        for (i = 0; i < mlast; i++) {
     85            STRINGLIB_BLOOM_ADD(mask, p[i]);
     86            if (p[i] == p[mlast])
     87                skip = mlast - i - 1;
     88        }
     89        /* process pattern[-1] outside the loop */
     90        STRINGLIB_BLOOM_ADD(mask, p[mlast]);
     91
     92        for (i = 0; i <= w; i++) {
     93            /* note: using mlast in the skip path slows things down on x86 */
     94            if (s[i+m-1] == p[m-1]) {
     95                /* candidate match */
     96                for (j = 0; j < mlast; j++)
     97                    if (s[i+j] != p[j])
     98                        break;
     99                if (j == mlast) {
     100                    /* got a match! */
     101                    if (mode != FAST_COUNT)
     102                        return i;
     103                    count++;
     104                    if (count == maxcount)
     105                        return maxcount;
     106                    i = i + mlast;
     107                    continue;
     108                }
     109                /* miss: check if next character is part of pattern */
     110                if (!STRINGLIB_BLOOM(mask, s[i+m]))
     111                    i = i + m;
     112                else
     113                    i = i + skip;
     114            } else {
     115                /* skip: check if next character is part of pattern */
     116                if (!STRINGLIB_BLOOM(mask, s[i+m]))
     117                    i = i + m;
     118            }
     119        }
     120    } else {    /* FAST_RSEARCH */
     121
     122        /* create compressed boyer-moore delta 1 table */
     123
     124        /* process pattern[0] outside the loop */
     125        STRINGLIB_BLOOM_ADD(mask, p[0]);
     126        /* process pattern[:0:-1] */
     127        for (i = mlast; i > 0; i--) {
     128            STRINGLIB_BLOOM_ADD(mask, p[i]);
     129            if (p[i] == p[0])
     130                skip = i - 1;
     131        }
     132
     133        for (i = w; i >= 0; i--) {
     134            if (s[i] == p[0]) {
     135                /* candidate match */
     136                for (j = mlast; j > 0; j--)
     137                    if (s[i+j] != p[j])
     138                        break;
     139                if (j == 0)
     140                    /* got a match! */
    75141                    return i;
    76                 count++;
    77                 i = i + mlast;
    78                 continue;
     142                /* miss: check if previous character is part of pattern */
     143                if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1]))
     144                    i = i - m;
     145                else
     146                    i = i - skip;
     147            } else {
     148                /* skip: check if previous character is part of pattern */
     149                if (i > 0 && !STRINGLIB_BLOOM(mask, s[i-1]))
     150                    i = i - m;
    79151            }
    80             /* miss: check if next character is part of pattern */
    81             if (!(mask & (1 << (s[i+m] & 0x1F))))
    82                 i = i + m;
    83             else
    84                 i = i + skip;
    85         } else {
    86             /* skip: check if next character is part of pattern */
    87             if (!(mask & (1 << (s[i+m] & 0x1F))))
    88                 i = i + m;
    89152        }
    90153    }
     
    96159
    97160#endif
    98 
    99 /*
    100 Local variables:
    101 c-basic-offset: 4
    102 indent-tabs-mode: nil
    103 End:
    104 */
  • python/trunk/Objects/stringlib/find.h

    r2 r391  
    2020        return offset;
    2121
    22     pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);
     22    pos = fastsearch(str, str_len, sub, sub_len, -1, FAST_SEARCH);
    2323
    2424    if (pos >= 0)
     
    3333                Py_ssize_t offset)
    3434{
    35     /* XXX - create reversefastsearch helper! */
    36     if (sub_len == 0) {
    37         if (str_len < 0)
    38             return -1;
    39         return str_len + offset;
    40     } else {
    41         Py_ssize_t j, pos = -1;
    42         for (j = str_len - sub_len; j >= 0; --j)
    43             if (STRINGLIB_CMP(str+j, sub, sub_len) == 0) {
    44                 pos = j + offset;
    45                 break;
    46             }
    47         return pos;
     35    Py_ssize_t pos;
     36
     37    if (str_len < 0)
     38        return -1;
     39    if (sub_len == 0)
     40        return str_len + offset;
     41
     42    pos = fastsearch(str, str_len, sub, sub_len, -1, FAST_RSEARCH);
     43
     44    if (pos >= 0)
     45        pos += offset;
     46
     47    return pos;
     48}
     49
     50/* helper macro to fixup start/end slice values */
     51#define ADJUST_INDICES(start, end, len)         \
     52    if (end > len)                              \
     53        end = len;                              \
     54    else if (end < 0) {                         \
     55        end += len;                             \
     56        if (end < 0)                            \
     57            end = 0;                            \
     58    }                                           \
     59    if (start < 0) {                            \
     60        start += len;                           \
     61        if (start < 0)                          \
     62            start = 0;                          \
    4863    }
    49 }
    5064
    5165Py_LOCAL_INLINE(Py_ssize_t)
     
    5468                     Py_ssize_t start, Py_ssize_t end)
    5569{
    56     if (start < 0)
    57         start += str_len;
    58     if (start < 0)
    59         start = 0;
    60     if (end > str_len)
    61         end = str_len;
    62     if (end < 0)
    63         end += str_len;
    64     if (end < 0)
    65         end = 0;
    66 
    67     return stringlib_find(
    68         str + start, end - start,
    69         sub, sub_len, start
    70         );
     70    ADJUST_INDICES(start, end, str_len);
     71    return stringlib_find(str + start, end - start, sub, sub_len, start);
    7172}
    7273
     
    7677                      Py_ssize_t start, Py_ssize_t end)
    7778{
    78     if (start < 0)
    79         start += str_len;
    80     if (start < 0)
    81         start = 0;
    82     if (end > str_len)
    83         end = str_len;
    84     if (end < 0)
    85         end += str_len;
    86     if (end < 0)
    87         end = 0;
    88 
     79    ADJUST_INDICES(start, end, str_len);
    8980    return stringlib_rfind(str + start, end - start, sub, sub_len, start);
    9081}
    9182
    92 #if defined(STRINGLIB_STR) && !defined(FROM_BYTEARRAY)
     83#ifdef STRINGLIB_WANT_CONTAINS_OBJ
    9384
    9485Py_LOCAL_INLINE(int)
     
    10192}
    10293
    103 #endif /* STRINGLIB_STR */
    104 
    105 #ifdef FROM_UNICODE
     94#endif /* STRINGLIB_WANT_CONTAINS_OBJ */
    10695
    10796/*
    10897This function is a helper for the "find" family (find, rfind, index,
    109 rindex) of unicodeobject.c file, because they all have the same
    110 behaviour for the arguments.
     98rindex) and for count, startswith and endswith, because they all have
     99the same behaviour for the arguments.
    111100
    112101It does not touch the variables received until it knows everything
    113102is ok.
    114 
    115 Note that we receive a pointer to the pointer of the substring object,
    116 so when we create that object in this function we don't DECREF it,
    117 because it continues living in the caller functions (those functions,
    118 after finishing using the substring, must DECREF it).
    119103*/
    120104
     105#define FORMAT_BUFFER_SIZE 50
     106
    121107Py_LOCAL_INLINE(int)
    122 _ParseTupleFinds (PyObject *args, PyObject **substring,
    123                   Py_ssize_t *start, Py_ssize_t *end) {
    124     PyObject *tmp_substring;
     108stringlib_parse_args_finds(const char * function_name, PyObject *args,
     109                           PyObject **subobj,
     110                           Py_ssize_t *start, Py_ssize_t *end)
     111{
     112    PyObject *tmp_subobj;
    125113    Py_ssize_t tmp_start = 0;
    126114    Py_ssize_t tmp_end = PY_SSIZE_T_MAX;
    127115    PyObject *obj_start=Py_None, *obj_end=Py_None;
     116    char format[FORMAT_BUFFER_SIZE] = "O|OO:";
     117    size_t len = strlen(format);
    128118
    129     if (!PyArg_ParseTuple(args, "O|OO:find", &tmp_substring,
    130          &obj_start, &obj_end))
     119    strncpy(format + len, function_name, FORMAT_BUFFER_SIZE - len - 1);
     120    format[FORMAT_BUFFER_SIZE - 1] = '\0';
     121
     122    if (!PyArg_ParseTuple(args, format, &tmp_subobj, &obj_start, &obj_end))
    131123        return 0;
    132124
     
    141133            return 0;
    142134
    143     tmp_substring = PyUnicode_FromObject(tmp_substring);
    144     if (!tmp_substring)
    145         return 0;
    146 
    147135    *start = tmp_start;
    148136    *end = tmp_end;
    149     *substring = tmp_substring;
     137    *subobj = tmp_subobj;
    150138    return 1;
    151139}
    152140
    153 #endif /* FROM_UNICODE */
     141#undef FORMAT_BUFFER_SIZE
     142
     143#if STRINGLIB_IS_UNICODE
     144
     145/*
     146Wraps stringlib_parse_args_finds() and additionally ensures that the
     147first argument is a unicode object.
     148
     149Note that we receive a pointer to the pointer of the substring object,
     150so when we create that object in this function we don't DECREF it,
     151because it continues living in the caller functions (those functions,
     152after finishing using the substring, must DECREF it).
     153*/
     154
     155Py_LOCAL_INLINE(int)
     156stringlib_parse_args_finds_unicode(const char * function_name, PyObject *args,
     157                                   PyUnicodeObject **substring,
     158                                   Py_ssize_t *start, Py_ssize_t *end)
     159{
     160    PyObject *tmp_substring;
     161
     162    if(stringlib_parse_args_finds(function_name, args, &tmp_substring,
     163                                  start, end)) {
     164        tmp_substring = PyUnicode_FromObject(tmp_substring);
     165        if (!tmp_substring)
     166            return 0;
     167        *substring = (PyUnicodeObject *)tmp_substring;
     168        return 1;
     169    }
     170    return 0;
     171}
     172
     173#endif /* STRINGLIB_IS_UNICODE */
    154174
    155175#endif /* STRINGLIB_FIND_H */
    156 
    157 /*
    158 Local variables:
    159 c-basic-offset: 4
    160 indent-tabs-mode: nil
    161 End:
    162 */
  • python/trunk/Objects/stringlib/formatter.h

    r2 r391  
    11/* implements the string, long, and float formatters.  that is,
    22   string.__format__, etc. */
     3
     4#include <locale.h>
    35
    46/* Before including this, you must include either:
     
    1012   FORMAT_LONG
    1113   FORMAT_FLOAT
     14   FORMAT_COMPLEX
    1215   to be whatever you want the public names of these functions to
    1316   be.  These are the only non-static functions defined here.
    1417*/
    15 
    16 #define ALLOW_PARENS_FOR_SIGN 0
    1718
    1819/* Raises an exception about an unknown presentation type for this
     
    4445}
    4546
     47static void
     48invalid_comma_type(STRINGLIB_CHAR presentation_type)
     49{
     50#if STRINGLIB_IS_UNICODE
     51    /* See comment in unknown_presentation_type */
     52    if (presentation_type > 32 && presentation_type < 128)
     53#endif
     54        PyErr_Format(PyExc_ValueError,
     55                     "Cannot specify ',' with '%c'.",
     56                     (char)presentation_type);
     57#if STRINGLIB_IS_UNICODE
     58    else
     59        PyErr_Format(PyExc_ValueError,
     60                     "Cannot specify ',' with '\\x%x'.",
     61                     (unsigned int)presentation_type);
     62#endif
     63}
     64
    4665/*
    4766    get_integer consumes 0 or more decimal digit characters from an
     
    5574                  Py_ssize_t *result)
    5675{
    57     Py_ssize_t accumulator, digitval, oldaccumulator;
     76    Py_ssize_t accumulator, digitval;
    5877    int numdigits;
    5978    accumulator = numdigits = 0;
     
    6584            break;
    6685        /*
    67            This trick was copied from old Unicode format code.  It's cute,
    68            but would really suck on an old machine with a slow divide
    69            implementation.  Fortunately, in the normal case we do not
    70            expect too many digits.
     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.
    7190        */
    72         oldaccumulator = accumulator;
    73         accumulator *= 10;
    74         if ((accumulator+10)/10 != oldaccumulator+1) {
     91        if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
    7592            PyErr_Format(PyExc_ValueError,
    7693                         "Too many decimal digits in format string");
    7794            return -1;
    7895        }
    79         accumulator += digitval;
     96        accumulator = accumulator * 10 + digitval;
    8097    }
    8198    *result = accumulator;
     
    105122    switch (c) {
    106123    case ' ': case '+': case '-':
    107 #if ALLOW_PARENS_FOR_SIGN
    108     case '(':
    109 #endif
    110124        return 1;
    111125    default:
     
    121135    STRINGLIB_CHAR sign;
    122136    Py_ssize_t width;
     137    int thousands_separators;
    123138    Py_ssize_t precision;
    124139    STRINGLIB_CHAR type;
    125140} InternalFormatSpec;
     141
     142
     143#if 0
     144/* Occassionally useful for debugging. Should normally be commented out. */
     145static void
     146DEBUG_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
    126161
    127162/*
     
    133168static int
    134169parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
    135                                   Py_ssize_t format_spec_len,
     170                                  Py_ssize_t format_spec_len,
    136171                                  InternalFormatSpec *format,
    137                                   char default_type)
     172                                  char default_type,
     173                                  char default_align)
    138174{
    139175    STRINGLIB_CHAR *ptr = format_spec;
     
    143179       the input string */
    144180
    145     Py_ssize_t specified_width;
     181    Py_ssize_t consumed;
     182    int align_specified = 0;
    146183
    147184    format->fill_char = '\0';
    148     format->align = '\0';
     185    format->align = default_align;
    149186    format->alternate = 0;
    150187    format->sign = '\0';
    151188    format->width = -1;
     189    format->thousands_separators = 0;
    152190    format->precision = -1;
    153191    format->type = default_type;
     
    158196        format->align = ptr[1];
    159197        format->fill_char = ptr[0];
     198        align_specified = 1;
    160199        ptr += 2;
    161200    }
    162201    else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
    163202        format->align = ptr[0];
     203        align_specified = 1;
    164204        ++ptr;
    165205    }
     
    169209        format->sign = ptr[0];
    170210        ++ptr;
    171 #if ALLOW_PARENS_FOR_SIGN
    172         if (end-ptr >= 1 && ptr[0] == ')') {
    173             ++ptr;
    174         }
    175 #endif
    176211    }
    177212
     
    179214       applies to integers. */
    180215    if (end-ptr >= 1 && ptr[0] == '#') {
    181         format->alternate = 1;
    182         ++ptr;
     216        format->alternate = 1;
     217        ++ptr;
    183218    }
    184219
     
    186221    if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
    187222        format->fill_char = '0';
    188         if (format->align == '\0') {
     223        if (!align_specified) {
    189224            format->align = '=';
    190225        }
     
    192227    }
    193228
    194     /* XXX add error checking */
    195     specified_width = get_integer(&ptr, end, &format->width);
    196 
    197     /* if specified_width is 0, we didn't consume any characters for
    198        the width. in that case, reset the width to -1, because
    199        get_integer() will have set it to zero */
    200     if (specified_width == 0) {
     229    consumed = get_integer(&ptr, end, &format->width);
     230    if (consumed == -1)
     231        /* Overflow error. Exception already set. */
     232        return 0;
     233
     234    /* If consumed is 0, we didn't consume any characters for the
     235       width. In that case, reset the width to -1, because
     236       get_integer() will have set it to zero. -1 is how we record
     237       that the width wasn't specified. */
     238    if (consumed == 0)
    201239        format->width = -1;
     240
     241    /* Comma signifies add thousands separators */
     242    if (end-ptr && ptr[0] == ',') {
     243        format->thousands_separators = 1;
     244        ++ptr;
    202245    }
    203246
     
    206249        ++ptr;
    207250
    208         /* XXX add error checking */
    209         specified_width = get_integer(&ptr, end, &format->precision);
    210 
    211         /* not having a precision after a dot is an error */
    212         if (specified_width == 0) {
     251        consumed = get_integer(&ptr, end, &format->precision);
     252        if (consumed == -1)
     253            /* Overflow error. Exception already set. */
     254            return 0;
     255
     256        /* Not having a precision after a dot is an error. */
     257        if (consumed == 0) {
    213258            PyErr_Format(PyExc_ValueError,
    214259                         "Format specifier missing precision");
     
    218263    }
    219264
    220     /* Finally, parse the type field */
     265    /* Finally, parse the type field. */
    221266
    222267    if (end-ptr > 1) {
    223         /* invalid conversion spec */
     268        /* More than one char remain, invalid conversion spec. */
    224269        PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
    225270        return 0;
     
    231276    }
    232277
     278    /* Do as much validating as we can, just by looking at the format
     279       specifier.  Do not take into account what type of formatting
     280       we're doing (int, float, string). */
     281
     282    if (format->thousands_separators) {
     283        switch (format->type) {
     284        case 'd':
     285        case 'e':
     286        case 'f':
     287        case 'g':
     288        case 'E':
     289        case 'G':
     290        case '%':
     291        case 'F':
     292        case '\0':
     293            /* These are allowed. See PEP 378.*/
     294            break;
     295        default:
     296            invalid_comma_type(format->type);
     297            return 0;
     298        }
     299    }
     300
    233301    return 1;
    234302}
    235303
    236 #if defined FORMAT_FLOAT || defined FORMAT_LONG
     304/* Calculate the padding needed. */
     305static void
     306calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
     307             Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
     308             Py_ssize_t *n_total)
     309{
     310    if (width >= 0) {
     311        if (nchars > width)
     312            *n_total = nchars;
     313        else
     314            *n_total = width;
     315    }
     316    else {
     317        /* not specified, use all of the chars and no more */
     318        *n_total = nchars;
     319    }
     320
     321    /* Figure out how much leading space we need, based on the
     322       aligning */
     323    if (align == '>')
     324        *n_lpadding = *n_total - nchars;
     325    else if (align == '^')
     326        *n_lpadding = (*n_total - nchars) / 2;
     327    else if (align == '<' || align == '=')
     328        *n_lpadding = 0;
     329    else {
     330        /* We should never have an unspecified alignment. */
     331        *n_lpadding = 0;
     332        assert(0);
     333    }
     334
     335    *n_rpadding = *n_total - nchars - *n_lpadding;
     336}
     337
     338/* Do the padding, and return a pointer to where the caller-supplied
     339   content goes. */
     340static STRINGLIB_CHAR *
     341fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
     342             Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
     343{
     344    /* Pad on left. */
     345    if (n_lpadding)
     346        STRINGLIB_FILL(p, fill_char, n_lpadding);
     347
     348    /* Pad on right. */
     349    if (n_rpadding)
     350        STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
     351
     352    /* Pointer to the user content. */
     353    return p + n_lpadding;
     354}
     355
     356#if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
    237357/************************************************************************/
    238358/*********** common routines for numeric formatting *********************/
    239359/************************************************************************/
     360
     361/* Locale type codes. */
     362#define LT_CURRENT_LOCALE 0
     363#define LT_DEFAULT_LOCALE 1
     364#define LT_NO_LOCALE 2
     365
     366/* Locale info needed for formatting integers and the part of floats
     367   before and including the decimal. Note that locales only support
     368   8-bit chars, not unicode. */
     369typedef struct {
     370    char *decimal_point;
     371    char *thousands_sep;
     372    char *grouping;
     373} LocaleInfo;
    240374
    241375/* describes the layout for an integer, see the comment in
     
    246380    Py_ssize_t n_spadding;
    247381    Py_ssize_t n_rpadding;
    248     char lsign;
    249     Py_ssize_t n_lsign;
    250     char rsign;
    251     Py_ssize_t n_rsign;
    252     Py_ssize_t n_total; /* just a convenience, it's derivable from the
    253                            other fields */
     382    char sign;
     383    Py_ssize_t n_sign;      /* number of digits needed for sign (0/1) */
     384    Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
     385                                    any grouping chars. */
     386    Py_ssize_t n_decimal;   /* 0 if only an integer */
     387    Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
     388                               excluding the decimal itself, if
     389                               present. */
     390
     391    /* These 2 are not the widths of fields, but are needed by
     392       STRINGLIB_GROUPING. */
     393    Py_ssize_t n_digits;    /* The number of digits before a decimal
     394                               or exponent. */
     395    Py_ssize_t n_min_width; /* The min_width we used when we computed
     396                               the n_grouped_digits width. */
    254397} NumberFieldWidths;
     398
     399
     400/* Given a number of the form:
     401   digits[remainder]
     402   where ptr points to the start and end points to the end, find where
     403    the integer part ends. This could be a decimal, an exponent, both,
     404    or neither.
     405   If a decimal point is present, set *has_decimal and increment
     406    remainder beyond it.
     407   Results are undefined (but shouldn't crash) for improperly
     408    formatted strings.
     409*/
     410static void
     411parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
     412             Py_ssize_t *n_remainder, int *has_decimal)
     413{
     414    STRINGLIB_CHAR *end = ptr + len;
     415    STRINGLIB_CHAR *remainder;
     416
     417    while (ptr<end && isdigit(*ptr))
     418        ++ptr;
     419    remainder = ptr;
     420
     421    /* Does remainder start with a decimal point? */
     422    *has_decimal = ptr<end && *remainder == '.';
     423
     424    /* Skip the decimal point. */
     425    if (*has_decimal)
     426        remainder++;
     427
     428    *n_remainder = end - remainder;
     429}
    255430
    256431/* not all fields of format are used.  for example, precision is
     
    258433   about what it does?  or is passing a single format parameter easier
    259434   and more efficient enough to justify a little obfuscation? */
    260 static void
    261 calc_number_widths(NumberFieldWidths *spec, STRINGLIB_CHAR actual_sign,
    262                    Py_ssize_t n_prefix, Py_ssize_t n_digits,
    263                    const InternalFormatSpec *format)
    264 {
     435static Py_ssize_t
     436calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
     437                   STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
     438                   Py_ssize_t n_number, Py_ssize_t n_remainder,
     439                   int has_decimal, const LocaleInfo *locale,
     440                   const InternalFormatSpec *format)
     441{
     442    Py_ssize_t n_non_digit_non_padding;
     443    Py_ssize_t n_padding;
     444
     445    spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
    265446    spec->n_lpadding = 0;
    266     spec->n_prefix = 0;
     447    spec->n_prefix = n_prefix;
     448    spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
     449    spec->n_remainder = n_remainder;
    267450    spec->n_spadding = 0;
    268451    spec->n_rpadding = 0;
    269     spec->lsign = '\0';
    270     spec->n_lsign = 0;
    271     spec->rsign = '\0';
    272     spec->n_rsign = 0;
     452    spec->sign = '\0';
     453    spec->n_sign = 0;
    273454
    274455    /* the output will look like:
    275        |                                                                    |
    276        | <lpadding> <lsign> <prefix> <spadding> <digits> <rsign> <rpadding> |
    277        |                                                                    |
    278 
    279        lsign and rsign are computed from format->sign and the actual
     456       |                                                                                         |
     457       | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
     458       |                                                                                         |
     459
     460       sign is computed from format->sign and the actual
    280461       sign of the number
    281462
     
    292473
    293474    /* compute the various parts we're going to write */
    294     if (format->sign == '+') {
     475    switch (format->sign) {
     476    case '+':
    295477        /* always put a + or - */
    296         spec->n_lsign = 1;
    297         spec->lsign = (actual_sign == '-' ? '-' : '+');
    298     }
    299 #if ALLOW_PARENS_FOR_SIGN
    300     else if (format->sign == '(') {
    301         if (actual_sign == '-') {
    302             spec->n_lsign = 1;
    303             spec->lsign = '(';
    304             spec->n_rsign = 1;
    305             spec->rsign = ')';
     478        spec->n_sign = 1;
     479        spec->sign = (sign_char == '-' ? '-' : '+');
     480        break;
     481    case ' ':
     482        spec->n_sign = 1;
     483        spec->sign = (sign_char == '-' ? '-' : ' ');
     484        break;
     485    default:
     486        /* Not specified, or the default (-) */
     487        if (sign_char == '-') {
     488            spec->n_sign = 1;
     489            spec->sign = '-';
    306490        }
    307491    }
    308 #endif
    309     else if (format->sign == ' ') {
    310         spec->n_lsign = 1;
    311         spec->lsign = (actual_sign == '-' ? '-' : ' ');
    312     }
    313     else {
    314         /* non specified, or the default (-) */
    315         if (actual_sign == '-') {
    316             spec->n_lsign = 1;
    317             spec->lsign = '-';
     492
     493    /* The number of chars used for non-digits and non-padding. */
     494    n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
     495        spec->n_remainder;
     496
     497    /* min_width can go negative, that's okay. format->width == -1 means
     498       we don't care. */
     499    if (format->fill_char == '0' && format->align == '=')
     500        spec->n_min_width = format->width - n_non_digit_non_padding;
     501    else
     502        spec->n_min_width = 0;
     503
     504    if (spec->n_digits == 0)
     505        /* This case only occurs when using 'c' formatting, we need
     506           to special case it because the grouping code always wants
     507           to have at least one character. */
     508        spec->n_grouped_digits = 0;
     509    else
     510        spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
     511                                                    spec->n_digits,
     512                                                    spec->n_min_width,
     513                                                    locale->grouping,
     514                                                    locale->thousands_sep);
     515
     516    /* Given the desired width and the total of digit and non-digit
     517       space we consume, see if we need any padding. format->width can
     518       be negative (meaning no padding), but this code still works in
     519       that case. */
     520    n_padding = format->width -
     521                        (n_non_digit_non_padding + spec->n_grouped_digits);
     522    if (n_padding > 0) {
     523        /* Some padding is needed. Determine if it's left, space, or right. */
     524        switch (format->align) {
     525        case '<':
     526            spec->n_rpadding = n_padding;
     527            break;
     528        case '^':
     529            spec->n_lpadding = n_padding / 2;
     530            spec->n_rpadding = n_padding - spec->n_lpadding;
     531            break;
     532        case '=':
     533            spec->n_spadding = n_padding;
     534            break;
     535        case '>':
     536            spec->n_lpadding = n_padding;
     537            break;
     538        default:
     539            /* Shouldn't get here, but treat it as '>' */
     540            spec->n_lpadding = n_padding;
     541            assert(0);
     542            break;
    318543        }
    319544    }
    320 
    321     spec->n_prefix = n_prefix;
    322 
    323     /* now the number of padding characters */
    324     if (format->width == -1) {
    325         /* no padding at all, nothing to do */
    326     }
    327     else {
    328         /* see if any padding is needed */
    329         if (spec->n_lsign + n_digits + spec->n_rsign +
    330                 spec->n_prefix >= format->width) {
    331             /* no padding needed, we're already bigger than the
    332                requested width */
     545    return spec->n_lpadding + spec->n_sign + spec->n_prefix +
     546        spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
     547        spec->n_remainder + spec->n_rpadding;
     548}
     549
     550/* Fill in the digit parts of a numbers's string representation,
     551   as determined in calc_number_widths().
     552   No error checking, since we know the buffer is the correct size. */
     553static void
     554fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
     555            STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
     556            STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
     557            LocaleInfo *locale, int toupper)
     558{
     559    /* Used to keep track of digits, decimal, and remainder. */
     560    STRINGLIB_CHAR *p = digits;
     561
     562#ifndef NDEBUG
     563    Py_ssize_t r;
     564#endif
     565
     566    if (spec->n_lpadding) {
     567        STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
     568        buf += spec->n_lpadding;
     569    }
     570    if (spec->n_sign == 1) {
     571        *buf++ = spec->sign;
     572    }
     573    if (spec->n_prefix) {
     574        memmove(buf,
     575                prefix,
     576                spec->n_prefix * sizeof(STRINGLIB_CHAR));
     577        if (toupper) {
     578            Py_ssize_t t;
     579            for (t = 0; t < spec->n_prefix; ++t)
     580                buf[t] = STRINGLIB_TOUPPER(buf[t]);
    333581        }
    334         else {
    335             /* determine which of left, space, or right padding is
    336                needed */
    337             Py_ssize_t padding = format->width -
    338                                     (spec->n_lsign + spec->n_prefix +
    339                                      n_digits + spec->n_rsign);
    340             if (format->align == '<')
    341                 spec->n_rpadding = padding;
    342             else if (format->align == '>')
    343                 spec->n_lpadding = padding;
    344             else if (format->align == '^') {
    345                 spec->n_lpadding = padding / 2;
    346                 spec->n_rpadding = padding - spec->n_lpadding;
    347             }
    348             else if (format->align == '=')
    349                 spec->n_spadding = padding;
    350             else
    351                 spec->n_lpadding = padding;
    352         }
    353     }
    354     spec->n_total = spec->n_lpadding + spec->n_lsign + spec->n_prefix +
    355             spec->n_spadding + n_digits + spec->n_rsign + spec->n_rpadding;
    356 }
    357 
    358 /* fill in the non-digit parts of a numbers's string representation,
    359    as determined in calc_number_widths().  returns the pointer to
    360    where the digits go. */
    361 static STRINGLIB_CHAR *
    362 fill_non_digits(STRINGLIB_CHAR *p_buf, const NumberFieldWidths *spec,
    363                 STRINGLIB_CHAR *prefix, Py_ssize_t n_digits,
    364                 STRINGLIB_CHAR fill_char)
    365 {
    366     STRINGLIB_CHAR *p_digits;
    367 
    368     if (spec->n_lpadding) {
    369         STRINGLIB_FILL(p_buf, fill_char, spec->n_lpadding);
    370         p_buf += spec->n_lpadding;
    371     }
    372     if (spec->n_lsign == 1) {
    373         *p_buf++ = spec->lsign;
    374     }
    375     if (spec->n_prefix) {
    376         memmove(p_buf,
    377                 prefix,
    378                 spec->n_prefix * sizeof(STRINGLIB_CHAR));
    379         p_buf += spec->n_prefix;
     582        buf += spec->n_prefix;
    380583    }
    381584    if (spec->n_spadding) {
    382         STRINGLIB_FILL(p_buf, fill_char, spec->n_spadding);
    383         p_buf += spec->n_spadding;
    384     }
    385     p_digits = p_buf;
    386     p_buf += n_digits;
    387     if (spec->n_rsign == 1) {
    388         *p_buf++ = spec->rsign;
    389     }
     585        STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
     586        buf += spec->n_spadding;
     587    }
     588
     589    /* Only for type 'c' special case, it has no digits. */
     590    if (spec->n_digits != 0) {
     591        /* Fill the digits with InsertThousandsGrouping. */
     592#ifndef NDEBUG
     593        r =
     594#endif
     595            STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
     596                               spec->n_digits, spec->n_min_width,
     597                               locale->grouping, locale->thousands_sep);
     598#ifndef NDEBUG
     599        assert(r == spec->n_grouped_digits);
     600#endif
     601        p += spec->n_digits;
     602    }
     603    if (toupper) {
     604        Py_ssize_t t;
     605        for (t = 0; t < spec->n_grouped_digits; ++t)
     606            buf[t] = STRINGLIB_TOUPPER(buf[t]);
     607    }
     608    buf += spec->n_grouped_digits;
     609
     610    if (spec->n_decimal) {
     611        Py_ssize_t t;
     612        for (t = 0; t < spec->n_decimal; ++t)
     613            buf[t] = locale->decimal_point[t];
     614        buf += spec->n_decimal;
     615        p += 1;
     616    }
     617
     618    if (spec->n_remainder) {
     619        memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
     620        buf += spec->n_remainder;
     621        p += spec->n_remainder;
     622    }
     623
    390624    if (spec->n_rpadding) {
    391         STRINGLIB_FILL(p_buf, fill_char, spec->n_rpadding);
    392         p_buf += spec->n_rpadding;
    393     }
    394     return p_digits;
    395 }
    396 #endif /* FORMAT_FLOAT || FORMAT_LONG */
     625        STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
     626        buf += spec->n_rpadding;
     627    }
     628}
     629
     630static char no_grouping[1] = {CHAR_MAX};
     631
     632/* Find the decimal point character(s?), thousands_separator(s?), and
     633   grouping description, either for the current locale if type is
     634   LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
     635   none if LT_NO_LOCALE. */
     636static void
     637get_locale_info(int type, LocaleInfo *locale_info)
     638{
     639    switch (type) {
     640    case LT_CURRENT_LOCALE: {
     641        struct lconv *locale_data = localeconv();
     642        locale_info->decimal_point = locale_data->decimal_point;
     643        locale_info->thousands_sep = locale_data->thousands_sep;
     644        locale_info->grouping = locale_data->grouping;
     645        break;
     646    }
     647    case LT_DEFAULT_LOCALE:
     648        locale_info->decimal_point = ".";
     649        locale_info->thousands_sep = ",";
     650        locale_info->grouping = "\3"; /* Group every 3 characters.  The
     651                                         (implicit) trailing 0 means repeat
     652                                         infinitely. */
     653        break;
     654    case LT_NO_LOCALE:
     655        locale_info->decimal_point = ".";
     656        locale_info->thousands_sep = "";
     657        locale_info->grouping = no_grouping;
     658        break;
     659    default:
     660        assert(0);
     661    }
     662}
     663
     664#endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
    397665
    398666/************************************************************************/
     
    403671format_string_internal(PyObject *value, const InternalFormatSpec *format)
    404672{
    405     Py_ssize_t width; /* total field width */
    406673    Py_ssize_t lpad;
    407     STRINGLIB_CHAR *dst;
    408     STRINGLIB_CHAR *src = STRINGLIB_STR(value);
     674    Py_ssize_t rpad;
     675    Py_ssize_t total;
     676    STRINGLIB_CHAR *p;
    409677    Py_ssize_t len = STRINGLIB_LEN(value);
    410678    PyObject *result = NULL;
     
    421689        PyErr_SetString(PyExc_ValueError,
    422690                        "Alternate form (#) not allowed in string format "
    423                         "specifier");
     691                        "specifier");
    424692        goto done;
    425693    }
     
    439707    }
    440708
    441     if (format->width >= 0) {
    442         width = format->width;
    443 
    444         /* but use at least len characters */
    445         if (len > width) {
    446             width = len;
    447         }
    448     }
    449     else {
    450         /* not specified, use all of the chars and no more */
    451         width = len;
    452     }
     709    calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
    453710
    454711    /* allocate the resulting string */
    455     result = STRINGLIB_NEW(NULL, width);
     712    result = STRINGLIB_NEW(NULL, total);
    456713    if (result == NULL)
    457714        goto done;
    458715
    459     /* now write into that space */
    460     dst = STRINGLIB_STR(result);
    461 
    462     /* figure out how much leading space we need, based on the
    463        aligning */
    464     if (format->align == '>')
    465         lpad = width - len;
    466     else if (format->align == '^')
    467         lpad = (width - len) / 2;
    468     else
    469         lpad = 0;
    470 
    471     /* if right aligning, increment the destination allow space on the
    472        left */
    473     memcpy(dst + lpad, src, len * sizeof(STRINGLIB_CHAR));
    474 
    475     /* do any padding */
    476     if (width > len) {
    477         STRINGLIB_CHAR fill_char = format->fill_char;
    478         if (fill_char == '\0') {
    479             /* use the default, if not specified */
    480             fill_char = ' ';
    481         }
    482 
    483         /* pad on left */
    484         if (lpad)
    485             STRINGLIB_FILL(dst, fill_char, lpad);
    486 
    487         /* pad on right */
    488         if (width - len - lpad)
    489             STRINGLIB_FILL(dst + len + lpad, fill_char, width - len - lpad);
    490     }
     716    /* Write into that space. First the padding. */
     717    p = fill_padding(STRINGLIB_STR(result), len,
     718                     format->fill_char=='\0'?' ':format->fill_char,
     719                     lpad, rpad);
     720
     721    /* Then the source string. */
     722    memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
    491723
    492724done:
     
    505737static PyObject *
    506738format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
    507                             IntOrLongToString tostring)
     739                            IntOrLongToString tostring)
    508740{
    509741    PyObject *result = NULL;
     
    511743    STRINGLIB_CHAR *pnumeric_chars;
    512744    STRINGLIB_CHAR numeric_char;
    513     STRINGLIB_CHAR sign = '\0';
    514     STRINGLIB_CHAR *p;
     745    STRINGLIB_CHAR sign_char = '\0';
    515746    Py_ssize_t n_digits;       /* count of digits need from the computed
    516747                                  string */
    517     Py_ssize_t n_leading_chars;
    518     Py_ssize_t n_grouping_chars = 0; /* Count of additional chars to
    519                                         allocate, used for 'n'
    520                                         formatting. */
     748    Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
     749                                   produces non-digits */
    521750    Py_ssize_t n_prefix = 0;   /* Count of prefix chars, (e.g., '0x') */
     751    Py_ssize_t n_total;
    522752    STRINGLIB_CHAR *prefix = NULL;
    523753    NumberFieldWidths spec;
    524754    long x;
     755
     756    /* Locale settings, either from the actual locale or
     757       from a hard-code pseudo-locale */
     758    LocaleInfo locale;
    525759
    526760    /* no precision allowed on integers */
     
    530764        goto done;
    531765    }
    532 
    533766
    534767    /* special case for character formatting */
     
    538771            PyErr_SetString(PyExc_ValueError,
    539772                            "Sign not allowed with integer"
     773                            " format specifier 'c'");
     774            goto done;
     775        }
     776
     777        /* Error to specify a comma. */
     778        if (format->thousands_separators) {
     779            PyErr_SetString(PyExc_ValueError,
     780                            "Thousands separators not allowed with integer"
    540781                            " format specifier 'c'");
    541782            goto done;
     
    563804        }
    564805#endif
    565         numeric_char = (STRINGLIB_CHAR)x;
    566         pnumeric_chars = &numeric_char;
     806        numeric_char = (STRINGLIB_CHAR)x;
     807        pnumeric_chars = &numeric_char;
    567808        n_digits = 1;
     809
     810        /* As a sort-of hack, we tell calc_number_widths that we only
     811           have "remainder" characters. calc_number_widths thinks
     812           these are characters that don't get formatted, only copied
     813           into the output string. We do this for 'c' formatting,
     814           because the characters are likely to be non-digits. */
     815        n_remainder = 1;
    568816    }
    569817    else {
    570818        int base;
    571         int leading_chars_to_skip = 0;  /* Number of characters added by
    572                                            PyNumber_ToBase that we want to
    573                                            skip over. */
     819        int leading_chars_to_skip = 0;  /* Number of characters added by
     820                                           PyNumber_ToBase that we want to
     821                                           skip over. */
    574822
    575823        /* Compute the base and how many characters will be added by
     
    578826        case 'b':
    579827            base = 2;
    580             leading_chars_to_skip = 2; /* 0b */
     828            leading_chars_to_skip = 2; /* 0b */
    581829            break;
    582830        case 'o':
    583831            base = 8;
    584             leading_chars_to_skip = 2; /* 0o */
     832            leading_chars_to_skip = 2; /* 0o */
    585833            break;
    586834        case 'x':
    587835        case 'X':
    588836            base = 16;
    589             leading_chars_to_skip = 2; /* 0x */
     837            leading_chars_to_skip = 2; /* 0x */
    590838            break;
    591839        default:  /* shouldn't be needed, but stops a compiler warning */
     
    596844        }
    597845
    598         /* The number of prefix chars is the same as the leading
    599            chars to skip */
    600         if (format->alternate)
    601             n_prefix = leading_chars_to_skip;
     846        /* The number of prefix chars is the same as the leading
     847           chars to skip */
     848        if (format->alternate)
     849            n_prefix = leading_chars_to_skip;
    602850
    603851        /* Do the hard part, converting to a string in a given base */
    604         tmp = tostring(value, base);
     852        tmp = tostring(value, base);
    605853        if (tmp == NULL)
    606854            goto done;
    607855
    608         pnumeric_chars = STRINGLIB_STR(tmp);
     856        pnumeric_chars = STRINGLIB_STR(tmp);
    609857        n_digits = STRINGLIB_LEN(tmp);
    610858
    611         prefix = pnumeric_chars;
    612 
    613         /* Remember not to modify what pnumeric_chars points to.  it
    614            might be interned.  Only modify it after we copy it into a
    615            newly allocated output buffer. */
     859        prefix = pnumeric_chars;
     860
     861        /* Remember not to modify what pnumeric_chars points to.  it
     862           might be interned.  Only modify it after we copy it into a
     863           newly allocated output buffer. */
    616864
    617865        /* Is a sign character present in the output?  If so, remember it
    618866           and skip it */
    619         sign = pnumeric_chars[0];
    620         if (sign == '-') {
    621             ++prefix;
    622             ++leading_chars_to_skip;
     867        if (pnumeric_chars[0] == '-') {
     868            sign_char = pnumeric_chars[0];
     869            ++prefix;
     870            ++leading_chars_to_skip;
    623871        }
    624872
    625         /* Skip over the leading chars (0x, 0b, etc.) */
    626         n_digits -= leading_chars_to_skip;
    627         pnumeric_chars += leading_chars_to_skip;
    628     }
    629 
    630     if (format->type == 'n')
    631             /* Compute how many additional chars we need to allocate
    632                to hold the thousands grouping. */
    633             STRINGLIB_GROUPING(NULL, n_digits, n_digits,
    634                                0, &n_grouping_chars, 0);
    635 
    636     /* Calculate the widths of the various leading and trailing parts */
    637     calc_number_widths(&spec, sign, n_prefix, n_digits + n_grouping_chars,
    638                        format);
    639 
    640     /* Allocate a new string to hold the result */
    641     result = STRINGLIB_NEW(NULL, spec.n_total);
     873        /* Skip over the leading chars (0x, 0b, etc.) */
     874        n_digits -= leading_chars_to_skip;
     875        pnumeric_chars += leading_chars_to_skip;
     876    }
     877
     878    /* Determine the grouping, separator, and decimal point, if any. */
     879    get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
     880                    (format->thousands_separators ?
     881                     LT_DEFAULT_LOCALE :
     882                     LT_NO_LOCALE),
     883                    &locale);
     884
     885    /* Calculate how much memory we'll need. */
     886    n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
     887                       n_digits, n_remainder, 0, &locale, format);
     888
     889    /* Allocate the memory. */
     890    result = STRINGLIB_NEW(NULL, n_total);
    642891    if (!result)
    643         goto done;
    644     p = STRINGLIB_STR(result);
    645 
    646     /* XXX There is too much magic here regarding the internals of
    647        spec and the location of the prefix and digits.  It would be
    648        better if calc_number_widths returned a number of logical
    649        offsets into the buffer, and those were used.  Maybe in a
    650        future code cleanup. */
    651 
    652     /* Fill in the digit parts */
    653     n_leading_chars = spec.n_lpadding + spec.n_lsign +
    654             spec.n_prefix + spec.n_spadding;
    655     memmove(p + n_leading_chars,
    656             pnumeric_chars,
    657             n_digits * sizeof(STRINGLIB_CHAR));
    658 
    659     /* If type is 'X', convert the filled in digits to uppercase */
    660     if (format->type == 'X') {
    661         Py_ssize_t t;
    662         for (t = 0; t < n_digits; ++t)
    663             p[t + n_leading_chars] = STRINGLIB_TOUPPER(p[t + n_leading_chars]);
    664     }
    665 
    666     /* Insert the grouping, if any, after the uppercasing of the digits, so
    667        we can ensure that grouping chars won't be affected. */
    668     if (n_grouping_chars) {
    669             /* We know this can't fail, since we've already
    670                reserved enough space. */
    671             STRINGLIB_CHAR *pstart = p + n_leading_chars;
    672 #ifndef NDEBUG
    673             int r =
    674 #endif
    675                 STRINGLIB_GROUPING(pstart, n_digits, n_digits,
    676                            spec.n_total+n_grouping_chars-n_leading_chars,
    677                            NULL, 0);
    678             assert(r);
    679     }
    680 
    681     /* Fill in the non-digit parts (padding, sign, etc.) */
    682     fill_non_digits(p, &spec, prefix, n_digits + n_grouping_chars,
    683                     format->fill_char == '\0' ? ' ' : format->fill_char);
    684 
    685     /* If type is 'X', uppercase the prefix.  This has to be done after the
    686        prefix is filled in by fill_non_digits */
    687     if (format->type == 'X') {
    688         Py_ssize_t t;
    689         for (t = 0; t < n_prefix; ++t)
    690             p[t + spec.n_lpadding + spec.n_lsign] =
    691                     STRINGLIB_TOUPPER(p[t + spec.n_lpadding + spec.n_lsign]);
    692     }
    693 
     892        goto done;
     893
     894    /* Populate the memory. */
     895    fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
     896                prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
     897                &locale, format->type == 'X');
    694898
    695899done:
     
    705909#ifdef FORMAT_FLOAT
    706910#if STRINGLIB_IS_UNICODE
    707 /* taken from unicodeobject.c */
    708 static Py_ssize_t
    709 strtounicode(Py_UNICODE *buffer, const char *charbuffer)
    710 {
    711     register Py_ssize_t i;
    712     Py_ssize_t len = strlen(charbuffer);
    713     for (i = len - 1; i >= 0; --i)
    714         buffer[i] = (Py_UNICODE) charbuffer[i];
    715 
    716     return len;
    717 }
    718 #endif
    719 
    720 /* see FORMATBUFLEN in unicodeobject.c */
    721 #define FLOAT_FORMATBUFLEN 120
     911static void
     912strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
     913{
     914    Py_ssize_t i;
     915    for (i = 0; i < len; ++i)
     916        buffer[i] = (Py_UNICODE)charbuffer[i];
     917}
     918#endif
    722919
    723920/* much of this is taken from unicodeobject.c */
    724921static PyObject *
    725922format_float_internal(PyObject *value,
    726                       const InternalFormatSpec *format)
    727 {
    728     /* fmt = '%.' + `prec` + `type` + '%%'
    729        worst case length = 2 + 10 (len of INT_MAX) + 1 + 2 = 15 (use 20)*/
    730     char fmt[20];
    731 
    732     /* taken from unicodeobject.c */
    733     /* Worst case length calc to ensure no buffer overrun:
    734 
    735        'g' formats:
    736          fmt = %#.<prec>g
    737          buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp
    738             for any double rep.)
    739          len = 1 + prec + 1 + 2 + 5 = 9 + prec
    740 
    741        'f' formats:
    742          buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50)
    743          len = 1 + 50 + 1 + prec = 52 + prec
    744 
    745        If prec=0 the effective precision is 1 (the leading digit is
    746        always given), therefore increase the length by one.
    747 
    748     */
    749     char charbuf[FLOAT_FORMATBUFLEN];
     923                      const InternalFormatSpec *format)
     924{
     925    char *buf = NULL;       /* buffer returned from PyOS_double_to_string */
    750926    Py_ssize_t n_digits;
    751     double x;
    752     Py_ssize_t precision = format->precision;
    753     PyObject *result = NULL;
    754     STRINGLIB_CHAR sign;
    755     char* trailing = "";
     927    Py_ssize_t n_remainder;
     928    Py_ssize_t n_total;
     929    int has_decimal;
     930    double val;
     931    Py_ssize_t precision;
     932    Py_ssize_t default_precision = 6;
     933    STRINGLIB_CHAR type = format->type;
     934    int add_pct = 0;
    756935    STRINGLIB_CHAR *p;
    757936    NumberFieldWidths spec;
    758     STRINGLIB_CHAR type = format->type;
     937    int flags = 0;
     938    PyObject *result = NULL;
     939    STRINGLIB_CHAR sign_char = '\0';
     940    int float_type; /* Used to see if we have a nan, inf, or regular float. */
    759941
    760942#if STRINGLIB_IS_UNICODE
    761     Py_UNICODE unicodebuf[FLOAT_FORMATBUFLEN];
    762 #endif
    763 
    764     /* alternate is not allowed on floats. */
     943    Py_UNICODE *unicode_tmp = NULL;
     944#endif
     945
     946    /* Locale settings, either from the actual locale or
     947       from a hard-code pseudo-locale */
     948    LocaleInfo locale;
     949
     950    if (format->precision > INT_MAX) {
     951        PyErr_SetString(PyExc_ValueError, "precision too big");
     952        goto done;
     953    }
     954    precision = (int)format->precision;
     955
     956    /* Alternate is not allowed on floats. */
    765957    if (format->alternate) {
    766958        PyErr_SetString(PyExc_ValueError,
    767959                        "Alternate form (#) not allowed in float format "
    768                         "specifier");
    769         goto done;
    770     }
    771 
    772     /* first, do the conversion as 8-bit chars, using the platform's
    773        snprintf.  then, if needed, convert to unicode. */
    774 
    775     /* 'F' is the same as 'f', per the PEP */
    776     if (type == 'F')
    777         type = 'f';
    778 
    779     x = PyFloat_AsDouble(value);
    780 
    781     if (x == -1.0 && PyErr_Occurred())
     960                        "specifier");
     961        goto done;
     962    }
     963
     964    if (type == '\0') {
     965        /* Omitted type specifier. This is like 'g' but with at least one
     966           digit after the decimal point, and different default precision.*/
     967        type = 'g';
     968        default_precision = PyFloat_STR_PRECISION;
     969        flags |= Py_DTSF_ADD_DOT_0;
     970    }
     971
     972    if (type == 'n')
     973        /* 'n' is the same as 'g', except for the locale used to
     974           format the result. We take care of that later. */
     975        type = 'g';
     976
     977    val = PyFloat_AsDouble(value);
     978    if (val == -1.0 && PyErr_Occurred())
    782979        goto done;
    783980
    784981    if (type == '%') {
    785982        type = 'f';
    786         x *= 100;
    787         trailing = "%";
     983        val *= 100;
     984        add_pct = 1;
    788985    }
    789986
    790987    if (precision < 0)
    791         precision = 6;
    792     if (type == 'f' && fabs(x) >= 1e50)
    793         type = 'g';
    794 
    795     /* cast "type", because if we're in unicode we need to pass a
    796        8-bit char.  this is safe, because we've restricted what "type"
    797        can be */
    798     PyOS_snprintf(fmt, sizeof(fmt), "%%.%" PY_FORMAT_SIZE_T "d%c", precision,
    799                   (char)type);
    800 
    801     /* do the actual formatting */
    802     PyOS_ascii_formatd(charbuf, sizeof(charbuf), fmt, x);
    803 
    804     /* adding trailing to fmt with PyOS_snprintf doesn't work, not
    805        sure why.  we'll just concatentate it here, no harm done.  we
    806        know we can't have a buffer overflow from the fmt size
    807        analysis */
    808     strcat(charbuf, trailing);
    809 
    810     /* rather than duplicate the code for snprintf for both unicode
    811        and 8 bit strings, we just use the 8 bit version and then
    812        convert to unicode in a separate code path.  that's probably
    813        the lesser of 2 evils. */
     988        precision = default_precision;
     989
     990    /* Cast "type", because if we're in unicode we need to pass a
     991       8-bit char. This is safe, because we've restricted what "type"
     992       can be. */
     993    buf = PyOS_double_to_string(val, (char)type, precision, flags,
     994                                &float_type);
     995    if (buf == NULL)
     996        goto done;
     997    n_digits = strlen(buf);
     998
     999    if (add_pct) {
     1000        /* We know that buf has a trailing zero (since we just called
     1001           strlen() on it), and we don't use that fact any more. So we
     1002           can just write over the trailing zero. */
     1003        buf[n_digits] = '%';
     1004        n_digits += 1;
     1005    }
     1006
     1007    /* Since there is no unicode version of PyOS_double_to_string,
     1008       just use the 8 bit version and then convert to unicode. */
    8141009#if STRINGLIB_IS_UNICODE
    815     n_digits = strtounicode(unicodebuf, charbuf);
    816     p = unicodebuf;
     1010    unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
     1011    if (unicode_tmp == NULL) {
     1012        PyErr_NoMemory();
     1013        goto done;
     1014    }
     1015    strtounicode(unicode_tmp, buf, n_digits);
     1016    p = unicode_tmp;
    8171017#else
    818     /* compute the length.  I believe this is done because the return
    819        value from snprintf above is unreliable */
    820     n_digits = strlen(charbuf);
    821     p = charbuf;
    822 #endif
    823 
    824     /* is a sign character present in the output?  if so, remember it
     1018    p = buf;
     1019#endif
     1020
     1021    /* Is a sign character present in the output?  If so, remember it
    8251022       and skip it */
    826     sign = p[0];
    827     if (sign == '-') {
     1023    if (*p == '-') {
     1024        sign_char = *p;
    8281025        ++p;
    8291026        --n_digits;
    8301027    }
    8311028
    832     calc_number_widths(&spec, sign, 0, n_digits, format);
    833 
    834     /* allocate a string with enough space */
    835     result = STRINGLIB_NEW(NULL, spec.n_total);
     1029    /* Determine if we have any "remainder" (after the digits, might include
     1030       decimal or exponent or both (or neither)) */
     1031    parse_number(p, n_digits, &n_remainder, &has_decimal);
     1032
     1033    /* Determine the grouping, separator, and decimal point, if any. */
     1034    get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
     1035                    (format->thousands_separators ?
     1036                     LT_DEFAULT_LOCALE :
     1037                     LT_NO_LOCALE),
     1038                    &locale);
     1039
     1040    /* Calculate how much memory we'll need. */
     1041    n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
     1042                                 n_remainder, has_decimal, &locale, format);
     1043
     1044    /* Allocate the memory. */
     1045    result = STRINGLIB_NEW(NULL, n_total);
    8361046    if (result == NULL)
    8371047        goto done;
    8381048
    839     /* Fill in the non-digit parts (padding, sign, etc.) */
    840     fill_non_digits(STRINGLIB_STR(result), &spec, NULL, n_digits,
    841                     format->fill_char == '\0' ? ' ' : format->fill_char);
    842 
    843     /* fill in the digit parts */
    844     memmove(STRINGLIB_STR(result) +
    845                (spec.n_lpadding + spec.n_lsign + spec.n_spadding),
    846             p,
    847             n_digits * sizeof(STRINGLIB_CHAR));
     1049    /* Populate the memory. */
     1050    fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
     1051                format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
     1052                0);
    8481053
    8491054done:
     1055    PyMem_Free(buf);
     1056#if STRINGLIB_IS_UNICODE
     1057    PyMem_Free(unicode_tmp);
     1058#endif
    8501059    return result;
    8511060}
    8521061#endif /* FORMAT_FLOAT */
     1062
     1063/************************************************************************/
     1064/*********** complex formatting *****************************************/
     1065/************************************************************************/
     1066
     1067#ifdef FORMAT_COMPLEX
     1068
     1069static PyObject *
     1070format_complex_internal(PyObject *value,
     1071                        const InternalFormatSpec *format)
     1072{
     1073    double re;
     1074    double im;
     1075    char *re_buf = NULL;       /* buffer returned from PyOS_double_to_string */
     1076    char *im_buf = NULL;       /* buffer returned from PyOS_double_to_string */
     1077
     1078    InternalFormatSpec tmp_format = *format;
     1079    Py_ssize_t n_re_digits;
     1080    Py_ssize_t n_im_digits;
     1081    Py_ssize_t n_re_remainder;
     1082    Py_ssize_t n_im_remainder;
     1083    Py_ssize_t n_re_total;
     1084    Py_ssize_t n_im_total;
     1085    int re_has_decimal;
     1086    int im_has_decimal;
     1087    Py_ssize_t precision;
     1088    Py_ssize_t default_precision = 6;
     1089    STRINGLIB_CHAR type = format->type;
     1090    STRINGLIB_CHAR *p_re;
     1091    STRINGLIB_CHAR *p_im;
     1092    NumberFieldWidths re_spec;
     1093    NumberFieldWidths im_spec;
     1094    int flags = 0;
     1095    PyObject *result = NULL;
     1096    STRINGLIB_CHAR *p;
     1097    STRINGLIB_CHAR re_sign_char = '\0';
     1098    STRINGLIB_CHAR im_sign_char = '\0';
     1099    int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
     1100    int im_float_type;
     1101    int add_parens = 0;
     1102    int skip_re = 0;
     1103    Py_ssize_t lpad;
     1104    Py_ssize_t rpad;
     1105    Py_ssize_t total;
     1106
     1107#if STRINGLIB_IS_UNICODE
     1108    Py_UNICODE *re_unicode_tmp = NULL;
     1109    Py_UNICODE *im_unicode_tmp = NULL;
     1110#endif
     1111
     1112    /* Locale settings, either from the actual locale or
     1113       from a hard-code pseudo-locale */
     1114    LocaleInfo locale;
     1115
     1116    if (format->precision > INT_MAX) {
     1117        PyErr_SetString(PyExc_ValueError, "precision too big");
     1118        goto done;
     1119    }
     1120    precision = (int)format->precision;
     1121
     1122    /* Alternate is not allowed on complex. */
     1123    if (format->alternate) {
     1124        PyErr_SetString(PyExc_ValueError,
     1125                        "Alternate form (#) not allowed in complex format "
     1126                        "specifier");
     1127        goto done;
     1128    }
     1129
     1130    /* Neither is zero pading. */
     1131    if (format->fill_char == '0') {
     1132        PyErr_SetString(PyExc_ValueError,
     1133                        "Zero padding is not allowed in complex format "
     1134                        "specifier");
     1135        goto done;
     1136    }
     1137
     1138    /* Neither is '=' alignment . */
     1139    if (format->align == '=') {
     1140        PyErr_SetString(PyExc_ValueError,
     1141                        "'=' alignment flag is not allowed in complex format "
     1142                        "specifier");
     1143        goto done;
     1144    }
     1145
     1146    re = PyComplex_RealAsDouble(value);
     1147    if (re == -1.0 && PyErr_Occurred())
     1148        goto done;
     1149    im = PyComplex_ImagAsDouble(value);
     1150    if (im == -1.0 && PyErr_Occurred())
     1151        goto done;
     1152
     1153    if (type == '\0') {
     1154        /* Omitted type specifier. Should be like str(self). */
     1155        type = 'g';
     1156        default_precision = PyFloat_STR_PRECISION;
     1157        if (re == 0.0 && copysign(1.0, re) == 1.0)
     1158            skip_re = 1;
     1159        else
     1160            add_parens = 1;
     1161    }
     1162
     1163    if (type == 'n')
     1164        /* 'n' is the same as 'g', except for the locale used to
     1165           format the result. We take care of that later. */
     1166        type = 'g';
     1167
     1168    if (precision < 0)
     1169        precision = default_precision;
     1170
     1171    /* Cast "type", because if we're in unicode we need to pass a
     1172       8-bit char. This is safe, because we've restricted what "type"
     1173       can be. */
     1174    re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
     1175                                   &re_float_type);
     1176    if (re_buf == NULL)
     1177        goto done;
     1178    im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
     1179                                   &im_float_type);
     1180    if (im_buf == NULL)
     1181        goto done;
     1182
     1183    n_re_digits = strlen(re_buf);
     1184    n_im_digits = strlen(im_buf);
     1185
     1186    /* Since there is no unicode version of PyOS_double_to_string,
     1187       just use the 8 bit version and then convert to unicode. */
     1188#if STRINGLIB_IS_UNICODE
     1189    re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
     1190    if (re_unicode_tmp == NULL) {
     1191        PyErr_NoMemory();
     1192        goto done;
     1193    }
     1194    strtounicode(re_unicode_tmp, re_buf, n_re_digits);
     1195    p_re = re_unicode_tmp;
     1196
     1197    im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
     1198    if (im_unicode_tmp == NULL) {
     1199        PyErr_NoMemory();
     1200        goto done;
     1201    }
     1202    strtounicode(im_unicode_tmp, im_buf, n_im_digits);
     1203    p_im = im_unicode_tmp;
     1204#else
     1205    p_re = re_buf;
     1206    p_im = im_buf;
     1207#endif
     1208
     1209    /* Is a sign character present in the output?  If so, remember it
     1210       and skip it */
     1211    if (*p_re == '-') {
     1212        re_sign_char = *p_re;
     1213        ++p_re;
     1214        --n_re_digits;
     1215    }
     1216    if (*p_im == '-') {
     1217        im_sign_char = *p_im;
     1218        ++p_im;
     1219        --n_im_digits;
     1220    }
     1221
     1222    /* Determine if we have any "remainder" (after the digits, might include
     1223       decimal or exponent or both (or neither)) */
     1224    parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
     1225    parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
     1226
     1227    /* Determine the grouping, separator, and decimal point, if any. */
     1228    get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
     1229                    (format->thousands_separators ?
     1230                     LT_DEFAULT_LOCALE :
     1231                     LT_NO_LOCALE),
     1232                    &locale);
     1233
     1234    /* Turn off any padding. We'll do it later after we've composed
     1235       the numbers without padding. */
     1236    tmp_format.fill_char = '\0';
     1237    tmp_format.align = '<';
     1238    tmp_format.width = -1;
     1239
     1240    /* Calculate how much memory we'll need. */
     1241    n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
     1242                                    n_re_digits, n_re_remainder,
     1243                                    re_has_decimal, &locale, &tmp_format);
     1244
     1245    /* Same formatting, but always include a sign, unless the real part is
     1246     * going to be omitted, in which case we use whatever sign convention was
     1247     * requested by the original format. */
     1248    if (!skip_re)
     1249        tmp_format.sign = '+';
     1250    n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
     1251                                    n_im_digits, n_im_remainder,
     1252                                    im_has_decimal, &locale, &tmp_format);
     1253
     1254    if (skip_re)
     1255        n_re_total = 0;
     1256
     1257    /* Add 1 for the 'j', and optionally 2 for parens. */
     1258    calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
     1259                 format->width, format->align, &lpad, &rpad, &total);
     1260
     1261    result = STRINGLIB_NEW(NULL, total);
     1262    if (result == NULL)
     1263        goto done;
     1264
     1265    /* Populate the memory. First, the padding. */
     1266    p = fill_padding(STRINGLIB_STR(result),
     1267                     n_re_total + n_im_total + 1 + add_parens * 2,
     1268                     format->fill_char=='\0' ? ' ' : format->fill_char,
     1269                     lpad, rpad);
     1270
     1271    if (add_parens)
     1272        *p++ = '(';
     1273
     1274    if (!skip_re) {
     1275        fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
     1276        p += n_re_total;
     1277    }
     1278    fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
     1279    p += n_im_total;
     1280    *p++ = 'j';
     1281
     1282    if (add_parens)
     1283        *p++ = ')';
     1284
     1285done:
     1286    PyMem_Free(re_buf);
     1287    PyMem_Free(im_buf);
     1288#if STRINGLIB_IS_UNICODE
     1289    PyMem_Free(re_unicode_tmp);
     1290    PyMem_Free(im_unicode_tmp);
     1291#endif
     1292    return result;
     1293}
     1294#endif /* FORMAT_COMPLEX */
    8531295
    8541296/************************************************************************/
     
    8571299PyObject *
    8581300FORMAT_STRING(PyObject *obj,
    859               STRINGLIB_CHAR *format_spec,
    860               Py_ssize_t format_spec_len)
     1301              STRINGLIB_CHAR *format_spec,
     1302              Py_ssize_t format_spec_len)
    8611303{
    8621304    InternalFormatSpec format;
     
    8721314    /* parse the format_spec */
    8731315    if (!parse_internal_render_format_spec(format_spec, format_spec_len,
    874                                            &format, 's'))
     1316                                           &format, 's', '<'))
    8751317        goto done;
    8761318
     
    8941336static PyObject*
    8951337format_int_or_long(PyObject* obj,
    896                    STRINGLIB_CHAR *format_spec,
    897                    Py_ssize_t format_spec_len,
    898                    IntOrLongToString tostring)
     1338                   STRINGLIB_CHAR *format_spec,
     1339                   Py_ssize_t format_spec_len,
     1340                   IntOrLongToString tostring)
    8991341{
    9001342    PyObject *result = NULL;
     
    9111353    /* parse the format_spec */
    9121354    if (!parse_internal_render_format_spec(format_spec,
    913                                            format_spec_len,
    914                                            &format, 'd'))
     1355                                           format_spec_len,
     1356                                           &format, 'd', '>'))
    9151357        goto done;
    9161358
     
    9251367    case 'n':
    9261368        /* no type conversion needed, already an int (or long).  do
    927            the formatting */
    928             result = format_int_or_long_internal(obj, &format, tostring);
     1369           the formatting */
     1370            result = format_int_or_long_internal(obj, &format, tostring);
    9291371        break;
    9301372
     
    9751417PyObject *
    9761418FORMAT_LONG(PyObject *obj,
    977             STRINGLIB_CHAR *format_spec,
    978             Py_ssize_t format_spec_len)
     1419            STRINGLIB_CHAR *format_spec,
     1420            Py_ssize_t format_spec_len)
    9791421{
    9801422    return format_int_or_long(obj, format_spec, format_spec_len,
    981                               long_format);
     1423                              long_format);
    9821424}
    9831425#endif /* FORMAT_LONG */
     
    9961438PyObject *
    9971439FORMAT_INT(PyObject *obj,
    998            STRINGLIB_CHAR *format_spec,
    999            Py_ssize_t format_spec_len)
     1440           STRINGLIB_CHAR *format_spec,
     1441           Py_ssize_t format_spec_len)
    10001442{
    10011443    return format_int_or_long(obj, format_spec, format_spec_len,
    1002                               int_format);
     1444                              int_format);
    10031445}
    10041446#endif /* FORMAT_INT */
     
    10071449PyObject *
    10081450FORMAT_FLOAT(PyObject *obj,
    1009              STRINGLIB_CHAR *format_spec,
    1010              Py_ssize_t format_spec_len)
     1451             STRINGLIB_CHAR *format_spec,
     1452             Py_ssize_t format_spec_len)
    10111453{
    10121454    PyObject *result = NULL;
     
    10221464    /* parse the format_spec */
    10231465    if (!parse_internal_render_format_spec(format_spec,
    1024                                            format_spec_len,
    1025                                            &format, '\0'))
     1466                                           format_spec_len,
     1467                                           &format, '\0', '>'))
    10261468        goto done;
    10271469
    10281470    /* type conversion? */
    10291471    switch (format.type) {
    1030     case '\0':
    1031         /* 'Z' means like 'g', but with at least one decimal.  See
    1032            PyOS_ascii_formatd */
    1033         format.type = 'Z';
    1034         /* Deliberate fall through to the next case statement */
     1472    case '\0': /* No format code: like 'g', but with at least one decimal. */
    10351473    case 'e':
    10361474    case 'E':
     
    10551493}
    10561494#endif /* FORMAT_FLOAT */
     1495
     1496#ifdef FORMAT_COMPLEX
     1497PyObject *
     1498FORMAT_COMPLEX(PyObject *obj,
     1499               STRINGLIB_CHAR *format_spec,
     1500               Py_ssize_t format_spec_len)
     1501{
     1502    PyObject *result = NULL;
     1503    InternalFormatSpec format;
     1504
     1505    /* check for the special case of zero length format spec, make
     1506       it equivalent to str(obj) */
     1507    if (format_spec_len == 0) {
     1508        result = STRINGLIB_TOSTR(obj);
     1509        goto done;
     1510    }
     1511
     1512    /* parse the format_spec */
     1513    if (!parse_internal_render_format_spec(format_spec,
     1514                                           format_spec_len,
     1515                                           &format, '\0', '>'))
     1516        goto done;
     1517
     1518    /* type conversion? */
     1519    switch (format.type) {
     1520    case '\0': /* No format code: like 'g', but with at least one decimal. */
     1521    case 'e':
     1522    case 'E':
     1523    case 'f':
     1524    case 'F':
     1525    case 'g':
     1526    case 'G':
     1527    case 'n':
     1528        /* no conversion, already a complex.  do the formatting */
     1529        result = format_complex_internal(obj, &format);
     1530        break;
     1531
     1532    default:
     1533        /* unknown */
     1534        unknown_presentation_type(format.type, obj->ob_type->tp_name);
     1535        goto done;
     1536    }
     1537
     1538done:
     1539    return result;
     1540}
     1541#endif /* FORMAT_COMPLEX */
  • python/trunk/Objects/stringlib/localeutil.h

    r2 r391  
    55
    66#include <locale.h>
     7
     8#define MAX(x, y) ((x) < (y) ? (y) : (x))
     9#define MIN(x, y) ((x) < (y) ? (x) : (y))
     10
     11typedef struct {
     12    const char *grouping;
     13    char previous;
     14    Py_ssize_t i; /* Where we're currently pointing in grouping. */
     15} GroupGenerator;
     16
     17static void
     18_GroupGenerator_init(GroupGenerator *self, const char *grouping)
     19{
     20    self->grouping = grouping;
     21    self->i = 0;
     22    self->previous = 0;
     23}
     24
     25/* Returns the next grouping, or 0 to signify end. */
     26static Py_ssize_t
     27_GroupGenerator_next(GroupGenerator *self)
     28{
     29    /* Note that we don't really do much error checking here. If a
     30       grouping string contains just CHAR_MAX, for example, then just
     31       terminate the generator. That shouldn't happen, but at least we
     32       fail gracefully. */
     33    switch (self->grouping[self->i]) {
     34    case 0:
     35        return self->previous;
     36    case CHAR_MAX:
     37        /* Stop the generator. */
     38        return 0;
     39    default: {
     40        char ch = self->grouping[self->i];
     41        self->previous = ch;
     42        self->i++;
     43        return (Py_ssize_t)ch;
     44    }
     45    }
     46}
     47
     48/* Fill in some digits, leading zeros, and thousands separator. All
     49   are optional, depending on when we're called. */
     50static void
     51fill(STRINGLIB_CHAR **digits_end, STRINGLIB_CHAR **buffer_end,
     52     Py_ssize_t n_chars, Py_ssize_t n_zeros, const char* thousands_sep,
     53     Py_ssize_t thousands_sep_len)
     54{
     55#if STRINGLIB_IS_UNICODE
     56    Py_ssize_t i;
     57#endif
     58
     59    if (thousands_sep) {
     60        *buffer_end -= thousands_sep_len;
     61
     62        /* Copy the thousands_sep chars into the buffer. */
     63#if STRINGLIB_IS_UNICODE
     64        /* Convert from the char's of the thousands_sep from
     65           the locale into unicode. */
     66        for (i = 0; i < thousands_sep_len; ++i)
     67            (*buffer_end)[i] = thousands_sep[i];
     68#else
     69        /* No conversion, just memcpy the thousands_sep. */
     70        memcpy(*buffer_end, thousands_sep, thousands_sep_len);
     71#endif
     72    }
     73
     74    *buffer_end -= n_chars;
     75    *digits_end -= n_chars;
     76    memcpy(*buffer_end, *digits_end, n_chars * sizeof(STRINGLIB_CHAR));
     77
     78    *buffer_end -= n_zeros;
     79    STRINGLIB_FILL(*buffer_end, '0', n_zeros);
     80}
    781
    882/**
    983 * _Py_InsertThousandsGrouping:
    1084 * @buffer: A pointer to the start of a string.
    11  * @n_buffer: The length of the string.
     85 * @n_buffer: Number of characters in @buffer.
     86 * @digits: A pointer to the digits we're reading from. If count
     87 *          is non-NULL, this is unused.
    1288 * @n_digits: The number of digits in the string, in which we want
    1389 *            to put the grouping chars.
    14  * @buf_size: The maximum size of the buffer pointed to by buffer.
    15  * @count: If non-NULL, points to a variable that will receive the
    16  *         number of characters we need to insert (and no formatting
    17  *         will actually occur).
    18  * @append_zero_char: If non-zero, put a trailing zero at the end of
    19  *         of the resulting string, if and only if we modified the
    20  *         string.
    21  *
    22  * Inserts thousand grouping characters (as defined in the current
    23  *  locale) into the string between buffer and buffer+n_digits.  If
    24  *  count is non-NULL, don't do any formatting, just count the number
    25  *  of characters to insert.  This is used by the caller to
    26  *  appropriately resize the buffer, if needed.  If count is non-NULL,
    27  *  buffer can be NULL (it is not dereferenced at all in that case).
     90 * @min_width: The minimum width of the digits in the output string.
     91 *             Output will be zero-padded on the left to fill.
     92 * @grouping: see definition in localeconv().
     93 * @thousands_sep: see definition in localeconv().
     94 *
     95 * There are 2 modes: counting and filling. If @buffer is NULL,
     96 *  we are in counting mode, else filling mode.
     97 * If counting, the required buffer size is returned.
     98 * If filling, we know the buffer will be large enough, so we don't
     99 *  need to pass in the buffer size.
     100 * Inserts thousand grouping characters (as defined by grouping and
     101 *  thousands_sep) into the string between buffer and buffer+n_digits.
    28102 *
    29103 * Return value: 0 on error, else 1.  Note that no error can occur if
     
    32106 * This name won't be used, the includer of this file should define
    33107 *  it to be the actual function name, based on unicode or string.
     108 *
     109 * As closely as possible, this code mimics the logic in decimal.py's
     110    _insert_thousands_sep().
    34111 **/
    35 int
     112Py_ssize_t
    36113_Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
    37                             Py_ssize_t n_buffer,
    38                             Py_ssize_t n_digits,
    39                             Py_ssize_t buf_size,
    40                             Py_ssize_t *count,
    41                             int append_zero_char)
    42 {
    43         struct lconv *locale_data = localeconv();
    44         const char *grouping = locale_data->grouping;
    45         const char *thousands_sep = locale_data->thousands_sep;
    46         Py_ssize_t thousands_sep_len = strlen(thousands_sep);
    47         STRINGLIB_CHAR *pend = NULL; /* current end of buffer */
    48         STRINGLIB_CHAR *pmax = NULL; /* max of buffer */
    49         char current_grouping;
    50         Py_ssize_t remaining = n_digits; /* Number of chars remaining to
    51                                             be looked at */
    52 
    53         /* Initialize the character count, if we're just counting. */
    54         if (count)
    55                 *count = 0;
    56         else {
    57                 /* We're not just counting, we're modifying buffer */
    58                 pend = buffer + n_buffer;
    59                 pmax = buffer + buf_size;
    60         }
    61 
    62         /* Starting at the end and working right-to-left, keep track of
    63            what grouping needs to be added and insert that. */
    64         current_grouping = *grouping++;
    65 
    66         /* If the first character is 0, perform no grouping at all. */
    67         if (current_grouping == 0)
    68                 return 1;
    69 
    70         while (remaining > current_grouping) {
    71                 /* Always leave buffer and pend valid at the end of this
    72                    loop, since we might leave with a return statement. */
    73 
    74                 remaining -= current_grouping;
    75                 if (count) {
    76                         /* We're only counting, not touching the memory. */
    77                         *count += thousands_sep_len;
    78                 }
    79                 else {
    80                         /* Do the formatting. */
    81 
    82                         STRINGLIB_CHAR *plast = buffer + remaining;
    83 
    84                         /* Is there room to insert thousands_sep_len chars? */
    85                         if (pmax - pend < thousands_sep_len)
    86                                 /* No room. */
    87                                 return 0;
    88 
    89                         /* Move the rest of the string down. */
    90                         memmove(plast + thousands_sep_len,
    91                                 plast,
    92                                 (pend - plast) * sizeof(STRINGLIB_CHAR));
    93                         /* Copy the thousands_sep chars into the buffer. */
    94 #if STRINGLIB_IS_UNICODE
    95                         /* Convert from the char's of the thousands_sep from
    96                            the locale into unicode. */
    97                         {
    98                                 Py_ssize_t i;
    99                                 for (i = 0; i < thousands_sep_len; ++i)
    100                                         plast[i] = thousands_sep[i];
    101                         }
    102 #else
    103                         /* No conversion, just memcpy the thousands_sep. */
    104                         memcpy(plast, thousands_sep, thousands_sep_len);
    105 #endif
    106                 }
    107 
    108                 /* Adjust end pointer. */
    109                 pend += thousands_sep_len;
    110 
    111                 /* Move to the next grouping character, unless we're
    112                    repeating (which is designated by a grouping of 0). */
    113                 if (*grouping != 0) {
    114                         current_grouping = *grouping++;
    115                         if (current_grouping == CHAR_MAX)
    116                                 /* We're done. */
    117                                 break;
    118                 }
    119         }
    120         if (append_zero_char) {
    121                 /* Append a zero character to mark the end of the string,
    122                    if there's room. */
    123                 if (pend - (buffer + remaining) < 1)
    124                         /* No room, error. */
    125                         return 0;
    126                 *pend = 0;
    127         }
    128         return 1;
     114                            Py_ssize_t n_buffer,
     115                            STRINGLIB_CHAR *digits,
     116                            Py_ssize_t n_digits,
     117                            Py_ssize_t min_width,
     118                            const char *grouping,
     119                            const char *thousands_sep)
     120{
     121    Py_ssize_t count = 0;
     122    Py_ssize_t n_zeros;
     123    int loop_broken = 0;
     124    int use_separator = 0; /* First time through, don't append the
     125                              separator. They only go between
     126                              groups. */
     127    STRINGLIB_CHAR *buffer_end = NULL;
     128    STRINGLIB_CHAR *digits_end = NULL;
     129    Py_ssize_t l;
     130    Py_ssize_t n_chars;
     131    Py_ssize_t thousands_sep_len = strlen(thousands_sep);
     132    Py_ssize_t remaining = n_digits; /* Number of chars remaining to
     133                                        be looked at */
     134    /* A generator that returns all of the grouping widths, until it
     135       returns 0. */
     136    GroupGenerator groupgen;
     137    _GroupGenerator_init(&groupgen, grouping);
     138
     139    if (buffer) {
     140        buffer_end = buffer + n_buffer;
     141        digits_end = digits + n_digits;
     142    }
     143
     144    while ((l = _GroupGenerator_next(&groupgen)) > 0) {
     145        l = MIN(l, MAX(MAX(remaining, min_width), 1));
     146        n_zeros = MAX(0, l - remaining);
     147        n_chars = MAX(0, MIN(remaining, l));
     148
     149        /* Use n_zero zero's and n_chars chars */
     150
     151        /* Count only, don't do anything. */
     152        count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
     153
     154        if (buffer) {
     155            /* Copy into the output buffer. */
     156            fill(&digits_end, &buffer_end, n_chars, n_zeros,
     157                 use_separator ? thousands_sep : NULL, thousands_sep_len);
     158        }
     159
     160        /* Use a separator next time. */
     161        use_separator = 1;
     162
     163        remaining -= n_chars;
     164        min_width -= l;
     165
     166        if (remaining <= 0 && min_width <= 0) {
     167            loop_broken = 1;
     168            break;
     169        }
     170        min_width -= thousands_sep_len;
     171    }
     172    if (!loop_broken) {
     173        /* We left the loop without using a break statement. */
     174
     175        l = MAX(MAX(remaining, min_width), 1);
     176        n_zeros = MAX(0, l - remaining);
     177        n_chars = MAX(0, MIN(remaining, l));
     178
     179        /* Use n_zero zero's and n_chars chars */
     180        count += (use_separator ? thousands_sep_len : 0) + n_zeros + n_chars;
     181        if (buffer) {
     182            /* Copy into the output buffer. */
     183            fill(&digits_end, &buffer_end, n_chars, n_zeros,
     184                 use_separator ? thousands_sep : NULL, thousands_sep_len);
     185        }
     186    }
     187    return count;
     188}
     189
     190/**
     191 * _Py_InsertThousandsGroupingLocale:
     192 * @buffer: A pointer to the start of a string.
     193 * @n_digits: The number of digits in the string, in which we want
     194 *            to put the grouping chars.
     195 *
     196 * Reads thee current locale and calls _Py_InsertThousandsGrouping().
     197 **/
     198Py_ssize_t
     199_Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR *buffer,
     200                                  Py_ssize_t n_buffer,
     201                                  STRINGLIB_CHAR *digits,
     202                                  Py_ssize_t n_digits,
     203                                  Py_ssize_t min_width)
     204{
     205        struct lconv *locale_data = localeconv();
     206        const char *grouping = locale_data->grouping;
     207        const char *thousands_sep = locale_data->thousands_sep;
     208
     209        return _Py_InsertThousandsGrouping(buffer, n_buffer, digits, n_digits,
     210                                           min_width, grouping, thousands_sep);
    129211}
    130212#endif /* STRINGLIB_LOCALEUTIL_H */
  • python/trunk/Objects/stringlib/partition.h

    r2 r391  
    99
    1010Py_LOCAL_INLINE(PyObject*)
    11 stringlib_partition(
    12     PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
    13     PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
    14     )
     11stringlib_partition(PyObject* str_obj,
     12                    const STRINGLIB_CHAR* str, Py_ssize_t str_len,
     13                    PyObject* sep_obj,
     14                    const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
    1515{
    1616    PyObject* out;
     
    1919    if (sep_len == 0) {
    2020        PyErr_SetString(PyExc_ValueError, "empty separator");
    21         return NULL;
     21        return NULL;
    2222    }
    2323
    2424    out = PyTuple_New(3);
    2525    if (!out)
    26         return NULL;
     26        return NULL;
    2727
    28     pos = fastsearch(str, str_len, sep, sep_len, FAST_SEARCH);
     28    pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_SEARCH);
    2929
    3030    if (pos < 0) {
    31         Py_INCREF(str_obj);
    32         PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
    33         Py_INCREF(STRINGLIB_EMPTY);
    34         PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
    35         Py_INCREF(STRINGLIB_EMPTY);
    36         PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);
    37         return out;
     31#if STRINGLIB_MUTABLE
     32        PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(str, str_len));
     33        PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
     34        PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(NULL, 0));
     35#else
     36        Py_INCREF(str_obj);
     37        PyTuple_SET_ITEM(out, 0, (PyObject*) str_obj);
     38        Py_INCREF(STRINGLIB_EMPTY);
     39        PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
     40        Py_INCREF(STRINGLIB_EMPTY);
     41        PyTuple_SET_ITEM(out, 2, (PyObject*) STRINGLIB_EMPTY);
     42#endif
     43        return out;
    3844    }
    3945
     
    4551
    4652    if (PyErr_Occurred()) {
    47         Py_DECREF(out);
    48         return NULL;
     53        Py_DECREF(out);
     54        return NULL;
    4955    }
    5056
     
    5359
    5460Py_LOCAL_INLINE(PyObject*)
    55 stringlib_rpartition(
    56     PyObject* str_obj, const STRINGLIB_CHAR* str, Py_ssize_t str_len,
    57     PyObject* sep_obj, const STRINGLIB_CHAR* sep, Py_ssize_t sep_len
    58     )
     61stringlib_rpartition(PyObject* str_obj,
     62                    const STRINGLIB_CHAR* str, Py_ssize_t str_len,
     63                     PyObject* sep_obj,
     64                     const STRINGLIB_CHAR* sep, Py_ssize_t sep_len)
    5965{
    6066    PyObject* out;
    61     Py_ssize_t pos, j;
     67    Py_ssize_t pos;
    6268
    6369    if (sep_len == 0) {
    6470        PyErr_SetString(PyExc_ValueError, "empty separator");
    65         return NULL;
     71        return NULL;
    6672    }
    6773
    6874    out = PyTuple_New(3);
    6975    if (!out)
    70         return NULL;
     76        return NULL;
    7177
    72     /* XXX - create reversefastsearch helper! */
    73         pos = -1;
    74         for (j = str_len - sep_len; j >= 0; --j)
    75             if (STRINGLIB_CMP(str+j, sep, sep_len) == 0) {
    76                 pos = j;
    77                 break;
    78             }
     78    pos = fastsearch(str, str_len, sep, sep_len, -1, FAST_RSEARCH);
    7979
    8080    if (pos < 0) {
    81         Py_INCREF(STRINGLIB_EMPTY);
    82         PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY);
    83         Py_INCREF(STRINGLIB_EMPTY);
    84         PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
    85         Py_INCREF(str_obj);       
    86         PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
    87         return out;
     81#if STRINGLIB_MUTABLE
     82        PyTuple_SET_ITEM(out, 0, STRINGLIB_NEW(NULL, 0));
     83        PyTuple_SET_ITEM(out, 1, STRINGLIB_NEW(NULL, 0));
     84        PyTuple_SET_ITEM(out, 2, STRINGLIB_NEW(str, str_len));
     85#else
     86        Py_INCREF(STRINGLIB_EMPTY);
     87        PyTuple_SET_ITEM(out, 0, (PyObject*) STRINGLIB_EMPTY);
     88        Py_INCREF(STRINGLIB_EMPTY);
     89        PyTuple_SET_ITEM(out, 1, (PyObject*) STRINGLIB_EMPTY);
     90        Py_INCREF(str_obj);
     91        PyTuple_SET_ITEM(out, 2, (PyObject*) str_obj);
     92#endif
     93        return out;
    8894    }
    8995
     
    95101
    96102    if (PyErr_Occurred()) {
    97         Py_DECREF(out);
    98         return NULL;
     103        Py_DECREF(out);
     104        return NULL;
    99105    }
    100106
     
    103109
    104110#endif
    105 
    106 /*
    107 Local variables:
    108 c-basic-offset: 4
    109 indent-tabs-mode: nil
    110 End:
    111 */
  • python/trunk/Objects/stringlib/string_format.h

    r2 r391  
    77
    88
    9 /* Defines for Python 2.6 compatability */
     9/* Defines for Python 2.6 compatibility */
    1010#if PY_VERSION_HEX < 0x03000000
    1111#define PyLong_FromSsize_t _PyLong_FromSsize_t
     
    3232
    3333
     34typedef enum {
     35    ANS_INIT,
     36    ANS_AUTO,
     37    ANS_MANUAL
     38} AutoNumberState;   /* Keep track if we're auto-numbering fields */
     39
     40/* Keeps track of our auto-numbering state, and which number field we're on */
     41typedef struct {
     42    AutoNumberState an_state;
     43    int an_field_number;
     44} AutoNumber;
     45
     46
    3447/* forward declaration for recursion */
    3548static PyObject *
    3649build_string(SubString *input, PyObject *args, PyObject *kwargs,
    37              int recursion_depth);
     50             int recursion_depth, AutoNumber *auto_number);
    3851
    3952
     
    4255/**************************  Utility  functions  ************************/
    4356/************************************************************************/
     57
     58static void
     59AutoNumber_Init(AutoNumber *auto_number)
     60{
     61    auto_number->an_state = ANS_INIT;
     62    auto_number->an_field_number = 0;
     63}
    4464
    4565/* fill in a SubString from a pointer and length */
     
    7494    return STRINGLIB_NEW(str->ptr, str->end - str->ptr);
    7595}
     96
     97/* Return 1 if an error has been detected switching between automatic
     98   field numbering and manual field specification, else return 0. Set
     99   ValueError on error. */
     100static int
     101autonumber_state_error(AutoNumberState state, int field_name_is_empty)
     102{
     103    if (state == ANS_MANUAL) {
     104        if (field_name_is_empty) {
     105            PyErr_SetString(PyExc_ValueError, "cannot switch from "
     106                            "manual field specification to "
     107                            "automatic field numbering");
     108            return 1;
     109        }
     110    }
     111    else {
     112        if (!field_name_is_empty) {
     113            PyErr_SetString(PyExc_ValueError, "cannot switch from "
     114                            "automatic field numbering to "
     115                            "manual field specification");
     116            return 1;
     117        }
     118    }
     119    return 0;
     120}
     121
    76122
    77123/************************************************************************/
     
    152198    Py_ssize_t accumulator = 0;
    153199    Py_ssize_t digitval;
    154     Py_ssize_t oldaccumulator;
    155200    STRINGLIB_CHAR *p;
    156201
     
    164209            return -1;
    165210        /*
    166            This trick was copied from old Unicode format code.  It's cute,
    167            but would really suck on an old machine with a slow divide
    168            implementation.  Fortunately, in the normal case we do not
    169            expect too many digits.
     211           Detect possible overflow before it happens:
     212
     213              accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
     214              accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
    170215        */
    171         oldaccumulator = accumulator;
    172         accumulator *= 10;
    173         if ((accumulator+10)/10 != oldaccumulator+1) {
     216        if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
    174217            PyErr_Format(PyExc_ValueError,
    175218                         "Too many decimal digits in format string");
    176219            return -1;
    177220        }
    178         accumulator += digitval;
     221        accumulator = accumulator * 10 + digitval;
    179222    }
    180223    return accumulator;
     
    328371            return 0;
    329372        *name_idx = get_integer(name);
     373        if (*name_idx == -1 && PyErr_Occurred())
     374            return 0;
    330375        break;
    331376    default:
     
    354399static int
    355400field_name_split(STRINGLIB_CHAR *ptr, Py_ssize_t len, SubString *first,
    356                  Py_ssize_t *first_idx, FieldNameIterator *rest)
     401                 Py_ssize_t *first_idx, FieldNameIterator *rest,
     402                 AutoNumber *auto_number)
    357403{
    358404    STRINGLIB_CHAR c;
    359405    STRINGLIB_CHAR *p = ptr;
    360406    STRINGLIB_CHAR *end = ptr + len;
     407    int field_name_is_empty;
     408    int using_numeric_index;
    361409
    362410    /* find the part up until the first '.' or '[' */
     
    381429    /* see if "first" is an integer, in which case it's used as an index */
    382430    *first_idx = get_integer(first);
    383 
    384     /* zero length string is an error */
    385     if (first->ptr >= first->end) {
    386         PyErr_SetString(PyExc_ValueError, "empty field name");
    387         goto error;
     431    if (*first_idx == -1 && PyErr_Occurred())
     432        return 0;
     433
     434    field_name_is_empty = first->ptr >= first->end;
     435
     436    /* If the field name is omitted or if we have a numeric index
     437       specified, then we're doing numeric indexing into args. */
     438    using_numeric_index = field_name_is_empty || *first_idx != -1;
     439
     440    /* We always get here exactly one time for each field we're
     441       processing. And we get here in field order (counting by left
     442       braces). So this is the perfect place to handle automatic field
     443       numbering if the field name is omitted. */
     444
     445    /* Check if we need to do the auto-numbering. It's not needed if
     446       we're called from string.Format routines, because it's handled
     447       in that class by itself. */
     448    if (auto_number) {
     449        /* Initialize our auto numbering state if this is the first
     450           time we're either auto-numbering or manually numbering. */
     451        if (auto_number->an_state == ANS_INIT && using_numeric_index)
     452            auto_number->an_state = field_name_is_empty ?
     453                ANS_AUTO : ANS_MANUAL;
     454
     455        /* Make sure our state is consistent with what we're doing
     456           this time through. Only check if we're using a numeric
     457           index. */
     458        if (using_numeric_index)
     459            if (autonumber_state_error(auto_number->an_state,
     460                                       field_name_is_empty))
     461                return 0;
     462        /* Zero length field means we want to do auto-numbering of the
     463           fields. */
     464        if (field_name_is_empty)
     465            *first_idx = (auto_number->an_field_number)++;
    388466    }
    389467
    390468    return 1;
    391 error:
    392     return 0;
    393469}
    394470
     
    400476*/
    401477static PyObject *
    402 get_field_object(SubString *input, PyObject *args, PyObject *kwargs)
     478get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
     479                 AutoNumber *auto_number)
    403480{
    404481    PyObject *obj = NULL;
     
    411488
    412489    if (!field_name_split(input->ptr, input->end - input->ptr, &first,
    413                           &index, &rest)) {
     490                          &index, &rest, auto_number)) {
    414491        goto error;
    415492    }
     
    488565    PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL;
    489566    STRINGLIB_CHAR* format_spec_start = format_spec->ptr ?
    490             format_spec->ptr : NULL;
     567            format_spec->ptr : NULL;
    491568    Py_ssize_t format_spec_len = format_spec->ptr ?
    492             format_spec->end - format_spec->ptr : 0;
     569            format_spec->end - format_spec->ptr : 0;
    493570
    494571    /* If we know the type exactly, skip the lookup of __format__ and just
     
    496573#if STRINGLIB_IS_UNICODE
    497574    if (PyUnicode_CheckExact(fieldobj))
    498         formatter = _PyUnicode_FormatAdvanced;
     575        formatter = _PyUnicode_FormatAdvanced;
    499576    /* Unfortunately, there's a problem with checking for int, long,
    500577       and float here.  If we're being included as unicode, their
     
    504581#else
    505582    if (PyString_CheckExact(fieldobj))
    506         formatter = _PyBytes_FormatAdvanced;
     583        formatter = _PyBytes_FormatAdvanced;
    507584    else if (PyInt_CheckExact(fieldobj))
    508         formatter =_PyInt_FormatAdvanced;
     585        formatter =_PyInt_FormatAdvanced;
    509586    else if (PyLong_CheckExact(fieldobj))
    510         formatter =_PyLong_FormatAdvanced;
     587        formatter =_PyLong_FormatAdvanced;
    511588    else if (PyFloat_CheckExact(fieldobj))
    512         formatter = _PyFloat_FormatAdvanced;
     589        formatter = _PyFloat_FormatAdvanced;
    513590#endif
    514591
    515592    if (formatter) {
    516         /* we know exactly which formatter will be called when __format__ is
    517            looked up, so call it directly, instead. */
    518         result = formatter(fieldobj, format_spec_start, format_spec_len);
     593        /* we know exactly which formatter will be called when __format__ is
     594           looked up, so call it directly, instead. */
     595        result = formatter(fieldobj, format_spec_start, format_spec_len);
    519596    }
    520597    else {
    521         /* We need to create an object out of the pointers we have, because
    522            __format__ takes a string/unicode object for format_spec. */
    523         format_spec_object = STRINGLIB_NEW(format_spec_start,
    524                                            format_spec_len);
    525         if (format_spec_object == NULL)
    526             goto done;
    527 
    528         result = PyObject_Format(fieldobj, format_spec_object);
     598        /* We need to create an object out of the pointers we have, because
     599           __format__ takes a string/unicode object for format_spec. */
     600        format_spec_object = STRINGLIB_NEW(format_spec_start,
     601                                           format_spec_len);
     602        if (format_spec_object == NULL)
     603            goto done;
     604
     605        result = PyObject_Format(fieldobj, format_spec_object);
    529606    }
    530607    if (result == NULL)
     
    539616       be unicode */
    540617    {
    541         PyObject *tmp = STRINGLIB_TOSTR(result);
    542         if (tmp == NULL)
    543             goto done;
    544         Py_DECREF(result);
    545         result = tmp;
     618        PyObject *tmp = STRINGLIB_TOSTR(result);
     619        if (tmp == NULL)
     620            goto done;
     621        Py_DECREF(result);
     622        result = tmp;
    546623    }
    547624#endif
     
    559636            STRINGLIB_CHAR *conversion)
    560637{
     638    /* Note this function works if the field name is zero length,
     639       which is good.  Zero length field names are handled later, in
     640       field_name_split. */
     641
    561642    STRINGLIB_CHAR c = 0;
    562643
     
    565646    SubString_init(format_spec, NULL, 0);
    566647
    567     /* search for the field name.  it's terminated by the end of the
    568        string, or a ':' or '!' */
     648    /* Search for the field name.  it's terminated by the end of
     649       the string, or a ':' or '!' */
    569650    field_name->ptr = str->ptr;
    570651    while (str->ptr < str->end) {
     
    609690            }
    610691        }
    611 
    612         return 1;
    613 
    614     }
    615     else {
     692    }
     693    else
    616694        /* end of string, there's no format_spec or conversion */
    617695        field_name->end = str->ptr;
    618         return 1;
    619     }
     696
     697    return 1;
    620698}
    621699
     
    644722static int
    645723MarkupIterator_next(MarkupIterator *self, SubString *literal,
    646                     SubString *field_name, SubString *format_spec,
    647                     STRINGLIB_CHAR *conversion,
     724                    int *field_present, SubString *field_name,
     725                    SubString *format_spec, STRINGLIB_CHAR *conversion,
    648726                    int *format_spec_needs_expanding)
    649727{
     
    661739    *conversion = '\0';
    662740    *format_spec_needs_expanding = 0;
     741    *field_present = 0;
    663742
    664743    /* No more input, end of iterator.  This is the normal exit
     
    722801       braces.  note that this prohibits escaped braces, so that
    723802       format_specs cannot have braces in them. */
     803    *field_present = 1;
    724804    count = 1;
    725805
     
    746826                    return 0;
    747827
    748                 /* a zero length field_name is an error */
    749                 if (field_name->ptr == field_name->end) {
    750                     PyErr_SetString(PyExc_ValueError, "zero length field name "
    751                                     "in format");
    752                     return 0;
    753                 }
    754 
    755828                /* success */
    756829                return 2;
     
    778851        return STRINGLIB_TOSTR(obj);
    779852    default:
    780         if (conversion > 32 && conversion < 127) {
    781                 /* It's the ASCII subrange; casting to char is safe
    782                    (assuming the execution character set is an ASCII
    783                    superset). */
    784                 PyErr_Format(PyExc_ValueError,
     853        if (conversion > 32 && conversion < 127) {
     854                /* It's the ASCII subrange; casting to char is safe
     855                   (assuming the execution character set is an ASCII
     856                   superset). */
     857                PyErr_Format(PyExc_ValueError,
    785858                     "Unknown conversion specifier %c",
    786859                     (char)conversion);
    787         } else
    788                 PyErr_Format(PyExc_ValueError,
    789                      "Unknown conversion specifier \\x%x",
    790                      (unsigned int)conversion);
     860        } else
     861                PyErr_Format(PyExc_ValueError,
     862                     "Unknown conversion specifier \\x%x",
     863                     (unsigned int)conversion);
    791864        return NULL;
    792865    }
     
    800873   format_spec_needs_expanding is an optimization.  if it's false,
    801874   just output the string directly, otherwise recursively expand the
    802    format_spec string. */
     875   format_spec string.
     876
     877   field_name is allowed to be zero length, in which case we
     878   are doing auto field numbering.
     879*/
    803880
    804881static int
     
    806883              int format_spec_needs_expanding, STRINGLIB_CHAR conversion,
    807884              OutputString *output, PyObject *args, PyObject *kwargs,
    808               int recursion_depth)
     885              int recursion_depth, AutoNumber *auto_number)
    809886{
    810887    PyObject *tmp = NULL;
     
    815892
    816893    /* convert field_name to an object */
    817     fieldobj = get_field_object(field_name, args, kwargs);
     894    fieldobj = get_field_object(field_name, args, kwargs, auto_number);
    818895    if (fieldobj == NULL)
    819896        goto done;
     
    832909    /* if needed, recurively compute the format_spec */
    833910    if (format_spec_needs_expanding) {
    834         tmp = build_string(format_spec, args, kwargs, recursion_depth-1);
     911        tmp = build_string(format_spec, args, kwargs, recursion_depth-1,
     912                           auto_number);
    835913        if (tmp == NULL)
    836914            goto done;
     
    866944static int
    867945do_markup(SubString *input, PyObject *args, PyObject *kwargs,
    868           OutputString *output, int recursion_depth)
     946          OutputString *output, int recursion_depth, AutoNumber *auto_number)
    869947{
    870948    MarkupIterator iter;
    871949    int format_spec_needs_expanding;
    872950    int result;
     951    int field_present;
    873952    SubString literal;
    874953    SubString field_name;
     
    877956
    878957    MarkupIterator_init(&iter, input->ptr, input->end - input->ptr);
    879     while ((result = MarkupIterator_next(&iter, &literal, &field_name,
    880                                          &format_spec, &conversion,
     958    while ((result = MarkupIterator_next(&iter, &literal, &field_present,
     959                                         &field_name, &format_spec,
     960                                         &conversion,
    881961                                         &format_spec_needs_expanding)) == 2) {
    882962        if (!output_data(output, literal.ptr, literal.end - literal.ptr))
    883963            return 0;
    884         if (field_name.ptr != field_name.end)
     964        if (field_present)
    885965            if (!output_markup(&field_name, &format_spec,
    886966                               format_spec_needs_expanding, conversion, output,
    887                                args, kwargs, recursion_depth))
     967                               args, kwargs, recursion_depth, auto_number))
    888968                return 0;
    889969    }
     
    898978static PyObject *
    899979build_string(SubString *input, PyObject *args, PyObject *kwargs,
    900              int recursion_depth)
     980             int recursion_depth, AutoNumber *auto_number)
    901981{
    902982    OutputString output;
     
    9201000        goto done;
    9211001
    922     if (!do_markup(input, args, kwargs, &output, recursion_depth)) {
     1002    if (!do_markup(input, args, kwargs, &output, recursion_depth,
     1003                   auto_number)) {
    9231004        goto done;
    9241005    }
     
    9541035    int recursion_depth = 2;
    9551036
     1037    AutoNumber auto_number;
     1038
     1039    AutoNumber_Init(&auto_number);
    9561040    SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self));
    957     return build_string(&input, args, kwargs, recursion_depth);
     1041    return build_string(&input, args, kwargs, recursion_depth, &auto_number);
    9581042}
    9591043
     
    10001084    STRINGLIB_CHAR conversion;
    10011085    int format_spec_needs_expanding;
    1002     int result = MarkupIterator_next(&it->it_markup, &literal, &field_name,
    1003                                      &format_spec, &conversion,
     1086    int field_present;
     1087    int result = MarkupIterator_next(&it->it_markup, &literal, &field_present,
     1088                                     &field_name, &format_spec, &conversion,
    10041089                                     &format_spec_needs_expanding);
    10051090
     
    10161101        PyObject *conversion_str = NULL;
    10171102        PyObject *tuple = NULL;
    1018         int has_field = field_name.ptr != field_name.end;
    10191103
    10201104        literal_str = SubString_new_object(&literal);
     
    10281112        /* if field_name is non-zero length, return a string for
    10291113           format_spec (even if zero length), else return None */
    1030         format_spec_str = (has_field ?
     1114        format_spec_str = (field_present ?
    10311115                           SubString_new_object_or_empty :
    10321116                           SubString_new_object)(&format_spec);
     
    10421126        }
    10431127        else
    1044             conversion_str = STRINGLIB_NEW(&conversion, 1);
     1128            conversion_str = STRINGLIB_NEW(&conversion, 1);
    10451129        if (conversion_str == NULL)
    10461130            goto done;
     
    10581142
    10591143static PyMethodDef formatteriter_methods[] = {
    1060     {NULL,              NULL}           /* sentinel */
     1144    {NULL,              NULL}           /* sentinel */
    10611145};
    10621146
    10631147static PyTypeObject PyFormatterIter_Type = {
    10641148    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1065     "formatteriterator",                /* tp_name */
    1066     sizeof(formatteriterobject),        /* tp_basicsize */
    1067     0,                                  /* tp_itemsize */
     1149    "formatteriterator",                /* tp_name */
     1150    sizeof(formatteriterobject),        /* tp_basicsize */
     1151    0,                                  /* tp_itemsize */
    10681152    /* methods */
    1069     (destructor)formatteriter_dealloc,  /* tp_dealloc */
    1070     0,                                  /* tp_print */
    1071     0,                                  /* tp_getattr */
    1072     0,                                  /* tp_setattr */
    1073     0,                                  /* tp_compare */
    1074     0,                                  /* tp_repr */
    1075     0,                                  /* tp_as_number */
    1076     0,                                  /* tp_as_sequence */
    1077     0,                                  /* tp_as_mapping */
    1078     0,                                  /* tp_hash */
    1079     0,                                  /* tp_call */
    1080     0,                                  /* tp_str */
    1081     PyObject_GenericGetAttr,            /* tp_getattro */
    1082     0,                                  /* tp_setattro */
    1083     0,                                  /* tp_as_buffer */
    1084     Py_TPFLAGS_DEFAULT,                 /* tp_flags */
    1085     0,                                  /* tp_doc */
    1086     0,                                  /* tp_traverse */
    1087     0,                                  /* tp_clear */
    1088     0,                                  /* tp_richcompare */
    1089     0,                                  /* tp_weaklistoffset */
    1090     PyObject_SelfIter,                  /* tp_iter */
    1091     (iternextfunc)formatteriter_next,   /* tp_iternext */
    1092     formatteriter_methods,              /* tp_methods */
     1153    (destructor)formatteriter_dealloc,  /* tp_dealloc */
     1154    0,                                  /* tp_print */
     1155    0,                                  /* tp_getattr */
     1156    0,                                  /* tp_setattr */
     1157    0,                                  /* tp_compare */
     1158    0,                                  /* tp_repr */
     1159    0,                                  /* tp_as_number */
     1160    0,                                  /* tp_as_sequence */
     1161    0,                                  /* tp_as_mapping */
     1162    0,                                  /* tp_hash */
     1163    0,                                  /* tp_call */
     1164    0,                                  /* tp_str */
     1165    PyObject_GenericGetAttr,            /* tp_getattro */
     1166    0,                                  /* tp_setattro */
     1167    0,                                  /* tp_as_buffer */
     1168    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
     1169    0,                                  /* tp_doc */
     1170    0,                                  /* tp_traverse */
     1171    0,                                  /* tp_clear */
     1172    0,                                  /* tp_richcompare */
     1173    0,                                  /* tp_weaklistoffset */
     1174    PyObject_SelfIter,                  /* tp_iter */
     1175    (iternextfunc)formatteriter_next,   /* tp_iternext */
     1176    formatteriter_methods,              /* tp_methods */
    10931177    0,
    10941178};
     
    11911275
    11921276static PyMethodDef fieldnameiter_methods[] = {
    1193     {NULL,              NULL}           /* sentinel */
     1277    {NULL,              NULL}           /* sentinel */
    11941278};
    11951279
    11961280static PyTypeObject PyFieldNameIter_Type = {
    11971281    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1198     "fieldnameiterator",                /* tp_name */
    1199     sizeof(fieldnameiterobject),        /* tp_basicsize */
    1200     0,                                  /* tp_itemsize */
     1282    "fieldnameiterator",                /* tp_name */
     1283    sizeof(fieldnameiterobject),        /* tp_basicsize */
     1284    0,                                  /* tp_itemsize */
    12011285    /* methods */
    1202     (destructor)fieldnameiter_dealloc,  /* tp_dealloc */
    1203     0,                                  /* tp_print */
    1204     0,                                  /* tp_getattr */
    1205     0,                                  /* tp_setattr */
    1206     0,                                  /* tp_compare */
    1207     0,                                  /* tp_repr */
    1208     0,                                  /* tp_as_number */
    1209     0,                                  /* tp_as_sequence */
    1210     0,                                  /* tp_as_mapping */
    1211     0,                                  /* tp_hash */
    1212     0,                                  /* tp_call */
    1213     0,                                  /* tp_str */
    1214     PyObject_GenericGetAttr,            /* tp_getattro */
    1215     0,                                  /* tp_setattro */
    1216     0,                                  /* tp_as_buffer */
    1217     Py_TPFLAGS_DEFAULT,                 /* tp_flags */
    1218     0,                                  /* tp_doc */
    1219     0,                                  /* tp_traverse */
    1220     0,                                  /* tp_clear */
    1221     0,                                  /* tp_richcompare */
    1222     0,                                  /* tp_weaklistoffset */
    1223     PyObject_SelfIter,                  /* tp_iter */
    1224     (iternextfunc)fieldnameiter_next,   /* tp_iternext */
    1225     fieldnameiter_methods,              /* tp_methods */
     1286    (destructor)fieldnameiter_dealloc,  /* tp_dealloc */
     1287    0,                                  /* tp_print */
     1288    0,                                  /* tp_getattr */
     1289    0,                                  /* tp_setattr */
     1290    0,                                  /* tp_compare */
     1291    0,                                  /* tp_repr */
     1292    0,                                  /* tp_as_number */
     1293    0,                                  /* tp_as_sequence */
     1294    0,                                  /* tp_as_mapping */
     1295    0,                                  /* tp_hash */
     1296    0,                                  /* tp_call */
     1297    0,                                  /* tp_str */
     1298    PyObject_GenericGetAttr,            /* tp_getattro */
     1299    0,                                  /* tp_setattro */
     1300    0,                                  /* tp_as_buffer */
     1301    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
     1302    0,                                  /* tp_doc */
     1303    0,                                  /* tp_traverse */
     1304    0,                                  /* tp_clear */
     1305    0,                                  /* tp_richcompare */
     1306    0,                                  /* tp_weaklistoffset */
     1307    PyObject_SelfIter,                  /* tp_iter */
     1308    (iternextfunc)fieldnameiter_next,   /* tp_iternext */
     1309    fieldnameiter_methods,              /* tp_methods */
    12261310    0};
    12271311
     
    12521336    it->str = self;
    12531337
     1338    /* Pass in auto_number = NULL. We'll return an empty string for
     1339       first_obj in that case. */
    12541340    if (!field_name_split(STRINGLIB_STR(self),
    12551341                          STRINGLIB_LEN(self),
    1256                           &first, &first_idx, &it->it_field))
     1342                          &first, &first_idx, &it->it_field, NULL))
    12571343        goto done;
    12581344
  • python/trunk/Objects/stringlib/stringdefs.h

    r2 r391  
    1212#define STRINGLIB_PARSE_CODE     "S"
    1313#define STRINGLIB_EMPTY          nullstring
     14#define STRINGLIB_ISSPACE        Py_ISSPACE
     15#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
    1416#define STRINGLIB_ISDECIMAL(x)   ((x >= '0') && (x <= '9'))
    1517#define STRINGLIB_TODECIMAL(x)   (STRINGLIB_ISDECIMAL(x) ? (x - '0') : -1)
    16 #define STRINGLIB_TOUPPER        toupper
    17 #define STRINGLIB_TOLOWER        tolower
     18#define STRINGLIB_TOUPPER        Py_TOUPPER
     19#define STRINGLIB_TOLOWER        Py_TOLOWER
    1820#define STRINGLIB_FILL           memset
    1921#define STRINGLIB_STR            PyString_AS_STRING
     
    2224#define STRINGLIB_RESIZE         _PyString_Resize
    2325#define STRINGLIB_CHECK          PyString_Check
    24 #define STRINGLIB_CMP            memcmp
     26#define STRINGLIB_CHECK_EXACT    PyString_CheckExact
    2527#define STRINGLIB_TOSTR          PyObject_Str
    2628#define STRINGLIB_GROUPING       _PyString_InsertThousandsGrouping
     29#define STRINGLIB_GROUPING_LOCALE _PyString_InsertThousandsGroupingLocale
     30
     31#define STRINGLIB_WANT_CONTAINS_OBJ 1
    2732
    2833#endif /* !STRINGLIB_STRINGDEFS_H */
  • python/trunk/Objects/stringlib/transmogrify.h

    r2 r391  
    11/* NOTE: this API is -ONLY- for use with single byte character strings. */
    22/* Do not use it with Unicode. */
    3 
    4 #include "bytes_methods.h"
    5 
    6 #ifndef STRINGLIB_MUTABLE
    7 #warning "STRINGLIB_MUTABLE not defined before #include, assuming 0"
    8 #define STRINGLIB_MUTABLE 0
    9 #endif
    103
    114/* the more complicated methods.  parts of these should be pulled out into the
     
    270263    return (PyObject*) s;
    271264}
    272 
    273 
    274 #define _STRINGLIB_SPLIT_APPEND(data, left, right)              \
    275         str = STRINGLIB_NEW((data) + (left),                    \
    276                                          (right) - (left));     \
    277         if (str == NULL)                                        \
    278                 goto onError;                                   \
    279         if (PyList_Append(list, str)) {                         \
    280                 Py_DECREF(str);                                 \
    281                 goto onError;                                   \
    282         }                                                       \
    283         else                                                    \
    284                 Py_DECREF(str);
    285 
    286 PyDoc_STRVAR(splitlines__doc__,
    287 "B.splitlines([keepends]) -> list of lines\n\
    288 \n\
    289 Return a list of the lines in B, breaking at line boundaries.\n\
    290 Line breaks are not included in the resulting list unless keepends\n\
    291 is given and true.");
    292 
    293 static PyObject*
    294 stringlib_splitlines(PyObject *self, PyObject *args)
    295 {
    296     register Py_ssize_t i;
    297     register Py_ssize_t j;
    298     Py_ssize_t len;
    299     int keepends = 0;
    300     PyObject *list;
    301     PyObject *str;
    302     char *data;
    303 
    304     if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
    305         return NULL;
    306 
    307     data = STRINGLIB_STR(self);
    308     len = STRINGLIB_LEN(self);
    309 
    310     /* This does not use the preallocated list because splitlines is
    311        usually run with hundreds of newlines.  The overhead of
    312        switching between PyList_SET_ITEM and append causes about a
    313        2-3% slowdown for that common case.  A smarter implementation
    314        could move the if check out, so the SET_ITEMs are done first
    315        and the appends only done when the prealloc buffer is full.
    316        That's too much work for little gain.*/
    317 
    318     list = PyList_New(0);
    319     if (!list)
    320         goto onError;
    321 
    322     for (i = j = 0; i < len; ) {
    323         Py_ssize_t eol;
    324 
    325         /* Find a line and append it */
    326         while (i < len && data[i] != '\n' && data[i] != '\r')
    327             i++;
    328 
    329         /* Skip the line break reading CRLF as one line break */
    330         eol = i;
    331         if (i < len) {
    332             if (data[i] == '\r' && i + 1 < len &&
    333                 data[i+1] == '\n')
    334                 i += 2;
    335             else
    336                 i++;
    337             if (keepends)
    338                 eol = i;
    339         }
    340         _STRINGLIB_SPLIT_APPEND(data, j, eol);
    341         j = i;
    342     }
    343     if (j < len) {
    344         _STRINGLIB_SPLIT_APPEND(data, j, len);
    345     }
    346 
    347     return list;
    348 
    349  onError:
    350     Py_XDECREF(list);
    351     return NULL;
    352 }
    353 
    354 #undef _STRINGLIB_SPLIT_APPEND
    355 
  • python/trunk/Objects/stringlib/unicodedefs.h

    r2 r391  
    1212#define STRINGLIB_PARSE_CODE     "U"
    1313#define STRINGLIB_EMPTY          unicode_empty
     14#define STRINGLIB_ISSPACE        Py_UNICODE_ISSPACE
     15#define STRINGLIB_ISLINEBREAK    BLOOM_LINEBREAK
    1416#define STRINGLIB_ISDECIMAL      Py_UNICODE_ISDECIMAL
    1517#define STRINGLIB_TODECIMAL      Py_UNICODE_TODECIMAL
     
    2224#define STRINGLIB_RESIZE         PyUnicode_Resize
    2325#define STRINGLIB_CHECK          PyUnicode_Check
     26#define STRINGLIB_CHECK_EXACT    PyUnicode_CheckExact
    2427#define STRINGLIB_GROUPING       _PyUnicode_InsertThousandsGrouping
    2528
     
    3235#define STRINGLIB_WANT_CONTAINS_OBJ 1
    3336
    34 /* STRINGLIB_CMP was defined as:
    35 
    36 Py_LOCAL_INLINE(int)
    37 STRINGLIB_CMP(const Py_UNICODE* str, const Py_UNICODE* other, Py_ssize_t len)
    38 {
    39     if (str[0] != other[0])
    40         return 1;
    41     return memcmp((void*) str, (void*) other, len * sizeof(Py_UNICODE));
    42 }
    43 
    44 but unfortunately that gives a error if the function isn't used in a file that
    45 includes this file.  So, reluctantly convert it to a macro instead. */
    46 
    47 #define STRINGLIB_CMP(str, other, len) \
    48     (((str)[0] != (other)[0]) ? \
    49      1 : \
    50      memcmp((void*) (str), (void*) (other), (len) * sizeof(Py_UNICODE)))
    51 
    52 
    5337#endif /* !STRINGLIB_UNICODEDEFS_H */
Note: See TracChangeset for help on using the changeset viewer.