mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-19 14:38:18 +00:00
Add str_startswith
and str_endswith
Also add a couple of tests for both.
This commit is contained in:
parent
57d3a61c3f
commit
746d3d6b1a
|
@ -2392,6 +2392,30 @@ int str_comp_filenames(const char *a, const char *b)
|
|||
return *a - *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;
|
||||
}
|
||||
|
||||
static int min3(int a, int b, int c)
|
||||
{
|
||||
int min = a;
|
||||
|
|
|
@ -1187,6 +1187,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_utf8_dist
|
||||
Computes the edit distance between two strings.
|
||||
|
|
|
@ -54,3 +54,37 @@ TEST(Str, Utf8CompConfusables)
|
|||
EXPECT_FALSE(str_utf8_comp_confusable("o", "x") == 0);
|
||||
EXPECT_TRUE(str_utf8_comp_confusable("aceiou", "ąçęįǫų") == 0);
|
||||
}
|
||||
|
||||
TEST(Str, Startswith)
|
||||
{
|
||||
EXPECT_TRUE(str_startswith("abcdef", "abc"));
|
||||
EXPECT_FALSE(str_startswith("abc", "abcdef"));
|
||||
|
||||
EXPECT_TRUE(str_startswith("xyz", ""));
|
||||
EXPECT_FALSE(str_startswith("", "xyz"));
|
||||
|
||||
EXPECT_FALSE(str_startswith("house", "home"));
|
||||
EXPECT_FALSE(str_startswith("blackboard", "board"));
|
||||
|
||||
EXPECT_TRUE(str_startswith("поплавать", "по"));
|
||||
EXPECT_FALSE(str_startswith("плавать", "по"));
|
||||
|
||||
static const char ABCDEF[] = "abcdef";
|
||||
static const char ABC[] = "abc";
|
||||
EXPECT_EQ(str_startswith(ABCDEF, ABC) - ABCDEF, str_length(ABC));
|
||||
}
|
||||
|
||||
TEST(Str, Endswith)
|
||||
{
|
||||
EXPECT_TRUE(str_endswith("abcdef", "def"));
|
||||
EXPECT_FALSE(str_endswith("def", "abcdef"));
|
||||
|
||||
EXPECT_TRUE(str_endswith("xyz", ""));
|
||||
EXPECT_FALSE(str_endswith("", "xyz"));
|
||||
|
||||
EXPECT_FALSE(str_endswith("rhyme", "mine"));
|
||||
EXPECT_FALSE(str_endswith("blackboard", "black"));
|
||||
|
||||
EXPECT_TRUE(str_endswith("люди", "юди"));
|
||||
EXPECT_FALSE(str_endswith("люди", "любовь"));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue