mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-18 10:46:23 +00:00
Change str_str() to return (char*) not (const char*)
Also rewrite implementation to handle edge cases, rename arguments to be more self documenting.
This commit is contained in:
parent
46f81d9d2e
commit
245c7e7f49
13
str.c
13
str.c
@ -46,10 +46,15 @@ int strcase_startswith(char *str, const char *substring, char **afterp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Like strstr() but doesn't depend on null termination */
|
/* Like strstr() but doesn't depend on null termination */
|
||||||
const char *str_str(const char *s1,const char *s2,int s1len)
|
char *str_str(char *haystack, const char *needle, int haystack_len)
|
||||||
{
|
{
|
||||||
int s2len=strlen(s2);
|
size_t needle_len = strlen(needle);
|
||||||
for(;*s1&&(s1len--);s1++)
|
if (needle_len == 0)
|
||||||
if (!strncmp(s1,s2,s2len)) return s1;
|
return haystack;
|
||||||
|
if (haystack_len >= needle_len) {
|
||||||
|
for (; *haystack && haystack_len >= needle_len; ++haystack, --haystack_len)
|
||||||
|
if (strncmp(haystack, needle, needle_len) == 0)
|
||||||
|
return haystack;
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
6
str.h
6
str.h
@ -38,7 +38,9 @@ int str_startswith(char *str, const char *substring, char **afterp);
|
|||||||
int strcase_startswith(char *str, const char *substring, char **afterp);
|
int strcase_startswith(char *str, const char *substring, char **afterp);
|
||||||
|
|
||||||
/* like strstr(), but doesn't depend on null termination.
|
/* like strstr(), but doesn't depend on null termination.
|
||||||
@author Paul Gardner-Stephen <paul@servalproject.org> */
|
@author Paul Gardner-Stephen <paul@servalproject.org>
|
||||||
const char *str_str(const char *s1,const char *s2,int s1len);
|
@author Andrew Bettison <andrew@servalproject.com>
|
||||||
|
*/
|
||||||
|
char *str_str(char *haystack, const char *needle, int haystack_len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user