3 /* xbt_str.c - various helping functions to deal with strings */
5 /* Copyright (C) 2005-2008 The SimGrid Team. */
6 /* All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package.
13 #include "xbt/sysdep.h"
14 #include "xbt/str.h" /* headers of these functions */
15 #include "xbt/strbuff.h"
17 #include "xbt/matrix.h" /* for the diff */
19 /** @brief Strip whitespace (or other characters) from the end of a string.
21 * Strips the whitespaces from the end of s.
22 * By default (when char_list=NULL), these characters get stripped:
24 * - " " (ASCII 32 (0x20)) space.
25 * - "\t" (ASCII 9 (0x09)) tab.
26 * - "\n" (ASCII 10 (0x0A)) line feed.
27 * - "\r" (ASCII 13 (0x0D)) carriage return.
28 * - "\0" (ASCII 0 (0x00)) NULL.
29 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
31 * @param s The string to strip. Modified in place.
32 * @param char_list A string which contains the characters you want to strip.
36 xbt_str_rtrim(char* s, const char* char_list)
39 const char* __char_list = " \t\n\r\x0B";
40 char white_char[256] = {1,0};
47 white_char[(unsigned char)*__char_list++] = 1;
51 white_char[(unsigned char)*char_list++] = 1;
58 while((cur >= s) && white_char[(unsigned char)*cur])
64 /** @brief Strip whitespace (or other characters) from the beginning of a string.
66 * Strips the whitespaces from the begining of s.
67 * By default (when char_list=NULL), these characters get stripped:
69 * - " " (ASCII 32 (0x20)) space.
70 * - "\t" (ASCII 9 (0x09)) tab.
71 * - "\n" (ASCII 10 (0x0A)) line feed.
72 * - "\r" (ASCII 13 (0x0D)) carriage return.
73 * - "\0" (ASCII 0 (0x00)) NULL.
74 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
76 * @param s The string to strip. Modified in place.
77 * @param char_list A string which contains the characters you want to strip.
81 xbt_str_ltrim( char* s, const char* char_list)
84 const char* __char_list = " \t\n\r\x0B";
85 char white_char[256] = {1,0};
92 white_char[(unsigned char)*__char_list++] = 1;
96 white_char[(unsigned char)*char_list++] = 1;
100 while(*cur && white_char[(unsigned char)*cur])
103 memmove(s,cur, strlen(cur)+1);
106 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
108 * Strips the whitespaces from both the beginning and the end of s.
109 * By default (when char_list=NULL), these characters get stripped:
111 * - " " (ASCII 32 (0x20)) space.
112 * - "\t" (ASCII 9 (0x09)) tab.
113 * - "\n" (ASCII 10 (0x0A)) line feed.
114 * - "\r" (ASCII 13 (0x0D)) carriage return.
115 * - "\0" (ASCII 0 (0x00)) NULL.
116 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
118 * @param s The string to strip.
119 * @param char_list A string which contains the characters you want to strip.
123 xbt_str_trim(char* s, const char* char_list ){
128 xbt_str_rtrim(s,char_list);
129 xbt_str_ltrim(s,char_list);
132 /** @brief Replace double whitespaces (but no other characters) from the string.
134 * The function modifies the string so that each time that several spaces appear,
135 * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20).
137 * @param s The string to strip. Modified in place.
141 xbt_str_strip_spaces(char *s) {
176 /** @brief Substitutes a char for another in a string
178 * @param str the string to modify
179 * @param from char to search
180 * @param to char to put instead
181 * @param amount amount of changes to do (=0 means all)
183 void xbt_str_subst(char *str, char from, char to, int occurence) {
195 /** @brief Replaces a set of variables by their values
197 * @param str where to apply the change
198 * @param patterns what to change
199 * @return The string modified
201 * Check xbt_strbuff_varsubst() for more details, and remember that the string may be reallocated (moved) in the process.
204 char *xbt_str_varsubst(char *str, xbt_dict_t patterns) {
205 xbt_strbuff_t buff = xbt_strbuff_new_from(str);
207 xbt_strbuff_varsubst(buff,patterns);
209 xbt_strbuff_free_container(buff);
214 /** @brief Splits a string into a dynar of strings
216 * @param s: the string to split
217 * @param sep: a string of all chars to consider as separator.
219 * By default (with sep=NULL), these characters are used as separator:
221 * - " " (ASCII 32 (0x20)) space.
222 * - "\t" (ASCII 9 (0x09)) tab.
223 * - "\n" (ASCII 10 (0x0A)) line feed.
224 * - "\r" (ASCII 13 (0x0D)) carriage return.
225 * - "\0" (ASCII 0 (0x00)) NULL.
226 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
229 xbt_dynar_t xbt_str_split(const char *s, const char *sep) {
230 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), xbt_free_ref);
233 const char* sep_dflt = " \t\n\r\x0B";
234 char is_sep[256] = {1,0};
236 /* check what are the separators */
237 memset(is_sep,0,sizeof(is_sep));
240 is_sep[(unsigned char) *sep_dflt++] = 1;
243 is_sep[(unsigned char) *sep++] = 1;
245 is_sep[0] = 1; /* End of string is also separator */
256 while (!is_sep[(unsigned char)*q]) {
262 topush=xbt_malloc(q-p+1);
263 memcpy(topush,p,q-p);
264 topush[q - p] = '\0';
265 xbt_dynar_push(res,&topush);
273 * \brief This functions splits a string after using another string as separator
274 * For example A!!B!!C splitted after !! will return the dynar {A,B,C}
275 * \return An array of dynars containing the string tokens
277 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);
309 to_push = malloc(q - p + 1);
310 memcpy(to_push, p, q - p);
311 //add string terminator
312 to_push[q - p] = '\0';
313 xbt_dynar_push(res, &to_push);
320 /** @brief Splits a string into a dynar of strings, taking quotes into account
322 * It basically does the same argument separation than the shell, where white
323 * spaces can be escaped and where arguments are never splitted within a
325 * Several subsequent spaces are ignored (unless within quotes, of course).
329 xbt_dynar_t xbt_str_split_quoted(const char *s) {
330 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), xbt_free_ref);
331 char *str_to_free; /* we have to copy the string before, to handle backslashes */
332 char *beg, *end; /* pointers around the parsed chunk */
333 int in_simple_quote=0, in_double_quote=0;
335 int ctn = 0; /* Got something in this block */
339 beg = str_to_free = xbt_strdup(s);
341 /* trim leading spaces */
342 xbt_str_ltrim(beg," ");
351 /* Protected char; move it closer */
352 memmove(end,end+1,strlen(end));
354 THROW0(arg_error,0,"String ends with \\");
355 end++; /* Pass the protected char */
360 if (!in_double_quote) {
361 in_simple_quote = !in_simple_quote;
362 memmove(end,end+1,strlen(end));
364 /* simple quote protected by double ones */
370 if (!in_simple_quote) {
371 in_double_quote = !in_double_quote;
372 memmove(end,end+1,strlen(end));
374 /* double quote protected by simple ones */
383 if (*end == '\0' && (in_simple_quote || in_double_quote)) {
385 "End of string found while searching for %c in %s",
386 (in_simple_quote?'\'':'"'),
389 if (in_simple_quote || in_double_quote) {
393 /* Found a separator. Push the string if contains something */
394 char *topush=xbt_malloc(end-beg+1);
395 memcpy(topush,beg,end-beg);
396 topush[end - beg] = '\0';
397 xbt_dynar_push(res,&topush);
407 xbt_str_ltrim(beg," ");
424 #define mytest(name, input, expected) \
425 xbt_test_add0(name); \
426 d=xbt_str_split_quoted(input); \
427 s=xbt_str_join(d,"XXX"); \
428 xbt_test_assert3(!strcmp(s,expected),\
429 "Input (%s) leads to (%s) instead of (%s)", \
434 XBT_TEST_SUITE("xbt_str","String Handling");
435 XBT_TEST_UNIT("xbt_str_split_quoted",test_split_quoted, "test the function xbt_str_split_quoted") {
439 mytest("Empty", "", "");
440 mytest("Basic test", "toto tutu", "totoXXXtutu");
441 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u", "totoXXXtutu");
442 mytest("Protected space", "toto\\ tutu", "toto tutu");
443 mytest("Several spaces", "toto tutu", "totoXXXtutu");
444 mytest("LTriming", " toto tatu", "totoXXXtatu");
445 mytest("Triming", " toto tutu ", "totoXXXtutu");
446 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
447 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
448 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
449 mytest("Backslashed quotes", "\\'toto tutu\\' tata", "'totoXXXtutu'XXXtata");
450 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata", "toto 'tutuXXXtata");
454 #define mytest_str(name, input, separator, expected) \
455 xbt_test_add0(name); \
456 d=xbt_str_split_str(input, separator); \
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_UNIT("xbt_str_split_str",test_split_str, "test the function xbt_str_split_str") {
468 mytest_str("Empty string and separator", "", "", "");
469 mytest_str("Empty string", "", "##", "");
470 mytest_str("Empty separator", "toto", "", "toto");
471 mytest_str("String with no separator in it", "toto", "##", "toto");
472 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
474 #endif /* SIMGRID_TEST */
476 /** @brief Join a set of strings as a single string */
478 char *xbt_str_join(xbt_dynar_t dyn, const char*sep) {
479 int len=1,dyn_len=xbt_dynar_length(dyn);
485 return xbt_strdup("");
487 /* compute the length */
488 xbt_dynar_foreach(dyn,cpt,cursor) {
491 len+=strlen(sep)*dyn_len;
493 res = xbt_malloc(len);
495 xbt_dynar_foreach(dyn,cpt,cursor) {
496 if ((int)cpt<dyn_len-1)
497 p+=sprintf(p,"%s%s",cursor,sep);
499 p+=sprintf(p,"%s",cursor);
504 #if !defined(HAVE_GETLINE) || defined(DOXYGEN)
505 /* prototype here, just in case */
506 long getline(char **buf, size_t *n, FILE *stream);
508 /** @brief Get a single line from the stream (reimplementation of the GNU getline)
510 * This is a redefinition of the GNU getline function, used on platforms where it does not exists.
512 * getline() reads an entire line from stream, storing the address of the buffer
513 * containing the text into *buf. The buffer is null-terminated and includes
514 * the newline character, if one was found.
516 * If *buf is NULL, then getline() will allocate a buffer for storing the line,
517 * which should be freed by the user program. Alternatively, before calling getline(),
518 * *buf can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer
519 * is not large enough to hold the line, getline() resizes it with realloc(), updating *buf and *n
520 * as necessary. In either case, on a successful call, *buf and *n will be updated to
521 * reflect the buffer address and allocated size respectively.
523 long getline(char **buf, size_t *n, FILE *stream) {
529 *buf = xbt_malloc(512);
536 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
539 *buf = xbt_realloc(*buf, *n += 512);
543 if ((*buf)[i] == '\n') {
551 *buf = xbt_realloc(*buf, *n += 1);
558 #endif /* HAVE_GETLINE */
561 * Diff related functions
563 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db) {
564 xbt_matrix_t C = xbt_matrix_new(xbt_dynar_length(da),xbt_dynar_length(db),
568 /* Compute the LCS */
570 C = array(0..m, 0..n)
578 C[i,j] := C[i-1,j-1] + 1
580 C[i,j] := max(C[i,j-1], C[i-1,j])
583 if (xbt_dynar_length(db) != 0)
584 for (i=0; i<xbt_dynar_length(da); i++)
585 *((int*) xbt_matrix_get_ptr(C,i,0) ) = 0;
587 if (xbt_dynar_length(da) != 0)
588 for (j=0; j<xbt_dynar_length(db); j++)
589 *((int*) xbt_matrix_get_ptr(C,0,j) ) = 0;
591 for (i=1; i<xbt_dynar_length(da); i++)
592 for (j=1; j<xbt_dynar_length(db); j++) {
594 if (!strcmp(xbt_dynar_get_as(da,i,char*), xbt_dynar_get_as(db,j,char*)))
595 *((int*) xbt_matrix_get_ptr(C,i,j) ) = xbt_matrix_get_as(C,i-1,j-1,int) + 1;
597 *((int*) xbt_matrix_get_ptr(C,i,j) ) = max(xbt_matrix_get_as(C,i ,j-1,int),
598 xbt_matrix_get_as(C,i-1,j,int));
603 static void diff_build_diff(xbt_dynar_t res,
605 xbt_dynar_t da, xbt_dynar_t db,
608 /* Construct the diff
609 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
610 if i > 0 and j > 0 and X[i] = Y[j]
611 printDiff(C, X, Y, i-1, j-1)
614 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
615 printDiff(C, X, Y, i, j-1)
617 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
618 printDiff(C, X, Y, i-1, j)
622 if (i>=0 && j >= 0 && !strcmp(xbt_dynar_get_as(da,i,char*),
623 xbt_dynar_get_as(db,j,char*))) {
624 diff_build_diff(res,C,da,db,i-1,j-1);
625 topush = bprintf(" %s",xbt_dynar_get_as(da,i,char*));
626 xbt_dynar_push(res, &topush);
628 (i<=0 ||j==0|| xbt_matrix_get_as(C,i,j-1,int) >= xbt_matrix_get_as(C,i-1,j,int))) {
629 diff_build_diff(res,C,da,db,i,j-1);
630 topush = bprintf("+ %s",xbt_dynar_get_as(db,j,char*));
631 xbt_dynar_push(res,&topush);
633 (j<=0 || xbt_matrix_get_as(C,i,j-1,int) < xbt_matrix_get_as(C,i-1,j,int))) {
634 diff_build_diff(res,C,da,db,i-1,j);
635 topush = bprintf("- %s",xbt_dynar_get_as(da,i,char*));
636 xbt_dynar_push(res,&topush);
637 } else if (i<=0 && j<=0) {
640 THROW2(arg_error,0,"Invalid values: i=%d, j=%d",i,j);
645 /** @brief Compute the unified diff of two strings */
646 char *xbt_str_diff(char *a, char *b) {
647 xbt_dynar_t da = xbt_str_split(a, "\n");
648 xbt_dynar_t db = xbt_str_split(b, "\n");
650 xbt_matrix_t C = diff_build_LCS(da,db);
651 xbt_dynar_t diff = xbt_dynar_new(sizeof(char*),xbt_free_ref);
654 diff_build_diff(diff, C, da,db, xbt_dynar_length(da)-1, xbt_dynar_length(db)-1);
655 /* Clean empty lines at the end */
656 while (xbt_dynar_length(diff) > 0) {
658 xbt_dynar_pop(diff,&str);
659 if (str[0]=='\0' || !strcmp(str," ")) {
662 xbt_dynar_push(diff,&str);
666 res = xbt_str_join(diff, "\n");
670 xbt_dynar_free(&diff);