regex.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. Search text engine.
  3. Regex search
  4. Copyright (C) 2009 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software; you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be
  13. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. MA 02110-1301, USA.
  20. */
  21. #include <config.h>
  22. #include <stdlib.h>
  23. #include "lib/global.h"
  24. #include "lib/strutil.h"
  25. #include "lib/search.h"
  26. #include "lib/strescape.h"
  27. #include "src/charsets.h"
  28. #include "internal.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. typedef enum
  33. {
  34. REPLACE_T_NO_TRANSFORM = 0,
  35. REPLACE_T_UPP_TRANSFORM_CHAR = 1,
  36. REPLACE_T_LOW_TRANSFORM_CHAR = 2,
  37. REPLACE_T_UPP_TRANSFORM = 4,
  38. REPLACE_T_LOW_TRANSFORM = 8
  39. } replace_transform_type_t;
  40. /*** file scope variables ************************************************************************/
  41. /*** file scope functions ************************************************************************/
  42. static gboolean
  43. mc_search__regex_str_append_if_special (GString * copy_to, GString * regex_str, gsize * offset)
  44. {
  45. char *tmp_regex_str;
  46. gsize spec_chr_len;
  47. const char **spec_chr;
  48. const char *special_chars[] = {
  49. "\\s", "\\S",
  50. "\\d", "\\D",
  51. "\\b", "\\B",
  52. "\\w", "\\W",
  53. "\\t", "\\n",
  54. "\\r", "\\f",
  55. "\\a", "\\e",
  56. "\\x", "\\X",
  57. "\\c", "\\C",
  58. "\\l", "\\L",
  59. "\\u", "\\U",
  60. "\\E", "\\Q",
  61. NULL
  62. };
  63. spec_chr = special_chars;
  64. tmp_regex_str = &(regex_str->str[*offset]);
  65. while (*spec_chr)
  66. {
  67. spec_chr_len = strlen (*spec_chr);
  68. if (!strncmp (tmp_regex_str, *spec_chr, spec_chr_len))
  69. {
  70. if (!strutils_is_char_escaped (regex_str->str, tmp_regex_str))
  71. {
  72. if (!strncmp ("\\x", *spec_chr, spec_chr_len))
  73. {
  74. if (*(tmp_regex_str + spec_chr_len) == '{')
  75. {
  76. while ((spec_chr_len < regex_str->len - *offset)
  77. && *(tmp_regex_str + spec_chr_len) != '}')
  78. spec_chr_len++;
  79. if (*(tmp_regex_str + spec_chr_len) == '}')
  80. spec_chr_len++;
  81. }
  82. else
  83. spec_chr_len += 2;
  84. }
  85. g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
  86. *offset += spec_chr_len;
  87. return TRUE;
  88. }
  89. }
  90. spec_chr++;
  91. }
  92. return FALSE;
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. static void
  96. mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
  97. const char *one_char, gsize str_len)
  98. {
  99. GString *upp, *low;
  100. gchar *tmp_str;
  101. gsize loop;
  102. upp = mc_search__toupper_case_str (charset, one_char, str_len);
  103. low = mc_search__tolower_case_str (charset, one_char, str_len);
  104. for (loop = 0; loop < upp->len; loop++)
  105. {
  106. if (loop < low->len)
  107. {
  108. if (upp->str[loop] == low->str[loop])
  109. tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
  110. else
  111. tmp_str =
  112. g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp->str[loop],
  113. (unsigned char) low->str[loop]);
  114. }
  115. else
  116. {
  117. tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
  118. }
  119. g_string_append (str_to, tmp_str);
  120. g_free (tmp_str);
  121. }
  122. g_string_free (upp, TRUE);
  123. g_string_free (low, TRUE);
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. static void
  127. mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
  128. GString * str_from)
  129. {
  130. GString *recoded_part;
  131. gsize loop = 0;
  132. recoded_part = g_string_sized_new (32);
  133. while (loop < str_from->len)
  134. {
  135. gchar *one_char;
  136. gsize one_char_len;
  137. gboolean just_letters;
  138. one_char =
  139. mc_search__get_one_symbol (charset, &(str_from->str[loop]),
  140. min (str_from->len - loop, 6), &just_letters);
  141. one_char_len = strlen (one_char);
  142. if (one_char_len == 0)
  143. loop++;
  144. else
  145. {
  146. loop += one_char_len;
  147. if (just_letters)
  148. mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
  149. one_char_len);
  150. else
  151. g_string_append_len (recoded_part, one_char, one_char_len);
  152. }
  153. g_free (one_char);
  154. }
  155. g_string_append (str_to, recoded_part->str);
  156. g_string_free (recoded_part, TRUE);
  157. g_string_set_size (str_from, 0);
  158. }
  159. /* --------------------------------------------------------------------------------------------- */
  160. static GString *
  161. mc_search__cond_struct_new_regex_ci_str (const char *charset, const char *str, gsize str_len)
  162. {
  163. GString *accumulator, *spec_char, *ret_str;
  164. gsize loop;
  165. GString *tmp;
  166. tmp = g_string_new_len (str, str_len);
  167. ret_str = g_string_new ("");
  168. accumulator = g_string_new ("");
  169. spec_char = g_string_new ("");
  170. loop = 0;
  171. while (loop <= str_len)
  172. {
  173. if (mc_search__regex_str_append_if_special (spec_char, tmp, &loop))
  174. {
  175. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  176. g_string_append_len (ret_str, spec_char->str, spec_char->len);
  177. g_string_set_size (spec_char, 0);
  178. continue;
  179. }
  180. if (tmp->str[loop] == '[' && !strutils_is_char_escaped (tmp->str, &(tmp->str[loop])))
  181. {
  182. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  183. while (loop < str_len && !(tmp->str[loop] == ']'
  184. && !strutils_is_char_escaped (tmp->str, &(tmp->str[loop]))))
  185. {
  186. g_string_append_c (ret_str, tmp->str[loop]);
  187. loop++;
  188. }
  189. g_string_append_c (ret_str, tmp->str[loop]);
  190. loop++;
  191. continue;
  192. }
  193. /*
  194. TODO: handle [ and ]
  195. */
  196. g_string_append_c (accumulator, tmp->str[loop]);
  197. loop++;
  198. }
  199. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  200. g_string_free (accumulator, TRUE);
  201. g_string_free (spec_char, TRUE);
  202. g_string_free (tmp, TRUE);
  203. return ret_str;
  204. }
  205. /* --------------------------------------------------------------------------------------------- */
  206. static mc_search__found_cond_t
  207. mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t * regex,
  208. GString * search_str)
  209. {
  210. #ifdef SEARCH_TYPE_GLIB
  211. GError *error = NULL;
  212. if (!g_regex_match_full
  213. (regex, search_str->str, -1, 0, G_REGEX_MATCH_NEWLINE_ANY, &lc_mc_search->regex_match_info,
  214. &error))
  215. {
  216. g_match_info_free (lc_mc_search->regex_match_info);
  217. lc_mc_search->regex_match_info = NULL;
  218. if (error)
  219. {
  220. lc_mc_search->error = MC_SEARCH_E_REGEX;
  221. lc_mc_search->error_str =
  222. str_conv_gerror_message (error, _("Regular expression error"));
  223. g_error_free (error);
  224. return COND__FOUND_ERROR;
  225. }
  226. return COND__NOT_FOUND;
  227. }
  228. lc_mc_search->num_results = g_match_info_get_match_count (lc_mc_search->regex_match_info);
  229. #else /* SEARCH_TYPE_GLIB */
  230. lc_mc_search->num_results = pcre_exec (regex, lc_mc_search->regex_match_info,
  231. search_str->str, search_str->len - 1, 0, 0,
  232. lc_mc_search->iovector, MC_SEARCH__NUM_REPLACE_ARGS);
  233. if (lc_mc_search->num_results < 0)
  234. {
  235. return COND__NOT_FOUND;
  236. }
  237. #endif /* SEARCH_TYPE_GLIB */
  238. return COND__FOUND_OK;
  239. }
  240. /* --------------------------------------------------------------------------------------------- */
  241. static mc_search__found_cond_t
  242. mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
  243. {
  244. gsize loop1;
  245. mc_search_cond_t *mc_search_cond;
  246. mc_search__found_cond_t ret;
  247. for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++)
  248. {
  249. mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
  250. if (!mc_search_cond->regex_handle)
  251. continue;
  252. ret =
  253. mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle,
  254. search_str);
  255. if (ret != COND__NOT_FOUND)
  256. return ret;
  257. }
  258. return COND__NOT_ALL_FOUND;
  259. }
  260. /* --------------------------------------------------------------------------------------------- */
  261. static int
  262. mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
  263. {
  264. int max_token = 0;
  265. gsize loop;
  266. for (loop = 0; loop < len - 1; loop++)
  267. {
  268. if (str[loop] == '\\' && g_ascii_isdigit(str[loop + 1]) )
  269. {
  270. if (strutils_is_char_escaped (str, &str[loop]))
  271. continue;
  272. if (max_token < str[loop + 1] - '0')
  273. max_token = str[loop + 1] - '0';
  274. continue;
  275. }
  276. if (str[loop] == '$' && str[loop + 1] == '{')
  277. {
  278. gsize tmp_len;
  279. char *tmp_str;
  280. int tmp_token;
  281. if (strutils_is_char_escaped (str, &str[loop]))
  282. continue;
  283. for (tmp_len = 0;
  284. loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
  285. tmp_len++);
  286. if (str[loop + 2 + tmp_len] == '}')
  287. {
  288. tmp_str = g_strndup (&str[loop + 2], tmp_len);
  289. tmp_token = atoi (tmp_str);
  290. if (max_token < tmp_token)
  291. max_token = tmp_token;
  292. g_free (tmp_str);
  293. }
  294. }
  295. }
  296. return max_token;
  297. }
  298. /* --------------------------------------------------------------------------------------------- */
  299. static char *
  300. mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
  301. {
  302. int fnd_start = 0, fnd_end = 0;
  303. #ifdef SEARCH_TYPE_GLIB
  304. g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
  305. #else /* SEARCH_TYPE_GLIB */
  306. fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
  307. fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
  308. #endif /* SEARCH_TYPE_GLIB */
  309. if (fnd_end - fnd_start == 0)
  310. return NULL;
  311. return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
  312. }
  313. /* --------------------------------------------------------------------------------------------- */
  314. static int
  315. mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
  316. gsize * skip_len, replace_transform_type_t * replace_flags)
  317. {
  318. int ret = -1;
  319. char *tmp_str;
  320. const char *curr_str = &(replace_str->str[current_pos]);
  321. if (current_pos > replace_str->len)
  322. return -1;
  323. *skip_len = 0;
  324. if (*curr_str == '$' && *(curr_str + 1) == '{' && (*(curr_str + 2) & (char) 0xf0) == 0x30)
  325. {
  326. if (strutils_is_char_escaped (replace_str->str, curr_str))
  327. {
  328. *skip_len = 1;
  329. return -1;
  330. }
  331. for (*skip_len = 0;
  332. current_pos + *skip_len + 2 < replace_str->len
  333. && (*(curr_str + 2 + *skip_len) & (char) 0xf0) == 0x30; (*skip_len)++);
  334. if (*(curr_str + 2 + *skip_len) != '}')
  335. return -1;
  336. tmp_str = g_strndup (curr_str + 2, *skip_len);
  337. if (tmp_str == NULL)
  338. return -1;
  339. ret = atoi (tmp_str);
  340. g_free (tmp_str);
  341. *skip_len += 3; /* ${} */
  342. return ret;
  343. }
  344. if (*curr_str == '\\')
  345. {
  346. if (strutils_is_char_escaped (replace_str->str, curr_str))
  347. {
  348. *skip_len = 1;
  349. return -1;
  350. }
  351. if ( g_ascii_isdigit(*(curr_str + 1)))
  352. {
  353. ret = g_ascii_digit_value (*(curr_str + 1));
  354. *skip_len = 2; /* \\ and one digit */
  355. return ret;
  356. }
  357. ret = -2;
  358. *skip_len += 2;
  359. switch (*(curr_str + 1))
  360. {
  361. case 'U':
  362. *replace_flags |= REPLACE_T_UPP_TRANSFORM;
  363. *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
  364. break;
  365. case 'u':
  366. *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
  367. break;
  368. case 'L':
  369. *replace_flags |= REPLACE_T_LOW_TRANSFORM;
  370. *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
  371. break;
  372. case 'l':
  373. *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
  374. break;
  375. case 'E':
  376. *replace_flags = REPLACE_T_NO_TRANSFORM;
  377. break;
  378. default:
  379. ret = -1;
  380. break;
  381. }
  382. }
  383. return ret;
  384. }
  385. static void
  386. mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
  387. replace_transform_type_t * replace_flags)
  388. {
  389. gsize loop = 0;
  390. gsize char_len;
  391. char *tmp_str;
  392. GString *tmp_string;
  393. if (len == (gsize) - 1)
  394. len = strlen (from);
  395. if (*replace_flags == REPLACE_T_NO_TRANSFORM)
  396. {
  397. g_string_append_len (dest_str, from, len);
  398. return;
  399. }
  400. while (loop < len)
  401. {
  402. tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
  403. char_len = strlen (tmp_str);
  404. if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR)
  405. {
  406. *replace_flags &= !REPLACE_T_UPP_TRANSFORM_CHAR;
  407. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  408. g_string_append (dest_str, tmp_string->str);
  409. g_string_free (tmp_string, TRUE);
  410. }
  411. else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR)
  412. {
  413. *replace_flags &= !REPLACE_T_LOW_TRANSFORM_CHAR;
  414. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  415. g_string_append (dest_str, tmp_string->str);
  416. g_string_free (tmp_string, TRUE);
  417. }
  418. else if (*replace_flags & REPLACE_T_UPP_TRANSFORM)
  419. {
  420. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  421. g_string_append (dest_str, tmp_string->str);
  422. g_string_free (tmp_string, TRUE);
  423. }
  424. else if (*replace_flags & REPLACE_T_LOW_TRANSFORM)
  425. {
  426. tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
  427. g_string_append (dest_str, tmp_string->str);
  428. g_string_free (tmp_string, TRUE);
  429. }
  430. else
  431. {
  432. g_string_append (dest_str, tmp_str);
  433. }
  434. g_free (tmp_str);
  435. loop += char_len;
  436. }
  437. }
  438. /*** public functions ****************************************************************************/
  439. void
  440. mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
  441. mc_search_cond_t * mc_search_cond)
  442. {
  443. GString *tmp = NULL;
  444. #ifdef SEARCH_TYPE_GLIB
  445. GError *error = NULL;
  446. #else /* SEARCH_TYPE_GLIB */
  447. const char *error;
  448. int erroffset;
  449. #endif /* SEARCH_TYPE_GLIB */
  450. if (!lc_mc_search->is_case_sensitive)
  451. {
  452. tmp = g_string_new_len (mc_search_cond->str->str, mc_search_cond->str->len);
  453. g_string_free (mc_search_cond->str, TRUE);
  454. mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp->str, tmp->len);
  455. g_string_free (tmp, TRUE);
  456. }
  457. #ifdef SEARCH_TYPE_GLIB
  458. mc_search_cond->regex_handle =
  459. g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL, 0,
  460. &error);
  461. if (error != NULL)
  462. {
  463. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  464. lc_mc_search->error_str = str_conv_gerror_message (error, _("Regular expression error"));
  465. g_error_free (error);
  466. return;
  467. }
  468. #else /* SEARCH_TYPE_GLIB */
  469. mc_search_cond->regex_handle =
  470. pcre_compile (mc_search_cond->str->str, PCRE_EXTRA, &error, &erroffset, NULL);
  471. if (mc_search_cond->regex_handle == NULL)
  472. {
  473. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  474. lc_mc_search->error_str = g_strdup (error);
  475. return;
  476. }
  477. lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
  478. if (lc_mc_search->regex_match_info == NULL)
  479. {
  480. if (error)
  481. {
  482. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  483. lc_mc_search->error_str = g_strdup (error);
  484. g_free (mc_search_cond->regex_handle);
  485. mc_search_cond->regex_handle = NULL;
  486. return;
  487. }
  488. }
  489. #endif /* SEARCH_TYPE_GLIB */
  490. }
  491. /* --------------------------------------------------------------------------------------------- */
  492. gboolean
  493. mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
  494. gsize start_search, gsize end_search, gsize * found_len)
  495. {
  496. gsize current_pos, virtual_pos;
  497. int current_chr = 0;
  498. gint start_pos;
  499. gint end_pos;
  500. if (lc_mc_search->regex_buffer != NULL)
  501. g_string_free (lc_mc_search->regex_buffer, TRUE);
  502. lc_mc_search->regex_buffer = g_string_new ("");
  503. virtual_pos = current_pos = start_search;
  504. while (virtual_pos <= end_search)
  505. {
  506. g_string_set_size (lc_mc_search->regex_buffer, 0);
  507. lc_mc_search->start_buffer = current_pos;
  508. while (1)
  509. {
  510. current_chr = mc_search__get_char (lc_mc_search, user_data, current_pos);
  511. if (current_chr == MC_SEARCH_CB_ABORT)
  512. break;
  513. current_pos++;
  514. if (current_chr == MC_SEARCH_CB_SKIP)
  515. continue;
  516. virtual_pos++;
  517. g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
  518. if (current_chr == 0 || (char) current_chr == '\n')
  519. break;
  520. if (virtual_pos > end_search)
  521. break;
  522. }
  523. switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer))
  524. {
  525. case COND__FOUND_OK:
  526. #ifdef SEARCH_TYPE_GLIB
  527. g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
  528. #else /* SEARCH_TYPE_GLIB */
  529. start_pos = lc_mc_search->iovector[0];
  530. end_pos = lc_mc_search->iovector[1];
  531. #endif /* SEARCH_TYPE_GLIB */
  532. if (found_len)
  533. *found_len = end_pos - start_pos;
  534. lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
  535. return TRUE;
  536. case COND__NOT_ALL_FOUND:
  537. break;
  538. default:
  539. g_string_free (lc_mc_search->regex_buffer, TRUE);
  540. lc_mc_search->regex_buffer = NULL;
  541. return FALSE;
  542. }
  543. if ((lc_mc_search->update_fn != NULL) &&
  544. ((lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
  545. current_chr = MC_SEARCH_CB_ABORT;
  546. if (current_chr == MC_SEARCH_CB_ABORT)
  547. break;
  548. }
  549. g_string_free (lc_mc_search->regex_buffer, TRUE);
  550. lc_mc_search->regex_buffer = NULL;
  551. lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
  552. if (current_chr != MC_SEARCH_CB_ABORT)
  553. lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
  554. else
  555. lc_mc_search->error_str = NULL;
  556. return FALSE;
  557. }
  558. /* --------------------------------------------------------------------------------------------- */
  559. GString *
  560. mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  561. {
  562. GString *ret;
  563. gchar *tmp_str;
  564. int num_replace_tokens, lc_index;
  565. gsize loop;
  566. gsize len = 0;
  567. gchar *prev_str;
  568. replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
  569. num_replace_tokens =
  570. mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
  571. if (lc_mc_search->num_results < 0)
  572. return g_string_new_len (replace_str->str, replace_str->len);
  573. if (num_replace_tokens > lc_mc_search->num_results - 1
  574. || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS)
  575. {
  576. lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
  577. lc_mc_search->error_str = g_strdup (_(STR_E_RPL_NOT_EQ_TO_FOUND));
  578. return NULL;
  579. }
  580. ret = g_string_new ("");
  581. prev_str = replace_str->str;
  582. for (loop = 0; loop < replace_str->len - 1; loop++)
  583. {
  584. lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
  585. if (lc_index == -1)
  586. {
  587. if (len != 0)
  588. {
  589. mc_search_regex__process_append_str (ret, prev_str,
  590. replace_str->str - prev_str + loop,
  591. &replace_flags);
  592. mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
  593. &replace_flags);
  594. prev_str = replace_str->str + loop + len;
  595. loop += len - 1;
  596. }
  597. continue;
  598. }
  599. if (lc_index == -2)
  600. {
  601. if (loop)
  602. mc_search_regex__process_append_str (ret, prev_str,
  603. replace_str->str - prev_str + loop,
  604. &replace_flags);
  605. prev_str = replace_str->str + loop + len;
  606. loop += len - 1;
  607. continue;
  608. }
  609. if (lc_index > lc_mc_search->num_results)
  610. {
  611. g_string_free (ret, TRUE);
  612. lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
  613. lc_mc_search->error_str = g_strdup_printf (_(STR_E_RPL_INVALID_TOKEN), lc_index);
  614. return NULL;
  615. }
  616. tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
  617. if (tmp_str == NULL)
  618. continue;
  619. if (loop)
  620. mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
  621. &replace_flags);
  622. prev_str = replace_str->str + loop + len;
  623. mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
  624. g_free (tmp_str);
  625. loop += len - 1;
  626. }
  627. mc_search_regex__process_append_str (ret, prev_str,
  628. replace_str->str - prev_str + replace_str->len,
  629. &replace_flags);
  630. return ret;
  631. }