mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
Now in find, rfind, index, and rindex, you can use None as defaults,
as usual with slicing (both with str and unicode strings). This fixes issue 1259. For str only the stringobject.c file was modified. But for unicode, I needed to repeat in the four functions a lot of code, so created a new function that does part of the job for them (and placed it in find.h, following a suggestion of Barry). Also added tests for this behaviour.
This commit is contained in:
parent
5397fd1a51
commit
57d5669f4b
4 changed files with 102 additions and 32 deletions
|
@ -6040,16 +6040,12 @@ static PyObject *
|
|||
unicode_find(PyUnicodeObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *substring;
|
||||
Py_ssize_t start = 0;
|
||||
Py_ssize_t end = PY_SSIZE_T_MAX;
|
||||
Py_ssize_t start;
|
||||
Py_ssize_t end;
|
||||
Py_ssize_t result;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O|O&O&:find", &substring,
|
||||
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
|
||||
if (!_ParseTupleFinds(args, &substring, &start, &end))
|
||||
return NULL;
|
||||
substring = PyUnicode_FromObject(substring);
|
||||
if (!substring)
|
||||
return NULL;
|
||||
|
||||
result = stringlib_find_slice(
|
||||
PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
|
||||
|
@ -6110,15 +6106,11 @@ unicode_index(PyUnicodeObject *self, PyObject *args)
|
|||
{
|
||||
Py_ssize_t result;
|
||||
PyObject *substring;
|
||||
Py_ssize_t start = 0;
|
||||
Py_ssize_t end = PY_SSIZE_T_MAX;
|
||||
Py_ssize_t start;
|
||||
Py_ssize_t end;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O|O&O&:index", &substring,
|
||||
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
|
||||
if (!_ParseTupleFinds(args, &substring, &start, &end))
|
||||
return NULL;
|
||||
substring = PyUnicode_FromObject(substring);
|
||||
if (!substring)
|
||||
return NULL;
|
||||
|
||||
result = stringlib_find_slice(
|
||||
PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
|
||||
|
@ -6781,16 +6773,12 @@ static PyObject *
|
|||
unicode_rfind(PyUnicodeObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *substring;
|
||||
Py_ssize_t start = 0;
|
||||
Py_ssize_t end = PY_SSIZE_T_MAX;
|
||||
Py_ssize_t start;
|
||||
Py_ssize_t end;
|
||||
Py_ssize_t result;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O|O&O&:rfind", &substring,
|
||||
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
|
||||
return NULL;
|
||||
substring = PyUnicode_FromObject(substring);
|
||||
if (!substring)
|
||||
return NULL;
|
||||
if (!_ParseTupleFinds(args, &substring, &start, &end))
|
||||
return NULL;
|
||||
|
||||
result = stringlib_rfind_slice(
|
||||
PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
|
||||
|
@ -6812,16 +6800,12 @@ static PyObject *
|
|||
unicode_rindex(PyUnicodeObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *substring;
|
||||
Py_ssize_t start = 0;
|
||||
Py_ssize_t end = PY_SSIZE_T_MAX;
|
||||
Py_ssize_t start;
|
||||
Py_ssize_t end;
|
||||
Py_ssize_t result;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O|O&O&:rindex", &substring,
|
||||
_PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
|
||||
return NULL;
|
||||
substring = PyUnicode_FromObject(substring);
|
||||
if (!substring)
|
||||
return NULL;
|
||||
if (!_ParseTupleFinds(args, &substring, &start, &end))
|
||||
return NULL;
|
||||
|
||||
result = stringlib_rfind_slice(
|
||||
PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue