diff options
author | anselm@anselm1 <unknown> | 2008-03-22 14:52:00 +0000 |
---|---|---|
committer | anselm@anselm1 <unknown> | 2008-03-22 14:52:00 +0000 |
commit | 542c58d8d00e26f23a5e0f09a9d1e68a7259db5e (patch) | |
tree | fc8bf439c4a9220f49543a3d46027c5b58ab834f | |
parent | 745c46d8fa810bbf45ef16e79a0f84c787f1a670 (diff) |
several performance tweaks
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | dmenu.c | 61 |
2 files changed, 44 insertions, 19 deletions
@@ -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 | ||
4 | include config.mk | 4 | include config.mk |
5 | 5 | ||
@@ -35,13 +35,12 @@ typedef struct { | |||
35 | typedef struct Item Item; | 35 | typedef struct Item Item; |
36 | struct Item { | 36 | struct 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 */ |
44 | Item *appenditem(Item *i, Item *last); | 43 | void appenditem(Item *i, Item **list, Item **last); |
45 | void calcoffsets(void); | 44 | void calcoffsets(void); |
46 | char *cistrstr(const char *s, const char *sub); | 45 | char *cistrstr(const char *s, const char *sub); |
47 | void cleanup(void); | 46 | void cleanup(void); |
@@ -92,17 +91,15 @@ Window root, win; | |||
92 | int (*fstrncmp)(const char *, const char *, size_t n) = strncmp; | 91 | int (*fstrncmp)(const char *, const char *, size_t n) = strncmp; |
93 | char *(*fstrstr)(const char *, const char *) = strstr; | 92 | char *(*fstrstr)(const char *, const char *) = strstr; |
94 | 93 | ||
95 | Item * | 94 | void |
96 | appenditem(Item *i, Item *last) { | 95 | appenditem(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 | ||
108 | void | 105 | void |
@@ -521,19 +518,47 @@ kpress(XKeyEvent * e) { | |||
521 | void | 518 | void |
522 | match(char *pattern) { | 519 | match(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 | } |