mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-19 19:10:17 +00:00
Compare commits
2 Commits
b6cefdeedc
...
ccd00e83a5
Author | SHA1 | Date | |
---|---|---|---|
|
ccd00e83a5 | ||
|
d5a511ad7c |
@ -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);
|
||||
|
@ -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));
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user