Optimize num_tokens() to use only one pass through the string
[citadel.git] / webcit / tools.c
index 1078e8beda7728c8b001fe6093ca8f25031d0827..f0ebcaca5c08c34720d6ea7d9edd5476551a0a5e 100644 (file)
@@ -45,17 +45,19 @@ char *safestrncpy(char *dest, const char *src, size_t n)
  */
 int num_tokens(char *source, char tok)
 {
-       int a = 0;
        int count = 1;
-       int len;
+       char *ptr = source;
 
-       if (source == NULL)
+       if (source == NULL) {
                return (0);
-       len = strlen(source);
-       for (a = 0; a < len; ++a) {
-               if (source[a] == tok)
+       }
+
+       while (*ptr) {
+               if (*ptr++ == tok) {
                        ++count;
+               }
        }
+       
        return (count);
 }