1 /* xbt_str.cpp - various helping functions to deal with strings */
3 /* Copyright (c) 2007-2014. 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 "src/internal_config.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/str.h" /* headers of these functions */
13 #include "xbt/strbuff.h"
15 /** @brief Strip whitespace (or other characters) from the end of a string.
17 * Strips the whitespaces from the end of s.
18 * By default (when char_list=NULL), these characters get stripped:
20 * - " " (ASCII 32 (0x20)) space.
21 * - "\t" (ASCII 9 (0x09)) tab.
22 * - "\n" (ASCII 10 (0x0A)) line feed.
23 * - "\r" (ASCII 13 (0x0D)) carriage return.
24 * - "\0" (ASCII 0 (0x00)) NULL.
25 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
27 * @param s The string to strip. Modified in place.
28 * @param char_list A string which contains the characters you want to strip.
30 void xbt_str_rtrim(char *s, const char *char_list)
33 const char *__char_list = " \t\n\r\x0B";
34 char white_char[256] = { 1, 0 };
40 while (*__char_list) {
41 white_char[(unsigned char) *__char_list++] = 1;
45 white_char[(unsigned char) *char_list++] = 1;
52 while ((cur >= s) && white_char[(unsigned char) *cur])
58 /** @brief Strip whitespace (or other characters) from the beginning of a string.
60 * Strips the whitespaces from the begining of s.
61 * By default (when char_list=NULL), these characters get stripped:
63 * - " " (ASCII 32 (0x20)) space.
64 * - "\t" (ASCII 9 (0x09)) tab.
65 * - "\n" (ASCII 10 (0x0A)) line feed.
66 * - "\r" (ASCII 13 (0x0D)) carriage return.
67 * - "\0" (ASCII 0 (0x00)) NULL.
68 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
70 * @param s The string to strip. Modified in place.
71 * @param char_list A string which contains the characters you want to strip.
73 void xbt_str_ltrim(char *s, const char *char_list)
76 const char *__char_list = " \t\n\r\x0B";
77 char white_char[256] = { 1, 0 };
83 while (*__char_list) {
84 white_char[(unsigned char) *__char_list++] = 1;
88 white_char[(unsigned char) *char_list++] = 1;
92 while (*cur && white_char[(unsigned char) *cur])
95 memmove(s, cur, strlen(cur) + 1);
98 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
100 * Strips the whitespaces from both the beginning and the end of s.
101 * By default (when char_list=NULL), these characters get stripped:
103 * - " " (ASCII 32 (0x20)) space.
104 * - "\t" (ASCII 9 (0x09)) tab.
105 * - "\n" (ASCII 10 (0x0A)) line feed.
106 * - "\r" (ASCII 13 (0x0D)) carriage return.
107 * - "\0" (ASCII 0 (0x00)) NULL.
108 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
110 * @param s The string to strip.
111 * @param char_list A string which contains the characters you want to strip.
113 void xbt_str_trim(char *s, const char *char_list)
118 xbt_str_rtrim(s, char_list);
119 xbt_str_ltrim(s, char_list);
122 /** @brief Substitutes a char for another in a string
124 * @param str the string to modify
125 * @param from char to search
126 * @param to char to put instead
127 * @param occurence number of changes to do (=0 means all)
129 void xbt_str_subst(char *str, char from, char to, int occurence)
143 /** @brief Replaces a set of variables by their values
145 * @param str The input of the replacement process
146 * @param patterns The changes to apply
147 * @return The string modified
149 * Both '$toto' and '${toto}' are valid (and the two writing are equivalent).
151 * If the variable name contains spaces, use the brace version (ie, ${toto tutu})
153 * You can provide a default value to use if the variable is not set in the dict by using '${var:=default}' or
154 * '${var:-default}'. These two forms are equivalent, even if they shouldn't to respect the shell standard (:= form
155 * should set the value in the dict, but does not) (BUG).
157 char *xbt_str_varsubst(const char *str, xbt_dict_t patterns)
159 xbt_strbuff_t buff = xbt_strbuff_new_from(str);
161 xbt_strbuff_varsubst(buff, patterns);
163 xbt_strbuff_free_container(buff);
168 /** @brief Splits a string into a dynar of strings
170 * @param s: the string to split
171 * @param sep: a string of all chars to consider as separator.
173 * By default (with sep=NULL), these characters are used as separator:
175 * - " " (ASCII 32 (0x20)) space.
176 * - "\t" (ASCII 9 (0x09)) tab.
177 * - "\n" (ASCII 10 (0x0A)) line feed.
178 * - "\r" (ASCII 13 (0x0D)) carriage return.
179 * - "\0" (ASCII 0 (0x00)) NULL.
180 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
182 xbt_dynar_t xbt_str_split(const char *s, const char *sep)
184 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
187 const char *sep_dflt = " \t\n\r\x0B";
188 char is_sep[256] = { 1, 0 };
190 /* check what are the separators */
191 memset(is_sep, 0, sizeof(is_sep));
194 is_sep[(unsigned char) *sep_dflt++] = 1;
197 is_sep[(unsigned char) *sep++] = 1;
199 is_sep[0] = 1; /* End of string is also separator */
210 while (!is_sep[(unsigned char) *q]) {
216 topush = (char*) xbt_malloc(q - p + 1);
217 memcpy(topush, p, q - p);
218 topush[q - p] = '\0';
219 xbt_dynar_push(res, &topush);
227 * \brief This functions splits a string after using another string as separator
228 * For example A!!B!!C splitted after !! will return the dynar {A,B,C}
229 * \return An array of dynars containing the string tokens
231 xbt_dynar_t xbt_str_split_str(const char *s, const char *sep)
233 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
242 if (sep[0] == '\0') {
244 xbt_dynar_push(res, &s);
251 //get the start of the first occurence of the substring
253 //if substring was not found add the entire string
256 to_push = (char*) xbt_malloc(v + 1);
257 memcpy(to_push, p, v);
259 xbt_dynar_push(res, &to_push);
263 to_push = (char*) xbt_malloc(q - p + 1);
264 memcpy(to_push, p, q - p);
265 //add string terminator
266 to_push[q - p] = '\0';
267 xbt_dynar_push(res, &to_push);
274 /** @brief Just like @ref xbt_str_split_quoted (Splits a string into a dynar of strings), but without memory allocation
276 * The string passed as argument must be writable (not const)
277 * The elements of the dynar are just parts of the string passed as argument.
278 * So if you don't store that argument elsewhere, you should free it in addition to freeing the dynar. This can be done
279 * by simply freeing the first argument of the dynar:
280 * free(xbt_dynar_get_ptr(dynar,0));
282 * Actually this function puts a bunch of \0 in the memory area you passed as argument to separate the elements, and
283 * pushes the address of each chunk in the resulting dynar. Yes, that's uneven. Yes, that's gory. But that's efficient.
285 xbt_dynar_t xbt_str_split_quoted_in_place(char *s) {
286 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), NULL);
287 char *beg, *end; /* pointers around the parsed chunk */
288 int in_simple_quote = 0, in_double_quote = 0;
290 int ctn = 0; /* Got something in this block */
297 /* do not trim leading spaces: caller responsibility to clean his cruft */
304 /* Protected char; move it closer */
305 memmove(end, end + 1, strlen(end));
307 THROWF(arg_error, 0, "String ends with \\");
308 end++; /* Pass the protected char */
312 if (!in_double_quote) {
313 in_simple_quote = !in_simple_quote;
314 memmove(end, end + 1, strlen(end));
316 /* simple quote protected by double ones */
322 if (!in_simple_quote) {
323 in_double_quote = !in_double_quote;
324 memmove(end, end + 1, strlen(end));
326 /* double quote protected by simple ones */
334 if (*end == '\0' && (in_simple_quote || in_double_quote)) {
335 THROWF(arg_error, 0, "End of string found while searching for %c in %s", (in_simple_quote ? '\'' : '"'), s);
337 if (in_simple_quote || in_double_quote) {
345 /* Found a separator. Push the string if contains something */
346 xbt_dynar_push(res, &beg);
354 /* trim within the string, manually to speed things up */
368 /** @brief Splits a string into a dynar of strings, taking quotes into account
370 * It basically does the same argument separation than the shell, where white spaces can be escaped and where arguments
371 * are never split within a quote group.
372 * Several subsequent spaces are ignored (unless within quotes, of course).
373 * You may want to trim the input string, if you want to avoid empty entries
375 xbt_dynar_t xbt_str_split_quoted(const char *s)
377 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
379 char *str_to_free; /* we have to copy the string before, to handle backslashes */
385 str_to_free = xbt_strdup(s);
387 parsed = xbt_str_split_quoted_in_place(str_to_free);
388 xbt_dynar_foreach(parsed,cursor,p) {
389 char *q=xbt_strdup(p);
390 xbt_dynar_push(res,&q);
393 xbt_dynar_shrink(res, 0);
394 xbt_dynar_free(&parsed);
398 /** @brief Join a set of strings as a single string */
399 char *xbt_str_join(xbt_dynar_t dyn, const char *sep)
401 int len = 1, dyn_len = xbt_dynar_length(dyn);
407 return xbt_strdup("");
409 /* compute the length */
410 xbt_dynar_foreach(dyn, cpt, cursor) {
411 len += strlen(cursor);
413 len += strlen(sep) * dyn_len;
415 res = (char*) xbt_malloc(len);
417 xbt_dynar_foreach(dyn, cpt, cursor) {
418 if ((int) cpt < dyn_len - 1)
419 p += snprintf(p,len, "%s%s", cursor, sep);
421 p += snprintf(p,len, "%s", cursor);
426 /** @brief Join a set of strings as a single string
428 * The parameter must be a NULL-terminated array of chars,
429 * just like xbt_dynar_to_array() produces
431 char *xbt_str_join_array(const char *const *strs, const char *sep)
434 int amount_strings=0;
438 if ((!strs) || (!strs[0]))
439 return xbt_strdup("");
441 /* compute the length before malloc */
442 for (i=0;strs[i];i++) {
443 len += strlen(strs[i]);
446 len += strlen(sep) * amount_strings;
449 q = res = (char*) xbt_malloc(len);
450 for (i=0;strs[i];i++) {
451 if (i!=0) { // not first loop
452 q += snprintf(q,len, "%s%s", sep, strs[i]);
454 q += snprintf(q,len, "%s",strs[i]);
460 /** @brief creates a new string containing what can be read on a fd */
461 char *xbt_str_from_file(FILE * file)
463 xbt_strbuff_t buff = xbt_strbuff_new();
466 memset(bread, 0, 1024);
468 while (!feof(file)) {
469 int got = fread(bread, 1, 1023, file);
471 xbt_strbuff_append(buff, bread);
475 xbt_strbuff_free_container(buff);
479 /** @brief Parse an integer out of a string, or raise an error
481 * The @a str is passed as argument to your @a error_msg, as follows:
482 * @verbatim THROWF(arg_error, 0, error_msg, str); @endverbatim
484 long int xbt_str_parse_int(const char* str, const char* error_msg)
487 if (str == NULL || str[0] == '\0')
488 THROWF(arg_error, 0, error_msg, str);
490 long int res = strtol(str, &endptr, 10);
491 if (endptr[0] != '\0')
492 THROWF(arg_error, 0, error_msg, str);
497 /** @brief Parse a double out of a string, or raise an error
499 * The @a str is passed as argument to your @a error_msg, as follows:
500 * @verbatim THROWF(arg_error, 0, error_msg, str); @endverbatim
502 double xbt_str_parse_double(const char* str, const char* error_msg)
505 if (str == NULL || str[0] == '\0')
506 THROWF(arg_error, 0, error_msg, str);
508 double res = strtod(str, &endptr);
509 if (endptr[0] != '\0')
510 THROWF(arg_error, 0, error_msg, str);
518 XBT_TEST_SUITE("xbt_str", "String Handling");
520 #define mytest(name, input, expected) \
521 xbt_test_add(name); \
522 d=xbt_str_split_quoted(input); \
523 s=xbt_str_join(d,"XXX"); \
524 xbt_test_assert(!strcmp(s,expected),\
525 "Input (%s) leads to (%s) instead of (%s)", \
529 XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted")
534 mytest("Empty", "", "");
535 mytest("Basic test", "toto tutu", "totoXXXtutu");
536 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u", "totoXXXtutu");
537 mytest("Protected space", "toto\\ tutu", "toto tutu");
538 mytest("Several spaces", "toto tutu", "totoXXXtutu");
539 mytest("LTriming", " toto tatu", "totoXXXtatu");
540 mytest("Triming", " toto tutu ", "totoXXXtutu");
541 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
542 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
543 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
544 mytest("Backslashed quotes", "\\'toto tutu\\' tata", "'totoXXXtutu'XXXtata");
545 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata", "toto 'tutuXXXtata");
548 #define mytest_str(name, input, separator, expected) \
549 xbt_test_add(name); \
550 d=xbt_str_split_str(input, separator); \
551 s=xbt_str_join(d,"XXX"); \
552 xbt_test_assert(!strcmp(s,expected),\
553 "Input (%s) leads to (%s) instead of (%s)", \
558 XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_split_str")
563 mytest_str("Empty string and separator", "", "", "");
564 mytest_str("Empty string", "", "##", "");
565 mytest_str("Empty separator", "toto", "", "toto");
566 mytest_str("String with no separator in it", "toto", "##", "toto");
567 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
570 #define test_parse_error(function, name, variable, str) \
572 xbt_test_add(name); \
574 variable = function(str, "Parse error"); \
575 xbt_test_fail("The test '%s' did not detect the problem",name ); \
576 } catch(xbt_ex& e) { \
577 if (e.category != arg_error) { \
578 xbt_test_exception(e); \
582 #define test_parse_ok(function, name, variable, str, value) \
584 xbt_test_add(name); \
586 variable = function(str, "Parse error"); \
587 } catch(xbt_ex& e) { \
588 xbt_test_exception(e); \
590 xbt_test_assert(variable == value, "Fail to parse '%s'", str); \
593 XBT_TEST_UNIT("xbt_str_parse", test_parse, "Test the parsing functions")
596 test_parse_ok(xbt_str_parse_int, "Parse int", rint, "42", 42);
597 test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", rint, "0", 0);
598 test_parse_ok(xbt_str_parse_int, "Parse -1 as an int", rint, "-1", -1);
600 test_parse_error(xbt_str_parse_int, "Parse int + noise", rint, "342 cruft");
601 test_parse_error(xbt_str_parse_int, "Parse NULL as an int", rint, NULL);
602 test_parse_error(xbt_str_parse_int, "Parse '' as an int", rint, "");
603 test_parse_error(xbt_str_parse_int, "Parse cruft as an int", rint, "cruft");
605 double rdouble = -9999;
606 test_parse_ok(xbt_str_parse_double, "Parse 42 as a double", rdouble, "42", 42);
607 test_parse_ok(xbt_str_parse_double, "Parse 42.5 as a double", rdouble, "42.5", 42.5);
608 test_parse_ok(xbt_str_parse_double, "Parse 0 as a double", rdouble, "0", 0);
609 test_parse_ok(xbt_str_parse_double, "Parse -1 as a double", rdouble, "-1", -1);
611 test_parse_error(xbt_str_parse_double, "Parse double + noise", rdouble, "342 cruft");
612 test_parse_error(xbt_str_parse_double, "Parse NULL as a double", rdouble, NULL);
613 test_parse_error(xbt_str_parse_double, "Parse '' as a double", rdouble, "");
614 test_parse_error(xbt_str_parse_double, "Parse cruft as a double", rdouble, "cruft");
616 #endif /* SIMGRID_TEST */