Skip to content

Commit 9c27ae4

Browse files
committed
Revert "Optimize str_strstr()"
Partial revert of commit 418922f Reason: premature optimization :) new implementation was actually slower
1 parent 486e961 commit 9c27ae4

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

ut.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,8 @@ static inline int str_casematch_nt(const str *a, const char *b)
11781178
*/
11791179
static inline char* str_strstr(const str *stra, const str *strb)
11801180
{
1181-
char *a, *ai, *b, *bi, *end1, *end2;
1181+
int i;
1182+
int len;
11821183

11831184
if (stra==NULL || strb==NULL || stra->s==NULL || strb->s==NULL
11841185
|| stra->len<=0 || strb->len<=0) {
@@ -1191,20 +1192,26 @@ static inline char* str_strstr(const str *stra, const str *strb)
11911192
if (strb->len > stra->len)
11921193
return NULL;
11931194

1194-
a = stra->s;
1195-
b = strb->s;
1196-
end1 = a + stra->len - strb->len + 1;
1197-
end2 = b + strb->len;
1195+
len=0;
1196+
while (stra->len-len >= strb->len){
1197+
if (stra->s[len] != strb->s[0]) {
1198+
len++;
1199+
continue;
1200+
}
1201+
1202+
for (i=1; i<strb->len; i++)
1203+
if (stra->s[len+i]!=strb->s[i]) {
1204+
len++;
1205+
break;
1206+
}
11981207

1199-
for (; a < end1; a++) {
1200-
ai = a;
1201-
bi = b;
1202-
while ((*ai++ == *bi) && ++bi < end2) ;
1208+
if (i != strb->len)
1209+
continue;
12031210

1204-
if (bi == end2)
1205-
return a;
1211+
return stra->s+len;
12061212
}
12071213

1214+
12081215
return NULL;
12091216
}
12101217

0 commit comments

Comments
 (0)