memstr
1 //find 'substr' from a fixed-length buffer 2 //('full_data' will be treated as binary data buffer) 3 //return NULL if not found 4 char* memstr(char* full_data, int full_data_len, char* substr) 5 { 6 if (full_data == NULL || full_data_len <= 0 || substr == NULL) { 7 return NULL; 8 } 9 10 if (*substr == '\0') { 11 return NULL; 12 } 13 14 int sublen = strlen(substr); 15 16 int i; 17 char* cur = full_data; 18 int last_possible = full_data_len - sublen + 1; 19 for (i = 0; i < last_possible; i++) { 20 if (*cur == *substr) { 21 //assert(full_data_len - i >= sublen); 22 if (memcmp(cur, substr, sublen) == 0) { 23 //found 24 return cur; 25 } 26 } 27 cur++; 28 } 29 30 return NULL; 31 }
memfind
1 char* memstr(char* full_data, int full_data_len, char* substr, int substr_len) 2 { 3 4 }