mirror of
https://gitlab.freedesktop.org/libinput/libinput.git
synced 2026-02-03 10:00:27 +01:00
modify macro streq/strneq for check one null pointer
Signed-off-by: weizhixiang <1138871845@qq.com> Minor modifications applied by Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
53595cb232
commit
2ea6589892
2 changed files with 41 additions and 2 deletions
|
|
@ -43,8 +43,23 @@
|
|||
#include <xlocale.h>
|
||||
#endif
|
||||
|
||||
#define streq(s1, s2) (strcmp((s1), (s2)) == 0)
|
||||
#define strneq(s1, s2, n) (strncmp((s1), (s2), (n)) == 0)
|
||||
static inline bool
|
||||
streq(const char *str1, const char *str2)
|
||||
{
|
||||
/* one NULL, one not NULL is always false */
|
||||
if (str1 && str2)
|
||||
return strcmp(str1, str2) == 0;
|
||||
return str1 == str2;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
strneq(const char *str1, const char *str2, int n)
|
||||
{
|
||||
/* one NULL, one not NULL is always false */
|
||||
if (str1 && str2)
|
||||
return strncmp(str1, str2, n) == 0;
|
||||
return str1 == str2;
|
||||
}
|
||||
|
||||
static inline void *
|
||||
zalloc(size_t size)
|
||||
|
|
|
|||
|
|
@ -1270,6 +1270,28 @@ START_TEST(strverscmp_test)
|
|||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(streq_test)
|
||||
{
|
||||
ck_assert(streq("", "") == true);
|
||||
ck_assert(streq(NULL, NULL) == true);
|
||||
ck_assert(streq("0.0.1", "") == false);
|
||||
ck_assert(streq("foo", NULL) == false);
|
||||
ck_assert(streq(NULL, "foo") == false);
|
||||
ck_assert(streq("0.0.1", "0.0.1") == true);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(strneq_test)
|
||||
{
|
||||
ck_assert(strneq("", "", 1) == true);
|
||||
ck_assert(strneq(NULL, NULL, 1) == true);
|
||||
ck_assert(strneq("0.0.1", "", 6) == false);
|
||||
ck_assert(strneq("foo", NULL, 5) == false);
|
||||
ck_assert(strneq(NULL, "foo", 5) == false);
|
||||
ck_assert(strneq("0.0.1", "0.0.1", 6) == true);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
static Suite *
|
||||
litest_utils_suite(void)
|
||||
{
|
||||
|
|
@ -1311,6 +1333,8 @@ litest_utils_suite(void)
|
|||
tcase_add_test(tc, list_test_insert);
|
||||
tcase_add_test(tc, list_test_append);
|
||||
tcase_add_test(tc, strverscmp_test);
|
||||
tcase_add_test(tc, streq_test);
|
||||
tcase_add_test(tc, strneq_test);
|
||||
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue