added sstrcasecmp

Mon, 19 Aug 2013 11:54:54 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 19 Aug 2013 11:54:54 +0200
changeset 149
3bf87676d42d
parent 148
c27c2425c0b1
child 150
1cf2eabf94ed

added sstrcasecmp

test/main.c file | annotate | diff | comparison | revisions
test/string_tests.c file | annotate | diff | comparison | revisions
test/string_tests.h file | annotate | diff | comparison | revisions
ucx/string.c file | annotate | diff | comparison | revisions
ucx/string.h file | annotate | diff | comparison | revisions
--- a/test/main.c	Mon Aug 19 10:44:11 2013 +0200
+++ b/test/main.c	Mon Aug 19 11:54:54 2013 +0200
@@ -115,7 +115,9 @@
         /* sstring Tests */
         ucx_test_register(suite, test_sstr);
         ucx_test_register(suite, test_sstr_len_cat);
-        ucx_test_register(suite, test_sstrchr);
+        ucx_test_register(suite, test_sstrcmp);
+        ucx_test_register(suite, test_sstrcasecmp);
+        ucx_test_register(suite, test_sstrchr_sstrrchr);
         ucx_test_register(suite, test_sstrsplit);
         ucx_test_register(suite, test_sstrtrim);
         ucx_test_register(suite, test_sstrprefixsuffix);
--- a/test/string_tests.c	Mon Aug 19 10:44:11 2013 +0200
+++ b/test/string_tests.c	Mon Aug 19 11:54:54 2013 +0200
@@ -69,7 +69,7 @@
     free(cat.ptr);
 }
 
-UCX_TEST(test_sstrchr) {
+UCX_TEST(test_sstrchr_sstrrchr) {
     sstr_t str = ST("I will find you - and I will kill you");
     UCX_TEST_BEGIN
     
@@ -86,6 +86,35 @@
     UCX_TEST_END
 }
 
+UCX_TEST(test_sstrcmp) {
+    sstr_t str = ST("compare this");
+    
+    UCX_TEST_BEGIN
+    UCX_TEST_ASSERT(sstrcmp(str, S("compare this")) == 0, "false negative");
+    UCX_TEST_ASSERT(sstrcmp(str, S("Compare This")) != 0, "false positive");
+    UCX_TEST_ASSERT(sstrcmp(str, S("compare tool")) < 0, "memcmp < 0 failed");
+    UCX_TEST_ASSERT(sstrcmp(str, S("compare shit")) > 0, "memcmp > 0 failed");
+    UCX_TEST_ASSERT(sstrcmp(str, S("compare this not")) < 0, "len < 0 failed");
+    UCX_TEST_ASSERT(sstrcmp(str, S("compare")) > 0, "len > 0 failed");
+    UCX_TEST_END
+}
+
+UCX_TEST(test_sstrcasecmp) {
+    
+    sstr_t str = ST("compare this");
+    
+    UCX_TEST_BEGIN
+    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare this")) == 0, "false negative");
+    UCX_TEST_ASSERT(sstrcasecmp(str, S("Compare This")) == 0,
+        "not ignoring case");
+    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare tool")) < 0, "< 0 failed");
+    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare shit")) > 0, "> 0 failed");
+    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare this not")) < 0,
+        "len < 0 failed");
+    UCX_TEST_ASSERT(sstrcasecmp(str, S("compare")) > 0, "len > 0 failed");
+    UCX_TEST_END
+}
+
 UCX_TEST(test_sstrsplit) {
 
     const char *original = "this,is,a,csv,string";
--- a/test/string_tests.h	Mon Aug 19 10:44:11 2013 +0200
+++ b/test/string_tests.h	Mon Aug 19 11:54:54 2013 +0200
@@ -38,7 +38,9 @@
 
 UCX_TEST(test_sstr);
 UCX_TEST(test_sstr_len_cat);
-UCX_TEST(test_sstrchr);
+UCX_TEST(test_sstrcmp);
+UCX_TEST(test_sstrcasecmp);
+UCX_TEST(test_sstrchr_sstrrchr);
 UCX_TEST(test_sstrsplit);
 UCX_TEST(test_sstrtrim);
 UCX_TEST(test_sstrprefixsuffix);
--- a/ucx/string.c	Mon Aug 19 10:44:11 2013 +0200
+++ b/ucx/string.c	Mon Aug 19 11:54:54 2013 +0200
@@ -211,6 +211,20 @@
     }
 }
 
+int sstrcasecmp(sstr_t s1, sstr_t s2) {
+    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
+    } else if (s1.length > s2.length) {
+        return 1;
+    } else {
+        return -1;
+    }
+}
+
 sstr_t sstrdup(sstr_t s) {
     return sstrdup_a(ucx_default_allocator(), s);
 }
--- a/ucx/string.h	Mon Aug 19 10:44:11 2013 +0200
+++ b/ucx/string.h	Mon Aug 19 11:54:54 2013 +0200
@@ -301,6 +301,22 @@
 int sstrcmp(sstr_t s1, sstr_t s2);
 
 /**
+ * Compares two UCX strings ignoring the case.
+ * 
+ * At first it compares the sstr_t.length attribute of the two strings. If and
+ * only if the lengths match, both strings are compared char by char ignoring
+ * the case.
+ * 
+ * @param s1 the first string
+ * @param s2 the second string
+ * @return -1, if the length of s1 is less than the length of s2 or 1, if the 
+ * length of s1 is greater than the length of s2 or the difference between the
+ * first two differing characters otherwise (i.e. 0 if the strings match and
+ * no characters differ)
+ */
+int sstrcasecmp(sstr_t s1, sstr_t s2);
+
+/**
  * Creates a duplicate of the specified string.
  * 
  * The new sstr_t will contain a copy allocated by standard

mercurial