From: André Malo Date: Sun, 27 Jul 2003 19:18:27 +0000 (+0000) Subject: cleanup the select_random_value_part function. X-Git-Tag: pre_ajp_proxy~1350 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b863e3a2db7a85e71f541aae4142556b709365e8;p=apache cleanup the select_random_value_part function. improve efficiency and readabilty. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@100800 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/modules/mappers/mod_rewrite.c b/modules/mappers/mod_rewrite.c index 0cc85184aa..a403d2f302 100644 --- a/modules/mappers/mod_rewrite.c +++ b/modules/mappers/mod_rewrite.c @@ -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 */