mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 12:30:19 +00:00
rename subscr to slice
This commit is contained in:
parent
f0ec979815
commit
07e07831c3
@ -31,8 +31,8 @@ void pkpy_Str__dtor(pkpy_Str* self);
|
|||||||
pkpy_Str pkpy_Str__copy(const pkpy_Str* self);
|
pkpy_Str pkpy_Str__copy(const pkpy_Str* self);
|
||||||
pkpy_Str pkpy_Str__concat(const pkpy_Str* self, const pkpy_Str* other);
|
pkpy_Str pkpy_Str__concat(const pkpy_Str* self, const pkpy_Str* other);
|
||||||
pkpy_Str pkpy_Str__concat2(const pkpy_Str* self, const char* other, int size);
|
pkpy_Str pkpy_Str__concat2(const pkpy_Str* self, const char* other, int size);
|
||||||
pkpy_Str pkpy_Str__substr(const pkpy_Str* self, int start);
|
pkpy_Str pkpy_Str__slice(const pkpy_Str* self, int start);
|
||||||
pkpy_Str pkpy_Str__substr2(const pkpy_Str* self, int start, int size);
|
pkpy_Str pkpy_Str__slice2(const pkpy_Str* self, int start, int stop);
|
||||||
pkpy_Str pkpy_Str__lower(const pkpy_Str* self);
|
pkpy_Str pkpy_Str__lower(const pkpy_Str* self);
|
||||||
pkpy_Str pkpy_Str__upper(const pkpy_Str* self);
|
pkpy_Str pkpy_Str__upper(const pkpy_Str* self);
|
||||||
pkpy_Str pkpy_Str__escape(const pkpy_Str* self, char quote);
|
pkpy_Str pkpy_Str__escape(const pkpy_Str* self, char quote);
|
||||||
|
@ -149,12 +149,16 @@ struct Str: pkpy_Str {
|
|||||||
return std::string(pkpy_Str__data(this), size);
|
return std::string(pkpy_Str__data(this), size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Str substr(int start, int size) const{
|
Str slice(int start, int stop) const{
|
||||||
return pkpy_Str__substr2(this, start, size);
|
return pkpy_Str__slice2(this, start, stop);
|
||||||
|
}
|
||||||
|
|
||||||
|
Str slice(int start) const{
|
||||||
|
return pkpy_Str__slice(this, start);
|
||||||
}
|
}
|
||||||
|
|
||||||
Str substr(int start) const{
|
Str substr(int start) const{
|
||||||
return pkpy_Str__substr(this, start);
|
return pkpy_Str__slice(this, start);
|
||||||
}
|
}
|
||||||
|
|
||||||
Str strip(bool left, bool right, const Str& chars) const;
|
Str strip(bool left, bool right, const Str& chars) const;
|
||||||
|
@ -90,13 +90,14 @@ pkpy_Str pkpy_Str__concat2(const pkpy_Str *self, const char *other, int size){
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
pkpy_Str pkpy_Str__substr(const pkpy_Str *self, int start){
|
pkpy_Str pkpy_Str__slice(const pkpy_Str *self, int start){
|
||||||
return pkpy_Str__substr2(self, start, self->size - start);
|
return pkpy_Str__slice2(self, start, self->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
pkpy_Str pkpy_Str__substr2(const pkpy_Str *self, int start, int size){
|
pkpy_Str pkpy_Str__slice2(const pkpy_Str *self, int start, int stop){
|
||||||
pkpy_Str retval;
|
pkpy_Str retval;
|
||||||
pkpy_Str__ctor2(&retval, pkpy_Str__data(self) + start, size);
|
if(stop < start) stop = start;
|
||||||
|
pkpy_Str__ctor2(&retval, pkpy_Str__data(self) + start, stop - start);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,6 +175,45 @@ pkpy_Str pkpy_Str__escape(const pkpy_Str* self, char quote){
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pkpy_Str pkpy_Str__strip(const pkpy_Str *self, bool left, bool right){
|
||||||
|
// const char* data = pkpy_Str__data(self);
|
||||||
|
// if(self->is_ascii) {
|
||||||
|
// int L = 0;
|
||||||
|
// int R = self->size;
|
||||||
|
// if(left) {
|
||||||
|
// while(L < R && (data[L] == ' ' || data[L] == '\t' || data[L] == '\n' || data[L] == '\r'))
|
||||||
|
// L++;
|
||||||
|
// }
|
||||||
|
// if(right) {
|
||||||
|
// while(L < R && (data[R - 1] == ' ' || data[R - 1] == '\t' || data[R - 1] == '\n' || data[R - 1] == '\r'))
|
||||||
|
// R--;
|
||||||
|
// }
|
||||||
|
// return pkpy_Str__substr2(self, L, R - L);
|
||||||
|
// } else {
|
||||||
|
// pkpy_Str tmp;
|
||||||
|
// pkpy_Str__ctor(&tmp, " \t\n\r");
|
||||||
|
// pkpy_Str retval = pkpy_Str__strip2(self, left, right, &tmp);
|
||||||
|
// pkpy_Str__dtor(&tmp);
|
||||||
|
// return retval;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pkpy_Str pkpy_Str__strip2(const pkpy_Str *self, bool left, bool right, const pkpy_Str *chars){
|
||||||
|
// int L = 0;
|
||||||
|
// int R = pkpy_Str__u8_length(self);
|
||||||
|
// pkpy_Str tmp;
|
||||||
|
// if(left) {
|
||||||
|
// tmp = pkpy_Str__u8_getitem(self, L);
|
||||||
|
// while(L < R && chars.index(u8_getitem(L)) != -1)
|
||||||
|
// L++;
|
||||||
|
// }
|
||||||
|
// if(right) {
|
||||||
|
// while(L < R && chars.index(u8_getitem(R - 1)) != -1)
|
||||||
|
// R--;
|
||||||
|
// }
|
||||||
|
// return pkpy_Str__u8_slice(self, L, R, 1);
|
||||||
|
// }
|
||||||
|
|
||||||
pkpy_Str pkpy_Str__replace(const pkpy_Str *self, char old, char new_){
|
pkpy_Str pkpy_Str__replace(const pkpy_Str *self, char old, char new_){
|
||||||
pkpy_Str retval = pkpy_Str__copy(self);
|
pkpy_Str retval = pkpy_Str__copy(self);
|
||||||
char* p = (char*)pkpy_Str__data(&retval);
|
char* p = (char*)pkpy_Str__data(&retval);
|
||||||
@ -190,13 +230,13 @@ pkpy_Str pkpy_Str__replace2(const pkpy_Str *self, const pkpy_Str *old, const pkp
|
|||||||
while(true) {
|
while(true) {
|
||||||
int i = pkpy_Str__index(self, old, start);
|
int i = pkpy_Str__index(self, old, start);
|
||||||
if(i == -1) break;
|
if(i == -1) break;
|
||||||
pkpy_Str tmp = pkpy_Str__substr2(self, start, i - start);
|
pkpy_Str tmp = pkpy_Str__slice2(self, start, i);
|
||||||
c11_vector__extend(char, &buffer, pkpy_Str__data(&tmp), tmp.size);
|
c11_vector__extend(char, &buffer, pkpy_Str__data(&tmp), tmp.size);
|
||||||
pkpy_Str__dtor(&tmp);
|
pkpy_Str__dtor(&tmp);
|
||||||
c11_vector__extend(char, &buffer, pkpy_Str__data(new_), new_->size);
|
c11_vector__extend(char, &buffer, pkpy_Str__data(new_), new_->size);
|
||||||
start = i + old->size;
|
start = i + old->size;
|
||||||
}
|
}
|
||||||
pkpy_Str tmp = pkpy_Str__substr2(self, start, self->size - start);
|
pkpy_Str tmp = pkpy_Str__slice2(self, start, self->size);
|
||||||
c11_vector__extend(char, &buffer, pkpy_Str__data(&tmp), tmp.size);
|
c11_vector__extend(char, &buffer, pkpy_Str__data(&tmp), tmp.size);
|
||||||
pkpy_Str__dtor(&tmp);
|
pkpy_Str__dtor(&tmp);
|
||||||
c11_vector__push(char, &buffer, '\0');
|
c11_vector__push(char, &buffer, '\0');
|
||||||
@ -221,10 +261,8 @@ int pkpy_Str__cmp2(const pkpy_Str *self, const char *other, int size){
|
|||||||
|
|
||||||
pkpy_Str pkpy_Str__u8_getitem(const pkpy_Str *self, int i){
|
pkpy_Str pkpy_Str__u8_getitem(const pkpy_Str *self, int i){
|
||||||
i = pkpy_Str__unicode_index_to_byte(self, i);
|
i = pkpy_Str__unicode_index_to_byte(self, i);
|
||||||
return pkpy_Str__substr2(
|
int size = pkpy_utils__u8_header(pkpy_Str__data(self)[i], false);
|
||||||
self, i,
|
return pkpy_Str__slice2(self, i, i + size);
|
||||||
pkpy_utils__u8_header(pkpy_Str__data(self)[i], false)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pkpy_Str pkpy_Str__u8_slice(const pkpy_Str *self, int start, int stop, int step){
|
pkpy_Str pkpy_Str__u8_slice(const pkpy_Str *self, int start, int stop, int step){
|
||||||
|
@ -50,7 +50,7 @@ Str Str::strip(bool left, bool right) const {
|
|||||||
while(L < R && (data[R - 1] == ' ' || data[R - 1] == '\t' || data[R - 1] == '\n' || data[R - 1] == '\r'))
|
while(L < R && (data[R - 1] == ' ' || data[R - 1] == '\t' || data[R - 1] == '\n' || data[R - 1] == '\r'))
|
||||||
R--;
|
R--;
|
||||||
}
|
}
|
||||||
return substr(L, R - L);
|
return slice(L, R);
|
||||||
} else {
|
} else {
|
||||||
return strip(left, right, " \t\n\r");
|
return strip(left, right, " \t\n\r");
|
||||||
}
|
}
|
||||||
|
@ -414,11 +414,11 @@ void FStringExpr::_load_simple_expr(CodeEmitContext* ctx, Str expr) {
|
|||||||
switch(expr.end()[-1]) {
|
switch(expr.end()[-1]) {
|
||||||
case 'r':
|
case 'r':
|
||||||
repr = true;
|
repr = true;
|
||||||
expr = expr.substr(0, expr.size - 2);
|
expr = expr.slice(0, expr.size - 2);
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
repr = false;
|
repr = false;
|
||||||
expr = expr.substr(0, expr.size - 2);
|
expr = expr.slice(0, expr.size - 2);
|
||||||
break;
|
break;
|
||||||
default: break; // nothing happens
|
default: break; // nothing happens
|
||||||
}
|
}
|
||||||
@ -472,7 +472,7 @@ void FStringExpr::emit_(CodeEmitContext* ctx) {
|
|||||||
if(flag) {
|
if(flag) {
|
||||||
if(src[j] == '}') {
|
if(src[j] == '}') {
|
||||||
// add expression
|
// add expression
|
||||||
Str expr = src.substr(i, j - i);
|
Str expr = src.slice(i, j);
|
||||||
// BUG: ':' is not a format specifier in f"{stack[2:]}"
|
// BUG: ':' is not a format specifier in f"{stack[2:]}"
|
||||||
int conon = expr.index(":");
|
int conon = expr.index(":");
|
||||||
if(conon >= 0) {
|
if(conon >= 0) {
|
||||||
@ -485,7 +485,7 @@ void FStringExpr::emit_(CodeEmitContext* ctx) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(ok) {
|
if(ok) {
|
||||||
_load_simple_expr(ctx, expr.substr(0, conon));
|
_load_simple_expr(ctx, expr.slice(0, conon));
|
||||||
ctx->emit_(OP_FORMAT_STRING, ctx->add_const_string(spec.sv()), line);
|
ctx->emit_(OP_FORMAT_STRING, ctx->add_const_string(spec.sv()), line);
|
||||||
} else {
|
} else {
|
||||||
// ':' is not a spec indicator
|
// ':' is not a spec indicator
|
||||||
@ -527,7 +527,7 @@ void FStringExpr::emit_(CodeEmitContext* ctx) {
|
|||||||
i = j;
|
i = j;
|
||||||
while(j < src.size && src[j] != '{' && src[j] != '}')
|
while(j < src.size && src[j] != '{' && src[j] != '}')
|
||||||
j++;
|
j++;
|
||||||
Str literal = src.substr(i, j - i);
|
Str literal = src.slice(i, j);
|
||||||
ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(literal.sv()), line);
|
ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(literal.sv()), line);
|
||||||
count++;
|
count++;
|
||||||
continue; // skip j++
|
continue; // skip j++
|
||||||
@ -538,7 +538,7 @@ void FStringExpr::emit_(CodeEmitContext* ctx) {
|
|||||||
|
|
||||||
if(flag) {
|
if(flag) {
|
||||||
// literal
|
// literal
|
||||||
Str literal = src.substr(i, src.size - i);
|
Str literal = src.slice(i, src.size);
|
||||||
ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(literal.sv()), line);
|
ctx->emit_(OP_LOAD_CONST, ctx->add_const_string(literal.sv()), line);
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ void StringIter::_register(VM* vm, PyObject* mod, PyObject* type) {
|
|||||||
int start = self.i;
|
int start = self.i;
|
||||||
int len = pkpy_utils__u8_header(s[self.i], false);
|
int len = pkpy_utils__u8_header(s[self.i], false);
|
||||||
self.i += len;
|
self.i += len;
|
||||||
vm->s_data.push(VAR(s.substr(start, len)));
|
vm->s_data.push(VAR(s.slice(start, self.i)));
|
||||||
return 1;
|
return 1;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -623,7 +623,7 @@ PyVar VM::__format_object(PyVar obj, Str spec) {
|
|||||||
case 'd':
|
case 'd':
|
||||||
case 's':
|
case 's':
|
||||||
type = spec.end()[-1];
|
type = spec.end()[-1];
|
||||||
spec = spec.substr(0, spec.length() - 1);
|
spec = spec.slice(0, spec.length() - 1);
|
||||||
break;
|
break;
|
||||||
default: type = ' '; break;
|
default: type = ' '; break;
|
||||||
}
|
}
|
||||||
@ -660,9 +660,9 @@ PyVar VM::__format_object(PyVar obj, Str spec) {
|
|||||||
if(dot == 0) {
|
if(dot == 0) {
|
||||||
width = -1;
|
width = -1;
|
||||||
} else {
|
} else {
|
||||||
width = std::stoi(spec.substr(0, dot).str());
|
width = std::stoi(spec.slice(0, dot).str());
|
||||||
}
|
}
|
||||||
precision = std::stoi(spec.substr(dot + 1).str());
|
precision = std::stoi(spec.slice(dot + 1).str());
|
||||||
} else {
|
} else {
|
||||||
width = std::stoi(spec.str());
|
width = std::stoi(spec.str());
|
||||||
precision = -1;
|
precision = -1;
|
||||||
@ -761,7 +761,7 @@ static std::string _opcode_argstr(VM* vm, int i, Bytecode byte, const CodeObject
|
|||||||
|
|
||||||
Str VM::disassemble(CodeObject_ co) {
|
Str VM::disassemble(CodeObject_ co) {
|
||||||
auto pad = [](const Str& s, const int n) {
|
auto pad = [](const Str& s, const int n) {
|
||||||
if(s.length() >= n) return s.substr(0, n);
|
if(s.length() >= n) return s.slice(0, n);
|
||||||
return s + std::string(n - s.length(), ' ');
|
return s + std::string(n - s.length(), ' ');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user