mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
temp fix a bug of f"{stack[2:]}"
This commit is contained in:
parent
7f52726b5b
commit
0419eb5bb0
14
src/expr.cpp
14
src/expr.cpp
@ -406,16 +406,28 @@ namespace pkpy{
|
|||||||
int count = 0; // how many string parts
|
int count = 0; // how many string parts
|
||||||
bool flag = false; // true if we are in a expression
|
bool flag = false; // true if we are in a expression
|
||||||
|
|
||||||
|
const char* fmt_valid_chars = "0-=*#@!~" "<>^" ".fds" "0123456789";
|
||||||
|
PK_LOCAL_STATIC const std::set<char> fmt_valid_char_set(fmt_valid_chars, fmt_valid_chars + strlen(fmt_valid_chars));
|
||||||
|
|
||||||
while(j < src.size){
|
while(j < src.size){
|
||||||
if(flag){
|
if(flag){
|
||||||
if(src[j] == '}'){
|
if(src[j] == '}'){
|
||||||
// add expression
|
// add expression
|
||||||
Str expr = src.substr(i, j-i);
|
Str expr = src.substr(i, j-i);
|
||||||
|
// BUG: ':' is not a format specifier in f"{stack[2:]}"
|
||||||
int conon = expr.index(":");
|
int conon = expr.index(":");
|
||||||
if(conon >= 0){
|
if(conon >= 0){
|
||||||
_load_simple_expr(ctx, expr.substr(0, conon));
|
|
||||||
Str spec = expr.substr(conon+1);
|
Str spec = expr.substr(conon+1);
|
||||||
|
// filter some invalid spec
|
||||||
|
bool ok = true;
|
||||||
|
for(char c: spec) if(!fmt_valid_char_set.count(c)){ ok = false; break; }
|
||||||
|
if(ok){
|
||||||
|
_load_simple_expr(ctx, expr.substr(0, conon));
|
||||||
ctx->emit_(OP_FORMAT_STRING, ctx->add_const(VAR(spec)), line);
|
ctx->emit_(OP_FORMAT_STRING, ctx->add_const(VAR(spec)), line);
|
||||||
|
}else{
|
||||||
|
// ':' is not a spec indicator
|
||||||
|
_load_simple_expr(ctx, expr);
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
_load_simple_expr(ctx, expr);
|
_load_simple_expr(ctx, expr);
|
||||||
}
|
}
|
||||||
|
@ -153,3 +153,6 @@ a = 'b'
|
|||||||
assert list(a) == ['b']
|
assert list(a) == ['b']
|
||||||
a = '测'
|
a = '测'
|
||||||
assert list(a) == ['测']
|
assert list(a) == ['测']
|
||||||
|
|
||||||
|
assert '\b\b\b' == '\x08\x08\x08'
|
||||||
|
stack=[1,2,3,4]; assert f"{stack[2:]}" == '[3, 4]'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user