utils: Add case-insensitive version of strpfx()
This commit is contained in:
committed by
Andreas Steffen
parent
49032d15be
commit
32a145fdbd
@@ -187,6 +187,38 @@ START_TEST(test_round)
|
|||||||
}
|
}
|
||||||
END_TEST
|
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
|
* memxor
|
||||||
*/
|
*/
|
||||||
@@ -442,6 +474,10 @@ Suite *utils_suite_create()
|
|||||||
tcase_add_test(tc, test_round);
|
tcase_add_test(tc, test_round);
|
||||||
suite_add_tcase(s, tc);
|
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");
|
tc = tcase_create("memxor");
|
||||||
tcase_add_test(tc, test_memxor);
|
tcase_add_test(tc, test_memxor);
|
||||||
tcase_add_test(tc, test_memxor_aligned);
|
tcase_add_test(tc, test_memxor_aligned);
|
||||||
|
|||||||
@@ -112,6 +112,14 @@ static inline bool strncaseeq(const char *x, const char *y, size_t len)
|
|||||||
return strncasecmp(x, y, len) == 0;
|
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
|
* NULL-safe strdup variant
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user