Added -g, --oldmouse option to support of NORMAL/BUTTON_EVENT mouse type.
[midnight-commander.git] / lib / search / regex.c
blob592d1250797076bd5aaadd43623b485805c5bee1
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,2010,2011
9 Vitaliy Filippov <vitalif@yourcmc.ru>, 2011
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software; you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be
19 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 MA 02110-1301, USA.
29 #include <config.h>
31 #include <stdlib.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.h"
36 #include "lib/strescape.h"
38 #include "lib/charsets.h"
40 #include "internal.h"
42 /*** global variables ****************************************************************************/
44 /*** file scope macro definitions ****************************************************************/
46 #define REPLACE_PREPARE_T_NOTHING_SPECIAL -1
47 #define REPLACE_PREPARE_T_REPLACE_FLAG -2
48 #define REPLACE_PREPARE_T_ESCAPE_SEQ -3
50 /*** file scope type declarations ****************************************************************/
52 typedef enum
54 REPLACE_T_NO_TRANSFORM = 0,
55 REPLACE_T_UPP_TRANSFORM_CHAR = 1,
56 REPLACE_T_LOW_TRANSFORM_CHAR = 2,
57 REPLACE_T_UPP_TRANSFORM = 4,
58 REPLACE_T_LOW_TRANSFORM = 8
59 } replace_transform_type_t;
62 /*** file scope variables ************************************************************************/
64 /*** file scope functions ************************************************************************/
66 static gboolean
67 mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str,
68 gsize * offset)
70 char *tmp_regex_str;
71 gsize spec_chr_len;
72 const char **spec_chr;
73 const char *special_chars[] = {
74 "\\s", "\\S",
75 "\\d", "\\D",
76 "\\b", "\\B",
77 "\\w", "\\W",
78 "\\t", "\\n",
79 "\\r", "\\f",
80 "\\a", "\\e",
81 "\\x", "\\X",
82 "\\c", "\\C",
83 "\\l", "\\L",
84 "\\u", "\\U",
85 "\\E", "\\Q",
86 NULL
88 spec_chr = special_chars;
90 tmp_regex_str = &(regex_str->str[*offset]);
92 while (*spec_chr)
94 spec_chr_len = strlen (*spec_chr);
95 if (!strncmp (tmp_regex_str, *spec_chr, spec_chr_len))
97 if (!strutils_is_char_escaped (regex_str->str, tmp_regex_str))
99 if (!strncmp ("\\x", *spec_chr, spec_chr_len))
101 if (*(tmp_regex_str + spec_chr_len) == '{')
103 while ((spec_chr_len < regex_str->len - *offset)
104 && *(tmp_regex_str + spec_chr_len) != '}')
105 spec_chr_len++;
106 if (*(tmp_regex_str + spec_chr_len) == '}')
107 spec_chr_len++;
109 else
110 spec_chr_len += 2;
112 g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
113 *offset += spec_chr_len;
114 return TRUE;
117 spec_chr++;
119 return FALSE;
123 /* --------------------------------------------------------------------------------------------- */
124 static void
125 mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
126 const char *one_char, gsize str_len)
128 GString *upp, *low;
129 gchar *tmp_str;
130 gsize loop;
132 upp = mc_search__toupper_case_str (charset, one_char, str_len);
133 low = mc_search__tolower_case_str (charset, one_char, str_len);
135 for (loop = 0; loop < upp->len; loop++)
138 if (loop < low->len)
140 if (upp->str[loop] == low->str[loop])
141 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
142 else
143 tmp_str =
144 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp->str[loop],
145 (unsigned char) low->str[loop]);
147 else
149 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
151 g_string_append (str_to, tmp_str);
152 g_free (tmp_str);
154 g_string_free (upp, TRUE);
155 g_string_free (low, TRUE);
158 /* --------------------------------------------------------------------------------------------- */
160 static void
161 mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
162 GString * str_from)
164 GString *recoded_part;
165 gsize loop = 0;
167 recoded_part = g_string_sized_new (32);
169 while (loop < str_from->len)
171 gchar *one_char;
172 gsize one_char_len;
173 gboolean just_letters;
175 one_char =
176 mc_search__get_one_symbol (charset, &(str_from->str[loop]),
177 min (str_from->len - loop, 6), &just_letters);
178 one_char_len = strlen (one_char);
180 if (one_char_len == 0)
181 loop++;
182 else
184 loop += one_char_len;
186 if (just_letters)
187 mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
188 one_char_len);
189 else
190 g_string_append_len (recoded_part, one_char, one_char_len);
193 g_free (one_char);
196 g_string_append (str_to, recoded_part->str);
197 g_string_free (recoded_part, TRUE);
198 g_string_set_size (str_from, 0);
201 /* --------------------------------------------------------------------------------------------- */
203 static GString *
204 mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString * astr)
206 GString *accumulator, *spec_char, *ret_str;
207 gsize loop;
209 ret_str = g_string_sized_new (64);
210 accumulator = g_string_sized_new (64);
211 spec_char = g_string_sized_new (64);
212 loop = 0;
214 while (loop <= astr->len)
216 if (mc_search__regex_str_append_if_special (spec_char, astr, &loop))
218 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
219 g_string_append_len (ret_str, spec_char->str, spec_char->len);
220 g_string_set_size (spec_char, 0);
221 continue;
224 if (astr->str[loop] == '[' && !strutils_is_char_escaped (astr->str, &(astr->str[loop])))
226 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
228 while (loop < astr->len && !(astr->str[loop] == ']'
229 && !strutils_is_char_escaped (astr->str,
230 &(astr->str[loop]))))
232 g_string_append_c (ret_str, astr->str[loop]);
233 loop++;
236 g_string_append_c (ret_str, astr->str[loop]);
237 loop++;
238 continue;
241 TODO: handle [ and ]
243 g_string_append_c (accumulator, astr->str[loop]);
244 loop++;
246 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
248 g_string_free (accumulator, TRUE);
249 g_string_free (spec_char, TRUE);
251 return ret_str;
254 /* --------------------------------------------------------------------------------------------- */
256 static mc_search__found_cond_t
257 mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t * regex,
258 GString * search_str)
260 #ifdef SEARCH_TYPE_GLIB
261 GError *error = NULL;
263 if (!g_regex_match_full
264 (regex, search_str->str, -1, 0, G_REGEX_MATCH_NEWLINE_ANY, &lc_mc_search->regex_match_info,
265 &error))
267 g_match_info_free (lc_mc_search->regex_match_info);
268 lc_mc_search->regex_match_info = NULL;
269 if (error)
271 lc_mc_search->error = MC_SEARCH_E_REGEX;
272 lc_mc_search->error_str =
273 str_conv_gerror_message (error, _("Regular expression error"));
274 g_error_free (error);
275 return COND__FOUND_ERROR;
277 return COND__NOT_FOUND;
279 lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
280 #else /* SEARCH_TYPE_GLIB */
281 lc_mc_search->num_results = pcre_exec (regex, lc_mc_search->regex_match_info,
282 search_str->str, search_str->len - 1, 0, 0,
283 lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
284 if (lc_mc_search->num_results < 0)
286 return COND__NOT_FOUND;
288 #endif /* SEARCH_TYPE_GLIB */
289 return COND__FOUND_OK;
293 /* --------------------------------------------------------------------------------------------- */
295 static mc_search__found_cond_t
296 mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
298 gsize loop1;
299 mc_search_cond_t *mc_search_cond;
300 mc_search__found_cond_t ret;
302 for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++)
304 mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
306 if (!mc_search_cond->regex_handle)
307 continue;
309 ret =
310 mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
311 search_str);
313 if (ret != COND__NOT_FOUND)
314 return ret;
316 return COND__NOT_ALL_FOUND;
319 /* --------------------------------------------------------------------------------------------- */
321 static int
322 mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
324 int max_token = 0;
325 gsize loop;
326 for (loop = 0; loop < len - 1; loop++)
328 if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1]))
330 if (strutils_is_char_escaped (str, &str[loop]))
331 continue;
332 if (max_token < str[loop + 1] - '0')
333 max_token = str[loop + 1] - '0';
334 continue;
336 if (str[loop] == '$' && str[loop + 1] == '{')
338 gsize tmp_len;
339 char *tmp_str;
340 int tmp_token;
341 if (strutils_is_char_escaped (str, &str[loop]))
342 continue;
344 for (tmp_len = 0;
345 loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
346 tmp_len++);
347 if (str[loop + 2 + tmp_len] == '}')
349 tmp_str = g_strndup (&str[loop + 2], tmp_len);
350 tmp_token = atoi (tmp_str);
351 if (max_token < tmp_token)
352 max_token = tmp_token;
353 g_free (tmp_str);
357 return max_token;
360 /* --------------------------------------------------------------------------------------------- */
362 static char *
363 mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
365 int fnd_start = 0, fnd_end = 0;
367 #ifdef SEARCH_TYPE_GLIB
368 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
369 #else /* SEARCH_TYPE_GLIB */
370 fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
371 fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
372 #endif /* SEARCH_TYPE_GLIB */
374 if (fnd_end - fnd_start == 0)
375 return NULL;
377 return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
381 /* --------------------------------------------------------------------------------------------- */
383 static gboolean
384 mc_search_regex__replace_handle_esc_seq (const GString * replace_str, const gsize current_pos,
385 gsize * skip_len, int *ret)
387 char *curr_str = &(replace_str->str[current_pos]);
388 char c = *(curr_str + 1);
390 if (replace_str->len > current_pos + 2)
392 if (c == '{')
394 for (*skip_len = 2; /* \{ */
395 current_pos + *skip_len < replace_str->len
396 && *(curr_str + *skip_len) >= '0'
397 && *(curr_str + *skip_len) <= '7'; (*skip_len)++);
398 if (current_pos + *skip_len < replace_str->len && *(curr_str + *skip_len) == '}')
400 (*skip_len)++;
401 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
402 return FALSE;
404 else
406 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
407 return TRUE;
411 if (c == 'x')
413 *skip_len = 2; /* \x */
414 c = *(curr_str + 2);
415 if (c == '{')
417 for (*skip_len = 3; /* \x{ */
418 current_pos + *skip_len < replace_str->len
419 && g_ascii_isxdigit ((guchar) * (curr_str + *skip_len)); (*skip_len)++);
420 if (current_pos + *skip_len < replace_str->len && *(curr_str + *skip_len) == '}')
422 (*skip_len)++;
423 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
424 return FALSE;
426 else
428 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
429 return TRUE;
432 else if (!g_ascii_isxdigit ((guchar) c))
434 *skip_len = 2; /* \x without number behind */
435 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
436 return FALSE;
438 else
440 c = *(curr_str + 3);
441 if (!g_ascii_isxdigit ((guchar) c))
442 *skip_len = 3; /* \xH */
443 else
444 *skip_len = 4; /* \xHH */
445 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
446 return FALSE;
451 if (strchr ("ntvbrfa", c) != NULL)
453 *skip_len = 2;
454 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
455 return FALSE;
457 return TRUE;
460 /* --------------------------------------------------------------------------------------------- */
462 static int
463 mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
464 gsize * skip_len, replace_transform_type_t * replace_flags)
466 int ret = -1;
467 char *tmp_str;
468 const char *curr_str = &(replace_str->str[current_pos]);
470 if (current_pos > replace_str->len)
471 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
473 *skip_len = 0;
475 if ((*curr_str == '$') && (*(curr_str + 1) == '{') && ((*(curr_str + 2) & (char) 0xf0) == 0x30)
476 && (replace_str->len > current_pos + 2))
478 if (strutils_is_char_escaped (replace_str->str, curr_str))
480 *skip_len = 1;
481 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
484 for (*skip_len = 0;
485 current_pos + *skip_len + 2 < replace_str->len
486 && (*(curr_str + 2 + *skip_len) & (char) 0xf0) == 0x30; (*skip_len)++);
488 if (*(curr_str + 2 + *skip_len) != '}')
489 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
491 tmp_str = g_strndup (curr_str + 2, *skip_len);
492 if (tmp_str == NULL)
493 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
495 ret = atoi (tmp_str);
496 g_free (tmp_str);
498 *skip_len += 3; /* ${} */
499 return ret; /* capture buffer index >= 0 */
502 if ((*curr_str == '\\') && (replace_str->len > current_pos + 1))
504 if (strutils_is_char_escaped (replace_str->str, curr_str))
506 *skip_len = 1;
507 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
510 if (g_ascii_isdigit (*(curr_str + 1)))
512 ret = g_ascii_digit_value (*(curr_str + 1)); /* capture buffer index >= 0 */
513 *skip_len = 2; /* \\ and one digit */
514 return ret;
517 if (!mc_search_regex__replace_handle_esc_seq (replace_str, current_pos, skip_len, &ret))
518 return ret;
520 ret = REPLACE_PREPARE_T_REPLACE_FLAG;
521 *skip_len += 2;
522 switch (*(curr_str + 1))
524 case 'U':
525 *replace_flags |= REPLACE_T_UPP_TRANSFORM;
526 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
527 break;
528 case 'u':
529 *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
530 break;
531 case 'L':
532 *replace_flags |= REPLACE_T_LOW_TRANSFORM;
533 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
534 break;
535 case 'l':
536 *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
537 break;
538 case 'E':
539 *replace_flags = REPLACE_T_NO_TRANSFORM;
540 break;
541 default:
542 ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
543 break;
546 return ret;
549 /* --------------------------------------------------------------------------------------------- */
551 static void
552 mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
553 replace_transform_type_t * replace_flags)
555 gsize loop = 0;
556 gsize char_len;
557 char *tmp_str;
558 GString *tmp_string;
560 if (len == (gsize) - 1)
561 len = strlen (from);
563 if (*replace_flags == REPLACE_T_NO_TRANSFORM)
565 g_string_append_len (dest_str, from, len);
566 return;
568 while (loop < len)
570 tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
571 char_len = strlen (tmp_str);
572 if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR)
574 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
575 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
576 g_string_append (dest_str, tmp_string->str);
577 g_string_free (tmp_string, TRUE);
580 else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR)
582 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
583 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
584 g_string_append (dest_str, tmp_string->str);
585 g_string_free (tmp_string, TRUE);
588 else if (*replace_flags & REPLACE_T_UPP_TRANSFORM)
590 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
591 g_string_append (dest_str, tmp_string->str);
592 g_string_free (tmp_string, TRUE);
595 else if (*replace_flags & REPLACE_T_LOW_TRANSFORM)
597 tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
598 g_string_append (dest_str, tmp_string->str);
599 g_string_free (tmp_string, TRUE);
602 else
604 g_string_append (dest_str, tmp_str);
606 g_free (tmp_str);
607 loop += char_len;
612 /* --------------------------------------------------------------------------------------------- */
614 static void
615 mc_search_regex__process_escape_sequence (GString * dest_str, const char *from, gsize len,
616 replace_transform_type_t * replace_flags,
617 gboolean is_utf8)
619 gsize i = 0;
620 unsigned int c = 0;
621 char b;
623 if (len == (gsize) (-1))
624 len = strlen (from);
625 if (len == 0)
626 return;
627 if (from[i] == '{')
628 i++;
629 if (i >= len)
630 return;
632 if (from[i] == 'x')
634 i++;
635 if (i < len && from[i] == '{')
636 i++;
637 for (; i < len; i++)
639 if (from[i] >= '0' && from[i] <= '9')
640 c = c * 16 + from[i] - '0';
641 else if (from[i] >= 'a' && from[i] <= 'f')
642 c = c * 16 + 10 + from[i] - 'a';
643 else if (from[i] >= 'A' && from[i] <= 'F')
644 c = c * 16 + 10 + from[i] - 'A';
645 else
646 break;
649 else if (from[i] >= '0' && from[i] <= '7')
650 for (; i < len && from[i] >= '0' && from[i] <= '7'; i++)
651 c = c * 8 + from[i] - '0';
652 else
654 switch (from[i])
656 case 'n':
657 c = '\n';
658 break;
659 case 't':
660 c = '\t';
661 break;
662 case 'v':
663 c = '\v';
664 break;
665 case 'b':
666 c = '\b';
667 break;
668 case 'r':
669 c = '\r';
670 break;
671 case 'f':
672 c = '\f';
673 break;
674 case 'a':
675 c = '\a';
676 break;
677 default:
678 mc_search_regex__process_append_str (dest_str, from, len, replace_flags);
679 return;
683 if (c < 0x80 || !is_utf8)
684 g_string_append_c (dest_str, (char) c);
685 else if (c < 0x800)
687 b = 0xC0 | (c >> 6);
688 g_string_append_c (dest_str, b);
689 b = 0x80 | (c & 0x3F);
690 g_string_append_c (dest_str, b);
692 else if (c < 0x10000)
694 b = 0xE0 | (c >> 12);
695 g_string_append_c (dest_str, b);
696 b = 0x80 | ((c >> 6) & 0x3F);
697 g_string_append_c (dest_str, b);
698 b = 0x80 | (c & 0x3F);
699 g_string_append_c (dest_str, b);
701 else if (c < 0x10FFFF)
703 b = 0xF0 | (c >> 16);
704 g_string_append_c (dest_str, b);
705 b = 0x80 | ((c >> 12) & 0x3F);
706 g_string_append_c (dest_str, b);
707 b = 0x80 | ((c >> 6) & 0x3F);
708 g_string_append_c (dest_str, b);
709 b = 0x80 | (c & 0x3F);
710 g_string_append_c (dest_str, b);
714 /* --------------------------------------------------------------------------------------------- */
715 /*** public functions ****************************************************************************/
716 /* --------------------------------------------------------------------------------------------- */
718 void
719 mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
720 mc_search_cond_t * mc_search_cond)
722 #ifdef SEARCH_TYPE_GLIB
723 GError *error = NULL;
725 if (!lc_mc_search->is_case_sensitive)
727 GString *tmp;
729 tmp = mc_search_cond->str;
730 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
731 g_string_free (tmp, TRUE);
733 mc_search_cond->regex_handle =
734 g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL,
735 0, &error);
737 if (error != NULL)
739 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
740 lc_mc_search->error_str = str_conv_gerror_message (error, _("Regular expression error"));
741 g_error_free (error);
742 return;
744 #else /* SEARCH_TYPE_GLIB */
745 const char *error;
746 int erroffset;
747 int pcre_options = PCRE_EXTRA | PCRE_MULTILINE;
749 if (str_isutf8 (charset))
751 pcre_options |= PCRE_UTF8;
752 if (lc_mc_search->is_case_sensitive)
753 pcre_options |= PCRE_CASELESS;
755 else
757 if (!lc_mc_search->is_case_sensitive)
759 GString *tmp;
761 tmp = mc_search_cond->str;
762 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
763 g_string_free (tmp, TRUE);
767 mc_search_cond->regex_handle =
768 pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
769 if (mc_search_cond->regex_handle == NULL)
771 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
772 lc_mc_search->error_str = g_strdup (error);
773 return;
775 lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
776 if (lc_mc_search->regex_match_info == NULL)
778 if (error)
780 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
781 lc_mc_search->error_str = g_strdup (error);
782 g_free (mc_search_cond->regex_handle);
783 mc_search_cond->regex_handle = NULL;
784 return;
787 #endif /* SEARCH_TYPE_GLIB */
788 lc_mc_search->is_utf8 = str_isutf8 (charset);
791 /* --------------------------------------------------------------------------------------------- */
793 gboolean
794 mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
795 gsize start_search, gsize end_search, gsize * found_len)
797 gsize current_pos, virtual_pos;
798 int current_chr = 0;
799 gint start_pos;
800 gint end_pos;
802 if (lc_mc_search->regex_buffer != NULL)
803 g_string_free (lc_mc_search->regex_buffer, TRUE);
805 lc_mc_search->regex_buffer = g_string_sized_new (64);
807 virtual_pos = current_pos = start_search;
808 while (virtual_pos <= end_search)
810 g_string_set_size (lc_mc_search->regex_buffer, 0);
811 lc_mc_search->start_buffer = current_pos;
813 while (1)
815 current_chr = mc_search__get_char (lc_mc_search, user_data, current_pos);
816 if (current_chr == MC_SEARCH_CB_ABORT)
817 break;
819 if (current_chr == MC_SEARCH_CB_INVALID)
820 continue;
822 current_pos++;
824 if (current_chr == MC_SEARCH_CB_SKIP)
825 continue;
827 virtual_pos++;
829 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
832 if (current_chr == 0 || (char) current_chr == '\n')
833 break;
835 if (virtual_pos > end_search)
836 break;
839 switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
841 case COND__FOUND_OK:
842 #ifdef SEARCH_TYPE_GLIB
843 if (lc_mc_search->whole_words)
845 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 2, &start_pos, &end_pos);
847 else
849 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
851 #else /* SEARCH_TYPE_GLIB */
852 if (lc_mc_search->whole_words)
854 start_pos = lc_mc_search->iovector[4];
855 end_pos = lc_mc_search->iovector[5];
857 else
859 start_pos = lc_mc_search->iovector[0];
860 end_pos = lc_mc_search->iovector[1];
862 #endif /* SEARCH_TYPE_GLIB */
863 if (found_len)
864 *found_len = end_pos - start_pos;
865 lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
866 return TRUE;
867 case COND__NOT_ALL_FOUND:
868 break;
869 default:
870 g_string_free (lc_mc_search->regex_buffer, TRUE);
871 lc_mc_search->regex_buffer = NULL;
872 return FALSE;
874 if ((lc_mc_search->update_fn != NULL) &&
875 ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
876 current_chr = MC_SEARCH_CB_ABORT;
878 if (current_chr == MC_SEARCH_CB_ABORT)
879 break;
881 g_string_free (lc_mc_search->regex_buffer, TRUE);
882 lc_mc_search->regex_buffer = NULL;
883 lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
885 if (current_chr != MC_SEARCH_CB_ABORT)
886 lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
887 else
888 lc_mc_search->error_str = NULL;
890 return FALSE;
893 /* --------------------------------------------------------------------------------------------- */
895 GString *
896 mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
898 GString *ret;
899 gchar *tmp_str;
901 int num_replace_tokens, lc_index;
902 gsize loop;
903 gsize len = 0;
904 gchar *prev_str;
905 replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
907 num_replace_tokens =
908 mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
910 if (lc_mc_search->num_results < 0)
911 return g_string_new_len (replace_str->str, replace_str->len);
913 if (num_replace_tokens > lc_mc_search->num_results - 1
914 || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
916 lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
917 lc_mc_search->error_str = g_strdup (_(STR_E_RPL_NOT_EQ_TO_FOUND));
918 return NULL;
921 ret = g_string_sized_new (64);
922 prev_str = replace_str->str;
924 for (loop = 0; loop < replace_str->len - 1; loop++)
926 lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
928 if (lc_index == REPLACE_PREPARE_T_NOTHING_SPECIAL)
930 if (len != 0)
932 mc_search_regex__process_append_str (ret, prev_str,
933 replace_str->str - prev_str + loop,
934 &replace_flags);
935 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
936 &replace_flags);
937 prev_str = replace_str->str + loop + len;
938 loop += len - 1;
940 continue;
943 if (lc_index == REPLACE_PREPARE_T_REPLACE_FLAG)
945 if (loop)
946 mc_search_regex__process_append_str (ret, prev_str,
947 replace_str->str - prev_str + loop,
948 &replace_flags);
949 prev_str = replace_str->str + loop + len;
950 loop += len - 1;
951 continue;
954 /* escape sequence */
955 if (lc_index == REPLACE_PREPARE_T_ESCAPE_SEQ)
957 mc_search_regex__process_append_str (ret, prev_str,
958 replace_str->str + loop - prev_str,
959 &replace_flags);
960 /* call process_escape_sequence without starting '\\' */
961 mc_search_regex__process_escape_sequence (ret, replace_str->str + loop + 1, len - 1,
962 &replace_flags, lc_mc_search->is_utf8);
963 prev_str = replace_str->str + loop + len;
964 loop += len - 1;
965 continue;
968 /* invalid capture buffer number */
969 if (lc_index > lc_mc_search->num_results)
971 g_string_free (ret, TRUE);
972 lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
973 lc_mc_search->error_str = g_strdup_printf (_(STR_E_RPL_INVALID_TOKEN), lc_index);
974 return NULL;
977 tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
978 if (tmp_str == NULL)
979 continue;
981 if (loop)
982 mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
983 &replace_flags);
984 prev_str = replace_str->str + loop + len;
986 mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
987 g_free (tmp_str);
988 loop += len - 1;
990 mc_search_regex__process_append_str (ret, prev_str,
991 replace_str->str - prev_str + replace_str->len,
992 &replace_flags);
994 return ret;