temp fix a bug of f"{stack[2:]}"

This commit is contained in:
blueloveTH 2023-12-19 20:21:17 +08:00
parent 7f52726b5b
commit 0419eb5bb0
2 changed files with 17 additions and 2 deletions

View File

@ -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);
ctx->emit_(OP_FORMAT_STRING, ctx->add_const(VAR(spec)), line); // 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);
}else{
// ':' is not a spec indicator
_load_simple_expr(ctx, expr);
}
}else{ }else{
_load_simple_expr(ctx, expr); _load_simple_expr(ctx, expr);
} }

View File

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