# HG changeset patch # User Mike Becker # Date 1572795269 -3600 # Node ID 5577d6c27a3351e6a8880e6a44f4fb4ff739e951 # Parent 8175ba2b3bcb6ed86e70eae262b16050897342e4 adds case independent versions of sstrprefix() and sstrsuffix() diff -r 8175ba2b3bcb -r 5577d6c27a33 src/string.c --- 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++) { diff -r 8175ba2b3bcb -r 5577d6c27a33 src/ucx/string.h --- 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 diff -r 8175ba2b3bcb -r 5577d6c27a33 test/main.c --- 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); diff -r 8175ba2b3bcb -r 5577d6c27a33 test/string_tests.c --- 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 +} diff -r 8175ba2b3bcb -r 5577d6c27a33 test/string_tests.h --- 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 }