Changeset 391 for python/trunk/Objects/stringlib/find.h
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Objects/stringlib/find.h
r2 r391 20 20 return offset; 21 21 22 pos = fastsearch(str, str_len, sub, sub_len, FAST_SEARCH);22 pos = fastsearch(str, str_len, sub, sub_len, -1, FAST_SEARCH); 23 23 24 24 if (pos >= 0) … … 33 33 Py_ssize_t offset) 34 34 { 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; \ 48 63 } 49 }50 64 51 65 Py_LOCAL_INLINE(Py_ssize_t) … … 54 68 Py_ssize_t start, Py_ssize_t end) 55 69 { 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); 71 72 } 72 73 … … 76 77 Py_ssize_t start, Py_ssize_t end) 77 78 { 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); 89 80 return stringlib_rfind(str + start, end - start, sub, sub_len, start); 90 81 } 91 82 92 #if defined(STRINGLIB_STR) && !defined(FROM_BYTEARRAY)83 #ifdef STRINGLIB_WANT_CONTAINS_OBJ 93 84 94 85 Py_LOCAL_INLINE(int) … … 101 92 } 102 93 103 #endif /* STRINGLIB_STR */ 104 105 #ifdef FROM_UNICODE 94 #endif /* STRINGLIB_WANT_CONTAINS_OBJ */ 106 95 107 96 /* 108 97 This function is a helper for the "find" family (find, rfind, index, 109 rindex) of unicodeobject.c file, because they all have the same110 behaviour for the arguments.98 rindex) and for count, startswith and endswith, because they all have 99 the same behaviour for the arguments. 111 100 112 101 It does not touch the variables received until it knows everything 113 102 is 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).119 103 */ 120 104 105 #define FORMAT_BUFFER_SIZE 50 106 121 107 Py_LOCAL_INLINE(int) 122 _ParseTupleFinds (PyObject *args, PyObject **substring, 123 Py_ssize_t *start, Py_ssize_t *end) { 124 PyObject *tmp_substring; 108 stringlib_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; 125 113 Py_ssize_t tmp_start = 0; 126 114 Py_ssize_t tmp_end = PY_SSIZE_T_MAX; 127 115 PyObject *obj_start=Py_None, *obj_end=Py_None; 116 char format[FORMAT_BUFFER_SIZE] = "O|OO:"; 117 size_t len = strlen(format); 128 118 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)) 131 123 return 0; 132 124 … … 141 133 return 0; 142 134 143 tmp_substring = PyUnicode_FromObject(tmp_substring);144 if (!tmp_substring)145 return 0;146 147 135 *start = tmp_start; 148 136 *end = tmp_end; 149 *sub string = tmp_substring;137 *subobj = tmp_subobj; 150 138 return 1; 151 139 } 152 140 153 #endif /* FROM_UNICODE */ 141 #undef FORMAT_BUFFER_SIZE 142 143 #if STRINGLIB_IS_UNICODE 144 145 /* 146 Wraps stringlib_parse_args_finds() and additionally ensures that the 147 first argument is a unicode object. 148 149 Note that we receive a pointer to the pointer of the substring object, 150 so when we create that object in this function we don't DECREF it, 151 because it continues living in the caller functions (those functions, 152 after finishing using the substring, must DECREF it). 153 */ 154 155 Py_LOCAL_INLINE(int) 156 stringlib_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 */ 154 174 155 175 #endif /* STRINGLIB_FIND_H */ 156 157 /*158 Local variables:159 c-basic-offset: 4160 indent-tabs-mode: nil161 End:162 */
Note:
See TracChangeset
for help on using the changeset viewer.