mirror of
https://github.com/pocketpy/pocketpy
synced 2026-03-21 20:50:16 +00:00
Compare commits
3 Commits
0791ca9db7
...
b33ac31726
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b33ac31726 | ||
|
|
35917fc5f4 | ||
|
|
6cc3d80233 |
@ -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) {
|
||||
if(argc > 3) return TypeError("find() takes at most 3 arguments");
|
||||
c11_string* self = pk_tostr(&argv[0]);
|
||||
int start = 0;
|
||||
if(argc == 3) {
|
||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||
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);
|
||||
c11_string* sub = pk_tostr(&argv[1]);
|
||||
int res = c11_sv__index2(c11_string__sv(self), c11_string__sv(sub), start);
|
||||
|
||||
@ -215,7 +215,31 @@ static bool io_FileIO_flush(int argc, py_Ref argv) {
|
||||
py_newnone(py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool io_FileIO_readlines(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
py_newlist(py_retval());
|
||||
char* buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
size_t len = 0;
|
||||
int c;
|
||||
while(true) {
|
||||
len = 0;
|
||||
while((c = fgetc(ud->file)) != EOF) {
|
||||
if(len + 1 >= buf_size) {
|
||||
buf_size = (buf_size == 0) ? 64 : buf_size * 2;
|
||||
buf = PK_REALLOC(buf, buf_size);
|
||||
}
|
||||
buf[len++] = (char)c;
|
||||
if(c == '\n') break;
|
||||
}
|
||||
if(len == 0) break;
|
||||
py_newstrv(py_getreg(0), (c11_sv){buf, len});
|
||||
py_list_append(py_retval(), py_getreg(0));
|
||||
}
|
||||
if(buf) PK_FREE(buf);
|
||||
return true;
|
||||
}
|
||||
void pk__add_module_io() {
|
||||
py_Ref mod = py_newmodule("io");
|
||||
|
||||
@ -230,6 +254,7 @@ void pk__add_module_io() {
|
||||
py_bindmethod(FileIO, "tell", io_FileIO_tell);
|
||||
py_bindmethod(FileIO, "seek", io_FileIO_seek);
|
||||
py_bindmethod(FileIO, "flush", io_FileIO_flush);
|
||||
py_bindmethod(FileIO, "readlines", io_FileIO_readlines);
|
||||
|
||||
py_newint(py_emplacedict(mod, py_name("SEEK_SET")), SEEK_SET);
|
||||
py_newint(py_emplacedict(mod, py_name("SEEK_CUR")), SEEK_CUR);
|
||||
|
||||
@ -317,6 +317,8 @@ static bool list_index(int argc, py_Ref argv) {
|
||||
if(argc == 3) {
|
||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||
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++) {
|
||||
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('1', 0) == 0
|
||||
assert a.index('2', -2) == 1
|
||||
|
||||
assert a.find('1') == 0
|
||||
assert a.find('1', 1) == -1
|
||||
|
||||
@ -75,6 +75,7 @@ assert a.index(3) == 2
|
||||
|
||||
assert a.index(2, 1) == 1
|
||||
assert a.index(1, 0) == 0
|
||||
assert a.index(2, -2) == 1
|
||||
|
||||
a, b = [1, 2]
|
||||
assert a == 1 and b == 2
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user