Return a pointer from str_endswith as well

This commit is contained in:
heinrich5991 2018-07-26 14:04:44 +02:00
parent 5c4e819e44
commit 9fcf5480f8
3 changed files with 21 additions and 7 deletions

View file

@ -2405,15 +2405,24 @@ const char *str_startswith(const char *str, const char *prefix)
} }
} }
int str_endswith(const char *str, const char *suffix) const char *str_endswith(const char *str, const char *suffix)
{ {
int strl = str_length(str); int strl = str_length(str);
int suffixl = str_length(suffix); int suffixl = str_length(suffix);
const char *strsuffix;
if(strl < suffixl) if(strl < suffixl)
{ {
return 0; return 0;
} }
return str_comp(str + strl - suffixl, suffix) == 0; strsuffix = str + strl - suffixl;
if(str_comp(strsuffix, suffix) == 0)
{
return strsuffix;
}
else
{
return 0;
}
} }
static int min3(int a, int b, int c) static int min3(int a, int b, int c)

View file

@ -1213,13 +1213,13 @@ const char *str_startswith(const char *str, const char *prefix);
suffix - Suffix to look for. suffix - Suffix to look for.
Returns: Returns:
0 - String suffix is not a suffix of string str A pointer to the beginning of the suffix in the string str, or
1 - String suffix is a suffix of string str 0 if the string suffix isn't a suffix of the string str.
Remarks: Remarks:
- The strings are treated as zero-terminated strings. - The strings are treated as zero-terminated strings.
*/ */
int str_endswith(const char *str, const char *suffix); const char *str_endswith(const char *str, const char *suffix);
/* /*
Function: str_utf8_dist Function: str_utf8_dist

View file

@ -69,9 +69,9 @@ TEST(Str, Startswith)
EXPECT_TRUE(str_startswith("поплавать", "по")); EXPECT_TRUE(str_startswith("поплавать", "по"));
EXPECT_FALSE(str_startswith("плавать", "по")); EXPECT_FALSE(str_startswith("плавать", "по"));
static const char ABCDEF[] = "abcdef"; static const char ABCDEFG[] = "abcdefg";
static const char ABC[] = "abc"; static const char ABC[] = "abc";
EXPECT_EQ(str_startswith(ABCDEF, ABC) - ABCDEF, str_length(ABC)); EXPECT_EQ(str_startswith(ABCDEFG, ABC) - ABCDEFG, str_length(ABC));
} }
TEST(Str, Endswith) TEST(Str, Endswith)
@ -87,4 +87,9 @@ TEST(Str, Endswith)
EXPECT_TRUE(str_endswith("люди", "юди")); EXPECT_TRUE(str_endswith("люди", "юди"));
EXPECT_FALSE(str_endswith("люди", "любовь")); EXPECT_FALSE(str_endswith("люди", "любовь"));
static const char ABCDEFG[] = "abcdefg";
static const char DEFG[] = "defg";
EXPECT_EQ(str_endswith(ABCDEFG, DEFG) - ABCDEFG,
str_length(ABCDEFG) - str_length(DEFG));
} }