Commit 3279482a authored by Stefan Behnel's avatar Stefan Behnel

Micro-streamline integer string formatting for small numbers.

parent 837f5498
...@@ -645,7 +645,8 @@ static const char DIGIT_PAIRS_8[2*8*8+1] = { ...@@ -645,7 +645,8 @@ static const char DIGIT_PAIRS_8[2*8*8+1] = {
}; };
static const char DIGITS_HEX[2*16+1] = { static const char DIGITS_HEX[2*16+1] = {
"0123456789abcdef0123456789ABCDEF" "0123456789abcdef"
"0123456789ABCDEF"
}; };
...@@ -686,13 +687,13 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid ...@@ -686,13 +687,13 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
if (format_char == 'X') { if (format_char == 'X') {
hex_digits += 16; hex_digits += 16;
format_char = 'x'; format_char = 'x';
}; }
// 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;
dpos = end; dpos = end;
while (remaining != 0) { do {
int digit_pos; int digit_pos;
switch (format_char) { switch (format_char) {
case 'o': case 'o':
...@@ -717,12 +718,11 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid ...@@ -717,12 +718,11 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
assert(0); assert(0);
break; break;
} }
} } while (unlikely(remaining != 0));
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;
......
...@@ -157,7 +157,7 @@ def format_c_number_const(): ...@@ -157,7 +157,7 @@ def format_c_number_const():
def format_c_number_range(int n): def format_c_number_range(int n):
""" """
>>> for i in range(-1000, 1000): >>> for i in range(-1000, 1001):
... assert format_c_number_range(i) == str(i) ... assert format_c_number_range(i) == str(i)
""" """
return f'{n}' return f'{n}'
...@@ -165,15 +165,47 @@ def format_c_number_range(int n): ...@@ -165,15 +165,47 @@ def format_c_number_range(int n):
def format_c_number_range_width(int n): def format_c_number_range_width(int n):
""" """
>>> for i in range(-1000, 1000): >>> for i in range(-1000, 1001):
... assert format_c_number_range_width(i) == '%04d' % i, format_c_number_range_width(i) ... formatted = format_c_number_range_width(i)
... expected = '{:04d}'.format(i)
... assert formatted == expected, "%r != %r" % (formatted, expected)
""" """
return f'{n:04}' return f'{n:04}'
def format_c_number_range_width0(int n):
"""
>>> for i in range(-100, 101):
... formatted = format_c_number_range_width0(i)
... expected = '{:00d}'.format(i)
... assert formatted == expected, "%r != %r" % (formatted, expected)
"""
return f'{n:00}'
def format_c_number_range_width1(int n):
"""
>>> for i in range(-100, 101):
... formatted = format_c_number_range_width1(i)
... expected = '{:01d}'.format(i)
... assert formatted == expected, "%r != %r" % (formatted, expected)
"""
return f'{n:01}'
def format_c_number_range_width_m4(int n):
"""
>>> for i in range(-100, 101):
... formatted = format_c_number_range_width_m4(i)
... expected = '{:-4d}'.format(i)
... assert formatted == expected, "%r != %r" % (formatted, expected)
"""
return f'{n:-4}'
def format_c_number_range_dyn_width(int n, int width): def format_c_number_range_dyn_width(int n, int width):
""" """
>>> for i in range(-1000, 1000): >>> for i in range(-1000, 1001):
... 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, 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, 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, 4) == '%04d' % i, format_c_number_range_dyn_width(i, 4)
......
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