]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Objects/stringlib/find.h
1 /* stringlib: find/index implementation */
3 #ifndef STRINGLIB_FIND_H
4 #define STRINGLIB_FIND_H
6 #ifndef STRINGLIB_FASTSEARCH_H
7 #error must include "stringlib/fastsearch.h" before including this module
10 Py_LOCAL_INLINE(Py_ssize_t
)
11 stringlib_find(const STRINGLIB_CHAR
* str
, Py_ssize_t str_len
,
12 const STRINGLIB_CHAR
* sub
, Py_ssize_t sub_len
,
22 pos
= fastsearch(str
, str_len
, sub
, sub_len
, -1, FAST_SEARCH
);
30 Py_LOCAL_INLINE(Py_ssize_t
)
31 stringlib_rfind(const STRINGLIB_CHAR
* str
, Py_ssize_t str_len
,
32 const STRINGLIB_CHAR
* sub
, Py_ssize_t sub_len
,
40 return str_len
+ offset
;
42 pos
= fastsearch(str
, str_len
, sub
, sub_len
, -1, FAST_RSEARCH
);
50 /* helper macro to fixup start/end slice values */
51 #define ADJUST_INDICES(start, end, len) \
65 Py_LOCAL_INLINE(Py_ssize_t
)
66 stringlib_find_slice(const STRINGLIB_CHAR
* str
, Py_ssize_t str_len
,
67 const STRINGLIB_CHAR
* sub
, Py_ssize_t sub_len
,
68 Py_ssize_t start
, Py_ssize_t end
)
70 ADJUST_INDICES(start
, end
, str_len
);
71 return stringlib_find(str
+ start
, end
- start
, sub
, sub_len
, start
);
74 Py_LOCAL_INLINE(Py_ssize_t
)
75 stringlib_rfind_slice(const STRINGLIB_CHAR
* str
, Py_ssize_t str_len
,
76 const STRINGLIB_CHAR
* sub
, Py_ssize_t sub_len
,
77 Py_ssize_t start
, Py_ssize_t end
)
79 ADJUST_INDICES(start
, end
, str_len
);
80 return stringlib_rfind(str
+ start
, end
- start
, sub
, sub_len
, start
);
83 #ifdef STRINGLIB_WANT_CONTAINS_OBJ
86 stringlib_contains_obj(PyObject
* str
, PyObject
* sub
)
88 return stringlib_find(
89 STRINGLIB_STR(str
), STRINGLIB_LEN(str
),
90 STRINGLIB_STR(sub
), STRINGLIB_LEN(sub
), 0
94 #endif /* STRINGLIB_WANT_CONTAINS_OBJ */
97 This function is a helper for the "find" family (find, rfind, index,
98 rindex) and for count, startswith and endswith, because they all have
99 the same behaviour for the arguments.
101 It does not touch the variables received until it knows everything
105 #define FORMAT_BUFFER_SIZE 50
108 stringlib_parse_args_finds(const char * function_name
, PyObject
*args
,
110 Py_ssize_t
*start
, Py_ssize_t
*end
)
112 PyObject
*tmp_subobj
;
113 Py_ssize_t tmp_start
= 0;
114 Py_ssize_t tmp_end
= PY_SSIZE_T_MAX
;
115 PyObject
*obj_start
=Py_None
, *obj_end
=Py_None
;
116 char format
[FORMAT_BUFFER_SIZE
] = "O|OO:";
117 size_t len
= strlen(format
);
119 strncpy(format
+ len
, function_name
, FORMAT_BUFFER_SIZE
- len
- 1);
120 format
[FORMAT_BUFFER_SIZE
- 1] = '\0';
122 if (!PyArg_ParseTuple(args
, format
, &tmp_subobj
, &obj_start
, &obj_end
))
125 /* To support None in "start" and "end" arguments, meaning
126 the same as if they were not passed.
128 if (obj_start
!= Py_None
)
129 if (!_PyEval_SliceIndex(obj_start
, &tmp_start
))
131 if (obj_end
!= Py_None
)
132 if (!_PyEval_SliceIndex(obj_end
, &tmp_end
))
137 *subobj
= tmp_subobj
;
141 #undef FORMAT_BUFFER_SIZE
143 #if STRINGLIB_IS_UNICODE
146 Wraps stringlib_parse_args_finds() and additionally ensures that the
147 first argument is a unicode object.
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).
156 stringlib_parse_args_finds_unicode(const char * function_name
, PyObject
*args
,
157 PyUnicodeObject
**substring
,
158 Py_ssize_t
*start
, Py_ssize_t
*end
)
160 PyObject
*tmp_substring
;
162 if(stringlib_parse_args_finds(function_name
, args
, &tmp_substring
,
164 tmp_substring
= PyUnicode_FromObject(tmp_substring
);
167 *substring
= (PyUnicodeObject
*)tmp_substring
;
173 #endif /* STRINGLIB_IS_UNICODE */
175 #endif /* STRINGLIB_FIND_H */