Add str_startswith and str_endswith

(cherry picked from commit 746d3d6b1a)
This commit is contained in:
heinrich5991 2018-07-25 10:23:07 +02:00
parent d215c73206
commit e162ee085f
2 changed files with 58 additions and 0 deletions

View file

@ -1951,6 +1951,30 @@ int str_comp_filenames(const char *a, const char *b)
return tolower(*a) - tolower(*b);
}
const char *str_startswith(const char *str, const char *prefix)
{
int prefixl = str_length(prefix);
if(str_comp_num(str, prefix, prefixl) == 0)
{
return str + prefixl;
}
else
{
return 0;
}
}
int str_endswith(const char *str, const char *suffix)
{
int strl = str_length(str);
int suffixl = str_length(suffix);
if(strl < suffixl)
{
return 0;
}
return str_comp(str + strl - suffixl, suffix) == 0;
}
const char *str_find_nocase(const char *haystack, const char *needle)
{
while(*haystack) /* native implementation */

View file

@ -1013,6 +1013,40 @@ int str_comp_num(const char *a, const char *b, const int num);
*/
int str_comp_filenames(const char *a, const char *b);
/*
Function: str_startswith
Checks whether the string begins with a certain prefix.
Parameter:
str - String to check.
prefix - Prefix to look for.
Returns:
A pointer to the string str after the string prefix, or 0 if
the string prefix isn't a prefix of the string str.
Remarks:
- The strings are treated as zero-terminated strings.
*/
const char *str_startswith(const char *str, const char *prefix);
/*
Function: str_endswith
Checks whether the string ends with a certain suffix.
Parameter:
str - String to check.
suffix - Suffix to look for.
Returns:
0 - String suffix is not a suffix of string str
1 - String suffix is a suffix of string str
Remarks:
- The strings are treated as zero-terminated strings.
*/
int str_endswith(const char *str, const char *suffix);
/*
Function: str_find_nocase
Finds a string inside another string case insensitive.