From 33357c6940d204acac23d9e7ae369d071ce61a01 Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Sat, 17 Jun 2006 14:16:38 +0200 Subject: Get rid of strsep in cache.c, avoid useless copy This destructive parsing is bad, as shown by the fact that its mere use in the code makes tuning the parser a hell. So let's drop it before going further. This also removes the need to memcpy the buffer, since we won't be modifying it. Note: there are also loads of strsep() calls in cvs_direct.c to get rid of. --- cache.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/cache.c b/cache.c index 01a8ed3..5f67a7c 100644 --- a/cache.c +++ b/cache.c @@ -365,7 +365,7 @@ enum CR_BRANCH_POINT }; -static void parse_cache_revision(PatchSetMember * psm, const char * p_buff) +static void parse_cache_revision(PatchSetMember * psm, const char * buff) { /* The format used to generate is: * "file:%s; pre_rev:%s; post_rev:%s; dead:%d; branch_point:%d\n" @@ -375,35 +375,37 @@ static void parse_cache_revision(PatchSetMember * psm, const char * p_buff) char post[REV_STR_MAX]; int dead = 0; int bp = 0; - char buff[BUFSIZ]; int state = CR_FILENAME; - const char *s; - char * p = buff; - - strcpy(buff, p_buff); + const char *sep; + char * p; + char * c; - while ((s = strsep(&p, ";"))) + for (p = buff, sep = buff; /* just ensure sep is non-NULL */ + (sep != NULL) && (c = strchr(p, ':')); + p = sep + 1) { - char * c = strchr(s, ':'); - - if (!c) - { - debug(DEBUG_APPERROR, "invalid cache revision line '%s'|'%s'", p_buff, s); - exit(1); - } + size_t len; + sep = strchr(c, ';'); + c++; - *c++ = 0; + if (sep != NULL) + len = sep - c; + else /* last field in the cache line */ + len = strlen(c); switch(state) { case CR_FILENAME: - strcpy(filename, c); + memcpy(filename, c, len); + filename[len] = '\0'; break; case CR_PRE_REV: - strcpy(pre, c); + memcpy(pre, c, len); + pre[len] = '\0'; break; case CR_POST_REV: - strcpy(post, c); + memcpy(post, c, len); + post[len] = '\0'; break; case CR_DEAD: dead = atoi(c); -- cgit v1.2.1