# HG changeset patch # User Mike Becker # Date 1735741610 -3600 # Node ID 028cb6d221978906c79cf29f71f334c5f667da1b # Parent 0a5a356a44868eeefd07580ccb56f12eae798609 fix cx_strcmp() and cx_strcasecmp() - fixes #546 diff -r 0a5a356a4486 -r 028cb6d22197 CHANGELOG --- a/CHANGELOG Wed Jan 01 14:03:49 2025 +0100 +++ b/CHANGELOG Wed Jan 01 15:26:50 2025 +0100 @@ -34,6 +34,7 @@ * removes CMake * removes GTest dependency * removes flags to disable SBO in tests + * fixes cx_strcmp() and cx_strcasecmp() not being useful for lexicographic ordering * fixes cx_hash_key_cxstr() evaluating the argument twice * fixes wrong link from UCX 2 documentation to UCX 3 documentation * fixes critical bug that produced wrong results when comparing lists of different type but same size diff -r 0a5a356a4486 -r 028cb6d22197 src/string.c --- a/src/string.c Wed Jan 01 14:03:49 2025 +0100 +++ b/src/string.c Wed Jan 01 15:26:50 2025 +0100 @@ -463,8 +463,10 @@ cxstring s1, cxstring s2 ) { + int r = strncmp(s1.ptr, s2.ptr, s1.length); + if (r != 0) return r; if (s1.length == s2.length) { - return memcmp(s1.ptr, s2.ptr, s1.length); + return 0; } else if (s1.length > s2.length) { return 1; } else { @@ -476,12 +478,14 @@ cxstring s1, cxstring s2 ) { +#ifdef _WIN32 + int r = _strnicmp(s1.ptr, s2.ptr, s1.length); +#else + int r = strncasecmp(s1.ptr, s2.ptr, s1.length); +#endif + if (r != 0) return r; if (s1.length == s2.length) { -#ifdef _WIN32 - return _strnicmp(s1.ptr, s2.ptr, s1.length); -#else - return strncasecmp(s1.ptr, s2.ptr, s1.length); -#endif + return 0; } else if (s1.length > s2.length) { return 1; } else { diff -r 0a5a356a4486 -r 028cb6d22197 tests/test_string.c --- a/tests/test_string.c Wed Jan 01 14:03:49 2025 +0100 +++ b/tests/test_string.c Wed Jan 01 15:26:50 2025 +0100 @@ -246,6 +246,10 @@ CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare shit"))); CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("compare this not"))); CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("compare"))); + CX_TEST_ASSERT(0 > cx_strcmp(str, CX_STR("lex"))); + CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("another lex test"))); + CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("Lex"))); + CX_TEST_ASSERT(0 < cx_strcmp(str, CX_STR("Another lex test"))); cxstring str2 = CX_STR("Compare This"); CX_TEST_ASSERT(0 != cx_strcmp_p(&str, &str2)); @@ -265,6 +269,10 @@ CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare shit"))); CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("compare this not"))); CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("compare"))); + CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("lex"))); + CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("another lex test"))); + CX_TEST_ASSERT(0 > cx_strcasecmp(str, CX_STR("Lex"))); + CX_TEST_ASSERT(0 < cx_strcasecmp(str, CX_STR("Another lex test"))); cxstring str2 = CX_STR("Compare This"); CX_TEST_ASSERT(0 == cx_strcasecmp_p(&str, &str2));