mirror of
https://github.com/python/cpython.git
synced 2025-11-08 21:52:45 +00:00
Added rindex(). index() and rindex() interpret negative start index
as normal indexing does.
This commit is contained in:
parent
06191530b6
commit
ee9012f58f
1 changed files with 41 additions and 5 deletions
|
|
@ -79,7 +79,6 @@ strop_splitfields(self, args)
|
||||||
{
|
{
|
||||||
int len, n, i, j;
|
int len, n, i, j;
|
||||||
char *s, *sub;
|
char *s, *sub;
|
||||||
char c;
|
|
||||||
object *list, *item;
|
object *list, *item;
|
||||||
|
|
||||||
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
|
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
|
||||||
|
|
@ -189,6 +188,8 @@ strop_index(self, args)
|
||||||
int len, n, i;
|
int len, n, i;
|
||||||
|
|
||||||
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
||||||
|
if (i < 0)
|
||||||
|
i += len;
|
||||||
if (i < 0 || i+n > len) {
|
if (i < 0 || i+n > len) {
|
||||||
err_setstr(ValueError, "start offset out of range");
|
err_setstr(ValueError, "start offset out of range");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -205,12 +206,46 @@ strop_index(self, args)
|
||||||
return newintobject((long)i);
|
return newintobject((long)i);
|
||||||
|
|
||||||
len -= n;
|
len -= n;
|
||||||
for (; i <= len; i++) {
|
for (; i <= len; ++i)
|
||||||
if (s[i] == sub[0]) {
|
if (s[i] == sub[0] &&
|
||||||
if (n == 1 || strncmp(s+i, sub, n) == 0)
|
(n == 1 || strncmp(&s[i+1], &sub[1], n-1) == 0))
|
||||||
return newintobject((long)i);
|
return newintobject((long)i);
|
||||||
|
|
||||||
|
err_setstr(ValueError, "substring not found");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static object *
|
||||||
|
strop_rindex(self, args)
|
||||||
|
object *self; /* Not used */
|
||||||
|
object *args;
|
||||||
|
{
|
||||||
|
char *s, *sub;
|
||||||
|
int len, n, i;
|
||||||
|
|
||||||
|
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
||||||
|
if (i < 0)
|
||||||
|
i += len;
|
||||||
|
if (i < 0 || i+n > len) {
|
||||||
|
err_setstr(ValueError, "start offset out of range");
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
err_clear();
|
||||||
|
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
|
||||||
|
return NULL;
|
||||||
|
i = len;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n == 0)
|
||||||
|
return newintobject((long)i);
|
||||||
|
|
||||||
|
for (i -= n; i >= 0; --i)
|
||||||
|
if (s[i] == sub[0] &&
|
||||||
|
(n == 1 || strncmp(&s[i+1], &sub[1], n-1) == 0))
|
||||||
|
return newintobject((long)i);
|
||||||
|
|
||||||
err_setstr(ValueError, "substring not found");
|
err_setstr(ValueError, "substring not found");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
@ -365,6 +400,7 @@ static struct methodlist strop_methods[] = {
|
||||||
{"index", strop_index},
|
{"index", strop_index},
|
||||||
{"joinfields", strop_joinfields},
|
{"joinfields", strop_joinfields},
|
||||||
{"lower", strop_lower},
|
{"lower", strop_lower},
|
||||||
|
{"rindex", strop_rindex},
|
||||||
{"split", strop_split},
|
{"split", strop_split},
|
||||||
{"splitfields", strop_splitfields},
|
{"splitfields", strop_splitfields},
|
||||||
{"strip", strop_strip},
|
{"strip", strop_strip},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue