adds case independent versions of sstrprefix() and sstrsuffix()

Sun, 03 Nov 2019 16:34:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Nov 2019 16:34:29 +0100
changeset 364
5577d6c27a33
parent 363
8175ba2b3bcb
child 365
72da0e4cbf4a

adds case independent versions of sstrprefix() and sstrsuffix()

src/string.c file | annotate | diff | comparison | revisions
src/ucx/string.h file | annotate | diff | comparison | revisions
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
--- a/src/string.c	Sun Nov 03 16:22:46 2019 +0100
+++ b/src/string.c	Sun Nov 03 16:34:29 2019 +0100
@@ -598,6 +598,38 @@
     }
 }
 
+int scstrcaseprefix(scstr_t string, scstr_t prefix) {
+    if (string.length == 0) {
+        return prefix.length == 0;
+    }
+    if (prefix.length == 0) {
+        return 1;
+    }
+    
+    if (prefix.length > string.length) {
+        return 0;
+    } else {
+        scstr_t subs = scstrsubsl(string, 0, prefix.length);
+        return scstrcasecmp(subs, prefix) == 0;
+    }
+}
+
+int scstrcasesuffix(scstr_t string, scstr_t suffix) {
+    if (string.length == 0) {
+        return suffix.length == 0;
+    }
+    if (suffix.length == 0) {
+        return 1;
+    }
+    
+    if (suffix.length > string.length) {
+        return 0;
+    } else {
+        scstr_t subs = scstrsubs(string, string.length-suffix.length);
+        return scstrcasecmp(subs, suffix) == 0;
+    }
+}
+
 sstr_t scstrlower(scstr_t string) {
     sstr_t ret = sstrdup(string);
     for (size_t i = 0; i < ret.length ; i++) {
--- a/src/ucx/string.h	Sun Nov 03 16:22:46 2019 +0100
+++ b/src/ucx/string.h	Sun Nov 03 16:34:29 2019 +0100
@@ -938,6 +938,44 @@
 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix))
 
 /**
+ * Checks, if a string has a specific prefix, ignoring the case.
+ * 
+ * @param string the string to check
+ * @param prefix the prefix the string should have
+ * @return 1, if and only if the string has the specified prefix, 0 otherwise
+ */
+int scstrcaseprefix(scstr_t string, scstr_t prefix);
+
+/**
+ * Checks, if a string has a specific prefix, ignoring the case.
+ * 
+ * @param string the string to check
+ * @param prefix the prefix the string should have
+ * @return 1, if and only if the string has the specified prefix, 0 otherwise
+ */
+#define sstrcaseprefix(string, prefix) \
+  scstrcaseprefix(SCSTR(string), SCSTR(prefix))
+
+/**
+ * Checks, if a string has a specific suffix, ignoring the case.
+ * 
+ * @param string the string to check
+ * @param suffix the suffix the string should have
+ * @return 1, if and only if the string has the specified suffix, 0 otherwise
+ */
+int scstrcasesuffix(scstr_t string, scstr_t suffix);
+
+/**
+ * Checks, if a string has a specific suffix, ignoring the case.
+ *
+ * @param string the string to check
+ * @param suffix the suffix the string should have
+ * @return 1, if and only if the string has the specified suffix, 0 otherwise
+ */
+#define sstrcasesuffix(string, suffix) \
+  scstrcasesuffix(SCSTR(string), SCSTR(suffix))
+
+/**
  * Returns a lower case version of a string.
  * 
  * This function creates a duplicate of the input string, first
--- a/test/main.c	Sun Nov 03 16:22:46 2019 +0100
+++ b/test/main.c	Sun Nov 03 16:34:29 2019 +0100
@@ -137,6 +137,7 @@
         ucx_test_register(suite, test_sstrsplit);
         ucx_test_register(suite, test_sstrtrim);
         ucx_test_register(suite, test_sstrprefixsuffix);
+        ucx_test_register(suite, test_sstrcaseprefixsuffix);
         
         /* UcxLogger Tests */
         ucx_test_register(suite, test_ucx_logger_new);
--- a/test/string_tests.c	Sun Nov 03 16:22:46 2019 +0100
+++ b/test/string_tests.c	Sun Nov 03 16:34:29 2019 +0100
@@ -452,3 +452,27 @@
     
     UCX_TEST_END
 }
+
+UCX_TEST(test_sstrcaseprefixsuffix) {
+    sstr_t str = ST("test my prefix and my suffix");
+    sstr_t empty = ST("");
+    
+    UCX_TEST_BEGIN
+    
+    UCX_TEST_ASSERT(!sstrcaseprefix(empty, S("pREf")), "prefix empty string fails");
+    UCX_TEST_ASSERT(!sstrcasesuffix(empty, S("sUf")), "suffix empty string fails");
+    
+    UCX_TEST_ASSERT(sstrcaseprefix(str, empty), "empty prefix fails");
+    UCX_TEST_ASSERT(sstrcasesuffix(str, empty), "empty suffix fails");
+    
+    UCX_TEST_ASSERT(sstrcaseprefix(empty, empty), "string and prefix empty fails");
+    UCX_TEST_ASSERT(sstrcasesuffix(empty, empty), "string and suffix empty fails");
+    
+    UCX_TEST_ASSERT(sstrcaseprefix(str, S("TEST ")), "prefix false negative");
+    UCX_TEST_ASSERT(!sstrcaseprefix(str, S("8-) fsck ")), "prefix false positive");
+    
+    UCX_TEST_ASSERT(sstrcasesuffix(str, S("FIX")), "suffix false negative");
+    UCX_TEST_ASSERT(!sstrcasesuffix(str, S("fox")), "suffix false positive");
+    
+    UCX_TEST_END
+}
--- a/test/string_tests.h	Sun Nov 03 16:22:46 2019 +0100
+++ b/test/string_tests.h	Sun Nov 03 16:34:29 2019 +0100
@@ -47,6 +47,7 @@
 UCX_TEST(test_sstrsplit);
 UCX_TEST(test_sstrtrim);
 UCX_TEST(test_sstrprefixsuffix);
+UCX_TEST(test_sstrcaseprefixsuffix);
 
 #ifdef	__cplusplus
 }

mercurial