Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
buglet
[simgrid.git] / src / xbt / xbt_str.c
index ab09f43..a949895 100644 (file)
@@ -26,8 +26,8 @@ static void free_string(void *d){
 
 /**  @brief Strip whitespace (or other characters) from the end of a string.
  *
- * This function returns a string with whitespace stripped from the end of s. 
- * By default (without the second parameter char_list), xbt_str_rtrim() will strip these characters :
+ * Strips the whitespaces from the end of s. 
+ * By default (when char_list=NULL), these characters get stripped:
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -36,13 +36,11 @@ static void free_string(void *d){
  *     - "\0"          (ASCII 0        (0x00)) NULL. 
  *     - "\x0B"        (ASCII 11       (0x0B)) vertical tab. 
  *
- * @param s The string to strip.
+ * @param s The string to strip. Modified in place.
  * @param char_list A string which contains the characters you want to strip.
  *
- * @return If the specified is NULL the function returns NULL. Otherwise the
- * function returns the string with whitespace stripped from the end.
  */
-char*
+void
 xbt_str_rtrim(char* s, const char* char_list)
 {
        char* cur = s;
@@ -50,7 +48,7 @@ xbt_str_rtrim(char* s, const char* char_list)
        char white_char[256] = {1,0};
        
        if(!s)
-               return NULL;
+               return;
 
        if(!char_list){
                while(*__char_list) {
@@ -69,13 +67,12 @@ xbt_str_rtrim(char* s, const char* char_list)
                --cur;
 
        *++cur = '\0';
-       return s;
 }
 
 /**  @brief Strip whitespace (or other characters) from the beginning of a string.
  *
- * This function returns a string with whitespace stripped from the beginning of s. 
- * By default (without the second parameter char_list), xbt_str_ltrim() will strip these characters :
+ * Strips the whitespaces from the begining of s.
+ * By default (when char_list=NULL), these characters get stripped:
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -84,13 +81,11 @@ xbt_str_rtrim(char* s, const char* char_list)
  *     - "\0"          (ASCII 0        (0x00)) NULL. 
  *     - "\x0B"        (ASCII 11       (0x0B)) vertical tab. 
  *
- * @param s The string to strip.
+ * @param s The string to strip. Modified in place.
  * @param char_list A string which contains the characters you want to strip.
  *
- * @return If the specified is NULL the function returns NULL. Otherwise the
- * function returns the string with whitespace stripped from the beginning.
  */
-char*
+void
 xbt_str_ltrim( char* s, const char* char_list)
 {
        char* cur = s;
@@ -98,7 +93,7 @@ xbt_str_ltrim( char* s, const char* char_list)
        char white_char[256] = {1,0};
        
        if(!s)
-               return NULL;
+               return;
 
        if(!char_list){
                while(*__char_list) {
@@ -113,13 +108,13 @@ xbt_str_ltrim( char* s, const char* char_list)
        while(*cur && white_char[(unsigned char)*cur])
                ++cur;
 
-       return memmove(s,cur, strlen(cur));
+       memmove(s,cur, strlen(cur));
 }
 
 /**  @brief Strip whitespace (or other characters) from the end and the begining of a string.
  *
- * This returns a string with whitespace stripped from the end and the begining of s. 
- * By default (without the second parameter char_list), xbt_str_trim() will strip these characters :
+ * Strips the whitespaces from both the beginning and the end of s.
+ * By default (when char_list=NULL), these characters get stripped:
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -131,16 +126,15 @@ xbt_str_ltrim( char* s, const char* char_list)
  * @param s The string to strip.
  * @param char_list A string which contains the characters you want to strip.
  *
- * @return If the specified is NULL the function returns NULL. Otherwise the
- * function returns the string with whitespace stripped from the end and the begining.
  */
-char* 
+void
 xbt_str_trim(char* s, const char* char_list ){
        
        if(!s)
-               return NULL;
+               return;
                
-    return xbt_str_ltrim(xbt_str_rtrim(s,char_list),char_list);
+    xbt_str_rtrim(s,char_list);
+    xbt_str_ltrim(s,char_list);
 }
 
 /**  @brief Replace double whitespaces (but no other characters) from the string.
@@ -202,10 +196,9 @@ xbt_str_strip_spaces(char *s) {
  *     - "\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};
@@ -222,30 +215,27 @@ xbt_dynar_t xbt_str_split(char *s, const char *sep) {
   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;
 }
 
@@ -260,7 +250,7 @@ char *xbt_str_join(xbt_dynar_t dyn, const char*sep) {
   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;
@@ -377,15 +367,16 @@ static void diff_build_diff(xbt_dynar_t res,
     topush = bprintf("+ %s",xbt_dynar_get_as(db,j,char*));
     xbt_dynar_push(res,&topush);
   } else if (i>=0 && 
-            (j<0 || xbt_matrix_get_as(C,i,j-1,int) < xbt_matrix_get_as(C,i-1,j,int))) {
+            (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*));
     xbt_dynar_push(res,&topush);        
-  } else if (i<0 && j<0) {
+  } else if (i<=0 && j<=0) {
     return;
   } else {
-    THROW_IMPOSSIBLE;
+    THROW2(arg_error,0,"Invalid values: i=%d, j=%d",i,j);
   }
+   
 }
 
 /** @brief Compute the unified diff of two strings */
@@ -398,6 +389,17 @@ char *xbt_str_diff(char *a, char *b) {
   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);