Sun, 03 Nov 2019 16:34:29 +0100
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 |
1.1 --- a/src/string.c Sun Nov 03 16:22:46 2019 +0100 1.2 +++ b/src/string.c Sun Nov 03 16:34:29 2019 +0100 1.3 @@ -598,6 +598,38 @@ 1.4 } 1.5 } 1.6 1.7 +int scstrcaseprefix(scstr_t string, scstr_t prefix) { 1.8 + if (string.length == 0) { 1.9 + return prefix.length == 0; 1.10 + } 1.11 + if (prefix.length == 0) { 1.12 + return 1; 1.13 + } 1.14 + 1.15 + if (prefix.length > string.length) { 1.16 + return 0; 1.17 + } else { 1.18 + scstr_t subs = scstrsubsl(string, 0, prefix.length); 1.19 + return scstrcasecmp(subs, prefix) == 0; 1.20 + } 1.21 +} 1.22 + 1.23 +int scstrcasesuffix(scstr_t string, scstr_t suffix) { 1.24 + if (string.length == 0) { 1.25 + return suffix.length == 0; 1.26 + } 1.27 + if (suffix.length == 0) { 1.28 + return 1; 1.29 + } 1.30 + 1.31 + if (suffix.length > string.length) { 1.32 + return 0; 1.33 + } else { 1.34 + scstr_t subs = scstrsubs(string, string.length-suffix.length); 1.35 + return scstrcasecmp(subs, suffix) == 0; 1.36 + } 1.37 +} 1.38 + 1.39 sstr_t scstrlower(scstr_t string) { 1.40 sstr_t ret = sstrdup(string); 1.41 for (size_t i = 0; i < ret.length ; i++) {
2.1 --- a/src/ucx/string.h Sun Nov 03 16:22:46 2019 +0100 2.2 +++ b/src/ucx/string.h Sun Nov 03 16:34:29 2019 +0100 2.3 @@ -938,6 +938,44 @@ 2.4 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix)) 2.5 2.6 /** 2.7 + * Checks, if a string has a specific prefix, ignoring the case. 2.8 + * 2.9 + * @param string the string to check 2.10 + * @param prefix the prefix the string should have 2.11 + * @return 1, if and only if the string has the specified prefix, 0 otherwise 2.12 + */ 2.13 +int scstrcaseprefix(scstr_t string, scstr_t prefix); 2.14 + 2.15 +/** 2.16 + * Checks, if a string has a specific prefix, ignoring the case. 2.17 + * 2.18 + * @param string the string to check 2.19 + * @param prefix the prefix the string should have 2.20 + * @return 1, if and only if the string has the specified prefix, 0 otherwise 2.21 + */ 2.22 +#define sstrcaseprefix(string, prefix) \ 2.23 + scstrcaseprefix(SCSTR(string), SCSTR(prefix)) 2.24 + 2.25 +/** 2.26 + * Checks, if a string has a specific suffix, ignoring the case. 2.27 + * 2.28 + * @param string the string to check 2.29 + * @param suffix the suffix the string should have 2.30 + * @return 1, if and only if the string has the specified suffix, 0 otherwise 2.31 + */ 2.32 +int scstrcasesuffix(scstr_t string, scstr_t suffix); 2.33 + 2.34 +/** 2.35 + * Checks, if a string has a specific suffix, ignoring the case. 2.36 + * 2.37 + * @param string the string to check 2.38 + * @param suffix the suffix the string should have 2.39 + * @return 1, if and only if the string has the specified suffix, 0 otherwise 2.40 + */ 2.41 +#define sstrcasesuffix(string, suffix) \ 2.42 + scstrcasesuffix(SCSTR(string), SCSTR(suffix)) 2.43 + 2.44 +/** 2.45 * Returns a lower case version of a string. 2.46 * 2.47 * This function creates a duplicate of the input string, first
3.1 --- a/test/main.c Sun Nov 03 16:22:46 2019 +0100 3.2 +++ b/test/main.c Sun Nov 03 16:34:29 2019 +0100 3.3 @@ -137,6 +137,7 @@ 3.4 ucx_test_register(suite, test_sstrsplit); 3.5 ucx_test_register(suite, test_sstrtrim); 3.6 ucx_test_register(suite, test_sstrprefixsuffix); 3.7 + ucx_test_register(suite, test_sstrcaseprefixsuffix); 3.8 3.9 /* UcxLogger Tests */ 3.10 ucx_test_register(suite, test_ucx_logger_new);
4.1 --- a/test/string_tests.c Sun Nov 03 16:22:46 2019 +0100 4.2 +++ b/test/string_tests.c Sun Nov 03 16:34:29 2019 +0100 4.3 @@ -452,3 +452,27 @@ 4.4 4.5 UCX_TEST_END 4.6 } 4.7 + 4.8 +UCX_TEST(test_sstrcaseprefixsuffix) { 4.9 + sstr_t str = ST("test my prefix and my suffix"); 4.10 + sstr_t empty = ST(""); 4.11 + 4.12 + UCX_TEST_BEGIN 4.13 + 4.14 + UCX_TEST_ASSERT(!sstrcaseprefix(empty, S("pREf")), "prefix empty string fails"); 4.15 + UCX_TEST_ASSERT(!sstrcasesuffix(empty, S("sUf")), "suffix empty string fails"); 4.16 + 4.17 + UCX_TEST_ASSERT(sstrcaseprefix(str, empty), "empty prefix fails"); 4.18 + UCX_TEST_ASSERT(sstrcasesuffix(str, empty), "empty suffix fails"); 4.19 + 4.20 + UCX_TEST_ASSERT(sstrcaseprefix(empty, empty), "string and prefix empty fails"); 4.21 + UCX_TEST_ASSERT(sstrcasesuffix(empty, empty), "string and suffix empty fails"); 4.22 + 4.23 + UCX_TEST_ASSERT(sstrcaseprefix(str, S("TEST ")), "prefix false negative"); 4.24 + UCX_TEST_ASSERT(!sstrcaseprefix(str, S("8-) fsck ")), "prefix false positive"); 4.25 + 4.26 + UCX_TEST_ASSERT(sstrcasesuffix(str, S("FIX")), "suffix false negative"); 4.27 + UCX_TEST_ASSERT(!sstrcasesuffix(str, S("fox")), "suffix false positive"); 4.28 + 4.29 + UCX_TEST_END 4.30 +}
5.1 --- a/test/string_tests.h Sun Nov 03 16:22:46 2019 +0100 5.2 +++ b/test/string_tests.h Sun Nov 03 16:34:29 2019 +0100 5.3 @@ -47,6 +47,7 @@ 5.4 UCX_TEST(test_sstrsplit); 5.5 UCX_TEST(test_sstrtrim); 5.6 UCX_TEST(test_sstrprefixsuffix); 5.7 +UCX_TEST(test_sstrcaseprefixsuffix); 5.8 5.9 #ifdef __cplusplus 5.10 }