Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
bea57060
Commit
bea57060
authored
Jan 28, 2018
by
INADA Naoki
Committed by
GitHub
Jan 28, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32677: Optimize str.isascii() (GH-5356)
parent
ea8fc52e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
4 deletions
+43
-4
Lib/test/string_tests.py
Lib/test/string_tests.py
+7
-0
Objects/bytes_methods.c
Objects/bytes_methods.c
+36
-4
No files found.
Lib/test/string_tests.py
View file @
bea57060
...
...
@@ -916,6 +916,13 @@ class BaseTest:
self
.
checkequal
(
True
,
'
\
x00
\
x7f
'
,
'isascii'
)
self
.
checkequal
(
False
,
'
\
x80
'
,
'isascii'
)
self
.
checkequal
(
False
,
'
\
xe9
'
,
'isascii'
)
# bytes.isascii() and bytearray.isascii() has optimization which
# check 4 or 8 bytes at once. So check some alignments.
for
p
in
range
(
8
):
self
.
checkequal
(
True
,
' '
*
p
+
'
\
x7f
'
,
'isascii'
)
self
.
checkequal
(
False
,
' '
*
p
+
'
\
x80
'
,
'isascii'
)
self
.
checkequal
(
True
,
' '
*
p
+
'
\
x7f
'
+
' '
*
8
,
'isascii'
)
self
.
checkequal
(
False
,
' '
*
p
+
'
\
x80
'
+
' '
*
8
,
'isascii'
)
def
test_isdigit
(
self
):
self
.
checkequal
(
False
,
''
,
'isdigit'
)
...
...
Objects/bytes_methods.c
View file @
bea57060
...
...
@@ -98,19 +98,51 @@ PyDoc_STRVAR_shared(_Py_isascii__doc__,
Return True if B is empty or all characters in B are ASCII,
\n
\
False otherwise."
);
// Optimization is copied from ascii_decode in unicodeobject.c
/* Mask to quickly check whether a C 'long' contains a
non-ASCII, UTF8-encoded char. */
#if (SIZEOF_LONG == 8)
# define ASCII_CHAR_MASK 0x8080808080808080UL
#elif (SIZEOF_LONG == 4)
# define ASCII_CHAR_MASK 0x80808080UL
#else
# error C 'long' size should be either 4 or 8!
#endif
PyObject
*
_Py_bytes_isascii
(
const
char
*
cptr
,
Py_ssize_t
len
)
{
const
unsigned
char
*
p
=
(
unsigned
char
*
)
cptr
;
const
unsigned
char
*
e
=
p
+
len
;
for
(;
p
<
e
;
p
++
)
{
if
(
*
p
>=
128
)
{
const
char
*
p
=
cptr
;
const
char
*
end
=
p
+
len
;
const
char
*
aligned_end
=
(
const
char
*
)
_Py_ALIGN_DOWN
(
end
,
SIZEOF_LONG
);
while
(
p
<
end
)
{
/* Fast path, see in STRINGLIB(utf8_decode) in stringlib/codecs.h
for an explanation. */
if
(
_Py_IS_ALIGNED
(
p
,
SIZEOF_LONG
))
{
/* Help allocation */
const
char
*
_p
=
p
;
while
(
_p
<
aligned_end
)
{
unsigned
long
value
=
*
(
unsigned
long
*
)
_p
;
if
(
value
&
ASCII_CHAR_MASK
)
{
Py_RETURN_FALSE
;
}
_p
+=
SIZEOF_LONG
;
}
p
=
_p
;
if
(
_p
==
end
)
break
;
}
if
((
unsigned
char
)
*
p
&
0x80
)
{
Py_RETURN_FALSE
;
}
p
++
;
}
Py_RETURN_TRUE
;
}
#undef ASCII_CHAR_MASK
PyDoc_STRVAR_shared
(
_Py_isdigit__doc__
,
"B.isdigit() -> bool
\n
\
...
...
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