5 Copyright (C) 2009 The Free Software Foundation, Inc.
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,
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"
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 ****************************************************************/
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 ************************************************************************/
67 mc_search__regex_str_append_if_special (GString
* copy_to
, const GString
* regex_str
,
72 const char **spec_chr
;
73 const char *special_chars
[] = {
88 spec_chr
= special_chars
;
90 tmp_regex_str
= &(regex_str
->str
[*offset
]);
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
) != '}')
106 if (*(tmp_regex_str
+ spec_chr_len
) == '}')
112 g_string_append_len (copy_to
, tmp_regex_str
, spec_chr_len
);
113 *offset
+= spec_chr_len
;
123 /* --------------------------------------------------------------------------------------------- */
125 mc_search__cond_struct_new_regex_hex_add (const char *charset
, GString
* str_to
,
126 const char *one_char
, gsize str_len
)
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
++)
140 if (upp
->str
[loop
] == low
->str
[loop
])
141 tmp_str
= g_strdup_printf ("\\x%02X", (unsigned char) upp
->str
[loop
]);
144 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp
->str
[loop
],
145 (unsigned char) low
->str
[loop
]);
149 tmp_str
= g_strdup_printf ("\\x%02X", (unsigned char) upp
->str
[loop
]);
151 g_string_append (str_to
, tmp_str
);
154 g_string_free (upp
, TRUE
);
155 g_string_free (low
, TRUE
);
158 /* --------------------------------------------------------------------------------------------- */
161 mc_search__cond_struct_new_regex_accum_append (const char *charset
, GString
* str_to
,
164 GString
*recoded_part
;
167 recoded_part
= g_string_sized_new (32);
169 while (loop
< str_from
->len
)
173 gboolean just_letters
;
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)
184 loop
+= one_char_len
;
187 mc_search__cond_struct_new_regex_hex_add (charset
, recoded_part
, one_char
,
190 g_string_append_len (recoded_part
, one_char
, one_char_len
);
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 /* --------------------------------------------------------------------------------------------- */
204 mc_search__cond_struct_new_regex_ci_str (const char *charset
, const GString
* astr
)
206 GString
*accumulator
, *spec_char
, *ret_str
;
209 ret_str
= g_string_sized_new (64);
210 accumulator
= g_string_sized_new (64);
211 spec_char
= g_string_sized_new (64);
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);
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
]);
236 g_string_append_c (ret_str
, astr
->str
[loop
]);
243 g_string_append_c (accumulator
, astr
->str
[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
);
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
,
267 g_match_info_free (lc_mc_search
->regex_match_info
);
268 lc_mc_search
->regex_match_info
= NULL
;
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
)
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
)
310 mc_search__regex_found_cond_one (lc_mc_search
, mc_search_cond
->regex_handle
,
313 if (ret
!= COND__NOT_FOUND
)
316 return COND__NOT_ALL_FOUND
;
319 /* --------------------------------------------------------------------------------------------- */
322 mc_search_regex__get_max_num_of_replace_tokens (const gchar
* str
, gsize len
)
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
]))
332 if (max_token
< str
[loop
+ 1] - '0')
333 max_token
= str
[loop
+ 1] - '0';
336 if (str
[loop
] == '$' && str
[loop
+ 1] == '{')
341 if (strutils_is_char_escaped (str
, &str
[loop
]))
345 loop
+ tmp_len
+ 2 < len
&& (str
[loop
+ 2 + tmp_len
] & (char) 0xf0) == 0x30;
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
;
360 /* --------------------------------------------------------------------------------------------- */
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)
377 return g_strndup (lc_mc_search
->regex_buffer
->str
+ fnd_start
, fnd_end
- fnd_start
);
381 /* --------------------------------------------------------------------------------------------- */
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)
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
) == '}')
401 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
406 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
413 *skip_len
= 2; /* \x */
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
) == '}')
423 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
428 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
432 else if (!g_ascii_isxdigit ((guchar
) c
))
434 *skip_len
= 2; /* \x without number behind */
435 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
441 if (!g_ascii_isxdigit ((guchar
) c
))
442 *skip_len
= 3; /* \xH */
444 *skip_len
= 4; /* \xHH */
445 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
451 if (strchr ("ntvbrfa", c
) != NULL
)
454 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
460 /* --------------------------------------------------------------------------------------------- */
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
)
468 const char *curr_str
= &(replace_str
->str
[current_pos
]);
470 if (current_pos
> replace_str
->len
)
471 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
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
))
481 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
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
);
493 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
495 ret
= atoi (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
))
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 */
517 if (!mc_search_regex__replace_handle_esc_seq (replace_str
, current_pos
, skip_len
, &ret
))
520 ret
= REPLACE_PREPARE_T_REPLACE_FLAG
;
522 switch (*(curr_str
+ 1))
525 *replace_flags
|= REPLACE_T_UPP_TRANSFORM
;
526 *replace_flags
&= ~REPLACE_T_LOW_TRANSFORM
;
529 *replace_flags
|= REPLACE_T_UPP_TRANSFORM_CHAR
;
532 *replace_flags
|= REPLACE_T_LOW_TRANSFORM
;
533 *replace_flags
&= ~REPLACE_T_UPP_TRANSFORM
;
536 *replace_flags
|= REPLACE_T_LOW_TRANSFORM_CHAR
;
539 *replace_flags
= REPLACE_T_NO_TRANSFORM
;
542 ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
549 /* --------------------------------------------------------------------------------------------- */
552 mc_search_regex__process_append_str (GString
* dest_str
, const char *from
, gsize len
,
553 replace_transform_type_t
* replace_flags
)
560 if (len
== (gsize
) - 1)
563 if (*replace_flags
== REPLACE_T_NO_TRANSFORM
)
565 g_string_append_len (dest_str
, from
, 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
);
604 g_string_append (dest_str
, tmp_str
);
612 /* --------------------------------------------------------------------------------------------- */
615 mc_search_regex__process_escape_sequence (GString
* dest_str
, const char *from
, gsize len
,
616 replace_transform_type_t
* replace_flags
,
623 if (len
== (gsize
) (-1))
635 if (i
< len
&& from
[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';
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';
678 mc_search_regex__process_append_str (dest_str
, from
, len
, replace_flags
);
683 if (c
< 0x80 || !is_utf8
)
684 g_string_append_c (dest_str
, (char) c
);
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 /* --------------------------------------------------------------------------------------------- */
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
)
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
,
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
);
744 #else /* SEARCH_TYPE_GLIB */
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
;
757 if (!lc_mc_search
->is_case_sensitive
)
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
);
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
)
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
;
787 #endif /* SEARCH_TYPE_GLIB */
788 lc_mc_search
->is_utf8
= str_isutf8 (charset
);
791 /* --------------------------------------------------------------------------------------------- */
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
;
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
;
815 current_chr
= mc_search__get_char (lc_mc_search
, user_data
, current_pos
);
816 if (current_chr
== MC_SEARCH_CB_ABORT
)
819 if (current_chr
== MC_SEARCH_CB_INVALID
)
824 if (current_chr
== MC_SEARCH_CB_SKIP
)
829 g_string_append_c (lc_mc_search
->regex_buffer
, (char) current_chr
);
832 if (current_chr
== 0 || (char) current_chr
== '\n')
835 if (virtual_pos
> end_search
)
839 switch (mc_search__regex_found_cond (lc_mc_search
, lc_mc_search
->regex_buffer
))
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
);
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];
859 start_pos
= lc_mc_search
->iovector
[0];
860 end_pos
= lc_mc_search
->iovector
[1];
862 #endif /* SEARCH_TYPE_GLIB */
864 *found_len
= end_pos
- start_pos
;
865 lc_mc_search
->normal_offset
= lc_mc_search
->start_buffer
+ start_pos
;
867 case COND__NOT_ALL_FOUND
:
870 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
871 lc_mc_search
->regex_buffer
= NULL
;
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
)
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
));
888 lc_mc_search
->error_str
= NULL
;
893 /* --------------------------------------------------------------------------------------------- */
896 mc_search_regex_prepare_replace_str (mc_search_t
* lc_mc_search
, GString
* replace_str
)
901 int num_replace_tokens
, lc_index
;
905 replace_transform_type_t replace_flags
= REPLACE_T_NO_TRANSFORM
;
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
));
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
)
932 mc_search_regex__process_append_str (ret
, prev_str
,
933 replace_str
->str
- prev_str
+ loop
,
935 mc_search_regex__process_append_str (ret
, replace_str
->str
+ loop
+ 1, len
- 1,
937 prev_str
= replace_str
->str
+ loop
+ len
;
943 if (lc_index
== REPLACE_PREPARE_T_REPLACE_FLAG
)
946 mc_search_regex__process_append_str (ret
, prev_str
,
947 replace_str
->str
- prev_str
+ loop
,
949 prev_str
= replace_str
->str
+ loop
+ len
;
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
,
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
;
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
);
977 tmp_str
= mc_search_regex__get_token_by_num (lc_mc_search
, lc_index
);
982 mc_search_regex__process_append_str (ret
, prev_str
, replace_str
->str
- prev_str
+ loop
,
984 prev_str
= replace_str
->str
+ loop
+ len
;
986 mc_search_regex__process_append_str (ret
, tmp_str
, -1, &replace_flags
);
990 mc_search_regex__process_append_str (ret
, prev_str
,
991 replace_str
->str
- prev_str
+ replace_str
->len
,