34 lines
1014 B
C
34 lines
1014 B
C
#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;
|
||
|
|
}
|
||
|
|
|