aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/MenuSearch.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/MenuSearch.cc')
-rw-r--r--src/FbTk/MenuSearch.cc37
1 files changed, 19 insertions, 18 deletions
diff --git a/src/FbTk/MenuSearch.cc b/src/FbTk/MenuSearch.cc
index 7efd232..689323c 100644
--- a/src/FbTk/MenuSearch.cc
+++ b/src/FbTk/MenuSearch.cc
@@ -19,7 +19,7 @@ size_t search_str_textstart(const std::string& text, const std::string& pattern)
19 19
20 size_t i; 20 size_t i;
21 for (i = l; i > 0; i--) { 21 for (i = l; i > 0; i--) {
22 if (std::tolower(text[i-1]) != std::tolower(pattern[i-1])) { 22 if (std::tolower(text[i-1]) != pattern[i-1]) {
23 return std::string::npos; 23 return std::string::npos;
24 } 24 }
25 } 25 }
@@ -40,12 +40,13 @@ size_t search_str_bmh(const std::string& text, const std::string& pattern) {
40 return std::string::npos; 40 return std::string::npos;
41 } 41 }
42 42
43 size_t t; 43 const size_t tlen = text.size();
44 size_t tlen = text.size(); 44 const size_t plen = pattern.size();
45 size_t t; // index in text
45 46
46 // simple case, no need to be too clever 47 // simple case, no need to be too clever
47 if (pattern.size() == 1) { 48 if (plen == 1) {
48 int b = std::tolower(pattern[0]); 49 int b = pattern[0];
49 for (t = 0; t < tlen; t++) { 50 for (t = 0; t < tlen; t++) {
50 if (b == std::tolower(text[t])) { 51 if (b == std::tolower(text[t])) {
51 return t; 52 return t;
@@ -54,28 +55,28 @@ size_t search_str_bmh(const std::string& text, const std::string& pattern) {
54 return std::string::npos; 55 return std::string::npos;
55 } 56 }
56 57
57
58 size_t plast = pattern.size() - 1;
59 size_t p;
60
61 // prepare skip-table 58 // prepare skip-table
62 // 59 //
63 size_t skip[256]; 60 size_t skip[256];
61 const size_t pe = plen - 1; // end index in pattern
62 size_t p; // index in pattern
63
64 for (p = 0; p < sizeof(skip)/sizeof(skip[0]); p++) { 64 for (p = 0; p < sizeof(skip)/sizeof(skip[0]); p++) {
65 skip[p] = plast + 1; 65 skip[p] = plen;
66 } 66 }
67 for (p = 0; p < plast; p++) { 67 for (p = 0; p < pe; p++) {
68 skip[std::tolower(pattern[p])] = plast - p; 68 skip[pattern[p]] = pe - p;
69 } 69 }
70 70
71 // match 71 // match
72 for (t = 0; t + plast < tlen; ) { 72 //
73 for (p = plast; std::tolower(text[t+p]) == std::tolower(pattern[p]); p--) { 73 for (t = 0; (t+pe) < tlen; ) {
74 for (p = pe; std::tolower(text[t+p]) == pattern[p]; p--) {
74 if (p == 0) { 75 if (p == 0) {
75 return t+p; 76 return t;
76 } 77 }
77 } 78 }
78 t += skip[std::tolower(text[t+plast])]; 79 t += skip[std::tolower(text[t+pe])];
79 } 80 }
80 81
81 return std::string::npos; 82 return std::string::npos;
@@ -117,7 +118,7 @@ void MenuSearch::clear() {
117} 118}
118 119
119void MenuSearch::add(char c) { 120void MenuSearch::add(char c) {
120 pattern.push_back(c); 121 pattern.push_back(std::tolower(c));
121} 122}
122 123
123void MenuSearch::backspace() { 124void MenuSearch::backspace() {