5 Copyright (C) 2009, 2011
6 The Free Software Foundation, Inc.
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/>.
32 #include "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/search.h"
35 #include "lib/strescape.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 ****************************************************************/
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 ************************************************************************/
64 mc_search__regex_str_append_if_special (GString
* copy_to
, const GString
* regex_str
,
69 const char **spec_chr
;
70 const char *special_chars
[] = {
85 spec_chr
= special_chars
;
87 tmp_regex_str
= &(regex_str
->str
[*offset
]);
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
) != '}')
103 if (*(tmp_regex_str
+ spec_chr_len
) == '}')
109 g_string_append_len (copy_to
, tmp_regex_str
, spec_chr_len
);
110 *offset
+= spec_chr_len
;
120 /* --------------------------------------------------------------------------------------------- */
122 mc_search__cond_struct_new_regex_hex_add (const char *charset
, GString
* str_to
,
123 const char *one_char
, gsize str_len
)
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
++)
137 if (upp
->str
[loop
] == low
->str
[loop
])
138 tmp_str
= g_strdup_printf ("\\x%02X", (unsigned char) upp
->str
[loop
]);
141 g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp
->str
[loop
],
142 (unsigned char) low
->str
[loop
]);
146 tmp_str
= g_strdup_printf ("\\x%02X", (unsigned char) upp
->str
[loop
]);
148 g_string_append (str_to
, tmp_str
);
151 g_string_free (upp
, TRUE
);
152 g_string_free (low
, TRUE
);
155 /* --------------------------------------------------------------------------------------------- */
158 mc_search__cond_struct_new_regex_accum_append (const char *charset
, GString
* str_to
,
161 GString
*recoded_part
;
164 recoded_part
= g_string_sized_new (32);
166 while (loop
< str_from
->len
)
170 gboolean just_letters
;
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)
181 loop
+= one_char_len
;
184 mc_search__cond_struct_new_regex_hex_add (charset
, recoded_part
, one_char
,
187 g_string_append_len (recoded_part
, one_char
, one_char_len
);
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 /* --------------------------------------------------------------------------------------------- */
201 mc_search__cond_struct_new_regex_ci_str (const char *charset
, const GString
* astr
)
203 GString
*accumulator
, *spec_char
, *ret_str
;
206 ret_str
= g_string_sized_new (64);
207 accumulator
= g_string_sized_new (64);
208 spec_char
= g_string_sized_new (64);
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);
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
]);
233 g_string_append_c (ret_str
, astr
->str
[loop
]);
240 g_string_append_c (accumulator
, astr
->str
[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
);
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
;
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
)
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
)
306 mc_search__regex_found_cond_one (lc_mc_search
, mc_search_cond
->regex_handle
,
309 if (ret
!= COND__NOT_FOUND
)
312 return COND__NOT_ALL_FOUND
;
315 /* --------------------------------------------------------------------------------------------- */
318 mc_search_regex__get_max_num_of_replace_tokens (const gchar
* str
, gsize len
)
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
]))
328 if (max_token
< str
[loop
+ 1] - '0')
329 max_token
= str
[loop
+ 1] - '0';
332 if (str
[loop
] == '$' && str
[loop
+ 1] == '{')
337 if (strutils_is_char_escaped (str
, &str
[loop
]))
341 loop
+ tmp_len
+ 2 < len
&& (str
[loop
+ 2 + tmp_len
] & (char) 0xf0) == 0x30;
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
;
356 /* --------------------------------------------------------------------------------------------- */
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)
373 return g_strndup (lc_mc_search
->regex_buffer
->str
+ fnd_start
, fnd_end
- fnd_start
);
377 /* --------------------------------------------------------------------------------------------- */
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)
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
) == '}')
397 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
402 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
409 *skip_len
= 2; /* \x */
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
) == '}')
419 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
424 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
428 else if (!g_ascii_isxdigit ((guchar
) c
))
430 *skip_len
= 2; /* \x without number behind */
431 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
437 if (!g_ascii_isxdigit ((guchar
) c
))
438 *skip_len
= 3; /* \xH */
440 *skip_len
= 4; /* \xHH */
441 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
447 if (strchr ("ntvbrfa", c
) != NULL
)
450 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
456 /* --------------------------------------------------------------------------------------------- */
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
)
464 const char *curr_str
= &(replace_str
->str
[current_pos
]);
466 if (current_pos
> replace_str
->len
)
467 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
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
))
477 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
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
);
489 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
491 ret
= atoi (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
))
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 */
513 if (!mc_search_regex__replace_handle_esc_seq (replace_str
, current_pos
, skip_len
, &ret
))
516 ret
= REPLACE_PREPARE_T_REPLACE_FLAG
;
518 switch (*(curr_str
+ 1))
521 *replace_flags
|= REPLACE_T_UPP_TRANSFORM
;
522 *replace_flags
&= ~REPLACE_T_LOW_TRANSFORM
;
525 *replace_flags
|= REPLACE_T_UPP_TRANSFORM_CHAR
;
528 *replace_flags
|= REPLACE_T_LOW_TRANSFORM
;
529 *replace_flags
&= ~REPLACE_T_UPP_TRANSFORM
;
532 *replace_flags
|= REPLACE_T_LOW_TRANSFORM_CHAR
;
535 *replace_flags
= REPLACE_T_NO_TRANSFORM
;
538 ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
545 /* --------------------------------------------------------------------------------------------- */
548 mc_search_regex__process_append_str (GString
* dest_str
, const char *from
, gsize len
,
549 replace_transform_type_t
* replace_flags
)
556 if (len
== (gsize
) - 1)
559 if (*replace_flags
== REPLACE_T_NO_TRANSFORM
)
561 g_string_append_len (dest_str
, from
, 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
);
600 g_string_append (dest_str
, tmp_str
);
608 /* --------------------------------------------------------------------------------------------- */
611 mc_search_regex__process_escape_sequence (GString
* dest_str
, const char *from
, gsize len
,
612 replace_transform_type_t
* replace_flags
,
619 if (len
== (gsize
) (-1))
631 if (i
< len
&& from
[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';
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';
674 mc_search_regex__process_append_str (dest_str
, from
, len
, replace_flags
);
679 if (c
< 0x80 || !is_utf8
)
680 g_string_append_c (dest_str
, (char) c
);
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 /* --------------------------------------------------------------------------------------------- */
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
)
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
,
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
);
740 #else /* SEARCH_TYPE_GLIB */
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
;
753 if (!lc_mc_search
->is_case_sensitive
)
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
);
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
)
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
;
783 #endif /* SEARCH_TYPE_GLIB */
784 lc_mc_search
->is_utf8
= str_isutf8 (charset
);
787 /* --------------------------------------------------------------------------------------------- */
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
;
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
;
811 int current_chr
= '\n'; /* stop search symbol */
813 ret
= mc_search__get_char (lc_mc_search
, user_data
, current_pos
, ¤t_chr
);
814 if (ret
== MC_SEARCH_CB_ABORT
)
817 if (ret
== MC_SEARCH_CB_INVALID
)
822 if (ret
== MC_SEARCH_CB_SKIP
)
827 g_string_append_c (lc_mc_search
->regex_buffer
, (char) current_chr
);
829 if ((char) current_chr
== '\n' || virtual_pos
> end_search
)
833 switch (mc_search__regex_found_cond (lc_mc_search
, lc_mc_search
->regex_buffer
))
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
);
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];
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
;
857 case COND__NOT_ALL_FOUND
:
860 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
861 lc_mc_search
->regex_buffer
= NULL
;
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
)
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
));
880 lc_mc_search
->error_str
= NULL
;
885 /* --------------------------------------------------------------------------------------------- */
888 mc_search_regex_prepare_replace_str (mc_search_t
* lc_mc_search
, GString
* replace_str
)
893 int num_replace_tokens
, lc_index
;
897 replace_transform_type_t replace_flags
= REPLACE_T_NO_TRANSFORM
;
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
));
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
)
924 mc_search_regex__process_append_str (ret
, prev_str
,
925 replace_str
->str
- prev_str
+ loop
,
927 mc_search_regex__process_append_str (ret
, replace_str
->str
+ loop
+ 1, len
- 1,
929 prev_str
= replace_str
->str
+ loop
+ len
;
935 if (lc_index
== REPLACE_PREPARE_T_REPLACE_FLAG
)
938 mc_search_regex__process_append_str (ret
, prev_str
,
939 replace_str
->str
- prev_str
+ loop
,
941 prev_str
= replace_str
->str
+ loop
+ len
;
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
,
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
;
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
);
969 tmp_str
= mc_search_regex__get_token_by_num (lc_mc_search
, lc_index
);
974 mc_search_regex__process_append_str (ret
, prev_str
, replace_str
->str
- prev_str
+ loop
,
976 prev_str
= replace_str
->str
+ loop
+ len
;
978 mc_search_regex__process_append_str (ret
, tmp_str
, -1, &replace_flags
);
982 mc_search_regex__process_append_str (ret
, prev_str
,
983 replace_str
->str
- prev_str
+ replace_str
->len
,