strstr()表示非null终止的字符串

Meh*_*dad 27 c string indexof null-terminated

如何在C 中对计数字符串(即空终止)进行就地等效?strstr()

Tim*_*per 8

看看下面的功能是否适合您.我没有彻底测试过,所以我建议你这样做.

char *sstrstr(char *haystack, char *needle, size_t length)
{
    size_t needle_length = strlen(needle);
    size_t i;
    for (i = 0; i < length; i++) {
        if (i + needle_length > length) {
            return NULL;
        }
        if (strncmp(&haystack[i], needle, needle_length) == 0) {
            return &haystack[i];
        }
    }
    return NULL;
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*her 7

如果你害怕O(m*n)行为 - 基本上,你不需要,这种情况不会自然发生 - 这里是我所说的KMP实现,我已经修改了它以占用大海捞针的长度.也是一个包装.如果要进行重复搜索,请编写自己的搜索并重用该borders数组.

不保证无错误,但它似乎仍然有效.

int *kmp_borders(char *needle, size_t nlen){
    if (!needle) return NULL;
    int i, j, *borders = malloc((nlen+1)*sizeof(*borders));
    if (!borders) return NULL;
    i = 0;
    j = -1;
    borders[i] = j;
    while((size_t)i < nlen){
        while(j >= 0 && needle[i] != needle[j]){
            j = borders[j];
        }
        ++i;
        ++j;
        borders[i] = j;
    }
    return borders;
}

char *kmp_search(char *haystack, size_t haylen, char *needle, size_t nlen, int *borders){
    size_t max_index = haylen-nlen, i = 0, j = 0;
    while(i <= max_index){
        while(j < nlen && *haystack && needle[j] == *haystack){
            ++j;
            ++haystack;
        }
        if (j == nlen){
            return haystack-nlen;
        }
        if (!(*haystack)){
            return NULL;
        }
        if (j == 0){
            ++haystack;
            ++i;
        } else {
            do{
                i += j - (size_t)borders[j];
                j = borders[j];
            }while(j > 0 && needle[j] != *haystack);
        }
    }
    return NULL;
}

char *sstrnstr(char *haystack, char *needle, size_t haylen){
    if (!haystack || !needle){
        return NULL;
    }
    size_t nlen = strlen(needle);
    if (haylen < nlen){
        return NULL;
    }
    int *borders = kmp_borders(needle, nlen);
    if (!borders){
        return NULL;
    }
    char *match = kmp_search(haystack, haylen, needle, nlen, borders);
    free(borders);
    return match;
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 5

我刚刚遇到这个,我想分享我的实现。它认为它相当快,我没有任何子调用。

它返回在大海捞针中找到针的索引,如果没有找到则返回 -1。

/* binary search in memory */
int memsearch(const char *hay, int haysize, const char *needle, int needlesize) {
    int haypos, needlepos;
    haysize -= needlesize;
    for (haypos = 0; haypos <= haysize; haypos++) {
        for (needlepos = 0; needlepos < needlesize; needlepos++) {
            if (hay[haypos + needlepos] != needle[needlepos]) {
                // Next character in haystack.
                break;
            }
        }
        if (needlepos == needlesize) {
            return haypos;
        }
    }
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

  • 当你在做的时候,应该继续做博耶-摩尔;) (2认同)