Fri, 05 Oct 2012 11:55:36 +0200
Merge with 22fb97e9f58a0a8b18db7f81b7392dae0cd64571
test/main.c | file | annotate | diff | comparison | revisions |
--- a/test/main.c Fri Oct 05 11:52:53 2012 +0200 +++ b/test/main.c Fri Oct 05 11:55:36 2012 +0200 @@ -152,6 +152,8 @@ ucx_test_register(suite, test_ucx_map_clone); /* sstring Tests */ + ucx_test_register(suite, test_sstr); + ucx_test_register(suite, test_sstr_len_cat); ucx_test_register(suite, test_sstrsplit); ucx_test_run(suite, stdout);
--- a/test/string_tests.c Fri Oct 05 11:52:53 2012 +0200 +++ b/test/string_tests.c Fri Oct 05 11:55:36 2012 +0200 @@ -4,6 +4,53 @@ #include "string_tests.h" +UCX_TEST_IMPLEMENT(test_sstr) { + sstr_t s1 = sstr("1234"); + sstr_t s2 = sstrn("ab", 2); + + UCX_TEST_BEGIN + + UCX_TEST_ASSERT(s1.length == 4, "s1 length must be 4"); + UCX_TEST_ASSERT(s2.length == 2, "s2 length must be 2"); + + UCX_TEST_END +} + +UCX_TEST_IMPLEMENT(test_sstr_len_cat) { + sstr_t s1 = sstr("1234"); + sstr_t s2 = sstr(".:.:."); + sstr_t s3 = sstr("X"); + + sstr_t cat; + + size_t len = sstrnlen(3, s1, s2, s3); + + UCX_TEST_BEGIN + + UCX_TEST_ASSERT(len == s1.length + s2.length + s3.length, + "sstrnlen returned wrong size"); + + len = s1.length + s2.length + s3.length; + + cat.ptr = malloc(len + 1); + cat.ptr[len] = 0; + cat.length = len; + + sstrncat(3, cat, s1, s2, s3); + + UCX_TEST_ASSERT(cat.ptr[0] == '1', "sstrncat, wrong content"); + UCX_TEST_ASSERT(cat.ptr[1] == '2', "sstrncat, wrong content"); + UCX_TEST_ASSERT(cat.ptr[2] == '3', "sstrncat, wrong content"); + UCX_TEST_ASSERT(cat.ptr[3] == '4', "sstrncat, wrong content"); + UCX_TEST_ASSERT(cat.ptr[4] == '.', "sstrncat, wrong content"); + UCX_TEST_ASSERT(cat.ptr[8] == '.', "sstrncat, wrong content"); + UCX_TEST_ASSERT(cat.ptr[9] == 'X', "sstrncat, wrong content"); + + UCX_TEST_END + + free(cat.ptr); +} + UCX_TEST_IMPLEMENT(test_sstrsplit) { const char *original = "this,is,a,csv,string";
--- a/test/string_tests.h Fri Oct 05 11:52:53 2012 +0200 +++ b/test/string_tests.h Fri Oct 05 11:55:36 2012 +0200 @@ -12,6 +12,8 @@ extern "C" { #endif +UCX_TEST_DECLARE(test_sstr) +UCX_TEST_DECLARE(test_sstr_len_cat) UCX_TEST_DECLARE(test_sstrsplit) #ifdef __cplusplus
--- a/ucx/string.c Fri Oct 05 11:52:53 2012 +0200 +++ b/ucx/string.c Fri Oct 05 11:55:36 2012 +0200 @@ -58,11 +58,24 @@ va_list ap; va_start(ap, c1); s.ptr[0] = 0; - - s.ptr = strncat (s.ptr, c1.ptr, s.length); + + size_t len = s.length; + size_t cplen = c1.length > len ? len : c1.length; + char *ptr = s.ptr; + + memcpy(ptr, c1.ptr, cplen); + len -= cplen; + ptr += cplen; for (int i=0;i<n-1;i++) { sstr_t str = va_arg (ap, sstr_t); - s.ptr = strncat (s.ptr, str.ptr, s.length); + cplen = str.length > len ? len : str.length; + if(cplen <= 0) { + va_end(ap); + return s; + } + memcpy(ptr, str.ptr, cplen); + len -= cplen; + ptr += cplen; } va_end(ap);