Ticket #400: support multi-line search.

In order to use multiline search, select "Regular expression" mode and
use "\n" in the input line:

For example, if file contains lines

aaa
bbb

search string should be "aaa\nbbb".

As a side effect, non-printable ASCII symbols (\r, \t, etc) in the
search string can be used too.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Steef Boerrigter 2022-08-07 18:35:48 +03:00 committed by Andrew Borodin
parent 1a2682dfab
commit e370818c09
3 changed files with 162 additions and 3 deletions

View File

@ -902,7 +902,7 @@ mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
{ {
while (TRUE) while (TRUE)
{ {
int current_chr = '\n'; /* stop search symbol */ int current_chr = '\0'; /* stop search symbol */
ret = lc_mc_search->search_fn (user_data, current_pos, &current_chr); ret = lc_mc_search->search_fn (user_data, current_pos, &current_chr);
@ -921,7 +921,7 @@ mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr); g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
if ((char) current_chr == '\n' || virtual_pos > end_search) if ((char) current_chr == '\0' || virtual_pos >= end_search)
break; break;
} }
} }
@ -940,7 +940,7 @@ mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
current_pos++; current_pos++;
if (current_chr == '\n' || current_pos > end_search) if (current_pos >= end_search)
break; break;
} }

View File

@ -20,6 +20,7 @@ TESTS = \
hex_translate_to_regex \ hex_translate_to_regex \
regex_replace_esc_seq \ regex_replace_esc_seq \
regex_process_escape_sequence \ regex_process_escape_sequence \
regex_multiline_str \
translate_replace_glob_to_regex translate_replace_glob_to_regex
check_PROGRAMS = $(TESTS) check_PROGRAMS = $(TESTS)
@ -33,6 +34,9 @@ regex_replace_esc_seq_SOURCES = \
regex_process_escape_sequence_SOURCES = \ regex_process_escape_sequence_SOURCES = \
regex_process_escape_sequence.c regex_process_escape_sequence.c
regex_multiline_str_SOURCES = \
regex_multiline_str.c
translate_replace_glob_to_regex_SOURCES = \ translate_replace_glob_to_regex_SOURCES = \
translate_replace_glob_to_regex.c translate_replace_glob_to_regex.c

View File

@ -0,0 +1,155 @@
/*
libmc - checks search functions
Copyright (C) 2022
Free Software Foundation, Inc.
Written by:
Steef Boerrigter <sxmboer@gmail.com>, 2022
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define TEST_SUITE_NAME "lib/search/search"
#include "tests/mctest.h"
#include "search.c" /* for testing static functions */
/* --------------------------------------------------------------------------------------------- */
/* @DataSource("test_regex_multiline_str_ds") */
/* *INDENT-OFF* */
static const struct test_regex_multiline_str_ds
{
const mc_search_type_t type;
const char *buffer;
const char *search_str;
const gboolean retval;
} test_regex_multiline_str_ds[] =
{
{ /* 1. */
MC_SEARCH_T_NORMAL,
"abcdefgh",
"nope",
FALSE
},
{ /* 2. */
MC_SEARCH_T_NORMAL,
"abcdefgh",
"def",
TRUE
},
{ /* 3. */
MC_SEARCH_T_NORMAL,
"abcdefgh",
"z",
FALSE
},
{ /* 4. */
MC_SEARCH_T_NORMAL,
"abcd \tefgh",
"\t ",
TRUE
},
{ /* 5. multiline */
MC_SEARCH_T_NORMAL,
"abcd\ne\nfgh",
"\ne\nf",
TRUE
},
{ /* 6. */
MC_SEARCH_T_NORMAL,
"abcd\nefgh",
"d\ne",
TRUE
},
{ /* 7. mc-4.8.28 fails this edge condition */
MC_SEARCH_T_NORMAL,
"abcdefgh\n",
"\n",
TRUE
},
{ /* 8. mc-4.8.28 fails this edge condition */
MC_SEARCH_T_NORMAL,
"abcdefgh\n",
"\n\n",
FALSE
},
{ /* 9. regex including newline */
MC_SEARCH_T_REGEX,
"abcd\nefgh",
"abc[c-e]\nef",
TRUE
},
{ /* 10. regex's newline support */
MC_SEARCH_T_REGEX,
"abcd\nefgh",
"abc[c-e]\\nef",
TRUE
},
};
/* *INDENT-ON* */
/* @Test(dataSource = "test_regex_multiline_str_ds") */
/* *INDENT-OFF* */
START_PARAMETRIZED_TEST (test_regex_multiline_str, test_regex_multiline_str_ds)
/* *INDENT-ON* */
{
/* given */
mc_search_t *s;
gboolean retval;
s = mc_search_new (data->buffer, NULL);
s->is_case_sensitive = FALSE;
s->search_type = data->type;
/* when */
retval = mc_search (data->search_str, DEFAULT_CHARSET, data->buffer, s->search_type);
/* then */
if (data->retval)
{
mctest_assert_true (retval);
}
else
{
mctest_assert_false (retval);
}
mc_search_free (s);
}
/* *INDENT-OFF* */
END_PARAMETRIZED_TEST
/* *INDENT-ON* */
/* --------------------------------------------------------------------------------------------- */
int
main (void)
{
TCase *tc_core;
tc_core = tcase_create ("Core");
/* Add new tests here: *************** */
mctest_add_parameterized_test (tc_core, test_regex_multiline_str, test_regex_multiline_str_ds);
/* *********************************** */
return mctest_run_all (tc_core);
}
/* --------------------------------------------------------------------------------------------- */