X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1873a02acdc52506c010f43b4c78a8b8400dc0de..7820e0e1436e6b1c4968e2bb7653285a5a11b0bc:/src/xbt/xbt_str.cpp diff --git a/src/xbt/xbt_str.cpp b/src/xbt/xbt_str.cpp index b7da3789f8..06f1860fac 100644 --- a/src/xbt/xbt_str.cpp +++ b/src/xbt/xbt_str.cpp @@ -188,40 +188,12 @@ xbt_dynar_t xbt_str_split_quoted(const char *s) char *q=xbt_strdup(p); xbt_dynar_push(res,&q); } - free(str_to_free); + xbt_free(str_to_free); xbt_dynar_shrink(res, 0); xbt_dynar_free(&parsed); return res; } -/** @brief Join a set of strings as a single string */ -char *xbt_str_join(xbt_dynar_t dyn, const char *sep) -{ - int len = 1; - int dyn_len = xbt_dynar_length(dyn); - unsigned int cpt; - char* cursor; - - if (not dyn_len) - return xbt_strdup(""); - - /* compute the length */ - xbt_dynar_foreach(dyn, cpt, cursor) { - len += strlen(cursor); - } - len += strlen(sep) * dyn_len; - /* Do the job */ - char* res = (char*)xbt_malloc(len); - char* p = res; - xbt_dynar_foreach(dyn, cpt, cursor) { - if ((int) cpt < dyn_len - 1) - p += snprintf(p,len, "%s%s", cursor, sep); - else - p += snprintf(p,len, "%s", cursor); - } - return res; -} - /** @brief Join a set of strings as a single string * * The parameter must be a nullptr-terminated array of chars, @@ -299,14 +271,16 @@ XBT_TEST_SUITE("xbt_str", "String Handling"); #define mytest(name, input, expected) \ xbt_test_add(name); \ - d = xbt_str_split_quoted(input); \ - s = xbt_str_join(d, "XXX"); \ + a = static_cast(xbt_dynar_to_array(xbt_str_split_quoted(input))); \ + s = xbt_str_join_array(a, "XXX"); \ xbt_test_assert(not strcmp(s, expected), "Input (%s) leads to (%s) instead of (%s)", input, s, expected); \ - free(s); \ - xbt_dynar_free(&d); -XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted") + xbt_free(s); \ + for (int i = 0; a[i] != nullptr; i++) \ + xbt_free(a[i]); \ + xbt_free(a); +XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "Test the function xbt_str_split_quoted") { - xbt_dynar_t d; + char** a; char *s; mytest("Empty", "", "");