From b063c5c8c89de1e8586dae1c6fade1e68f371165 Mon Sep 17 00:00:00 2001 From: Wilfried Goesgens Date: Fri, 26 Nov 2010 01:11:08 +0100 Subject: [PATCH] * rewrite stripallbut() to return the length - remove strlen from the for loop abort condition - move string around _once_ - return the length of the string we operated on, since we know it after the operation anyways. --- libcitadel/lib/libcitadel.h | 2 +- libcitadel/lib/tools.c | 34 ++++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 2070c14fc..79a16ed19 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -356,7 +356,7 @@ const char *cmemreadlinelen(const char *start, char *buf, int maxlen, int *retle #define IsEmptyStr(a) ((a)[0] == '\0') #define num_parms(source) num_tokens(source,(char)'|') int stripout(char *str, char leftboundary, char rightboundary); -void stripallbut(char *str, char leftboundary, char rightboundary); +long stripallbut(char *str, char leftboundary, char rightboundary); char *myfgets(char *s, int size, FILE *stream); void urlesc(char *outbuf, size_t oblen, char *strbuf); char *CtdlTempFileName(char *prefix1, int prefix2); diff --git a/libcitadel/lib/tools.c b/libcitadel/lib/tools.c index 9060e130c..35871b677 100644 --- a/libcitadel/lib/tools.c +++ b/libcitadel/lib/tools.c @@ -771,17 +771,39 @@ int stripout(char *str, char leftboundary, char rightboundary) { * Reduce a string down to a boundarized substring (for example, remove * parentheses and anything outside them). */ -void stripallbut(char *str, char leftboundary, char rightboundary) { +long stripallbut(char *str, char leftboundary, char rightboundary) { int a; - - for (a = 0; a < strlen(str); ++ a) { - if (str[a] == leftboundary) strcpy(str, &str[a+1]); + long orglen, len; + char *pchs; + long min; + + orglen = len = strlen(str); + pchs = NULL; + for (a = 0; a < len; ++ a) { + if (str[a] == leftboundary) + pchs = &str[a+1]; } - for (a = 0; a < strlen(str); ++ a) { - if (str[a] == rightboundary) str[a] = 0; + if (pchs == NULL) + min = 0; + else + min = pchs - str; + + for (a = min; a < len; ++ a) { + if (str[a] == rightboundary) + len = a - 1; } + if (len != orglen) + str[len] = '\0'; + if (pchs != NULL) + { + orglen = len - min; + memmove(str, pchs, orglen); + return orglen; + } + else + return len; } char *myfgets(char *s, int size, FILE *stream) { -- 2.30.2