diff --git a/src/base/system.c b/src/base/system.c index d95802a87..9a5878eb3 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -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 */ diff --git a/src/base/system.h b/src/base/system.h index 98bcd8e1a..ed27da081 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -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.