Compare commits

...

3 Commits

Author SHA1 Message Date
Tanisha Vasudeva
b33ac31726
Merge 6cc3d8023350163e72b83d49981ad2c4b08d39b4 into 35917fc5f4b25375169e5e2417ae659ef0528200 2026-03-06 11:11:04 +05:30
blueloveTH
35917fc5f4 fix #468 2026-03-04 14:30:35 +08:00
tanisha
6cc3d80233 Added readline() 2026-02-28 20:28:04 +05:30
5 changed files with 33 additions and 2 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) {
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);

View File

@ -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);

View File

@ -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));

View File

@ -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

View File

@ -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