Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further cleanups in the xbt_str module: add a proper prefix to the exported functions...
[simgrid.git] / src / xbt / xbt_str.c
index ac798bf..11a50b8 100644 (file)
@@ -8,8 +8,10 @@
  * under the terms of the license (GNU LGPL) which comes with this package. 
  */
 
-/* Returns a copy of a string without leading whitespaces (ltrim), trailing whitespaces (rtrim), 
- * or both leading and trailing whitespaces (trim).
+/* 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"
@@ -19,8 +21,8 @@
 
 /**  @brief Strip whitespace (or other characters) from the end of a string.
  *
- * The function rtrim() returns a string with whitespace stripped from the end of s. 
- * By default (without the second parameter char_list), rtrim() will strip these characters :
+ * 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 :
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -36,7 +38,7 @@
  * function returns the string with whitespace stripped from the end.
  */
 char*
-rtrim(char* s, const char* char_list)
+xbt_str_rtrim(char* s, const char* char_list)
 {
        char* cur = s;
        const char* __char_list = " \t\n\r\x0B";
@@ -67,8 +69,8 @@ rtrim(char* s, const char* char_list)
 
 /**  @brief Strip whitespace (or other characters) from the beginning of a string.
  *
- * The function ltrim() returns a string with whitespace stripped from the beginning of s. 
- * By default (without the second parameter char_list), ltrim() will strip these characters :
+ * 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 :
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -84,7 +86,7 @@ rtrim(char* s, const char* char_list)
  * function returns the string with whitespace stripped from the beginning.
  */
 char*
-ltrim( char* s, const char* char_list)
+xbt_str_ltrim( char* s, const char* char_list)
 {
        char* cur = s;
        const char* __char_list = " \t\n\r\x0B";
@@ -111,8 +113,8 @@ ltrim( char* s, const char* char_list)
 
 /**  @brief Strip whitespace (or other characters) from the end and the begining of a string.
  *
- * The function trim() returns a string with whitespace stripped from the end and the begining of s. 
- * By default (without the second parameter char_list), trim() will strip these characters :
+ * 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 :
  *     
  *     - " "           (ASCII 32       (0x20)) space. 
  *     - "\t"          (ASCII 9        (0x09)) tab. 
@@ -128,12 +130,56 @@ ltrim( char* s, const char* char_list)
  * function returns the string with whitespace stripped from the end and the begining.
  */
 char* 
-trim(char* s, const char* char_list ){
+xbt_str_trim(char* s, const char* char_list ){
        
        if(!s)
                return NULL;
                
-    return ltrim(rtrim(s,char_list),char_list);
+    return xbt_str_ltrim(xbt_str_rtrim(s,char_list),char_list);
+}
+
+/**  @brief Replace double whitespaces (but no other characters) from the string.
+ *
+ * The function modifies the string so that each time that several spaces appear,
+ * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20). 
+ *
+ * @param s The string to strip. Modified in place.
+ *
+ */
+void
+xbt_str_strip_spaces(char *s) {
+  char *p = s;
+  int   e = 0;
+
+  if (!s)
+    return;
+
+  while (1) {
+    if (!*p)
+      goto end;
+    
+    if (*p != ' ')
+      break;
+    
+    p++;
+  }
+  
+  e = 1;
+  
+  do {
+    if (e)
+      *s++ = *p;
+    
+    if (!*++p)
+      goto end;
+    
+    if (e ^ (*p!=' '))
+      if ((e = !e))
+       *s++ = ' ';
+  } while (1);
+
+ end:
+  *s = '\0';
 }
    
 #ifndef HAVE_GETLINE