mc_search__cond_struct_new_regex_ci_str(): get rid of extra string duplication.
[midnight-commander.git] / lib / search / regex.c
blob2e40f6035088e06ffba4d2f35d36d10acd6a637f
1 /*
2 Search text engine.
3 Regex 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 <stdlib.h>
32 #include "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/search.h"
35 #include "lib/strescape.h"
37 #include "src/charsets.h"
39 #include "internal.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 typedef enum
49 REPLACE_T_NO_TRANSFORM = 0,
50 REPLACE_T_UPP_TRANSFORM_CHAR = 1,
51 REPLACE_T_LOW_TRANSFORM_CHAR = 2,
52 REPLACE_T_UPP_TRANSFORM = 4,
53 REPLACE_T_LOW_TRANSFORM = 8
54 } replace_transform_type_t;
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
60 static gboolean
61 mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str, gsize * offset)
63 char *tmp_regex_str;
64 gsize spec_chr_len;
65 const char **spec_chr;
66 const char *special_chars[] = {
67 "\\s", "\\S",
68 "\\d", "\\D",
69 "\\b", "\\B",
70 "\\w", "\\W",
71 "\\t", "\\n",
72 "\\r", "\\f",
73 "\\a", "\\e",
74 "\\x", "\\X",
75 "\\c", "\\C",
76 "\\l", "\\L",
77 "\\u", "\\U",
78 "\\E", "\\Q",
79 NULL
81 spec_chr = special_chars;
83 tmp_regex_str = &(regex_str->str[*offset]);
85 while (*spec_chr)
87 spec_chr_len = strlen (*spec_chr);
88 if (!strncmp (tmp_regex_str, *spec_chr, spec_chr_len))
90 if (!strutils_is_char_escaped (regex_str->str, tmp_regex_str))
92 if (!strncmp ("\\x", *spec_chr, spec_chr_len))
94 if (*(tmp_regex_str + spec_chr_len) == '{')
96 while ((spec_chr_len < regex_str->len - *offset)
97 && *(tmp_regex_str + spec_chr_len) != '}')
98 spec_chr_len++;
99 if (*(tmp_regex_str + spec_chr_len) == '}')
100 spec_chr_len++;
102 else
103 spec_chr_len += 2;
105 g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
106 *offset += spec_chr_len;
107 return TRUE;
110 spec_chr++;
112 return FALSE;
116 /* --------------------------------------------------------------------------------------------- */
117 static void
118 mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
119 const char *one_char, gsize str_len)
121 GString *upp, *low;
122 gchar *tmp_str;
123 gsize loop;
125 upp = mc_search__toupper_case_str (charset, one_char, str_len);
126 low = mc_search__tolower_case_str (charset, one_char, str_len);
128 for (loop = 0; loop < upp->len; loop++)
131 if (loop < low->len)
133 if (upp->str[loop] == low->str[loop])
134 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
135 else
136 tmp_str =
137 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp->str[loop],
138 (unsigned char) low->str[loop]);
140 else
142 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
144 g_string_append (str_to, tmp_str);
145 g_free (tmp_str);
147 g_string_free (upp, TRUE);
148 g_string_free (low, TRUE);
151 /* --------------------------------------------------------------------------------------------- */
153 static void
154 mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
155 GString * str_from)
157 GString *recoded_part;
158 gsize loop = 0;
160 recoded_part = g_string_sized_new (32);
162 while (loop < str_from->len)
164 gchar *one_char;
165 gsize one_char_len;
166 gboolean just_letters;
168 one_char =
169 mc_search__get_one_symbol (charset, &(str_from->str[loop]),
170 min (str_from->len - loop, 6), &just_letters);
171 one_char_len = strlen (one_char);
173 if (one_char_len == 0)
174 loop++;
175 else
177 loop += one_char_len;
179 if (just_letters)
180 mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
181 one_char_len);
182 else
183 g_string_append_len (recoded_part, one_char, one_char_len);
186 g_free (one_char);
189 g_string_append (str_to, recoded_part->str);
190 g_string_free (recoded_part, TRUE);
191 g_string_set_size (str_from, 0);
194 /* --------------------------------------------------------------------------------------------- */
196 static GString *
197 mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *astr)
199 GString *accumulator, *spec_char, *ret_str;
200 gsize loop;
202 ret_str = g_string_new ("");
203 accumulator = g_string_new ("");
204 spec_char = g_string_new ("");
205 loop = 0;
207 while (loop <= astr->len)
209 if (mc_search__regex_str_append_if_special (spec_char, astr, &loop))
211 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
212 g_string_append_len (ret_str, spec_char->str, spec_char->len);
213 g_string_set_size (spec_char, 0);
214 continue;
217 if (astr->str[loop] == '[' && !strutils_is_char_escaped (astr->str, &(astr->str[loop])))
219 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
221 while (loop < astr->len && !(astr->str[loop] == ']'
222 && !strutils_is_char_escaped (astr->str, &(astr->str[loop]))))
224 g_string_append_c (ret_str, astr->str[loop]);
225 loop++;
228 g_string_append_c (ret_str, astr->str[loop]);
229 loop++;
230 continue;
233 TODO: handle [ and ]
235 g_string_append_c (accumulator, astr->str[loop]);
236 loop++;
238 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
240 g_string_free (accumulator, TRUE);
241 g_string_free (spec_char, TRUE);
243 return ret_str;
246 /* --------------------------------------------------------------------------------------------- */
248 static mc_search__found_cond_t
249 mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t * regex,
250 GString * search_str)
252 #ifdef SEARCH_TYPE_GLIB
253 GError *error = NULL;
255 if (!g_regex_match_full
256 (regex, search_str->str, -1, 0, G_REGEX_MATCH_NEWLINE_ANY, &lc_mc_search->regex_match_info,
257 &error))
259 g_match_info_free (lc_mc_search->regex_match_info);
260 lc_mc_search->regex_match_info = NULL;
261 if (error)
263 lc_mc_search->error = MC_SEARCH_E_REGEX;
264 lc_mc_search->error_str =
265 str_conv_gerror_message (error, _("Regular expression error"));
266 g_error_free (error);
267 return COND__FOUND_ERROR;
269 return COND__NOT_FOUND;
271 lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
272 #else /* SEARCH_TYPE_GLIB */
273 lc_mc_search->num_results = pcre_exec (regex, lc_mc_search->regex_match_info,
274 search_str->str, search_str->len - 1, 0, 0,
275 lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
276 if (lc_mc_search->num_results < 0)
278 return COND__NOT_FOUND;
280 #endif /* SEARCH_TYPE_GLIB */
281 return COND__FOUND_OK;
285 /* --------------------------------------------------------------------------------------------- */
287 static mc_search__found_cond_t
288 mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
290 gsize loop1;
291 mc_search_cond_t *mc_search_cond;
292 mc_search__found_cond_t ret;
294 for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++)
296 mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
298 if (!mc_search_cond->regex_handle)
299 continue;
301 ret =
302 mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
303 search_str);
305 if (ret != COND__NOT_FOUND)
306 return ret;
308 return COND__NOT_ALL_FOUND;
311 /* --------------------------------------------------------------------------------------------- */
313 static int
314 mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
316 int max_token = 0;
317 gsize loop;
318 for (loop = 0; loop < len - 1; loop++)
320 if (str[loop] == '\\' && g_ascii_isdigit(str[loop + 1]) )
322 if (strutils_is_char_escaped (str, &str[loop]))
323 continue;
324 if (max_token < str[loop + 1] - '0')
325 max_token = str[loop + 1] - '0';
326 continue;
328 if (str[loop] == '$' && str[loop + 1] == '{')
330 gsize tmp_len;
331 char *tmp_str;
332 int tmp_token;
333 if (strutils_is_char_escaped (str, &str[loop]))
334 continue;
336 for (tmp_len = 0;
337 loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
338 tmp_len++);
339 if (str[loop + 2 + tmp_len] == '}')
341 tmp_str = g_strndup (&str[loop + 2], tmp_len);
342 tmp_token = atoi (tmp_str);
343 if (max_token < tmp_token)
344 max_token = tmp_token;
345 g_free (tmp_str);
349 return max_token;
352 /* --------------------------------------------------------------------------------------------- */
354 static char *
355 mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
357 int fnd_start = 0, fnd_end = 0;
359 #ifdef SEARCH_TYPE_GLIB
360 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
361 #else /* SEARCH_TYPE_GLIB */
362 fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
363 fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
364 #endif /* SEARCH_TYPE_GLIB */
366 if (fnd_end - fnd_start == 0)
367 return NULL;
369 return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
373 /* --------------------------------------------------------------------------------------------- */
374 static int
375 mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
376 gsize * skip_len, replace_transform_type_t * replace_flags)
378 int ret = -1;
379 char *tmp_str;
380 const char *curr_str = &(replace_str->str[current_pos]);
382 if (current_pos > replace_str->len)
383 return -1;
385 *skip_len = 0;
387 if (*curr_str == '$' && *(curr_str + 1) == '{' && (*(curr_str + 2) & (char) 0xf0) == 0x30)
389 if (strutils_is_char_escaped (replace_str->str, curr_str))
391 *skip_len = 1;
392 return -1;
395 for (*skip_len = 0;
396 current_pos + *skip_len + 2 < replace_str->len
397 && (*(curr_str + 2 + *skip_len) & (char) 0xf0) == 0x30; (*skip_len)++);
399 if (*(curr_str + 2 + *skip_len) != '}')
400 return -1;
402 tmp_str = g_strndup (curr_str + 2, *skip_len);
403 if (tmp_str == NULL)
404 return -1;
406 ret = atoi (tmp_str);
407 g_free (tmp_str);
409 *skip_len += 3; /* ${} */
410 return ret;
413 if (*curr_str == '\\')
415 if (strutils_is_char_escaped (replace_str->str, curr_str))
417 *skip_len = 1;
418 return -1;
421 if ( g_ascii_isdigit(*(curr_str + 1)))
423 ret = g_ascii_digit_value (*(curr_str + 1));
424 *skip_len = 2; /* \\ and one digit */
425 return ret;
427 ret = -2;
428 *skip_len += 2;
429 switch (*(curr_str + 1))
431 case 'U':
432 *replace_flags |= REPLACE_T_UPP_TRANSFORM;
433 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
434 break;
435 case 'u':
436 *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
437 break;
438 case 'L':
439 *replace_flags |= REPLACE_T_LOW_TRANSFORM;
440 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
441 break;
442 case 'l':
443 *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
444 break;
445 case 'E':
446 *replace_flags = REPLACE_T_NO_TRANSFORM;
447 break;
448 default:
449 ret = -1;
450 break;
453 return ret;
455 static void
456 mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
457 replace_transform_type_t * replace_flags)
459 gsize loop = 0;
460 gsize char_len;
461 char *tmp_str;
462 GString *tmp_string;
464 if (len == (gsize) - 1)
465 len = strlen (from);
467 if (*replace_flags == REPLACE_T_NO_TRANSFORM)
469 g_string_append_len (dest_str, from, len);
470 return;
472 while (loop < len)
474 tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
475 char_len = strlen (tmp_str);
476 if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR)
478 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
479 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
480 g_string_append (dest_str, tmp_string->str);
481 g_string_free (tmp_string, TRUE);
484 else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR)
486 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
487 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
488 g_string_append (dest_str, tmp_string->str);
489 g_string_free (tmp_string, TRUE);
492 else if (*replace_flags & REPLACE_T_UPP_TRANSFORM)
494 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
495 g_string_append (dest_str, tmp_string->str);
496 g_string_free (tmp_string, TRUE);
499 else if (*replace_flags & REPLACE_T_LOW_TRANSFORM)
501 tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
502 g_string_append (dest_str, tmp_string->str);
503 g_string_free (tmp_string, TRUE);
506 else
508 g_string_append (dest_str, tmp_str);
510 g_free (tmp_str);
511 loop += char_len;
516 /*** public functions ****************************************************************************/
518 void
519 mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
520 mc_search_cond_t * mc_search_cond)
522 #ifdef SEARCH_TYPE_GLIB
523 GError *error = NULL;
524 #else /* SEARCH_TYPE_GLIB */
525 const char *error;
526 int erroffset;
527 #endif /* SEARCH_TYPE_GLIB */
529 if (!lc_mc_search->is_case_sensitive)
531 GString *tmp;
533 tmp = mc_search_cond->str;
534 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
535 g_string_free (tmp, TRUE);
537 #ifdef SEARCH_TYPE_GLIB
538 mc_search_cond->regex_handle =
539 g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL, 0,
540 &error);
542 if (error != NULL)
544 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
545 lc_mc_search->error_str = str_conv_gerror_message (error, _("Regular expression error"));
546 g_error_free (error);
547 return;
549 #else /* SEARCH_TYPE_GLIB */
550 mc_search_cond->regex_handle =
551 pcre_compile (mc_search_cond->str->str, PCRE_EXTRA, &error, &erroffset, NULL);
552 if (mc_search_cond->regex_handle == NULL)
554 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
555 lc_mc_search->error_str = g_strdup (error);
556 return;
558 lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
559 if (lc_mc_search->regex_match_info == NULL)
561 if (error)
563 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
564 lc_mc_search->error_str = g_strdup (error);
565 g_free (mc_search_cond->regex_handle);
566 mc_search_cond->regex_handle = NULL;
567 return;
570 #endif /* SEARCH_TYPE_GLIB */
573 /* --------------------------------------------------------------------------------------------- */
575 gboolean
576 mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
577 gsize start_search, gsize end_search, gsize * found_len)
579 gsize current_pos, virtual_pos;
580 int current_chr = 0;
581 gint start_pos;
582 gint end_pos;
584 if (lc_mc_search->regex_buffer != NULL)
585 g_string_free (lc_mc_search->regex_buffer, TRUE);
587 lc_mc_search->regex_buffer = g_string_new ("");
589 virtual_pos = current_pos = start_search;
590 while (virtual_pos <= end_search)
592 g_string_set_size (lc_mc_search->regex_buffer, 0);
593 lc_mc_search->start_buffer = current_pos;
595 while (1)
597 current_chr = mc_search__get_char (lc_mc_search, user_data, current_pos);
598 if (current_chr == MC_SEARCH_CB_ABORT)
599 break;
601 current_pos++;
603 if (current_chr == MC_SEARCH_CB_SKIP)
604 continue;
606 virtual_pos++;
608 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
611 if (current_chr == 0 || (char) current_chr == '\n')
612 break;
614 if (virtual_pos > end_search)
615 break;
618 switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
620 case COND__FOUND_OK:
621 #ifdef SEARCH_TYPE_GLIB
622 if (lc_mc_search->whole_words)
624 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 2, &start_pos, &end_pos);
626 else
628 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
630 #else /* SEARCH_TYPE_GLIB */
631 if (lc_mc_search->whole_words)
633 start_pos = lc_mc_search->iovector[4];
634 end_pos = lc_mc_search->iovector[5];
636 else
638 start_pos = lc_mc_search->iovector[0];
639 end_pos = lc_mc_search->iovector[1];
641 #endif /* SEARCH_TYPE_GLIB */
642 if (found_len)
643 *found_len = end_pos - start_pos;
644 lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
645 return TRUE;
646 case COND__NOT_ALL_FOUND:
647 break;
648 default:
649 g_string_free (lc_mc_search->regex_buffer, TRUE);
650 lc_mc_search->regex_buffer = NULL;
651 return FALSE;
653 if ((lc_mc_search->update_fn != NULL) &&
654 ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
655 current_chr = MC_SEARCH_CB_ABORT;
657 if (current_chr == MC_SEARCH_CB_ABORT)
658 break;
660 g_string_free (lc_mc_search->regex_buffer, TRUE);
661 lc_mc_search->regex_buffer = NULL;
662 lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
664 if (current_chr != MC_SEARCH_CB_ABORT)
665 lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
666 else
667 lc_mc_search->error_str = NULL;
669 return FALSE;
672 /* --------------------------------------------------------------------------------------------- */
673 GString *
674 mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
676 GString *ret;
677 gchar *tmp_str;
679 int num_replace_tokens, lc_index;
680 gsize loop;
681 gsize len = 0;
682 gchar *prev_str;
683 replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
685 num_replace_tokens =
686 mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
688 if (lc_mc_search->num_results < 0)
689 return g_string_new_len (replace_str->str, replace_str->len);
691 if (num_replace_tokens > lc_mc_search->num_results - 1
692 || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
694 lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
695 lc_mc_search->error_str = g_strdup (_(STR_E_RPL_NOT_EQ_TO_FOUND));
696 return NULL;
699 ret = g_string_new ("");
700 prev_str = replace_str->str;
701 for (loop = 0; loop < replace_str->len - 1; loop++)
703 lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
705 if (lc_index == -1)
707 if (len != 0)
709 mc_search_regex__process_append_str (ret, prev_str,
710 replace_str->str - prev_str + loop,
711 &replace_flags);
712 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
713 &replace_flags);
714 prev_str = replace_str->str + loop + len;
715 loop += len - 1;
717 continue;
720 if (lc_index == -2)
722 if (loop)
723 mc_search_regex__process_append_str (ret, prev_str,
724 replace_str->str - prev_str + loop,
725 &replace_flags);
726 prev_str = replace_str->str + loop + len;
727 loop += len - 1;
728 continue;
731 if (lc_index > lc_mc_search->num_results)
733 g_string_free (ret, TRUE);
734 lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
735 lc_mc_search->error_str = g_strdup_printf (_(STR_E_RPL_INVALID_TOKEN), lc_index);
736 return NULL;
739 tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
740 if (tmp_str == NULL)
741 continue;
743 if (loop)
744 mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
745 &replace_flags);
746 prev_str = replace_str->str + loop + len;
748 mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
749 g_free (tmp_str);
750 loop += len - 1;
752 mc_search_regex__process_append_str (ret, prev_str,
753 replace_str->str - prev_str + replace_str->len,
754 &replace_flags);
756 return ret;