diff --git a/src/vm.cpp b/src/vm.cpp index 269832f6..17864776 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -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; diff --git a/tests/25_rawstring.py b/tests/25_rawstring.py index 07cd31bd..6c889892 100644 --- a/tests/25_rawstring.py +++ b/tests/25_rawstring.py @@ -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 '