From: mquinson Date: Thu, 10 May 2007 15:00:02 +0000 (+0000) Subject: Allow to split const strings, and avoid a strdup in the process X-Git-Tag: v3.3~1857 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/f93e0f7ac86d849ef6b202557d83e9a75852e0d4 Allow to split const strings, and avoid a strdup in the process git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3503 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/include/xbt/str.h b/include/xbt/str.h index 36c4bdc54c..dbb5f8090e 100644 --- a/include/xbt/str.h +++ b/include/xbt/str.h @@ -37,7 +37,7 @@ XBT_PUBLIC(void) xbt_str_rtrim(char* s, const char* char_list); XBT_PUBLIC(void) xbt_str_ltrim( char* s, const char* char_list); XBT_PUBLIC(void) xbt_str_trim(char* s, const char* char_list); -XBT_PUBLIC(xbt_dynar_t) xbt_str_split(char *s, const char *sep); +XBT_PUBLIC(xbt_dynar_t) xbt_str_split(const char *s, const char *sep); XBT_PUBLIC(char *) xbt_str_join(xbt_dynar_t dynar, const char *sep); /* */ diff --git a/src/xbt/xbt_str.c b/src/xbt/xbt_str.c index 0897546e6c..1694797105 100644 --- a/src/xbt/xbt_str.c +++ b/src/xbt/xbt_str.c @@ -196,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}; @@ -216,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; }