Mon, 14 Jul 2014 13:51:02 +0200
added new sstrcat
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 Mon Jul 14 12:45:48 2014 +0200 +++ b/test/main.c Mon Jul 14 13:51:02 2014 +0200 @@ -121,6 +121,7 @@ ucx_test_register(suite, test_sstr_len); ucx_test_register(suite, test_sstrcmp); ucx_test_register(suite, test_sstrcasecmp); + ucx_test_register(suite, test_sstrcat); ucx_test_register(suite, test_sstrchr_sstrrchr); ucx_test_register(suite, test_sstrsplit); ucx_test_register(suite, test_sstrtrim);
--- a/test/string_tests.c Mon Jul 14 12:45:48 2014 +0200 +++ b/test/string_tests.c Mon Jul 14 13:51:02 2014 +0200 @@ -54,6 +54,7 @@ UCX_TEST_END } + UCX_TEST(test_sstrchr_sstrrchr) { sstr_t str = ST("I will find you - and I will kill you"); UCX_TEST_BEGIN @@ -100,6 +101,37 @@ UCX_TEST_END } + +UCX_TEST(test_sstrcat) { + sstr_t s1 = S("12"); + sstr_t s2 = S("34"); + sstr_t s3 = S("56"); + sstr_t sn = { NULL, 0 }; + + UCX_TEST_BEGIN + + sstr_t t1 = sstrcat(2, s1, s2); + UCX_TEST_ASSERT(!sstrcmp(t1, S("1234")), "t1: wrong content"); + free(t1.ptr); + + sstr_t t2 = sstrcat(3, s1, s2, s3); + UCX_TEST_ASSERT(!sstrcmp(t2, S("123456")), "t2: wrong content"); + free(t2.ptr); + + sstr_t t3 = sstrcat(6, s1, sn, s2, sn, s3, sn); + UCX_TEST_ASSERT(!sstrcmp(t3, S("123456")), "t3: wrong content"); + free(t3.ptr); + + sstr_t t4 = sstrcat(2, sn, sn); + UCX_TEST_ASSERT(t4.ptr, "t4.ptr is NULL"); + UCX_TEST_ASSERT(t4.length == 0, "t4 has wrong length"); + free(t4.ptr); + + + UCX_TEST_END + +} + UCX_TEST(test_sstrsplit) { const char *original = "this,is,a,csv,string";
--- a/test/string_tests.h Mon Jul 14 12:45:48 2014 +0200 +++ b/test/string_tests.h Mon Jul 14 13:51:02 2014 +0200 @@ -40,6 +40,7 @@ UCX_TEST(test_sstr_len); UCX_TEST(test_sstrcmp); UCX_TEST(test_sstrcasecmp); +UCX_TEST(test_sstrcat); UCX_TEST(test_sstrchr_sstrrchr); UCX_TEST(test_sstrsplit); UCX_TEST(test_sstrtrim);
--- a/ucx/string.c Mon Jul 14 12:45:48 2014 +0200 +++ b/ucx/string.c Mon Jul 14 13:51:02 2014 +0200 @@ -61,6 +61,74 @@ return size; } +static sstr_t sstrvcat_a( + UcxAllocator *a, + size_t count, + sstr_t s1, + sstr_t s2, + va_list ap) { + sstr_t str; + str.ptr = NULL; + str.length = 0; + if(count < 2) { + return str; + } + + sstr_t *strings = calloc(count, sizeof(sstr_t)); + if(!strings) { + return str; + } + + // get all args and overall length + strings[0] = s1; + strings[1] = s2; + size_t strlen = s1.length + s2.length; + for (size_t i=2;i<count;i++) { + sstr_t s = va_arg (ap, sstr_t); + strings[i] = s; + strlen += s.length; + } + + // create new string + str.ptr = malloc(strlen + 1); + str.length = strlen; + if(!str.ptr) { + free(strings); + str.length = 0; + return str; + } + + // concatenate strings + size_t pos = 0; + for (size_t i=0;i<count;i++) { + sstr_t s = strings[i]; + memcpy(str.ptr + pos, s.ptr, s.length); + pos += s.length; + } + + str.ptr[str.length] = '\0'; + + free(strings); + + return str; +} + +sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...) { + va_list ap; + va_start(ap, s2); + sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, s2, ap); + va_end(ap); + return s; +} + +sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...) { + va_list ap; + va_start(ap, s2); + sstr_t s = sstrvcat_a(a, count, s1, s2, ap); + va_end(ap); + return s; +} + sstr_t sstrsubs(sstr_t s, size_t start) { return sstrsubsl (s, start, s.length-start); }
--- a/ucx/string.h Mon Jul 14 12:45:48 2014 +0200 +++ b/ucx/string.h Mon Jul 14 13:51:02 2014 +0200 @@ -120,6 +120,17 @@ size_t sstrnlen(size_t count, sstr_t string, ...); /** + * Concatenates strings. + * + * @param count the total number of strings to concatenate + * @param ... all strings + * @return the concatenated string + */ +sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); +sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); + + +/** * Returns a substring starting at the specified location. * * <b>Attention:</b> the new string references the same memory area as the