Mon, 19 Aug 2013 10:44:11 +0200
added sstrrchr
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 Fri Aug 16 14:48:58 2013 +0200 +++ b/test/main.c Mon Aug 19 10:44:11 2013 +0200 @@ -115,6 +115,7 @@ /* 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_sstrsplit); ucx_test_register(suite, test_sstrtrim); ucx_test_register(suite, test_sstrprefixsuffix);
--- a/test/string_tests.c Fri Aug 16 14:48:58 2013 +0200 +++ b/test/string_tests.c Mon Aug 19 10:44:11 2013 +0200 @@ -69,6 +69,23 @@ free(cat.ptr); } +UCX_TEST(test_sstrchr) { + sstr_t str = ST("I will find you - and I will kill you"); + UCX_TEST_BEGIN + + sstr_t result = sstrchr(str, 'w'); + UCX_TEST_ASSERT(result.length == 35, "sstrchr returned wrong length"); + UCX_TEST_ASSERT(strcmp("will find you - and I will kill you", result.ptr) + == 0, "sstrchr did not return the expected string"); + + result = sstrrchr(str, 'w'); + UCX_TEST_ASSERT(result.length == 13, "sstrrchr returned wrong length"); + UCX_TEST_ASSERT(strcmp("will kill you", result.ptr) + == 0, "sstrrchr did not return the expected string"); + + UCX_TEST_END +} + UCX_TEST(test_sstrsplit) { const char *original = "this,is,a,csv,string";
--- a/test/string_tests.h Fri Aug 16 14:48:58 2013 +0200 +++ b/test/string_tests.h Mon Aug 19 10:44:11 2013 +0200 @@ -38,6 +38,7 @@ UCX_TEST(test_sstr); UCX_TEST(test_sstr_len_cat); +UCX_TEST(test_sstrchr); UCX_TEST(test_sstrsplit); UCX_TEST(test_sstrtrim); UCX_TEST(test_sstrprefixsuffix);
--- a/ucx/string.c Fri Aug 16 14:48:58 2013 +0200 +++ b/ucx/string.c Mon Aug 19 10:44:11 2013 +0200 @@ -119,6 +119,20 @@ return n; } +sstr_t sstrrchr(sstr_t s, int c) { + if (s.length > 0) { + for(size_t i=s.length-1;i>=0;i--) { + if(s.ptr[i] == c) { + return sstrsubs(s, i); + } + } + } + sstr_t n; + n.ptr = NULL; + n.length = 0; + return n; +} + sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) { return sstrsplit_a(ucx_default_allocator(), s, d, n); }
--- a/ucx/string.h Fri Aug 16 14:48:58 2013 +0200 +++ b/ucx/string.h Mon Aug 19 10:44:11 2013 +0200 @@ -195,13 +195,27 @@ * * @param string the string where to locate the character * @param chr the character to locate - * @return a substring starting at the least location of <code>chr</code> + * @return a substring starting at the first location of <code>chr</code> * * @see sstrsubs() */ sstr_t sstrchr(sstr_t string, int chr); /** + * Returns a substring starting at the location of the last occurrence of the + * specified character. + * + * If the string does not contain the character, an empty string is returned. + * + * @param string the string where to locate the character + * @param chr the character to locate + * @return a substring starting at the last location of <code>chr</code> + * + * @see sstrsubs() + */ +sstr_t sstrrchr(sstr_t string, int chr); + +/** * Splits a string into parts by using a delimiter string. * * This function will return <code>NULL</code>, if one of the following happens: