# HG changeset patch # User Mike Becker # Date 1376906094 -7200 # Node ID 3bf87676d42dd38b829d9150bb3399238c43850a # Parent c27c2425c0b1d2c6c0d132adbe8f7ca7ef975e50 added sstrcasecmp diff -r c27c2425c0b1 -r 3bf87676d42d test/main.c --- 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); diff -r c27c2425c0b1 -r 3bf87676d42d test/string_tests.c --- 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"; diff -r c27c2425c0b1 -r 3bf87676d42d test/string_tests.h --- 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); diff -r c27c2425c0b1 -r 3bf87676d42d ucx/string.c --- 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); } diff -r c27c2425c0b1 -r 3bf87676d42d ucx/string.h --- 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