fix a bug of string interop

This commit is contained in:
BLUELOVETH 2023-07-06 19:48:39 +08:00
parent 890d5a8662
commit 7993eb9000
2 changed files with 8 additions and 2 deletions

View File

@ -398,7 +398,11 @@ PyObject* VM::format(Str spec, PyObject* obj){
int width, precision;
try{
if(dot >= 0){
width = Number::stoi(spec.substr(0, dot).str());
if(dot == 0){
width = -1;
}else{
width = Number::stoi(spec.substr(0, dot).str());
}
precision = Number::stoi(spec.substr(dot+1).str());
}else{
width = Number::stoi(spec.str());
@ -424,7 +428,7 @@ PyObject* VM::format(Str spec, PyObject* obj){
}else{
ret = CAST(Str&, py_str(obj));
}
if(width > ret.length()){
if(width != -1 && width > ret.length()){
int pad = width - ret.length();
std::string padding(pad, pad_c);
if(align == '>') ret = padding.c_str() + ret;

View File

@ -42,6 +42,8 @@ assert f'{a:010}' == '0000000010'
assert f'{a:010d}' == '0000000010'
assert f'{a:010f}' == '010.000000'
assert f'{a:010.2f}' == '0000010.00'
assert f'{a:.2f}' == '10.00'
assert f'{a:.5f}' == '10.00000'
b = '123'
assert f'{b:10}' == '123 '