Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of :func:`str.startswith` and :func:`str.endswith`.
26 changes: 13 additions & 13 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9272,24 +9272,24 @@ tailmatch(PyObject *self,
else
offset = start;

if (PyUnicode_READ(kind_self, data_self, offset) ==
PyUnicode_READ(kind_sub, data_sub, 0) &&
PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
PyUnicode_READ(kind_sub, data_sub, end_sub)) {
int match_last = PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
PyUnicode_READ(kind_sub, data_sub, end_sub);

if (match_last) {
if (end_sub == 0) {
return 1;
}
/* If both are of the same kind, memcmp is sufficient */
if (kind_self == kind_sub) {
return ! memcmp((char *)data_self +
(offset * PyUnicode_KIND(substring)),
data_sub,
PyUnicode_GET_LENGTH(substring) *
PyUnicode_KIND(substring));
return ! memcmp((char *)data_self + (offset * kind_sub),
data_sub, end_sub * kind_sub);
}
/* otherwise we have to compare each character by first accessing it */
else {
/* We do not need to compare 0 and len(substring)-1 because
the if statement above ensured already that they are equal
when we end up here. */
for (i = 1; i < end_sub; ++i) {
/* We do not need to compare len(substring)-1 because the if
statement above ensured already that they are equal when we
end up here. */
for (i = 0; i < end_sub; ++i) {
if (PyUnicode_READ(kind_self, data_self, offset + i) !=
PyUnicode_READ(kind_sub, data_sub, i))
return 0;
Expand Down