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:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • 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 */
Note: See TracChangeset for help on using the changeset viewer.