utils: Add case-insensitive version of strpfx()

This commit is contained in:
Tobias Brunner 2013-08-06 17:27:15 +02:00 committed by Andreas Steffen
parent 49032d15be
commit 32a145fdbd
2 changed files with 44 additions and 0 deletions

View File

@ -187,6 +187,38 @@ START_TEST(test_round)
}
END_TEST
/*******************************************************************************
* strpfx
*/
static struct {
char *str;
char *pfx;
bool prefix;
bool case_prefix;
} strpfx_data[] = {
{"", "", TRUE, TRUE},
{"abc", "", TRUE, TRUE},
{"abc", "a", TRUE, TRUE},
{"abc", "ab", TRUE, TRUE},
{"abc", "abc", TRUE, TRUE},
{"abc", "abcd", FALSE, FALSE},
{"abc", "AB", FALSE, TRUE},
{"ABC", "ab", FALSE, TRUE},
{" abc", "abc", FALSE, FALSE},
};
START_TEST(test_strpfx)
{
bool prefix;
prefix = strpfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
ck_assert(prefix == strpfx_data[_i].prefix);
prefix = strcasepfx(strpfx_data[_i].str, strpfx_data[_i].pfx);
ck_assert(prefix == strpfx_data[_i].case_prefix);
}
END_TEST
/*******************************************************************************
* memxor
*/
@ -442,6 +474,10 @@ Suite *utils_suite_create()
tcase_add_test(tc, test_round);
suite_add_tcase(s, tc);
tc = tcase_create("string helper");
tcase_add_loop_test(tc, test_strpfx, 0, countof(strpfx_data));
suite_add_tcase(s, tc);
tc = tcase_create("memxor");
tcase_add_test(tc, test_memxor);
tcase_add_test(tc, test_memxor_aligned);

View File

@ -112,6 +112,14 @@ static inline bool strncaseeq(const char *x, const char *y, size_t len)
return strncasecmp(x, y, len) == 0;
}
/**
* Helper function that checks if a string starts with a given prefix
*/
static inline bool strcasepfx(const char *x, const char *prefix)
{
return strncaseeq(x, prefix, strlen(prefix));
}
/**
* NULL-safe strdup variant
*/