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 * Strips the whitespaces from the end of s.
30 * By default (when char_list=NULL), these characters get stripped:
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. Modified in place.
40 * @param char_list A string which contains the characters you want to strip.
44 xbt_str_rtrim(char* s, const char* char_list)
47 const char* __char_list = " \t\n\r\x0B";
48 char white_char[256] = {1,0};
55 white_char[(unsigned char)*__char_list++] = 1;
59 white_char[(unsigned char)*char_list++] = 1;
66 while((cur >= s) && white_char[(unsigned char)*cur])
72 /** @brief Strip whitespace (or other characters) from the beginning of a string.
74 * Strips the whitespaces from the begining of s.
75 * By default (when char_list=NULL), these characters get stripped:
77 * - " " (ASCII 32 (0x20)) space.
78 * - "\t" (ASCII 9 (0x09)) tab.
79 * - "\n" (ASCII 10 (0x0A)) line feed.
80 * - "\r" (ASCII 13 (0x0D)) carriage return.
81 * - "\0" (ASCII 0 (0x00)) NULL.
82 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
84 * @param s The string to strip. Modified in place.
85 * @param char_list A string which contains the characters you want to strip.
89 xbt_str_ltrim( char* s, const char* char_list)
92 const char* __char_list = " \t\n\r\x0B";
93 char white_char[256] = {1,0};
100 white_char[(unsigned char)*__char_list++] = 1;
104 white_char[(unsigned char)*char_list++] = 1;
108 while(*cur && white_char[(unsigned char)*cur])
111 memmove(s,cur, strlen(cur));
114 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
116 * Strips the whitespaces from both the beginning and the end of s.
117 * By default (when char_list=NULL), these characters get stripped:
119 * - " " (ASCII 32 (0x20)) space.
120 * - "\t" (ASCII 9 (0x09)) tab.
121 * - "\n" (ASCII 10 (0x0A)) line feed.
122 * - "\r" (ASCII 13 (0x0D)) carriage return.
123 * - "\0" (ASCII 0 (0x00)) NULL.
124 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
126 * @param s The string to strip.
127 * @param char_list A string which contains the characters you want to strip.
131 xbt_str_trim(char* s, const char* char_list ){
136 xbt_str_rtrim(s,char_list);
137 xbt_str_ltrim(s,char_list);
140 /** @brief Replace double whitespaces (but no other characters) from the string.
142 * The function modifies the string so that each time that several spaces appear,
143 * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20).
145 * @param s The string to strip. Modified in place.
149 xbt_str_strip_spaces(char *s) {
184 /** @brief Splits a string into a dynar of strings
186 * @param s: the string to split
187 * @param sep: a string of all chars to consider as separator.
189 * By default (with sep=NULL), these characters are used as separator:
191 * - " " (ASCII 32 (0x20)) space.
192 * - "\t" (ASCII 9 (0x09)) tab.
193 * - "\n" (ASCII 10 (0x0A)) line feed.
194 * - "\r" (ASCII 13 (0x0D)) carriage return.
195 * - "\0" (ASCII 0 (0x00)) NULL.
196 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
199 xbt_dynar_t xbt_str_split(const char *s, const char *sep) {
200 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), free_string);
203 const char* sep_dflt = " \t\n\r\x0B";
204 char is_sep[256] = {1,0};
206 /* check what are the separators */
207 memset(is_sep,0,sizeof(is_sep));
210 is_sep[(unsigned char) *sep_dflt++] = 1;
213 is_sep[(unsigned char) *sep++] = 1;
215 is_sep[0] = 1; /* End of string is also separator */
226 while (!is_sep[(unsigned char)*q]) {
232 topush=xbt_malloc(q-p+1);
233 memcpy(topush,p,q-p);
234 topush[q - p] = '\0';
235 xbt_dynar_push(res,&topush);
242 /** @brief Join a set of strings as a single string */
244 char *xbt_str_join(xbt_dynar_t dyn, const char*sep) {
249 /* compute the length */
250 xbt_dynar_foreach(dyn,cpt,cursor) {
253 len+=strlen(sep)*(xbt_dynar_length(dyn)-1);
255 res = xbt_malloc(len);
257 xbt_dynar_foreach(dyn,cpt,cursor) {
258 p+=sprintf(p,"%s%s",cursor,sep);
264 long getline(char **buf, size_t *n, FILE *stream) {
269 *buf = xbt_malloc(512);
276 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
279 *buf = xbt_realloc(*buf, *n += 512);
283 if ((*buf)[i] == '\n') {
291 *buf = xbt_realloc(*buf, *n += 1);
298 #endif /* HAVE_GETLINE */
301 * Diff related functions
303 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db) {
304 xbt_matrix_t C = xbt_matrix_new(xbt_dynar_length(da),xbt_dynar_length(db),
307 /* Compute the LCS */
309 C = array(0..m, 0..n)
317 C[i,j] := C[i-1,j-1] + 1
319 C[i,j] := max(C[i,j-1], C[i-1,j])
322 for (i=0; i<xbt_dynar_length(da); i++)
323 *((int*) xbt_matrix_get_ptr(C,i,0) ) = 0;
325 for (j=0; j<xbt_dynar_length(db); j++)
326 *((int*) xbt_matrix_get_ptr(C,0,j) ) = 0;
328 for (i=1; i<xbt_dynar_length(da); i++)
329 for (j=1; j<xbt_dynar_length(db); j++) {
331 if (!strcmp(xbt_dynar_get_as(da,i,char*), xbt_dynar_get_as(db,j,char*)))
332 *((int*) xbt_matrix_get_ptr(C,i,j) ) = xbt_matrix_get_as(C,i-1,j-1,int) + 1;
334 *((int*) xbt_matrix_get_ptr(C,i,j) ) = max(xbt_matrix_get_as(C,i ,j-1,int),
335 xbt_matrix_get_as(C,i-1,j,int));
340 static void diff_build_diff(xbt_dynar_t res,
342 xbt_dynar_t da, xbt_dynar_t db,
345 /* Construct the diff
346 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
347 if i > 0 and j > 0 and X[i] = Y[j]
348 printDiff(C, X, Y, i-1, j-1)
351 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
352 printDiff(C, X, Y, i, j-1)
354 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
355 printDiff(C, X, Y, i-1, j)
359 if (i>=0 && j >= 0 && !strcmp(xbt_dynar_get_as(da,i,char*),
360 xbt_dynar_get_as(db,j,char*))) {
361 diff_build_diff(res,C,da,db,i-1,j-1);
362 topush = bprintf(" %s",xbt_dynar_get_as(da,i,char*));
363 xbt_dynar_push(res, &topush);
365 (i<=0 || xbt_matrix_get_as(C,i,j-1,int) >= xbt_matrix_get_as(C,i-1,j,int))) {
366 diff_build_diff(res,C,da,db,i,j-1);
367 topush = bprintf("+ %s",xbt_dynar_get_as(db,j,char*));
368 xbt_dynar_push(res,&topush);
370 (j<=0 || xbt_matrix_get_as(C,i,j-1,int) < xbt_matrix_get_as(C,i-1,j,int))) {
371 diff_build_diff(res,C,da,db,i-1,j);
372 topush = bprintf("- %s",xbt_dynar_get_as(da,i,char*));
373 xbt_dynar_push(res,&topush);
374 } else if (i<=0 && j<=0) {
377 THROW2(arg_error,0,"Invalid values: i=%d, j=%d",i,j);
382 /** @brief Compute the unified diff of two strings */
383 char *xbt_str_diff(char *a, char *b) {
384 xbt_dynar_t da = xbt_str_split(a, "\n");
385 xbt_dynar_t db = xbt_str_split(b, "\n");
387 xbt_matrix_t C = diff_build_LCS(da,db);
388 xbt_dynar_t diff = xbt_dynar_new(sizeof(char*),free_string);
391 diff_build_diff(diff, C, da,db, xbt_dynar_length(da)-1, xbt_dynar_length(db)-1);
392 /* Clean empty lines at the end */
393 while (xbt_dynar_length(diff) > 0) {
395 xbt_dynar_pop(diff,&str);
396 if (str[0]=='\0' || !strcmp(str," ")) {
399 xbt_dynar_push(diff,&str);
403 res = xbt_str_join(diff, "\n");
407 xbt_dynar_free(&diff);