* - "\x0B" (ASCII 11 (0x0B)) vertical tab.
*/
-xbt_dynar_t xbt_str_split(char *s, const char *sep) {
- char *str=xbt_strdup(s);
+xbt_dynar_t xbt_str_split(const char *s, const char *sep) {
xbt_dynar_t res = xbt_dynar_new(sizeof(char*), free_string);
- char *p, *q;
+ const char *p, *q;
int done;
const char* sep_dflt = " \t\n\r\x0B";
char is_sep[256] = {1,0};
is_sep[0] = 1; /* End of string is also separator */
/* Do the job */
- p=q=str;
+ p=q=s;
done=0;
+
+ if (s[0] == '\0')
+ return res;
+
while (!done) {
char *topush;
while (!is_sep[(unsigned char)*q]) {
q++;
}
- if (*q == '\0') {
-#ifdef UNDEF
- if (p==q) {
- /* do not push last empty line */
- free(str);
- return res;
- }
-#endif
+ if (*q == '\0')
done = 1;
- } else {
- *q='\0';
- }
- topush=xbt_strdup(p);
+
+ topush=xbt_malloc(q-p+1);
+ memcpy(topush,p,q-p);
+ topush[q - p] = '\0';
xbt_dynar_push(res,&topush);
p = ++q;
}
- free (str);
+
return res;
}
xbt_dynar_foreach(dyn,cpt,cursor) {
len+=strlen(cursor);
}
- len+=strlen(sep)*(xbt_dynar_length(dyn)-1);
+ len+=strlen(sep)*(xbt_dynar_length(dyn));
/* Do the job */
res = xbt_malloc(len);
p=res;
diff_build_diff(res,C,da,db,i-1,j-1);
topush = bprintf(" %s",xbt_dynar_get_as(da,i,char*));
xbt_dynar_push(res, &topush);
- } else if (j>0 &&
+ } else if (j>=0 &&
(i<=0 || xbt_matrix_get_as(C,i,j-1,int) >= xbt_matrix_get_as(C,i-1,j,int))) {
diff_build_diff(res,C,da,db,i,j-1);
topush = bprintf("+ %s",xbt_dynar_get_as(db,j,char*));
xbt_dynar_push(res,&topush);
- } else if (i>0 &&
+ } else if (i>=0 &&
(j<=0 || xbt_matrix_get_as(C,i,j-1,int) < xbt_matrix_get_as(C,i-1,j,int))) {
diff_build_diff(res,C,da,db,i-1,j);
topush = bprintf("- %s",xbt_dynar_get_as(da,i,char*));
} else {
THROW2(arg_error,0,"Invalid values: i=%d, j=%d",i,j);
}
+
}
/** @brief Compute the unified diff of two strings */
char *res=NULL;
diff_build_diff(diff, C, da,db, xbt_dynar_length(da)-1, xbt_dynar_length(db)-1);
+ /* Clean empty lines at the end */
+ while (xbt_dynar_length(diff) > 0) {
+ char *str;
+ xbt_dynar_pop(diff,&str);
+ if (str[0]=='\0' || !strcmp(str," ")) {
+ free(str);
+ } else {
+ xbt_dynar_push(diff,&str);
+ break;
+ }
+ }
res = xbt_str_join(diff, "\n");
xbt_dynar_free(&da);