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(char *s, const char *sep) {
200 char *str=xbt_strdup(s);
201 xbt_dynar_t res = xbt_dynar_new(sizeof(char*), free_string);
204 const char* sep_dflt = " \t\n\r\x0B";
205 char is_sep[256] = {1,0};
207 /* check what are the separators */
208 memset(is_sep,0,sizeof(is_sep));
211 is_sep[(unsigned char) *sep_dflt++] = 1;
214 is_sep[(unsigned char) *sep++] = 1;
216 is_sep[0] = 1; /* End of string is also separator */
223 while (!is_sep[(unsigned char)*q]) {
229 /* do not push last empty line */
238 topush=xbt_strdup(p);
239 xbt_dynar_push(res,&topush);
246 /** @brief Join a set of strings as a single string */
248 char *xbt_str_join(xbt_dynar_t dyn, const char*sep) {
253 /* compute the length */
254 xbt_dynar_foreach(dyn,cpt,cursor) {
257 len+=strlen(sep)*(xbt_dynar_length(dyn)-1);
259 res = xbt_malloc(len);
261 xbt_dynar_foreach(dyn,cpt,cursor) {
262 p+=sprintf(p,"%s%s",cursor,sep);
268 long getline(char **buf, size_t *n, FILE *stream) {
273 *buf = xbt_malloc(512);
280 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
283 *buf = xbt_realloc(*buf, *n += 512);
287 if ((*buf)[i] == '\n') {
295 *buf = xbt_realloc(*buf, *n += 1);
302 #endif /* HAVE_GETLINE */
305 * Diff related functions
307 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db) {
308 xbt_matrix_t C = xbt_matrix_new(xbt_dynar_length(da),xbt_dynar_length(db),
311 /* Compute the LCS */
313 C = array(0..m, 0..n)
321 C[i,j] := C[i-1,j-1] + 1
323 C[i,j] := max(C[i,j-1], C[i-1,j])
326 for (i=0; i<xbt_dynar_length(da); i++)
327 *((int*) xbt_matrix_get_ptr(C,i,0) ) = 0;
329 for (j=0; j<xbt_dynar_length(db); j++)
330 *((int*) xbt_matrix_get_ptr(C,0,j) ) = 0;
332 for (i=1; i<xbt_dynar_length(da); i++)
333 for (j=1; j<xbt_dynar_length(db); j++) {
335 if (!strcmp(xbt_dynar_get_as(da,i,char*), xbt_dynar_get_as(db,j,char*)))
336 *((int*) xbt_matrix_get_ptr(C,i,j) ) = xbt_matrix_get_as(C,i-1,j-1,int) + 1;
338 *((int*) xbt_matrix_get_ptr(C,i,j) ) = max(xbt_matrix_get_as(C,i ,j-1,int),
339 xbt_matrix_get_as(C,i-1,j,int));
344 static void diff_build_diff(xbt_dynar_t res,
346 xbt_dynar_t da, xbt_dynar_t db,
349 /* Construct the diff
350 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
351 if i > 0 and j > 0 and X[i] = Y[j]
352 printDiff(C, X, Y, i-1, j-1)
355 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
356 printDiff(C, X, Y, i, j-1)
358 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
359 printDiff(C, X, Y, i-1, j)
363 if (i>=0 && j >= 0 && !strcmp(xbt_dynar_get_as(da,i,char*),
364 xbt_dynar_get_as(db,j,char*))) {
365 diff_build_diff(res,C,da,db,i-1,j-1);
366 topush = bprintf(" %s",xbt_dynar_get_as(da,i,char*));
367 xbt_dynar_push(res, &topush);
369 (i<=0 || xbt_matrix_get_as(C,i,j-1,int) >= xbt_matrix_get_as(C,i-1,j,int))) {
370 diff_build_diff(res,C,da,db,i,j-1);
371 topush = bprintf("+ %s",xbt_dynar_get_as(db,j,char*));
372 xbt_dynar_push(res,&topush);
374 (j<=0 || xbt_matrix_get_as(C,i,j-1,int) < xbt_matrix_get_as(C,i-1,j,int))) {
375 diff_build_diff(res,C,da,db,i-1,j);
376 topush = bprintf("- %s",xbt_dynar_get_as(da,i,char*));
377 xbt_dynar_push(res,&topush);
378 } else if (i<=0 && j<=0) {
381 THROW2(arg_error,0,"Invalid values: i=%d, j=%d",i,j);
385 /** @brief Compute the unified diff of two strings */
386 char *xbt_str_diff(char *a, char *b) {
387 xbt_dynar_t da = xbt_str_split(a, "\n");
388 xbt_dynar_t db = xbt_str_split(b, "\n");
390 xbt_matrix_t C = diff_build_LCS(da,db);
391 xbt_dynar_t diff = xbt_dynar_new(sizeof(char*),free_string);
394 diff_build_diff(diff, C, da,db, xbt_dynar_length(da)-1, xbt_dynar_length(db)-1);
395 res = xbt_str_join(diff, "\n");
399 xbt_dynar_free(&diff);