2 /* xbt_str.c - various helping functions to deal with strings */
4 /* Copyright (C) 2005-2007 Malek Cherier, Martin Quinson. */
5 /* All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package.
11 /* Returns the string without leading whitespaces (xbt_str_ltrim),
12 * trailing whitespaces (xbt_str_rtrim),
13 * or both leading and trailing whitespaces (xbt_str_trim).
14 * (in-place modification of the string)
18 #include "xbt/sysdep.h"
19 #include "xbt/str.h" /* headers of these functions */
21 #include "xbt/matrix.h" /* for the diff */
23 static void free_string(void *d){
27 /** @brief Strip whitespace (or other characters) from the end of a string.
29 * This function returns a string with whitespace stripped from the end of s.
30 * By default (without the second parameter char_list), xbt_str_rtrim() will strip these characters :
32 * - " " (ASCII 32 (0x20)) space.
33 * - "\t" (ASCII 9 (0x09)) tab.
34 * - "\n" (ASCII 10 (0x0A)) line feed.
35 * - "\r" (ASCII 13 (0x0D)) carriage return.
36 * - "\0" (ASCII 0 (0x00)) NULL.
37 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
39 * @param s The string to strip.
40 * @param char_list A string which contains the characters you want to strip.
42 * @return If the specified is NULL the function returns NULL. Otherwise the
43 * function returns the string with whitespace stripped from the end.
46 xbt_str_rtrim(char* s, const char* char_list)
49 const char* __char_list = " \t\n\r\x0B";
50 char white_char[256] = {1,0};
57 white_char[(unsigned char)*__char_list++] = 1;
61 white_char[(unsigned char)*char_list++] = 1;
68 while((cur >= s) && white_char[(unsigned char)*cur])
75 /** @brief Strip whitespace (or other characters) from the beginning of a string.
77 * This function returns a string with whitespace stripped from the beginning of s.
78 * By default (without the second parameter char_list), xbt_str_ltrim() will strip these characters :
80 * - " " (ASCII 32 (0x20)) space.
81 * - "\t" (ASCII 9 (0x09)) tab.
82 * - "\n" (ASCII 10 (0x0A)) line feed.
83 * - "\r" (ASCII 13 (0x0D)) carriage return.
84 * - "\0" (ASCII 0 (0x00)) NULL.
85 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
87 * @param s The string to strip.
88 * @param char_list A string which contains the characters you want to strip.
90 * @return If the specified is NULL the function returns NULL. Otherwise the
91 * function returns the string with whitespace stripped from the beginning.
94 xbt_str_ltrim( char* s, const char* char_list)
97 const char* __char_list = " \t\n\r\x0B";
98 char white_char[256] = {1,0};
104 while(*__char_list) {
105 white_char[(unsigned char)*__char_list++] = 1;
109 white_char[(unsigned char)*char_list++] = 1;
113 while(*cur && white_char[(unsigned char)*cur])
116 return memmove(s,cur, strlen(cur));
119 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
121 * This returns a string with whitespace stripped from the end and the begining of s.
122 * By default (without the second parameter char_list), xbt_str_trim() will strip these characters :
124 * - " " (ASCII 32 (0x20)) space.
125 * - "\t" (ASCII 9 (0x09)) tab.
126 * - "\n" (ASCII 10 (0x0A)) line feed.
127 * - "\r" (ASCII 13 (0x0D)) carriage return.
128 * - "\0" (ASCII 0 (0x00)) NULL.
129 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
131 * @param s The string to strip.
132 * @param char_list A string which contains the characters you want to strip.
134 * @return If the specified is NULL the function returns NULL. Otherwise the
135 * function returns the string with whitespace stripped from the end and the begining.
138 xbt_str_trim(char* s, const char* char_list ){
143 return xbt_str_ltrim(xbt_str_rtrim(s,char_list),char_list);
146 /** @brief Replace double whitespaces (but no other characters) from the string.
148 * The function modifies the string so that each time that several spaces appear,
149 * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20).
151 * @param s The string to strip. Modified in place.
155 xbt_str_strip_spaces(char *s) {
190 /** @brief Splits a string into a dynar of strings
192 * @param s: the string to split
193 * @param sep: a string of all chars to consider as separator.
195 * By default (with sep=NULL), these characters are used as separator:
197 * - " " (ASCII 32 (0x20)) space.
198 * - "\t" (ASCII 9 (0x09)) tab.
199 * - "\n" (ASCII 10 (0x0A)) line feed.
200 * - "\r" (ASCII 13 (0x0D)) carriage return.
201 * - "\0" (ASCII 0 (0x00)) NULL.
202 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
205 xbt_dynar_t xbt_str_split(char *s, const char *sep) {
206 char *str=xbt_strdup(s);
207 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), free_string);
210 const char* sep_dflt = " \t\n\r\x0B";
211 char is_sep[256] = {1,0};
213 /* check what are the separators */
214 memset(is_sep,0,sizeof(is_sep));
217 is_sep[(unsigned char) *sep_dflt++] = 1;
220 is_sep[(unsigned char) *sep++] = 1;
222 is_sep[0] = 1; /* End of string is also separator */
229 while (!is_sep[(unsigned char)*q]) {
235 /* do not push last empty line */
244 topush=xbt_strdup(p);
245 xbt_dynar_push(res,&topush);
252 /** @brief Join a set of strings as a single string */
254 char *xbt_str_join(xbt_dynar_t dyn, const char*sep) {
259 /* compute the length */
260 xbt_dynar_foreach(dyn,cpt,cursor) {
263 len+=strlen(sep)*(xbt_dynar_length(dyn)-1);
265 res = xbt_malloc(len);
267 xbt_dynar_foreach(dyn,cpt,cursor) {
268 p+=sprintf(p,"%s%s",cursor,sep);
274 long getline(char **buf, size_t *n, FILE *stream) {
279 *buf = xbt_malloc(512);
286 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
289 *buf = xbt_realloc(*buf, *n += 512);
293 if ((*buf)[i] == '\n') {
301 *buf = xbt_realloc(*buf, *n += 1);
308 #endif /* HAVE_GETLINE */
311 * Diff related functions
313 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db) {
314 xbt_matrix_t C = xbt_matrix_new(xbt_dynar_length(da),xbt_dynar_length(db),
317 /* Compute the LCS */
319 C = array(0..m, 0..n)
327 C[i,j] := C[i-1,j-1] + 1
329 C[i,j] := max(C[i,j-1], C[i-1,j])
332 for (i=0; i<xbt_dynar_length(da); i++)
333 *((int*) xbt_matrix_get_ptr(C,i,0) ) = 0;
335 for (j=0; j<xbt_dynar_length(db); j++)
336 *((int*) xbt_matrix_get_ptr(C,0,j) ) = 0;
338 for (i=1; i<xbt_dynar_length(da); i++)
339 for (j=1; j<xbt_dynar_length(db); j++) {
341 if (!strcmp(xbt_dynar_get_as(da,i,char*), xbt_dynar_get_as(db,j,char*)))
342 *((int*) xbt_matrix_get_ptr(C,i,j) ) = xbt_matrix_get_as(C,i-1,j-1,int) + 1;
344 *((int*) xbt_matrix_get_ptr(C,i,j) ) = max(xbt_matrix_get_as(C,i ,j-1,int),
345 xbt_matrix_get_as(C,i-1,j,int));
350 static void diff_build_diff(xbt_dynar_t res,
352 xbt_dynar_t da, xbt_dynar_t db,
355 /* Construct the diff
356 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
357 if i > 0 and j > 0 and X[i] = Y[j]
358 printDiff(C, X, Y, i-1, j-1)
361 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
362 printDiff(C, X, Y, i, j-1)
364 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
365 printDiff(C, X, Y, i-1, j)
369 if (i>=0 && j >= 0 && !strcmp(xbt_dynar_get_as(da,i,char*),
370 xbt_dynar_get_as(db,j,char*))) {
371 diff_build_diff(res,C,da,db,i-1,j-1);
372 topush = bprintf(" %s",xbt_dynar_get_as(da,i,char*));
373 xbt_dynar_push(res, &topush);
375 (i<=0 || xbt_matrix_get_as(C,i,j-1,int) >= xbt_matrix_get_as(C,i-1,j,int))) {
376 diff_build_diff(res,C,da,db,i,j-1);
377 topush = bprintf("+ %s",xbt_dynar_get_as(db,j,char*));
378 xbt_dynar_push(res,&topush);
380 (j<0 || xbt_matrix_get_as(C,i,j-1,int) < xbt_matrix_get_as(C,i-1,j,int))) {
381 diff_build_diff(res,C,da,db,i-1,j);
382 topush = bprintf("- %s",xbt_dynar_get_as(da,i,char*));
383 xbt_dynar_push(res,&topush);
384 } else if (i<0 && j<0) {
391 /** @brief Compute the unified diff of two strings */
392 char *xbt_str_diff(char *a, char *b) {
393 xbt_dynar_t da = xbt_str_split(a, "\n");
394 xbt_dynar_t db = xbt_str_split(b, "\n");
396 xbt_matrix_t C = diff_build_LCS(da,db);
397 xbt_dynar_t diff = xbt_dynar_new(sizeof(char*),free_string);
400 diff_build_diff(diff, C, da,db, xbt_dynar_length(da)-1, xbt_dynar_length(db)-1);
401 res = xbt_str_join(diff, "\n");
405 xbt_dynar_free(&diff);