5 Copyright (C) 2009-2024
6 Free Software Foundation, Inc.
9 Slava Zanko <slavazanko@gmail.com>, 2009, 2010, 2011, 2013
10 Vitaliy Filippov <vitalif@yourcmc.ru>, 2011
11 Andrew Borodin <aborodin@vmail.ru>, 2013-2015
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.h"
36 #include "lib/util.h" /* MC_PTR_FREE */
40 /*** global variables ****************************************************************************/
42 /*** file scope macro definitions ****************************************************************/
44 #define REPLACE_PREPARE_T_NOTHING_SPECIAL -1
45 #define REPLACE_PREPARE_T_REPLACE_FLAG -2
46 #define REPLACE_PREPARE_T_ESCAPE_SEQ -3
48 /*** file scope type declarations ****************************************************************/
52 REPLACE_T_NO_TRANSFORM
= 0,
53 REPLACE_T_UPP_TRANSFORM_CHAR
= 1,
54 REPLACE_T_LOW_TRANSFORM_CHAR
= 2,
55 REPLACE_T_UPP_TRANSFORM
= 4,
56 REPLACE_T_LOW_TRANSFORM
= 8
57 } replace_transform_type_t
;
59 /*** forward declarations (file scope functions) *************************************************/
61 /*** file scope variables ************************************************************************/
63 /* --------------------------------------------------------------------------------------------- */
64 /*** file scope functions ************************************************************************/
65 /* --------------------------------------------------------------------------------------------- */
68 mc_search__regex_str_append_if_special (GString
*copy_to
, const GString
*regex_str
, gsize
*offset
)
70 const char *special_chars
[] = {
87 const char **spec_chr
;
89 tmp_regex_str
= &(regex_str
->str
[*offset
]);
91 for (spec_chr
= special_chars
; *spec_chr
!= NULL
; spec_chr
++)
95 spec_chr_len
= strlen (*spec_chr
);
97 if (strncmp (tmp_regex_str
, *spec_chr
, spec_chr_len
) == 0
98 && !str_is_char_escaped (regex_str
->str
, tmp_regex_str
))
100 if (strncmp ("\\x", *spec_chr
, spec_chr_len
) == 0)
102 if (tmp_regex_str
[spec_chr_len
] != '{')
106 while ((spec_chr_len
< regex_str
->len
- *offset
)
107 && tmp_regex_str
[spec_chr_len
] != '}')
109 if (tmp_regex_str
[spec_chr_len
] == '}')
113 g_string_append_len (copy_to
, tmp_regex_str
, spec_chr_len
);
114 *offset
+= spec_chr_len
;
122 /* --------------------------------------------------------------------------------------------- */
125 mc_search__cond_struct_new_regex_hex_add (const char *charset
, GString
*str_to
,
126 const GString
*one_char
)
131 upp
= mc_search__toupper_case_str (charset
, one_char
);
132 low
= mc_search__tolower_case_str (charset
, one_char
);
134 for (loop
= 0; loop
< upp
->len
; loop
++)
136 gchar tmp_str
[10 + 1]; /* longest content is "[\\x%02X\\x%02X]" */
139 if (loop
>= low
->len
|| upp
->str
[loop
] == low
->str
[loop
])
141 g_snprintf (tmp_str
, sizeof (tmp_str
), "\\x%02X", (unsigned char) upp
->str
[loop
]);
144 g_snprintf (tmp_str
, sizeof (tmp_str
), "[\\x%02X\\x%02X]",
145 (unsigned char) upp
->str
[loop
], (unsigned char) low
->str
[loop
]);
147 g_string_append_len (str_to
, tmp_str
, tmp_len
);
150 g_string_free (upp
, TRUE
);
151 g_string_free (low
, TRUE
);
154 /* --------------------------------------------------------------------------------------------- */
157 mc_search__cond_struct_new_regex_accum_append (const char *charset
, GString
*str_to
,
160 GString
*recoded_part
;
163 recoded_part
= g_string_sized_new (32);
165 while (loop
< str_from
->len
)
168 gboolean just_letters
;
171 mc_search__get_one_symbol (charset
, str_from
->str
+ loop
,
172 MIN (str_from
->len
- loop
, 6), &just_letters
);
174 if (one_char
->len
== 0)
178 loop
+= one_char
->len
;
181 mc_search__cond_struct_new_regex_hex_add (charset
, recoded_part
, one_char
);
183 g_string_append_len (recoded_part
, one_char
->str
, one_char
->len
);
186 g_string_free (one_char
, TRUE
);
189 g_string_append_len (str_to
, recoded_part
->str
, recoded_part
->len
);
190 g_string_free (recoded_part
, TRUE
);
191 g_string_set_size (str_from
, 0);
194 /* --------------------------------------------------------------------------------------------- */
197 * Creates a case-insensitive version of a regex pattern.
199 * For example (assuming ASCII charset): given "\\bHello!\\xAB", returns
200 * "\\b[Hh][Ee][Ll][Ll][Oo]!\\xAB" (this example is for easier reading; in
201 * reality hex codes are used instead of letters).
203 * This function knows not to ruin special regex symbols.
205 * This function is used when working with non-UTF-8 charsets: GLib's
206 * regex engine doesn't understand such charsets and therefore can't do
210 mc_search__cond_struct_new_regex_ci_str (const char *charset
, const GString
*astr
)
212 GString
*accumulator
, *spec_char
, *ret_str
;
215 ret_str
= g_string_sized_new (64);
216 accumulator
= g_string_sized_new (64);
217 spec_char
= g_string_sized_new (64);
220 while (loop
< astr
->len
)
222 if (mc_search__regex_str_append_if_special (spec_char
, astr
, &loop
))
224 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
225 g_string_append_len (ret_str
, spec_char
->str
, spec_char
->len
);
226 g_string_set_size (spec_char
, 0);
230 if (astr
->str
[loop
] == '[' && !str_is_char_escaped (astr
->str
, &(astr
->str
[loop
])))
232 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
234 while (loop
< astr
->len
&& !(astr
->str
[loop
] == ']'
235 && !str_is_char_escaped (astr
->str
, &(astr
->str
[loop
]))))
237 g_string_append_c (ret_str
, astr
->str
[loop
]);
241 g_string_append_c (ret_str
, astr
->str
[loop
]);
248 g_string_append_c (accumulator
, astr
->str
[loop
]);
251 mc_search__cond_struct_new_regex_accum_append (charset
, ret_str
, accumulator
);
253 g_string_free (accumulator
, TRUE
);
254 g_string_free (spec_char
, TRUE
);
259 /* --------------------------------------------------------------------------------------------- */
261 #ifdef SEARCH_TYPE_GLIB
262 /* A thin wrapper above g_regex_match_full that makes sure the string passed
263 * to it is valid UTF-8 (unless G_REGEX_RAW compile flag was set), as it is a
264 * requirement by glib and it might crash otherwise. See: mc ticket 3449.
265 * Be careful: there might be embedded NULs in the strings. */
267 mc_search__g_regex_match_full_safe (const GRegex
*regex
,
271 GRegexMatchFlags match_options
,
272 GMatchInfo
**match_info
, GError
**error
)
274 char *string_safe
, *p
, *end
;
278 string_len
= strlen (string
);
280 if ((g_regex_get_compile_flags (regex
) & G_REGEX_RAW
)
281 || g_utf8_validate (string
, string_len
, NULL
))
283 return g_regex_match_full (regex
, string
, string_len
, start_position
, match_options
,
287 /* Correctly handle embedded NULs while copying */
288 p
= string_safe
= g_malloc (string_len
+ 1);
289 memcpy (string_safe
, string
, string_len
);
290 string_safe
[string_len
] = '\0';
291 end
= p
+ string_len
;
295 gunichar c
= g_utf8_get_char_validated (p
, -1);
296 if (c
!= (gunichar
) (-1) && c
!= (gunichar
) (-2))
298 p
= g_utf8_next_char (p
);
302 /* U+FFFD would be the proper choice, but then we'd have to
303 maintain mapping between old and new offsets.
304 So rather do a byte by byte replacement. */
310 g_regex_match_full (regex
, string_safe
, string_len
, start_position
, match_options
,
312 g_free (string_safe
);
315 #endif /* SEARCH_TYPE_GLIB */
317 /* --------------------------------------------------------------------------------------------- */
319 static mc_search__found_cond_t
320 mc_search__regex_found_cond_one (mc_search_t
*lc_mc_search
, mc_search_regex_t
*regex
,
323 #ifdef SEARCH_TYPE_GLIB
324 GError
*mcerror
= NULL
;
326 if (!mc_search__g_regex_match_full_safe
327 (regex
, search_str
->str
, search_str
->len
, 0, G_REGEX_MATCH_NEWLINE_ANY
,
328 &lc_mc_search
->regex_match_info
, &mcerror
))
330 g_match_info_free (lc_mc_search
->regex_match_info
);
331 lc_mc_search
->regex_match_info
= NULL
;
334 lc_mc_search
->error
= MC_SEARCH_E_REGEX
;
335 g_free (lc_mc_search
->error_str
);
336 lc_mc_search
->error_str
=
337 str_conv_gerror_message (mcerror
, _("Regular expression error"));
338 g_error_free (mcerror
);
339 return COND__FOUND_ERROR
;
341 return COND__NOT_FOUND
;
343 lc_mc_search
->num_results
= g_match_info_get_match_count (lc_mc_search
->regex_match_info
);
344 #else /* SEARCH_TYPE_GLIB */
346 lc_mc_search
->num_results
=
348 pcre2_match (regex
, (unsigned char *) search_str
->str
, search_str
->len
, 0, 0,
349 lc_mc_search
->regex_match_info
, NULL
);
351 pcre_exec (regex
, lc_mc_search
->regex_match_info
, search_str
->str
, search_str
->len
, 0, 0,
352 lc_mc_search
->iovector
, MC_SEARCH__NUM_REPLACE_ARGS
);
354 if (lc_mc_search
->num_results
< 0)
356 return COND__NOT_FOUND
;
358 #endif /* SEARCH_TYPE_GLIB */
359 return COND__FOUND_OK
;
363 /* --------------------------------------------------------------------------------------------- */
365 static mc_search__found_cond_t
366 mc_search__regex_found_cond (mc_search_t
*lc_mc_search
, GString
*search_str
)
370 for (loop1
= 0; loop1
< lc_mc_search
->prepared
.conditions
->len
; loop1
++)
372 mc_search_cond_t
*mc_search_cond
;
373 mc_search__found_cond_t ret
;
376 (mc_search_cond_t
*) g_ptr_array_index (lc_mc_search
->prepared
.conditions
, loop1
);
378 if (!mc_search_cond
->regex_handle
)
382 mc_search__regex_found_cond_one (lc_mc_search
, mc_search_cond
->regex_handle
,
384 if (ret
!= COND__NOT_FOUND
)
387 return COND__NOT_ALL_FOUND
;
390 /* --------------------------------------------------------------------------------------------- */
393 mc_search_regex__get_max_num_of_replace_tokens (const gchar
*str
, gsize len
)
398 for (loop
= 0; loop
< len
- 1; loop
++)
399 if (str
[loop
] == '\\' && g_ascii_isdigit (str
[loop
+ 1]))
401 if (str_is_char_escaped (str
, &str
[loop
]))
403 if (max_token
< str
[loop
+ 1] - '0')
404 max_token
= str
[loop
+ 1] - '0';
406 else if (str
[loop
] == '$' && str
[loop
+ 1] == '{')
410 if (str_is_char_escaped (str
, &str
[loop
]))
414 loop
+ tmp_len
+ 2 < len
&& (str
[loop
+ 2 + tmp_len
] & (char) 0xf0) == 0x30;
417 if (str
[loop
+ 2 + tmp_len
] == '}')
422 tmp_str
= g_strndup (&str
[loop
+ 2], tmp_len
);
423 tmp_token
= atoi (tmp_str
);
424 if (max_token
< tmp_token
)
425 max_token
= tmp_token
;
433 /* --------------------------------------------------------------------------------------------- */
436 mc_search_regex__get_token_by_num (const mc_search_t
*lc_mc_search
, gsize lc_index
)
438 int fnd_start
= 0, fnd_end
= 0;
440 #ifdef SEARCH_TYPE_GLIB
441 g_match_info_fetch_pos (lc_mc_search
->regex_match_info
, lc_index
, &fnd_start
, &fnd_end
);
442 #else /* SEARCH_TYPE_GLIB */
443 fnd_start
= lc_mc_search
->iovector
[lc_index
* 2 + 0];
444 fnd_end
= lc_mc_search
->iovector
[lc_index
* 2 + 1];
445 #endif /* SEARCH_TYPE_GLIB */
447 if (fnd_end
== fnd_start
)
448 return g_strdup ("");
450 return g_strndup (lc_mc_search
->regex_buffer
->str
+ fnd_start
, fnd_end
- fnd_start
);
454 /* --------------------------------------------------------------------------------------------- */
457 mc_search_regex__replace_handle_esc_seq (const GString
*replace_str
, const gsize current_pos
,
458 gsize
*skip_len
, int *ret
)
460 char *curr_str
= &(replace_str
->str
[current_pos
]);
461 char c
= curr_str
[1];
463 if (replace_str
->len
> current_pos
+ 2)
467 for (*skip_len
= 2; /* \{ */
468 current_pos
+ *skip_len
< replace_str
->len
&& curr_str
[*skip_len
] >= '0'
469 && curr_str
[*skip_len
] <= '7'; (*skip_len
)++)
472 if (current_pos
+ *skip_len
< replace_str
->len
&& curr_str
[*skip_len
] == '}')
475 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
480 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
487 *skip_len
= 2; /* \x */
491 for (*skip_len
= 3; /* \x{ */
492 current_pos
+ *skip_len
< replace_str
->len
493 && g_ascii_isxdigit ((guchar
) curr_str
[*skip_len
]); (*skip_len
)++)
496 if (current_pos
+ *skip_len
< replace_str
->len
&& curr_str
[*skip_len
] == '}')
499 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
504 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
508 else if (!g_ascii_isxdigit ((guchar
) c
))
510 *skip_len
= 2; /* \x without number behind */
511 *ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
517 if (!g_ascii_isxdigit ((guchar
) c
))
518 *skip_len
= 3; /* \xH */
520 *skip_len
= 4; /* \xHH */
521 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
527 if (strchr ("ntvbrfa", c
) != NULL
)
530 *ret
= REPLACE_PREPARE_T_ESCAPE_SEQ
;
536 /* --------------------------------------------------------------------------------------------- */
539 mc_search_regex__process_replace_str (const GString
*replace_str
, const gsize current_pos
,
540 gsize
*skip_len
, replace_transform_type_t
*replace_flags
)
543 const char *curr_str
= &(replace_str
->str
[current_pos
]);
545 if (current_pos
> replace_str
->len
)
546 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
550 if (replace_str
->len
> current_pos
+ 2 && curr_str
[0] == '$' && curr_str
[1] == '{'
551 && (curr_str
[2] & (char) 0xf0) == 0x30)
555 if (str_is_char_escaped (replace_str
->str
, curr_str
))
558 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
562 current_pos
+ *skip_len
+ 2 < replace_str
->len
563 && (curr_str
[2 + *skip_len
] & (char) 0xf0) == 0x30; (*skip_len
)++)
566 if (curr_str
[2 + *skip_len
] != '}')
567 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
569 tmp_str
= g_strndup (curr_str
+ 2, *skip_len
);
571 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
573 ret
= atoi (tmp_str
);
576 *skip_len
+= 3; /* ${} */
577 return ret
; /* capture buffer index >= 0 */
580 if (curr_str
[0] == '\\' && replace_str
->len
> current_pos
+ 1)
582 if (str_is_char_escaped (replace_str
->str
, curr_str
))
585 return REPLACE_PREPARE_T_NOTHING_SPECIAL
;
588 if (g_ascii_isdigit (curr_str
[1]))
590 ret
= g_ascii_digit_value (curr_str
[1]); /* capture buffer index >= 0 */
591 *skip_len
= 2; /* \\ and one digit */
595 if (!mc_search_regex__replace_handle_esc_seq (replace_str
, current_pos
, skip_len
, &ret
))
598 ret
= REPLACE_PREPARE_T_REPLACE_FLAG
;
604 *replace_flags
|= REPLACE_T_UPP_TRANSFORM
;
605 *replace_flags
&= ~REPLACE_T_LOW_TRANSFORM
;
608 *replace_flags
|= REPLACE_T_UPP_TRANSFORM_CHAR
;
611 *replace_flags
|= REPLACE_T_LOW_TRANSFORM
;
612 *replace_flags
&= ~REPLACE_T_UPP_TRANSFORM
;
615 *replace_flags
|= REPLACE_T_LOW_TRANSFORM_CHAR
;
618 *replace_flags
= REPLACE_T_NO_TRANSFORM
;
621 ret
= REPLACE_PREPARE_T_NOTHING_SPECIAL
;
628 /* --------------------------------------------------------------------------------------------- */
631 mc_search_regex__process_append_str (GString
*dest_str
, const char *from
, gsize len
,
632 replace_transform_type_t
*replace_flags
)
637 if (len
== (gsize
) (-1))
640 if (*replace_flags
== REPLACE_T_NO_TRANSFORM
)
642 g_string_append_len (dest_str
, from
, len
);
646 for (loop
= 0; loop
< len
; loop
+= char_len
)
648 GString
*tmp_string
= NULL
;
651 s
= mc_search__get_one_symbol (NULL
, from
+ loop
, len
- loop
, NULL
);
654 if ((*replace_flags
& REPLACE_T_UPP_TRANSFORM_CHAR
) != 0)
656 *replace_flags
&= ~REPLACE_T_UPP_TRANSFORM_CHAR
;
657 tmp_string
= mc_search__toupper_case_str (NULL
, s
);
658 g_string_append_len (dest_str
, tmp_string
->str
, tmp_string
->len
);
660 else if ((*replace_flags
& REPLACE_T_LOW_TRANSFORM_CHAR
) != 0)
662 *replace_flags
&= ~REPLACE_T_LOW_TRANSFORM_CHAR
;
663 tmp_string
= mc_search__tolower_case_str (NULL
, s
);
664 g_string_append_len (dest_str
, tmp_string
->str
, tmp_string
->len
);
666 else if ((*replace_flags
& REPLACE_T_UPP_TRANSFORM
) != 0)
668 tmp_string
= mc_search__toupper_case_str (NULL
, s
);
669 g_string_append_len (dest_str
, tmp_string
->str
, tmp_string
->len
);
671 else if ((*replace_flags
& REPLACE_T_LOW_TRANSFORM
) != 0)
673 tmp_string
= mc_search__tolower_case_str (NULL
, s
);
674 g_string_append_len (dest_str
, tmp_string
->str
, tmp_string
->len
);
677 g_string_free (s
, TRUE
);
678 if (tmp_string
!= NULL
)
679 g_string_free (tmp_string
, TRUE
);
683 /* --------------------------------------------------------------------------------------------- */
686 mc_search_regex__process_escape_sequence (GString
*dest_str
, const char *from
, gsize len
,
687 replace_transform_type_t
*replace_flags
, gboolean is_utf8
)
693 if (len
== (gsize
) (-1))
706 if (i
< len
&& from
[i
] == '{')
710 if (from
[i
] >= '0' && from
[i
] <= '9')
711 c
= c
* 16 + from
[i
] - '0';
712 else if (from
[i
] >= 'a' && from
[i
] <= 'f')
713 c
= c
* 16 + 10 + from
[i
] - 'a';
714 else if (from
[i
] >= 'A' && from
[i
] <= 'F')
715 c
= c
* 16 + 10 + from
[i
] - 'A';
720 else if (from
[i
] >= '0' && from
[i
] <= '7')
721 for (; i
< len
&& from
[i
] >= '0' && from
[i
] <= '7'; i
++)
722 c
= c
* 8 + from
[i
] - '0';
749 mc_search_regex__process_append_str (dest_str
, from
, len
, replace_flags
);
754 if (c
< 0x80 || !is_utf8
)
755 g_string_append_c (dest_str
, (char) c
);
759 g_string_append_c (dest_str
, b
);
760 b
= 0x80 | (c
& 0x3F);
761 g_string_append_c (dest_str
, b
);
763 else if (c
< 0x10000)
765 b
= 0xE0 | (c
>> 12);
766 g_string_append_c (dest_str
, b
);
767 b
= 0x80 | ((c
>> 6) & 0x3F);
768 g_string_append_c (dest_str
, b
);
769 b
= 0x80 | (c
& 0x3F);
770 g_string_append_c (dest_str
, b
);
772 else if (c
< 0x10FFFF)
774 b
= 0xF0 | (c
>> 16);
775 g_string_append_c (dest_str
, b
);
776 b
= 0x80 | ((c
>> 12) & 0x3F);
777 g_string_append_c (dest_str
, b
);
778 b
= 0x80 | ((c
>> 6) & 0x3F);
779 g_string_append_c (dest_str
, b
);
780 b
= 0x80 | (c
& 0x3F);
781 g_string_append_c (dest_str
, b
);
785 /* --------------------------------------------------------------------------------------------- */
786 /*** public functions ****************************************************************************/
787 /* --------------------------------------------------------------------------------------------- */
790 mc_search__cond_struct_new_init_regex (const char *charset
, mc_search_t
*lc_mc_search
,
791 mc_search_cond_t
*mc_search_cond
)
793 if (lc_mc_search
->whole_words
&& !lc_mc_search
->is_entire_line
)
795 /* NOTE: \b as word boundary doesn't allow search
796 * whole words with non-ASCII symbols.
797 * Update: Is it still true nowadays? Probably not. #2396, #3524 */
798 g_string_prepend (mc_search_cond
->str
, "(?<![\\p{L}\\p{N}_])");
799 g_string_append (mc_search_cond
->str
, "(?![\\p{L}\\p{N}_])");
803 #ifdef SEARCH_TYPE_GLIB
804 GError
*mcerror
= NULL
;
805 GRegexCompileFlags g_regex_options
= G_REGEX_OPTIMIZE
| G_REGEX_DOTALL
;
807 if (str_isutf8 (charset
) && mc_global
.utf8_display
)
809 if (!lc_mc_search
->is_case_sensitive
)
810 g_regex_options
|= G_REGEX_CASELESS
;
814 g_regex_options
|= G_REGEX_RAW
;
816 if (!lc_mc_search
->is_case_sensitive
)
820 tmp
= mc_search_cond
->str
;
821 mc_search_cond
->str
= mc_search__cond_struct_new_regex_ci_str (charset
, tmp
);
822 g_string_free (tmp
, TRUE
);
826 mc_search_cond
->regex_handle
=
827 g_regex_new (mc_search_cond
->str
->str
, g_regex_options
, 0, &mcerror
);
831 lc_mc_search
->error
= MC_SEARCH_E_REGEX_COMPILE
;
832 g_free (lc_mc_search
->error_str
);
833 lc_mc_search
->error_str
=
834 str_conv_gerror_message (mcerror
, _("Regular expression error"));
835 g_error_free (mcerror
);
838 #else /* SEARCH_TYPE_GLIB */
842 char error
[BUF_SMALL
];
844 int pcre_options
= PCRE2_MULTILINE
;
848 int pcre_options
= PCRE_EXTRA
| PCRE_MULTILINE
;
851 if (str_isutf8 (charset
) && mc_global
.utf8_display
)
854 pcre_options
|= PCRE2_UTF
;
855 if (!lc_mc_search
->is_case_sensitive
)
856 pcre_options
|= PCRE2_CASELESS
;
858 pcre_options
|= PCRE_UTF8
;
859 if (!lc_mc_search
->is_case_sensitive
)
860 pcre_options
|= PCRE_CASELESS
;
863 else if (!lc_mc_search
->is_case_sensitive
)
867 tmp
= mc_search_cond
->str
;
868 mc_search_cond
->str
= mc_search__cond_struct_new_regex_ci_str (charset
, tmp
);
869 g_string_free (tmp
, TRUE
);
872 mc_search_cond
->regex_handle
=
874 pcre2_compile ((unsigned char *) mc_search_cond
->str
->str
, PCRE2_ZERO_TERMINATED
,
875 pcre_options
, &errcode
, &erroffset
, NULL
);
877 pcre_compile (mc_search_cond
->str
->str
, pcre_options
, &error
, &erroffset
, NULL
);
879 if (mc_search_cond
->regex_handle
== NULL
)
882 pcre2_get_error_message (errcode
, (unsigned char *) error
, sizeof (error
));
884 mc_search_set_error (lc_mc_search
, MC_SEARCH_E_REGEX_COMPILE
, "%s", error
);
888 if (pcre2_jit_compile (mc_search_cond
->regex_handle
, PCRE2_JIT_COMPLETE
) && *error
!= '\0')
890 lc_mc_search
->regex_match_info
= pcre_study (mc_search_cond
->regex_handle
, 0, &error
);
891 if (lc_mc_search
->regex_match_info
== NULL
&& error
!= NULL
)
894 mc_search_set_error (lc_mc_search
, MC_SEARCH_E_REGEX_COMPILE
, "%s", error
);
895 MC_PTR_FREE (mc_search_cond
->regex_handle
);
898 #endif /* SEARCH_TYPE_GLIB */
901 lc_mc_search
->is_utf8
= str_isutf8 (charset
);
904 /* --------------------------------------------------------------------------------------------- */
907 mc_search__run_regex (mc_search_t
*lc_mc_search
, const void *user_data
,
908 gsize start_search
, gsize end_search
, gsize
*found_len
)
910 mc_search_cbret_t ret
= MC_SEARCH_CB_NOTFOUND
;
911 gsize current_pos
, virtual_pos
;
915 if (lc_mc_search
->regex_buffer
!= NULL
)
916 g_string_set_size (lc_mc_search
->regex_buffer
, 0);
918 lc_mc_search
->regex_buffer
= g_string_sized_new (64);
920 virtual_pos
= current_pos
= start_search
;
921 while (virtual_pos
<= end_search
)
923 g_string_set_size (lc_mc_search
->regex_buffer
, 0);
924 lc_mc_search
->start_buffer
= current_pos
;
926 if (lc_mc_search
->search_fn
!= NULL
)
930 int current_chr
= '\n'; /* stop search symbol */
932 ret
= lc_mc_search
->search_fn (user_data
, current_pos
, ¤t_chr
);
934 if (ret
== MC_SEARCH_CB_ABORT
)
937 if (ret
== MC_SEARCH_CB_INVALID
)
942 if (ret
== MC_SEARCH_CB_SKIP
)
947 g_string_append_c (lc_mc_search
->regex_buffer
, (char) current_chr
);
949 if ((char) current_chr
== '\n' || virtual_pos
> end_search
)
955 /* optimization for standard case (for search from file manager)
956 * where there is no MC_SEARCH_CB_INVALID or MC_SEARCH_CB_SKIP
957 * return codes, so we can copy line at regex buffer all at once
961 const char current_chr
= ((const char *) user_data
)[current_pos
];
963 if (current_chr
== '\0')
968 if (current_chr
== '\n' || current_pos
> end_search
)
972 /* use virtual_pos as index of start of current chunk */
973 g_string_append_len (lc_mc_search
->regex_buffer
, (const char *) user_data
+ virtual_pos
,
974 current_pos
- virtual_pos
);
975 virtual_pos
= current_pos
;
978 switch (mc_search__regex_found_cond (lc_mc_search
, lc_mc_search
->regex_buffer
))
981 #ifdef SEARCH_TYPE_GLIB
982 g_match_info_fetch_pos (lc_mc_search
->regex_match_info
, 0, &start_pos
, &end_pos
);
983 #else /* SEARCH_TYPE_GLIB */
984 start_pos
= lc_mc_search
->iovector
[0];
985 end_pos
= lc_mc_search
->iovector
[1];
986 #endif /* SEARCH_TYPE_GLIB */
987 if (found_len
!= NULL
)
988 *found_len
= end_pos
- start_pos
;
989 lc_mc_search
->normal_offset
= lc_mc_search
->start_buffer
+ start_pos
;
991 case COND__NOT_ALL_FOUND
:
994 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
995 lc_mc_search
->regex_buffer
= NULL
;
999 if ((lc_mc_search
->update_fn
!= NULL
) &&
1000 ((lc_mc_search
->update_fn
) (user_data
, current_pos
) == MC_SEARCH_CB_ABORT
))
1001 ret
= MC_SEARCH_CB_ABORT
;
1003 if (ret
== MC_SEARCH_CB_ABORT
|| ret
== MC_SEARCH_CB_NOTFOUND
)
1007 g_string_free (lc_mc_search
->regex_buffer
, TRUE
);
1008 lc_mc_search
->regex_buffer
= NULL
;
1010 MC_PTR_FREE (lc_mc_search
->error_str
);
1011 lc_mc_search
->error
= ret
== MC_SEARCH_CB_ABORT
? MC_SEARCH_E_ABORT
: MC_SEARCH_E_NOTFOUND
;
1016 /* --------------------------------------------------------------------------------------------- */
1019 mc_search_regex_prepare_replace_str (mc_search_t
*lc_mc_search
, GString
*replace_str
)
1023 int num_replace_tokens
;
1026 replace_transform_type_t replace_flags
= REPLACE_T_NO_TRANSFORM
;
1028 num_replace_tokens
=
1029 mc_search_regex__get_max_num_of_replace_tokens (replace_str
->str
, replace_str
->len
);
1031 if (lc_mc_search
->num_results
< 0)
1032 return mc_g_string_dup (replace_str
);
1034 if (num_replace_tokens
> lc_mc_search
->num_results
- 1
1035 || num_replace_tokens
> MC_SEARCH__NUM_REPLACE_ARGS
)
1037 mc_search_set_error (lc_mc_search
, MC_SEARCH_E_REGEX_REPLACE
, "%s",
1038 _(STR_E_RPL_NOT_EQ_TO_FOUND
));
1042 ret
= g_string_sized_new (64);
1044 for (loop
= 0; loop
< replace_str
->len
- 1; loop
++)
1050 lc_index
= mc_search_regex__process_replace_str (replace_str
, loop
, &len
, &replace_flags
);
1052 if (lc_index
== REPLACE_PREPARE_T_NOTHING_SPECIAL
)
1056 mc_search_regex__process_append_str (ret
, replace_str
->str
+ prev
, loop
- prev
,
1058 mc_search_regex__process_append_str (ret
, replace_str
->str
+ loop
+ 1, len
- 1,
1061 loop
= prev
- 1; /* prepare to loop++ */
1067 if (lc_index
== REPLACE_PREPARE_T_REPLACE_FLAG
)
1070 mc_search_regex__process_append_str (ret
, replace_str
->str
+ prev
, loop
- prev
,
1073 loop
= prev
- 1; /* prepare to loop++ */
1077 /* escape sequence */
1078 if (lc_index
== REPLACE_PREPARE_T_ESCAPE_SEQ
)
1080 mc_search_regex__process_append_str (ret
, replace_str
->str
+ prev
, loop
- prev
,
1082 /* call process_escape_sequence without starting '\\' */
1083 mc_search_regex__process_escape_sequence (ret
, replace_str
->str
+ loop
+ 1, len
- 1,
1084 &replace_flags
, lc_mc_search
->is_utf8
);
1086 loop
= prev
- 1; /* prepare to loop++ */
1090 /* invalid capture buffer number */
1091 if (lc_index
> lc_mc_search
->num_results
)
1093 g_string_free (ret
, TRUE
);
1094 mc_search_set_error (lc_mc_search
, MC_SEARCH_E_REGEX_REPLACE
,
1095 _(STR_E_RPL_INVALID_TOKEN
), lc_index
);
1099 tmp_str
= mc_search_regex__get_token_by_num (lc_mc_search
, lc_index
);
1102 mc_search_regex__process_append_str (ret
, replace_str
->str
+ prev
, loop
- prev
,
1105 mc_search_regex__process_append_str (ret
, tmp_str
, -1, &replace_flags
);
1109 loop
= prev
- 1; /* prepare to loop++ */
1112 mc_search_regex__process_append_str (ret
, replace_str
->str
+ prev
, replace_str
->len
- prev
,