X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/634bf3aac7589078ddaadb1f3678d535d4c2edd4..af65faa449b38729c5419a84426def9228083b8c:/src/xbt/xbt_str.c diff --git a/src/xbt/xbt_str.c b/src/xbt/xbt_str.c index ac798bfca7..aa61389215 100644 --- a/src/xbt/xbt_str.c +++ b/src/xbt/xbt_str.c @@ -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"; @@ -58,7 +60,7 @@ rtrim(char* s, const char* char_list) while(*cur) ++cur; - while(white_char[(unsigned char)*cur] && (cur >= s)) + while((cur >= s) && white_char[(unsigned char)*cur]) --cur; *++cur = '\0'; @@ -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"; @@ -106,13 +108,13 @@ ltrim( char* s, const char* char_list) while(*cur && white_char[(unsigned char)*cur]) ++cur; - return strcpy(s,cur); + return memmove(s,cur, strlen(cur)); } /** @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