From: Martin Quinson Date: Sat, 20 May 2017 23:50:56 +0000 (+0200) Subject: Drop unused functions: xbt_str_varsubst(), xbt_strbuff_varsubst() X-Git-Tag: v3.16~281^2~2 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/784dd701843ed092f51ac04a2958e81ab0a24bb9?hp=0e871a7d4990b64178018d630fd18d6cf771c64a Drop unused functions: xbt_str_varsubst(), xbt_strbuff_varsubst() --- diff --git a/ChangeLog b/ChangeLog index a4961616b0..c8464eece9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -28,9 +28,10 @@ SimGrid (3.16) UNRELEASED - New algorithm to privatize globals: dlopen, with dynamic loading tricks - New option: smpi/keep-temps to not cleanup temp files - XBT/Replay: - - New function xbt_replay_action_get(): + XBT + - Replay: New function xbt_replay_action_get(): Retrieve the function previously associated to an event type. + - Drop unused functions: xbt_str_varsubst(), xbt_strbuff_varsubst() -- Release target: June 21 2017 -- Da SimGrid team diff --git a/include/xbt/str.h b/include/xbt/str.h index 78d7b3ba81..0f10bef8d0 100644 --- a/include/xbt/str.h +++ b/include/xbt/str.h @@ -41,7 +41,6 @@ XBT_PUBLIC(char *) xbt_str_join(xbt_dynar_t dynar, const char *sep); XBT_PUBLIC(char *) xbt_str_join_array(const char *const *strs, const char *sep); XBT_PUBLIC(void) xbt_str_subst(char *str, char from, char to, int amount); -XBT_PUBLIC(char *) xbt_str_varsubst(const char *str, xbt_dict_t patterns); XBT_PUBLIC(long int) xbt_str_parse_int(const char* str, const char* error_msg); XBT_PUBLIC(double) xbt_str_parse_double(const char* str, const char* error_msg); diff --git a/include/xbt/strbuff.h b/include/xbt/strbuff.h index b81e4354df..41bb52e435 100644 --- a/include/xbt/strbuff.h +++ b/include/xbt/strbuff.h @@ -45,7 +45,6 @@ XBT_PUBLIC(void) xbt_strbuff_append(xbt_strbuff_t b, const char *toadd); XBT_PUBLIC(void) xbt_strbuff_printf(xbt_strbuff_t b, const char *fmt, ...); XBT_PUBLIC(void) xbt_strbuff_chomp(xbt_strbuff_t b); XBT_PUBLIC(void) xbt_strbuff_trim(xbt_strbuff_t b); -XBT_PUBLIC(void) xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns); /** @} */ SG_END_DECL() diff --git a/src/xbt/xbt_str.cpp b/src/xbt/xbt_str.cpp index 90eb701824..68ee3604b5 100644 --- a/src/xbt/xbt_str.cpp +++ b/src/xbt/xbt_str.cpp @@ -141,31 +141,6 @@ void xbt_str_subst(char *str, char from, char to, int occurence) } } -/** @brief Replaces a set of variables by their values - * - * @param str The input of the replacement process - * @param patterns The changes to apply - * @return The string modified - * - * Both '$toto' and '${toto}' are valid (and the two writing are equivalent). - * - * If the variable name contains spaces, use the brace version (ie, ${toto tutu}) - * - * You can provide a default value to use if the variable is not set in the dict by using '${var:=default}' or - * '${var:-default}'. These two forms are equivalent, even if they shouldn't to respect the shell standard (:= form - * should set the value in the dict, but does not) (BUG). - */ -char *xbt_str_varsubst(const char *str, xbt_dict_t patterns) -{ - xbt_strbuff_t buff = xbt_strbuff_new_from(str); - char *res; - xbt_strbuff_varsubst(buff, patterns); - res = buff->data; - xbt_strbuff_free_container(buff); - return res; -} - - /** @brief Splits a string into a dynar of strings * * @param s: the string to split diff --git a/src/xbt/xbt_strbuff.c b/src/xbt/xbt_strbuff.c index 4369aca483..e87d383c70 100644 --- a/src/xbt/xbt_strbuff.c +++ b/src/xbt/xbt_strbuff.c @@ -87,291 +87,3 @@ void xbt_strbuff_printf(xbt_strbuff_t b, const char *fmt, ...) xbt_free(data); va_end(ap); } - -/** @brief Replaces a set of variables by their values - * - * @param b buffer to modify - * @param patterns variables to substitute in the buffer - * - * Both '$toto' and '${toto}' are valid (and the two writing are equivalent). - * - * If the variable name contains spaces, use the brace version (ie, ${toto tutu}) - * - * You can provide a default value to use if the variable is not set in the dict by using '${var:=default}' or - * '${var:-default}'. These two forms are equivalent, even if they shouldn't to respect the shell standard (:= form - * should set the value in the dict, but does not) (BUG). - */ -void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) -{ - char *end; /* pointers around the parsed chunk */ - int in_simple_quote = 0; - int in_double_quote = 0; - int done = 0; - - if (b->data[0] == '\0') - return; - end = b->data; - - while (!done) { - switch (*end) { - case '\\': - /* Protected char; pass the protection */ - end++; - xbt_assert(*end != '\0', "String ends with \\"); - break; - case '\'': - if (!in_double_quote) { - /* simple quote not protected by double ones, note it */ - in_simple_quote = !in_simple_quote; - } - break; - case '"': - if (!in_simple_quote) { - /* double quote protected by simple ones, note it */ - in_double_quote = !in_double_quote; - } - break; - case '$': - if (!in_simple_quote) { - /* Go for the substitution. First search the variable name */ - char *beg_var; - char *end_var; /* variable name boundary */ - char *beg_subst = end; - char *end_subst = NULL; /* where value should be written to */ - char *value; - char *default_value = NULL; - int val_len; - - if (*(++end) == '{') { - /* the variable name is enclosed in braces. */ - beg_var = end + 1; - /* Search name's end */ - end_var = beg_var; - while (*end_var != '\0' && *end_var != '}') { - /* TODO: we do not respect the standard for ":=", we should set this value in the dict */ - if (*end_var == ':' - && ((*(end_var + 1) == '=') || (*(end_var + 1) == '-'))) { - /* damn, we have a default value */ - char *p = end_var + 1; - while (*p != '\0' && *p != '}') - p++; - xbt_assert (*p != '\0', "Variable default value not terminated ('}' missing)"); - - default_value = xbt_malloc(p - end_var - 1); - memcpy(default_value, end_var + 2, p - end_var - 2); - default_value[p - end_var - 2] = '\0'; - - end_subst = p + 1; /* eat '}' */ - break; - } - end_var++; - } - xbt_assert(*end_var != '\0', "Variable name not terminated ('}' missing)"); - - if (!end_subst) /* already set if there's a default value */ - end_subst = end_var + 1; /* also kill the } in the name */ - - xbt_assert(end_var != beg_var, "Variable name empty (${} is not valid)"); - } else { - /* name given directly */ - beg_var = end; - end_var = beg_var; - while (*end_var != '\0' && *end_var != ' ' && *end_var != '\t' && *end_var != '\n') - end_var++; - end_subst = end_var; - xbt_assert (end_var != beg_var, "Variable name empty ($ is not valid)"); - } - - /* ok, we now have the variable name. Search the dictionary for the substituted value */ - value = xbt_dict_get_or_null_ext(patterns, beg_var, end_var - beg_var); - - if (value) - value = xbt_strdup(value); - else if (default_value) - value = xbt_strdup(default_value); - else - value = xbt_strdup(""); - - /* En route for the actual substitution */ - val_len = strlen(value); - if (val_len <= end_subst - beg_subst) { - /* enough room to do the substitute in place */ - memmove(beg_subst, value, val_len); /* substitute */ - /* move the end of the string closer */ - memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); -// XBT_DEBUG("String is now: '%s'",b->data); - end = beg_subst + val_len; /* update the currently explored char in the overall loop */ -// XBT_DEBUG("end of substituted section is now '%s'",end); - b->used -= end_subst - beg_subst - val_len; /* update string buffer used size */ -// XBT_DEBUG("Used:%d end:%d ending char:%d",b->used,end-b->data,*end); - } else { - /* we have to extend the data area */ - int tooshort = val_len - (end_subst - beg_subst) + 1 /* don't forget \0 */ ; - int newused = b->used + tooshort; - end += tooshort; /* update the pointer of the overall loop */ -// XBT_DEBUG("Too short (by %d chars; %d chars left in area)",val_len-(end_subst-beg_subst),b->size - b->used); - if (newused > b->size) { - /* We have to realloc the data area before (because b->size is too small). - * We have to update our pointers, too */ - char *newdata = xbt_realloc(b->data, b->used + MAX(minimal_increment, tooshort)); - int offset = newdata - b->data; - b->data = newdata; - b->size = b->used + MAX(minimal_increment, tooshort); - end += offset; - beg_subst += offset; - end_subst += offset; - } - /* move the end of the string a bit further */ - memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); - memmove(beg_subst, value, val_len); /* substitute */ - b->used = newused; -// XBT_DEBUG("String is now: %s",b->data); - } - free(value); - free(default_value); - end--; /* compensate the next end++ */ - } - break; - case '\0': - done = 1; - break; - } - end++; - } -} - -#ifdef SIMGRID_TEST -#include "xbt/strbuff.h" - -/* buffstr have 512 chars by default. Adding 1000 chars like this will force a resize, allowing us to test that - * b->used and b->size are consistent */ -#define force_resize \ - "1.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "2.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "3.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "4.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "5.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "6.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "7.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "8.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "9.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \ - "0.........1.........2.........3.........4.........5.........6.........7.........8.........9........." - -static void mytest(const char *input, const char *patterns, const char *expected) -{ - xbt_dynar_t dyn_patterns; /* splited string */ - xbt_dict_t p; /* patterns */ - unsigned int cpt; - char *str; /*foreach */ - xbt_strbuff_t sb; /* what we test */ - - p = xbt_dict_new_homogeneous(free); - dyn_patterns = xbt_str_split(patterns, " "); - xbt_dynar_foreach(dyn_patterns, cpt, str) { - xbt_dynar_t keyvals = xbt_str_split(str, "="); - char *key = xbt_dynar_get_as(keyvals, 0, char *); - char *val = xbt_dynar_get_as(keyvals, 1, char *); - xbt_str_subst(key, '_', ' ', 0); // to put space in names without breaking the enclosing dynar_foreach - xbt_dict_set(p, key, xbt_strdup(val), NULL); - xbt_dynar_free(&keyvals); - } - xbt_dynar_free(&dyn_patterns); - sb = xbt_strbuff_new(); - xbt_strbuff_append(sb, input); - xbt_strbuff_varsubst(sb, p); - xbt_dict_free(&p); - xbt_test_assert(!strcmp(sb->data, expected), "Input (%s) with patterns (%s) leads to (%s) instead of (%s)", - input, patterns, sb->data, expected); - xbt_strbuff_free(sb); -} - -XBT_TEST_SUITE("xbt_strbuff", "String Buffers"); -XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute, "test the function xbt_strbuff_substitute") -{ - xbt_test_add("Empty"); - mytest("", "", ""); - - xbt_test_add("Value shorter, no braces, only variable"); - mytest("$tutu", "tutu=t", "t"); - xbt_test_add("Value shorter, braces, only variable"); - mytest("${tutu}", "tutu=t", "t"); - xbt_test_add("Value shorter, no braces, data after"); - mytest("$tutu toto", "tutu=t", "t toto"); - xbt_test_add("Value shorter, braces, data after"); - mytest("${tutu} toto", "tutu=t", "t toto"); - xbt_test_add("Value shorter, no braces, data before"); - mytest("toto $tutu", "tutu=t", "toto t"); - xbt_test_add("Value shorter, braces, data before"); - mytest("toto ${tutu}", "tutu=t", "toto t"); - xbt_test_add("Value shorter, no braces, data before and after"); - mytest("toto $tutu tata", "tutu=t", "toto t tata"); - xbt_test_add("Value shorter, braces, data before and after"); - mytest("toto ${tutu} tata", "tutu=t", "toto t tata"); - - xbt_test_add("Value as long, no braces, only variable"); - mytest("$tutu", "tutu=12345", "12345"); - xbt_test_add("Value as long, braces, only variable"); - mytest("${tutu}", "tutu=1234567", "1234567"); - xbt_test_add("Value as long, no braces, data after"); - mytest("$tutu toto", "tutu=12345", "12345 toto"); - xbt_test_add("Value as long, braces, data after"); - mytest("${tutu} toto", "tutu=1234567", "1234567 toto"); - xbt_test_add("Value as long, no braces, data before"); - mytest("toto $tutu", "tutu=12345", "toto 12345"); - xbt_test_add("Value as long, braces, data before"); - mytest("toto ${tutu}", "tutu=1234567", "toto 1234567"); - xbt_test_add("Value as long, no braces, data before and after"); - mytest("toto $tutu tata", "tutu=12345", "toto 12345 tata"); - xbt_test_add("Value as long, braces, data before and after"); - mytest("toto ${tutu} tata", "tutu=1234567", "toto 1234567 tata"); - - xbt_test_add("Value longer, no braces, only variable"); - mytest("$t", "t=tututu", "tututu"); - xbt_test_add("Value longer, braces, only variable"); - mytest("${t}", "t=tututu", "tututu"); - xbt_test_add("Value longer, no braces, data after"); - mytest("$t toto", "t=tututu", "tututu toto"); - xbt_test_add("Value longer, braces, data after"); - mytest("${t} toto", "t=tututu", "tututu toto"); - xbt_test_add("Value longer, no braces, data before"); - mytest("toto $t", "t=tututu", "toto tututu"); - xbt_test_add("Value longer, braces, data before"); - mytest("toto ${t}", "t=tututu", "toto tututu"); - xbt_test_add("Value longer, no braces, data before and after"); - mytest("toto $t tata", "t=tututu", "toto tututu tata"); - xbt_test_add("Value longer, braces, data before and after"); - mytest("toto ${t} tata", "t=tututu", "toto tututu tata"); - - xbt_test_add("Value much longer, no braces, only variable"); - mytest("$t", "t=" force_resize, force_resize); - xbt_test_add("Value much longer, no braces, data after"); - mytest("$t toto", "t=" force_resize, force_resize " toto"); - xbt_test_add("Value much longer, braces, data after"); - mytest("${t} toto", "t=" force_resize, force_resize " toto"); - xbt_test_add("Value much longer, no braces, data before"); - mytest("toto $t", "t=" force_resize, "toto " force_resize); - xbt_test_add("Value much longer, braces, data before"); - mytest("toto ${t}", "t=" force_resize, "toto " force_resize); - xbt_test_add("Value much longer, no braces, data before and after"); - mytest("toto $t tata", "t=" force_resize, "toto " force_resize " tata"); - xbt_test_add("Value much longer, braces, data before and after"); - mytest("toto ${t} tata", "t=" force_resize, "toto " force_resize " tata"); - - xbt_test_add("Escaped $"); - mytest("\\$tutu", "tutu=t", "\\$tutu"); - xbt_test_add("Space in var name (with braces)"); - mytest("${tu ti}", "tu_ti=t", "t"); - - xbt_test_add("Two variables"); - mytest("$toto $tutu", "toto=1 tutu=2", "1 2"); - - // Commented: I'm too lazy to do a memmove in var name to remove the backslash after use. - // Users should use braces. - // xbt_test_add("Escaped space in var name", "$tu\\ ti", "tu_ti=t", "t"); - - xbt_test_add("Default value"); - mytest("${t:-toto}", "", "toto"); - xbt_test_add("Useless default value (variable already defined)"); - mytest("${t:-toto}", "t=TRUC", "TRUC"); -} -#endif /* SIMGRID_TEST */ diff --git a/tools/cmake/UnitTesting.cmake b/tools/cmake/UnitTesting.cmake index 9ba75d7f6c..16701f711a 100644 --- a/tools/cmake/UnitTesting.cmake +++ b/tools/cmake/UnitTesting.cmake @@ -11,7 +11,6 @@ set(FILES_CONTAINING_UNITTESTS src/xbt/dict.cpp src/xbt/swag.c src/xbt/xbt_str.cpp - src/xbt/xbt_strbuff.c src/xbt/config.cpp )