Code

use glib regex for list_window_jump.
[ncmpc.git] / src / list_window.c
index a98176fbc937610ff20c000b79133e3bc5527346..9d120c14e24eb97d2ce17994364cc830cfe78409 100644 (file)
@@ -127,6 +127,9 @@ list_window_resize(struct list_window *lw, unsigned width, unsigned height)
 void
 list_window_set_length(struct list_window *lw, unsigned length)
 {
+       if (length == lw->length)
+               return;
+
        lw->length = length;
 
        list_window_check_selected(lw);
@@ -486,44 +489,60 @@ list_window_rfind(struct list_window *lw,
        return false;
 }
 
-static bool
-jump_match(const char *haystack, const char *needle)
-{
 #ifdef NCMPC_MINI
-       bool jump_prefix_only = true;
-#else
-       bool jump_prefix_only = options.jump_prefix_only;
-#endif
+bool
+list_window_jump(struct list_window *lw,
+                list_window_callback_fn_t callback,
+                void *callback_data,
+                const char *str)
+{
+       unsigned i;
+       const char *label;
+
+       assert(str != NULL);
 
-       assert(haystack != NULL);
-       assert(needle != NULL);
+       for (i = 0; i < lw->length; i++) {
+               label = callback(i, callback_data);
+               assert(label != NULL);
 
-       return jump_prefix_only
-               ? g_ascii_strncasecmp(haystack, needle, strlen(needle)) == 0
-               : match_line(haystack, needle);
+               if (g_ascii_strncasecmp(label, str, strlen(str)) == 0) {
+                       list_window_move_cursor(lw, i);
+                       return true;
+               }
+       }
+       return false;
 }
-
+#else
 bool
 list_window_jump(struct list_window *lw,
                 list_window_callback_fn_t callback,
                 void *callback_data,
                 const char *str)
 {
+       unsigned i;
        const char *label;
+       GRegex *regex;
 
        assert(str != NULL);
 
-       for (unsigned i = 0; i < lw->length; ++i) {
+       regex = compile_regex(str, options.jump_prefix_only);
+       if (regex == NULL)
+               return false;
+
+       for (i = 0; i < lw->length; i++) {
                label = callback(i, callback_data);
                assert(label != NULL);
 
-               if (jump_match(label, str)) {
+               if (match_regex(regex, label)) {
+                       g_regex_unref(regex);
                        list_window_move_cursor(lw, i);
                        return true;
                }
        }
+       g_regex_unref(regex);
        return false;
 }
+#endif
 
 /* perform basic list window commands (movement) */
 bool