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 */
16 #include "xbt/matrix.h" /* for the diff */
18 /** @brief Strip whitespace (or other characters) from the end of a string.
20 * Strips the whitespaces from the end of s.
21 * By default (when char_list=NULL), these characters get stripped:
23 * - " " (ASCII 32 (0x20)) space.
24 * - "\t" (ASCII 9 (0x09)) tab.
25 * - "\n" (ASCII 10 (0x0A)) line feed.
26 * - "\r" (ASCII 13 (0x0D)) carriage return.
27 * - "\0" (ASCII 0 (0x00)) NULL.
28 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
30 * @param s The string to strip. Modified in place.
31 * @param char_list A string which contains the characters you want to strip.
35 xbt_str_rtrim(char* s, const char* char_list)
38 const char* __char_list = " \t\n\r\x0B";
39 char white_char[256] = {1,0};
46 white_char[(unsigned char)*__char_list++] = 1;
50 white_char[(unsigned char)*char_list++] = 1;
57 while((cur >= s) && white_char[(unsigned char)*cur])
63 /** @brief Strip whitespace (or other characters) from the beginning of a string.
65 * Strips the whitespaces from the begining of s.
66 * By default (when char_list=NULL), these characters get stripped:
68 * - " " (ASCII 32 (0x20)) space.
69 * - "\t" (ASCII 9 (0x09)) tab.
70 * - "\n" (ASCII 10 (0x0A)) line feed.
71 * - "\r" (ASCII 13 (0x0D)) carriage return.
72 * - "\0" (ASCII 0 (0x00)) NULL.
73 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
75 * @param s The string to strip. Modified in place.
76 * @param char_list A string which contains the characters you want to strip.
80 xbt_str_ltrim( char* s, const char* char_list)
83 const char* __char_list = " \t\n\r\x0B";
84 char white_char[256] = {1,0};
91 white_char[(unsigned char)*__char_list++] = 1;
95 white_char[(unsigned char)*char_list++] = 1;
99 while(*cur && white_char[(unsigned char)*cur])
102 memmove(s,cur, strlen(cur)+1);
105 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
107 * Strips the whitespaces from both the beginning and the end of s.
108 * By default (when char_list=NULL), these characters get stripped:
110 * - " " (ASCII 32 (0x20)) space.
111 * - "\t" (ASCII 9 (0x09)) tab.
112 * - "\n" (ASCII 10 (0x0A)) line feed.
113 * - "\r" (ASCII 13 (0x0D)) carriage return.
114 * - "\0" (ASCII 0 (0x00)) NULL.
115 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
117 * @param s The string to strip.
118 * @param char_list A string which contains the characters you want to strip.
122 xbt_str_trim(char* s, const char* char_list ){
127 xbt_str_rtrim(s,char_list);
128 xbt_str_ltrim(s,char_list);
131 /** @brief Replace double whitespaces (but no other characters) from the string.
133 * The function modifies the string so that each time that several spaces appear,
134 * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20).
136 * @param s The string to strip. Modified in place.
140 xbt_str_strip_spaces(char *s) {
175 /** @brief Substitutes a char for another in a string
177 * @param str the string to modify
178 * @param from char to search
179 * @param to char to put instead
180 * @param amount amount of changes to do (=0 means all)
182 void xbt_str_subst(char *str, char from, char to, int occurence) {
195 /** @brief Splits a string into a dynar of strings
197 * @param s: the string to split
198 * @param sep: a string of all chars to consider as separator.
200 * By default (with sep=NULL), these characters are used as separator:
202 * - " " (ASCII 32 (0x20)) space.
203 * - "\t" (ASCII 9 (0x09)) tab.
204 * - "\n" (ASCII 10 (0x0A)) line feed.
205 * - "\r" (ASCII 13 (0x0D)) carriage return.
206 * - "\0" (ASCII 0 (0x00)) NULL.
207 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
210 xbt_dynar_t xbt_str_split(const char *s, const char *sep) {
211 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), xbt_free_ref);
214 const char* sep_dflt = " \t\n\r\x0B";
215 char is_sep[256] = {1,0};
217 /* check what are the separators */
218 memset(is_sep,0,sizeof(is_sep));
221 is_sep[(unsigned char) *sep_dflt++] = 1;
224 is_sep[(unsigned char) *sep++] = 1;
226 is_sep[0] = 1; /* End of string is also separator */
237 while (!is_sep[(unsigned char)*q]) {
243 topush=xbt_malloc(q-p+1);
244 memcpy(topush,p,q-p);
245 topush[q - p] = '\0';
246 xbt_dynar_push(res,&topush);
254 * \brief This functions splits a string after using another string as separator
255 * For example A!!B!!C splitted after !! will return the dynar {A,B,C}
256 * \return An array of dynars containing the string tokens
258 xbt_dynar_t xbt_str_split_str(const char *s, const char *sep) {
259 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), xbt_free_ref);
268 if (sep[0] == '\0') {
270 xbt_dynar_push(res, &s);
277 //get the start of the first occurence of the substring
279 //if substring was not found add the entire string
282 to_push = malloc(v + 1);
283 memcpy(to_push, p, v);
285 xbt_dynar_push(res, &to_push);
290 to_push = malloc(q - p + 1);
291 memcpy(to_push, p, q - p);
292 //add string terminator
293 to_push[q - p] = '\0';
294 xbt_dynar_push(res, &to_push);
301 /** @brief Splits a string into a dynar of strings, taking quotes into account
303 * It basically does the same argument separation than the shell, where white
304 * spaces can be escaped and where arguments are never splitted within a
306 * Several subsequent spaces are ignored (unless within quotes, of course).
310 xbt_dynar_t xbt_str_split_quoted(const char *s) {
311 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), xbt_free_ref);
312 char *str_to_free; /* we have to copy the string before, to handle backslashes */
313 char *beg, *end; /* pointers around the parsed chunk */
314 int in_simple_quote=0, in_double_quote=0;
316 int ctn = 0; /* Got something in this block */
320 beg = str_to_free = xbt_strdup(s);
322 /* trim leading spaces */
323 xbt_str_ltrim(beg," ");
332 /* Protected char; move it closer */
333 memmove(end,end+1,strlen(end));
335 THROW0(arg_error,0,"String ends with \\");
336 end++; /* Pass the protected char */
341 if (!in_double_quote) {
342 in_simple_quote = !in_simple_quote;
343 memmove(end,end+1,strlen(end));
345 /* simple quote protected by double ones */
351 if (!in_simple_quote) {
352 in_double_quote = !in_double_quote;
353 memmove(end,end+1,strlen(end));
355 /* double quote protected by simple ones */
364 if (*end == '\0' && (in_simple_quote || in_double_quote)) {
366 "End of string found while searching for %c in %s",
367 (in_simple_quote?'\'':'"'),
370 if (in_simple_quote || in_double_quote) {
374 /* Found a separator. Push the string if contains something */
375 char *topush=xbt_malloc(end-beg+1);
376 memcpy(topush,beg,end-beg);
377 topush[end - beg] = '\0';
378 xbt_dynar_push(res,&topush);
388 xbt_str_ltrim(beg," ");
405 #define mytest(name, input, expected) \
406 xbt_test_add0(name); \
407 d=xbt_str_split_quoted(input); \
408 s=xbt_str_join(d,"XXX"); \
409 xbt_test_assert3(!strcmp(s,expected),\
410 "Input (%s) leads to (%s) instead of (%s)", \
415 XBT_TEST_SUITE("xbt_str","String Handling");
416 XBT_TEST_UNIT("xbt_str_split_quoted",test_split_quoted, "test the function xbt_str_split_quoted") {
420 mytest("Empty", "", "");
421 mytest("Basic test", "toto tutu", "totoXXXtutu");
422 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u", "totoXXXtutu");
423 mytest("Protected space", "toto\\ tutu", "toto tutu");
424 mytest("Several spaces", "toto tutu", "totoXXXtutu");
425 mytest("LTriming", " toto tatu", "totoXXXtatu");
426 mytest("Triming", " toto tutu ", "totoXXXtutu");
427 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
428 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
429 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
430 mytest("Backslashed quotes", "\\'toto tutu\\' tata", "'totoXXXtutu'XXXtata");
431 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata", "toto 'tutuXXXtata");
435 #define mytest_str(name, input, separator, expected) \
436 xbt_test_add0(name); \
437 d=xbt_str_split_str(input, separator); \
438 s=xbt_str_join(d,"XXX"); \
439 xbt_test_assert3(!strcmp(s,expected),\
440 "Input (%s) leads to (%s) instead of (%s)", \
445 XBT_TEST_UNIT("xbt_str_split_str",test_split_str, "test the function xbt_str_split_str") {
449 mytest_str("Empty string and separator", "", "", "");
450 mytest_str("Empty string", "", "##", "");
451 mytest_str("Empty separator", "toto", "", "toto");
452 mytest_str("String with no separator in it", "toto", "##", "toto");
453 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
455 #endif /* SIMGRID_TEST */
457 /** @brief Join a set of strings as a single string */
459 char *xbt_str_join(xbt_dynar_t dyn, const char*sep) {
460 int len=1,dyn_len=xbt_dynar_length(dyn);
466 return xbt_strdup("");
468 /* compute the length */
469 xbt_dynar_foreach(dyn,cpt,cursor) {
472 len+=strlen(sep)*dyn_len;
474 res = xbt_malloc(len);
476 xbt_dynar_foreach(dyn,cpt,cursor) {
477 if ((int)cpt<dyn_len-1)
478 p+=sprintf(p,"%s%s",cursor,sep);
480 p+=sprintf(p,"%s",cursor);
485 #if !defined(HAVE_GETLINE) || defined(DOXYGEN)
486 /* prototype here, just in case */
487 long getline(char **buf, size_t *n, FILE *stream);
489 /** @brief Get a single line from the stream (reimplementation of the GNU getline)
491 * This is a redefinition of the GNU getline function, used on platforms where it does not exists.
493 * getline() reads an entire line from stream, storing the address of the buffer
494 * containing the text into *buf. The buffer is null-terminated and includes
495 * the newline character, if one was found.
497 * If *buf is NULL, then getline() will allocate a buffer for storing the line,
498 * which should be freed by the user program. Alternatively, before calling getline(),
499 * *buf can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer
500 * is not large enough to hold the line, getline() resizes it with realloc(), updating *buf and *n
501 * as necessary. In either case, on a successful call, *buf and *n will be updated to
502 * reflect the buffer address and allocated size respectively.
504 long getline(char **buf, size_t *n, FILE *stream) {
510 *buf = xbt_malloc(512);
517 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
520 *buf = xbt_realloc(*buf, *n += 512);
524 if ((*buf)[i] == '\n') {
532 *buf = xbt_realloc(*buf, *n += 1);
539 #endif /* HAVE_GETLINE */
542 * Diff related functions
544 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db) {
545 xbt_matrix_t C = xbt_matrix_new(xbt_dynar_length(da),xbt_dynar_length(db),
549 /* Compute the LCS */
551 C = array(0..m, 0..n)
559 C[i,j] := C[i-1,j-1] + 1
561 C[i,j] := max(C[i,j-1], C[i-1,j])
564 if (xbt_dynar_length(db) != 0)
565 for (i=0; i<xbt_dynar_length(da); i++)
566 *((int*) xbt_matrix_get_ptr(C,i,0) ) = 0;
568 if (xbt_dynar_length(da) != 0)
569 for (j=0; j<xbt_dynar_length(db); j++)
570 *((int*) xbt_matrix_get_ptr(C,0,j) ) = 0;
572 for (i=1; i<xbt_dynar_length(da); i++)
573 for (j=1; j<xbt_dynar_length(db); j++) {
575 if (!strcmp(xbt_dynar_get_as(da,i,char*), xbt_dynar_get_as(db,j,char*)))
576 *((int*) xbt_matrix_get_ptr(C,i,j) ) = xbt_matrix_get_as(C,i-1,j-1,int) + 1;
578 *((int*) xbt_matrix_get_ptr(C,i,j) ) = max(xbt_matrix_get_as(C,i ,j-1,int),
579 xbt_matrix_get_as(C,i-1,j,int));
584 static void diff_build_diff(xbt_dynar_t res,
586 xbt_dynar_t da, xbt_dynar_t db,
589 /* Construct the diff
590 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
591 if i > 0 and j > 0 and X[i] = Y[j]
592 printDiff(C, X, Y, i-1, j-1)
595 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
596 printDiff(C, X, Y, i, j-1)
598 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
599 printDiff(C, X, Y, i-1, j)
603 if (i>=0 && j >= 0 && !strcmp(xbt_dynar_get_as(da,i,char*),
604 xbt_dynar_get_as(db,j,char*))) {
605 diff_build_diff(res,C,da,db,i-1,j-1);
606 topush = bprintf(" %s",xbt_dynar_get_as(da,i,char*));
607 xbt_dynar_push(res, &topush);
609 (i<=0 ||j==0|| xbt_matrix_get_as(C,i,j-1,int) >= xbt_matrix_get_as(C,i-1,j,int))) {
610 diff_build_diff(res,C,da,db,i,j-1);
611 topush = bprintf("+ %s",xbt_dynar_get_as(db,j,char*));
612 xbt_dynar_push(res,&topush);
614 (j<=0 || xbt_matrix_get_as(C,i,j-1,int) < xbt_matrix_get_as(C,i-1,j,int))) {
615 diff_build_diff(res,C,da,db,i-1,j);
616 topush = bprintf("- %s",xbt_dynar_get_as(da,i,char*));
617 xbt_dynar_push(res,&topush);
618 } else if (i<=0 && j<=0) {
621 THROW2(arg_error,0,"Invalid values: i=%d, j=%d",i,j);
626 /** @brief Compute the unified diff of two strings */
627 char *xbt_str_diff(char *a, char *b) {
628 xbt_dynar_t da = xbt_str_split(a, "\n");
629 xbt_dynar_t db = xbt_str_split(b, "\n");
631 xbt_matrix_t C = diff_build_LCS(da,db);
632 xbt_dynar_t diff = xbt_dynar_new(sizeof(char*),xbt_free_ref);
635 diff_build_diff(diff, C, da,db, xbt_dynar_length(da)-1, xbt_dynar_length(db)-1);
636 /* Clean empty lines at the end */
637 while (xbt_dynar_length(diff) > 0) {
639 xbt_dynar_pop(diff,&str);
640 if (str[0]=='\0' || !strcmp(str," ")) {
643 xbt_dynar_push(diff,&str);
647 res = xbt_str_join(diff, "\n");
651 xbt_dynar_free(&diff);