Removed mc_global.args structure.
[midnight-commander.git] / lib / search / normal.c
blob581935899de0c7a74b1d2eb2489e68e2261b0830
1 /*
2 Search text engine.
3 Plain search
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
30 #include "lib/global.h"
31 #include "lib/strutil.h"
32 #include "lib/search.h"
34 #include "lib/charsets.h"
36 #include "internal.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 /*** file scope functions ************************************************************************/
48 static GString *
49 mc_search__normal_translate_to_regex (const GString * astr)
51 const char *str = astr->str;
52 GString *buff;
53 gsize loop;
55 buff = g_string_sized_new (32);
57 for (loop = 0; loop < astr->len; loop++)
58 switch (str[loop])
60 case '*':
61 case '?':
62 case ',':
63 case '{':
64 case '}':
65 case '[':
66 case ']':
67 case '\\':
68 case '+':
69 case '.':
70 case '$':
71 case '(':
72 case ')':
73 case '^':
74 case '-':
75 case '|':
76 g_string_append_c (buff, '\\');
77 /* fall through */
78 default:
79 g_string_append_c (buff, str[loop]);
80 break;
83 return buff;
86 /*** public functions ****************************************************************************/
88 void
89 mc_search__cond_struct_new_init_normal (const char *charset, mc_search_t * lc_mc_search,
90 mc_search_cond_t * mc_search_cond)
92 GString *tmp;
94 tmp = mc_search__normal_translate_to_regex (mc_search_cond->str);
95 g_string_free (mc_search_cond->str, TRUE);
97 if (lc_mc_search->whole_words)
99 /* NOTE: \b as word boundary doesn't allow search
100 * whole words with non-ASCII symbols */
101 g_string_prepend (tmp, "(^|[^\\p{L}\\p{N}_])(");
102 g_string_append (tmp, ")([^\\p{L}\\p{N}_]|$)");
105 mc_search_cond->str = tmp;
106 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
109 /* --------------------------------------------------------------------------------------------- */
111 gboolean
112 mc_search__run_normal (mc_search_t * lc_mc_search, const void *user_data,
113 gsize start_search, gsize end_search, gsize * found_len)
115 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
118 /* --------------------------------------------------------------------------------------------- */
119 GString *
120 mc_search_normal_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
122 (void) lc_mc_search;
123 return g_string_new_len (replace_str->str, replace_str->len);