From 9abf6a2709e9c1a2cc1c246d8307fd2e2b3d566a Mon Sep 17 00:00:00 2001 From: David Steele Date: Fri, 30 Jul 2021 18:08:04 -0400 Subject: [PATCH] Add lstComparatorZ(). Works just like lstComparatorStr() but with zero-terminated strings. --- src/common/type/list.c | 15 +++++++++++++++ src/common/type/list.h | 3 +++ test/src/module/common/typeListTest.c | 10 ++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/common/type/list.c b/src/common/type/list.c index baf983512..b64624ea8 100644 --- a/src/common/type/list.c +++ b/src/common/type/list.c @@ -97,6 +97,21 @@ lstComparatorStr(const void *item1, const void *item2) FUNCTION_TEST_RETURN(strCmp(*(String **)item1, *(String **)item2)); } +/**********************************************************************************************************************************/ +int +lstComparatorZ(const void *item1, const void *item2) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM_P(VOID, item1); + FUNCTION_TEST_PARAM_P(VOID, item2); + FUNCTION_TEST_END(); + + ASSERT(item1 != NULL); + ASSERT(item2 != NULL); + + FUNCTION_TEST_RETURN(strcmp(*(char **)item1, *(char **)item2)); +} + /*********************************************************************************************************************************** General function for a descending comparator that simply switches the parameters on the main comparator (which should be asc) ***********************************************************************************************************************************/ diff --git a/src/common/type/list.h b/src/common/type/list.h index 230d05005..047302c28 100644 --- a/src/common/type/list.h +++ b/src/common/type/list.h @@ -46,6 +46,9 @@ typedef int ListComparator(const void *item1, const void *item2); // General purpose list comparator for Strings or structs with a String as the first member int lstComparatorStr(const void *item1, const void *item2); +// General purpose list comparator for zero-terminated strings or structs with a zero-terminated string as the first member +int lstComparatorZ(const void *item1, const void *item2); + /*********************************************************************************************************************************** Constructors ***********************************************************************************************************************************/ diff --git a/test/src/module/common/typeListTest.c b/test/src/module/common/typeListTest.c index 1c02e56bf..278969226 100644 --- a/test/src/module/common/typeListTest.c +++ b/test/src/module/common/typeListTest.c @@ -180,6 +180,16 @@ testRun(void) TEST_RESULT_INT(*(int *)lstGet(list, 1), 3, "sort value 1"); TEST_RESULT_INT(*(int *)lstGet(list, 2), 3, "sort value 2"); TEST_RESULT_INT(*(int *)lstGet(list, 3), 2, "sort value 3"); + + // ------------------------------------------------------------------------------------------------------------------------- + TEST_TITLE("lstComparatorZ()"); + + const char *string1 = "abc"; + const char *string2 = "def"; + + TEST_RESULT_INT(lstComparatorZ(&string1, &string1), 0, "strings are equal"); + TEST_RESULT_INT(lstComparatorZ(&string1, &string2), -1, "first string is less"); + TEST_RESULT_INT(lstComparatorZ(&string2, &string1), 1, "first string is greater"); } // *****************************************************************************************************************************