]> granicus.if.org Git - apache/commitdiff
cleanup the select_random_value_part function.
authorAndré Malo <nd@apache.org>
Sun, 27 Jul 2003 19:18:27 +0000 (19:18 +0000)
committerAndré Malo <nd@apache.org>
Sun, 27 Jul 2003 19:18:27 +0000 (19:18 +0000)
improve efficiency and readabilty.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@100800 13f79535-47bb-0310-9956-ffa450edef68

modules/mappers/mod_rewrite.c

index 0cc85184aa95ac9aa6cd4983e8240baa915399a2..a403d2f3027019571a68f86151b78e0ef9e0ee13 100644 (file)
@@ -1226,38 +1226,30 @@ static int rewrite_rand(int l, int h)
 
 static char *select_random_value_part(request_rec *r, char *value)
 {
-    char *buf;
-    int n, i, k;
+    char *p = value;
+    unsigned n = 1;
 
-    /*  count number of distinct values  */
-    for (n = 1, i = 0; value[i] != '\0'; i++) {
-        if (value[i] == '|') {
-            n++;
-        }
-    }
-
-    /*  when only one value we have no option to choose  */
-    if (n == 1) {
-        return value;
+    /* count number of distinct values */
+    while ((p = ap_strchr(p, '|')) != NULL) {
+        ++n;
+        ++p;
     }
 
-    /*  else randomly select one  */
-    k = rewrite_rand(1, n);
-
-    /*  and grep it out  */
-    for (n = 1, i = 0; value[i] != '\0'; i++) {
-        if (n == k) {
-            break;
+    if (n > 1) {
+        n = rewrite_rand(1, n);
+        while (--n && (value = ap_strchr(value, '|')) != NULL) {
+            ++value;
         }
-        if (value[i] == '|') {
-            n++;
+
+        if (value) { /* should not be NULL, but ... */
+            p = ap_strchr(value, '|');
+            if (p) {
+                *p = '\0';
+            }
         }
     }
-    buf = apr_pstrdup(r->pool, &value[i]);
-    for (i = 0; buf[i] != '\0' && buf[i] != '|'; i++)
-        ;
-    buf[i] = '\0';
-    return buf;
+
+    return value;
 }
 
 /* child process code */