aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoranselm@anselm1 <unknown>2008-03-22 14:52:00 +0000
committeranselm@anselm1 <unknown>2008-03-22 14:52:00 +0000
commit542c58d8d00e26f23a5e0f09a9d1e68a7259db5e (patch)
treefc8bf439c4a9220f49543a3d46027c5b58ab834f
parent745c46d8fa810bbf45ef16e79a0f84c787f1a670 (diff)
several performance tweaks
-rw-r--r--Makefile2
-rw-r--r--dmenu.c61
2 files changed, 44 insertions, 19 deletions
diff --git a/Makefile b/Makefile
index 8c0aa39..e7df2d7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
1# dmenu - dynamic menu 1# dmenu - dynamic menu
2# © 2006-2007 Anselm R. Garbe, Sander van Dijk 2# See LICENSE file for copyright and license details.
3 3
4include config.mk 4include config.mk
5 5
diff --git a/dmenu.c b/dmenu.c
index 688fabf..5191a20 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -35,13 +35,12 @@ typedef struct {
35typedef struct Item Item; 35typedef struct Item Item;
36struct Item { 36struct Item {
37 char *text; 37 char *text;
38 Bool matched;
39 Item *next; /* traverses all items */ 38 Item *next; /* traverses all items */
40 Item *left, *right; /* traverses items matching current search pattern */ 39 Item *left, *right; /* traverses items matching current search pattern */
41}; 40};
42 41
43/* forward declarations */ 42/* forward declarations */
44Item *appenditem(Item *i, Item *last); 43void appenditem(Item *i, Item **list, Item **last);
45void calcoffsets(void); 44void calcoffsets(void);
46char *cistrstr(const char *s, const char *sub); 45char *cistrstr(const char *s, const char *sub);
47void cleanup(void); 46void cleanup(void);
@@ -92,17 +91,15 @@ Window root, win;
92int (*fstrncmp)(const char *, const char *, size_t n) = strncmp; 91int (*fstrncmp)(const char *, const char *, size_t n) = strncmp;
93char *(*fstrstr)(const char *, const char *) = strstr; 92char *(*fstrstr)(const char *, const char *) = strstr;
94 93
95Item * 94void
96appenditem(Item *i, Item *last) { 95appenditem(Item *i, Item **list, Item **last) {
97 if(!last) 96 if(!(*last))
98 item = i; 97 *list = i;
99 else 98 else
100 last->right = i; 99 (*last)->right = i;
101 i->left = last; 100 i->left = *last;
102 i->right = NULL; 101 i->right = NULL;
103 last = i; 102 *last = i;
104 nitem++;
105 return last;
106} 103}
107 104
108void 105void
@@ -521,19 +518,47 @@ kpress(XKeyEvent * e) {
521void 518void
522match(char *pattern) { 519match(char *pattern) {
523 unsigned int plen; 520 unsigned int plen;
524 Item *i, *j; 521 Item *i, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend;
525 522
526 if(!pattern) 523 if(!pattern)
527 return; 524 return;
528 plen = strlen(pattern); 525 plen = strlen(pattern);
529 item = j = NULL; 526 item = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL;
530 nitem = 0; 527 nitem = 0;
531 for(i = allitems; i; i = i->next) 528 for(i = allitems; i; i = i->next)
532 if((i->matched = !fstrncmp(pattern, i->text, plen))) 529 if(!fstrncmp(pattern, i->text, plen + 1)) {
533 j = appenditem(i, j); 530 appenditem(i, &lexact, &exactend);
534 for(i = allitems; i; i = i->next) 531 nitem++;
535 if(!i->matched && fstrstr(i->text, pattern)) 532 }
536 j = appenditem(i, j); 533 else if(!fstrncmp(pattern, i->text, plen)) {
534 appenditem(i, &lprefix, &prefixend);
535 nitem++;
536 }
537 else if(fstrstr(i->text, pattern)) {
538 appenditem(i, &lsubstr, &substrend);
539 nitem++;
540 }
541 if(lexact) {
542 item = lexact;
543 itemend = exactend;
544 }
545 if(lprefix) {
546 if(itemend) {
547 itemend->right - lprefix;
548 lprefix->left = itemend;
549 }
550 else
551 item = lprefix;
552 itemend = prefixend;
553 }
554 if(lsubstr) {
555 if(itemend) {
556 itemend->right = lsubstr;
557 lsubstr->left = itemend;
558 }
559 else
560 item = lsubstr;
561 }
537 curr = prev = next = sel = item; 562 curr = prev = next = sel = item;
538 calcoffsets(); 563 calcoffsets();
539} 564}