3 /* xbt_str.c - various helping functions to deal with strings */
5 /* Copyright (C) 2005-2007 Malek Cherier, Martin Quinson. */
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 Splits a string into a dynar of strings
177 * @param s: the string to split
178 * @param sep: a string of all chars to consider as separator.
180 * By default (with sep=NULL), these characters are used as separator:
182 * - " " (ASCII 32 (0x20)) space.
183 * - "\t" (ASCII 9 (0x09)) tab.
184 * - "\n" (ASCII 10 (0x0A)) line feed.
185 * - "\r" (ASCII 13 (0x0D)) carriage return.
186 * - "\0" (ASCII 0 (0x00)) NULL.
187 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
190 xbt_dynar_t xbt_str_split(const char *s, const char *sep) {
191 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), xbt_free_ref);
194 const char* sep_dflt = " \t\n\r\x0B";
195 char is_sep[256] = {1,0};
197 /* check what are the separators */
198 memset(is_sep,0,sizeof(is_sep));
201 is_sep[(unsigned char) *sep_dflt++] = 1;
204 is_sep[(unsigned char) *sep++] = 1;
206 is_sep[0] = 1; /* End of string is also separator */
217 while (!is_sep[(unsigned char)*q]) {
223 topush=xbt_malloc(q-p+1);
224 memcpy(topush,p,q-p);
225 topush[q - p] = '\0';
226 xbt_dynar_push(res,&topush);
234 * \brief This functions splits a string after using another string as separator
235 * For example A!!B!!C splitted after !! will return the dynar {A,B,C}
236 * \return An array of dynars containing the string tokens
238 xbt_dynar_t xbt_str_split_str(const char *s, const char *sep) {
239 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), xbt_free_ref);
248 if (sep[0] == '\0') {
250 xbt_dynar_push(res, &s);
257 //get the start of the first occurence of the substring
259 //if substring was not found add the entire string
262 to_push = malloc(v + 1);
263 memcpy(to_push, p, v);
265 xbt_dynar_push(res, &to_push);
270 to_push = malloc(q - p + 1);
271 memcpy(to_push, p, q - p);
272 //add string terminator
273 to_push[q - p] = '\0';
274 xbt_dynar_push(res, &to_push);
281 /** @brief Splits a string into a dynar of strings, taking quotes into account
283 * It basically does the same argument separation than the shell, where white
284 * spaces can be escaped and where arguments are never splitted within a
286 * Several subsequent spaces are ignored (unless within quotes, of course).
290 xbt_dynar_t xbt_str_split_quoted(const char *s) {
291 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), xbt_free_ref);
292 char *str; /* we have to copy the string before, to handle backslashes */
293 char *beg, *end; /* pointers around the parsed chunk */
294 int in_simple_quote=0, in_double_quote=0;
296 int ctn = 0; /* Got something in this block */
300 beg = str = xbt_strdup(s);
302 /* trim leading spaces */
303 xbt_str_ltrim(beg," ");
312 /* Protected char; move it closer */
313 memmove(end,end+1,strlen(end));
315 THROW0(arg_error,0,"String ends with \\");
316 end++; /* Pass the protected char */
321 if (!in_double_quote) {
322 in_simple_quote = !in_simple_quote;
323 memmove(end,end+1,strlen(end));
325 /* simple quote protected by double ones */
331 if (!in_simple_quote) {
332 in_double_quote = !in_double_quote;
333 memmove(end,end+1,strlen(end));
335 /* double quote protected by simple ones */
344 if (*end == '\0' && (in_simple_quote || in_double_quote)) {
346 "End of string found while searching for %c in %s",
347 (in_simple_quote?'\'':'"'),
350 if (in_simple_quote || in_double_quote) {
354 /* Found a separator. Push the string if contains something */
355 char *topush=xbt_malloc(end-beg+1);
356 memcpy(topush,beg,end-beg);
357 topush[end - beg] = '\0';
358 xbt_dynar_push(res,&topush);
368 xbt_str_ltrim(beg," ");
385 #define mytest(name, input, expected) \
386 xbt_test_add0(name); \
387 d=xbt_str_split_quoted(input); \
388 s=xbt_str_join(d,"XXX"); \
389 xbt_test_assert3(!strcmp(s,expected),\
390 "Input (%s) leads to (%s) instead of (%s)", \
395 XBT_TEST_SUITE("xbt_str","String Handling");
396 XBT_TEST_UNIT("xbt_str_split_quoted",test_split_quoted, "test the function xbt_str_split_quoted") {
400 mytest("Empty", "", "");
401 mytest("Basic test", "toto tutu", "totoXXXtutu");
402 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u", "totoXXXtutu");
403 mytest("Protected space", "toto\\ tutu", "toto tutu");
404 mytest("Several spaces", "toto tutu", "totoXXXtutu");
405 mytest("LTriming", " toto tatu", "totoXXXtatu");
406 mytest("Triming", " toto tutu ", "totoXXXtutu");
407 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
408 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
409 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
410 mytest("Backslashed quotes", "\\'toto tutu\\' tata", "'totoXXXtutu'XXXtata");
411 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata", "toto 'tutuXXXtata");
415 #define mytest_str(name, input, separator, expected) \
416 xbt_test_add0(name); \
417 d=xbt_str_split_str(input, separator); \
418 s=xbt_str_join(d,"XXX"); \
419 xbt_test_assert3(!strcmp(s,expected),\
420 "Input (%s) leads to (%s) instead of (%s)", \
425 XBT_TEST_UNIT("xbt_str_split_str",test_split_str, "test the function xbt_str_split_str") {
429 mytest_str("Empty string and separator", "", "", "");
430 mytest_str("Empty string", "", "##", "");
431 mytest_str("Empty separator", "toto", "", "toto");
432 mytest_str("String with no separator in it", "toto", "##", "toto");
433 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
435 #endif /* SIMGRID_TEST */
437 /** @brief Join a set of strings as a single string */
439 char *xbt_str_join(xbt_dynar_t dyn, const char*sep) {
440 int len=1,dyn_len=xbt_dynar_length(dyn);
446 return xbt_strdup("");
448 /* compute the length */
449 xbt_dynar_foreach(dyn,cpt,cursor) {
452 len+=strlen(sep)*dyn_len;
454 res = xbt_malloc(len);
456 xbt_dynar_foreach(dyn,cpt,cursor) {
457 if ((int)cpt<dyn_len-1)
458 p+=sprintf(p,"%s%s",cursor,sep);
460 p+=sprintf(p,"%s",cursor);
465 #if !defined(HAVE_GETLINE) || defined(DOXYGEN)
466 /* prototype here, just in case */
467 long getline(char **buf, size_t *n, FILE *stream);
469 /** @brief Get a single line from the stream (reimplementation of the GNU getline)
471 * This is a redefinition of the GNU getline function, used on platforms where it does not exists.
473 * getline() reads an entire line from stream, storing the address of the buffer
474 * containing the text into *buf. The buffer is null-terminated and includes
475 * the newline character, if one was found.
477 * If *buf is NULL, then getline() will allocate a buffer for storing the line,
478 * which should be freed by the user program. Alternatively, before calling getline(),
479 * *buf can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer
480 * is not large enough to hold the line, getline() resizes it with realloc(), updating *buf and *n
481 * as necessary. In either case, on a successful call, *buf and *n will be updated to
482 * reflect the buffer address and allocated size respectively.
484 long getline(char **buf, size_t *n, FILE *stream) {
490 *buf = xbt_malloc(512);
497 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
500 *buf = xbt_realloc(*buf, *n += 512);
504 if ((*buf)[i] == '\n') {
512 *buf = xbt_realloc(*buf, *n += 1);
519 #endif /* HAVE_GETLINE */
522 * Diff related functions
524 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db) {
525 xbt_matrix_t C = xbt_matrix_new(xbt_dynar_length(da),xbt_dynar_length(db),
529 /* Compute the LCS */
531 C = array(0..m, 0..n)
539 C[i,j] := C[i-1,j-1] + 1
541 C[i,j] := max(C[i,j-1], C[i-1,j])
544 if (xbt_dynar_length(db) != 0)
545 for (i=0; i<xbt_dynar_length(da); i++)
546 *((int*) xbt_matrix_get_ptr(C,i,0) ) = 0;
548 if (xbt_dynar_length(da) != 0)
549 for (j=0; j<xbt_dynar_length(db); j++)
550 *((int*) xbt_matrix_get_ptr(C,0,j) ) = 0;
552 for (i=1; i<xbt_dynar_length(da); i++)
553 for (j=1; j<xbt_dynar_length(db); j++) {
555 if (!strcmp(xbt_dynar_get_as(da,i,char*), xbt_dynar_get_as(db,j,char*)))
556 *((int*) xbt_matrix_get_ptr(C,i,j) ) = xbt_matrix_get_as(C,i-1,j-1,int) + 1;
558 *((int*) xbt_matrix_get_ptr(C,i,j) ) = max(xbt_matrix_get_as(C,i ,j-1,int),
559 xbt_matrix_get_as(C,i-1,j,int));
564 static void diff_build_diff(xbt_dynar_t res,
566 xbt_dynar_t da, xbt_dynar_t db,
569 /* Construct the diff
570 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
571 if i > 0 and j > 0 and X[i] = Y[j]
572 printDiff(C, X, Y, i-1, j-1)
575 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
576 printDiff(C, X, Y, i, j-1)
578 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
579 printDiff(C, X, Y, i-1, j)
583 if (i>=0 && j >= 0 && !strcmp(xbt_dynar_get_as(da,i,char*),
584 xbt_dynar_get_as(db,j,char*))) {
585 diff_build_diff(res,C,da,db,i-1,j-1);
586 topush = bprintf(" %s",xbt_dynar_get_as(da,i,char*));
587 xbt_dynar_push(res, &topush);
589 (i<=0 ||j==0|| xbt_matrix_get_as(C,i,j-1,int) >= xbt_matrix_get_as(C,i-1,j,int))) {
590 diff_build_diff(res,C,da,db,i,j-1);
591 topush = bprintf("+ %s",xbt_dynar_get_as(db,j,char*));
592 xbt_dynar_push(res,&topush);
594 (j<=0 || xbt_matrix_get_as(C,i,j-1,int) < xbt_matrix_get_as(C,i-1,j,int))) {
595 diff_build_diff(res,C,da,db,i-1,j);
596 topush = bprintf("- %s",xbt_dynar_get_as(da,i,char*));
597 xbt_dynar_push(res,&topush);
598 } else if (i<=0 && j<=0) {
601 THROW2(arg_error,0,"Invalid values: i=%d, j=%d",i,j);
606 /** @brief Compute the unified diff of two strings */
607 char *xbt_str_diff(char *a, char *b) {
608 xbt_dynar_t da = xbt_str_split(a, "\n");
609 xbt_dynar_t db = xbt_str_split(b, "\n");
611 xbt_matrix_t C = diff_build_LCS(da,db);
612 xbt_dynar_t diff = xbt_dynar_new(sizeof(char*),xbt_free_ref);
615 diff_build_diff(diff, C, da,db, xbt_dynar_length(da)-1, xbt_dynar_length(db)-1);
616 /* Clean empty lines at the end */
617 while (xbt_dynar_length(diff) > 0) {
619 xbt_dynar_pop(diff,&str);
620 if (str[0]=='\0' || !strcmp(str," ")) {
623 xbt_dynar_push(diff,&str);
627 res = xbt_str_join(diff, "\n");
631 xbt_dynar_free(&diff);