1 /* strbuff -- string buffers */
3 /* Copyright (c) 2007-2015. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/strbuff.h"
12 #define minimal_increment 512
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(strbuff, xbt, "String buffers");
16 /** @brief Remove any content from the buffer */
17 inline void xbt_strbuff_clear(xbt_strbuff_t b)
23 /** @brief Constructor */
24 xbt_strbuff_t xbt_strbuff_new(void)
26 xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t));
27 res->data = xbt_malloc(512);
29 xbt_strbuff_clear(res);
33 /** @brief creates a new string buffer containing the provided string
35 * Beware, the ctn is copied, you want to free it afterward, anyhow
37 inline xbt_strbuff_t xbt_strbuff_new_from(const char *ctn)
39 xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t));
40 res->data = xbt_strdup(ctn);
41 res->size = strlen(ctn);
42 res->used = res->size;
46 /** @brief frees only the container without touching to the contained string */
47 inline void xbt_strbuff_free_container(xbt_strbuff_t b)
52 /** @brief frees the buffer and its content */
53 inline void xbt_strbuff_free(xbt_strbuff_t b)
61 /** @brief Adds some content at the end of the buffer */
62 void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
67 xbt_assert(b, "Asked to append stuff to NULL buffer");
69 addlen = strlen(toadd);
70 needed_space = b->used + addlen + 1;
72 if (needed_space > b->size) {
73 b->size = MAX(minimal_increment + b->used, needed_space);
74 b->data = xbt_realloc(b->data, b->size);
76 strncpy(b->data + b->used, toadd, b->size-b->used);
80 /** @brief format some content and push it at the end of the buffer */
81 void xbt_strbuff_printf(xbt_strbuff_t b, const char *fmt, ...)
85 char *data = bvprintf(fmt, ap);
86 xbt_strbuff_append(b, data);
91 /** @brief Replaces a set of variables by their values
93 * @param b buffer to modify
94 * @param patterns variables to substitute in the buffer
96 * Both '$toto' and '${toto}' are valid (and the two writing are equivalent).
98 * If the variable name contains spaces, use the brace version (ie, ${toto tutu})
100 * You can provide a default value to use if the variable is not set in the dict by using '${var:=default}' or
101 * '${var:-default}'. These two forms are equivalent, even if they shouldn't to respect the shell standard (:= form
102 * should set the value in the dict, but does not) (BUG).
104 void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
106 char *end; /* pointers around the parsed chunk */
107 int in_simple_quote = 0;
108 int in_double_quote = 0;
111 if (b->data[0] == '\0')
118 /* Protected char; pass the protection */
120 xbt_assert(*end != '\0', "String ends with \\");
123 if (!in_double_quote) {
124 /* simple quote not protected by double ones, note it */
125 in_simple_quote = !in_simple_quote;
129 if (!in_simple_quote) {
130 /* double quote protected by simple ones, note it */
131 in_double_quote = !in_double_quote;
135 if (!in_simple_quote) {
136 /* Go for the substitution. First search the variable name */
138 char *end_var; /* variable name boundary */
139 char *beg_subst = end;
140 char *end_subst = NULL; /* where value should be written to */
142 char *default_value = NULL;
145 if (*(++end) == '{') {
146 /* the variable name is enclosed in braces. */
148 /* Search name's end */
150 while (*end_var != '\0' && *end_var != '}') {
151 /* TODO: we do not respect the standard for ":=", we should set this value in the dict */
153 && ((*(end_var + 1) == '=') || (*(end_var + 1) == '-'))) {
154 /* damn, we have a default value */
155 char *p = end_var + 1;
156 while (*p != '\0' && *p != '}')
158 xbt_assert (*p != '\0', "Variable default value not terminated ('}' missing)");
160 default_value = xbt_malloc(p - end_var - 1);
161 memcpy(default_value, end_var + 2, p - end_var - 2);
162 default_value[p - end_var - 2] = '\0';
164 end_subst = p + 1; /* eat '}' */
169 xbt_assert(*end_var != '\0', "Variable name not terminated ('}' missing)");
171 if (!end_subst) /* already set if there's a default value */
172 end_subst = end_var + 1; /* also kill the } in the name */
174 xbt_assert(end_var != beg_var, "Variable name empty (${} is not valid)");
176 /* name given directly */
179 while (*end_var != '\0' && *end_var != ' ' && *end_var != '\t' && *end_var != '\n')
182 xbt_assert (end_var != beg_var, "Variable name empty ($ is not valid)");
185 /* ok, we now have the variable name. Search the dictionary for the substituted value */
186 value = xbt_dict_get_or_null_ext(patterns, beg_var, end_var - beg_var);
189 value = xbt_strdup(value);
190 else if (default_value)
191 value = xbt_strdup(default_value);
193 value = xbt_strdup("");
195 /* En route for the actual substitution */
196 val_len = strlen(value);
197 if (val_len <= end_subst - beg_subst) {
198 /* enough room to do the substitute in place */
199 memmove(beg_subst, value, val_len); /* substitute */
200 /* move the end of the string closer */
201 memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1);
202 // XBT_DEBUG("String is now: '%s'",b->data);
203 end = beg_subst + val_len; /* update the currently explored char in the overall loop */
204 // XBT_DEBUG("end of substituted section is now '%s'",end);
205 b->used -= end_subst - beg_subst - val_len; /* update string buffer used size */
206 // XBT_DEBUG("Used:%d end:%d ending char:%d",b->used,end-b->data,*end);
208 /* we have to extend the data area */
209 int tooshort = val_len - (end_subst - beg_subst) + 1 /* don't forget \0 */ ;
210 int newused = b->used + tooshort;
211 end += tooshort; /* update the pointer of the overall loop */
212 // XBT_DEBUG("Too short (by %d chars; %d chars left in area)",val_len-(end_subst-beg_subst),b->size - b->used);
213 if (newused > b->size) {
214 /* We have to realloc the data area before (because b->size is too small).
215 * We have to update our pointers, too */
216 char *newdata = xbt_realloc(b->data, b->used + MAX(minimal_increment, tooshort));
217 int offset = newdata - b->data;
219 b->size = b->used + MAX(minimal_increment, tooshort);
224 /* move the end of the string a bit further */
225 memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1);
226 memmove(beg_subst, value, val_len); /* substitute */
228 // XBT_DEBUG("String is now: %s",b->data);
232 end--; /* compensate the next end++ */
244 #include "xbt/strbuff.h"
246 /* buffstr have 512 chars by default. Adding 1000 chars like this will force a resize, allowing us to test that
247 * b->used and b->size are consistent */
248 #define force_resize \
249 "1.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
250 "2.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
251 "3.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
252 "4.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
253 "5.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
254 "6.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
255 "7.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
256 "8.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
257 "9.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
258 "0.........1.........2.........3.........4.........5.........6.........7.........8.........9........."
260 static void mytest(const char *input, const char *patterns, const char *expected)
262 xbt_dynar_t dyn_patterns; /* splited string */
263 xbt_dict_t p; /* patterns */
265 char *str; /*foreach */
266 xbt_strbuff_t sb; /* what we test */
268 p = xbt_dict_new_homogeneous(free);
269 dyn_patterns = xbt_str_split(patterns, " ");
270 xbt_dynar_foreach(dyn_patterns, cpt, str) {
271 xbt_dynar_t keyvals = xbt_str_split(str, "=");
272 char *key = xbt_dynar_get_as(keyvals, 0, char *);
273 char *val = xbt_dynar_get_as(keyvals, 1, char *);
274 xbt_str_subst(key, '_', ' ', 0); // to put space in names without breaking the enclosing dynar_foreach
275 xbt_dict_set(p, key, xbt_strdup(val), NULL);
276 xbt_dynar_free(&keyvals);
278 xbt_dynar_free(&dyn_patterns);
279 sb = xbt_strbuff_new();
280 xbt_strbuff_append(sb, input);
281 xbt_strbuff_varsubst(sb, p);
283 xbt_test_assert(!strcmp(sb->data, expected), "Input (%s) with patterns (%s) leads to (%s) instead of (%s)",
284 input, patterns, sb->data, expected);
285 xbt_strbuff_free(sb);
288 XBT_TEST_SUITE("xbt_strbuff", "String Buffers");
289 XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute, "test the function xbt_strbuff_substitute")
291 xbt_test_add("Empty");
294 xbt_test_add("Value shorter, no braces, only variable");
295 mytest("$tutu", "tutu=t", "t");
296 xbt_test_add("Value shorter, braces, only variable");
297 mytest("${tutu}", "tutu=t", "t");
298 xbt_test_add("Value shorter, no braces, data after");
299 mytest("$tutu toto", "tutu=t", "t toto");
300 xbt_test_add("Value shorter, braces, data after");
301 mytest("${tutu} toto", "tutu=t", "t toto");
302 xbt_test_add("Value shorter, no braces, data before");
303 mytest("toto $tutu", "tutu=t", "toto t");
304 xbt_test_add("Value shorter, braces, data before");
305 mytest("toto ${tutu}", "tutu=t", "toto t");
306 xbt_test_add("Value shorter, no braces, data before and after");
307 mytest("toto $tutu tata", "tutu=t", "toto t tata");
308 xbt_test_add("Value shorter, braces, data before and after");
309 mytest("toto ${tutu} tata", "tutu=t", "toto t tata");
311 xbt_test_add("Value as long, no braces, only variable");
312 mytest("$tutu", "tutu=12345", "12345");
313 xbt_test_add("Value as long, braces, only variable");
314 mytest("${tutu}", "tutu=1234567", "1234567");
315 xbt_test_add("Value as long, no braces, data after");
316 mytest("$tutu toto", "tutu=12345", "12345 toto");
317 xbt_test_add("Value as long, braces, data after");
318 mytest("${tutu} toto", "tutu=1234567", "1234567 toto");
319 xbt_test_add("Value as long, no braces, data before");
320 mytest("toto $tutu", "tutu=12345", "toto 12345");
321 xbt_test_add("Value as long, braces, data before");
322 mytest("toto ${tutu}", "tutu=1234567", "toto 1234567");
323 xbt_test_add("Value as long, no braces, data before and after");
324 mytest("toto $tutu tata", "tutu=12345", "toto 12345 tata");
325 xbt_test_add("Value as long, braces, data before and after");
326 mytest("toto ${tutu} tata", "tutu=1234567", "toto 1234567 tata");
328 xbt_test_add("Value longer, no braces, only variable");
329 mytest("$t", "t=tututu", "tututu");
330 xbt_test_add("Value longer, braces, only variable");
331 mytest("${t}", "t=tututu", "tututu");
332 xbt_test_add("Value longer, no braces, data after");
333 mytest("$t toto", "t=tututu", "tututu toto");
334 xbt_test_add("Value longer, braces, data after");
335 mytest("${t} toto", "t=tututu", "tututu toto");
336 xbt_test_add("Value longer, no braces, data before");
337 mytest("toto $t", "t=tututu", "toto tututu");
338 xbt_test_add("Value longer, braces, data before");
339 mytest("toto ${t}", "t=tututu", "toto tututu");
340 xbt_test_add("Value longer, no braces, data before and after");
341 mytest("toto $t tata", "t=tututu", "toto tututu tata");
342 xbt_test_add("Value longer, braces, data before and after");
343 mytest("toto ${t} tata", "t=tututu", "toto tututu tata");
345 xbt_test_add("Value much longer, no braces, only variable");
346 mytest("$t", "t=" force_resize, force_resize);
347 xbt_test_add("Value much longer, no braces, data after");
348 mytest("$t toto", "t=" force_resize, force_resize " toto");
349 xbt_test_add("Value much longer, braces, data after");
350 mytest("${t} toto", "t=" force_resize, force_resize " toto");
351 xbt_test_add("Value much longer, no braces, data before");
352 mytest("toto $t", "t=" force_resize, "toto " force_resize);
353 xbt_test_add("Value much longer, braces, data before");
354 mytest("toto ${t}", "t=" force_resize, "toto " force_resize);
355 xbt_test_add("Value much longer, no braces, data before and after");
356 mytest("toto $t tata", "t=" force_resize, "toto " force_resize " tata");
357 xbt_test_add("Value much longer, braces, data before and after");
358 mytest("toto ${t} tata", "t=" force_resize, "toto " force_resize " tata");
360 xbt_test_add("Escaped $");
361 mytest("\\$tutu", "tutu=t", "\\$tutu");
362 xbt_test_add("Space in var name (with braces)");
363 mytest("${tu ti}", "tu_ti=t", "t");
365 xbt_test_add("Two variables");
366 mytest("$toto $tutu", "toto=1 tutu=2", "1 2");
368 // Commented: I'm too lazy to do a memmove in var name to remove the backslash after use.
369 // Users should use braces.
370 // xbt_test_add("Escaped space in var name", "$tu\\ ti", "tu_ti=t", "t");
372 xbt_test_add("Default value");
373 mytest("${t:-toto}", "", "toto");
374 xbt_test_add("Useless default value (variable already defined)");
375 mytest("${t:-toto}", "t=TRUC", "TRUC");
377 #endif /* SIMGRID_TEST */