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

Skip to content

Commit 396f6e0

Browse files
committed
Fredrik Lundh <[email protected]>:
Simplify find code; this is a performance improvement on at least some platforms.
1 parent 440d898 commit 396f6e0

1 file changed

Lines changed: 4 additions & 8 deletions

File tree

Objects/stringobject.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ string_split(self, args)
651651

652652
i = j = 0;
653653
while (i+n <= len) {
654-
if (s[i] == sub[0] && (n == 1 || memcmp(s+i, sub, n) == 0)) {
654+
if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) {
655655
if (maxsplit-- <= 0)
656656
break;
657657
item = PyString_FromStringAndSize(s+j, (int)(i-j));
@@ -852,8 +852,7 @@ string_find_internal(self, args, dir)
852852
return (long)i;
853853
last -= n;
854854
for (; i <= last; ++i)
855-
if (s[i] == sub[0] &&
856-
(n == 1 || memcmp(&s[i+1], &sub[1], n-1) == 0))
855+
if (s[i] == sub[0] && memcmp(&s[i], sub, n) == 0)
857856
return (long)i;
858857
}
859858
else {
@@ -862,8 +861,7 @@ string_find_internal(self, args, dir)
862861
if (n == 0 && i <= last)
863862
return (long)last;
864863
for (j = last-n; j >= i; --j)
865-
if (s[j] == sub[0] &&
866-
(n == 1 || memcmp(&s[j+1], &sub[1], n-1) == 0))
864+
if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0)
867865
return (long)j;
868866
}
869867

@@ -1415,9 +1413,7 @@ mymemfind(mem, len, pat, pat_len)
14151413
len -= pat_len;
14161414

14171415
for (ii = 0; ii <= len; ii++) {
1418-
if (mem[ii] == pat[0] &&
1419-
(pat_len == 1 ||
1420-
memcmp(&mem[ii+1], &pat[1], pat_len-1) == 0)) {
1416+
if (mem[ii] == pat[0] && memcmp(&mem[ii], pat, pat_len) == 0) {
14211417
return ii;
14221418
}
14231419
}

0 commit comments

Comments
 (0)