From: Matt Portas Date: Mon, 16 Mar 2009 16:13:33 +0000 (+0100) Subject: list_window: search in strings with the "jump" command X-Git-Tag: release-0.14~58 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=b36fbc3ed8b9ceed41e289410cbdcff853b906d3;p=ncmpc.git list_window: search in strings with the "jump" command This patch extends the jump function. Current jump only searches the start of the lines, but with this patch it searches through the whole line for a match. Since this takes more cpu power, I have inlined it back to the original behavior in MINI. --- diff --git a/src/list_window.c b/src/list_window.c index 40fd179..fedae31 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -416,7 +416,7 @@ list_window_jump(struct list_window *lw, while ((label = callback(i,&h,callback_data))) { if (label && label[0] != '[') { - if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0) { + if (str && label && find_occurence(label, str, strlen(str)) == 0) { lw->selected = i; if(!lw->visual_selection || i > lw->selected_end) lw->selected_end = i; @@ -425,7 +425,7 @@ list_window_jump(struct list_window *lw, return true; } } - else if (str && label && g_ascii_strncasecmp(label+1, str, strlen(str)) == 0) { + else if (str && label && find_occurence(label+1, str, strlen(str)) == 0) { lw->selected = i; if(!lw->visual_selection || i > lw->selected_end) lw->selected_end = i; diff --git a/src/match.c b/src/match.c index c97cde5..313c76e 100644 --- a/src/match.c +++ b/src/match.c @@ -22,6 +22,7 @@ #include #include +#include static char * locale_casefold(const char *src) @@ -49,3 +50,29 @@ match_line(const char *line, const char *needle) return ret; } + +int +find_occurence(const char *str_orig, const char *str_occur, const int str_occur_len) +{ + const int str_orig_len = strlen (str_orig); + int i, j; + + if (str_occur_len > str_orig_len) + return -1; + + for (i = 0; i < str_orig_len; i++) { + if ((i + str_occur_len) > str_orig_len) + return -1; + + for (j = 0; j < str_occur_len; j++) { + if (tolower (str_occur[j]) != tolower (str_orig[i+j])) + break; + + if (j == str_occur_len - 1) + return 0; + } + } + + return -1; +} + diff --git a/src/match.h b/src/match.h index d42f14d..4362cce 100644 --- a/src/match.h +++ b/src/match.h @@ -34,6 +34,12 @@ match_line(const char *line, const char *needle) return strstr(line, needle) != NULL; } +static inline int +find_occurence(const char *str_orig, const char *str_occur, const int str_occur_len) +{ + return g_ascii_strncasecmp(str_orig, str_occur, str_occur_len); +} + #else /** @@ -43,6 +49,9 @@ match_line(const char *line, const char *needle) bool match_line(const char *line, const char *needle); +int +find_occurence(const char *str_orig, const char *str_occur, const int str_occur_len); + #endif #endif