libc: Add strstr()
This commit is contained in:
parent
3fde7e46f5
commit
a002e75725
@ -56,6 +56,9 @@ extern "C"
|
|||||||
/* Compares at most max bytes of the strings a and b. */
|
/* Compares at most max bytes of the strings a and b. */
|
||||||
int strncmp(const char* a, const char* b, size_t max);
|
int strncmp(const char* a, const char* b, size_t max);
|
||||||
|
|
||||||
|
/* Searches for the needle string in the haystack string. */
|
||||||
|
char* strstr(const char* haystack, const char* needle);
|
||||||
|
|
||||||
/* Returns the error string associated with the error number err. */
|
/* Returns the error string associated with the error number err. */
|
||||||
char* strerror(int err);
|
char* strerror(int err);
|
||||||
|
|
||||||
|
@ -40,9 +40,9 @@ extern "C"
|
|||||||
{
|
{
|
||||||
if (dest == src) return dest;
|
if (dest == src) return dest;
|
||||||
if (dest > src)
|
if (dest > src)
|
||||||
for (long i = n - 1; i >= 0; i++) { *((char*)dest + i) = *((char*)src + i); }
|
for (long i = n - 1; i >= 0; i++) { *((char*)dest + i) = *((const char*)src + i); }
|
||||||
else
|
else
|
||||||
for (long i = 0; i < (long)n; i++) { *((char*)dest + i) = *((char*)src + i); }
|
for (long i = 0; i < (long)n; i++) { *((char*)dest + i) = *((const char*)src + i); }
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,6 +138,26 @@ extern "C"
|
|||||||
return memset(buf, 0, n);
|
return memset(buf, 0, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* strstr(const char* haystack, const char* needle)
|
||||||
|
{
|
||||||
|
size_t needle_size = strlen(needle);
|
||||||
|
size_t haystack_size = strlen(haystack);
|
||||||
|
while (*haystack)
|
||||||
|
{
|
||||||
|
if (*haystack == *needle)
|
||||||
|
{
|
||||||
|
if (needle_size <= haystack_size)
|
||||||
|
{
|
||||||
|
if (!strncmp(haystack, needle, needle_size)) return const_cast<char*>(haystack);
|
||||||
|
}
|
||||||
|
else { return NULL; }
|
||||||
|
}
|
||||||
|
haystack++;
|
||||||
|
haystack_size--;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
#pragma GCC push_options
|
#pragma GCC push_options
|
||||||
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user