1 /* xbt_str.c - various helping functions to deal with strings */
3 /* Copyright (c) 2007, 2008, 2009, 2010. 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. */
11 #include "xbt/sysdep.h"
12 #include "xbt/str.h" /* headers of these functions */
13 #include "xbt/strbuff.h"
14 #include "xbt/matrix.h" /* for the diff */
16 /** @brief Strip whitespace (or other characters) from the end of a string.
18 * Strips the whitespaces from the end of s.
19 * By default (when char_list=NULL), these characters get stripped:
21 * - " " (ASCII 32 (0x20)) space.
22 * - "\t" (ASCII 9 (0x09)) tab.
23 * - "\n" (ASCII 10 (0x0A)) line feed.
24 * - "\r" (ASCII 13 (0x0D)) carriage return.
25 * - "\0" (ASCII 0 (0x00)) NULL.
26 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
28 * @param s The string to strip. Modified in place.
29 * @param char_list A string which contains the characters you want to strip.
32 void xbt_str_rtrim(char *s, const char *char_list)
35 const char *__char_list = " \t\n\r\x0B";
36 char white_char[256] = { 1, 0 };
42 while (*__char_list) {
43 white_char[(unsigned char) *__char_list++] = 1;
47 white_char[(unsigned char) *char_list++] = 1;
54 while ((cur >= s) && white_char[(unsigned char) *cur])
60 /** @brief Strip whitespace (or other characters) from the beginning of a string.
62 * Strips the whitespaces from the begining of s.
63 * By default (when char_list=NULL), these characters get stripped:
65 * - " " (ASCII 32 (0x20)) space.
66 * - "\t" (ASCII 9 (0x09)) tab.
67 * - "\n" (ASCII 10 (0x0A)) line feed.
68 * - "\r" (ASCII 13 (0x0D)) carriage return.
69 * - "\0" (ASCII 0 (0x00)) NULL.
70 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
72 * @param s The string to strip. Modified in place.
73 * @param char_list A string which contains the characters you want to strip.
76 void xbt_str_ltrim(char *s, const char *char_list)
79 const char *__char_list = " \t\n\r\x0B";
80 char white_char[256] = { 1, 0 };
86 while (*__char_list) {
87 white_char[(unsigned char) *__char_list++] = 1;
91 white_char[(unsigned char) *char_list++] = 1;
95 while (*cur && white_char[(unsigned char) *cur])
98 memmove(s, cur, strlen(cur) + 1);
101 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
103 * Strips the whitespaces from both the beginning and the end of s.
104 * By default (when char_list=NULL), these characters get stripped:
106 * - " " (ASCII 32 (0x20)) space.
107 * - "\t" (ASCII 9 (0x09)) tab.
108 * - "\n" (ASCII 10 (0x0A)) line feed.
109 * - "\r" (ASCII 13 (0x0D)) carriage return.
110 * - "\0" (ASCII 0 (0x00)) NULL.
111 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
113 * @param s The string to strip.
114 * @param char_list A string which contains the characters you want to strip.
117 void xbt_str_trim(char *s, const char *char_list)
123 xbt_str_rtrim(s, char_list);
124 xbt_str_ltrim(s, char_list);
127 /** @brief Replace double whitespaces (but no other characters) from the string.
129 * The function modifies the string so that each time that several spaces appear,
130 * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20).
132 * @param s The string to strip. Modified in place.
135 void xbt_str_strip_spaces(char *s)
171 /** @brief Substitutes a char for another in a string
173 * @param str the string to modify
174 * @param from char to search
175 * @param to char to put instead
176 * @param occurence number of changes to do (=0 means all)
178 void xbt_str_subst(char *str, char from, char to, int occurence)
192 /** @brief Replaces a set of variables by their values
194 * @param str The input of the replacement process
195 * @param patterns The changes to apply
196 * @return The string modified
198 * Both '$toto' and '${toto}' are valid (and the two writing are equivalent).
200 * If the variable name contains spaces, use the brace version (ie, ${toto tutu})
202 * You can provide a default value to use if the variable is not set in the dict by using
203 * '${var:=default}' or '${var:-default}'. These two forms are equivalent, even if they
204 * shouldn't to respect the shell standard (:= form should set the value in the dict,
205 * but does not) (BUG).
208 char *xbt_str_varsubst(const char *str, xbt_dict_t patterns)
210 xbt_strbuff_t buff = xbt_strbuff_new_from(str);
212 xbt_strbuff_varsubst(buff, patterns);
214 xbt_strbuff_free_container(buff);
219 /** @brief Splits a string into a dynar of strings
221 * @param s: the string to split
222 * @param sep: a string of all chars to consider as separator.
224 * By default (with sep=NULL), these characters are used as separator:
226 * - " " (ASCII 32 (0x20)) space.
227 * - "\t" (ASCII 9 (0x09)) tab.
228 * - "\n" (ASCII 10 (0x0A)) line feed.
229 * - "\r" (ASCII 13 (0x0D)) carriage return.
230 * - "\0" (ASCII 0 (0x00)) NULL.
231 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
234 xbt_dynar_t xbt_str_split(const char *s, const char *sep)
236 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
239 const char *sep_dflt = " \t\n\r\x0B";
240 char is_sep[256] = { 1, 0 };
242 /* check what are the separators */
243 memset(is_sep, 0, sizeof(is_sep));
246 is_sep[(unsigned char) *sep_dflt++] = 1;
249 is_sep[(unsigned char) *sep++] = 1;
251 is_sep[0] = 1; /* End of string is also separator */
262 while (!is_sep[(unsigned char) *q]) {
268 topush = xbt_malloc(q - p + 1);
269 memcpy(topush, p, q - p);
270 topush[q - p] = '\0';
271 xbt_dynar_push(res, &topush);
279 * \brief This functions splits a string after using another string as separator
280 * For example A!!B!!C splitted after !! will return the dynar {A,B,C}
281 * \return An array of dynars containing the string tokens
283 xbt_dynar_t xbt_str_split_str(const char *s, const char *sep)
285 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
294 if (sep[0] == '\0') {
296 xbt_dynar_push(res, &s);
303 //get the start of the first occurence of the substring
305 //if substring was not found add the entire string
308 to_push = malloc(v + 1);
309 memcpy(to_push, p, v);
311 xbt_dynar_push(res, &to_push);
315 to_push = malloc(q - p + 1);
316 memcpy(to_push, p, q - p);
317 //add string terminator
318 to_push[q - p] = '\0';
319 xbt_dynar_push(res, &to_push);
326 /** @brief Just like @ref xbt_str_split_quoted (Splits a string into a dynar of strings), but without memory allocation
328 * The string passed as argument must be writable (not const)
329 * The elements of the dynar are just parts of the string passed as argument.
331 * To free the structure constructed by this function, free the first element and free the dynar:
333 * free(xbt_dynar_get_ptr(dynar,0));
334 * xbt_dynar_free(&dynar);
336 xbt_dynar_t xbt_str_split_quoted_in_place(char *s) {
337 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), NULL);
338 char *beg, *end; /* pointers around the parsed chunk */
339 int in_simple_quote = 0, in_double_quote = 0;
341 int ctn = 0; /* Got something in this block */
348 /* do not trim leading spaces: caller responsability to clean his cruft */
357 /* Protected char; move it closer */
358 memmove(end, end + 1, strlen(end));
360 THROWF(arg_error, 0, "String ends with \\");
361 end++; /* Pass the protected char */
366 if (!in_double_quote) {
367 in_simple_quote = !in_simple_quote;
368 memmove(end, end + 1, strlen(end));
370 /* simple quote protected by double ones */
376 if (!in_simple_quote) {
377 in_double_quote = !in_double_quote;
378 memmove(end, end + 1, strlen(end));
380 /* double quote protected by simple ones */
389 if (*end == '\0' && (in_simple_quote || in_double_quote)) {
391 "End of string found while searching for %c in %s",
392 (in_simple_quote ? '\'' : '"'), s);
394 if (in_simple_quote || in_double_quote) {
402 /* Found a separator. Push the string if contains something */
403 xbt_dynar_push(res, &beg);
411 /* trim within the string, manually to speed things up */
426 /** @brief Splits a string into a dynar of strings, taking quotes into account
428 * It basically does the same argument separation than the shell, where white
429 * spaces can be escaped and where arguments are never split within a
431 * Several subsequent spaces are ignored (unless within quotes, of course).
432 * You may want to trim the input string, if you want to avoid empty entries
436 xbt_dynar_t xbt_str_split_quoted(const char *s)
438 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
440 char *str_to_free; /* we have to copy the string before, to handle backslashes */
446 str_to_free = xbt_strdup(s);
448 parsed = xbt_str_split_quoted_in_place(str_to_free);
449 xbt_dynar_foreach(parsed,cursor,p) {
450 char *q=xbt_strdup(p);
451 xbt_dynar_push(res,&q);
454 xbt_dynar_shrink(res, 0);
455 xbt_dynar_free(&parsed);
459 /** @brief Join a set of strings as a single string */
460 char *xbt_str_join(xbt_dynar_t dyn, const char *sep)
462 int len = 1, dyn_len = xbt_dynar_length(dyn);
468 return xbt_strdup("");
470 /* compute the length */
471 xbt_dynar_foreach(dyn, cpt, cursor) {
472 len += strlen(cursor);
474 len += strlen(sep) * dyn_len;
476 res = xbt_malloc(len);
478 xbt_dynar_foreach(dyn, cpt, cursor) {
479 if ((int) cpt < dyn_len - 1)
480 p += sprintf(p, "%s%s", cursor, sep);
482 p += sprintf(p, "%s", cursor);
486 /** @brief Join a set of strings as a single string
488 * The parameter must be a NULL-terminated array of chars,
489 * just like xbt_dynar_to_array() produces
491 char *xbt_str_join_array(const char *const *strs, const char *sep)
494 int amount_strings=0;
498 if ((!strs) || (!strs[0]))
499 return xbt_strdup("");
501 /* compute the length before malloc */
502 for (i=0;strs[i];i++) {
503 len += strlen(strs[i]);
506 len += strlen(sep) * amount_strings;
509 q = res = xbt_malloc(len);
510 for (i=0;strs[i];i++) {
511 if (i!=0) { // not first loop
512 q += sprintf(q, "%s%s", sep, strs[i]);
514 q += sprintf(q,"%s",strs[i]);
520 #if defined(SIMGRID_NEED_GETLINE) || defined(DOXYGEN)
521 /** @brief Get a single line from the stream (reimplementation of the GNU getline)
523 * This is a redefinition of the GNU getline function, used on platforms where it does not exists.
525 * getline() reads an entire line from stream, storing the address of the buffer
526 * containing the text into *buf. The buffer is null-terminated and includes
527 * the newline character, if one was found.
529 * If *buf is NULL, then getline() will allocate a buffer for storing the line,
530 * which should be freed by the user program. Alternatively, before calling getline(),
531 * *buf can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer
532 * is not large enough to hold the line, getline() resizes it with realloc(), updating *buf and *n
533 * as necessary. In either case, on a successful call, *buf and *n will be updated to
534 * reflect the buffer address and allocated size respectively.
536 long getline(char **buf, size_t * n, FILE * stream)
543 *buf = xbt_malloc(512);
548 return (ssize_t) - 1;
550 for (i = 0; (ch = fgetc(stream)) != EOF; i++) {
553 *buf = xbt_realloc(*buf, *n += 512);
557 if ((*buf)[i] == '\n') {
565 *buf = xbt_realloc(*buf, *n += 1);
572 #endif /* HAVE_GETLINE */
575 * Diff related functions
577 static XBT_INLINE int diff_get(xbt_matrix_t C, int i, int j)
579 return (i == -1 || j == -1) ? 0 : xbt_matrix_get_as(C, i, j, int);
582 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db)
585 xbt_matrix_new(xbt_dynar_length(da), xbt_dynar_length(db),
589 /* Compute the LCS */
591 function LCSLength(X[1..m], Y[1..n])
592 C = array(0..m, 0..n)
600 C[i,j] := C[i-1,j-1] + 1
602 C[i,j] := max(C[i,j-1], C[i-1,j])
605 for (i = 0; i < (int)xbt_dynar_length(da); i++)
606 for (j = 0; j < (int)xbt_dynar_length(db); j++) {
608 if (!strcmp(xbt_dynar_get_as(da, i, char *),
609 xbt_dynar_get_as(db, j, char *)))
610 *((int *) xbt_matrix_get_ptr(C, i, j)) = diff_get(C, i - 1, j - 1) + 1;
612 *((int *) xbt_matrix_get_ptr(C, i, j)) = max(diff_get(C, i, j - 1),
613 diff_get(C, i - 1, j));
618 static void diff_build_diff(xbt_dynar_t res,
620 xbt_dynar_t da, xbt_dynar_t db, int i, int j)
623 /* Construct the diff
624 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
625 if i > 0 and j > 0 and X[i] = Y[j]
626 printDiff(C, X, Y, i-1, j-1)
629 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
630 printDiff(C, X, Y, i, j-1)
632 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
633 printDiff(C, X, Y, i-1, j)
637 if (i >= 0 && j >= 0 && !strcmp(xbt_dynar_get_as(da, i, char *),
638 xbt_dynar_get_as(db, j, char *))) {
639 diff_build_diff(res, C, da, db, i - 1, j - 1);
640 topush = bprintf(" %s", xbt_dynar_get_as(da, i, char *));
641 xbt_dynar_push(res, &topush);
643 (i == -1 || diff_get(C, i, j - 1) >= diff_get(C, i - 1, j))) {
644 diff_build_diff(res, C, da, db, i, j - 1);
645 topush = bprintf("+ %s", xbt_dynar_get_as(db, j, char *));
646 xbt_dynar_push(res, &topush);
648 (j == -1 || diff_get(C, i, j - 1) < diff_get(C, i - 1, j))) {
649 diff_build_diff(res, C, da, db, i - 1, j);
650 topush = bprintf("- %s", xbt_dynar_get_as(da, i, char *));
651 xbt_dynar_push(res, &topush);
655 /** @brief Compute the unified diff of two strings */
656 char *xbt_str_diff(const char *a, const char *b)
658 xbt_dynar_t da = xbt_str_split(a, "\n");
659 xbt_dynar_t db = xbt_str_split(b, "\n");
665 /* Clean empty lines at the end of da and db */
667 if (len > 0 && a[len - 1] == '\n') {
669 xbt_dynar_pop(da, &str);
673 if (len > 0 && b[len - 1] == '\n') {
675 xbt_dynar_pop(db, &str);
679 C = diff_build_LCS(da, db);
680 diff = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
682 diff_build_diff(diff, C, da, db, xbt_dynar_length(da) - 1,
683 xbt_dynar_length(db) - 1);
684 res = xbt_str_join(diff, "\n");
688 xbt_dynar_free(&diff);
695 /** @brief creates a new string containing what can be read on a fd
698 char *xbt_str_from_file(FILE * file)
700 xbt_strbuff_t buff = xbt_strbuff_new();
703 memset(bread, 0, 1024);
705 while (!feof(file)) {
706 int got = fread(bread, 1, 1023, file);
708 xbt_strbuff_append(buff, bread);
712 xbt_strbuff_free_container(buff);
719 #define mytest(name, input, expected) \
720 xbt_test_add(name); \
721 d=xbt_str_split_quoted(input); \
722 s=xbt_str_join(d,"XXX"); \
723 xbt_test_assert(!strcmp(s,expected),\
724 "Input (%s) leads to (%s) instead of (%s)", \
729 XBT_TEST_SUITE("xbt_str", "String Handling");
730 XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted")
735 mytest("Empty", "", "");
736 mytest("Basic test", "toto tutu", "totoXXXtutu");
737 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u",
739 mytest("Protected space", "toto\\ tutu", "toto tutu");
740 mytest("Several spaces", "toto tutu", "totoXXXtutu");
741 mytest("LTriming", " toto tatu", "totoXXXtatu");
742 mytest("Triming", " toto tutu ", "totoXXXtutu");
743 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
744 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
745 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
746 mytest("Backslashed quotes", "\\'toto tutu\\' tata",
747 "'totoXXXtutu'XXXtata");
748 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata",
749 "toto 'tutuXXXtata");
753 #define mytest_str(name, input, separator, expected) \
754 xbt_test_add(name); \
755 d=xbt_str_split_str(input, separator); \
756 s=xbt_str_join(d,"XXX"); \
757 xbt_test_assert(!strcmp(s,expected),\
758 "Input (%s) leads to (%s) instead of (%s)", \
763 XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_split_str")
768 mytest_str("Empty string and separator", "", "", "");
769 mytest_str("Empty string", "", "##", "");
770 mytest_str("Empty separator", "toto", "", "toto");
771 mytest_str("String with no separator in it", "toto", "##", "toto");
772 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
775 /* Last args are format string and parameters for xbt_test_add */
776 #define mytest_diff(s1, s2, diff, ...) \
778 char *mytest_diff_res; \
779 xbt_test_add(__VA_ARGS__); \
780 mytest_diff_res = xbt_str_diff(s1, s2); \
781 xbt_test_assert(!strcmp(mytest_diff_res, diff), \
782 "Wrong output:\n--- got:\n%s\n--- expected:\n%s\n---", \
783 mytest_diff_res, diff); \
784 free(mytest_diff_res); \
787 XBT_TEST_UNIT("xbt_str_diff", test_diff, "test the function xbt_str_diff")
792 mytest_diff("a", "a", " a", "1 word, no difference");
793 mytest_diff("a", "A", "- a\n+ A", "1 word, different");
794 mytest_diff("a\n", "a\n", " a", "1 line, no difference");
795 mytest_diff("a\n", "A\n", "- a\n+ A", "1 line, different");
798 mytest_diff("", "", "", "empty strings");
799 mytest_diff("", "a", "+ a", "1 word, added");
800 mytest_diff("a", "", "- a", "1 word, removed");
801 mytest_diff("", "a\n", "+ a", "1 line, added");
802 mytest_diff("a\n", "", "- a", "1 line, removed");
803 mytest_diff("", "a\nb\nc\n", "+ a\n+ b\n+ c", "4 lines, all added");
804 mytest_diff("a\nb\nc\n", "", "- a\n- b\n- c", "4 lines, all removed");
807 mytest_diff("\n", "\n", " ", "empty lines");
808 mytest_diff("", "\n", "+ ", "empty line, added");
809 mytest_diff("\n", "", "- ", "empty line, removed");
811 mytest_diff("a", "\na", "+ \n a", "empty line added before word");
812 mytest_diff("a", "a\n\n", " a\n+ ", "empty line added after word");
813 mytest_diff("\na", "a", "- \n a", "empty line removed before word");
814 mytest_diff("a\n\n", "a", " a\n- ", "empty line removed after word");
816 mytest_diff("a\n", "\na\n", "+ \n a", "empty line added before line");
817 mytest_diff("a\n", "a\n\n", " a\n+ ", "empty line added after line");
818 mytest_diff("\na\n", "a\n", "- \n a", "empty line removed before line");
819 mytest_diff("a\n\n", "a\n", " a\n- ", "empty line removed after line");
821 mytest_diff("a\nb\nc\nd\n", "\na\nb\nc\nd\n", "+ \n a\n b\n c\n d",
822 "empty line added before 4 lines");
823 mytest_diff("a\nb\nc\nd\n", "a\nb\nc\nd\n\n", " a\n b\n c\n d\n+ ",
824 "empty line added after 4 lines");
825 mytest_diff("\na\nb\nc\nd\n", "a\nb\nc\nd\n", "- \n a\n b\n c\n d",
826 "empty line removed before 4 lines");
827 mytest_diff("a\nb\nc\nd\n\n", "a\nb\nc\nd\n", " a\n b\n c\n d\n- ",
828 "empty line removed after 4 lines");
830 /* Missing newline at the end of one of the strings */
831 mytest_diff("a\n", "a", " a", "1 line, 1 word, no difference");
832 mytest_diff("a", "a\n", " a", "1 word, 1 line, no difference");
833 mytest_diff("a\n", "A", "- a\n+ A", "1 line, 1 word, different");
834 mytest_diff("a", "A\n", "- a\n+ A", "1 word, 1 line, different");
836 mytest_diff("a\nb\nc\nd", "a\nb\nc\nd\n", " a\n b\n c\n d",
837 "4 lines, no newline on first");
838 mytest_diff("a\nb\nc\nd\n", "a\nb\nc\nd", " a\n b\n c\n d",
839 "4 lines, no newline on second");
841 /* Four lines, all combinations of differences */
842 for (i = 0 ; i < (1U << 4) ; i++) {
852 for (/* j */ ; j < 4 && !(i & (1U << j)) ; j++) {
854 ps += sprintf(ps, "%c\n", "abcd"[j]);
855 pr += sprintf(pr, " %c\n", "abcd"[j]);
857 for (k = j ; k < 4 && (i & (1U << k)) ; k++) {
859 ps += sprintf(ps, "%c\n", "ABCD"[k]);
860 pr += sprintf(pr, "- %c\n", "abcd"[k]);
862 for (/* j */ ; j < k ; j++) {
863 pr += sprintf(pr, "+ %c\n", "ABCD"[j]);
867 *--pr = '\0'; /* strip last '\n' from expected result */
868 mytest_diff("a\nb\nc\nd\n", s2, res,
869 "compare (abcd) with changed (%s)", d2);
872 /* Subsets of four lines, do not test for empty subset */
873 for (i = 1 ; i < (1U << 4) ; i++) {
882 for (/* j */ ; j < 4 && (i & (1U << j)) ; j++) {
884 ps += sprintf(ps, "%c\n", "abcd"[j]);
885 pr += sprintf(pr, " %c\n", "abcd"[j]);
887 for (/* j */; j < 4 && !(i & (1U << j)) ; j++) {
888 pr += sprintf(pr, "- %c\n", "abcd"[j]);
892 *--pr = '\0'; /* strip last '\n' from expected result */
893 mytest_diff("a\nb\nc\nd\n", s2, res,
894 "compare (abcd) with subset (%s)", d2);
896 for (pr = res ; *pr != '\0' ; pr++)
899 mytest_diff(s2, "a\nb\nc\nd\n", res,
900 "compare subset (%s) with (abcd)", d2);
904 #endif /* SIMGRID_TEST */