use binary search on utf8_ranges
[m6w6/ext-http] / src / php_http_utf8.h
index ab4e05feb6c4e58491c46a49d62abc8954e278b7..74fd19bb0046a543990f8cc32c5e376b735dbd18 100644 (file)
@@ -786,24 +786,51 @@ static inline size_t utf8towc(unsigned *wc, const unsigned char *uc, size_t len)
 
 static inline zend_bool isualpha(unsigned ch)
 {
-       unsigned j;
-       const utf8_range_t *u = &utf8_ranges[0], *e = &utf8_ranges[sizeof(utf8_ranges)/sizeof(utf8_range_t)-1];
+       unsigned count = sizeof(utf8_ranges)/sizeof(utf8_range_t),
+                       lo = 0,
+                       hi = count-1,
+                       cur = (count-1)/2,
+                       prev;
+
+#undef u
+#define u (utf8_ranges[cur])
 
        do {
-               if (u->start == ch) {
+#if 0
+               fprintf(stderr, "=> cur=%u lo=%u hi=%u (%u in %u-%u)\n", cur, lo, hi, ch, u.start, u.end);
+#endif
+               if (u.start == ch) {
                        return 1;
-               } else if (u->start <= ch && u->end >= ch) {
-                       if (u->step == 1) {
+               } else if (u.start <= ch && u.end >= ch) {
+                       unsigned j;
+
+                       if (u.step == 1) {
                                return 1;
                        }
-                       for (j = u->start; j <= u->end; j+= u->step) {
+                       for (j = u.start; j <= u.end; j+= u.step) {
                                if (ch == j) {
                                        return 1;
                                }
                        }
                        return 0;
                }
-       } while (++u != e);
+
+               prev = cur;
+
+               if (u.start > ch) {
+                       hi = cur;
+                       cur -= (cur - lo) / 2;
+               } else if ((u.end && u.end < ch) || (!u.end && u.start < ch)) {
+                       lo = cur;
+                       cur += (hi - cur) / 2;
+               } else {
+                       return 0;
+               }
+
+       } while (cur != prev);
+
+#undef u
+#undef check
 
        return 0;
 }