Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
3279482a
Commit
3279482a
authored
Dec 24, 2017
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Micro-streamline integer string formatting for small numbers.
parent
837f5498
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
10 deletions
+42
-10
Cython/Utility/TypeConversion.c
Cython/Utility/TypeConversion.c
+6
-6
tests/run/fstring.pyx
tests/run/fstring.pyx
+36
-4
No files found.
Cython/Utility/TypeConversion.c
View file @
3279482a
...
...
@@ -645,7 +645,8 @@ static const char DIGIT_PAIRS_8[2*8*8+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
if
(
format_char
==
'X'
)
{
hex_digits
+=
16
;
format_char
=
'x'
;
}
;
}
// surprise: even trivial sprintf() calls don't get optimised in gcc (4.8)
remaining
=
value
;
/* not using abs(value) to avoid overflow problems */
last_one_off
=
0
;
dpos
=
end
;
while
(
remaining
!=
0
)
{
do
{
int
digit_pos
;
switch
(
format_char
)
{
case
'o'
:
...
...
@@ -717,12 +718,11 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value, Py_ssize_t wid
assert
(
0
);
break
;
}
}
}
while
(
unlikely
(
remaining
!=
0
));
if
(
last_one_off
)
{
assert
(
*
dpos
==
'0'
);
dpos
++
;
}
else
if
(
unlikely
(
dpos
==
end
))
{
*
(
--
dpos
)
=
'0'
;
}
length
=
end
-
dpos
;
ulength
=
length
;
...
...
tests/run/fstring.pyx
View file @
3279482a
...
...
@@ -157,7 +157,7 @@ def format_c_number_const():
def
format_c_number_range
(
int
n
):
"""
>>> for i in range(-1000, 100
0
):
>>> for i in range(-1000, 100
1
):
... assert format_c_number_range(i) == str(i)
"""
return
f'
{
n
}
'
...
...
@@ -165,15 +165,47 @@ def format_c_number_range(int 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)
>>> for i in range(-1000, 1001):
... formatted = format_c_number_range_width(i)
... expected = '{:04d}'.format(i)
... assert formatted == expected, "%r != %r" % (formatted, expected)
"""
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
):
"""
>>> for i in range(-1000, 100
0
):
>>> for i in range(-1000, 100
1
):
... 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)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment