fix cx_strcmp() and cx_strcasecmp() - fixes #546

Wed, 01 Jan 2025 15:26:50 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 01 Jan 2025 15:26:50 +0100
changeset 1071
028cb6d22197
parent 1070
0a5a356a4486
child 1072
c89283cd559b

fix cx_strcmp() and cx_strcasecmp() - fixes #546

CHANGELOG file | annotate | diff | comparison | revisions
src/string.c file | annotate | diff | comparison | revisions
tests/test_string.c file | annotate | diff | comparison | revisions
--- 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
--- 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 {
--- 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));

mercurial