大家用了memchr、strchr、strstr之後,有沒有想要一個叫memstr的函數?從內存裡面找特定的字符串?
glibc裡面沒有提供memstr,我這裡提供一個吧。驗證過的可靠版本:
[cpp]
//find 'substr' from a fixed-length buffer
//('full_data' will be treated as binary data buffer)
//return NULL if not found
char* memstr(char* full_data, int full_data_len, char* substr)
{
if (full_data == NULL || full_data_len <= 0 || substr == NULL) {
return NULL;
}
if (*substr == '\0') {
return NULL;
}
int sublen = strlen(substr);
int i;
char* cur = full_data;
int last_possible = full_data_len - sublen + 1;
for (i = 0; i < last_possible; i++) {
if (*cur == *substr) {
//assert(full_data_len - i >= sublen);
if (memcmp(cur, substr, sublen) == 0) {
//found
return cur;
}
}
cur++;
}
return NULL;
}
//find 'substr' from a fixed-length buffer
//('full_data' will be treated as binary data buffer)
//return NULL if not found
char* memstr(char* full_data, int full_data_len, char* substr)
{
if (full_data == NULL || full_data_len <= 0 || substr == NULL) {
return NULL;
}
if (*substr == '\0') {
return NULL;
}
int sublen = strlen(substr);
int i;
char* cur = full_data;
int last_possible = full_data_len - sublen + 1;
for (i = 0; i < last_possible; i++) {
if (*cur == *substr) {
//assert(full_data_len - i >= sublen);
if (memcmp(cur, substr, sublen) == 0) {
//found
return cur;
}
}
cur++;
}
return NULL;
}
這個函數調用了memcmp,應該可以利用memcmp的加速能力。
ps:研究過memcmp的同學應該都知道,memcmp會針對硬件做優化,比如一次比較多個字節什麼的。