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 where to apply the change
195 * @param patterns what to change
196 * @return The string modified
198 * Check xbt_strbuff_varsubst() for more details, and remember that the string may be reallocated (moved) in the process.
201 char *xbt_str_varsubst(char *str, xbt_dict_t patterns)
203 xbt_strbuff_t buff = xbt_strbuff_new_from(str);
205 xbt_strbuff_varsubst(buff, patterns);
207 xbt_strbuff_free_container(buff);
212 /** @brief Splits a string into a dynar of strings
214 * @param s: the string to split
215 * @param sep: a string of all chars to consider as separator.
217 * By default (with sep=NULL), these characters are used as separator:
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.
227 xbt_dynar_t xbt_str_split(const char *s, const char *sep)
229 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
232 const char *sep_dflt = " \t\n\r\x0B";
233 char is_sep[256] = { 1, 0 };
235 /* check what are the separators */
236 memset(is_sep, 0, sizeof(is_sep));
239 is_sep[(unsigned char) *sep_dflt++] = 1;
242 is_sep[(unsigned char) *sep++] = 1;
244 is_sep[0] = 1; /* End of string is also separator */
255 while (!is_sep[(unsigned char) *q]) {
261 topush = xbt_malloc(q - p + 1);
262 memcpy(topush, p, q - p);
263 topush[q - p] = '\0';
264 xbt_dynar_push(res, &topush);
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
276 xbt_dynar_t xbt_str_split_str(const char *s, const char *sep)
278 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
287 if (sep[0] == '\0') {
289 xbt_dynar_push(res, &s);
296 //get the start of the first occurence of the substring
298 //if substring was not found add the entire string
301 to_push = malloc(v + 1);
302 memcpy(to_push, p, v);
304 xbt_dynar_push(res, &to_push);
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);
319 /** @brief Just like @xbt_str_split_quoted (Splits a string into a dynar of strings), but without memory allocation
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.
324 * To free the structure constructed by this function, free the first element and free the dynar:
326 * free(xbt_dynar_get_ptr(dynar,0));
327 * xbt_dynar_free(&dynar);
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;
334 int ctn = 0; /* Got something in this block */
341 /* do not trim leading spaces: caller responsability to clean his cruft */
350 /* Protected char; move it closer */
351 memmove(end, end + 1, strlen(end));
353 THROW0(arg_error, 0, "String ends with \\");
354 end++; /* Pass the protected char */
359 if (!in_double_quote) {
360 in_simple_quote = !in_simple_quote;
361 memmove(end, end + 1, strlen(end));
363 /* simple quote protected by double ones */
369 if (!in_simple_quote) {
370 in_double_quote = !in_double_quote;
371 memmove(end, end + 1, strlen(end));
373 /* double quote protected by simple ones */
382 if (*end == '\0' && (in_simple_quote || in_double_quote)) {
384 "End of string found while searching for %c in %s",
385 (in_simple_quote ? '\'' : '"'), s);
387 if (in_simple_quote || in_double_quote) {
395 /* Found a separator. Push the string if contains something */
396 xbt_dynar_push(res, &beg);
404 /* trim within the string, manually to speed things up */
419 /** @brief Splits a string into a dynar of strings, taking quotes into account
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
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
429 xbt_dynar_t xbt_str_split_quoted(const char *s)
431 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
433 char *str_to_free; /* we have to copy the string before, to handle backslashes */
439 str_to_free = xbt_strdup(s);
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);
447 xbt_dynar_shrink(res, 0);
454 #define mytest(name, input, expected) \
455 xbt_test_add0(name); \
456 d=xbt_str_split_quoted(input); \
457 s=xbt_str_join(d,"XXX"); \
458 xbt_test_assert3(!strcmp(s,expected),\
459 "Input (%s) leads to (%s) instead of (%s)", \
464 XBT_TEST_SUITE("xbt_str", "String Handling");
465 XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted")
470 mytest("Empty", "", "");
471 mytest("Basic test", "toto tutu", "totoXXXtutu");
472 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u",
474 mytest("Protected space", "toto\\ tutu", "toto tutu");
475 mytest("Several spaces", "toto tutu", "totoXXXtutu");
476 mytest("LTriming", " toto tatu", "totoXXXtatu");
477 mytest("Triming", " toto tutu ", "totoXXXtutu");
478 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
479 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
480 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
481 mytest("Backslashed quotes", "\\'toto tutu\\' tata",
482 "'totoXXXtutu'XXXtata");
483 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata",
484 "toto 'tutuXXXtata");
488 #define mytest_str(name, input, separator, expected) \
489 xbt_test_add0(name); \
490 d=xbt_str_split_str(input, separator); \
491 s=xbt_str_join(d,"XXX"); \
492 xbt_test_assert3(!strcmp(s,expected),\
493 "Input (%s) leads to (%s) instead of (%s)", \
498 XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_split_str")
503 mytest_str("Empty string and separator", "", "", "");
504 mytest_str("Empty string", "", "##", "");
505 mytest_str("Empty separator", "toto", "", "toto");
506 mytest_str("String with no separator in it", "toto", "##", "toto");
507 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
509 #endif /* SIMGRID_TEST */
511 /** @brief Join a set of strings as a single string */
513 char *xbt_str_join(xbt_dynar_t dyn, const char *sep)
515 int len = 1, dyn_len = xbt_dynar_length(dyn);
521 return xbt_strdup("");
523 /* compute the length */
524 xbt_dynar_foreach(dyn, cpt, cursor) {
525 len += strlen(cursor);
527 len += strlen(sep) * dyn_len;
529 res = xbt_malloc(len);
531 xbt_dynar_foreach(dyn, cpt, cursor) {
532 if ((int) cpt < dyn_len - 1)
533 p += sprintf(p, "%s%s", cursor, sep);
535 p += sprintf(p, "%s", cursor);
540 #if defined(SIMGRID_NEED_GETLINE) || defined(DOXYGEN)
541 /** @brief Get a single line from the stream (reimplementation of the GNU getline)
543 * This is a redefinition of the GNU getline function, used on platforms where it does not exists.
545 * getline() reads an entire line from stream, storing the address of the buffer
546 * containing the text into *buf. The buffer is null-terminated and includes
547 * the newline character, if one was found.
549 * If *buf is NULL, then getline() will allocate a buffer for storing the line,
550 * which should be freed by the user program. Alternatively, before calling getline(),
551 * *buf can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer
552 * is not large enough to hold the line, getline() resizes it with realloc(), updating *buf and *n
553 * as necessary. In either case, on a successful call, *buf and *n will be updated to
554 * reflect the buffer address and allocated size respectively.
556 long getline(char **buf, size_t * n, FILE * stream)
563 *buf = xbt_malloc(512);
568 return (ssize_t) - 1;
570 for (i = 0; (ch = fgetc(stream)) != EOF; i++) {
573 *buf = xbt_realloc(*buf, *n += 512);
577 if ((*buf)[i] == '\n') {
585 *buf = xbt_realloc(*buf, *n += 1);
592 #endif /* HAVE_GETLINE */
595 * Diff related functions
597 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db)
600 xbt_matrix_new(xbt_dynar_length(da), xbt_dynar_length(db),
604 /* Compute the LCS */
606 C = array(0..m, 0..n)
614 C[i,j] := C[i-1,j-1] + 1
616 C[i,j] := max(C[i,j-1], C[i-1,j])
619 if (xbt_dynar_length(db) != 0)
620 for (i = 0; i < xbt_dynar_length(da); i++)
621 *((int *) xbt_matrix_get_ptr(C, i, 0)) = 0;
623 if (xbt_dynar_length(da) != 0)
624 for (j = 0; j < xbt_dynar_length(db); j++)
625 *((int *) xbt_matrix_get_ptr(C, 0, j)) = 0;
627 for (i = 1; i < xbt_dynar_length(da); i++)
628 for (j = 1; j < xbt_dynar_length(db); j++) {
631 (xbt_dynar_get_as(da, i, char *),
632 xbt_dynar_get_as(db, j, char *)))
633 *((int *) xbt_matrix_get_ptr(C, i, j)) =
634 xbt_matrix_get_as(C, i - 1, j - 1, int) + 1;
636 *((int *) xbt_matrix_get_ptr(C, i, j)) =
637 max(xbt_matrix_get_as(C, i, j - 1, int),
638 xbt_matrix_get_as(C, i - 1, j, int));
643 static void diff_build_diff(xbt_dynar_t res,
645 xbt_dynar_t da, xbt_dynar_t db, int i, int j)
648 /* Construct the diff
649 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
650 if i > 0 and j > 0 and X[i] = Y[j]
651 printDiff(C, X, Y, i-1, j-1)
654 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
655 printDiff(C, X, Y, i, j-1)
657 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
658 printDiff(C, X, Y, i-1, j)
662 if (i >= 0 && j >= 0 && !strcmp(xbt_dynar_get_as(da, i, char *),
663 xbt_dynar_get_as(db, j, char *))) {
664 diff_build_diff(res, C, da, db, i - 1, j - 1);
665 topush = bprintf(" %s", xbt_dynar_get_as(da, i, char *));
666 xbt_dynar_push(res, &topush);
669 || xbt_matrix_get_as(C, i, j - 1,
670 int) >= xbt_matrix_get_as(C, i - 1, j,
672 diff_build_diff(res, C, da, db, i, j - 1);
673 topush = bprintf("+ %s", xbt_dynar_get_as(db, j, char *));
674 xbt_dynar_push(res, &topush);
677 || xbt_matrix_get_as(C, i, j - 1, int) < xbt_matrix_get_as(C,
684 diff_build_diff(res, C, da, db, i - 1, j);
685 topush = bprintf("- %s", xbt_dynar_get_as(da, i, char *));
686 xbt_dynar_push(res, &topush);
687 } else if (i <= 0 && j <= 0) {
690 THROW2(arg_error, 0, "Invalid values: i=%d, j=%d", i, j);
695 /** @brief Compute the unified diff of two strings */
696 char *xbt_str_diff(char *a, char *b)
698 xbt_dynar_t da = xbt_str_split(a, "\n");
699 xbt_dynar_t db = xbt_str_split(b, "\n");
701 xbt_matrix_t C = diff_build_LCS(da, db);
702 xbt_dynar_t diff = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
705 diff_build_diff(diff, C, da, db, xbt_dynar_length(da) - 1,
706 xbt_dynar_length(db) - 1);
707 /* Clean empty lines at the end */
708 while (xbt_dynar_length(diff) > 0) {
710 xbt_dynar_pop(diff, &str);
711 if (str[0] == '\0' || !strcmp(str, " ")) {
714 xbt_dynar_push(diff, &str);
718 res = xbt_str_join(diff, "\n");
722 xbt_dynar_free(&diff);
729 /** @brief creates a new string containing what can be read on a fd
732 char *xbt_str_from_file(FILE * file)
734 xbt_strbuff_t buff = xbt_strbuff_new();
737 memset(bread, 0, 1024);
739 while (!feof(file)) {
740 int got = fread(bread, 1, 1023, file);
742 xbt_strbuff_append(buff, bread);
746 xbt_strbuff_free_container(buff);