]> granicus.if.org Git - php/commitdiff
fix the bug I introduced previously, sorry
authorNuno Lopes <nlopess@php.net>
Fri, 10 Nov 2006 23:27:11 +0000 (23:27 +0000)
committerNuno Lopes <nlopess@php.net>
Fri, 10 Nov 2006 23:27:11 +0000 (23:27 +0000)
anyway, convert the recursive version of the binary search to an iterative one

ext/date/lib/parse_tz.c

index 723017eef0c9dcecec1590f515d1b570043f6297..69551ac6666b14a7b2bb020ce2e892730b46a1ca 100644 (file)
@@ -192,39 +192,26 @@ void timelib_dump_tzinfo(timelib_tzinfo *tz)
        }
 }
 
-static int tz_search(char *timezone, unsigned int left, unsigned int right, const timelib_tzdb *tzdb)
-{
-       int mid, cmp;
-
-       if (left > right) {
-               return -1; /* not found */
-       }
-       mid = (left + right) / 2;
-       cmp = strcasecmp(timezone, tzdb->index[mid].id);
-       if (cmp < 0) {
-               return tz_search(timezone, left, mid - 1, tzdb);
-       } else if (cmp > 0) {
-               return tz_search(timezone, mid + 1, right, tzdb);
-       } else { /* (cmp == 0) */
-               return tzdb->index[mid].pos;
-       }
-}
-
-
 static int seek_to_tz_position(const unsigned char **tzf, char *timezone, const timelib_tzdb *tzdb)
 {
-       int     pos;
-       
-       pos = tz_search(timezone, 0, tzdb->index_size - 1, tzdb);
+       int left = 0, right = tzdb->index_size - 1;
+
+       do {
+               int mid = ((unsigned)left + right) >> 1;
+               int cmp = strcasecmp(timezone, tzdb->index[mid].id);
+
+               if (cmp < 0) {
+                       right = mid - 1;
+               } else if (cmp > 0) {
+                       left = mid + 1;
+               } else { /* (cmp == 0) */
+                       (*tzf) = &(tzdb->data[tzdb->index[mid].pos + 20]);
+                       return 1;
+               }
 
-       if (pos == -1) {
-               return 0;
-       }
+       } while (left <= right);
 
-       (*tzf) = &(tzdb->data[pos + 20]);
-       return 1;
+       return 0;
 }
 
 const timelib_tzdb *timelib_builtin_db(void)