Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ff2e35

Browse files
committed
simplify by shortcutting when the kind of the needle is larger than the haystack
1 parent c049952 commit 1ff2e35

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

Objects/unicodeobject.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9075,15 +9075,14 @@ PyUnicode_Count(PyObject *str,
90759075

90769076
kind1 = PyUnicode_KIND(str_obj);
90779077
kind2 = PyUnicode_KIND(sub_obj);
9078-
kind = kind1 > kind2 ? kind1 : kind2;
9078+
kind = kind2;
90799079
buf1 = PyUnicode_DATA(str_obj);
9080-
if (kind1 != kind)
9081-
buf1 = _PyUnicode_AsKind(str_obj, kind);
9082-
if (!buf1)
9083-
goto onError;
90849080
buf2 = PyUnicode_DATA(sub_obj);
9085-
if (kind2 != kind)
9081+
if (kind2 != kind) {
9082+
if (kind2 > kind)
9083+
return 0;
90869084
buf2 = _PyUnicode_AsKind(sub_obj, kind);
9085+
}
90879086
if (!buf2)
90889087
goto onError;
90899088
len1 = PyUnicode_GET_LENGTH(str_obj);
@@ -9122,17 +9121,13 @@ PyUnicode_Count(PyObject *str,
91229121
Py_DECREF(sub_obj);
91239122
Py_DECREF(str_obj);
91249123

9125-
if (kind1 != kind)
9126-
PyMem_Free(buf1);
91279124
if (kind2 != kind)
91289125
PyMem_Free(buf2);
91299126

91309127
return result;
91319128
onError:
91329129
Py_DECREF(sub_obj);
91339130
Py_DECREF(str_obj);
9134-
if (kind1 != kind && buf1)
9135-
PyMem_Free(buf1);
91369131
if (kind2 != kind && buf2)
91379132
PyMem_Free(buf2);
91389133
return -1;
@@ -10660,20 +10655,17 @@ PyUnicode_Contains(PyObject *container, PyObject *element)
1066010655

1066110656
kind1 = PyUnicode_KIND(str);
1066210657
kind2 = PyUnicode_KIND(sub);
10663-
kind = kind1 > kind2 ? kind1 : kind2;
10658+
kind = kind1;
1066410659
buf1 = PyUnicode_DATA(str);
1066510660
buf2 = PyUnicode_DATA(sub);
10666-
if (kind1 != kind)
10667-
buf1 = _PyUnicode_AsKind(str, kind);
10668-
if (!buf1) {
10669-
Py_DECREF(sub);
10670-
return -1;
10671-
}
10672-
if (kind2 != kind)
10661+
if (kind2 != kind) {
10662+
if (kind2 > kind)
10663+
return 0;
1067310664
buf2 = _PyUnicode_AsKind(sub, kind);
10665+
}
1067410666
if (!buf2) {
1067510667
Py_DECREF(sub);
10676-
if (kind1 != kind) PyMem_Free(buf1);
10668+
Py_DECREF(str);
1067710669
return -1;
1067810670
}
1067910671
len1 = PyUnicode_GET_LENGTH(str);
@@ -10697,8 +10689,6 @@ PyUnicode_Contains(PyObject *container, PyObject *element)
1069710689
Py_DECREF(str);
1069810690
Py_DECREF(sub);
1069910691

10700-
if (kind1 != kind)
10701-
PyMem_Free(buf1);
1070210692
if (kind2 != kind)
1070310693
PyMem_Free(buf2);
1070410694

0 commit comments

Comments
 (0)