/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package.
*/
-
-/* Returns the string without leading whitespaces (xbt_str_ltrim),
- * trailing whitespaces (xbt_str_rtrim),
- * or both leading and trailing whitespaces (xbt_str_trim).
- * (in-place modification of the string)
- */
#include "xbt/misc.h"
#include "xbt/sysdep.h"
* - "\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;
return res;
}
-#ifndef HAVE_GETLINE
+#if !defined(HAVE_GETLINE) || defined(DOXYGEN)
+/** @brief Get a single line from the stream (reimplementation of the GNU getline)
+ *
+ * This is a redefinition of the GNU getline function, used on platforms where it does not exists.
+ *
+ * getline() reads an entire line from stream, storing the address of the buffer
+ * containing the text into *buf. The buffer is null-terminated and includes
+ * the newline character, if one was found.
+ *
+ * If *buf is NULL, then getline() will allocate a buffer for storing the line,
+ * which should be freed by the user program. Alternatively, before calling getline(),
+ * *buf can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer
+ * is not large enough to hold the line, getline() resizes it with realloc(), updating *buf and *n
+ * as necessary. In either case, on a successful call, *buf and *n will be updated to
+ * reflect the buffer address and allocated size respectively.
+ */
long getline(char **buf, size_t *n, FILE *stream) {
int i, ch;
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);