Ticket #400: 400-001-search-introduce-slurp-mode.patch

File 400-001-search-introduce-slurp-mode.patch, 7.3 KB (added by mooffie, 8 years ago)
  • lib/search.h

    From 6e6f05b78ff8a4bdb45f7bf0679a778c6210c17f Mon Sep 17 00:00:00 2001
    From: Mooffie <mooffie@gmail.com>
    Date: Tue, 6 Dec 2016 22:26:33 +0200
    Subject: [PATCH 1/3] Ticket #400: search: introduce slurp mode.
    
    (The "get around sloppy callers" comment refers to an imperfection in the
    editor's backwards-search in which the 1st iteration turns out to have
    start_search==(end_search+1).)
    ---
     lib/search.h        | 14 ++++++++++++++
     lib/search/lib.c    |  3 +++
     lib/search/regex.c  |  5 +++--
     lib/search/search.c | 21 +++++++++++++++++++++
     src/setup.c         | 30 ++++++++++++++++++++++++++++++
     5 files changed, 71 insertions(+), 2 deletions(-)
    
    diff --git a/lib/search.h b/lib/search.h
    index 6e79609..d2a199f 100644
    a b typedef enum 
    3737    MC_SEARCH_E_REGEX, 
    3838    MC_SEARCH_E_REGEX_REPLACE, 
    3939    MC_SEARCH_E_NOTFOUND, 
     40    MC_SEARCH_E_SLURP_MODE_MAXED, 
    4041    MC_SEARCH_E_ABORT 
    4142} mc_search_error_t; 
    4243 
    typedef struct mc_search_struct 
    8182    /* search entire string (from begin to end). Used only with GLOB search type */ 
    8283    gboolean is_entire_line; 
    8384 
     85    /* 
     86     * Whether to accumulate the entire haystack (the data to search in) 
     87     * into one buffer before searching it. This makes it possible to 
     88     * search across lines. 
     89     * 
     90     * Normally the haystack is accumulated till a "\n" is encountered, 
     91     * repeatedly. This limits the scope of the search to one line only. 
     92     */ 
     93    gboolean slurp_mode; 
     94 
    8495    /* function, used for getting data. NULL if not used */ 
    8596    mc_search_fn search_fn; 
    8697 
    typedef struct mc_search_type_str_struct 
    133144/* Error messages */ 
    134145extern const char *STR_E_NOTFOUND; 
    135146extern const char *STR_E_UNKNOWN_TYPE; 
     147extern const char *STR_E_SLURP_MODE_MAXED; 
    136148extern const char *STR_E_RPL_NOT_EQ_TO_FOUND; 
    137149extern const char *STR_E_RPL_INVALID_TOKEN; 
    138150 
    gboolean mc_search (const gchar * pattern, const gchar * pattern_charset, const 
    167179int mc_search_getstart_result_by_num (mc_search_t *, int); 
    168180int mc_search_getend_result_by_num (mc_search_t *, int); 
    169181 
     182void mc_search_set_slurp_mode_max (gsize max); 
     183 
    170184/* *INDENT-OFF* */ 
    171185void mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...) 
    172186     G_GNUC_PRINTF (3, 4); 
  • lib/search/lib.c

    diff --git a/lib/search/lib.c b/lib/search/lib.c
    index 5d086f1..8e6c362 100644
    a b const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet"); 
    4646const char *STR_E_RPL_NOT_EQ_TO_FOUND = 
    4747N_("Num of replace tokens not equal to num of found tokens"); 
    4848const char *STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d"); 
     49const char *STR_E_SLURP_MODE_MAXED = N_("Buffer is too large:\n" 
     50                                        "Searches across lines, or searches for 0x0A bytes,\n" 
     51                                        "are limited to buffers of size %d MB."); 
    4952 
    5053/*** file scope macro definitions ****************************************************************/ 
    5154 
  • lib/search/regex.c

    diff --git a/lib/search/regex.c b/lib/search/regex.c
    index e8d896c..070d463 100644
    a b mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data, 
    912912 
    913913                g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr); 
    914914 
    915                 if ((char) current_chr == '\n' || virtual_pos > end_search) 
     915                if (((char) current_chr == '\n' && !lc_mc_search->slurp_mode) 
     916                    || virtual_pos > end_search) 
    916917                    break; 
    917918            } 
    918919        } 
    mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data, 
    931932 
    932933                current_pos++; 
    933934 
    934                 if (current_chr == '\n' || current_pos > end_search) 
     935                if ((current_chr == '\n' && !lc_mc_search->slurp_mode) || current_pos > end_search) 
    935936                    break; 
    936937            } 
    937938 
  • lib/search/search.c

    diff --git a/lib/search/search.c b/lib/search/search.c
    index 2e19ccf..f52dfc3 100644
    a b static const mc_search_type_str_t mc_search__list_types[] = { 
    5757    {NULL, MC_SEARCH_T_INVALID} 
    5858}; 
    5959 
     60/* The maximal buffer size to allocate for slurp mode. */ 
     61static gsize slurp_mode_max = 0; 
     62 
    6063/*** file scope functions ************************************************************************/ 
    6164 
    6265static mc_search_cond_t * 
    mc_search_prepare (mc_search_t * lc_mc_search) 
    263266 
    264267/* --------------------------------------------------------------------------------------------- */ 
    265268 
     269void 
     270mc_search_set_slurp_mode_max (gsize max) 
     271{ 
     272    slurp_mode_max = max; 
     273} 
     274 
     275/* --------------------------------------------------------------------------------------------- */ 
     276 
    266277/** 
    267278 * Carries out the search. 
    268279 * 
    mc_search_run (mc_search_t * lc_mc_search, const void *user_data, 
    302313    if ((lc_mc_search->conditions == NULL) && !mc_search_prepare (lc_mc_search)) 
    303314        return FALSE; 
    304315 
     316    /* This has to come after mc_search_prepare(), as mc_search__cond_struct_new_init_XXX() 
     317     * may elect to turn slurp_mode on or off. */ 
     318    if (lc_mc_search->slurp_mode && end_search > start_search   /* get around sloppy callers */ 
     319        && end_search - start_search > slurp_mode_max) 
     320    { 
     321        mc_search_set_error (lc_mc_search, MC_SEARCH_E_SLURP_MODE_MAXED, _(STR_E_SLURP_MODE_MAXED), 
     322                             (int) (slurp_mode_max / 1024 / 1024)); 
     323        return FALSE; 
     324    } 
     325 
    305326    switch (lc_mc_search->search_type) 
    306327    { 
    307328    case MC_SEARCH_T_NORMAL: 
  • src/setup.c

    diff --git a/src/setup.c b/src/setup.c
    index 951edb8..1861d9b 100644
    a b  
    3737#include "lib/tty/tty.h" 
    3838#include "lib/tty/key.h" 
    3939#include "lib/mcconfig.h" 
     40#include "lib/search.h"         /* mc_search_set_slurp_mode_max() */ 
    4041#include "lib/fileloc.h" 
    4142#include "lib/timefmt.h" 
    4243#include "lib/util.h" 
    save_panel_types (void) 
    951952} 
    952953 
    953954/* --------------------------------------------------------------------------------------------- */ 
     955 
     956/** 
     957 * Returns the user's preference for the largest buffer for some text 
     958 * operations. 
     959 * 
     960 * Code copied from the editor's edit.c:check_file_access(). 
     961 * 
     962 * @todo: make the editor use this function. 
     963 */ 
     964static uintmax_t 
     965get_filesize_memory_threshold (void) 
     966{ 
     967    static uintmax_t threshold = UINTMAX_MAX; 
     968 
     969    if (threshold == UINTMAX_MAX) 
     970    { 
     971        gboolean err = FALSE; 
     972 
     973        threshold = parse_integer (option_filesize_threshold, &err); 
     974        if (err) 
     975            threshold = 64 * 1024 * 1024;       /* @todo: move edit.c's constant here. */ 
     976    } 
     977 
     978    return threshold; 
     979} 
     980 
     981/* --------------------------------------------------------------------------------------------- */ 
    954982/*** public functions ****************************************************************************/ 
    955983/* --------------------------------------------------------------------------------------------- */ 
    956984 
    load_setup (void) 
    11461174        mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_store", ""); 
    11471175    clipboard_paste_path = 
    11481176        mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_paste", ""); 
     1177 
     1178    mc_search_set_slurp_mode_max ((gsize) get_filesize_memory_threshold ()); 
    11491179} 
    11501180 
    11511181/* --------------------------------------------------------------------------------------------- */