Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add const qualifier to parameters of xbt_str_diff.
[simgrid.git] / src / xbt / xbt_str.c
1 /* xbt_str.c - various helping functions to deal with strings               */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "portable.h"
10 #include "xbt/misc.h"
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 */
15
16 /**  @brief Strip whitespace (or other characters) from the end of a string.
17  *
18  * Strips the whitespaces from the end of s.
19  * By default (when char_list=NULL), these characters get stripped:
20  *
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.
27  *
28  * @param s The string to strip. Modified in place.
29  * @param char_list A string which contains the characters you want to strip.
30  *
31  */
32 void xbt_str_rtrim(char *s, const char *char_list)
33 {
34   char *cur = s;
35   const char *__char_list = " \t\n\r\x0B";
36   char white_char[256] = { 1, 0 };
37
38   if (!s)
39     return;
40
41   if (!char_list) {
42     while (*__char_list) {
43       white_char[(unsigned char) *__char_list++] = 1;
44     }
45   } else {
46     while (*char_list) {
47       white_char[(unsigned char) *char_list++] = 1;
48     }
49   }
50
51   while (*cur)
52     ++cur;
53
54   while ((cur >= s) && white_char[(unsigned char) *cur])
55     --cur;
56
57   *++cur = '\0';
58 }
59
60 /**  @brief Strip whitespace (or other characters) from the beginning of a string.
61  *
62  * Strips the whitespaces from the begining of s.
63  * By default (when char_list=NULL), these characters get stripped:
64  *
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.
71  *
72  * @param s The string to strip. Modified in place.
73  * @param char_list A string which contains the characters you want to strip.
74  *
75  */
76 void xbt_str_ltrim(char *s, const char *char_list)
77 {
78   char *cur = s;
79   const char *__char_list = " \t\n\r\x0B";
80   char white_char[256] = { 1, 0 };
81
82   if (!s)
83     return;
84
85   if (!char_list) {
86     while (*__char_list) {
87       white_char[(unsigned char) *__char_list++] = 1;
88     }
89   } else {
90     while (*char_list) {
91       white_char[(unsigned char) *char_list++] = 1;
92     }
93   }
94
95   while (*cur && white_char[(unsigned char) *cur])
96     ++cur;
97
98   memmove(s, cur, strlen(cur) + 1);
99 }
100
101 /**  @brief Strip whitespace (or other characters) from the end and the begining of a string.
102  *
103  * Strips the whitespaces from both the beginning and the end of s.
104  * By default (when char_list=NULL), these characters get stripped:
105  *
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.
112  *
113  * @param s The string to strip.
114  * @param char_list A string which contains the characters you want to strip.
115  *
116  */
117 void xbt_str_trim(char *s, const char *char_list)
118 {
119
120   if (!s)
121     return;
122
123   xbt_str_rtrim(s, char_list);
124   xbt_str_ltrim(s, char_list);
125 }
126
127 /**  @brief Replace double whitespaces (but no other characters) from the string.
128  *
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).
131  *
132  * @param s The string to strip. Modified in place.
133  *
134  */
135 void xbt_str_strip_spaces(char *s)
136 {
137   char *p = s;
138   int e = 0;
139
140   if (!s)
141     return;
142
143   while (1) {
144     if (!*p)
145       goto end;
146
147     if (*p != ' ')
148       break;
149
150     p++;
151   }
152
153   e = 1;
154
155   do {
156     if (e)
157       *s++ = *p;
158
159     if (!*++p)
160       goto end;
161
162     if (e ^ (*p != ' '))
163       if ((e = !e))
164         *s++ = ' ';
165   } while (1);
166
167 end:
168   *s = '\0';
169 }
170
171 /** @brief Substitutes a char for another in a string
172  *
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)
177  */
178 void xbt_str_subst(char *str, char from, char to, int occurence)
179 {
180   char *p = str;
181   while (*p != '\0') {
182     if (*p == from) {
183       *p = to;
184       if (occurence == 1)
185         return;
186       occurence--;
187     }
188     p++;
189   }
190 }
191
192 /** @brief Replaces a set of variables by their values
193  *
194  * @param str where to apply the change
195  * @param patterns what to change
196  * @return The string modified
197  *
198  * Check xbt_strbuff_varsubst() for more details, and remember that the string may be reallocated (moved) in the process.
199  */
200
201 char *xbt_str_varsubst(char *str, xbt_dict_t patterns)
202 {
203   xbt_strbuff_t buff = xbt_strbuff_new_from(str);
204   char *res;
205   xbt_strbuff_varsubst(buff, patterns);
206   res = buff->data;
207   xbt_strbuff_free_container(buff);
208   return res;
209 }
210
211
212 /** @brief Splits a string into a dynar of strings
213  *
214  * @param s: the string to split
215  * @param sep: a string of all chars to consider as separator.
216  *
217  * By default (with sep=NULL), these characters are used as separator:
218  *
219  *      - " "           (ASCII 32       (0x20)) space.
220  *      - "\t"          (ASCII 9        (0x09)) tab.
221  *      - "\n"          (ASCII 10       (0x0A)) line feed.
222  *      - "\r"          (ASCII 13       (0x0D)) carriage return.
223  *      - "\0"          (ASCII 0        (0x00)) NULL.
224  *      - "\x0B"        (ASCII 11       (0x0B)) vertical tab.
225  */
226
227 xbt_dynar_t xbt_str_split(const char *s, const char *sep)
228 {
229   xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
230   const char *p, *q;
231   int done;
232   const char *sep_dflt = " \t\n\r\x0B";
233   char is_sep[256] = { 1, 0 };
234
235   /* check what are the separators */
236   memset(is_sep, 0, sizeof(is_sep));
237   if (!sep) {
238     while (*sep_dflt)
239       is_sep[(unsigned char) *sep_dflt++] = 1;
240   } else {
241     while (*sep)
242       is_sep[(unsigned char) *sep++] = 1;
243   }
244   is_sep[0] = 1;                /* End of string is also separator */
245
246   /* Do the job */
247   p = q = s;
248   done = 0;
249
250   if (s[0] == '\0')
251     return res;
252
253   while (!done) {
254     char *topush;
255     while (!is_sep[(unsigned char) *q]) {
256       q++;
257     }
258     if (*q == '\0')
259       done = 1;
260
261     topush = xbt_malloc(q - p + 1);
262     memcpy(topush, p, q - p);
263     topush[q - p] = '\0';
264     xbt_dynar_push(res, &topush);
265     p = ++q;
266   }
267
268   return res;
269 }
270
271 /**
272  * \brief This functions splits a string after using another string as separator
273  * For example A!!B!!C splitted after !! will return the dynar {A,B,C}
274  * \return An array of dynars containing the string tokens
275  */
276 xbt_dynar_t xbt_str_split_str(const char *s, const char *sep)
277 {
278   xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
279   int done;
280   const char *p, *q;
281
282   p = q = s;
283   done = 0;
284
285   if (s[0] == '\0')
286     return res;
287   if (sep[0] == '\0') {
288     s = xbt_strdup(s);
289     xbt_dynar_push(res, &s);
290     return res;
291   }
292
293   while (!done) {
294     char *to_push;
295     int v = 0;
296     //get the start of the first occurence of the substring
297     q = strstr(p, sep);
298     //if substring was not found add the entire string
299     if (NULL == q) {
300       v = strlen(p);
301       to_push = malloc(v + 1);
302       memcpy(to_push, p, v);
303       to_push[v] = '\0';
304       xbt_dynar_push(res, &to_push);
305       done = 1;
306     } else {
307       //get the appearance
308       to_push = malloc(q - p + 1);
309       memcpy(to_push, p, q - p);
310       //add string terminator
311       to_push[q - p] = '\0';
312       xbt_dynar_push(res, &to_push);
313       p = q + strlen(sep);
314     }
315   }
316   return res;
317 }
318
319 /** @brief Just like @ref xbt_str_split_quoted (Splits a string into a dynar of strings), but without memory allocation
320  *
321  * The string passed as argument must be writable (not const)
322  * The elements of the dynar are just parts of the string passed as argument.
323  *
324  * To free the structure constructed by this function, free the first element and free the dynar:
325  *
326  * free(xbt_dynar_get_ptr(dynar,0));
327  * xbt_dynar_free(&dynar);
328  */
329 xbt_dynar_t xbt_str_split_quoted_in_place(char *s) {
330   xbt_dynar_t res = xbt_dynar_new(sizeof(char *), NULL);
331   char *beg, *end;              /* pointers around the parsed chunk */
332   int in_simple_quote = 0, in_double_quote = 0;
333   int done = 0;
334   int ctn = 0;                  /* Got something in this block */
335
336   if (s[0] == '\0')
337     return res;
338
339   beg = s;
340
341   /* do not trim leading spaces: caller responsability to clean his cruft */
342   end = beg;
343
344   while (!done) {
345
346
347     switch (*end) {
348     case '\\':
349       ctn = 1;
350       /* Protected char; move it closer */
351       memmove(end, end + 1, strlen(end));
352       if (*end == '\0')
353         THROWF(arg_error, 0, "String ends with \\");
354       end++;                    /* Pass the protected char */
355       break;
356
357     case '\'':
358       ctn = 1;
359       if (!in_double_quote) {
360         in_simple_quote = !in_simple_quote;
361         memmove(end, end + 1, strlen(end));
362       } else {
363         /* simple quote protected by double ones */
364         end++;
365       }
366       break;
367     case '"':
368       ctn = 1;
369       if (!in_simple_quote) {
370         in_double_quote = !in_double_quote;
371         memmove(end, end + 1, strlen(end));
372       } else {
373         /* double quote protected by simple ones */
374         end++;
375       }
376       break;
377
378     case ' ':
379     case '\t':
380     case '\n':
381     case '\0':
382       if (*end == '\0' && (in_simple_quote || in_double_quote)) {
383         THROWF(arg_error, 0,
384                "End of string found while searching for %c in %s",
385                (in_simple_quote ? '\'' : '"'), s);
386       }
387       if (in_simple_quote || in_double_quote) {
388         end++;
389       } else {
390         if (*end == '\0')
391           done = 1;
392
393         *end = '\0';
394         if (ctn) {
395           /* Found a separator. Push the string if contains something */
396           xbt_dynar_push(res, &beg);
397         }
398         ctn = 0;
399
400         if (done)
401           break;
402
403         beg = ++end;
404         /* trim within the string, manually to speed things up */
405         while (*beg == ' ')
406           beg++;
407         end = beg;
408       }
409       break;
410
411     default:
412       ctn = 1;
413       end++;
414     }
415   }
416   return res;
417 }
418
419 /** @brief Splits a string into a dynar of strings, taking quotes into account
420  *
421  * It basically does the same argument separation than the shell, where white
422  * spaces can be escaped and where arguments are never split within a
423  * quote group.
424  * Several subsequent spaces are ignored (unless within quotes, of course).
425  * You may want to trim the input string, if you want to avoid empty entries
426  *
427  */
428
429 xbt_dynar_t xbt_str_split_quoted(const char *s)
430 {
431   xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
432   xbt_dynar_t parsed;
433   char *str_to_free;            /* we have to copy the string before, to handle backslashes */
434   unsigned int cursor;
435   char *p;
436
437   if (s[0] == '\0')
438     return res;
439   str_to_free = xbt_strdup(s);
440
441   parsed = xbt_str_split_quoted_in_place(str_to_free);
442   xbt_dynar_foreach(parsed,cursor,p) {
443     char *q=xbt_strdup(p);
444     xbt_dynar_push(res,&q);
445   }
446   free(str_to_free);
447   xbt_dynar_shrink(res, 0);
448   xbt_dynar_free(&parsed);
449   return res;
450 }
451
452 /** @brief Join a set of strings as a single string */
453 char *xbt_str_join(xbt_dynar_t dyn, const char *sep)
454 {
455   int len = 1, dyn_len = xbt_dynar_length(dyn);
456   unsigned int cpt;
457   char *cursor;
458   char *res, *p;
459
460   if (!dyn_len)
461     return xbt_strdup("");
462
463   /* compute the length */
464   xbt_dynar_foreach(dyn, cpt, cursor) {
465     len += strlen(cursor);
466   }
467   len += strlen(sep) * dyn_len;
468   /* Do the job */
469   res = xbt_malloc(len);
470   p = res;
471   xbt_dynar_foreach(dyn, cpt, cursor) {
472     if ((int) cpt < dyn_len - 1)
473       p += sprintf(p, "%s%s", cursor, sep);
474     else
475       p += sprintf(p, "%s", cursor);
476   }
477   return res;
478 }
479 /** @brief Join a set of strings as a single string
480  *
481  * The parameter must be a NULL-terminated array of chars,
482  * just like xbt_dynar_to_array() produces
483  */
484 char *xbt_str_join_array(const char *const *strs, const char *sep)
485 {
486   char *res,*q;
487   int amount_strings=0;
488   int len=0;
489   int i;
490
491   if ((!strs) || (!strs[0]))
492     return xbt_strdup("");
493
494   /* compute the length before malloc */
495   for (i=0;strs[i];i++) {
496     len += strlen(strs[i]);
497     amount_strings++;
498   }
499   len += strlen(sep) * amount_strings;
500
501   /* Do the job */
502   q = res = xbt_malloc(len);
503   for (i=0;strs[i];i++) {
504     if (i!=0) { // not first loop
505       q += sprintf(q, "%s%s", sep, strs[i]);
506     } else {
507       q += sprintf(q,"%s",strs[i]);
508     }
509   }
510   return res;
511 }
512
513 #if defined(SIMGRID_NEED_GETLINE) || defined(DOXYGEN)
514 /** @brief Get a single line from the stream (reimplementation of the GNU getline)
515  *
516  * This is a redefinition of the GNU getline function, used on platforms where it does not exists.
517  *
518  * getline() reads an entire line from stream, storing the address of the buffer
519  * containing the text into *buf.  The buffer is null-terminated and includes
520  * the newline character, if one was found.
521  *
522  * If *buf is NULL, then getline() will allocate a buffer for storing the line,
523  * which should be freed by the user program.  Alternatively, before calling getline(),
524  * *buf can contain a pointer to a malloc()-allocated buffer *n bytes in size.  If the buffer
525  * is not large enough to hold the line, getline() resizes it with realloc(), updating *buf and *n
526  * as necessary.  In either case, on a successful call, *buf and *n will be updated to
527  * reflect the buffer address and allocated size respectively.
528  */
529 long getline(char **buf, size_t * n, FILE * stream)
530 {
531
532   size_t i;
533   int ch;
534
535   if (!*buf) {
536     *buf = xbt_malloc(512);
537     *n = 512;
538   }
539
540   if (feof(stream))
541     return (ssize_t) - 1;
542
543   for (i = 0; (ch = fgetc(stream)) != EOF; i++) {
544
545     if (i >= (*n) + 1)
546       *buf = xbt_realloc(*buf, *n += 512);
547
548     (*buf)[i] = ch;
549
550     if ((*buf)[i] == '\n') {
551       i++;
552       (*buf)[i] = '\0';
553       break;
554     }
555   }
556
557   if (i == *n)
558     *buf = xbt_realloc(*buf, *n += 1);
559
560   (*buf)[i] = '\0';
561
562   return (ssize_t) i;
563 }
564
565 #endif                          /* HAVE_GETLINE */
566
567 /*
568  * Diff related functions
569  */
570 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db)
571 {
572   xbt_matrix_t C =
573       xbt_matrix_new(xbt_dynar_length(da), xbt_dynar_length(db),
574                      sizeof(int), NULL);
575   unsigned long i, j;
576
577   /* Compute the LCS */
578   /*
579      C = array(0..m, 0..n)
580      for i := 0..m
581      C[i,0] = 0
582      for j := 1..n
583      C[0,j] = 0
584      for i := 1..m
585      for j := 1..n
586      if X[i] = Y[j]
587      C[i,j] := C[i-1,j-1] + 1
588      else:
589      C[i,j] := max(C[i,j-1], C[i-1,j])
590      return C[m,n]
591    */
592   if (xbt_dynar_length(db) != 0)
593     for (i = 0; i < xbt_dynar_length(da); i++)
594       *((int *) xbt_matrix_get_ptr(C, i, 0)) = 0;
595
596   if (xbt_dynar_length(da) != 0)
597     for (j = 0; j < xbt_dynar_length(db); j++)
598       *((int *) xbt_matrix_get_ptr(C, 0, j)) = 0;
599
600   for (i = 1; i < xbt_dynar_length(da); i++)
601     for (j = 1; j < xbt_dynar_length(db); j++) {
602
603       if (!strcmp
604           (xbt_dynar_get_as(da, i, char *),
605            xbt_dynar_get_as(db, j, char *)))
606         *((int *) xbt_matrix_get_ptr(C, i, j)) =
607             xbt_matrix_get_as(C, i - 1, j - 1, int) + 1;
608       else
609         *((int *) xbt_matrix_get_ptr(C, i, j)) =
610             max(xbt_matrix_get_as(C, i, j - 1, int),
611                 xbt_matrix_get_as(C, i - 1, j, int));
612     }
613   return C;
614 }
615
616 static void diff_build_diff(xbt_dynar_t res,
617                             xbt_matrix_t C,
618                             xbt_dynar_t da, xbt_dynar_t db, int i, int j)
619 {
620   char *topush;
621   /* Construct the diff
622      function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
623      if i > 0 and j > 0 and X[i] = Y[j]
624      printDiff(C, X, Y, i-1, j-1)
625      print "  " + X[i]
626      else
627      if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
628      printDiff(C, X, Y, i, j-1)
629      print "+ " + Y[j]
630      else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
631      printDiff(C, X, Y, i-1, j)
632      print "- " + X[i]
633    */
634
635   if (i >= 0 && j >= 0 && !strcmp(xbt_dynar_get_as(da, i, char *),
636                                   xbt_dynar_get_as(db, j, char *))) {
637     diff_build_diff(res, C, da, db, i - 1, j - 1);
638     topush = bprintf("  %s", xbt_dynar_get_as(da, i, char *));
639     xbt_dynar_push(res, &topush);
640   } else if (j >= 0 &&
641              (i <= 0 || j == 0
642               || xbt_matrix_get_as(C, i, j - 1,
643                                    int) >= xbt_matrix_get_as(C, i - 1, j,
644                                                              int))) {
645     diff_build_diff(res, C, da, db, i, j - 1);
646     topush = bprintf("+ %s", xbt_dynar_get_as(db, j, char *));
647     xbt_dynar_push(res, &topush);
648   } else if (i >= 0 &&
649              (j <= 0
650               || xbt_matrix_get_as(C, i, j - 1, int) < xbt_matrix_get_as(C,
651                                                                          i
652                                                                          -
653                                                                          1,
654                                                                          j,
655                                                                          int)))
656   {
657     diff_build_diff(res, C, da, db, i - 1, j);
658     topush = bprintf("- %s", xbt_dynar_get_as(da, i, char *));
659     xbt_dynar_push(res, &topush);
660   } else if (i <= 0 && j <= 0) {
661     return;
662   } else {
663     THROWF(arg_error, 0, "Invalid values: i=%d, j=%d", i, j);
664   }
665
666 }
667
668 /** @brief Compute the unified diff of two strings */
669 char *xbt_str_diff(const char *a, const char *b)
670 {
671   xbt_dynar_t da = xbt_str_split(a, "\n");
672   xbt_dynar_t db = xbt_str_split(b, "\n");
673
674   xbt_matrix_t C = diff_build_LCS(da, db);
675   xbt_dynar_t diff = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
676   char *res = NULL;
677
678   diff_build_diff(diff, C, da, db, xbt_dynar_length(da) - 1,
679                   xbt_dynar_length(db) - 1);
680   /* Clean empty lines at the end */
681   while (xbt_dynar_length(diff) > 0) {
682     char *str;
683     xbt_dynar_pop(diff, &str);
684     if (str[0] == '\0' || !strcmp(str, "  ")) {
685       free(str);
686     } else {
687       xbt_dynar_push(diff, &str);
688       break;
689     }
690   }
691   res = xbt_str_join(diff, "\n");
692
693   xbt_dynar_free(&da);
694   xbt_dynar_free(&db);
695   xbt_dynar_free(&diff);
696   xbt_matrix_free(C);
697
698   return res;
699 }
700
701
702 /** @brief creates a new string containing what can be read on a fd
703  *
704  */
705 char *xbt_str_from_file(FILE * file)
706 {
707   xbt_strbuff_t buff = xbt_strbuff_new();
708   char *res;
709   char bread[1024];
710   memset(bread, 0, 1024);
711
712   while (!feof(file)) {
713     int got = fread(bread, 1, 1023, file);
714     bread[got] = '\0';
715     xbt_strbuff_append(buff, bread);
716   }
717
718   res = buff->data;
719   xbt_strbuff_free_container(buff);
720   return res;
721 }
722
723 #ifdef SIMGRID_TEST
724 #include "xbt/str.h"
725
726 #define mytest(name, input, expected) \
727   xbt_test_add(name); \
728   d=xbt_str_split_quoted(input); \
729   s=xbt_str_join(d,"XXX"); \
730   xbt_test_assert(!strcmp(s,expected),\
731                    "Input (%s) leads to (%s) instead of (%s)", \
732                    input,s,expected);\
733                    free(s); \
734                    xbt_dynar_free(&d);
735
736 XBT_TEST_SUITE("xbt_str", "String Handling");
737 XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted")
738 {
739   xbt_dynar_t d;
740   char *s;
741
742   mytest("Empty", "", "");
743   mytest("Basic test", "toto tutu", "totoXXXtutu");
744   mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u",
745          "totoXXXtutu");
746   mytest("Protected space", "toto\\ tutu", "toto tutu");
747   mytest("Several spaces", "toto   tutu", "totoXXXtutu");
748   mytest("LTriming", "  toto tatu", "totoXXXtatu");
749   mytest("Triming", "  toto   tutu  ", "totoXXXtutu");
750   mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
751   mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
752   mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
753   mytest("Backslashed quotes", "\\'toto tutu\\' tata",
754          "'totoXXXtutu'XXXtata");
755   mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata",
756          "toto 'tutuXXXtata");
757
758 }
759
760 #define mytest_str(name, input, separator, expected) \
761   xbt_test_add(name); \
762   d=xbt_str_split_str(input, separator); \
763   s=xbt_str_join(d,"XXX"); \
764   xbt_test_assert(!strcmp(s,expected),\
765                    "Input (%s) leads to (%s) instead of (%s)", \
766                    input,s,expected);\
767                    free(s); \
768                    xbt_dynar_free(&d);
769
770 XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_split_str")
771 {
772   xbt_dynar_t d;
773   char *s;
774
775   mytest_str("Empty string and separator", "", "", "");
776   mytest_str("Empty string", "", "##", "");
777   mytest_str("Empty separator", "toto", "", "toto");
778   mytest_str("String with no separator in it", "toto", "##", "toto");
779   mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
780 }
781 #endif                          /* SIMGRID_TEST */