char *qsearch(const char *text, int n, const char *patt, int m)
{
// get the length of the text and the pattern, if necessary
if (n < 0)//这里什么意思
n = strlen(text);
if (m < 0)//这里什么意思
m = strlen(patt);
if (m == 0)//这里什么意思
return (char*)text;
// construct delta shift table
int td[128];//这里什么意思
for (int c = 0; c < 128; c++)
td[c] = m + 1;
const char* p;
for (p=patt; *p; p++)
td[*p] = m - (p - patt);
// start searching...
const char *t, *tx = text;
// the main searching loop
while (tx + m <= text + n) {
for (p = patt, t = tx; *p; ++p, ++t) {
if (*p != *t) // found a mismatch
break;
}
if (*p == 0) // Yes! we found it!
return (char*)tx;
tx += td[tx[m]]; // move the pattern by a distance
}
return NULL;
}