This source file includes following definitions.
- mc_search__regex_str_append_if_special
- mc_search__cond_struct_new_regex_hex_add
- mc_search__cond_struct_new_regex_accum_append
- mc_search__cond_struct_new_regex_ci_str
- mc_search__g_regex_match_full_safe
- mc_search__regex_found_cond_one
- mc_search__regex_found_cond
- mc_search_regex__get_max_num_of_replace_tokens
- mc_search_regex__get_token_by_num
- mc_search_regex__replace_handle_esc_seq
- mc_search_regex__process_replace_str
- mc_search_regex__process_append_str
- mc_search_regex__process_escape_sequence
- mc_search__cond_struct_new_init_regex
- mc_search__run_regex
- mc_search_regex_prepare_replace_str
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <config.h>
30
31 #include <stdlib.h>
32
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.h"
36 #include "lib/strescape.h"
37 #include "lib/util.h"
38
39 #include "internal.h"
40
41
42
43
44
45 #define REPLACE_PREPARE_T_NOTHING_SPECIAL -1
46 #define REPLACE_PREPARE_T_REPLACE_FLAG -2
47 #define REPLACE_PREPARE_T_ESCAPE_SEQ -3
48
49
50
51 typedef enum
52 {
53 REPLACE_T_NO_TRANSFORM = 0,
54 REPLACE_T_UPP_TRANSFORM_CHAR = 1,
55 REPLACE_T_LOW_TRANSFORM_CHAR = 2,
56 REPLACE_T_UPP_TRANSFORM = 4,
57 REPLACE_T_LOW_TRANSFORM = 8
58 } replace_transform_type_t;
59
60
61
62
63
64
65
66
67
68 static gboolean
69 mc_search__regex_str_append_if_special (GString * copy_to, const GString * regex_str,
70 gsize * offset)
71 {
72 const char *special_chars[] = {
73 "\\s", "\\S",
74 "\\d", "\\D",
75 "\\b", "\\B",
76 "\\w", "\\W",
77 "\\t", "\\n",
78 "\\r", "\\f",
79 "\\a", "\\e",
80 "\\x", "\\X",
81 "\\c", "\\C",
82 "\\l", "\\L",
83 "\\u", "\\U",
84 "\\E", "\\Q",
85 NULL
86 };
87
88 char *tmp_regex_str;
89 const char **spec_chr;
90
91 tmp_regex_str = &(regex_str->str[*offset]);
92
93 for (spec_chr = special_chars; *spec_chr != NULL; spec_chr++)
94 {
95 gsize spec_chr_len;
96
97 spec_chr_len = strlen (*spec_chr);
98
99 if (strncmp (tmp_regex_str, *spec_chr, spec_chr_len) == 0
100 && !strutils_is_char_escaped (regex_str->str, tmp_regex_str))
101 {
102 if (strncmp ("\\x", *spec_chr, spec_chr_len) == 0)
103 {
104 if (tmp_regex_str[spec_chr_len] != '{')
105 spec_chr_len += 2;
106 else
107 {
108 while ((spec_chr_len < regex_str->len - *offset)
109 && tmp_regex_str[spec_chr_len] != '}')
110 spec_chr_len++;
111 if (tmp_regex_str[spec_chr_len] == '}')
112 spec_chr_len++;
113 }
114 }
115 g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
116 *offset += spec_chr_len;
117 return TRUE;
118 }
119 }
120
121 return FALSE;
122 }
123
124
125
126 static void
127 mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
128 const GString * one_char)
129 {
130 GString *upp, *low;
131 gsize loop;
132
133 upp = mc_search__toupper_case_str (charset, one_char);
134 low = mc_search__tolower_case_str (charset, one_char);
135
136 for (loop = 0; loop < upp->len; loop++)
137 {
138 gchar tmp_str[10 + 1];
139 gint tmp_len;
140
141 if (loop >= low->len || upp->str[loop] == low->str[loop])
142 tmp_len =
143 g_snprintf (tmp_str, sizeof (tmp_str), "\\x%02X", (unsigned char) upp->str[loop]);
144 else
145 tmp_len =
146 g_snprintf (tmp_str, sizeof (tmp_str), "[\\x%02X\\x%02X]",
147 (unsigned char) upp->str[loop], (unsigned char) low->str[loop]);
148
149 g_string_append_len (str_to, tmp_str, tmp_len);
150 }
151
152 g_string_free (upp, TRUE);
153 g_string_free (low, TRUE);
154 }
155
156
157
158 static void
159 mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
160 GString * str_from)
161 {
162 GString *recoded_part;
163 gsize loop = 0;
164
165 recoded_part = g_string_sized_new (32);
166
167 while (loop < str_from->len)
168 {
169 GString *one_char;
170 gboolean just_letters;
171
172 one_char =
173 mc_search__get_one_symbol (charset, str_from->str + loop,
174 MIN (str_from->len - loop, 6), &just_letters);
175
176 if (one_char->len == 0)
177 loop++;
178 else
179 {
180 loop += one_char->len;
181
182 if (just_letters)
183 mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char);
184 else
185 g_string_append_len (recoded_part, one_char->str, one_char->len);
186 }
187
188 g_string_free (one_char, TRUE);
189 }
190
191 g_string_append_len (str_to, recoded_part->str, recoded_part->len);
192 g_string_free (recoded_part, TRUE);
193 g_string_set_size (str_from, 0);
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 static GString *
212 mc_search__cond_struct_new_regex_ci_str (const char *charset, const GString * astr)
213 {
214 GString *accumulator, *spec_char, *ret_str;
215 gsize loop;
216
217 ret_str = g_string_sized_new (64);
218 accumulator = g_string_sized_new (64);
219 spec_char = g_string_sized_new (64);
220 loop = 0;
221
222 while (loop < astr->len)
223 {
224 if (mc_search__regex_str_append_if_special (spec_char, astr, &loop))
225 {
226 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
227 g_string_append_len (ret_str, spec_char->str, spec_char->len);
228 g_string_set_size (spec_char, 0);
229 continue;
230 }
231
232 if (astr->str[loop] == '[' && !strutils_is_char_escaped (astr->str, &(astr->str[loop])))
233 {
234 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
235
236 while (loop < astr->len && !(astr->str[loop] == ']'
237 && !strutils_is_char_escaped (astr->str,
238 &(astr->str[loop]))))
239 {
240 g_string_append_c (ret_str, astr->str[loop]);
241 loop++;
242 }
243
244 g_string_append_c (ret_str, astr->str[loop]);
245 loop++;
246 continue;
247 }
248
249
250
251 g_string_append_c (accumulator, astr->str[loop]);
252 loop++;
253 }
254 mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
255
256 g_string_free (accumulator, TRUE);
257 g_string_free (spec_char, TRUE);
258
259 return ret_str;
260 }
261
262
263
264 #ifdef SEARCH_TYPE_GLIB
265
266
267
268
269 static gboolean
270 mc_search__g_regex_match_full_safe (const GRegex * regex,
271 const gchar * string,
272 gssize string_len,
273 gint start_position,
274 GRegexMatchFlags match_options,
275 GMatchInfo ** match_info, GError ** error)
276 {
277 char *string_safe, *p, *end;
278 gboolean ret;
279
280 if (string_len < 0)
281 string_len = strlen (string);
282
283 if ((g_regex_get_compile_flags (regex) & G_REGEX_RAW)
284 || g_utf8_validate (string, string_len, NULL))
285 {
286 return g_regex_match_full (regex, string, string_len, start_position, match_options,
287 match_info, error);
288 }
289
290
291 p = string_safe = g_malloc (string_len + 1);
292 memcpy (string_safe, string, string_len);
293 string_safe[string_len] = '\0';
294 end = p + string_len;
295
296 while (p < end)
297 {
298 gunichar c = g_utf8_get_char_validated (p, -1);
299 if (c != (gunichar) (-1) && c != (gunichar) (-2))
300 {
301 p = g_utf8_next_char (p);
302 }
303 else
304 {
305
306
307
308 *p++ = '\0';
309 }
310 }
311
312 ret =
313 g_regex_match_full (regex, string_safe, string_len, start_position, match_options,
314 match_info, error);
315 g_free (string_safe);
316 return ret;
317 }
318 #endif
319
320
321
322 static mc_search__found_cond_t
323 mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t * regex,
324 GString * search_str)
325 {
326 #ifdef SEARCH_TYPE_GLIB
327 GError *mcerror = NULL;
328
329 if (!mc_search__g_regex_match_full_safe
330 (regex, search_str->str, search_str->len, 0, G_REGEX_MATCH_NEWLINE_ANY,
331 &lc_mc_search->regex_match_info, &mcerror))
332 {
333 g_match_info_free (lc_mc_search->regex_match_info);
334 lc_mc_search->regex_match_info = NULL;
335 if (mcerror != NULL)
336 {
337 lc_mc_search->error = MC_SEARCH_E_REGEX;
338 g_free (lc_mc_search->error_str);
339 lc_mc_search->error_str =
340 str_conv_gerror_message (mcerror, _("Regular expression error"));
341 g_error_free (mcerror);
342 return COND__FOUND_ERROR;
343 }
344 return COND__NOT_FOUND;
345 }
346 lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
347 #else
348
349 lc_mc_search->num_results =
350 #ifdef HAVE_PCRE2
351 pcre2_match (regex, (unsigned char *) search_str->str, search_str->len, 0, 0,
352 lc_mc_search->regex_match_info, NULL);
353 #else
354 pcre_exec (regex, lc_mc_search->regex_match_info, search_str->str, search_str->len, 0, 0,
355 lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
356 #endif
357 if (lc_mc_search->num_results < 0)
358 {
359 return COND__NOT_FOUND;
360 }
361 #endif
362 return COND__FOUND_OK;
363
364 }
365
366
367
368 static mc_search__found_cond_t
369 mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
370 {
371 gsize loop1;
372
373 for (loop1 = 0; loop1 < lc_mc_search->prepared.conditions->len; loop1++)
374 {
375 mc_search_cond_t *mc_search_cond;
376 mc_search__found_cond_t ret;
377
378 mc_search_cond =
379 (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->prepared.conditions, loop1);
380
381 if (!mc_search_cond->regex_handle)
382 continue;
383
384 ret =
385 mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
386 search_str);
387 if (ret != COND__NOT_FOUND)
388 return ret;
389 }
390 return COND__NOT_ALL_FOUND;
391 }
392
393
394
395 static int
396 mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
397 {
398 int max_token = 0;
399 gsize loop;
400
401 for (loop = 0; loop < len - 1; loop++)
402 if (str[loop] == '\\' && g_ascii_isdigit (str[loop + 1]))
403 {
404 if (strutils_is_char_escaped (str, &str[loop]))
405 continue;
406 if (max_token < str[loop + 1] - '0')
407 max_token = str[loop + 1] - '0';
408 }
409 else if (str[loop] == '$' && str[loop + 1] == '{')
410 {
411 gsize tmp_len;
412
413 if (strutils_is_char_escaped (str, &str[loop]))
414 continue;
415
416 for (tmp_len = 0;
417 loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
418 tmp_len++);
419
420 if (str[loop + 2 + tmp_len] == '}')
421 {
422 int tmp_token;
423 char *tmp_str;
424
425 tmp_str = g_strndup (&str[loop + 2], tmp_len);
426 tmp_token = atoi (tmp_str);
427 if (max_token < tmp_token)
428 max_token = tmp_token;
429 g_free (tmp_str);
430 }
431 }
432
433 return max_token;
434 }
435
436
437
438 static char *
439 mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
440 {
441 int fnd_start = 0, fnd_end = 0;
442
443 #ifdef SEARCH_TYPE_GLIB
444 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
445 #else
446 fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
447 fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
448 #endif
449
450 if (fnd_end == fnd_start)
451 return g_strdup ("");
452
453 return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
454
455 }
456
457
458
459 static gboolean
460 mc_search_regex__replace_handle_esc_seq (const GString * replace_str, const gsize current_pos,
461 gsize * skip_len, int *ret)
462 {
463 char *curr_str = &(replace_str->str[current_pos]);
464 char c = curr_str[1];
465
466 if (replace_str->len > current_pos + 2)
467 {
468 if (c == '{')
469 {
470 for (*skip_len = 2;
471 current_pos + *skip_len < replace_str->len && curr_str[*skip_len] >= '0'
472 && curr_str[*skip_len] <= '7'; (*skip_len)++)
473 ;
474
475 if (current_pos + *skip_len < replace_str->len && curr_str[*skip_len] == '}')
476 {
477 (*skip_len)++;
478 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
479 return FALSE;
480 }
481 else
482 {
483 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
484 return TRUE;
485 }
486 }
487
488 if (c == 'x')
489 {
490 *skip_len = 2;
491 c = curr_str[2];
492 if (c == '{')
493 {
494 for (*skip_len = 3;
495 current_pos + *skip_len < replace_str->len
496 && g_ascii_isxdigit ((guchar) curr_str[*skip_len]); (*skip_len)++)
497 ;
498
499 if (current_pos + *skip_len < replace_str->len && curr_str[*skip_len] == '}')
500 {
501 (*skip_len)++;
502 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
503 return FALSE;
504 }
505 else
506 {
507 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
508 return TRUE;
509 }
510 }
511 else if (!g_ascii_isxdigit ((guchar) c))
512 {
513 *skip_len = 2;
514 *ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
515 return FALSE;
516 }
517 else
518 {
519 c = curr_str[3];
520 if (!g_ascii_isxdigit ((guchar) c))
521 *skip_len = 3;
522 else
523 *skip_len = 4;
524 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
525 return FALSE;
526 }
527 }
528 }
529
530 if (strchr ("ntvbrfa", c) != NULL)
531 {
532 *skip_len = 2;
533 *ret = REPLACE_PREPARE_T_ESCAPE_SEQ;
534 return FALSE;
535 }
536 return TRUE;
537 }
538
539
540
541 static int
542 mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
543 gsize * skip_len, replace_transform_type_t * replace_flags)
544 {
545 int ret = -1;
546 const char *curr_str = &(replace_str->str[current_pos]);
547
548 if (current_pos > replace_str->len)
549 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
550
551 *skip_len = 0;
552
553 if (replace_str->len > current_pos + 2 && curr_str[0] == '$' && curr_str[1] == '{'
554 && (curr_str[2] & (char) 0xf0) == 0x30)
555 {
556 char *tmp_str;
557
558 if (strutils_is_char_escaped (replace_str->str, curr_str))
559 {
560 *skip_len = 1;
561 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
562 }
563
564 for (*skip_len = 0;
565 current_pos + *skip_len + 2 < replace_str->len
566 && (curr_str[2 + *skip_len] & (char) 0xf0) == 0x30; (*skip_len)++)
567 ;
568
569 if (curr_str[2 + *skip_len] != '}')
570 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
571
572 tmp_str = g_strndup (curr_str + 2, *skip_len);
573 if (tmp_str == NULL)
574 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
575
576 ret = atoi (tmp_str);
577 g_free (tmp_str);
578
579 *skip_len += 3;
580 return ret;
581 }
582
583 if (curr_str[0] == '\\' && replace_str->len > current_pos + 1)
584 {
585 if (strutils_is_char_escaped (replace_str->str, curr_str))
586 {
587 *skip_len = 1;
588 return REPLACE_PREPARE_T_NOTHING_SPECIAL;
589 }
590
591 if (g_ascii_isdigit (curr_str[1]))
592 {
593 ret = g_ascii_digit_value (curr_str[1]);
594 *skip_len = 2;
595 return ret;
596 }
597
598 if (!mc_search_regex__replace_handle_esc_seq (replace_str, current_pos, skip_len, &ret))
599 return ret;
600
601 ret = REPLACE_PREPARE_T_REPLACE_FLAG;
602 *skip_len += 2;
603
604 switch (curr_str[1])
605 {
606 case 'U':
607 *replace_flags |= REPLACE_T_UPP_TRANSFORM;
608 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
609 break;
610 case 'u':
611 *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
612 break;
613 case 'L':
614 *replace_flags |= REPLACE_T_LOW_TRANSFORM;
615 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
616 break;
617 case 'l':
618 *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
619 break;
620 case 'E':
621 *replace_flags = REPLACE_T_NO_TRANSFORM;
622 break;
623 default:
624 ret = REPLACE_PREPARE_T_NOTHING_SPECIAL;
625 break;
626 }
627 }
628 return ret;
629 }
630
631
632
633 static void
634 mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
635 replace_transform_type_t * replace_flags)
636 {
637 gsize loop;
638 gsize char_len;
639
640 if (len == (gsize) (-1))
641 len = strlen (from);
642
643 if (*replace_flags == REPLACE_T_NO_TRANSFORM)
644 {
645 g_string_append_len (dest_str, from, len);
646 return;
647 }
648
649 for (loop = 0; loop < len; loop += char_len)
650 {
651 GString *tmp_string = NULL;
652 GString *s;
653
654 s = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
655 char_len = s->len;
656
657 if ((*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR) != 0)
658 {
659 *replace_flags &= ~REPLACE_T_UPP_TRANSFORM_CHAR;
660 tmp_string = mc_search__toupper_case_str (NULL, s);
661 g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
662 }
663 else if ((*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR) != 0)
664 {
665 *replace_flags &= ~REPLACE_T_LOW_TRANSFORM_CHAR;
666 tmp_string = mc_search__tolower_case_str (NULL, s);
667 g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
668 }
669 else if ((*replace_flags & REPLACE_T_UPP_TRANSFORM) != 0)
670 {
671 tmp_string = mc_search__toupper_case_str (NULL, s);
672 g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
673 }
674 else if ((*replace_flags & REPLACE_T_LOW_TRANSFORM) != 0)
675 {
676 tmp_string = mc_search__tolower_case_str (NULL, s);
677 g_string_append_len (dest_str, tmp_string->str, tmp_string->len);
678 }
679
680 g_string_free (s, TRUE);
681 if (tmp_string != NULL)
682 g_string_free (tmp_string, TRUE);
683 }
684 }
685
686
687
688 static void
689 mc_search_regex__process_escape_sequence (GString * dest_str, const char *from, gsize len,
690 replace_transform_type_t * replace_flags,
691 gboolean is_utf8)
692 {
693 gsize i = 0;
694 unsigned int c = 0;
695 char b;
696
697 if (len == (gsize) (-1))
698 len = strlen (from);
699 if (len == 0)
700 return;
701
702 if (from[i] == '{')
703 i++;
704 if (i >= len)
705 return;
706
707 if (from[i] == 'x')
708 {
709 i++;
710 if (i < len && from[i] == '{')
711 i++;
712 for (; i < len; i++)
713 {
714 if (from[i] >= '0' && from[i] <= '9')
715 c = c * 16 + from[i] - '0';
716 else if (from[i] >= 'a' && from[i] <= 'f')
717 c = c * 16 + 10 + from[i] - 'a';
718 else if (from[i] >= 'A' && from[i] <= 'F')
719 c = c * 16 + 10 + from[i] - 'A';
720 else
721 break;
722 }
723 }
724 else if (from[i] >= '0' && from[i] <= '7')
725 for (; i < len && from[i] >= '0' && from[i] <= '7'; i++)
726 c = c * 8 + from[i] - '0';
727 else
728 {
729 switch (from[i])
730 {
731 case 'n':
732 c = '\n';
733 break;
734 case 't':
735 c = '\t';
736 break;
737 case 'v':
738 c = '\v';
739 break;
740 case 'b':
741 c = '\b';
742 break;
743 case 'r':
744 c = '\r';
745 break;
746 case 'f':
747 c = '\f';
748 break;
749 case 'a':
750 c = '\a';
751 break;
752 default:
753 mc_search_regex__process_append_str (dest_str, from, len, replace_flags);
754 return;
755 }
756 }
757
758 if (c < 0x80 || !is_utf8)
759 g_string_append_c (dest_str, (char) c);
760 else if (c < 0x800)
761 {
762 b = 0xC0 | (c >> 6);
763 g_string_append_c (dest_str, b);
764 b = 0x80 | (c & 0x3F);
765 g_string_append_c (dest_str, b);
766 }
767 else if (c < 0x10000)
768 {
769 b = 0xE0 | (c >> 12);
770 g_string_append_c (dest_str, b);
771 b = 0x80 | ((c >> 6) & 0x3F);
772 g_string_append_c (dest_str, b);
773 b = 0x80 | (c & 0x3F);
774 g_string_append_c (dest_str, b);
775 }
776 else if (c < 0x10FFFF)
777 {
778 b = 0xF0 | (c >> 16);
779 g_string_append_c (dest_str, b);
780 b = 0x80 | ((c >> 12) & 0x3F);
781 g_string_append_c (dest_str, b);
782 b = 0x80 | ((c >> 6) & 0x3F);
783 g_string_append_c (dest_str, b);
784 b = 0x80 | (c & 0x3F);
785 g_string_append_c (dest_str, b);
786 }
787 }
788
789
790
791
792
793 void
794 mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
795 mc_search_cond_t * mc_search_cond)
796 {
797 if (lc_mc_search->whole_words && !lc_mc_search->is_entire_line)
798 {
799
800
801
802 g_string_prepend (mc_search_cond->str, "(?<![\\p{L}\\p{N}_])");
803 g_string_append (mc_search_cond->str, "(?![\\p{L}\\p{N}_])");
804 }
805
806 {
807 #ifdef SEARCH_TYPE_GLIB
808 GError *mcerror = NULL;
809 GRegexCompileFlags g_regex_options = G_REGEX_OPTIMIZE | G_REGEX_DOTALL;
810
811 if (str_isutf8 (charset) && mc_global.utf8_display)
812 {
813 if (!lc_mc_search->is_case_sensitive)
814 g_regex_options |= G_REGEX_CASELESS;
815 }
816 else
817 {
818 g_regex_options |= G_REGEX_RAW;
819
820 if (!lc_mc_search->is_case_sensitive)
821 {
822 GString *tmp;
823
824 tmp = mc_search_cond->str;
825 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
826 g_string_free (tmp, TRUE);
827 }
828 }
829
830 mc_search_cond->regex_handle =
831 g_regex_new (mc_search_cond->str->str, g_regex_options, 0, &mcerror);
832
833 if (mcerror != NULL)
834 {
835 lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
836 g_free (lc_mc_search->error_str);
837 lc_mc_search->error_str =
838 str_conv_gerror_message (mcerror, _("Regular expression error"));
839 g_error_free (mcerror);
840 return;
841 }
842 #else
843
844 #ifdef HAVE_PCRE2
845 int errcode;
846 char error[BUF_SMALL];
847 size_t erroffset;
848 int pcre_options = PCRE2_MULTILINE;
849 #else
850 const char *error;
851 int erroffset;
852 int pcre_options = PCRE_EXTRA | PCRE_MULTILINE;
853 #endif
854
855 if (str_isutf8 (charset) && mc_global.utf8_display)
856 {
857 #ifdef HAVE_PCRE2
858 pcre_options |= PCRE2_UTF;
859 if (!lc_mc_search->is_case_sensitive)
860 pcre_options |= PCRE2_CASELESS;
861 #else
862 pcre_options |= PCRE_UTF8;
863 if (!lc_mc_search->is_case_sensitive)
864 pcre_options |= PCRE_CASELESS;
865 #endif
866 }
867 else if (!lc_mc_search->is_case_sensitive)
868 {
869 GString *tmp;
870
871 tmp = mc_search_cond->str;
872 mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp);
873 g_string_free (tmp, TRUE);
874 }
875
876 mc_search_cond->regex_handle =
877 #ifdef HAVE_PCRE2
878 pcre2_compile ((unsigned char *) mc_search_cond->str->str, PCRE2_ZERO_TERMINATED,
879 pcre_options, &errcode, &erroffset, NULL);
880 #else
881 pcre_compile (mc_search_cond->str->str, pcre_options, &error, &erroffset, NULL);
882 #endif
883 if (mc_search_cond->regex_handle == NULL)
884 {
885 #ifdef HAVE_PCRE2
886 pcre2_get_error_message (errcode, (unsigned char *) error, sizeof (error));
887 #endif
888 mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
889 return;
890 }
891 #ifdef HAVE_PCRE2
892 if (pcre2_jit_compile (mc_search_cond->regex_handle, PCRE2_JIT_COMPLETE) && *error != '\0')
893 #else
894 lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
895 if (lc_mc_search->regex_match_info == NULL && error != NULL)
896 #endif
897 {
898 mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_COMPILE, "%s", error);
899 MC_PTR_FREE (mc_search_cond->regex_handle);
900 return;
901 }
902 #endif
903 }
904
905 lc_mc_search->is_utf8 = str_isutf8 (charset);
906 }
907
908
909
910 gboolean
911 mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
912 gsize start_search, gsize end_search, gsize * found_len)
913 {
914 mc_search_cbret_t ret = MC_SEARCH_CB_NOTFOUND;
915 gsize current_pos, virtual_pos;
916 gint start_pos;
917 gint end_pos;
918
919 if (lc_mc_search->regex_buffer != NULL)
920 g_string_set_size (lc_mc_search->regex_buffer, 0);
921 else
922 lc_mc_search->regex_buffer = g_string_sized_new (64);
923
924 virtual_pos = current_pos = start_search;
925 while (virtual_pos <= end_search)
926 {
927 g_string_set_size (lc_mc_search->regex_buffer, 0);
928 lc_mc_search->start_buffer = current_pos;
929
930 if (lc_mc_search->search_fn != NULL)
931 {
932 while (TRUE)
933 {
934 int current_chr = '\n';
935
936 ret = lc_mc_search->search_fn (user_data, current_pos, ¤t_chr);
937
938 if (ret == MC_SEARCH_CB_ABORT)
939 break;
940
941 if (ret == MC_SEARCH_CB_INVALID)
942 continue;
943
944 current_pos++;
945
946 if (ret == MC_SEARCH_CB_SKIP)
947 continue;
948
949 virtual_pos++;
950
951 g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
952
953 if ((char) current_chr == '\n' || virtual_pos > end_search)
954 break;
955 }
956 }
957 else
958 {
959
960
961
962
963 while (TRUE)
964 {
965 const char current_chr = ((const char *) user_data)[current_pos];
966
967 if (current_chr == '\0')
968 break;
969
970 current_pos++;
971
972 if (current_chr == '\n' || current_pos > end_search)
973 break;
974 }
975
976
977 g_string_append_len (lc_mc_search->regex_buffer, (const char *) user_data + virtual_pos,
978 current_pos - virtual_pos);
979 virtual_pos = current_pos;
980 }
981
982 switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
983 {
984 case COND__FOUND_OK:
985 #ifdef SEARCH_TYPE_GLIB
986 g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
987 #else
988 start_pos = lc_mc_search->iovector[0];
989 end_pos = lc_mc_search->iovector[1];
990 #endif
991 if (found_len != NULL)
992 *found_len = end_pos - start_pos;
993 lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
994 return TRUE;
995 case COND__NOT_ALL_FOUND:
996 break;
997 default:
998 g_string_free (lc_mc_search->regex_buffer, TRUE);
999 lc_mc_search->regex_buffer = NULL;
1000 return FALSE;
1001 }
1002
1003 if ((lc_mc_search->update_fn != NULL) &&
1004 ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
1005 ret = MC_SEARCH_CB_ABORT;
1006
1007 if (ret == MC_SEARCH_CB_ABORT || ret == MC_SEARCH_CB_NOTFOUND)
1008 break;
1009 }
1010
1011 g_string_free (lc_mc_search->regex_buffer, TRUE);
1012 lc_mc_search->regex_buffer = NULL;
1013
1014 MC_PTR_FREE (lc_mc_search->error_str);
1015 lc_mc_search->error = ret == MC_SEARCH_CB_ABORT ? MC_SEARCH_E_ABORT : MC_SEARCH_E_NOTFOUND;
1016
1017 return FALSE;
1018 }
1019
1020
1021
1022 GString *
1023 mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
1024 {
1025 GString *ret;
1026
1027 int num_replace_tokens;
1028 gsize loop;
1029 gsize prev = 0;
1030 replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
1031
1032 num_replace_tokens =
1033 mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
1034
1035 if (lc_mc_search->num_results < 0)
1036 return mc_g_string_dup (replace_str);
1037
1038 if (num_replace_tokens > lc_mc_search->num_results - 1
1039 || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
1040 {
1041 mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_REPLACE, "%s",
1042 _(STR_E_RPL_NOT_EQ_TO_FOUND));
1043 return NULL;
1044 }
1045
1046 ret = g_string_sized_new (64);
1047
1048 for (loop = 0; loop < replace_str->len - 1; loop++)
1049 {
1050 int lc_index;
1051 gchar *tmp_str;
1052 gsize len = 0;
1053
1054 lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
1055
1056 if (lc_index == REPLACE_PREPARE_T_NOTHING_SPECIAL)
1057 {
1058 if (len != 0)
1059 {
1060 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1061 &replace_flags);
1062 mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
1063 &replace_flags);
1064 prev = loop + len;
1065 loop = prev - 1;
1066 }
1067
1068 continue;
1069 }
1070
1071 if (lc_index == REPLACE_PREPARE_T_REPLACE_FLAG)
1072 {
1073 if (loop != 0)
1074 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1075 &replace_flags);
1076 prev = loop + len;
1077 loop = prev - 1;
1078 continue;
1079 }
1080
1081
1082 if (lc_index == REPLACE_PREPARE_T_ESCAPE_SEQ)
1083 {
1084 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1085 &replace_flags);
1086
1087 mc_search_regex__process_escape_sequence (ret, replace_str->str + loop + 1, len - 1,
1088 &replace_flags, lc_mc_search->is_utf8);
1089 prev = loop + len;
1090 loop = prev - 1;
1091 continue;
1092 }
1093
1094
1095 if (lc_index > lc_mc_search->num_results)
1096 {
1097 g_string_free (ret, TRUE);
1098 mc_search_set_error (lc_mc_search, MC_SEARCH_E_REGEX_REPLACE,
1099 _(STR_E_RPL_INVALID_TOKEN), lc_index);
1100 return NULL;
1101 }
1102
1103 tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
1104
1105 if (loop != 0)
1106 mc_search_regex__process_append_str (ret, replace_str->str + prev, loop - prev,
1107 &replace_flags);
1108
1109 mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
1110 g_free (tmp_str);
1111
1112 prev = loop + len;
1113 loop = prev - 1;
1114 }
1115
1116 mc_search_regex__process_append_str (ret, replace_str->str + prev, replace_str->len - prev,
1117 &replace_flags);
1118
1119 return ret;
1120 }