Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix the constness of xbt_strbuff_new_from(), it's too dangerous with previous prototype
authorMartin Quinson <martin.quinson@loria.fr>
Thu, 10 Nov 2011 16:20:04 +0000 (17:20 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Thu, 10 Nov 2011 17:44:53 +0000 (18:44 +0100)
include/xbt/str.h
include/xbt/strbuff.h
src/xbt/xbt_str.c
src/xbt/xbt_strbuff.c

index 225ef42..aa15a11 100644 (file)
@@ -41,7 +41,7 @@ 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(char *str, xbt_dict_t patterns);
+XBT_PUBLIC(char *) xbt_str_varsubst(const char *str, xbt_dict_t patterns);
 
 /* */
 XBT_PUBLIC(void) xbt_str_strip_spaces(char *);
index 1666655..eb1f59f 100644 (file)
@@ -28,7 +28,7 @@ typedef struct {
 
 XBT_PUBLIC(void) xbt_strbuff_empty(xbt_strbuff_t b);
 XBT_PUBLIC(xbt_strbuff_t) xbt_strbuff_new(void);
-XBT_PUBLIC(xbt_strbuff_t) xbt_strbuff_new_from(char *s);
+XBT_PUBLIC(xbt_strbuff_t) xbt_strbuff_new_from(const char *s);
 XBT_PUBLIC(void) xbt_strbuff_free(xbt_strbuff_t b);
 XBT_PUBLIC(void) xbt_strbuff_free_container(xbt_strbuff_t b);
 XBT_PUBLIC(void) xbt_strbuff_append(xbt_strbuff_t b, const char *toadd);
index 12b674a..6438d43 100644 (file)
@@ -191,14 +191,21 @@ void xbt_str_subst(char *str, char from, char to, int occurence)
 
 /** @brief Replaces a set of variables by their values
  *
- * @param str where to apply the change
- * @param patterns what to change
+ * @param str The input of the replacement process
+ * @param patterns The changes to apply
  * @return The string modified
  *
- * Check xbt_strbuff_varsubst() for more details, and remember that the string may be reallocated (moved) in the process.
+ * 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(char *str, xbt_dict_t patterns)
+char *xbt_str_varsubst(const char *str, xbt_dict_t patterns)
 {
   xbt_strbuff_t buff = xbt_strbuff_new_from(str);
   char *res;
index 16835fd..1db4432 100644 (file)
@@ -40,12 +40,12 @@ xbt_strbuff_t xbt_strbuff_new(void)
 
 /** @brief creates a new string buffer containing the provided string
  *
- * Beware, we store the ctn directly, not a copy of it
+ * Beware, the ctn is copied, you want to free it afterward, anyhow
  */
-XBT_INLINE xbt_strbuff_t xbt_strbuff_new_from(char *ctn)
+XBT_INLINE xbt_strbuff_t xbt_strbuff_new_from(const char *ctn)
 {
   xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t));
-  res->data = ctn;
+  res->data = xbt_strdup(ctn);
   res->used = res->size = strlen(ctn);
   return res;
 }