(copy_file_file_display_progress): remove dead code.
[midnight-commander.git] / lib / search / regex.c
blob465c3d3449164fafe3879b3b2ad68b0dac4d7f6c
1 /*
2 Search text engine.
3 Regex search
5 Copyright (C) 2009-2024
6 Free Software Foundation, Inc.
8 Written by:
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/>.
29 #include <config.h>
31 #include <stdlib.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.h"
36 #include "lib/util.h" /* MC_PTR_FREE */
38 #include "internal.h"
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 ****************************************************************/
50 typedef enum
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 /* --------------------------------------------------------------------------------------------- */
67 static gboolean
68 mc_search__regex_str_append_if_special (GString *copy_to, const GString *regex_str, gsize *offset)
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
86 char *tmp_regex_str;
87 const char **spec_chr;
89 tmp_regex_str = &(regex_str->str[*offset]);
91 for (spec_chr = special_chars; *spec_chr != NULL; spec_chr++)
93 gsize spec_chr_len;
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] != '{')
103 spec_chr_len += 2;
104 else
106 while ((spec_chr_len < regex_str->len - *offset)
107 && tmp_regex_str[spec_chr_len] != '}')
108 spec_chr_len++;
109 if (tmp_regex_str[spec_chr_len] == '}')
110 spec_chr_len++;
113 g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
114 *offset += spec_chr_len;
115 return TRUE;
119 return FALSE;
122 /* --------------------------------------------------------------------------------------------- */
124 static void
125 mc_search__cond_struct_new_regex_hex_add (const char *charset, GString *str_to,
126 const GString *one_char)
128 GString *upp, *low;
129 gsize loop;
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]" */
137 gint tmp_len;
139 if (loop >= low->len || upp->str[loop] == low->str[loop])
140 tmp_len =
141 g_snprintf (tmp_str, sizeof (tmp_str), "\\x%02X", (unsigned char) upp->str[loop]);
142 else
143 tmp_len =
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 /* --------------------------------------------------------------------------------------------- */
156 static void
157 mc_search__cond_struct_new_regex_accum_append (const char *charset, GString *str_to,
158 GString *str_from)
160 GString *recoded_part;
161 gsize loop = 0;
163 recoded_part = g_string_sized_new (32);
165 while (loop < str_from->len)
167 GString *one_char;
168 gboolean just_letters;
170 one_char =
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)
175 loop++;
176 else
178 loop += one_char->len;
180 if (just_letters)
181 mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char);
182 else
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
207 * this job itself.
209 static GString *
210 mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString *astr)
212 GString *accumulator, *spec_char, *ret_str;
213 gsize loop;
215 ret_str = g_string_sized_new (64);
216 accumulator = g_string_sized_new (64);
217 spec_char = g_string_sized_new (64);
218 loop = 0;
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);
227 continue;
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]);
238 loop++;
241 g_string_append_c (ret_str, astr->str[loop]);
242 loop++;
243 continue;
246 TODO: handle [ and ]
248 g_string_append_c (accumulator, astr->str[loop]);
249 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);
256 return ret_str;
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. */
266 static gboolean
267 mc_search__g_regex_match_full_safe (const GRegex *regex,
268 const gchar *string,
269 gssize string_len,
270 gint start_position,
271 GRegexMatchFlags match_options,
272 GMatchInfo **match_info, GError **error)
274 char *string_safe, *p, *end;
275 gboolean ret;
277 if (string_len < 0)
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,
284 match_info, error);
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;
293 while (p < end)
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);
300 else
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. */
305 *p++ = '\0';
309 ret =
310 g_regex_match_full (regex, string_safe, string_len, start_position, match_options,
311 match_info, error);
312 g_free (string_safe);
313 return ret;
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,
321 GString *search_str)
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;
332 if (mcerror != 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 =
347 #ifdef HAVE_PCRE2
348 pcre2_match (regex, (unsigned char *) search_str->str, search_str->len, 0, 0,
349 lc_mc_search->regex_match_info, NULL);
350 #else
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);
353 #endif
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)
368 gsize loop1;
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;
375 mc_search_cond =
376 (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->prepared.conditions, loop1);
378 if (!mc_search_cond->regex_handle)
379 continue;
381 ret =
382 mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
383 search_str);
384 if (ret != COND__NOT_FOUND)
385 return ret;
387 return COND__NOT_ALL_FOUND;
390 /* --------------------------------------------------------------------------------------------- */
392 static int
393 mc_search_regex__get_max_num_of_replace_tokens (const gchar *str, gsize len)
395 int max_token = 0;
396 gsize loop;
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]))
402 continue;
403 if (max_token < str[loop + 1] - '0')
404 max_token = str[loop + 1] - '0';
406 else if (str[loop] == '$' && str[loop + 1] == '{')
408 gsize tmp_len;
410 if (str_is_char_escaped (str, &str[loop]))
411 continue;
413 for (tmp_len = 0;
414 loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
415 tmp_len++);
417 if (str[loop + 2 + tmp_len] == '}')
419 int tmp_token;
420 char *tmp_str;
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;
426 g_free (tmp_str);
430 return max_token;
433 /* --------------------------------------------------------------------------------------------- */
435 static char *
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 /* --------------------------------------------------------------------------------------------- */
456 static gboolean
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)
465 if (c == '{')
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] == '}')
474 (*skip_len)++;
475 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
476 return FALSE;
478 else
480 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
481 return TRUE;
485 if (c == 'x')
487 *skip_len = 2; /* \x */
488 c = curr_str[2];
489 if (c == '{')
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] == '}')
498 (*skip_len)++;
499 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
500 return FALSE;
502 else
504 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
505 return TRUE;
508 else if (!g_ascii_isxdigit ((guchar) c))
510 *skip_len = 2; /* \x without number behind */
511 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
512 return FALSE;
514 else
516 c = curr_str[3];
517 if (!g_ascii_isxdigit ((guchar) c))
518 *skip_len = 3; /* \xH */
519 else
520 *skip_len = 4; /* \xHH */
521 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
522 return FALSE;
527 if (strchr ("ntvbrfa", c) != NULL)
529 *skip_len = 2;
530 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
531 return FALSE;
533 return TRUE;
536 /* --------------------------------------------------------------------------------------------- */
538 static int
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)
542 int ret = -1;
543 const char *curr_str = &(replace_str->str[current_pos]);
545 if (current_pos > replace_str->len)
546 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
548 *skip_len = 0;
550 if (replace_str->len > current_pos + 2 && curr_str[0] == '$' && curr_str[1] == '{'
551 && (curr_str[2] & (char) 0xf0) == 0x30)
553 char *tmp_str;
555 if (str_is_char_escaped (replace_str->str, curr_str))
557 *skip_len = 1;
558 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
561 for (*skip_len = 0;
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);
570 if (tmp_str == NULL)
571 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
573 ret = atoi (tmp_str);
574 g_free (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))
584 *skip_len = 1;
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 */
592 return ret;
595 if (!mc_search_regex__replace_handle_esc_seq (replace_str, current_pos, skip_len, &ret))
596 return ret;
598 ret = REPLACE_PREPARE_T_REPLACE_FLAG;
599 *skip_len += 2;
601 switch (curr_str[1])
603 case 'U':
604 *replace_flags |= REPLACE_T_UPP_TRANSFORM;
605 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
606 break;
607 case 'u':
608 *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
609 break;
610 case 'L':
611 *replace_flags |= REPLACE_T_LOW_TRANSFORM;
612 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
613 break;
614 case 'l':
615 *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
616 break;
617 case 'E':
618 *replace_flags = REPLACE_T_NO_TRANSFORM;
619 break;
620 default:
621 ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
622 break;
625 return ret;
628 /* --------------------------------------------------------------------------------------------- */
630 static void
631 mc_search_regex__process_append_str (GString *dest_str, const char *from, gsize len,
632 replace_transform_type_t *replace_flags)
634 gsize loop;
635 gsize char_len;
637 if (len == (gsize) (-1))
638 len = strlen (from);
640 if (*replace_flags == REPLACE_T_NO_TRANSFORM)
642 g_string_append_len (dest_str, from, len);
643 return;
646 for (loop = 0; loop < len; loop += char_len)
648 GString *tmp_string = NULL;
649 GString *s;
651 s = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
652 char_len = s->len;
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 /* --------------------------------------------------------------------------------------------- */
685 static void
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)
689 gsize i = 0;
690 unsigned int c = 0;
691 char b;
693 if (len == (gsize) (-1))
694 len = strlen (from);
695 if (len == 0)
696 return;
698 if (from[i] == '{')
699 i++;
700 if (i >= len)
701 return;
703 if (from[i] == 'x')
705 i++;
706 if (i < len && from[i] == '{')
707 i++;
708 for (; i < len; 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';
716 else
717 break;
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';
723 else
725 switch (from[i])
727 case 'n':
728 c = '\n';
729 break;
730 case 't':
731 c = '\t';
732 break;
733 case 'v':
734 c = '\v';
735 break;
736 case 'b':
737 c = '\b';
738 break;
739 case 'r':
740 c = '\r';
741 break;
742 case 'f':
743 c = '\f';
744 break;
745 case 'a':
746 c = '\a';
747 break;
748 default:
749 mc_search_regex__process_append_str (dest_str, from, len, replace_flags);
750 return;
754 if (c < 0x80 || !is_utf8)
755 g_string_append_c (dest_str, (char) c);
756 else if (c < 0x800)
758 b = 0xC0 | (c >> 6);
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 /* --------------------------------------------------------------------------------------------- */
789 void
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;
812 else
814 g_regex_options |= G_REGEX_RAW;
816 if (!lc_mc_search->is_case_sensitive)
818 GString *tmp;
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);
829 if (mcerror != NULL)
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);
836 return;
838 #else /* SEARCH_TYPE_GLIB */
840 #ifdef HAVE_PCRE2
841 int errcode;
842 char error[BUF_SMALL];
843 size_t erroffset;
844 int pcre_options = PCRE2_MULTILINE;
845 #else
846 const char *error;
847 int erroffset;
848 int pcre_options = PCRE_EXTRA | PCRE_MULTILINE;
849 #endif
851 if (str_isutf8 (charset) && mc_global.utf8_display)
853 #ifdef HAVE_PCRE2
854 pcre_options |= PCRE2_UTF;
855 if (!lc_mc_search->is_case_sensitive)
856 pcre_options |= PCRE2_CASELESS;
857 #else
858 pcre_options |= PCRE_UTF8;
859 if (!lc_mc_search->is_case_sensitive)
860 pcre_options |= PCRE_CASELESS;
861 #endif
863 else if (!lc_mc_search->is_case_sensitive)
865 GString *tmp;
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 =
873 #ifdef HAVE_PCRE2
874 pcre2_compile ((unsigned char *) mc_search_cond->str->str, PCRE2_ZERO_TERMINATED,
875 pcre_options, &errcode, &erroffset, NULL);
876 #else
877 pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
878 #endif
879 if (mc_search_cond->regex_handle == NULL)
881 #ifdef HAVE_PCRE2
882 pcre2_get_error_message (errcode, (unsigned char *) error, sizeof (error));
883 #endif
884 mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
885 return;
887 #ifdef HAVE_PCRE2
888 if (pcre2_jit_compile (mc_search_cond->regex_handle, PCRE2_JIT_COMPLETE) && *error != '\0')
889 #else
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)
892 #endif
894 mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
895 MC_PTR_FREE (mc_search_cond->regex_handle);
896 return;
898 #endif /* SEARCH_TYPE_GLIB */
901 lc_mc_search->is_utf8 = str_isutf8 (charset);
904 /* --------------------------------------------------------------------------------------------- */
906 gboolean
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;
912 gint start_pos;
913 gint end_pos;
915 if (lc_mc_search->regex_buffer != NULL)
916 g_string_set_size (lc_mc_search->regex_buffer, 0);
917 else
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)
928 while (TRUE)
930 int current_chr = '\n'; /* stop search symbol */
932 ret = lc_mc_search->search_fn (user_data, current_pos, &current_chr);
934 if (ret == MC_SEARCH_CB_ABORT)
935 break;
937 if (ret == MC_SEARCH_CB_INVALID)
938 continue;
940 current_pos++;
942 if (ret == MC_SEARCH_CB_SKIP)
943 continue;
945 virtual_pos++;
947 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
949 if ((char) current_chr == '\n' || virtual_pos > end_search)
950 break;
953 else
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
959 while (TRUE)
961 const char current_chr = ((const char *) user_data)[current_pos];
963 if (current_chr == '\0')
964 break;
966 current_pos++;
968 if (current_chr == '\n' || current_pos > end_search)
969 break;
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))
980 case COND__FOUND_OK:
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;
990 return TRUE;
991 case COND__NOT_ALL_FOUND:
992 break;
993 default:
994 g_string_free (lc_mc_search->regex_buffer, TRUE);
995 lc_mc_search->regex_buffer = NULL;
996 return FALSE;
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)
1004 break;
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;
1013 return FALSE;
1016 /* --------------------------------------------------------------------------------------------- */
1018 GString *
1019 mc_search_regex_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
1021 GString *ret;
1023 int num_replace_tokens;
1024 gsize loop;
1025 gsize prev = 0;
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));
1039 return NULL;
1042 ret = g_string_sized_new (64);
1044 for (loop = 0; loop < replace_str->len - 1; loop++)
1046 int lc_index;
1047 gchar *tmp_str;
1048 gsize len = 0;
1050 lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
1052 if (lc_index == REPLACE_PREPARE_T_NOTHING_SPECIAL)
1054 if (len != 0)
1056 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1057 &replace_flags);
1058 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
1059 &replace_flags);
1060 prev = loop + len;
1061 loop = prev - 1; /* prepare to loop++ */
1064 continue;
1067 if (lc_index == REPLACE_PREPARE_T_REPLACE_FLAG)
1069 if (loop != 0)
1070 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1071 &replace_flags);
1072 prev = loop + len;
1073 loop = prev - 1; /* prepare to loop++ */
1074 continue;
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,
1081 &replace_flags);
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);
1085 prev = loop + len;
1086 loop = prev - 1; /* prepare to loop++ */
1087 continue;
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);
1096 return NULL;
1099 tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
1101 if (loop != 0)
1102 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1103 &replace_flags);
1105 mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
1106 g_free (tmp_str);
1108 prev = loop + len;
1109 loop = prev - 1; /* prepare to loop++ */
1112 mc_search_regex__process_append_str (ret, replace_str->str + prev, replace_str->len - prev,
1113 &replace_flags);
1115 return ret;