Xinqi Bao's Git

significantly improve performance on large strings
[dmenu.git] / dmenu.c
diff --git a/dmenu.c b/dmenu.c
index d06bea1..cde394b 100644 (file)
--- a/dmenu.c
+++ b/dmenu.c
@@ -58,6 +58,13 @@ static Clr *scheme[SchemeLast];
 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
 static char *(*fstrstr)(const char *, const char *) = strstr;
 
+static unsigned int
+textw_clamp(const char *str, unsigned int n)
+{
+       unsigned int w = drw_fontset_getwidth_clamp(drw, str, n) + lrpad;
+       return MIN(w, n);
+}
+
 static void
 appenditem(struct item *item, struct item **list, struct item **last)
 {
@@ -82,10 +89,10 @@ calcoffsets(void)
                n = mw - (promptw + inputw + TEXTW("<") + TEXTW(">"));
        /* calculate which items will begin the next page and previous page */
        for (i = 0, next = curr; next; next = next->right)
-               if ((i += (lines > 0) ? bh : MIN(TEXTW(next->text), n)) > n)
+               if ((i += (lines > 0) ? bh : textw_clamp(next->text, n)) > n)
                        break;
        for (i = 0, prev = curr; prev && prev->left; prev = prev->left)
-               if ((i += (lines > 0) ? bh : MIN(TEXTW(prev->left->text), n)) > n)
+               if ((i += (lines > 0) ? bh : textw_clamp(prev->left->text, n)) > n)
                        break;
 }
 
@@ -102,6 +109,24 @@ cleanup(void)
        XCloseDisplay(dpy);
 }
 
+static char *
+cistrstr(const char *h, const char *n)
+{
+       size_t i;
+
+       if (!n[0])
+               return (char *)h;
+
+       for (; *h; ++h) {
+               for (i = 0; n[i] && tolower((unsigned char)n[i]) ==
+                           tolower((unsigned char)h[i]); ++i)
+                       ;
+               if (n[i] == '\0')
+                       return (char *)h;
+       }
+       return NULL;
+}
+
 static int
 drawitem(struct item *item, int x, int y, int w)
 {
@@ -154,7 +179,7 @@ drawmenu(void)
                }
                x += w;
                for (item = curr; item != next; item = item->right)
-                       x = drawitem(item, x, 0, MIN(TEXTW(item->text), mw - x - TEXTW(">")));
+                       x = drawitem(item, x, 0, textw_clamp(item->text, mw - x - TEXTW(">")));
                if (next) {
                        w = TEXTW(">");
                        drw_setscheme(drw, scheme[SchemeNorm]);
@@ -634,7 +659,7 @@ setup(void)
                /* no focused window is on screen, so use pointer location instead */
                if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
                        for (i = 0; i < n; i++)
-                               if (INTERSECT(x, y, 1, 1, info[i]))
+                               if (INTERSECT(x, y, 1, 1, info[i]) != 0)
                                        break;
 
                x = info[i].x_org;
@@ -711,7 +736,7 @@ main(int argc, char *argv[])
                        fast = 1;
                else if (!strcmp(argv[i], "-i")) { /* case-insensitive item matching */
                        fstrncmp = strncasecmp;
-                       fstrstr = strcasestr;
+                       fstrstr = cistrstr;
                } else if (i + 1 == argc)
                        usage();
                /* these options take one argument */