diff --git a/src/bindings/py_str.c b/src/bindings/py_str.c index 7b71de89..ea300b72 100644 --- a/src/bindings/py_str.c +++ b/src/bindings/py_str.c @@ -447,6 +447,12 @@ static bool str_zfill(int argc, py_Ref argv) { } c11_sbuf buf; c11_sbuf__ctor(&buf); + // a leading sign is kept in front; the padding goes after it + if(self.size > 0 && (self.data[0] == '+' || self.data[0] == '-')) { + c11_sbuf__write_char(&buf, self.data[0]); + self.data++; + self.size--; + } for(int i = 0; i < delta; i++) { c11_sbuf__write_char(&buf, '0'); } diff --git a/tests/041_str.py b/tests/041_str.py index 475d15c2..f7fda43a 100644 --- a/tests/041_str.py +++ b/tests/041_str.py @@ -112,6 +112,9 @@ assert s2.join( seq ) == "runoob" assert 'x'.zfill(5) == '0000x' assert '568'.zfill(1) == '568' +assert '-5'.zfill(4) == '-005' +assert '+5'.zfill(4) == '+005' +assert '-'.zfill(3) == '-00' num = 6 assert str(num) == '6'