(query_dialog): add horizontal line.
[midnight-commander.git] / lib / search / regex.c
blobf5e1fc7623a7290a540e045addf76c4171b7e32a
1 /*
2 Search text engine.
3 Regex search
5 Copyright (C) 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009,2010,2011
10 Vitaliy Filippov <vitalif@yourcmc.ru>, 2011
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 "internal.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 #define REPLACE_PREPARE_T_NOTHING_SPECIAL -1
44 #define REPLACE_PREPARE_T_REPLACE_FLAG -2
45 #define REPLACE_PREPARE_T_ESCAPE_SEQ -3
47 /*** file scope type declarations ****************************************************************/
49 typedef enum
51 REPLACE_T_NO_TRANSFORM = 0,
52 REPLACE_T_UPP_TRANSFORM_CHAR = 1,
53 REPLACE_T_LOW_TRANSFORM_CHAR = 2,
54 REPLACE_T_UPP_TRANSFORM = 4,
55 REPLACE_T_LOW_TRANSFORM = 8
56 } replace_transform_type_t;
59 /*** file scope variables ************************************************************************/
61 /*** file scope functions ************************************************************************/
63 static gboolean
64 mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str,
65 gsize * offset)
67 char *tmp_regex_str;
68 gsize spec_chr_len;
69 const char **spec_chr;
70 const char *special_chars[] = {
71 "\\s", "\\S",
72 "\\d", "\\D",
73 "\\b", "\\B",
74 "\\w", "\\W",
75 "\\t", "\\n",
76 "\\r", "\\f",
77 "\\a", "\\e",
78 "\\x", "\\X",
79 "\\c", "\\C",
80 "\\l", "\\L",
81 "\\u", "\\U",
82 "\\E", "\\Q",
83 NULL
85 spec_chr = special_chars;
87 tmp_regex_str = &(regex_str->str[*offset]);
89 while (*spec_chr)
91 spec_chr_len = strlen (*spec_chr);
92 if (!strncmp (tmp_regex_str, *spec_chr, spec_chr_len))
94 if (!strutils_is_char_escaped (regex_str->str, tmp_regex_str))
96 if (!strncmp ("\\x", *spec_chr, spec_chr_len))
98 if (*(tmp_regex_str + spec_chr_len) == '{')
100 while ((spec_chr_len < regex_str->len - *offset)
101 && *(tmp_regex_str + spec_chr_len) != '}')
102 spec_chr_len++;
103 if (*(tmp_regex_str + spec_chr_len) == '}')
104 spec_chr_len++;
106 else
107 spec_chr_len += 2;
109 g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
110 *offset += spec_chr_len;
111 return TRUE;
114 spec_chr++;
116 return FALSE;
120 /* --------------------------------------------------------------------------------------------- */
121 static void
122 mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
123 const char *one_char, gsize str_len)
125 GString *upp, *low;
126 gchar *tmp_str;
127 gsize loop;
129 upp = mc_search__toupper_case_str (charset, one_char, str_len);
130 low = mc_search__tolower_case_str (charset, one_char, str_len);
132 for (loop = 0; loop < upp->len; loop++)
135 if (loop < low->len)
137 if (upp->str[loop] == low->str[loop])
138 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
139 else
140 tmp_str =
141 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp->str[loop],
142 (unsigned char) low->str[loop]);
144 else
146 tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
148 g_string_append (str_to, tmp_str);
149 g_free (tmp_str);
151 g_string_free (upp, TRUE);
152 g_string_free (low, TRUE);
155 /* --------------------------------------------------------------------------------------------- */
157 static void
158 mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
159 GString * str_from)
161 GString *recoded_part;
162 gsize loop = 0;
164 recoded_part = g_string_sized_new (32);
166 while (loop < str_from->len)
168 gchar *one_char;
169 gsize one_char_len;
170 gboolean just_letters;
172 one_char =
173 mc_search__get_one_symbol (charset, &(str_from->str[loop]),
174 min (str_from->len - loop, 6), &just_letters);
175 one_char_len = strlen (one_char);
177 if (one_char_len == 0)
178 loop++;
179 else
181 loop += one_char_len;
183 if (just_letters)
184 mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
185 one_char_len);
186 else
187 g_string_append_len (recoded_part, one_char, one_char_len);
190 g_free (one_char);
193 g_string_append (str_to, recoded_part->str);
194 g_string_free (recoded_part, TRUE);
195 g_string_set_size (str_from, 0);
198 /* --------------------------------------------------------------------------------------------- */
200 static GString *
201 mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString * astr)
203 GString *accumulator, *spec_char, *ret_str;
204 gsize loop;
206 ret_str = g_string_sized_new (64);
207 accumulator = g_string_sized_new (64);
208 spec_char = g_string_sized_new (64);
209 loop = 0;
211 while (loop <= astr->len)
213 if (mc_search__regex_str_append_if_special (spec_char, astr, &loop))
215 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
216 g_string_append_len (ret_str, spec_char->str, spec_char->len);
217 g_string_set_size (spec_char, 0);
218 continue;
221 if (astr->str[loop] == '[' && !strutils_is_char_escaped (astr->str, &(astr->str[loop])))
223 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
225 while (loop < astr->len && !(astr->str[loop] == ']'
226 && !strutils_is_char_escaped (astr->str,
227 &(astr->str[loop]))))
229 g_string_append_c (ret_str, astr->str[loop]);
230 loop++;
233 g_string_append_c (ret_str, astr->str[loop]);
234 loop++;
235 continue;
238 TODO: handle [ and ]
240 g_string_append_c (accumulator, astr->str[loop]);
241 loop++;
243 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
245 g_string_free (accumulator, TRUE);
246 g_string_free (spec_char, TRUE);
248 return ret_str;
251 /* --------------------------------------------------------------------------------------------- */
253 static mc_search__found_cond_t
254 mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t * regex,
255 GString * search_str)
257 #ifdef SEARCH_TYPE_GLIB
258 GError *error = NULL;
260 if (!g_regex_match_full (regex, search_str->str, search_str->len, 0, G_REGEX_MATCH_NEWLINE_ANY,
261 &lc_mc_search->regex_match_info, &error))
263 g_match_info_free (lc_mc_search->regex_match_info);
264 lc_mc_search->regex_match_info = NULL;
265 if (error)
267 lc_mc_search->error = MC_SEARCH_E_REGEX;
268 lc_mc_search->error_str =
269 str_conv_gerror_message (error, _("Regular expression error"));
270 g_error_free (error);
271 return COND__FOUND_ERROR;
273 return COND__NOT_FOUND;
275 lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
276 #else /* SEARCH_TYPE_GLIB */
277 lc_mc_search->num_results = pcre_exec (regex, lc_mc_search->regex_match_info,
278 search_str->str, search_str->len, 0, 0,
279 lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
280 if (lc_mc_search->num_results < 0)
282 return COND__NOT_FOUND;
284 #endif /* SEARCH_TYPE_GLIB */
285 return COND__FOUND_OK;
289 /* --------------------------------------------------------------------------------------------- */
291 static mc_search__found_cond_t
292 mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
294 gsize loop1;
295 mc_search_cond_t *mc_search_cond;
296 mc_search__found_cond_t ret;
298 for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++)
300 mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
302 if (!mc_search_cond->regex_handle)
303 continue;
305 ret =
306 mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
307 search_str);
309 if (ret != COND__NOT_FOUND)
310 return ret;
312 return COND__NOT_ALL_FOUND;
315 /* --------------------------------------------------------------------------------------------- */
317 static int
318 mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
320 int max_token = 0;
321 gsize loop;
322 for (loop = 0; loop < len - 1; loop++)
324 if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1]))
326 if (strutils_is_char_escaped (str, &str[loop]))
327 continue;
328 if (max_token < str[loop + 1] - '0')
329 max_token = str[loop + 1] - '0';
330 continue;
332 if (str[loop] == '$' && str[loop + 1] == '{')
334 gsize tmp_len;
335 char *tmp_str;
336 int tmp_token;
337 if (strutils_is_char_escaped (str, &str[loop]))
338 continue;
340 for (tmp_len = 0;
341 loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
342 tmp_len++);
343 if (str[loop + 2 + tmp_len] == '}')
345 tmp_str = g_strndup (&str[loop + 2], tmp_len);
346 tmp_token = atoi (tmp_str);
347 if (max_token < tmp_token)
348 max_token = tmp_token;
349 g_free (tmp_str);
353 return max_token;
356 /* --------------------------------------------------------------------------------------------- */
358 static char *
359 mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
361 int fnd_start = 0, fnd_end = 0;
363 #ifdef SEARCH_TYPE_GLIB
364 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
365 #else /* SEARCH_TYPE_GLIB */
366 fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
367 fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
368 #endif /* SEARCH_TYPE_GLIB */
370 if (fnd_end - fnd_start == 0)
371 return NULL;
373 return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
377 /* --------------------------------------------------------------------------------------------- */
379 static gboolean
380 mc_search_regex__replace_handle_esc_seq (const GString * replace_str, const gsize current_pos,
381 gsize * skip_len, int *ret)
383 char *curr_str = &(replace_str->str[current_pos]);
384 char c = *(curr_str + 1);
386 if (replace_str->len > current_pos + 2)
388 if (c == '{')
390 for (*skip_len = 2; /* \{ */
391 current_pos + *skip_len < replace_str->len
392 && *(curr_str + *skip_len) >= '0'
393 && *(curr_str + *skip_len) <= '7'; (*skip_len)++);
394 if (current_pos + *skip_len < replace_str->len && *(curr_str + *skip_len) == '}')
396 (*skip_len)++;
397 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
398 return FALSE;
400 else
402 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
403 return TRUE;
407 if (c == 'x')
409 *skip_len = 2; /* \x */
410 c = *(curr_str + 2);
411 if (c == '{')
413 for (*skip_len = 3; /* \x{ */
414 current_pos + *skip_len < replace_str->len
415 && g_ascii_isxdigit ((guchar) * (curr_str + *skip_len)); (*skip_len)++);
416 if (current_pos + *skip_len < replace_str->len && *(curr_str + *skip_len) == '}')
418 (*skip_len)++;
419 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
420 return FALSE;
422 else
424 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
425 return TRUE;
428 else if (!g_ascii_isxdigit ((guchar) c))
430 *skip_len = 2; /* \x without number behind */
431 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
432 return FALSE;
434 else
436 c = *(curr_str + 3);
437 if (!g_ascii_isxdigit ((guchar) c))
438 *skip_len = 3; /* \xH */
439 else
440 *skip_len = 4; /* \xHH */
441 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
442 return FALSE;
447 if (strchr ("ntvbrfa", c) != NULL)
449 *skip_len = 2;
450 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
451 return FALSE;
453 return TRUE;
456 /* --------------------------------------------------------------------------------------------- */
458 static int
459 mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
460 gsize * skip_len, replace_transform_type_t * replace_flags)
462 int ret = -1;
463 char *tmp_str;
464 const char *curr_str = &(replace_str->str[current_pos]);
466 if (current_pos > replace_str->len)
467 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
469 *skip_len = 0;
471 if ((*curr_str == '$') && (*(curr_str + 1) == '{') && ((*(curr_str + 2) & (char) 0xf0) == 0x30)
472 && (replace_str->len > current_pos + 2))
474 if (strutils_is_char_escaped (replace_str->str, curr_str))
476 *skip_len = 1;
477 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
480 for (*skip_len = 0;
481 current_pos + *skip_len + 2 < replace_str->len
482 && (*(curr_str + 2 + *skip_len) & (char) 0xf0) == 0x30; (*skip_len)++);
484 if (*(curr_str + 2 + *skip_len) != '}')
485 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
487 tmp_str = g_strndup (curr_str + 2, *skip_len);
488 if (tmp_str == NULL)
489 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
491 ret = atoi (tmp_str);
492 g_free (tmp_str);
494 *skip_len += 3; /* ${} */
495 return ret; /* capture buffer index >= 0 */
498 if ((*curr_str == '\\') && (replace_str->len > current_pos + 1))
500 if (strutils_is_char_escaped (replace_str->str, curr_str))
502 *skip_len = 1;
503 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
506 if (g_ascii_isdigit (*(curr_str + 1)))
508 ret = g_ascii_digit_value (*(curr_str + 1)); /* capture buffer index >= 0 */
509 *skip_len = 2; /* \\ and one digit */
510 return ret;
513 if (!mc_search_regex__replace_handle_esc_seq (replace_str, current_pos, skip_len, &ret))
514 return ret;
516 ret = REPLACE_PREPARE_T_REPLACE_FLAG;
517 *skip_len += 2;
518 switch (*(curr_str + 1))
520 case 'U':
521 *replace_flags |= REPLACE_T_UPP_TRANSFORM;
522 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
523 break;
524 case 'u':
525 *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
526 break;
527 case 'L':
528 *replace_flags |= REPLACE_T_LOW_TRANSFORM;
529 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
530 break;
531 case 'l':
532 *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
533 break;
534 case 'E':
535 *replace_flags = REPLACE_T_NO_TRANSFORM;
536 break;
537 default:
538 ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
539 break;
542 return ret;
545 /* --------------------------------------------------------------------------------------------- */
547 static void
548 mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
549 replace_transform_type_t * replace_flags)
551 gsize loop = 0;
552 gsize char_len;
553 char *tmp_str;
554 GString *tmp_string;
556 if (len == (gsize) - 1)
557 len = strlen (from);
559 if (*replace_flags == REPLACE_T_NO_TRANSFORM)
561 g_string_append_len (dest_str, from, len);
562 return;
564 while (loop < len)
566 tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
567 char_len = strlen (tmp_str);
568 if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR)
570 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
571 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
572 g_string_append (dest_str, tmp_string->str);
573 g_string_free (tmp_string, TRUE);
576 else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR)
578 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
579 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
580 g_string_append (dest_str, tmp_string->str);
581 g_string_free (tmp_string, TRUE);
584 else if (*replace_flags & REPLACE_T_UPP_TRANSFORM)
586 tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
587 g_string_append (dest_str, tmp_string->str);
588 g_string_free (tmp_string, TRUE);
591 else if (*replace_flags & REPLACE_T_LOW_TRANSFORM)
593 tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
594 g_string_append (dest_str, tmp_string->str);
595 g_string_free (tmp_string, TRUE);
598 else
600 g_string_append (dest_str, tmp_str);
602 g_free (tmp_str);
603 loop += char_len;
608 /* --------------------------------------------------------------------------------------------- */
610 static void
611 mc_search_regex__process_escape_sequence (GString * dest_str, const char *from, gsize len,
612 replace_transform_type_t * replace_flags,
613 gboolean is_utf8)
615 gsize i = 0;
616 unsigned int c = 0;
617 char b;
619 if (len == (gsize) (-1))
620 len = strlen (from);
621 if (len == 0)
622 return;
623 if (from[i] == '{')
624 i++;
625 if (i >= len)
626 return;
628 if (from[i] == 'x')
630 i++;
631 if (i < len && from[i] == '{')
632 i++;
633 for (; i < len; i++)
635 if (from[i] >= '0' && from[i] <= '9')
636 c = c * 16 + from[i] - '0';
637 else if (from[i] >= 'a' && from[i] <= 'f')
638 c = c * 16 + 10 + from[i] - 'a';
639 else if (from[i] >= 'A' && from[i] <= 'F')
640 c = c * 16 + 10 + from[i] - 'A';
641 else
642 break;
645 else if (from[i] >= '0' && from[i] <= '7')
646 for (; i < len && from[i] >= '0' && from[i] <= '7'; i++)
647 c = c * 8 + from[i] - '0';
648 else
650 switch (from[i])
652 case 'n':
653 c = '\n';
654 break;
655 case 't':
656 c = '\t';
657 break;
658 case 'v':
659 c = '\v';
660 break;
661 case 'b':
662 c = '\b';
663 break;
664 case 'r':
665 c = '\r';
666 break;
667 case 'f':
668 c = '\f';
669 break;
670 case 'a':
671 c = '\a';
672 break;
673 default:
674 mc_search_regex__process_append_str (dest_str, from, len, replace_flags);
675 return;
679 if (c < 0x80 || !is_utf8)
680 g_string_append_c (dest_str, (char) c);
681 else if (c < 0x800)
683 b = 0xC0 | (c >> 6);
684 g_string_append_c (dest_str, b);
685 b = 0x80 | (c & 0x3F);
686 g_string_append_c (dest_str, b);
688 else if (c < 0x10000)
690 b = 0xE0 | (c >> 12);
691 g_string_append_c (dest_str, b);
692 b = 0x80 | ((c >> 6) & 0x3F);
693 g_string_append_c (dest_str, b);
694 b = 0x80 | (c & 0x3F);
695 g_string_append_c (dest_str, b);
697 else if (c < 0x10FFFF)
699 b = 0xF0 | (c >> 16);
700 g_string_append_c (dest_str, b);
701 b = 0x80 | ((c >> 12) & 0x3F);
702 g_string_append_c (dest_str, b);
703 b = 0x80 | ((c >> 6) & 0x3F);
704 g_string_append_c (dest_str, b);
705 b = 0x80 | (c & 0x3F);
706 g_string_append_c (dest_str, b);
710 /* --------------------------------------------------------------------------------------------- */
711 /*** public functions ****************************************************************************/
712 /* --------------------------------------------------------------------------------------------- */
714 void
715 mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
716 mc_search_cond_t * mc_search_cond)
718 #ifdef SEARCH_TYPE_GLIB
719 GError *error = NULL;
721 if (!lc_mc_search->is_case_sensitive)
723 GString *tmp;
725 tmp = mc_search_cond->str;
726 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
727 g_string_free (tmp, TRUE);
729 mc_search_cond->regex_handle =
730 g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL,
731 0, &error);
733 if (error != NULL)
735 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
736 lc_mc_search->error_str = str_conv_gerror_message (error, _("Regular expression error"));
737 g_error_free (error);
738 return;
740 #else /* SEARCH_TYPE_GLIB */
741 const char *error;
742 int erroffset;
743 int pcre_options = PCRE_EXTRA | PCRE_MULTILINE;
745 if (str_isutf8 (charset) && mc_global.utf8_display)
747 pcre_options |= PCRE_UTF8;
748 if (!lc_mc_search->is_case_sensitive)
749 pcre_options |= PCRE_CASELESS;
751 else
753 if (!lc_mc_search->is_case_sensitive)
755 GString *tmp;
757 tmp = mc_search_cond->str;
758 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
759 g_string_free (tmp, TRUE);
763 mc_search_cond->regex_handle =
764 pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
765 if (mc_search_cond->regex_handle == NULL)
767 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
768 lc_mc_search->error_str = g_strdup (error);
769 return;
771 lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
772 if (lc_mc_search->regex_match_info == NULL)
774 if (error)
776 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
777 lc_mc_search->error_str = g_strdup (error);
778 g_free (mc_search_cond->regex_handle);
779 mc_search_cond->regex_handle = NULL;
780 return;
783 #endif /* SEARCH_TYPE_GLIB */
784 lc_mc_search->is_utf8 = str_isutf8 (charset);
787 /* --------------------------------------------------------------------------------------------- */
789 gboolean
790 mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
791 gsize start_search, gsize end_search, gsize * found_len)
793 mc_search_cbret_t ret = MC_SEARCH_CB_ABORT;
794 gsize current_pos, virtual_pos;
795 gint start_pos;
796 gint end_pos;
798 if (lc_mc_search->regex_buffer != NULL)
799 g_string_free (lc_mc_search->regex_buffer, TRUE);
801 lc_mc_search->regex_buffer = g_string_sized_new (64);
803 virtual_pos = current_pos = start_search;
804 while (virtual_pos <= end_search)
806 g_string_set_size (lc_mc_search->regex_buffer, 0);
807 lc_mc_search->start_buffer = current_pos;
809 while (TRUE)
811 int current_chr = '\n'; /* stop search symbol */
813 ret = mc_search__get_char (lc_mc_search, user_data, current_pos, &current_chr);
814 if (ret == MC_SEARCH_CB_ABORT)
815 break;
817 if (ret == MC_SEARCH_CB_INVALID)
818 continue;
820 current_pos++;
822 if (ret == MC_SEARCH_CB_SKIP)
823 continue;
825 virtual_pos++;
827 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
829 if ((char) current_chr == '\n' || virtual_pos > end_search)
830 break;
833 switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
835 case COND__FOUND_OK:
836 #ifdef SEARCH_TYPE_GLIB
837 if (lc_mc_search->whole_words)
838 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 2, &start_pos, &end_pos);
839 else
840 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
841 #else /* SEARCH_TYPE_GLIB */
842 if (lc_mc_search->whole_words)
844 start_pos = lc_mc_search->iovector[4];
845 end_pos = lc_mc_search->iovector[5];
847 else
849 start_pos = lc_mc_search->iovector[0];
850 end_pos = lc_mc_search->iovector[1];
852 #endif /* SEARCH_TYPE_GLIB */
853 if (found_len != NULL)
854 *found_len = end_pos - start_pos;
855 lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
856 return TRUE;
857 case COND__NOT_ALL_FOUND:
858 break;
859 default:
860 g_string_free (lc_mc_search->regex_buffer, TRUE);
861 lc_mc_search->regex_buffer = NULL;
862 return FALSE;
865 if ((lc_mc_search->update_fn != NULL) &&
866 ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
867 ret = MC_SEARCH_CB_ABORT;
869 if (ret == MC_SEARCH_CB_ABORT)
870 break;
873 g_string_free (lc_mc_search->regex_buffer, TRUE);
874 lc_mc_search->regex_buffer = NULL;
875 lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
877 if (ret != MC_SEARCH_CB_ABORT)
878 lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
879 else
880 lc_mc_search->error_str = NULL;
882 return FALSE;
885 /* --------------------------------------------------------------------------------------------- */
887 GString *
888 mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
890 GString *ret;
891 gchar *tmp_str;
893 int num_replace_tokens, lc_index;
894 gsize loop;
895 gsize len = 0;
896 gchar *prev_str;
897 replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
899 num_replace_tokens =
900 mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
902 if (lc_mc_search->num_results < 0)
903 return g_string_new_len (replace_str->str, replace_str->len);
905 if (num_replace_tokens > lc_mc_search->num_results - 1
906 || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
908 lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
909 lc_mc_search->error_str = g_strdup (_(STR_E_RPL_NOT_EQ_TO_FOUND));
910 return NULL;
913 ret = g_string_sized_new (64);
914 prev_str = replace_str->str;
916 for (loop = 0; loop < replace_str->len - 1; loop++)
918 lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
920 if (lc_index == REPLACE_PREPARE_T_NOTHING_SPECIAL)
922 if (len != 0)
924 mc_search_regex__process_append_str (ret, prev_str,
925 replace_str->str - prev_str + loop,
926 &replace_flags);
927 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
928 &replace_flags);
929 prev_str = replace_str->str + loop + len;
930 loop += len - 1;
932 continue;
935 if (lc_index == REPLACE_PREPARE_T_REPLACE_FLAG)
937 if (loop)
938 mc_search_regex__process_append_str (ret, prev_str,
939 replace_str->str - prev_str + loop,
940 &replace_flags);
941 prev_str = replace_str->str + loop + len;
942 loop += len - 1;
943 continue;
946 /* escape sequence */
947 if (lc_index == REPLACE_PREPARE_T_ESCAPE_SEQ)
949 mc_search_regex__process_append_str (ret, prev_str,
950 replace_str->str + loop - prev_str,
951 &replace_flags);
952 /* call process_escape_sequence without starting '\\' */
953 mc_search_regex__process_escape_sequence (ret, replace_str->str + loop + 1, len - 1,
954 &replace_flags, lc_mc_search->is_utf8);
955 prev_str = replace_str->str + loop + len;
956 loop += len - 1;
957 continue;
960 /* invalid capture buffer number */
961 if (lc_index > lc_mc_search->num_results)
963 g_string_free (ret, TRUE);
964 lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
965 lc_mc_search->error_str = g_strdup_printf (_(STR_E_RPL_INVALID_TOKEN), lc_index);
966 return NULL;
969 tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
970 if (tmp_str == NULL)
971 continue;
973 if (loop)
974 mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
975 &replace_flags);
976 prev_str = replace_str->str + loop + len;
978 mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
979 g_free (tmp_str);
980 loop += len - 1;
982 mc_search_regex__process_append_str (ret, prev_str,
983 replace_str->str - prev_str + replace_str->len,
984 &replace_flags);
986 return ret;