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
ce14184b
Commit
ce14184b
authored
Jan 10, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise str == unicode / unicode == str in Py2
parent
9ef2b067
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
12 deletions
+49
-12
Cython/Utility/StringTools.c
Cython/Utility/StringTools.c
+49
-12
No files found.
Cython/Utility/StringTools.c
View file @
ce14184b
...
...
@@ -138,10 +138,32 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int
#if CYTHON_COMPILING_IN_PYPY
return
PyObject_RichCompareBool
(
s1
,
s2
,
equals
);
#else
#if PY_MAJOR_VERSION < 3
PyObject
*
owned_ref
=
NULL
;
#endif
int
s1_is_unicode
,
s2_is_unicode
;
if
(
s1
==
s2
)
{
/* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */
return
(
equals
==
Py_EQ
);
}
else
if
(
PyUnicode_CheckExact
(
s1
)
&
PyUnicode_CheckExact
(
s2
))
{
goto
return_eq
;
}
s1_is_unicode
=
PyUnicode_CheckExact
(
s1
);
s2_is_unicode
=
PyUnicode_CheckExact
(
s2
);
#if PY_MAJOR_VERSION < 3
if
((
s1_is_unicode
&
(
!
s2_is_unicode
))
&&
PyString_CheckExact
(
s2
))
{
owned_ref
=
PyUnicode_FromObject
(
s2
);
if
(
unlikely
(
!
owned_ref
))
return
-
1
;
s2
=
owned_ref
;
s2_is_unicode
=
1
;
}
else
if
((
s2_is_unicode
&
(
!
s1_is_unicode
))
&&
PyString_CheckExact
(
s1
))
{
owned_ref
=
PyUnicode_FromObject
(
s1
);
if
(
unlikely
(
!
owned_ref
))
return
-
1
;
s1
=
owned_ref
;
s1_is_unicode
=
1
;
}
#endif
if
(
s1_is_unicode
&
s2_is_unicode
)
{
Py_ssize_t
length
;
int
kind
;
void
*
data1
,
*
data2
;
...
...
@@ -150,26 +172,31 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int
return
-
1
;
#endif
length
=
__Pyx_PyUnicode_GET_LENGTH
(
s1
);
if
(
length
!=
__Pyx_PyUnicode_GET_LENGTH
(
s2
))
return
(
equals
==
Py_NE
);
if
(
length
!=
__Pyx_PyUnicode_GET_LENGTH
(
s2
))
{
goto
return_ne
;
}
// len(s1) == len(s2) >= 1 (empty string is interned, and "s1 is not s2")
kind
=
__Pyx_PyUnicode_KIND
(
s1
);
if
(
kind
!=
__Pyx_PyUnicode_KIND
(
s2
))
return
(
equals
==
Py_NE
);
if
(
kind
!=
__Pyx_PyUnicode_KIND
(
s2
))
{
goto
return_ne
;
}
data1
=
__Pyx_PyUnicode_DATA
(
s1
);
data2
=
__Pyx_PyUnicode_DATA
(
s2
);
if
(
__Pyx_PyUnicode_READ
(
kind
,
data1
,
0
)
!=
__Pyx_PyUnicode_READ
(
kind
,
data2
,
0
))
{
return
(
equals
==
Py_NE
)
;
goto
return_ne
;
}
else
if
(
length
==
1
)
{
return
(
equals
==
Py_EQ
)
;
goto
return_eq
;
}
else
{
int
result
=
memcmp
(
data1
,
data2
,
length
*
kind
);
#if PY_MAJOR_VERSION < 3
Py_XDECREF
(
owned_ref
);
#endif
return
(
equals
==
Py_EQ
)
?
(
result
==
0
)
:
(
result
!=
0
);
}
}
else
if
((
s1
==
Py_None
)
&
PyUnicode_CheckExact
(
s2
)
)
{
return
(
equals
==
Py_NE
)
;
}
else
if
((
s2
==
Py_None
)
&
PyUnicode_CheckExact
(
s1
)
)
{
return
(
equals
==
Py_NE
)
;
}
else
if
((
s1
==
Py_None
)
&
s2_is_unicode
)
{
goto
return_ne
;
}
else
if
((
s2
==
Py_None
)
&
s1_is_unicode
)
{
goto
return_ne
;
}
else
{
int
result
;
PyObject
*
py_result
=
PyObject_RichCompare
(
s1
,
s2
,
equals
);
...
...
@@ -179,6 +206,16 @@ static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int
Py_DECREF
(
py_result
);
return
result
;
}
return_eq:
#if PY_MAJOR_VERSION < 3
Py_XDECREF
(
owned_ref
);
#endif
return
(
equals
==
Py_EQ
);
return_ne:
#if PY_MAJOR_VERSION < 3
Py_XDECREF
(
owned_ref
);
#endif
return
(
equals
==
Py_NE
);
#endif
}
...
...
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