mirror of
https://github.com/pocketpy/pocketpy
synced 2026-03-21 20:50:16 +00:00
Compare commits
3 Commits
1cd56fbcc5
...
834986dd5b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
834986dd5b | ||
|
|
35917fc5f4 | ||
|
|
1d33267092 |
@ -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);
|
||||||
|
|||||||
@ -569,6 +569,24 @@ static bool dict_pop(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool dict_popitem(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(1);
|
||||||
|
Dict* self = py_touserdata(argv);
|
||||||
|
for(int i = self->entries.length - 1; i >= 0; i--) {
|
||||||
|
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
|
||||||
|
if(py_isnil(&entry->key)) continue;
|
||||||
|
py_Ref p = py_newtuple(py_pushtmp(), 2);
|
||||||
|
p[0] = entry->key;
|
||||||
|
p[1] = entry->val;
|
||||||
|
int res = Dict__pop(self, &p[0]);
|
||||||
|
c11__rtassert(res == 1);
|
||||||
|
py_assign(py_retval(), py_peek(-1));
|
||||||
|
py_pop();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return KeyError(py_None());
|
||||||
|
}
|
||||||
|
|
||||||
static bool dict_keys(int argc, py_Ref argv) {
|
static bool dict_keys(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
Dict* self = py_touserdata(argv);
|
Dict* self = py_touserdata(argv);
|
||||||
@ -616,6 +634,7 @@ py_Type pk_dict__register() {
|
|||||||
py_bindmethod(type, "update", dict_update);
|
py_bindmethod(type, "update", dict_update);
|
||||||
py_bindmethod(type, "get", dict_get);
|
py_bindmethod(type, "get", dict_get);
|
||||||
py_bindmethod(type, "pop", dict_pop);
|
py_bindmethod(type, "pop", dict_pop);
|
||||||
|
py_bindmethod(type, "popitem", dict_popitem);
|
||||||
py_bindmethod(type, "keys", dict_keys);
|
py_bindmethod(type, "keys", dict_keys);
|
||||||
py_bindmethod(type, "values", dict_values);
|
py_bindmethod(type, "values", dict_values);
|
||||||
py_bindmethod(type, "items", dict_items);
|
py_bindmethod(type, "items", dict_items);
|
||||||
|
|||||||
@ -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));
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -142,6 +142,17 @@ for i in range(n):
|
|||||||
del a[str(i)]
|
del a[str(i)]
|
||||||
assert len(a) == 0
|
assert len(a) == 0
|
||||||
|
|
||||||
|
# test popitem
|
||||||
|
n = 2 ** 17
|
||||||
|
a = {}
|
||||||
|
for i in range(n):
|
||||||
|
a[str(i)] = i
|
||||||
|
for i in range(n):
|
||||||
|
k, v = a.popitem()
|
||||||
|
assert k == str(n - 1 - i)
|
||||||
|
assert v == n - 1 - i
|
||||||
|
assert len(a) == 0
|
||||||
|
|
||||||
# test del with int keys
|
# test del with int keys
|
||||||
if 0:
|
if 0:
|
||||||
n = 2 ** 17
|
n = 2 ** 17
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user