Create strings with small preallocated sizes.
[pantumic.git] / lib / search / glob.c
blob6e99076007ac5538bca97049e1a9b828234089eb
1 /*
2 Search text engine.
3 Glob-style pattern matching
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>
31 #include "lib/global.h"
32 #include "lib/strutil.h"
33 #include "lib/search.h"
34 #include "lib/strescape.h"
36 #include "src/charsets.h"
38 #include "internal.h"
40 /*** global variables ****************************************************************************/
42 /*** file scope macro definitions ****************************************************************/
44 /*** file scope type declarations ****************************************************************/
46 /*** file scope variables ************************************************************************/
48 /*** file scope functions ************************************************************************/
50 static GString *
51 mc_search__glob_translate_to_regex (const GString * astr)
53 const char *str = astr->str;
54 GString *buff;
55 gsize loop = 0;
56 gboolean inside_group = FALSE;
58 buff = g_string_sized_new (32);
60 while (loop < astr->len)
62 switch (str[loop])
64 case '*':
65 if (!strutils_is_char_escaped (str, &(str[loop])))
67 g_string_append (buff, (inside_group) ? ".*" : "(.*)");
68 loop++;
69 continue;
71 break;
72 case '?':
73 if (!strutils_is_char_escaped (str, &(str[loop])))
75 g_string_append (buff, (inside_group) ? "." : "(.)");
76 loop++;
77 continue;
79 break;
80 case ',':
81 if (!strutils_is_char_escaped (str, &(str[loop])))
83 g_string_append (buff, "|");
84 loop++;
85 continue;
87 break;
88 case '{':
89 if (!strutils_is_char_escaped (str, &(str[loop])))
91 g_string_append (buff, "(");
92 inside_group = TRUE;
93 loop++;
94 continue;
96 break;
97 case '}':
98 if (!strutils_is_char_escaped (str, &(str[loop])))
100 g_string_append (buff, ")");
101 inside_group = FALSE;
102 loop++;
103 continue;
105 break;
106 case '+':
107 case '.':
108 case '$':
109 case '(':
110 case ')':
111 case '^':
112 g_string_append_c (buff, '\\');
113 g_string_append_c (buff, str[loop]);
114 loop++;
115 continue;
117 g_string_append_c (buff, str[loop]);
118 loop++;
121 return buff;
124 /* --------------------------------------------------------------------------------------------- */
126 static GString *
127 mc_search__translate_replace_glob_to_regex (gchar * str)
129 GString *buff;
130 int cnt = '0';
131 gboolean escaped_mode = FALSE;
133 buff = g_string_sized_new (32);
135 while (*str)
137 char c = *str++;
138 switch (c)
140 case '\\':
141 if (!escaped_mode)
143 escaped_mode = TRUE;
145 g_string_append_c (buff, c);
146 continue;
147 case '*':
148 case '?':
149 if (!escaped_mode)
151 g_string_append_c (buff, '\\');
152 c = ++cnt;
154 break;
155 case '&':
156 g_string_append_c (buff, '\\');
157 break;
159 g_string_append_c (buff, c);
160 escaped_mode = FALSE;
162 return buff;
165 /*** public functions ****************************************************************************/
167 void
168 mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_search,
169 mc_search_cond_t * mc_search_cond)
171 GString *tmp;
173 tmp = mc_search__glob_translate_to_regex (mc_search_cond->str);
174 g_string_free (mc_search_cond->str, TRUE);
176 if (lc_mc_search->is_entire_line)
178 g_string_prepend_c (tmp, '^');
179 g_string_append_c (tmp, '$');
181 mc_search_cond->str = tmp;
183 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
186 /* --------------------------------------------------------------------------------------------- */
188 gboolean
189 mc_search__run_glob (mc_search_t * lc_mc_search, const void *user_data,
190 gsize start_search, gsize end_search, gsize * found_len)
192 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
195 /* --------------------------------------------------------------------------------------------- */
197 GString *
198 mc_search_glob_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
200 GString *repl = mc_search__translate_replace_glob_to_regex (replace_str->str);
201 GString *res = mc_search_regex_prepare_replace_str (lc_mc_search, repl);
202 g_string_free (repl, TRUE);
203 return res;
206 /* --------------------------------------------------------------------------------------------- */