优化
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#include <str_util.h>
|
||||
|
||||
|
||||
const char* str_util_strnstr(const char* haystack, const char* needle, os_size_t needle_len) {
|
||||
if (needle_len == 0) return (char*)haystack;
|
||||
if (!haystack || !needle || haystack[0]=='\0') return NULL;
|
||||
|
||||
os_size_t haystack_len = strlen(haystack);
|
||||
if (haystack_len < needle_len) return NULL;
|
||||
|
||||
for (os_size_t i = 0; i <= haystack_len - needle_len; i++) {
|
||||
if (memcmp(haystack + i, needle, needle_len) == 0) {
|
||||
return haystack + i;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* str_util_strstr(const char* haystack, const char* needle) {
|
||||
if (!haystack || !needle || haystack[0]=='\0' || needle[0]=='\0') return NULL;
|
||||
|
||||
os_size_t needle_len = strlen(needle);
|
||||
os_size_t haystack_len = strlen(haystack);
|
||||
if (haystack_len < needle_len) return NULL;
|
||||
|
||||
for (os_size_t i = 0; i <= haystack_len - needle_len; i++) {
|
||||
if (memcmp(haystack + i, needle, needle_len) == 0) {
|
||||
return haystack + i;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user