added sstrrchr

Mon, 19 Aug 2013 10:44:11 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 19 Aug 2013 10:44:11 +0200
changeset 148
c27c2425c0b1
parent 147
1aa598f36872
child 149
3bf87676d42d

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
     1.1 --- a/test/main.c	Fri Aug 16 14:48:58 2013 +0200
     1.2 +++ b/test/main.c	Mon Aug 19 10:44:11 2013 +0200
     1.3 @@ -115,6 +115,7 @@
     1.4          /* sstring Tests */
     1.5          ucx_test_register(suite, test_sstr);
     1.6          ucx_test_register(suite, test_sstr_len_cat);
     1.7 +        ucx_test_register(suite, test_sstrchr);
     1.8          ucx_test_register(suite, test_sstrsplit);
     1.9          ucx_test_register(suite, test_sstrtrim);
    1.10          ucx_test_register(suite, test_sstrprefixsuffix);
     2.1 --- a/test/string_tests.c	Fri Aug 16 14:48:58 2013 +0200
     2.2 +++ b/test/string_tests.c	Mon Aug 19 10:44:11 2013 +0200
     2.3 @@ -69,6 +69,23 @@
     2.4      free(cat.ptr);
     2.5  }
     2.6  
     2.7 +UCX_TEST(test_sstrchr) {
     2.8 +    sstr_t str = ST("I will find you - and I will kill you");
     2.9 +    UCX_TEST_BEGIN
    2.10 +    
    2.11 +    sstr_t result = sstrchr(str, 'w');
    2.12 +    UCX_TEST_ASSERT(result.length == 35, "sstrchr returned wrong length");
    2.13 +    UCX_TEST_ASSERT(strcmp("will find you - and I will kill you", result.ptr)
    2.14 +        == 0, "sstrchr did not return the expected string");
    2.15 +    
    2.16 +    result = sstrrchr(str, 'w');
    2.17 +    UCX_TEST_ASSERT(result.length == 13, "sstrrchr returned wrong length");
    2.18 +    UCX_TEST_ASSERT(strcmp("will kill you", result.ptr)
    2.19 +        == 0, "sstrrchr did not return the expected string");
    2.20 +    
    2.21 +    UCX_TEST_END
    2.22 +}
    2.23 +
    2.24  UCX_TEST(test_sstrsplit) {
    2.25  
    2.26      const char *original = "this,is,a,csv,string";
     3.1 --- a/test/string_tests.h	Fri Aug 16 14:48:58 2013 +0200
     3.2 +++ b/test/string_tests.h	Mon Aug 19 10:44:11 2013 +0200
     3.3 @@ -38,6 +38,7 @@
     3.4  
     3.5  UCX_TEST(test_sstr);
     3.6  UCX_TEST(test_sstr_len_cat);
     3.7 +UCX_TEST(test_sstrchr);
     3.8  UCX_TEST(test_sstrsplit);
     3.9  UCX_TEST(test_sstrtrim);
    3.10  UCX_TEST(test_sstrprefixsuffix);
     4.1 --- a/ucx/string.c	Fri Aug 16 14:48:58 2013 +0200
     4.2 +++ b/ucx/string.c	Mon Aug 19 10:44:11 2013 +0200
     4.3 @@ -119,6 +119,20 @@
     4.4      return n;
     4.5  }
     4.6  
     4.7 +sstr_t sstrrchr(sstr_t s, int c) {
     4.8 +    if (s.length > 0) {
     4.9 +        for(size_t i=s.length-1;i>=0;i--) {
    4.10 +            if(s.ptr[i] == c) {
    4.11 +                return sstrsubs(s, i);
    4.12 +            }
    4.13 +        }
    4.14 +    }
    4.15 +    sstr_t n;
    4.16 +    n.ptr = NULL;
    4.17 +    n.length = 0;
    4.18 +    return n;
    4.19 +}
    4.20 +
    4.21  sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
    4.22      return sstrsplit_a(ucx_default_allocator(), s, d, n);
    4.23  }
     5.1 --- a/ucx/string.h	Fri Aug 16 14:48:58 2013 +0200
     5.2 +++ b/ucx/string.h	Mon Aug 19 10:44:11 2013 +0200
     5.3 @@ -195,13 +195,27 @@
     5.4   * 
     5.5   * @param string the string where to locate the character
     5.6   * @param chr    the character to locate
     5.7 - * @return       a substring starting at the least location of <code>chr</code>
     5.8 + * @return       a substring starting at the first location of <code>chr</code>
     5.9   * 
    5.10   * @see sstrsubs()
    5.11   */
    5.12  sstr_t sstrchr(sstr_t string, int chr);
    5.13  
    5.14  /**
    5.15 + * Returns a substring starting at the location of the last occurrence of the
    5.16 + * specified character.
    5.17 + * 
    5.18 + * If the string does not contain the character, an empty string is returned.
    5.19 + * 
    5.20 + * @param string the string where to locate the character
    5.21 + * @param chr    the character to locate
    5.22 + * @return       a substring starting at the last location of <code>chr</code>
    5.23 + * 
    5.24 + * @see sstrsubs()
    5.25 + */
    5.26 +sstr_t sstrrchr(sstr_t string, int chr);
    5.27 +
    5.28 +/**
    5.29   * Splits a string into parts by using a delimiter string.
    5.30   * 
    5.31   * This function will return <code>NULL</code>, if one of the following happens:

mercurial