* rewrite stripallbut() to return the length
authorWilfried Goesgens <dothebart@citadel.org>
Fri, 26 Nov 2010 00:11:08 +0000 (01:11 +0100)
committerWilfried Goesgens <dothebart@citadel.org>
Sat, 27 Nov 2010 12:58:50 +0000 (13:58 +0100)
 - 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
libcitadel/lib/tools.c

index 2070c14fc23b5b69e4ad7d0aadac31118f6b90c1..79a16ed19ce9be0fb460d4357eca9177c3ea739e 100644 (file)
@@ -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);
index 9060e130c609d7012e639971cbe7cfc1da113aa5..35871b67701bc82bf3446fab7e2425b5d8be076b 100644 (file)
@@ -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) {