Commit dbdd8b14 authored by Stefan Behnel's avatar Stefan Behnel

fix f-string formatting of '0'

parent 74335c2b
...@@ -634,12 +634,6 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid ...@@ -634,12 +634,6 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
format_char = 'x'; format_char = 'x';
}; };
// single character unicode strings are cached in CPython => use PyUnicode_FromOrdinal() for them
if (unlikely((is_unsigned || value >= const_zero) && (width <= 1) && (
(format_char == 'o') ? value <= 7 : (format_char == 'd') ? value <= 9 : value <= 15))) {
return PyUnicode_FromOrdinal(hex_digits[((int) value)]);
}
// surprise: even trivial sprintf() calls don't get optimised in gcc (4.8) // surprise: even trivial sprintf() calls don't get optimised in gcc (4.8)
remaining = value; /* not using abs(value) to avoid overflow problems */ remaining = value; /* not using abs(value) to avoid overflow problems */
last_one_off = 0; last_one_off = 0;
...@@ -673,6 +667,8 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid ...@@ -673,6 +667,8 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
if (last_one_off) { if (last_one_off) {
assert(*dpos == '0'); assert(*dpos == '0');
dpos++; dpos++;
} else if (unlikely(dpos == end)) {
*(--dpos) = '0';
} }
length = end - dpos; length = end - dpos;
ulength = length; ulength = length;
...@@ -689,6 +685,10 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid ...@@ -689,6 +685,10 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
if (width > ulength) { if (width > ulength) {
ulength = width; ulength = width;
} }
// single character unicode strings are cached in CPython => use PyUnicode_FromOrdinal() for them
if (ulength == 1) {
return PyUnicode_FromOrdinal(*dpos);
}
return __Pyx_PyUnicode_BuildFromAscii(ulength, dpos, length, prepend_sign, padding_char); return __Pyx_PyUnicode_BuildFromAscii(ulength, dpos, length, prepend_sign, padding_char);
} }
......
...@@ -67,6 +67,16 @@ def format_c_numbers(signed char c, short s, int n, long l, float f, double d): ...@@ -67,6 +67,16 @@ def format_c_numbers(signed char c, short s, int n, long l, float f, double d):
>>> print(s4) >>> print(s4)
-C-14-3.14 -C-14-3.14
>>> s1, s2, s3, s4 = format_c_numbers(0, 0, 0, 0, -2.3456, -0.1415926)
>>> print(s1)
0 000-2.35
>>> print(s2)
-0.142-2.3
>>> print(s3)
0f
>>> print(s4)
00000-0.142
""" """
s1 = f"{c}{s:4}{l}{n}{f:.3}" s1 = f"{c}{s:4}{l}{n}{f:.3}"
assert isinstance(s1, unicode), type(s1) assert isinstance(s1, unicode), type(s1)
...@@ -102,6 +112,34 @@ def format_c_numbers_max(int n, long l): ...@@ -102,6 +112,34 @@ def format_c_numbers_max(int n, long l):
return s1, s2 return s1, s2
def format_c_number_range(int n):
"""
>>> for i in range(-1000, 1000):
... assert format_c_number_range(i) == str(i)
"""
return f'{n}'
def format_c_number_range_width(int n):
"""
>>> for i in range(-1000, 1000):
... assert format_c_number_range_width(i) == '%04d' % i, format_c_number_range_width(i)
"""
return f'{n:04}'
def format_c_number_range_dyn_width(int n, int width):
"""
>>> for i in range(-1000, 1000):
... assert format_c_number_range_dyn_width(i, 0) == str(i), format_c_number_range_dyn_width(i, 0)
... assert format_c_number_range_dyn_width(i, 1) == '%01d' % i, format_c_number_range_dyn_width(i, 1)
... assert format_c_number_range_dyn_width(i, 4) == '%04d' % i, format_c_number_range_dyn_width(i, 4)
... assert format_c_number_range_dyn_width(i, 5) == '%05d' % i, format_c_number_range_dyn_width(i, 5)
... assert format_c_number_range_dyn_width(i, 6) == '%06d' % i, format_c_number_range_dyn_width(i, 6)
"""
return f'{n:0{width}}'
def format_bool(bint x): def format_bool(bint x):
""" """
>>> a, b, c, d = format_bool(1) >>> a, b, c, d = format_bool(1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment