Compare commits

...

2 Commits

Author SHA1 Message Date
blueloveTH
ccd00e83a5 fix https://github.com/pocketpy/pocketpy/issues/336 2025-07-03 18:11:35 +08:00
blueloveTH
d5a511ad7c fix https://github.com/pocketpy/pocketpy/issues/378 2025-07-03 18:04:36 +08:00
6 changed files with 35 additions and 3 deletions

View File

@ -67,6 +67,7 @@ c11_string* c11_sv__replace2(c11_sv self, c11_sv old, c11_sv new_);
c11_vector /* T=c11_sv */ c11_sv__split(c11_sv self, char sep);
c11_vector /* T=c11_sv */ c11_sv__split2(c11_sv self, c11_sv sep);
c11_vector /* T=c11_sv */ c11_sv__splitwhitespace(c11_sv self);
// misc
int c11__unicode_index_to_byte(const char* data, int i);

View File

@ -2,6 +2,7 @@
#include "pocketpy/common/sstream.h"
#include "pocketpy/common/utils.h"
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
@ -188,6 +189,26 @@ uint64_t c11_sv__hash(c11_sv self) {
return hash;
}
c11_vector /* T=c11_sv */ c11_sv__splitwhitespace(c11_sv self) {
c11_vector retval;
c11_vector__ctor(&retval, sizeof(c11_sv));
const char* data = self.data;
int i = 0;
for(int j = 0; j < self.size; j++) {
if(isspace(data[j])) {
assert(j >= i);
c11_sv tmp = {data + i, j - i};
c11_vector__push(c11_sv, &retval, tmp);
i = j + 1;
}
}
if(i <= self.size) {
c11_sv tmp = {data + i, self.size - i};
c11_vector__push(c11_sv, &retval, tmp);
}
return retval;
}
c11_vector /* T=c11_sv */ c11_sv__split(c11_sv self, char sep) {
c11_vector retval;
c11_vector__ctor(&retval, sizeof(c11_sv));

View File

@ -2865,13 +2865,14 @@ static Error* compile_stmt(Compiler* self) {
bool is_typed_name = false; // e.g. x: int
// eat variable's type hint if it is a single name
if(Ctx__s_top(ctx())->vt->is_name) {
const ExprVt* top_vt = Ctx__s_top(ctx())->vt;
if(top_vt->is_name || top_vt->is_attrib) {
if(match(TK_COLON)) {
c11_sv type_hint;
check(consume_type_hints_sv(self, &type_hint));
is_typed_name = true;
if(ctx()->is_compiling_class) {
if(ctx()->is_compiling_class && top_vt->is_name) {
NameExpr* ne = (NameExpr*)Ctx__s_top(ctx());
int index = Ctx__add_const_string(ctx(), type_hint);
Ctx__emit_(ctx(), OP_LOAD_CONST, index, BC_KEEPLINE);

View File

@ -327,7 +327,7 @@ static bool str_split(int argc, py_Ref argv) {
if(argc > 2) return TypeError("split() takes at most 2 arguments");
if(argc == 1) {
// sep = None
res = c11_sv__split(self, ' ');
res = c11_sv__splitwhitespace(self);
discard_empty = true;
}
if(argc == 2) {

View File

@ -57,6 +57,9 @@ assert 'aa bb cccc'.split('cc') == ['aa bb ', '', '']
assert '.a.b.'.split('.') == ['', 'a', 'b', '']
assert '.a...b.'.split('.') == ['', 'a', '', '', 'b', '']
# https://github.com/pocketpy/pocketpy/issues/378
assert "a b \n c\td".split() == ['a', 'b', 'c', 'd']
try:
'a'.split('')
exit(1)

View File

@ -126,7 +126,13 @@ class MyClass:
b, c = 1, 2
d = b + c
def __init__(self, m, n) -> None:
self.m: int = m
self.n: float = n
assert MyClass.a == (1, 2, 3)
assert MyClass.b == 1
assert MyClass.c == 2
assert MyClass.d == 3
assert MyClass(1, 2).m == 1