1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Added strstr(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2012-03-04 17:20:42 +01:00
parent 6c6e1091a1
commit 5d826ec284
2 changed files with 19 additions and 1 deletions

View file

@ -52,6 +52,7 @@ int strncmp(const char*, const char*, size_t);
char* strncpy(char* restrict, const char* restrict, size_t);
char* strrchr(const char*, int);
size_t strspn(const char*, const char*);
char* strstr(const char*, const char*);
char* strtok(char* restrict, const char* restrict);
char* strtok_r(char* restrict, const char* restrict, char** restrict);
@ -67,7 +68,6 @@ char* strndup(const char*, size_t);
size_t strnlen(const char*, size_t);
char* strpbrk(const char*, const char*);
char* strsignal(int);
char* strstr(const char*, const char*);
size_t strxfrm(char* restrict, const char* restrict, size_t);
size_t strxfrm_l(char* restrict, const char* restrict, size_t, locale_t);
#endif

View file

@ -244,6 +244,24 @@ namespace Maxsi
return result;
}
// TODO: This simple and hacky implementation runs in O(N^2) even though
// this problem can be solved in O(N).
char* strstr(const char* haystack, const char* needle)
{
if ( !needle[0] ) { return (char*) haystack; }
for ( size_t i = 0; haystack[i]; i++ )
{
bool diff = false;
for ( size_t j = 0; needle[j]; j++ )
{
if ( haystack[i+j] != needle[j] ) { diff = true; break; }
}
if ( diff ) { continue; }
return (char*) haystack + i;
}
return NULL;
}
#ifndef SORTIX_KERNEL
extern "C" char* strdup(const char* input)
{