From bb2f5d35f3a005effb72ab9dc030a5495e9b0a4a Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Fri, 9 Aug 2013 09:22:24 +0400 Subject: [PATCH] (mc_search__run_regex): optimization ...for case where there is no MC_SEARCH_CB_INVALID or MC_SEARCH_CB_SKIP return codes (for search from file manager), so we can copy line at regex buffer all at once. Thanks Sergey Naumov for the original patch. Signed-off-by: Andrew Borodin --- lib/search/internal.h | 2 -- lib/search/lib.c | 21 +++------------ lib/search/regex.c | 61 +++++++++++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/lib/search/internal.h b/lib/search/internal.h index 47a3d3145..bbd38b62c 100644 --- a/lib/search/internal.h +++ b/lib/search/internal.h @@ -47,8 +47,6 @@ gchar *mc_search__recode_str (const char *, gsize, const char *, const char *, g gchar *mc_search__get_one_symbol (const char *, const char *, gsize, gboolean *); -mc_search_cbret_t mc_search__get_char (mc_search_t *, const void *, gsize, int *); - GString *mc_search__tolower_case_str (const char *, const char *, gsize); GString *mc_search__toupper_case_str (const char *, const char *, gsize); diff --git a/lib/search/lib.c b/lib/search/lib.c index 2a1c38098..dd5cbfdf0 100644 --- a/lib/search/lib.c +++ b/lib/search/lib.c @@ -2,11 +2,12 @@ Search text engine. Common share code for module. - Copyright (C) 2009, 2011 + Copyright (C) 2009, 2011, 2013 The Free Software Foundation, Inc. Written by: - Slava Zanko , 2009. + Slava Zanko , 2009, 2011 + Andrew Borodin , 2013 This file is part of the Midnight Commander. @@ -140,22 +141,6 @@ mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len, /* --------------------------------------------------------------------------------------------- */ -mc_search_cbret_t -mc_search__get_char (mc_search_t * lc_mc_search, const void *user_data, gsize current_pos, - int *current_char) -{ - unsigned char *data; - - if (lc_mc_search->search_fn != NULL) - return lc_mc_search->search_fn (user_data, current_pos, current_char); - - data = (unsigned char *) user_data; - *current_char = (int) data[current_pos]; - return (*current_char == 0) ? MC_SEARCH_CB_ABORT : MC_SEARCH_CB_OK; -} - -/* --------------------------------------------------------------------------------------------- */ - GString * mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len) { diff --git a/lib/search/regex.c b/lib/search/regex.c index 7431d5649..ce85842cd 100644 --- a/lib/search/regex.c +++ b/lib/search/regex.c @@ -2,12 +2,13 @@ Search text engine. Regex search - Copyright (C) 2009, 2011 + Copyright (C) 2009, 2011, 2013 The Free Software Foundation, Inc. Written by: - Slava Zanko , 2009,2010,2011 + Slava Zanko , 2009, 2010, 2011 Vitaliy Filippov , 2011 + Andrew Borodin , 2013 This file is part of the Midnight Commander. @@ -806,28 +807,56 @@ mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data, g_string_set_size (lc_mc_search->regex_buffer, 0); lc_mc_search->start_buffer = current_pos; - while (TRUE) + if (lc_mc_search->search_fn != NULL) { - int current_chr = '\n'; /* stop search symbol */ + int current_chr; - ret = mc_search__get_char (lc_mc_search, user_data, current_pos, ¤t_chr); - if (ret == MC_SEARCH_CB_ABORT) - break; + do + { + /* stop search symbol */ + current_chr = '\n'; - if (ret == MC_SEARCH_CB_INVALID) - continue; + ret = lc_mc_search->search_fn (user_data, current_pos, ¤t_chr); - current_pos++; + if (ret == MC_SEARCH_CB_ABORT) + break; - if (ret == MC_SEARCH_CB_SKIP) - continue; + if (ret == MC_SEARCH_CB_INVALID) + continue; - virtual_pos++; + current_pos++; - g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr); + if (ret == MC_SEARCH_CB_SKIP) + continue; - if ((char) current_chr == '\n' || virtual_pos > end_search) - break; + virtual_pos++; + + g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr); + } + while ((char) current_chr != '\n' && virtual_pos <= end_search); + } + else + { + char current_chr; + + /* optimization for standard case (for search from file manager) + * where there is no MC_SEARCH_CB_INVALID or MC_SEARCH_CB_SKIP + * return codes, so we can copy line at regex buffer all at once + */ + do + { + current_chr = ((char *) user_data)[current_pos]; + if (current_chr == '\0') + break; + + current_pos++; + } + while (current_chr != '\n' && current_pos <= end_search); + + /* use virtual_pos as index of start of current chunk */ + g_string_append_len (lc_mc_search->regex_buffer, (char *) user_data + virtual_pos, + current_pos - virtual_pos); + virtual_pos = current_pos; } switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))