This commit is contained in:
blueloveTH 2026-03-04 14:30:35 +08:00
parent 1d33267092
commit 35917fc5f4
4 changed files with 7 additions and 1 deletions

View File

@ -477,12 +477,14 @@ static bool str_rjust(int argc, py_Ref argv) { return str__widthjust_impl(false,
static bool str_find(int argc, py_Ref argv) { static bool str_find(int argc, py_Ref argv) {
if(argc > 3) return TypeError("find() takes at most 3 arguments"); if(argc > 3) return TypeError("find() takes at most 3 arguments");
c11_string* self = pk_tostr(&argv[0]);
int start = 0; int start = 0;
if(argc == 3) { if(argc == 3) {
PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(2, tp_int);
start = py_toint(py_arg(2)); start = py_toint(py_arg(2));
if(start < 0) start += c11_sv__u8_length(c11_string__sv(self));
if(start < 0) start = 0;
} }
c11_string* self = pk_tostr(&argv[0]);
PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(1, tp_str);
c11_string* sub = pk_tostr(&argv[1]); c11_string* sub = pk_tostr(&argv[1]);
int res = c11_sv__index2(c11_string__sv(self), c11_string__sv(sub), start); int res = c11_sv__index2(c11_string__sv(self), c11_string__sv(sub), start);

View File

@ -317,6 +317,8 @@ static bool list_index(int argc, py_Ref argv) {
if(argc == 3) { if(argc == 3) {
PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(2, tp_int);
start = py_toint(py_arg(2)); start = py_toint(py_arg(2));
if(start < 0) start += py_list_len(py_arg(0));
if(start < 0) start = 0;
} }
for(int i = start; i < py_list_len(py_arg(0)); i++) { for(int i = start; i < py_list_len(py_arg(0)); i++) {
int res = py_equal(py_list_getitem(py_arg(0), i), py_arg(1)); int res = py_equal(py_list_getitem(py_arg(0), i), py_arg(1));

View File

@ -169,6 +169,7 @@ assert a.index('23') == 1
assert a.index('2', 1) == 1 assert a.index('2', 1) == 1
assert a.index('1', 0) == 0 assert a.index('1', 0) == 0
assert a.index('2', -2) == 1
assert a.find('1') == 0 assert a.find('1') == 0
assert a.find('1', 1) == -1 assert a.find('1', 1) == -1

View File

@ -75,6 +75,7 @@ assert a.index(3) == 2
assert a.index(2, 1) == 1 assert a.index(2, 1) == 1
assert a.index(1, 0) == 0 assert a.index(1, 0) == 0
assert a.index(2, -2) == 1
a, b = [1, 2] a, b = [1, 2]
assert a == 1 and b == 2 assert a == 1 and b == 2