# HG changeset patch # User Mike Becker # Date 1376901851 -7200 # Node ID c27c2425c0b1d2c6c0d132adbe8f7ca7ef975e50 # Parent 1aa598f36872b12fb9eaea37f905011686d41fa8 added sstrrchr diff -r 1aa598f36872 -r c27c2425c0b1 test/main.c --- 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); diff -r 1aa598f36872 -r c27c2425c0b1 test/string_tests.c --- 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"; diff -r 1aa598f36872 -r c27c2425c0b1 test/string_tests.h --- 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); diff -r 1aa598f36872 -r c27c2425c0b1 ucx/string.c --- 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); } diff -r 1aa598f36872 -r c27c2425c0b1 ucx/string.h --- 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 chr + * @return a substring starting at the first location of chr * * @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 chr + * + * @see sstrsubs() + */ +sstr_t sstrrchr(sstr_t string, int chr); + +/** * Splits a string into parts by using a delimiter string. * * This function will return NULL, if one of the following happens: