C를 사용하면 null이 포함될 수있는 버퍼 내부의 하위 문자열을 찾아야합니다.null이 포함 된 버퍼에서 하위 문자열을 검색하려면 어떻게해야합니까?
haystack = "Some text\0\0\0\0 that has embedded nulls".
needle = "has embedded"r
내가 문자열, 또는 null의 시작을 반환해야, similat()는 않는 strstr합니다 :
request_segment_end = mystrstr(request_segment_start, boundary);
당신이 알고있는 기존의 구현이 있습니까?
나는, 그대로 여기에 검증되지 않은 복사 한 구글의 codesearch에 memove의 구현을 발견
업데이트,
/*
* memmem.c
*
* Find a byte string inside a longer byte string
*
* This uses the "Not So Naive" algorithm, a very simple but
* usually effective algorithm, see:
*
* http://www-igm.univ-mlv.fr/~lecroq/string/
*/
#include <string.h>
void *memmem(const void *haystack, size_t n, const void *needle, size_t m)
{
const unsigned char *y = (const unsigned char *)haystack;
const unsigned char *x = (const unsigned char *)needle;
size_t j, k, l;
if (m > n || !m || !n)
return NULL;
if (1 != m) {
if (x[0] == x[1]) {
k = 2;
l = 1;
} else {
k = 1;
l = 2;
}
j = 0;
while (j <= n - m) {
if (x[1] != y[j + 1]) {
j += k;
} else {
if (!memcmp(x + 2, y + j + 2, m - 2)
&& x[0] == y[j])
return (void *)&y[j];
j += l;
}
}
} else
do {
if (*y == *x)
return (void *)y;
y++;
} while (--n);
return NULL;
}
이 경우 "경계"란 무엇입니까? – templatetypedef
[ESMAJ] (http://www-igm.univ-mlv.fr/~lecroq/string/index.html)에 설명 된 방법 중 하나를 구현할 수 있습니다. – pmg